xref: /titanic_52/usr/src/uts/common/io/gldutil.c (revision 605445d5657096e69d948ccb554c9ff024fa34df)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*605445d5Sdg199075  * Common Development and Distribution License (the "License").
6*605445d5Sdg199075  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  *
21*605445d5Sdg199075  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
227c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
237c478bd9Sstevel@tonic-gate  */
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate  * gld - Generic LAN Driver
297c478bd9Sstevel@tonic-gate  * media dependent routines
307c478bd9Sstevel@tonic-gate  */
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include <sys/types.h>
337c478bd9Sstevel@tonic-gate #include <sys/errno.h>
347c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
357c478bd9Sstevel@tonic-gate #include <sys/stream.h>
367c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
377c478bd9Sstevel@tonic-gate #include <sys/stat.h>
387c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
397c478bd9Sstevel@tonic-gate #include <sys/kstat.h>
407c478bd9Sstevel@tonic-gate #include <sys/debug.h>
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #include <sys/byteorder.h>
437c478bd9Sstevel@tonic-gate #include <sys/strsun.h>
447c478bd9Sstevel@tonic-gate #include <sys/dlpi.h>
457c478bd9Sstevel@tonic-gate #include <sys/ethernet.h>
467c478bd9Sstevel@tonic-gate #include <sys/multidata.h>
477c478bd9Sstevel@tonic-gate #include <sys/gld.h>
487c478bd9Sstevel@tonic-gate #include <sys/gldpriv.h>
497c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
507c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
517c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
527c478bd9Sstevel@tonic-gate #include <sys/ib/clients/ibd/ibd.h>
537c478bd9Sstevel@tonic-gate #include <sys/pattr.h>
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate #define	DLSAPLENGTH(macinfo) \
567c478bd9Sstevel@tonic-gate 	((macinfo)->gldm_addrlen + ABS((macinfo)->gldm_saplen))
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate #ifdef GLD_DEBUG
597c478bd9Sstevel@tonic-gate extern int gld_debug;
607c478bd9Sstevel@tonic-gate #endif
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate extern void gld_bitrevcopy(caddr_t src, caddr_t target, size_t n);
637c478bd9Sstevel@tonic-gate extern char *gld_macaddr_sprintf(char *, unsigned char *, int);
647c478bd9Sstevel@tonic-gate extern gld_vlan_t *gld_find_vlan(gld_mac_info_t *, uint32_t);
657c478bd9Sstevel@tonic-gate extern uint32_t gld_global_options;
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate static struct	llc_snap_hdr llc_snap_def = {
687c478bd9Sstevel@tonic-gate 	LSAP_SNAP,		/* DLSAP 0xaa */
697c478bd9Sstevel@tonic-gate 	LSAP_SNAP,		/* SLSAP 0xaa */
707c478bd9Sstevel@tonic-gate 	CNTL_LLC_UI,		/* Control 0x03 */
717c478bd9Sstevel@tonic-gate 	0x00, 0x00, 0x00,	/* Org[3] */
727c478bd9Sstevel@tonic-gate 	0x00			/* Type */
737c478bd9Sstevel@tonic-gate };
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate #define	ISETHERTYPE(snaphdr) \
767c478bd9Sstevel@tonic-gate 	(snaphdr->d_lsap == LSAP_SNAP && \
777c478bd9Sstevel@tonic-gate 	snaphdr->s_lsap == LSAP_SNAP && \
787c478bd9Sstevel@tonic-gate 	snaphdr->control == CNTL_LLC_UI && \
797c478bd9Sstevel@tonic-gate 	snaphdr->org[0] == 0 && \
807c478bd9Sstevel@tonic-gate 	snaphdr->org[1] == 0 && \
817c478bd9Sstevel@tonic-gate 	snaphdr->org[2] == 0)
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate /* ======== */
847c478bd9Sstevel@tonic-gate /* Ethernet */
857c478bd9Sstevel@tonic-gate /* ======== */
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate static mac_addr_t ether_broadcast = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate void
907c478bd9Sstevel@tonic-gate gld_init_ether(gld_mac_info_t *macinfo)
917c478bd9Sstevel@tonic-gate {
927c478bd9Sstevel@tonic-gate 	struct gldkstats *sp =
937c478bd9Sstevel@tonic-gate 	    ((gld_mac_pvt_t *)macinfo->gldm_mac_pvt)->kstatp->ks_data;
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate 	/* Assumptions we make for this medium */
967c478bd9Sstevel@tonic-gate 	ASSERT(macinfo->gldm_type == DL_ETHER);
977c478bd9Sstevel@tonic-gate 	ASSERT(macinfo->gldm_addrlen == 6);
987c478bd9Sstevel@tonic-gate 	ASSERT(macinfo->gldm_saplen == -2);
997c478bd9Sstevel@tonic-gate #ifndef	lint
100*605445d5Sdg199075 	ASSERT(sizeof (struct ether_header) == 14);
1017c478bd9Sstevel@tonic-gate 	ASSERT(sizeof (mac_addr_t) == 6);
1027c478bd9Sstevel@tonic-gate #endif
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 	kstat_named_init(&sp->glds_frame, "align_errors", KSTAT_DATA_ULONG);
1057c478bd9Sstevel@tonic-gate 	kstat_named_init(&sp->glds_crc, "fcs_errors", KSTAT_DATA_ULONG);
1067c478bd9Sstevel@tonic-gate 	kstat_named_init(&sp->glds_collisions, "collisions", KSTAT_DATA_ULONG);
1077c478bd9Sstevel@tonic-gate 	kstat_named_init(&sp->glds_nocarrier, "carrier_errors",
1087c478bd9Sstevel@tonic-gate 	    KSTAT_DATA_ULONG);
1097c478bd9Sstevel@tonic-gate 	kstat_named_init(&sp->glds_defer, "defer_xmts", KSTAT_DATA_ULONG);
1107c478bd9Sstevel@tonic-gate 	kstat_named_init(&sp->glds_xmtlatecoll, "tx_late_collisions",
1117c478bd9Sstevel@tonic-gate 					KSTAT_DATA_ULONG);
1127c478bd9Sstevel@tonic-gate 	kstat_named_init(&sp->glds_short, "runt_errors", KSTAT_DATA_ULONG);
1137c478bd9Sstevel@tonic-gate 	kstat_named_init(&sp->glds_excoll, "ex_collisions", KSTAT_DATA_ULONG);
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 	/*
1167c478bd9Sstevel@tonic-gate 	 * only initialize the new statistics if the driver
1177c478bd9Sstevel@tonic-gate 	 * knows about them.
1187c478bd9Sstevel@tonic-gate 	 */
1197c478bd9Sstevel@tonic-gate 	if (macinfo->gldm_driver_version != GLD_VERSION_200)
1207c478bd9Sstevel@tonic-gate 		return;
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 	kstat_named_init(&sp->glds_dot3_first_coll,
1237c478bd9Sstevel@tonic-gate 	    "first_collisions", KSTAT_DATA_UINT32);
1247c478bd9Sstevel@tonic-gate 	kstat_named_init(&sp->glds_dot3_multi_coll,
1257c478bd9Sstevel@tonic-gate 	    "multi_collisions", KSTAT_DATA_UINT32);
1267c478bd9Sstevel@tonic-gate 	kstat_named_init(&sp->glds_dot3_sqe_error,
1277c478bd9Sstevel@tonic-gate 	    "sqe_errors", KSTAT_DATA_UINT32);
1287c478bd9Sstevel@tonic-gate 	kstat_named_init(&sp->glds_dot3_mac_xmt_error,
1297c478bd9Sstevel@tonic-gate 	    "macxmt_errors", KSTAT_DATA_UINT32);
1307c478bd9Sstevel@tonic-gate 	kstat_named_init(&sp->glds_dot3_mac_rcv_error,
1317c478bd9Sstevel@tonic-gate 	    "macrcv_errors", KSTAT_DATA_UINT32);
1327c478bd9Sstevel@tonic-gate 	kstat_named_init(&sp->glds_dot3_frame_too_long,
1337c478bd9Sstevel@tonic-gate 	    "toolong_errors", KSTAT_DATA_UINT32);
1347c478bd9Sstevel@tonic-gate 	kstat_named_init(&sp->glds_duplex, "duplex", KSTAT_DATA_CHAR);
1357c478bd9Sstevel@tonic-gate }
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1387c478bd9Sstevel@tonic-gate void
1397c478bd9Sstevel@tonic-gate gld_uninit_ether(gld_mac_info_t *macinfo)
1407c478bd9Sstevel@tonic-gate {
1417c478bd9Sstevel@tonic-gate }
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate int
1447c478bd9Sstevel@tonic-gate gld_interpret_ether(gld_mac_info_t *macinfo, mblk_t *mp, pktinfo_t *pktinfo,
1457c478bd9Sstevel@tonic-gate     packet_flag_t flags)
1467c478bd9Sstevel@tonic-gate {
147*605445d5Sdg199075 	struct ether_header *mh;
1487c478bd9Sstevel@tonic-gate 	gld_mac_pvt_t *mac_pvt = (gld_mac_pvt_t *)macinfo->gldm_mac_pvt;
1497c478bd9Sstevel@tonic-gate 	struct llc_snap_hdr *snaphdr;
150*605445d5Sdg199075 	mblk_t *pmp = NULL, *savemp = mp;
1517c478bd9Sstevel@tonic-gate 	unsigned short typelen;
152*605445d5Sdg199075 	int ret = 0;
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 	/*
1557c478bd9Sstevel@tonic-gate 	 * Quickly handle receive fastpath for IPQ hack.
1567c478bd9Sstevel@tonic-gate 	 */
1577c478bd9Sstevel@tonic-gate 	if (flags == GLD_RXQUICK) {
1587c478bd9Sstevel@tonic-gate 		pktinfo->pktLen = msgdsize(mp);
1597c478bd9Sstevel@tonic-gate 		/*
1607c478bd9Sstevel@tonic-gate 		 * Check whether the header is contiguous, which
1617c478bd9Sstevel@tonic-gate 		 * also implicitly makes sure the packet is big enough.
1627c478bd9Sstevel@tonic-gate 		 */
163*605445d5Sdg199075 		if (MBLKL(mp) < sizeof (struct ether_header))
1647c478bd9Sstevel@tonic-gate 			return (-1);
165*605445d5Sdg199075 		mh = (struct ether_header *)mp->b_rptr;
1667c478bd9Sstevel@tonic-gate 		pktinfo->ethertype = REF_NET_USHORT(mh->ether_type);
167*605445d5Sdg199075 		pktinfo->isForMe = mac_eq(&mh->ether_dhost,
1687c478bd9Sstevel@tonic-gate 		    mac_pvt->curr_macaddr, macinfo->gldm_addrlen);
169*605445d5Sdg199075 		pktinfo->macLen = sizeof (struct ether_header);
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 		return (0);
1727c478bd9Sstevel@tonic-gate 	}
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 	bzero((void *)pktinfo, sizeof (*pktinfo));
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	pktinfo->pktLen = msgdsize(mp);
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 	/* make sure packet has at least a whole mac header */
179*605445d5Sdg199075 	if (pktinfo->pktLen < sizeof (struct ether_header))
1807c478bd9Sstevel@tonic-gate 		return (-1);
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	/* make sure the mac header falls into contiguous memory */
183*605445d5Sdg199075 	if (MBLKL(mp) < sizeof (struct ether_header)) {
1847c478bd9Sstevel@tonic-gate 		if ((pmp = msgpullup(mp, -1)) == NULL) {
1857c478bd9Sstevel@tonic-gate #ifdef GLD_DEBUG
1867c478bd9Sstevel@tonic-gate 			if (gld_debug & GLDERRS)
1877c478bd9Sstevel@tonic-gate 				cmn_err(CE_WARN,
1887c478bd9Sstevel@tonic-gate 				    "GLD: interpret_ether cannot msgpullup");
1897c478bd9Sstevel@tonic-gate #endif
1907c478bd9Sstevel@tonic-gate 			return (-1);
1917c478bd9Sstevel@tonic-gate 		}
1927c478bd9Sstevel@tonic-gate 		mp = pmp;	/* this mblk contains the whole mac header */
1937c478bd9Sstevel@tonic-gate 	}
1947c478bd9Sstevel@tonic-gate 
195*605445d5Sdg199075 	mh = (struct ether_header *)mp->b_rptr;
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	/* Check to see if the mac is a broadcast or multicast address. */
198*605445d5Sdg199075 	if (mac_eq(&mh->ether_dhost, ether_broadcast, macinfo->gldm_addrlen))
1997c478bd9Sstevel@tonic-gate 		pktinfo->isBroadcast = 1;
200*605445d5Sdg199075 	else if (mh->ether_dhost.ether_addr_octet[0] & 1)
2017c478bd9Sstevel@tonic-gate 		pktinfo->isMulticast = 1;
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	typelen = REF_NET_USHORT(mh->ether_type);
2047c478bd9Sstevel@tonic-gate 	/*
2057c478bd9Sstevel@tonic-gate 	 * If the hardware is capable of VLAN tag insertion
2067c478bd9Sstevel@tonic-gate 	 * strip out the VLAN tag info. Knowing hardware is
2077c478bd9Sstevel@tonic-gate 	 * capable of VLAN can be established by the presance
2087c478bd9Sstevel@tonic-gate 	 * of non null 'macinfo->gldm_send_tagged'.
2097c478bd9Sstevel@tonic-gate 	 */
2107c478bd9Sstevel@tonic-gate 	if (flags == GLD_TX) {
211*605445d5Sdg199075 		if ((typelen == ETHERTYPE_VLAN) &&
2127c478bd9Sstevel@tonic-gate 		    (macinfo->gldm_send_tagged != NULL)) {
213*605445d5Sdg199075 			struct ether_vlan_header *evhp;
214*605445d5Sdg199075 			uint16_t tci;
215*605445d5Sdg199075 
216*605445d5Sdg199075 			if ((MBLKL(mp) < sizeof (struct ether_vlan_header)) &&
217*605445d5Sdg199075 			    (pullupmsg(mp, sizeof (struct ether_vlan_header))
218*605445d5Sdg199075 			    == 0)) {
219*605445d5Sdg199075 				ret = -1;
220*605445d5Sdg199075 				goto out;
221*605445d5Sdg199075 			}
222*605445d5Sdg199075 			evhp = (struct ether_vlan_header *)mp->b_rptr;
223*605445d5Sdg199075 			tci = REF_NET_USHORT(evhp->ether_tci);
224*605445d5Sdg199075 
225*605445d5Sdg199075 			/*
226*605445d5Sdg199075 			 * We don't allow the VID and priority are both zero.
227*605445d5Sdg199075 			 */
228*605445d5Sdg199075 			if ((GLD_VTAG_PRI((int32_t)tci) == 0 &&
229*605445d5Sdg199075 			    GLD_VTAG_VID((int32_t)tci) == VLAN_VID_NONE) ||
230*605445d5Sdg199075 			    (GLD_VTAG_CFI((uint32_t)tci)) != VLAN_CFI_ETHER) {
231*605445d5Sdg199075 				ret = -1;
232*605445d5Sdg199075 				goto out;
233*605445d5Sdg199075 			}
234*605445d5Sdg199075 
235*605445d5Sdg199075 			/*
236*605445d5Sdg199075 			 * Remember the VTAG info in order to reinsert it,
237*605445d5Sdg199075 			 * Then strip the tag. This is required because some
238*605445d5Sdg199075 			 * drivers do not allow the size of message (passed
239*605445d5Sdg199075 			 * by the gldm_send_tagged() function) to be greater
240*605445d5Sdg199075 			 * than ETHERMAX.
241*605445d5Sdg199075 			 */
242*605445d5Sdg199075 			GLD_SAVE_MBLK_VTAG(savemp, GLD_TCI2VTAG(tci));
243*605445d5Sdg199075 			ovbcopy(mp->b_rptr, mp->b_rptr + VTAG_SIZE,
2447c478bd9Sstevel@tonic-gate 			    2 * ETHERADDRL);
2457c478bd9Sstevel@tonic-gate 			mp->b_rptr += VTAG_SIZE;
2467c478bd9Sstevel@tonic-gate 		}
2477c478bd9Sstevel@tonic-gate 		goto out;	/* Got all info we need for xmit case */
2487c478bd9Sstevel@tonic-gate 	}
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 	ASSERT(GLDM_LOCK_HELD(macinfo));
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 	/*
2537c478bd9Sstevel@tonic-gate 	 * Deal with the mac header
2547c478bd9Sstevel@tonic-gate 	 */
2557c478bd9Sstevel@tonic-gate 
256*605445d5Sdg199075 	mac_copy(&mh->ether_dhost, pktinfo->dhost, macinfo->gldm_addrlen);
257*605445d5Sdg199075 	mac_copy(&mh->ether_shost, pktinfo->shost, macinfo->gldm_addrlen);
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 	pktinfo->isLooped = mac_eq(pktinfo->shost,
2607c478bd9Sstevel@tonic-gate 	    mac_pvt->curr_macaddr, macinfo->gldm_addrlen);
2617c478bd9Sstevel@tonic-gate 	pktinfo->isForMe = mac_eq(pktinfo->dhost,
2627c478bd9Sstevel@tonic-gate 	    mac_pvt->curr_macaddr, macinfo->gldm_addrlen);
2637c478bd9Sstevel@tonic-gate 
264*605445d5Sdg199075 	pktinfo->macLen = sizeof (struct ether_header);
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 	if (typelen > ETHERMTU) {
2677c478bd9Sstevel@tonic-gate 		pktinfo->ethertype = typelen; /* use type interpretation */
2687c478bd9Sstevel@tonic-gate 		goto out;
2697c478bd9Sstevel@tonic-gate 	}
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 	/*
2727c478bd9Sstevel@tonic-gate 	 * Packet is 802.3 so the ether type/length field
2737c478bd9Sstevel@tonic-gate 	 * specifies the number of bytes that should be present
2747c478bd9Sstevel@tonic-gate 	 * in the data field.  Additional bytes are padding, and
2757c478bd9Sstevel@tonic-gate 	 * should be removed
2767c478bd9Sstevel@tonic-gate 	 */
2777c478bd9Sstevel@tonic-gate 	{
2787c478bd9Sstevel@tonic-gate 	int delta = pktinfo->pktLen -
279*605445d5Sdg199075 	    (sizeof (struct ether_header) + typelen);
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 	if (delta > 0 && adjmsg(mp, -delta))
2827c478bd9Sstevel@tonic-gate 		pktinfo->pktLen -= delta;
2837c478bd9Sstevel@tonic-gate 	}
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate 	/*
2867c478bd9Sstevel@tonic-gate 	 * Before trying to look beyond the MAC header, make sure the LLC
2877c478bd9Sstevel@tonic-gate 	 * header exists, and that both it and any SNAP header are contiguous.
2887c478bd9Sstevel@tonic-gate 	 */
2897c478bd9Sstevel@tonic-gate 	if (pktinfo->pktLen < pktinfo->macLen + LLC_HDR1_LEN)
2907c478bd9Sstevel@tonic-gate 		goto out;	/* LLC hdr should have been there! */
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 	pktinfo->isLLC = 1;
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 	if (gld_global_options & GLD_OPT_NO_ETHRXSNAP ||
2957c478bd9Sstevel@tonic-gate 	    pktinfo->pktLen <  pktinfo->macLen + LLC_SNAP_HDR_LEN)
2967c478bd9Sstevel@tonic-gate 		goto out;
2977c478bd9Sstevel@tonic-gate 
298*605445d5Sdg199075 	if (MBLKL(mp) < sizeof (struct ether_header) + LLC_SNAP_HDR_LEN &&
2997c478bd9Sstevel@tonic-gate 	    MBLKL(mp) < pktinfo->pktLen) {
3007c478bd9Sstevel@tonic-gate 		/*
3017c478bd9Sstevel@tonic-gate 		 * we don't have the entire packet within the first mblk (and
3027c478bd9Sstevel@tonic-gate 		 * therefore we didn't do the msgpullup above), AND the first
3037c478bd9Sstevel@tonic-gate 		 * mblk may not contain all the data we need to look at.
3047c478bd9Sstevel@tonic-gate 		 */
3057c478bd9Sstevel@tonic-gate 		ASSERT(pmp == NULL);	/* couldn't have done msgpullup above */
3067c478bd9Sstevel@tonic-gate 		if ((pmp = msgpullup(mp, -1)) == NULL) {
3077c478bd9Sstevel@tonic-gate #ifdef GLD_DEBUG
3087c478bd9Sstevel@tonic-gate 			if (gld_debug & GLDERRS)
3097c478bd9Sstevel@tonic-gate 				cmn_err(CE_WARN,
3107c478bd9Sstevel@tonic-gate 				    "GLD: interpret_ether cannot msgpullup2");
3117c478bd9Sstevel@tonic-gate #endif
3127c478bd9Sstevel@tonic-gate 			goto out;	/* can't interpret this pkt further */
3137c478bd9Sstevel@tonic-gate 		}
3147c478bd9Sstevel@tonic-gate 		mp = pmp;	/* this mblk should contain everything needed */
3157c478bd9Sstevel@tonic-gate 	}
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 	/*
3187c478bd9Sstevel@tonic-gate 	 * Check SAP/SNAP information for EtherType.
3197c478bd9Sstevel@tonic-gate 	 */
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate 	snaphdr = (struct llc_snap_hdr *)(mp->b_rptr + pktinfo->macLen);
3227c478bd9Sstevel@tonic-gate 	if (ISETHERTYPE(snaphdr)) {
3237c478bd9Sstevel@tonic-gate 		pktinfo->ethertype = REF_NET_USHORT(snaphdr->type);
3247c478bd9Sstevel@tonic-gate 		pktinfo->hdrLen = LLC_SNAP_HDR_LEN;
3257c478bd9Sstevel@tonic-gate 	}
3267c478bd9Sstevel@tonic-gate out:
3277c478bd9Sstevel@tonic-gate 	if (pmp != NULL)
3287c478bd9Sstevel@tonic-gate 		freemsg(pmp);
3297c478bd9Sstevel@tonic-gate 
330*605445d5Sdg199075 	return (ret);
3317c478bd9Sstevel@tonic-gate }
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate mblk_t *
3347c478bd9Sstevel@tonic-gate gld_unitdata_ether(gld_t *gld, mblk_t *mp)
3357c478bd9Sstevel@tonic-gate {
3367c478bd9Sstevel@tonic-gate 	gld_mac_info_t *macinfo = gld->gld_mac_info;
3377c478bd9Sstevel@tonic-gate 	dl_unitdata_req_t *dlp = (dl_unitdata_req_t *)mp->b_rptr;
3387c478bd9Sstevel@tonic-gate 	struct gld_dlsap *gldp = DLSAP(dlp, dlp->dl_dest_addr_offset);
3397c478bd9Sstevel@tonic-gate 	mac_addr_t dhost;
3407c478bd9Sstevel@tonic-gate 	unsigned short typelen;
3417c478bd9Sstevel@tonic-gate 	mblk_t *nmp;
342*605445d5Sdg199075 	struct ether_header *mh;
3437c478bd9Sstevel@tonic-gate 	int hdrlen;
3447c478bd9Sstevel@tonic-gate 	uint32_t vptag;
3457c478bd9Sstevel@tonic-gate 	gld_vlan_t *gld_vlan;
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 	ASSERT(macinfo);
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 	/* extract needed info from the mblk before we maybe reuse it */
3507c478bd9Sstevel@tonic-gate 	mac_copy(gldp->glda_addr, dhost, macinfo->gldm_addrlen);
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 	/* look in the unitdata request for a sap, else use bound one */
3537c478bd9Sstevel@tonic-gate 	if (dlp->dl_dest_addr_length >= DLSAPLENGTH(macinfo) &&
3547c478bd9Sstevel@tonic-gate 	    REF_HOST_USHORT(gldp->glda_sap) != 0)
3557c478bd9Sstevel@tonic-gate 		typelen = REF_HOST_USHORT(gldp->glda_sap);
3567c478bd9Sstevel@tonic-gate 	else
3577c478bd9Sstevel@tonic-gate 		typelen = gld->gld_sap;
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 	/*
3607c478bd9Sstevel@tonic-gate 	 * We take values less than or equal to ETHERMTU to mean that the
3617c478bd9Sstevel@tonic-gate 	 * packet should not have an encoded EtherType and so we use the
3627c478bd9Sstevel@tonic-gate 	 * IEEE 802.3 length interpretation of the type/length field.
3637c478bd9Sstevel@tonic-gate 	 */
3647c478bd9Sstevel@tonic-gate 	if (typelen <= ETHERMTU)
3657c478bd9Sstevel@tonic-gate 		typelen = msgdsize(mp);
3667c478bd9Sstevel@tonic-gate 
367*605445d5Sdg199075 	hdrlen = sizeof (struct ether_header);
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate 	/*
3707c478bd9Sstevel@tonic-gate 	 * Check to see if VLAN is enabled on this stream
3717c478bd9Sstevel@tonic-gate 	 * if so then make the header bigger to hold a clone
3727c478bd9Sstevel@tonic-gate 	 * vlan tag.
3737c478bd9Sstevel@tonic-gate 	 */
3747c478bd9Sstevel@tonic-gate 	gld_vlan = (gld_vlan_t *)gld->gld_vlan;
3757c478bd9Sstevel@tonic-gate 	if (gld_vlan && (gld_vlan->gldv_id != VLAN_VID_NONE)) {
3767c478bd9Sstevel@tonic-gate 		hdrlen += VTAG_SIZE;
3777c478bd9Sstevel@tonic-gate 		vptag = gld_vlan->gldv_ptag;
3787c478bd9Sstevel@tonic-gate 	}
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 	/* need a buffer big enough for the headers */
3817c478bd9Sstevel@tonic-gate 	nmp = mp->b_cont;	/* where the packet payload M_DATA is */
3827c478bd9Sstevel@tonic-gate 	if (DB_REF(nmp) == 1 && MBLKHEAD(nmp) >= hdrlen) {
3837c478bd9Sstevel@tonic-gate 		/* it fits at the beginning of the first M_DATA block */
3847c478bd9Sstevel@tonic-gate 		freeb(mp);	/* don't need the M_PROTO anymore */
3857c478bd9Sstevel@tonic-gate 	} else if (DB_REF(mp) == 1 && MBLKSIZE(mp) >= hdrlen) {
3867c478bd9Sstevel@tonic-gate 		/* we can reuse the dl_unitdata_req M_PROTO mblk */
3877c478bd9Sstevel@tonic-gate 		nmp = mp;
3887c478bd9Sstevel@tonic-gate 		DB_TYPE(nmp) = M_DATA;
3897c478bd9Sstevel@tonic-gate 		nmp->b_rptr = nmp->b_wptr = DB_LIM(nmp);
3907c478bd9Sstevel@tonic-gate 	} else {
3917c478bd9Sstevel@tonic-gate 		/* we need to allocate one */
3927c478bd9Sstevel@tonic-gate 		if ((nmp = allocb(hdrlen, BPRI_MED)) == NULL)
3937c478bd9Sstevel@tonic-gate 			return (NULL);
3947c478bd9Sstevel@tonic-gate 		nmp->b_rptr = nmp->b_wptr = DB_LIM(nmp);
3957c478bd9Sstevel@tonic-gate 		linkb(nmp, mp->b_cont);
3967c478bd9Sstevel@tonic-gate 		freeb(mp);
3977c478bd9Sstevel@tonic-gate 	}
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate 	/* Got the space, now copy in the header components */
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate 	nmp->b_rptr -= sizeof (typelen);
4027c478bd9Sstevel@tonic-gate 	SET_NET_USHORT(*(uint16_t *)nmp->b_rptr, typelen);
403*605445d5Sdg199075 	if (hdrlen > sizeof (struct ether_header)) {
4047c478bd9Sstevel@tonic-gate 		nmp->b_rptr -= sizeof (uint16_t);
4057c478bd9Sstevel@tonic-gate 		SET_NET_USHORT(*(uint16_t *)nmp->b_rptr, vptag);
4067c478bd9Sstevel@tonic-gate 		vptag >>= 16;
4077c478bd9Sstevel@tonic-gate 		nmp->b_rptr -= sizeof (uint16_t);
4087c478bd9Sstevel@tonic-gate 		SET_NET_USHORT(*(uint16_t *)nmp->b_rptr, vptag);
4097c478bd9Sstevel@tonic-gate 	}
4107c478bd9Sstevel@tonic-gate 	nmp->b_rptr -= (ETHERADDRL * 2);
411*605445d5Sdg199075 	mh = (struct ether_header *)nmp->b_rptr;
412*605445d5Sdg199075 	mac_copy(dhost, &mh->ether_dhost, macinfo->gldm_addrlen);
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 	/*
4157c478bd9Sstevel@tonic-gate 	 * We access the mac address without the mutex to prevent
4167c478bd9Sstevel@tonic-gate 	 * mutex contention (BUG 4211361)
4177c478bd9Sstevel@tonic-gate 	 */
4187c478bd9Sstevel@tonic-gate 	mac_copy(((gld_mac_pvt_t *)macinfo->gldm_mac_pvt)->curr_macaddr,
419*605445d5Sdg199075 	    &mh->ether_shost, macinfo->gldm_addrlen);
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate 	return (nmp);
4227c478bd9Sstevel@tonic-gate }
4237c478bd9Sstevel@tonic-gate 
424*605445d5Sdg199075 /*
425*605445d5Sdg199075  * Insert the VLAN tag into the packet. The packet now is an Ethernet header
426*605445d5Sdg199075  * without VLAN tag information.
427*605445d5Sdg199075  */
428*605445d5Sdg199075 mblk_t *
429*605445d5Sdg199075 gld_insert_vtag_ether(mblk_t *mp, uint32_t vtag)
430*605445d5Sdg199075 {
431*605445d5Sdg199075 	struct ether_vlan_header *evhp;
432*605445d5Sdg199075 	struct ether_header *ehp;
433*605445d5Sdg199075 	mblk_t *nmp;
434*605445d5Sdg199075 
435*605445d5Sdg199075 	if (vtag == VLAN_VID_NONE)
436*605445d5Sdg199075 		return (mp);
437*605445d5Sdg199075 
438*605445d5Sdg199075 	if (DB_REF(mp) == 1 && MBLKHEAD(mp) >= VTAG_SIZE) {
439*605445d5Sdg199075 		/* it fits at the beginning of the message block */
440*605445d5Sdg199075 		nmp = mp;
441*605445d5Sdg199075 		ovbcopy(nmp->b_rptr, nmp->b_rptr - VTAG_SIZE, 2 * ETHERADDRL);
442*605445d5Sdg199075 		nmp->b_rptr -= VTAG_SIZE;
443*605445d5Sdg199075 		evhp = (struct ether_vlan_header *)nmp->b_rptr;
444*605445d5Sdg199075 	} else {
445*605445d5Sdg199075 		/* we need to allocate one */
446*605445d5Sdg199075 		if ((nmp = allocb(sizeof (struct ether_vlan_header),
447*605445d5Sdg199075 		    BPRI_MED)) == NULL) {
448*605445d5Sdg199075 			return (NULL);
449*605445d5Sdg199075 		}
450*605445d5Sdg199075 		nmp->b_wptr += sizeof (struct ether_vlan_header);
451*605445d5Sdg199075 
452*605445d5Sdg199075 		/* transfer the ether_header fields */
453*605445d5Sdg199075 		evhp = (struct ether_vlan_header *)nmp->b_rptr;
454*605445d5Sdg199075 		ehp = (struct ether_header *)mp->b_rptr;
455*605445d5Sdg199075 		mac_copy(&ehp->ether_dhost, &evhp->ether_dhost, ETHERADDRL);
456*605445d5Sdg199075 		mac_copy(&ehp->ether_shost, &evhp->ether_shost, ETHERADDRL);
457*605445d5Sdg199075 		bcopy(&ehp->ether_type, &evhp->ether_type, sizeof (uint16_t));
458*605445d5Sdg199075 
459*605445d5Sdg199075 		/* offset the mp of the MAC header length. */
460*605445d5Sdg199075 		mp->b_rptr += sizeof (struct ether_header);
461*605445d5Sdg199075 		if (MBLKL(mp) == 0) {
462*605445d5Sdg199075 			nmp->b_cont = mp->b_cont;
463*605445d5Sdg199075 			freeb(mp);
464*605445d5Sdg199075 		} else {
465*605445d5Sdg199075 			nmp->b_cont = mp;
466*605445d5Sdg199075 		}
467*605445d5Sdg199075 	}
468*605445d5Sdg199075 
469*605445d5Sdg199075 	SET_NET_USHORT(evhp->ether_tci, vtag);
470*605445d5Sdg199075 	vtag >>= 16;
471*605445d5Sdg199075 	SET_NET_USHORT(evhp->ether_tpid, vtag);
472*605445d5Sdg199075 	return (nmp);
473*605445d5Sdg199075 }
474*605445d5Sdg199075 
4757c478bd9Sstevel@tonic-gate mblk_t *
4767c478bd9Sstevel@tonic-gate gld_fastpath_ether(gld_t *gld, mblk_t *mp)
4777c478bd9Sstevel@tonic-gate {
4787c478bd9Sstevel@tonic-gate 	gld_mac_info_t *macinfo = gld->gld_mac_info;
4797c478bd9Sstevel@tonic-gate 	dl_unitdata_req_t *dlp = (dl_unitdata_req_t *)mp->b_cont->b_rptr;
4807c478bd9Sstevel@tonic-gate 	struct gld_dlsap *gldp = DLSAP(dlp, dlp->dl_dest_addr_offset);
4817c478bd9Sstevel@tonic-gate 	unsigned short typelen;
4827c478bd9Sstevel@tonic-gate 	mblk_t *nmp;
483*605445d5Sdg199075 	struct ether_header *mh;
4847c478bd9Sstevel@tonic-gate 	int hdrlen;
4857c478bd9Sstevel@tonic-gate 	uint32_t vptag;
4867c478bd9Sstevel@tonic-gate 	gld_vlan_t *gld_vlan;
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate 	ASSERT(macinfo);
4897c478bd9Sstevel@tonic-gate 
4907c478bd9Sstevel@tonic-gate 	/* look in the unitdata request for a sap, else use bound one */
4917c478bd9Sstevel@tonic-gate 	if (dlp->dl_dest_addr_length >= DLSAPLENGTH(macinfo) &&
4927c478bd9Sstevel@tonic-gate 	    REF_HOST_USHORT(gldp->glda_sap) != 0)
4937c478bd9Sstevel@tonic-gate 		typelen = REF_HOST_USHORT(gldp->glda_sap);
4947c478bd9Sstevel@tonic-gate 	else
4957c478bd9Sstevel@tonic-gate 		typelen = gld->gld_sap;
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate 	/*
4987c478bd9Sstevel@tonic-gate 	 * We only do fast-path for EtherType encoding because this is the only
4997c478bd9Sstevel@tonic-gate 	 * case where the media header will be consistent from packet to packet.
5007c478bd9Sstevel@tonic-gate 	 */
5017c478bd9Sstevel@tonic-gate 	if (typelen <= ETHERMTU)
5027c478bd9Sstevel@tonic-gate 		return (NULL);
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate 	/*
5057c478bd9Sstevel@tonic-gate 	 * Initialize the fast path header to include the
5067c478bd9Sstevel@tonic-gate 	 * basic source address information and type field.
5077c478bd9Sstevel@tonic-gate 	 */
508*605445d5Sdg199075 	hdrlen = sizeof (struct ether_header);
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate 	/*
5117c478bd9Sstevel@tonic-gate 	 * Check to see if VLAN is enabled on this stream
5127c478bd9Sstevel@tonic-gate 	 * if so then make the header bigger to hold a clone
5137c478bd9Sstevel@tonic-gate 	 * vlan tag.
5147c478bd9Sstevel@tonic-gate 	 */
5157c478bd9Sstevel@tonic-gate 	gld_vlan = (gld_vlan_t *)gld->gld_vlan;
5167c478bd9Sstevel@tonic-gate 	if (gld_vlan && (gld_vlan->gldv_id != VLAN_VID_NONE)) {
5177c478bd9Sstevel@tonic-gate 		hdrlen += VTAG_SIZE;
5187c478bd9Sstevel@tonic-gate 		vptag = gld_vlan->gldv_ptag;
5197c478bd9Sstevel@tonic-gate 	}
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 	if ((nmp = allocb(hdrlen, BPRI_MED)) == NULL)
5227c478bd9Sstevel@tonic-gate 		return (NULL);
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate 	nmp->b_rptr = nmp->b_wptr = DB_LIM(nmp);
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate 	/* Got the space, now copy in the header components */
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate 	nmp->b_rptr -= sizeof (typelen);
5297c478bd9Sstevel@tonic-gate 	SET_NET_USHORT(*(uint16_t *)nmp->b_rptr, typelen);
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate 	/*
5327c478bd9Sstevel@tonic-gate 	 * If the header is for a VLAN stream, then add
5337c478bd9Sstevel@tonic-gate 	 * in the VLAN tag to the clone header.
5347c478bd9Sstevel@tonic-gate 	 */
535*605445d5Sdg199075 	if (hdrlen > sizeof (struct ether_header)) {
5367c478bd9Sstevel@tonic-gate 		nmp->b_rptr -= sizeof (uint16_t);
5377c478bd9Sstevel@tonic-gate 		SET_NET_USHORT(*(uint16_t *)nmp->b_rptr, vptag);
5387c478bd9Sstevel@tonic-gate 		vptag >>= 16;
5397c478bd9Sstevel@tonic-gate 		nmp->b_rptr -= sizeof (uint16_t);
5407c478bd9Sstevel@tonic-gate 		SET_NET_USHORT(*(uint16_t *)nmp->b_rptr, vptag);
5417c478bd9Sstevel@tonic-gate 	}
5427c478bd9Sstevel@tonic-gate 	nmp->b_rptr -= (ETHERADDRL * 2);
543*605445d5Sdg199075 	mh = (struct ether_header *)nmp->b_rptr;
544*605445d5Sdg199075 	mac_copy(gldp->glda_addr, &mh->ether_dhost, macinfo->gldm_addrlen);
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate 	GLDM_LOCK(macinfo, RW_WRITER);
5477c478bd9Sstevel@tonic-gate 	mac_copy(((gld_mac_pvt_t *)macinfo->gldm_mac_pvt)->curr_macaddr,
548*605445d5Sdg199075 	    &mh->ether_shost, macinfo->gldm_addrlen);
5497c478bd9Sstevel@tonic-gate 	GLDM_UNLOCK(macinfo);
5507c478bd9Sstevel@tonic-gate 
5517c478bd9Sstevel@tonic-gate 	return (nmp);
5527c478bd9Sstevel@tonic-gate }
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate /* == */
5557c478bd9Sstevel@tonic-gate /* IB */
5567c478bd9Sstevel@tonic-gate /* == */
5577c478bd9Sstevel@tonic-gate 
5587c478bd9Sstevel@tonic-gate void
5597c478bd9Sstevel@tonic-gate gld_init_ib(gld_mac_info_t *macinfo)
5607c478bd9Sstevel@tonic-gate {
5617c478bd9Sstevel@tonic-gate 	/*
5627c478bd9Sstevel@tonic-gate 	 * Currently, the generic stats maintained by GLD is
5637c478bd9Sstevel@tonic-gate 	 * sufficient for IPoIB.
5647c478bd9Sstevel@tonic-gate 	 */
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate 	/* Assumptions we make for this medium */
5677c478bd9Sstevel@tonic-gate 	ASSERT(macinfo->gldm_type == DL_IB);
5687c478bd9Sstevel@tonic-gate 	ASSERT(macinfo->gldm_addrlen == IPOIB_ADDRL);
5697c478bd9Sstevel@tonic-gate 	ASSERT(macinfo->gldm_saplen == -2);
5707c478bd9Sstevel@tonic-gate }
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate /* ARGSUSED */
5737c478bd9Sstevel@tonic-gate void
5747c478bd9Sstevel@tonic-gate gld_uninit_ib(gld_mac_info_t *macinfo)
5757c478bd9Sstevel@tonic-gate {
5767c478bd9Sstevel@tonic-gate }
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate /*
5797c478bd9Sstevel@tonic-gate  * The packet format sent to the driver is:
5807c478bd9Sstevel@tonic-gate  * IPOIB_ADDRL bytes dest addr :: 2b sap :: 2b 0s :: data
5817c478bd9Sstevel@tonic-gate  * The packet format received from the driver is:
5827c478bd9Sstevel@tonic-gate  * IPOIB_GRH_SIZE bytes pseudo GRH :: 2b sap :: 2b 0s :: data.
5837c478bd9Sstevel@tonic-gate  */
5847c478bd9Sstevel@tonic-gate int
5857c478bd9Sstevel@tonic-gate gld_interpret_ib(gld_mac_info_t *macinfo, mblk_t *mp, pktinfo_t *pktinfo,
5867c478bd9Sstevel@tonic-gate     packet_flag_t flags)
5877c478bd9Sstevel@tonic-gate {
5887c478bd9Sstevel@tonic-gate 	ipoib_pgrh_t *grh;
5897c478bd9Sstevel@tonic-gate 	ipoib_ptxhdr_t *gldp;
5907c478bd9Sstevel@tonic-gate 	mblk_t *pmp = NULL;
5917c478bd9Sstevel@tonic-gate 	gld_mac_pvt_t *mac_pvt = (gld_mac_pvt_t *)macinfo->gldm_mac_pvt;
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate 	/*
5947c478bd9Sstevel@tonic-gate 	 * Quickly handle receive fastpath for IPQ hack.
5957c478bd9Sstevel@tonic-gate 	 */
5967c478bd9Sstevel@tonic-gate 	if (flags == GLD_RXQUICK) {
5977c478bd9Sstevel@tonic-gate 		pktinfo->pktLen = msgdsize(mp) - IPOIB_GRH_SIZE;
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate 		/*
6007c478bd9Sstevel@tonic-gate 		 * Check whether the header is contiguous, which
6017c478bd9Sstevel@tonic-gate 		 * also implicitly makes sure the packet is big enough.
6027c478bd9Sstevel@tonic-gate 		 */
6037c478bd9Sstevel@tonic-gate 		if (MBLKL(mp) < (IPOIB_GRH_SIZE + IPOIB_HDRSIZE))
6047c478bd9Sstevel@tonic-gate 			return (-1);
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate 		/*
6077c478bd9Sstevel@tonic-gate 		 * Almost all times, unicast will not have
6087c478bd9Sstevel@tonic-gate 		 * a valid pgrh; quickly identify and ask for
6097c478bd9Sstevel@tonic-gate 		 * IPQ hack optimization only in that case.
6107c478bd9Sstevel@tonic-gate 		 */
6117c478bd9Sstevel@tonic-gate 		grh = (ipoib_pgrh_t *)mp->b_rptr;
6127c478bd9Sstevel@tonic-gate 		if (grh->ipoib_vertcflow == 0) {
6137c478bd9Sstevel@tonic-gate 			struct ipoib_header *ihp = (struct ipoib_header *)
6147c478bd9Sstevel@tonic-gate 			    (mp->b_rptr + IPOIB_GRH_SIZE);
6157c478bd9Sstevel@tonic-gate 
6167c478bd9Sstevel@tonic-gate 			pktinfo->isForMe = 1;
6177c478bd9Sstevel@tonic-gate 			pktinfo->ethertype = REF_NET_USHORT(ihp->ipoib_type);
6187c478bd9Sstevel@tonic-gate 			pktinfo->macLen = IPOIB_GRH_SIZE + IPOIB_HDRSIZE;
6197c478bd9Sstevel@tonic-gate 			return (0);
6207c478bd9Sstevel@tonic-gate 		} else {
6217c478bd9Sstevel@tonic-gate 			return (-1);
6227c478bd9Sstevel@tonic-gate 		}
6237c478bd9Sstevel@tonic-gate 	}
6247c478bd9Sstevel@tonic-gate 
6257c478bd9Sstevel@tonic-gate 	/*
6267c478bd9Sstevel@tonic-gate 	 * Handle the GLD_TX, GLD_RX, GLD_RXLOOP cases now.
6277c478bd9Sstevel@tonic-gate 	 */
6287c478bd9Sstevel@tonic-gate 	ASSERT(flags != GLD_RXQUICK);
6297c478bd9Sstevel@tonic-gate 	bzero((void *)pktinfo, sizeof (*pktinfo));
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate 	if (flags != GLD_RX) {
6327c478bd9Sstevel@tonic-gate 		/*
6337c478bd9Sstevel@tonic-gate 		 * GLD_TX and GLD_RXLOOP cases.
6347c478bd9Sstevel@tonic-gate 		 */
6357c478bd9Sstevel@tonic-gate 		gldp = (ipoib_ptxhdr_t *)mp->b_rptr;
6367c478bd9Sstevel@tonic-gate 		pktinfo->pktLen = msgdsize(mp);
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate 		/* make sure packet has at least a pseudo header */
6397c478bd9Sstevel@tonic-gate 		if (pktinfo->pktLen < sizeof (ipoib_ptxhdr_t))
6407c478bd9Sstevel@tonic-gate 			return (-1);
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate 		/* make sure the mac header falls into contiguous memory */
6437c478bd9Sstevel@tonic-gate 		if (MBLKL(mp) < sizeof (ipoib_ptxhdr_t)) {
6447c478bd9Sstevel@tonic-gate 			if ((pmp = msgpullup(mp, -1)) == NULL) {
6457c478bd9Sstevel@tonic-gate #ifdef GLD_DEBUG
6467c478bd9Sstevel@tonic-gate 				if (gld_debug & GLDERRS)
6477c478bd9Sstevel@tonic-gate 					cmn_err(CE_WARN,
6487c478bd9Sstevel@tonic-gate 					    "GLD: interpret_ib "
6497c478bd9Sstevel@tonic-gate 					    "cannot msgpullup");
6507c478bd9Sstevel@tonic-gate #endif
6517c478bd9Sstevel@tonic-gate 				return (-1);
6527c478bd9Sstevel@tonic-gate 			}
6537c478bd9Sstevel@tonic-gate 			/* this mblk contains the whole mac header */
6547c478bd9Sstevel@tonic-gate 			mp = pmp;
6557c478bd9Sstevel@tonic-gate 		}
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate 		/*
6587c478bd9Sstevel@tonic-gate 		 * Check if mac is broadcast or multicast address; all these
6597c478bd9Sstevel@tonic-gate 		 * types of address have the top 4 bytes as 0x00FFFFFF.
6607c478bd9Sstevel@tonic-gate 		 */
6617c478bd9Sstevel@tonic-gate 		if (mac_eq(&gldp->ipoib_dest, macinfo->gldm_broadcast_addr,
6627c478bd9Sstevel@tonic-gate 		    sizeof (uint32_t))) {
6637c478bd9Sstevel@tonic-gate 			if (mac_eq(&gldp->ipoib_dest,
6647c478bd9Sstevel@tonic-gate 			    macinfo->gldm_broadcast_addr, IPOIB_ADDRL))
6657c478bd9Sstevel@tonic-gate 				pktinfo->isBroadcast = 1;
6667c478bd9Sstevel@tonic-gate 			else
6677c478bd9Sstevel@tonic-gate 				pktinfo->isMulticast = 1;
6687c478bd9Sstevel@tonic-gate 		}
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate 		/*
6717c478bd9Sstevel@tonic-gate 		 * Only count bytes we will be sending over the wire
6727c478bd9Sstevel@tonic-gate 		 * or looping back.
6737c478bd9Sstevel@tonic-gate 		 */
6747c478bd9Sstevel@tonic-gate 		pktinfo->pktLen -= IPOIB_ADDRL;
6757c478bd9Sstevel@tonic-gate 		if (flags == GLD_TX)
6767c478bd9Sstevel@tonic-gate 			goto out;	/* Got all info we need for xmit case */
6777c478bd9Sstevel@tonic-gate 
6787c478bd9Sstevel@tonic-gate 		/*
6797c478bd9Sstevel@tonic-gate 		 * Loopback case: this is a dup'ed message.
6807c478bd9Sstevel@tonic-gate 		 */
6817c478bd9Sstevel@tonic-gate 		mp->b_rptr += IPOIB_ADDRL;
6827c478bd9Sstevel@tonic-gate 		mac_copy(&gldp->ipoib_dest, pktinfo->dhost, IPOIB_ADDRL);
6837c478bd9Sstevel@tonic-gate 		mac_copy(mac_pvt->curr_macaddr, pktinfo->shost, IPOIB_ADDRL);
6847c478bd9Sstevel@tonic-gate 	} else {
6857c478bd9Sstevel@tonic-gate 		/*
6867c478bd9Sstevel@tonic-gate 		 * GLD_RX case; process packet sent from driver.
6877c478bd9Sstevel@tonic-gate 		 */
6887c478bd9Sstevel@tonic-gate 		ipoib_mac_t *mact, *tact;
6897c478bd9Sstevel@tonic-gate 		ib_qpn_t dqpn;
6907c478bd9Sstevel@tonic-gate 
6917c478bd9Sstevel@tonic-gate 		pktinfo->pktLen = msgdsize(mp);
6927c478bd9Sstevel@tonic-gate 		/* make sure packet has at least pgrh and mac header */
6937c478bd9Sstevel@tonic-gate 		if (pktinfo->pktLen < (IPOIB_GRH_SIZE + IPOIB_HDRSIZE))
6947c478bd9Sstevel@tonic-gate 			return (-1);
6957c478bd9Sstevel@tonic-gate 
6967c478bd9Sstevel@tonic-gate 		/* make sure the header falls into contiguous memory */
6977c478bd9Sstevel@tonic-gate 		if (MBLKL(mp) < (IPOIB_GRH_SIZE + IPOIB_HDRSIZE)) {
6987c478bd9Sstevel@tonic-gate 			if ((pmp = msgpullup(mp, -1)) == NULL) {
6997c478bd9Sstevel@tonic-gate #ifdef GLD_DEBUG
7007c478bd9Sstevel@tonic-gate 				if (gld_debug & GLDERRS)
7017c478bd9Sstevel@tonic-gate 					cmn_err(CE_WARN,
7027c478bd9Sstevel@tonic-gate 					    "GLD: interpret_ib "
7037c478bd9Sstevel@tonic-gate 					    "cannot msgpullup2");
7047c478bd9Sstevel@tonic-gate #endif
7057c478bd9Sstevel@tonic-gate 				return (-1);
7067c478bd9Sstevel@tonic-gate 			}
7077c478bd9Sstevel@tonic-gate 			/* this mblk contains the whole mac header */
7087c478bd9Sstevel@tonic-gate 			mp = pmp;
7097c478bd9Sstevel@tonic-gate 		}
7107c478bd9Sstevel@tonic-gate 
7117c478bd9Sstevel@tonic-gate 		grh = (ipoib_pgrh_t *)mp->b_rptr;
7127c478bd9Sstevel@tonic-gate 		mp->b_rptr += IPOIB_GRH_SIZE;
7137c478bd9Sstevel@tonic-gate 		pktinfo->pktLen -= IPOIB_GRH_SIZE;
7147c478bd9Sstevel@tonic-gate 		if (grh->ipoib_vertcflow) {
7157c478bd9Sstevel@tonic-gate 			/*
7167c478bd9Sstevel@tonic-gate 			 * First, copy source address from grh.
7177c478bd9Sstevel@tonic-gate 			 */
7187c478bd9Sstevel@tonic-gate 			mact = (ipoib_mac_t *)pktinfo->shost;
7197c478bd9Sstevel@tonic-gate 			mac_copy(&grh->ipoib_sqpn, &mact->ipoib_qpn,
7207c478bd9Sstevel@tonic-gate 			    IPOIB_ADDRL);
7217c478bd9Sstevel@tonic-gate 
7227c478bd9Sstevel@tonic-gate 			/*
7237c478bd9Sstevel@tonic-gate 			 * Then copy destination address from grh;
7247c478bd9Sstevel@tonic-gate 			 * first, the 16 bytes of GID.
7257c478bd9Sstevel@tonic-gate 			 */
7267c478bd9Sstevel@tonic-gate 			mact = (ipoib_mac_t *)pktinfo->dhost;
7277c478bd9Sstevel@tonic-gate 			mac_copy(&grh->ipoib_dgid_pref,
7287c478bd9Sstevel@tonic-gate 			    &mact->ipoib_gidpref, IPOIB_ADDRL -
7297c478bd9Sstevel@tonic-gate 			    sizeof (mact->ipoib_qpn));
7307c478bd9Sstevel@tonic-gate 			tact = (ipoib_mac_t *)mac_pvt->curr_macaddr;
7317c478bd9Sstevel@tonic-gate 
7327c478bd9Sstevel@tonic-gate 			/* Is this a multicast address */
7337c478bd9Sstevel@tonic-gate 			if (*(uchar_t *)(grh->ipoib_dgid_pref) == 0xFF) {
7347c478bd9Sstevel@tonic-gate 				/*
7357c478bd9Sstevel@tonic-gate 				 * Only check for hardware looping in
7367c478bd9Sstevel@tonic-gate 				 * multicast case. It is assumed higher
7377c478bd9Sstevel@tonic-gate 				 * layer code (IP) will stop unicast loops;
7387c478bd9Sstevel@tonic-gate 				 * ie will prevent a transmit to self.
7397c478bd9Sstevel@tonic-gate 				 */
7407c478bd9Sstevel@tonic-gate 				if (bcmp(&grh->ipoib_sqpn, tact,
7417c478bd9Sstevel@tonic-gate 				    IPOIB_ADDRL) == 0)
7427c478bd9Sstevel@tonic-gate 					pktinfo->isLooped = 1;
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate 				tact = (ipoib_mac_t *)macinfo->
7457c478bd9Sstevel@tonic-gate 				    gldm_broadcast_addr;
7467c478bd9Sstevel@tonic-gate 				if (mac_eq(tact->ipoib_gidpref,
7477c478bd9Sstevel@tonic-gate 				    grh->ipoib_dgid_pref,
7487c478bd9Sstevel@tonic-gate 				    IPOIB_ADDRL - sizeof (tact->ipoib_qpn)))
7497c478bd9Sstevel@tonic-gate 					pktinfo->isBroadcast = 1;
7507c478bd9Sstevel@tonic-gate 				else
7517c478bd9Sstevel@tonic-gate 					pktinfo->isMulticast = 1;
7527c478bd9Sstevel@tonic-gate 				/*
7537c478bd9Sstevel@tonic-gate 				 * Now copy the 4 bytes QPN part of the
7547c478bd9Sstevel@tonic-gate 				 * destination address.
7557c478bd9Sstevel@tonic-gate 				 */
7567c478bd9Sstevel@tonic-gate 				dqpn = htonl(IB_MC_QPN);
7577c478bd9Sstevel@tonic-gate 				mac_copy(&dqpn, &mact->ipoib_qpn,
7587c478bd9Sstevel@tonic-gate 				    sizeof (mact->ipoib_qpn));
7597c478bd9Sstevel@tonic-gate 			} else {
7607c478bd9Sstevel@tonic-gate 				/*
7617c478bd9Sstevel@tonic-gate 				 * Now copy the 4 bytes QPN part of the
7627c478bd9Sstevel@tonic-gate 				 * destination address.
7637c478bd9Sstevel@tonic-gate 				 */
7647c478bd9Sstevel@tonic-gate 				mac_copy(&tact->ipoib_qpn, &mact->ipoib_qpn,
7657c478bd9Sstevel@tonic-gate 				    sizeof (mact->ipoib_qpn));
7667c478bd9Sstevel@tonic-gate 				/*
7677c478bd9Sstevel@tonic-gate 				 * Any unicast packets received on IBA are
7687c478bd9Sstevel@tonic-gate 				 * for the node.
7697c478bd9Sstevel@tonic-gate 				 */
7707c478bd9Sstevel@tonic-gate 				pktinfo->isForMe = 1;
7717c478bd9Sstevel@tonic-gate 			}
7727c478bd9Sstevel@tonic-gate 		} else {
7737c478bd9Sstevel@tonic-gate 			/*
7747c478bd9Sstevel@tonic-gate 			 * It can not be a IBA multicast packet.
7757c478bd9Sstevel@tonic-gate 			 * Must have been unicast to us. We do not
7767c478bd9Sstevel@tonic-gate 			 * have shost information, which is used in
7777c478bd9Sstevel@tonic-gate 			 * gld_addudind(); IP/ARP does not care.
7787c478bd9Sstevel@tonic-gate 			 */
7797c478bd9Sstevel@tonic-gate 			pktinfo->nosource = 1;
7807c478bd9Sstevel@tonic-gate 			mac_copy(mac_pvt->curr_macaddr, pktinfo->dhost,
7817c478bd9Sstevel@tonic-gate 			    IPOIB_ADDRL);
7827c478bd9Sstevel@tonic-gate 			/*
7837c478bd9Sstevel@tonic-gate 			 * Any unicast packets received on IBA are
7847c478bd9Sstevel@tonic-gate 			 * for the node.
7857c478bd9Sstevel@tonic-gate 			 */
7867c478bd9Sstevel@tonic-gate 			pktinfo->isForMe = 1;
7877c478bd9Sstevel@tonic-gate 		}
7887c478bd9Sstevel@tonic-gate 	}
7897c478bd9Sstevel@tonic-gate 
7907c478bd9Sstevel@tonic-gate 	ASSERT((flags == GLD_RX) || (flags == GLD_RXLOOP));
7917c478bd9Sstevel@tonic-gate 	ASSERT(GLDM_LOCK_HELD(macinfo));
7927c478bd9Sstevel@tonic-gate 	pktinfo->ethertype = REF_NET_USHORT(((ipoib_hdr_t *)
7937c478bd9Sstevel@tonic-gate 	    (mp->b_rptr))->ipoib_type);
7947c478bd9Sstevel@tonic-gate 	pktinfo->macLen = IPOIB_HDRSIZE;
7957c478bd9Sstevel@tonic-gate 
7967c478bd9Sstevel@tonic-gate out:
7977c478bd9Sstevel@tonic-gate 	if (pmp != NULL)
7987c478bd9Sstevel@tonic-gate 		freemsg(pmp);
7997c478bd9Sstevel@tonic-gate 
8007c478bd9Sstevel@tonic-gate 	return (0);
8017c478bd9Sstevel@tonic-gate }
8027c478bd9Sstevel@tonic-gate 
8037c478bd9Sstevel@tonic-gate /*
8047c478bd9Sstevel@tonic-gate  * The packet format sent to the driver is: 2b sap :: 2b 0s :: data
8057c478bd9Sstevel@tonic-gate  */
8067c478bd9Sstevel@tonic-gate void
8077c478bd9Sstevel@tonic-gate gld_interpret_mdt_ib(gld_mac_info_t *macinfo, mblk_t *mp, pdescinfo_t *pinfo,
8087c478bd9Sstevel@tonic-gate     pktinfo_t *pktinfo, mdt_packet_flag_t flags)
8097c478bd9Sstevel@tonic-gate {
8107c478bd9Sstevel@tonic-gate 	gld_mac_pvt_t *mac_pvt;
8117c478bd9Sstevel@tonic-gate 	multidata_t *dlmdp;
8127c478bd9Sstevel@tonic-gate 	pattrinfo_t attr_info = { PATTR_DSTADDRSAP, };
8137c478bd9Sstevel@tonic-gate 	pattr_t *patr;
8147c478bd9Sstevel@tonic-gate 	ipoib_ptxhdr_t *dlap = NULL;
8157c478bd9Sstevel@tonic-gate 
8167c478bd9Sstevel@tonic-gate 	/*
8177c478bd9Sstevel@tonic-gate 	 * Per packet formatting.
8187c478bd9Sstevel@tonic-gate 	 */
8197c478bd9Sstevel@tonic-gate 	if (flags == GLD_MDT_TXPKT) {
8207c478bd9Sstevel@tonic-gate 		ipoib_hdr_t *hptr;
8217c478bd9Sstevel@tonic-gate 		uint_t seg;
8227c478bd9Sstevel@tonic-gate 
8237c478bd9Sstevel@tonic-gate 		if (PDESC_HDRL(pinfo) == 0)
8247c478bd9Sstevel@tonic-gate 			return;
8257c478bd9Sstevel@tonic-gate 
8267c478bd9Sstevel@tonic-gate 		/*
8277c478bd9Sstevel@tonic-gate 		 * Update packet's link header.
8287c478bd9Sstevel@tonic-gate 		 */
8297c478bd9Sstevel@tonic-gate 		pinfo->hdr_rptr -= IPOIB_HDRSIZE;
8307c478bd9Sstevel@tonic-gate 		hptr = (ipoib_hdr_t *)pinfo->hdr_rptr;
8317c478bd9Sstevel@tonic-gate 		hptr->ipoib_mbz = htons(0);
8327c478bd9Sstevel@tonic-gate 		hptr->ipoib_type = pktinfo->ethertype;
8337c478bd9Sstevel@tonic-gate 
8347c478bd9Sstevel@tonic-gate 		/*
8357c478bd9Sstevel@tonic-gate 		 * Total #bytes that will be put on wire.
8367c478bd9Sstevel@tonic-gate 		 */
8377c478bd9Sstevel@tonic-gate 		pktinfo->pktLen = PDESC_HDRL(pinfo);
8387c478bd9Sstevel@tonic-gate 		for (seg = 0; seg < pinfo->pld_cnt; seg++)
8397c478bd9Sstevel@tonic-gate 			pktinfo->pktLen += PDESC_PLDL(pinfo, seg);
8407c478bd9Sstevel@tonic-gate 
8417c478bd9Sstevel@tonic-gate 		return;
8427c478bd9Sstevel@tonic-gate 	}
8437c478bd9Sstevel@tonic-gate 
8447c478bd9Sstevel@tonic-gate 	/*
8457c478bd9Sstevel@tonic-gate 	 * The following two cases of GLD_MDT_TX and GLD_MDT_RXLOOP are per
8467c478bd9Sstevel@tonic-gate 	 * MDT message processing.
8477c478bd9Sstevel@tonic-gate 	 */
8487c478bd9Sstevel@tonic-gate 	dlmdp = mmd_getmultidata(mp);
8497c478bd9Sstevel@tonic-gate 	patr = mmd_getpattr(dlmdp, NULL, &attr_info);
8507c478bd9Sstevel@tonic-gate 	ASSERT(patr != NULL);
8517c478bd9Sstevel@tonic-gate 	ASSERT(macinfo->gldm_saplen == -2);
8527c478bd9Sstevel@tonic-gate 	if (patr != NULL)
8537c478bd9Sstevel@tonic-gate 		dlap = (ipoib_ptxhdr_t *)((pattr_addr_t *)attr_info.buf)->addr;
8547c478bd9Sstevel@tonic-gate 
8557c478bd9Sstevel@tonic-gate 	if (flags == GLD_MDT_TX) {
8567c478bd9Sstevel@tonic-gate 		bzero((void *)pktinfo, sizeof (*pktinfo));
8577c478bd9Sstevel@tonic-gate 		if (dlap == NULL)
8587c478bd9Sstevel@tonic-gate 			return;
8597c478bd9Sstevel@tonic-gate 
8607c478bd9Sstevel@tonic-gate 		/*
8617c478bd9Sstevel@tonic-gate 		 * Check if mac is broadcast or multicast address; all these
8627c478bd9Sstevel@tonic-gate 		 * types of address have the top 4 bytes as 0x00FFFFFF.
8637c478bd9Sstevel@tonic-gate 		 */
8647c478bd9Sstevel@tonic-gate 		if (mac_eq(dlap, macinfo->gldm_broadcast_addr,
8657c478bd9Sstevel@tonic-gate 		    sizeof (uint32_t))) {
8667c478bd9Sstevel@tonic-gate 			if (mac_eq(dlap, macinfo->gldm_broadcast_addr,
8677c478bd9Sstevel@tonic-gate 			    IPOIB_ADDRL))
8687c478bd9Sstevel@tonic-gate 				pktinfo->isBroadcast = 1;
8697c478bd9Sstevel@tonic-gate 			else
8707c478bd9Sstevel@tonic-gate 				pktinfo->isMulticast = 1;
8717c478bd9Sstevel@tonic-gate 		}
8727c478bd9Sstevel@tonic-gate 		pktinfo->ethertype = REF_NET_USHORT(dlap->
8737c478bd9Sstevel@tonic-gate 		    ipoib_rhdr.ipoib_type);
8747c478bd9Sstevel@tonic-gate 	} else {
8757c478bd9Sstevel@tonic-gate 		ASSERT(flags == GLD_MDT_RXLOOP);
8767c478bd9Sstevel@tonic-gate 		pktinfo->macLen = IPOIB_HDRSIZE;
8777c478bd9Sstevel@tonic-gate 		mac_pvt = (gld_mac_pvt_t *)macinfo->gldm_mac_pvt;
8787c478bd9Sstevel@tonic-gate 		mac_copy(mac_pvt->curr_macaddr, pktinfo->shost, IPOIB_ADDRL);
8797c478bd9Sstevel@tonic-gate 		if (dlap == NULL)
8807c478bd9Sstevel@tonic-gate 			return;
8817c478bd9Sstevel@tonic-gate 		mac_copy(&dlap->ipoib_dest, pktinfo->dhost, IPOIB_ADDRL);
8827c478bd9Sstevel@tonic-gate 	}
8837c478bd9Sstevel@tonic-gate }
8847c478bd9Sstevel@tonic-gate 
8857c478bd9Sstevel@tonic-gate mblk_t *
8867c478bd9Sstevel@tonic-gate gld_unitdata_ib(gld_t *gld, mblk_t *mp)
8877c478bd9Sstevel@tonic-gate {
8887c478bd9Sstevel@tonic-gate 	gld_mac_info_t *macinfo = gld->gld_mac_info;
8897c478bd9Sstevel@tonic-gate 	dl_unitdata_req_t *dlp = (dl_unitdata_req_t *)mp->b_rptr;
8907c478bd9Sstevel@tonic-gate 	ipoib_ptxhdr_t *gldp = IPOIBDLSAP(dlp, dlp->dl_dest_addr_offset);
8917c478bd9Sstevel@tonic-gate 	ipoib_mac_t dhost;
8927c478bd9Sstevel@tonic-gate 	unsigned short type;
8937c478bd9Sstevel@tonic-gate 	mblk_t *nmp;
8947c478bd9Sstevel@tonic-gate 	int hdrlen;
8957c478bd9Sstevel@tonic-gate 
8967c478bd9Sstevel@tonic-gate 	ASSERT(macinfo != NULL);
8977c478bd9Sstevel@tonic-gate 
8987c478bd9Sstevel@tonic-gate 	/* extract needed info from the mblk before we maybe reuse it */
8997c478bd9Sstevel@tonic-gate 	mac_copy(&gldp->ipoib_dest, &dhost, IPOIB_ADDRL);
9007c478bd9Sstevel@tonic-gate 
9017c478bd9Sstevel@tonic-gate 	/* look in the unitdata request for a sap, else use bound one */
9027c478bd9Sstevel@tonic-gate 	if (dlp->dl_dest_addr_length >= DLSAPLENGTH(macinfo) &&
9037c478bd9Sstevel@tonic-gate 	    REF_HOST_USHORT(gldp->ipoib_rhdr.ipoib_type) != 0)
9047c478bd9Sstevel@tonic-gate 		type = REF_HOST_USHORT(gldp->ipoib_rhdr.ipoib_type);
9057c478bd9Sstevel@tonic-gate 	else
9067c478bd9Sstevel@tonic-gate 		type = gld->gld_sap;
9077c478bd9Sstevel@tonic-gate 
9087c478bd9Sstevel@tonic-gate 	hdrlen = sizeof (ipoib_ptxhdr_t);
9097c478bd9Sstevel@tonic-gate 
9107c478bd9Sstevel@tonic-gate 	/* need a buffer big enough for the headers */
9117c478bd9Sstevel@tonic-gate 	nmp = mp->b_cont;	/* where the packet payload M_DATA is */
9127c478bd9Sstevel@tonic-gate 	if (DB_REF(nmp) == 1 && MBLKHEAD(nmp) >= hdrlen) {
9137c478bd9Sstevel@tonic-gate 		/* it fits at the beginning of the first M_DATA block */
9147c478bd9Sstevel@tonic-gate 		freeb(mp);	/* don't need the M_PROTO anymore */
9157c478bd9Sstevel@tonic-gate 	} else if (DB_REF(mp) == 1 && MBLKSIZE(mp) >= hdrlen) {
9167c478bd9Sstevel@tonic-gate 		/* we can reuse the dl_unitdata_req M_PROTO mblk */
9177c478bd9Sstevel@tonic-gate 		nmp = mp;
9187c478bd9Sstevel@tonic-gate 		DB_TYPE(nmp) = M_DATA;
9197c478bd9Sstevel@tonic-gate 		nmp->b_rptr = nmp->b_wptr = DB_LIM(nmp);
9207c478bd9Sstevel@tonic-gate 	} else {
9217c478bd9Sstevel@tonic-gate 		/* we need to allocate one */
9227c478bd9Sstevel@tonic-gate 		if ((nmp = allocb(hdrlen, BPRI_MED)) == NULL)
9237c478bd9Sstevel@tonic-gate 			return (NULL);
9247c478bd9Sstevel@tonic-gate 		nmp->b_rptr = nmp->b_wptr = DB_LIM(nmp);
9257c478bd9Sstevel@tonic-gate 		linkb(nmp, mp->b_cont);
9267c478bd9Sstevel@tonic-gate 		freeb(mp);
9277c478bd9Sstevel@tonic-gate 	}
9287c478bd9Sstevel@tonic-gate 
9297c478bd9Sstevel@tonic-gate 	/* Got the space, now copy in the header components */
9307c478bd9Sstevel@tonic-gate 
9317c478bd9Sstevel@tonic-gate 	nmp->b_rptr -= sizeof (ipoib_ptxhdr_t);
9327c478bd9Sstevel@tonic-gate 	gldp = (ipoib_ptxhdr_t *)nmp->b_rptr;
9337c478bd9Sstevel@tonic-gate 	SET_NET_USHORT(gldp->ipoib_rhdr.ipoib_type, type);
9347c478bd9Sstevel@tonic-gate 	gldp->ipoib_rhdr.ipoib_mbz = 0;
9357c478bd9Sstevel@tonic-gate 	mac_copy(&dhost, &gldp->ipoib_dest, IPOIB_ADDRL);
9367c478bd9Sstevel@tonic-gate 
9377c478bd9Sstevel@tonic-gate 	return (nmp);
9387c478bd9Sstevel@tonic-gate }
9397c478bd9Sstevel@tonic-gate 
9407c478bd9Sstevel@tonic-gate mblk_t *
9417c478bd9Sstevel@tonic-gate gld_fastpath_ib(gld_t *gld, mblk_t *mp)
9427c478bd9Sstevel@tonic-gate {
9437c478bd9Sstevel@tonic-gate 	gld_mac_info_t *macinfo = gld->gld_mac_info;
9447c478bd9Sstevel@tonic-gate 	dl_unitdata_req_t *dlp = (dl_unitdata_req_t *)mp->b_cont->b_rptr;
9457c478bd9Sstevel@tonic-gate 	ipoib_ptxhdr_t *gldp = IPOIBDLSAP(dlp, dlp->dl_dest_addr_offset);
9467c478bd9Sstevel@tonic-gate 	unsigned short type;
9477c478bd9Sstevel@tonic-gate 	mblk_t *nmp;
9487c478bd9Sstevel@tonic-gate 	ipoib_ptxhdr_t *tgldp;
9497c478bd9Sstevel@tonic-gate 	int hdrlen;
9507c478bd9Sstevel@tonic-gate 
9517c478bd9Sstevel@tonic-gate 	ASSERT(macinfo != NULL);
9527c478bd9Sstevel@tonic-gate 
9537c478bd9Sstevel@tonic-gate 	/* look in the unitdata request for a sap, else use bound one */
9547c478bd9Sstevel@tonic-gate 	if (dlp->dl_dest_addr_length >= DLSAPLENGTH(macinfo) &&
9557c478bd9Sstevel@tonic-gate 	    REF_HOST_USHORT(gldp->ipoib_rhdr.ipoib_type) != 0)
9567c478bd9Sstevel@tonic-gate 		type = REF_HOST_USHORT(gldp->ipoib_rhdr.ipoib_type);
9577c478bd9Sstevel@tonic-gate 	else
9587c478bd9Sstevel@tonic-gate 		type = gld->gld_sap;
9597c478bd9Sstevel@tonic-gate 
9607c478bd9Sstevel@tonic-gate 	hdrlen = sizeof (ipoib_ptxhdr_t);
9617c478bd9Sstevel@tonic-gate 
9627c478bd9Sstevel@tonic-gate 	if ((nmp = allocb(hdrlen, BPRI_MED)) == NULL)
9637c478bd9Sstevel@tonic-gate 		return (NULL);
9647c478bd9Sstevel@tonic-gate 
9657c478bd9Sstevel@tonic-gate 	nmp->b_rptr = nmp->b_wptr = DB_LIM(nmp);
9667c478bd9Sstevel@tonic-gate 
9677c478bd9Sstevel@tonic-gate 	/* Got the space, now copy in the header components */
9687c478bd9Sstevel@tonic-gate 
9697c478bd9Sstevel@tonic-gate 	nmp->b_rptr -= sizeof (ipoib_ptxhdr_t);
9707c478bd9Sstevel@tonic-gate 	tgldp = (ipoib_ptxhdr_t *)nmp->b_rptr;
9717c478bd9Sstevel@tonic-gate 	tgldp->ipoib_rhdr.ipoib_type = htons(type);
9727c478bd9Sstevel@tonic-gate 	tgldp->ipoib_rhdr.ipoib_mbz = 0;
9737c478bd9Sstevel@tonic-gate 	mac_copy(&gldp->ipoib_dest, &tgldp->ipoib_dest, IPOIB_ADDRL);
9747c478bd9Sstevel@tonic-gate 
9757c478bd9Sstevel@tonic-gate 	return (nmp);
9767c478bd9Sstevel@tonic-gate }
9777c478bd9Sstevel@tonic-gate 
9787c478bd9Sstevel@tonic-gate /* ==== */
9797c478bd9Sstevel@tonic-gate /* FDDI */
9807c478bd9Sstevel@tonic-gate /* ==== */
9817c478bd9Sstevel@tonic-gate 
9827c478bd9Sstevel@tonic-gate void
9837c478bd9Sstevel@tonic-gate gld_init_fddi(gld_mac_info_t *macinfo)
9847c478bd9Sstevel@tonic-gate {
9857c478bd9Sstevel@tonic-gate 	struct gldkstats *sp =
9867c478bd9Sstevel@tonic-gate 	    ((gld_mac_pvt_t *)macinfo->gldm_mac_pvt)->kstatp->ks_data;
9877c478bd9Sstevel@tonic-gate 
9887c478bd9Sstevel@tonic-gate 	/* Assumptions we make for this medium */
9897c478bd9Sstevel@tonic-gate 	ASSERT(macinfo->gldm_type == DL_FDDI);
9907c478bd9Sstevel@tonic-gate 	ASSERT(macinfo->gldm_addrlen == 6);
9917c478bd9Sstevel@tonic-gate 	ASSERT(macinfo->gldm_saplen == -2);
9927c478bd9Sstevel@tonic-gate #ifndef	lint
9937c478bd9Sstevel@tonic-gate 	ASSERT(sizeof (struct fddi_mac_frm) == 13);
9947c478bd9Sstevel@tonic-gate 	ASSERT(sizeof (mac_addr_t) == 6);
9957c478bd9Sstevel@tonic-gate #endif
9967c478bd9Sstevel@tonic-gate 
9977c478bd9Sstevel@tonic-gate 	/* Wire address format is bit reversed from canonical format */
9987c478bd9Sstevel@tonic-gate 	macinfo->gldm_options |= GLDOPT_CANONICAL_ADDR;
9997c478bd9Sstevel@tonic-gate 
10007c478bd9Sstevel@tonic-gate 	kstat_named_init(&sp->glds_fddi_mac_error,
10017c478bd9Sstevel@tonic-gate 	    "mac_errors", KSTAT_DATA_UINT32);
10027c478bd9Sstevel@tonic-gate 	kstat_named_init(&sp->glds_fddi_mac_lost,
10037c478bd9Sstevel@tonic-gate 	    "mac_lost_errors", KSTAT_DATA_UINT32);
10047c478bd9Sstevel@tonic-gate 	kstat_named_init(&sp->glds_fddi_mac_token,
10057c478bd9Sstevel@tonic-gate 	    "mac_tokens", KSTAT_DATA_UINT32);
10067c478bd9Sstevel@tonic-gate 	kstat_named_init(&sp->glds_fddi_mac_tvx_expired,
10077c478bd9Sstevel@tonic-gate 	    "mac_tvx_expired", KSTAT_DATA_UINT32);
10087c478bd9Sstevel@tonic-gate 	kstat_named_init(&sp->glds_fddi_mac_late,
10097c478bd9Sstevel@tonic-gate 	    "mac_late", KSTAT_DATA_UINT32);
10107c478bd9Sstevel@tonic-gate 	kstat_named_init(&sp->glds_fddi_mac_ring_op,
10117c478bd9Sstevel@tonic-gate 	    "mac_ring_ops", KSTAT_DATA_UINT32);
10127c478bd9Sstevel@tonic-gate }
10137c478bd9Sstevel@tonic-gate 
10147c478bd9Sstevel@tonic-gate /*ARGSUSED*/
10157c478bd9Sstevel@tonic-gate void
10167c478bd9Sstevel@tonic-gate gld_uninit_fddi(gld_mac_info_t *macinfo)
10177c478bd9Sstevel@tonic-gate {
10187c478bd9Sstevel@tonic-gate }
10197c478bd9Sstevel@tonic-gate 
10207c478bd9Sstevel@tonic-gate int
10217c478bd9Sstevel@tonic-gate gld_interpret_fddi(gld_mac_info_t *macinfo, mblk_t *mp, pktinfo_t *pktinfo,
10227c478bd9Sstevel@tonic-gate     packet_flag_t flags)
10237c478bd9Sstevel@tonic-gate {
10247c478bd9Sstevel@tonic-gate 	struct fddi_mac_frm *mh;
10257c478bd9Sstevel@tonic-gate 	gld_mac_pvt_t *mac_pvt;
10267c478bd9Sstevel@tonic-gate 	struct llc_snap_hdr *snaphdr;
10277c478bd9Sstevel@tonic-gate 	mblk_t *pmp = NULL;
10287c478bd9Sstevel@tonic-gate 
10297c478bd9Sstevel@tonic-gate 	/*
10307c478bd9Sstevel@tonic-gate 	 * Quickly handle receive fastpath; FDDI does not support IPQ hack.
10317c478bd9Sstevel@tonic-gate 	 */
10327c478bd9Sstevel@tonic-gate 	if (flags == GLD_RXQUICK) {
10337c478bd9Sstevel@tonic-gate 		pktinfo->pktLen = msgdsize(mp);
10347c478bd9Sstevel@tonic-gate 		return (-1);
10357c478bd9Sstevel@tonic-gate 	}
10367c478bd9Sstevel@tonic-gate 
10377c478bd9Sstevel@tonic-gate 	bzero((void *)pktinfo, sizeof (*pktinfo));
10387c478bd9Sstevel@tonic-gate 
10397c478bd9Sstevel@tonic-gate 	pktinfo->pktLen = msgdsize(mp);
10407c478bd9Sstevel@tonic-gate 
10417c478bd9Sstevel@tonic-gate 	/* make sure packet has at least a whole mac header */
10427c478bd9Sstevel@tonic-gate 	if (pktinfo->pktLen < sizeof (struct fddi_mac_frm))
10437c478bd9Sstevel@tonic-gate 		return (-1);
10447c478bd9Sstevel@tonic-gate 
10457c478bd9Sstevel@tonic-gate 	/* make sure the mac header falls into contiguous memory */
10467c478bd9Sstevel@tonic-gate 	if (MBLKL(mp) < sizeof (struct fddi_mac_frm)) {
10477c478bd9Sstevel@tonic-gate 		if ((pmp = msgpullup(mp, -1)) == NULL) {
10487c478bd9Sstevel@tonic-gate #ifdef GLD_DEBUG
10497c478bd9Sstevel@tonic-gate 			if (gld_debug & GLDERRS)
10507c478bd9Sstevel@tonic-gate 				cmn_err(CE_WARN,
10517c478bd9Sstevel@tonic-gate 				    "GLD: interpret_fddi cannot msgpullup");
10527c478bd9Sstevel@tonic-gate #endif
10537c478bd9Sstevel@tonic-gate 			return (-1);
10547c478bd9Sstevel@tonic-gate 		}
10557c478bd9Sstevel@tonic-gate 		mp = pmp;	/* this mblk contains the whole mac header */
10567c478bd9Sstevel@tonic-gate 	}
10577c478bd9Sstevel@tonic-gate 
10587c478bd9Sstevel@tonic-gate 	mh = (struct fddi_mac_frm *)mp->b_rptr;
10597c478bd9Sstevel@tonic-gate 
10607c478bd9Sstevel@tonic-gate 	/* Check to see if the mac is a broadcast or multicast address. */
10617c478bd9Sstevel@tonic-gate 	/* NB we are still in wire format (non canonical) */
10627c478bd9Sstevel@tonic-gate 	/* mac_eq works because ether_broadcast is the same either way */
10637c478bd9Sstevel@tonic-gate 	if (mac_eq(mh->fddi_dhost, ether_broadcast, macinfo->gldm_addrlen))
10647c478bd9Sstevel@tonic-gate 		pktinfo->isBroadcast = 1;
10657c478bd9Sstevel@tonic-gate 	else if (mh->fddi_dhost[0] & 0x80)
10667c478bd9Sstevel@tonic-gate 		pktinfo->isMulticast = 1;
10677c478bd9Sstevel@tonic-gate 
10687c478bd9Sstevel@tonic-gate 	if (flags == GLD_TX)
10697c478bd9Sstevel@tonic-gate 		goto out;	/* Got all info we need for xmit case */
10707c478bd9Sstevel@tonic-gate 
10717c478bd9Sstevel@tonic-gate 	ASSERT(GLDM_LOCK_HELD(macinfo));
10727c478bd9Sstevel@tonic-gate 
10737c478bd9Sstevel@tonic-gate 	/*
10747c478bd9Sstevel@tonic-gate 	 * Deal with the mac header
10757c478bd9Sstevel@tonic-gate 	 */
10767c478bd9Sstevel@tonic-gate 
10777c478bd9Sstevel@tonic-gate 	cmac_copy(mh->fddi_dhost, pktinfo->dhost,
10787c478bd9Sstevel@tonic-gate 	    macinfo->gldm_addrlen, macinfo);
10797c478bd9Sstevel@tonic-gate 	cmac_copy(mh->fddi_shost, pktinfo->shost,
10807c478bd9Sstevel@tonic-gate 	    macinfo->gldm_addrlen, macinfo);
10817c478bd9Sstevel@tonic-gate 
10827c478bd9Sstevel@tonic-gate 	mac_pvt = (gld_mac_pvt_t *)macinfo->gldm_mac_pvt;
10837c478bd9Sstevel@tonic-gate 	pktinfo->isLooped = mac_eq(pktinfo->shost,
10847c478bd9Sstevel@tonic-gate 	    mac_pvt->curr_macaddr, macinfo->gldm_addrlen);
10857c478bd9Sstevel@tonic-gate 	pktinfo->isForMe = mac_eq(pktinfo->dhost,
10867c478bd9Sstevel@tonic-gate 	    mac_pvt->curr_macaddr, macinfo->gldm_addrlen);
10877c478bd9Sstevel@tonic-gate 
10887c478bd9Sstevel@tonic-gate 	pktinfo->macLen = sizeof (struct fddi_mac_frm);
10897c478bd9Sstevel@tonic-gate 
10907c478bd9Sstevel@tonic-gate 	/*
10917c478bd9Sstevel@tonic-gate 	 * Before trying to look beyond the MAC header, make sure the LLC
10927c478bd9Sstevel@tonic-gate 	 * header exists, and that both it and any SNAP header are contiguous.
10937c478bd9Sstevel@tonic-gate 	 */
10947c478bd9Sstevel@tonic-gate 	if (MBLKL(mp) < sizeof (struct fddi_mac_frm) + LLC_SNAP_HDR_LEN &&
10957c478bd9Sstevel@tonic-gate 	    MBLKL(mp) < pktinfo->pktLen) {
10967c478bd9Sstevel@tonic-gate 		/*
10977c478bd9Sstevel@tonic-gate 		 * we don't have the entire packet within the first mblk (and
10987c478bd9Sstevel@tonic-gate 		 * therefore we didn't do the msgpullup above), AND the first
10997c478bd9Sstevel@tonic-gate 		 * mblk may not contain all the data we need to look at.
11007c478bd9Sstevel@tonic-gate 		 */
11017c478bd9Sstevel@tonic-gate 		ASSERT(pmp == NULL);	/* couldn't have done msgpullup above */
11027c478bd9Sstevel@tonic-gate 		if ((pmp = msgpullup(mp, -1)) == NULL) {
11037c478bd9Sstevel@tonic-gate #ifdef GLD_DEBUG
11047c478bd9Sstevel@tonic-gate 			if (gld_debug & GLDERRS)
11057c478bd9Sstevel@tonic-gate 				cmn_err(CE_WARN,
11067c478bd9Sstevel@tonic-gate 				    "GLD: interpret_fddi cannot msgpullup2");
11077c478bd9Sstevel@tonic-gate #endif
11087c478bd9Sstevel@tonic-gate 			goto out;	/* can't interpret this pkt further */
11097c478bd9Sstevel@tonic-gate 		}
11107c478bd9Sstevel@tonic-gate 		mp = pmp;	/* this mblk should contain everything needed */
11117c478bd9Sstevel@tonic-gate 	}
11127c478bd9Sstevel@tonic-gate 
11137c478bd9Sstevel@tonic-gate 	/*
11147c478bd9Sstevel@tonic-gate 	 * Check SAP/SNAP information.
11157c478bd9Sstevel@tonic-gate 	 */
11167c478bd9Sstevel@tonic-gate 	if ((mh->fddi_fc & 0x70) == 0x50) {
11177c478bd9Sstevel@tonic-gate 		if (pktinfo->pktLen < pktinfo->macLen + LLC_HDR1_LEN)
11187c478bd9Sstevel@tonic-gate 			goto out;
11197c478bd9Sstevel@tonic-gate 
11207c478bd9Sstevel@tonic-gate 		pktinfo->isLLC = 1;
11217c478bd9Sstevel@tonic-gate 
11227c478bd9Sstevel@tonic-gate 		if (pktinfo->pktLen < pktinfo->macLen + LLC_SNAP_HDR_LEN)
11237c478bd9Sstevel@tonic-gate 			goto out;
11247c478bd9Sstevel@tonic-gate 
11257c478bd9Sstevel@tonic-gate 		snaphdr = (struct llc_snap_hdr *)(mp->b_rptr + pktinfo->macLen);
11267c478bd9Sstevel@tonic-gate 		if (ISETHERTYPE(snaphdr)) {
11277c478bd9Sstevel@tonic-gate 			pktinfo->ethertype = REF_NET_USHORT(snaphdr->type);
11287c478bd9Sstevel@tonic-gate 			pktinfo->hdrLen = LLC_SNAP_HDR_LEN;
11297c478bd9Sstevel@tonic-gate 		}
11307c478bd9Sstevel@tonic-gate 	}
11317c478bd9Sstevel@tonic-gate out:
11327c478bd9Sstevel@tonic-gate 	if (pmp != NULL)
11337c478bd9Sstevel@tonic-gate 		freemsg(pmp);
11347c478bd9Sstevel@tonic-gate 
11357c478bd9Sstevel@tonic-gate 	return (0);
11367c478bd9Sstevel@tonic-gate }
11377c478bd9Sstevel@tonic-gate 
11387c478bd9Sstevel@tonic-gate mblk_t *
11397c478bd9Sstevel@tonic-gate gld_unitdata_fddi(gld_t *gld, mblk_t *mp)
11407c478bd9Sstevel@tonic-gate {
11417c478bd9Sstevel@tonic-gate 	gld_mac_info_t *macinfo = gld->gld_mac_info;
11427c478bd9Sstevel@tonic-gate 	dl_unitdata_req_t *dlp = (dl_unitdata_req_t *)mp->b_rptr;
11437c478bd9Sstevel@tonic-gate 	struct gld_dlsap *gldp = DLSAP(dlp, dlp->dl_dest_addr_offset);
11447c478bd9Sstevel@tonic-gate 	mac_addr_t dhost;
11457c478bd9Sstevel@tonic-gate 	unsigned short type;
11467c478bd9Sstevel@tonic-gate 	mblk_t *nmp;
11477c478bd9Sstevel@tonic-gate 	struct fddi_mac_frm *mh;
11487c478bd9Sstevel@tonic-gate 	int hdrlen;
11497c478bd9Sstevel@tonic-gate 
11507c478bd9Sstevel@tonic-gate 	ASSERT(macinfo);
11517c478bd9Sstevel@tonic-gate 
11527c478bd9Sstevel@tonic-gate 	/* extract needed info from the mblk before we maybe reuse it */
11537c478bd9Sstevel@tonic-gate 	mac_copy(gldp->glda_addr, dhost, macinfo->gldm_addrlen);
11547c478bd9Sstevel@tonic-gate 
11557c478bd9Sstevel@tonic-gate 	/* look in the unitdata request for a sap, else use bound one */
11567c478bd9Sstevel@tonic-gate 	if (dlp->dl_dest_addr_length >= DLSAPLENGTH(macinfo) &&
11577c478bd9Sstevel@tonic-gate 	    REF_HOST_USHORT(gldp->glda_sap) != 0)
11587c478bd9Sstevel@tonic-gate 		type = REF_HOST_USHORT(gldp->glda_sap);
11597c478bd9Sstevel@tonic-gate 	else
11607c478bd9Sstevel@tonic-gate 		type = gld->gld_sap;
11617c478bd9Sstevel@tonic-gate 
11627c478bd9Sstevel@tonic-gate 
11637c478bd9Sstevel@tonic-gate 	hdrlen = sizeof (struct fddi_mac_frm);
11647c478bd9Sstevel@tonic-gate 
11657c478bd9Sstevel@tonic-gate 	/*
11667c478bd9Sstevel@tonic-gate 	 * Check whether we need to do EtherType encoding or whether the packet
11677c478bd9Sstevel@tonic-gate 	 * is LLC.
11687c478bd9Sstevel@tonic-gate 	 */
11697c478bd9Sstevel@tonic-gate 	if (type > GLD_MAX_802_SAP)
11707c478bd9Sstevel@tonic-gate 		hdrlen += sizeof (struct llc_snap_hdr);
11717c478bd9Sstevel@tonic-gate 
11727c478bd9Sstevel@tonic-gate 	/* need a buffer big enough for the headers */
11737c478bd9Sstevel@tonic-gate 	nmp = mp->b_cont;	/* where the packet payload M_DATA is */
11747c478bd9Sstevel@tonic-gate 	if (DB_REF(nmp) == 1 && MBLKHEAD(nmp) >= hdrlen) {
11757c478bd9Sstevel@tonic-gate 		/* it fits at the beginning of the first M_DATA block */
11767c478bd9Sstevel@tonic-gate 		freeb(mp);	/* don't need the M_PROTO anymore */
11777c478bd9Sstevel@tonic-gate 	} else if (DB_REF(mp) == 1 && MBLKSIZE(mp) >= hdrlen) {
11787c478bd9Sstevel@tonic-gate 		/* we can reuse the dl_unitdata_req M_PROTO mblk */
11797c478bd9Sstevel@tonic-gate 		nmp = mp;
11807c478bd9Sstevel@tonic-gate 		DB_TYPE(nmp) = M_DATA;
11817c478bd9Sstevel@tonic-gate 		nmp->b_rptr = nmp->b_wptr = DB_LIM(nmp);
11827c478bd9Sstevel@tonic-gate 	} else {
11837c478bd9Sstevel@tonic-gate 		/* we need to allocate one */
11847c478bd9Sstevel@tonic-gate 		if ((nmp = allocb(hdrlen, BPRI_MED)) == NULL)
11857c478bd9Sstevel@tonic-gate 			return (NULL);
11867c478bd9Sstevel@tonic-gate 		nmp->b_rptr = nmp->b_wptr = DB_LIM(nmp);
11877c478bd9Sstevel@tonic-gate 		linkb(nmp, mp->b_cont);
11887c478bd9Sstevel@tonic-gate 		freeb(mp);
11897c478bd9Sstevel@tonic-gate 	}
11907c478bd9Sstevel@tonic-gate 
11917c478bd9Sstevel@tonic-gate 
11927c478bd9Sstevel@tonic-gate 	/* Got the space, now copy in the header components */
11937c478bd9Sstevel@tonic-gate 	if (type > GLD_MAX_802_SAP) {
11947c478bd9Sstevel@tonic-gate 		/* create the snap header */
11957c478bd9Sstevel@tonic-gate 		struct llc_snap_hdr *snap;
11967c478bd9Sstevel@tonic-gate 		nmp->b_rptr -= sizeof (struct llc_snap_hdr);
11977c478bd9Sstevel@tonic-gate 		snap  = (struct llc_snap_hdr *)(nmp->b_rptr);
11987c478bd9Sstevel@tonic-gate 		*snap = llc_snap_def;
11997c478bd9Sstevel@tonic-gate 		SET_NET_USHORT(snap->type, type);
12007c478bd9Sstevel@tonic-gate 	}
12017c478bd9Sstevel@tonic-gate 
12027c478bd9Sstevel@tonic-gate 	nmp->b_rptr -= sizeof (struct fddi_mac_frm);
12037c478bd9Sstevel@tonic-gate 
12047c478bd9Sstevel@tonic-gate 	mh = (struct fddi_mac_frm *)nmp->b_rptr;
12057c478bd9Sstevel@tonic-gate 
12067c478bd9Sstevel@tonic-gate 	mh->fddi_fc = 0x50;
12077c478bd9Sstevel@tonic-gate 	cmac_copy(dhost, mh->fddi_dhost, macinfo->gldm_addrlen, macinfo);
12087c478bd9Sstevel@tonic-gate 
12097c478bd9Sstevel@tonic-gate 	/*
12107c478bd9Sstevel@tonic-gate 	 * We access the mac address without the mutex to prevent
12117c478bd9Sstevel@tonic-gate 	 * mutex contention (BUG 4211361)
12127c478bd9Sstevel@tonic-gate 	 */
12137c478bd9Sstevel@tonic-gate 	cmac_copy(((gld_mac_pvt_t *)macinfo->gldm_mac_pvt)->curr_macaddr,
12147c478bd9Sstevel@tonic-gate 	    mh->fddi_shost, macinfo->gldm_addrlen, macinfo);
12157c478bd9Sstevel@tonic-gate 	return (nmp);
12167c478bd9Sstevel@tonic-gate }
12177c478bd9Sstevel@tonic-gate 
12187c478bd9Sstevel@tonic-gate mblk_t *
12197c478bd9Sstevel@tonic-gate gld_fastpath_fddi(gld_t *gld, mblk_t *mp)
12207c478bd9Sstevel@tonic-gate {
12217c478bd9Sstevel@tonic-gate 	gld_mac_info_t *macinfo = gld->gld_mac_info;
12227c478bd9Sstevel@tonic-gate 	dl_unitdata_req_t *dlp = (dl_unitdata_req_t *)mp->b_cont->b_rptr;
12237c478bd9Sstevel@tonic-gate 	struct gld_dlsap *gldp = DLSAP(dlp, dlp->dl_dest_addr_offset);
12247c478bd9Sstevel@tonic-gate 	unsigned short type;
12257c478bd9Sstevel@tonic-gate 	mblk_t *nmp;
12267c478bd9Sstevel@tonic-gate 	struct fddi_mac_frm *mh;
12277c478bd9Sstevel@tonic-gate 	int hdrlen;
12287c478bd9Sstevel@tonic-gate 
12297c478bd9Sstevel@tonic-gate 	ASSERT(macinfo);
12307c478bd9Sstevel@tonic-gate 
12317c478bd9Sstevel@tonic-gate 	/* look in the unitdata request for a sap, else use bound one */
12327c478bd9Sstevel@tonic-gate 	if (dlp->dl_dest_addr_length >= DLSAPLENGTH(macinfo) &&
12337c478bd9Sstevel@tonic-gate 	    REF_HOST_USHORT(gldp->glda_sap) != 0)
12347c478bd9Sstevel@tonic-gate 		type = REF_HOST_USHORT(gldp->glda_sap);
12357c478bd9Sstevel@tonic-gate 	else
12367c478bd9Sstevel@tonic-gate 		type = gld->gld_sap;
12377c478bd9Sstevel@tonic-gate 
12387c478bd9Sstevel@tonic-gate 	hdrlen = sizeof (struct fddi_mac_frm);
12397c478bd9Sstevel@tonic-gate 
12407c478bd9Sstevel@tonic-gate 	/*
12417c478bd9Sstevel@tonic-gate 	 * Check whether we need to do EtherType encoding or whether the packet
12427c478bd9Sstevel@tonic-gate 	 * will be LLC.
12437c478bd9Sstevel@tonic-gate 	 */
12447c478bd9Sstevel@tonic-gate 	if (type > GLD_MAX_802_SAP)
12457c478bd9Sstevel@tonic-gate 		hdrlen += sizeof (struct llc_snap_hdr);
12467c478bd9Sstevel@tonic-gate 
12477c478bd9Sstevel@tonic-gate 	if ((nmp = allocb(hdrlen, BPRI_MED)) == NULL)
12487c478bd9Sstevel@tonic-gate 		return (NULL);
12497c478bd9Sstevel@tonic-gate 
12507c478bd9Sstevel@tonic-gate 	nmp->b_rptr = nmp->b_wptr = DB_LIM(nmp);
12517c478bd9Sstevel@tonic-gate 
12527c478bd9Sstevel@tonic-gate 	/* Got the space, now copy in the header components */
12537c478bd9Sstevel@tonic-gate 
12547c478bd9Sstevel@tonic-gate 	if (type > GLD_MAX_802_SAP) {
12557c478bd9Sstevel@tonic-gate 		/* create the snap header */
12567c478bd9Sstevel@tonic-gate 		struct llc_snap_hdr *snap;
12577c478bd9Sstevel@tonic-gate 		nmp->b_rptr -= sizeof (struct llc_snap_hdr);
12587c478bd9Sstevel@tonic-gate 		snap  = (struct llc_snap_hdr *)(nmp->b_rptr);
12597c478bd9Sstevel@tonic-gate 		*snap = llc_snap_def;
12607c478bd9Sstevel@tonic-gate 		snap->type = htons(type);	/* we know it's aligned */
12617c478bd9Sstevel@tonic-gate 	}
12627c478bd9Sstevel@tonic-gate 
12637c478bd9Sstevel@tonic-gate 	nmp->b_rptr -= sizeof (struct fddi_mac_frm);
12647c478bd9Sstevel@tonic-gate 
12657c478bd9Sstevel@tonic-gate 	mh = (struct fddi_mac_frm *)nmp->b_rptr;
12667c478bd9Sstevel@tonic-gate 	mh->fddi_fc = 0x50;
12677c478bd9Sstevel@tonic-gate 	cmac_copy(gldp->glda_addr, mh->fddi_dhost,
12687c478bd9Sstevel@tonic-gate 	    macinfo->gldm_addrlen, macinfo);
12697c478bd9Sstevel@tonic-gate 
12707c478bd9Sstevel@tonic-gate 	GLDM_LOCK(macinfo, RW_WRITER);
12717c478bd9Sstevel@tonic-gate 	cmac_copy(((gld_mac_pvt_t *)macinfo->gldm_mac_pvt)->curr_macaddr,
12727c478bd9Sstevel@tonic-gate 	    mh->fddi_shost, macinfo->gldm_addrlen, macinfo);
12737c478bd9Sstevel@tonic-gate 	GLDM_UNLOCK(macinfo);
12747c478bd9Sstevel@tonic-gate 
12757c478bd9Sstevel@tonic-gate 	return (nmp);
12767c478bd9Sstevel@tonic-gate }
12777c478bd9Sstevel@tonic-gate 
12787c478bd9Sstevel@tonic-gate /* ========== */
12797c478bd9Sstevel@tonic-gate /* Token Ring */
12807c478bd9Sstevel@tonic-gate /* ========== */
12817c478bd9Sstevel@tonic-gate 
12827c478bd9Sstevel@tonic-gate #define	GLD_SR_VAR(macinfo)	\
12837c478bd9Sstevel@tonic-gate 	(((gld_mac_pvt_t *)macinfo->gldm_mac_pvt)->data)
12847c478bd9Sstevel@tonic-gate 
12857c478bd9Sstevel@tonic-gate #define	GLD_SR_HASH(macinfo)	((struct srtab **)GLD_SR_VAR(macinfo))
12867c478bd9Sstevel@tonic-gate 
12877c478bd9Sstevel@tonic-gate #define	GLD_SR_MUTEX(macinfo)	\
12887c478bd9Sstevel@tonic-gate 	(&((gld_mac_pvt_t *)macinfo->gldm_mac_pvt)->datalock)
12897c478bd9Sstevel@tonic-gate 
12907c478bd9Sstevel@tonic-gate static void gld_sr_clear(gld_mac_info_t *);
12917c478bd9Sstevel@tonic-gate static void gld_rcc_receive(gld_mac_info_t *, pktinfo_t *, struct gld_ri *,
12927c478bd9Sstevel@tonic-gate     uchar_t *, int);
12937c478bd9Sstevel@tonic-gate static void gld_rcc_send(gld_mac_info_t *, queue_t *, uchar_t *,
12947c478bd9Sstevel@tonic-gate     struct gld_ri **, uchar_t *);
12957c478bd9Sstevel@tonic-gate 
12967c478bd9Sstevel@tonic-gate static mac_addr_t tokenbroadcastaddr2 = { 0xc0, 0x00, 0xff, 0xff, 0xff, 0xff };
12977c478bd9Sstevel@tonic-gate static struct gld_ri ri_ste_def;
12987c478bd9Sstevel@tonic-gate 
12997c478bd9Sstevel@tonic-gate void
13007c478bd9Sstevel@tonic-gate gld_init_tr(gld_mac_info_t *macinfo)
13017c478bd9Sstevel@tonic-gate {
13027c478bd9Sstevel@tonic-gate 	struct gldkstats *sp =
13037c478bd9Sstevel@tonic-gate 	    ((gld_mac_pvt_t *)macinfo->gldm_mac_pvt)->kstatp->ks_data;
13047c478bd9Sstevel@tonic-gate 
13057c478bd9Sstevel@tonic-gate 	/* avoid endian-dependent code by initializing here instead of static */
13067c478bd9Sstevel@tonic-gate 	ri_ste_def.len = 2;
13077c478bd9Sstevel@tonic-gate 	ri_ste_def.rt = RT_STE;
13087c478bd9Sstevel@tonic-gate 	ri_ste_def.mtu = RT_MTU_MAX;
13097c478bd9Sstevel@tonic-gate 	ri_ste_def.dir = 0;
13107c478bd9Sstevel@tonic-gate 	ri_ste_def.res = 0;
13117c478bd9Sstevel@tonic-gate 
13127c478bd9Sstevel@tonic-gate 	/* Assumptions we make for this medium */
13137c478bd9Sstevel@tonic-gate 	ASSERT(macinfo->gldm_type == DL_TPR);
13147c478bd9Sstevel@tonic-gate 	ASSERT(macinfo->gldm_addrlen == 6);
13157c478bd9Sstevel@tonic-gate 	ASSERT(macinfo->gldm_saplen == -2);
13167c478bd9Sstevel@tonic-gate #ifndef	lint
13177c478bd9Sstevel@tonic-gate 	ASSERT(sizeof (struct tr_mac_frm_nori) == 14);
13187c478bd9Sstevel@tonic-gate 	ASSERT(sizeof (mac_addr_t) == 6);
13197c478bd9Sstevel@tonic-gate #endif
13207c478bd9Sstevel@tonic-gate 
13217c478bd9Sstevel@tonic-gate 	mutex_init(GLD_SR_MUTEX(macinfo), NULL, MUTEX_DRIVER, NULL);
13227c478bd9Sstevel@tonic-gate 
13237c478bd9Sstevel@tonic-gate 	GLD_SR_VAR(macinfo) = kmem_zalloc(sizeof (struct srtab *)*SR_HASH_SIZE,
13247c478bd9Sstevel@tonic-gate 				KM_SLEEP);
13257c478bd9Sstevel@tonic-gate 
13267c478bd9Sstevel@tonic-gate 	/* Default is RDE enabled for this medium */
13277c478bd9Sstevel@tonic-gate 	((gld_mac_pvt_t *)macinfo->gldm_mac_pvt)->rde_enabled =
13287c478bd9Sstevel@tonic-gate 	    ddi_getprop(DDI_DEV_T_NONE, macinfo->gldm_devinfo, 0,
13297c478bd9Sstevel@tonic-gate 	    "gld_rde_enable", 1);
13307c478bd9Sstevel@tonic-gate 
13317c478bd9Sstevel@tonic-gate 	/*
13327c478bd9Sstevel@tonic-gate 	 * Default is to use STE for unknown paths if RDE is enabled.
13337c478bd9Sstevel@tonic-gate 	 * If RDE is disabled, default is to use NULL RIF fields.
13347c478bd9Sstevel@tonic-gate 	 *
13357c478bd9Sstevel@tonic-gate 	 * It's possible to force use of STE for ALL packets:
13367c478bd9Sstevel@tonic-gate 	 * disable RDE but enable STE.  This may be useful for
13377c478bd9Sstevel@tonic-gate 	 * non-transparent bridges, when it is not desired to run
13387c478bd9Sstevel@tonic-gate 	 * the RDE algorithms.
13397c478bd9Sstevel@tonic-gate 	 */
13407c478bd9Sstevel@tonic-gate 	((gld_mac_pvt_t *)macinfo->gldm_mac_pvt)->rde_str_indicator_ste =
13417c478bd9Sstevel@tonic-gate 	    ddi_getprop(DDI_DEV_T_NONE, macinfo->gldm_devinfo, 0,
13427c478bd9Sstevel@tonic-gate 	    "gld_rde_str_indicator_ste",
13437c478bd9Sstevel@tonic-gate 	    ((gld_mac_pvt_t *)macinfo->gldm_mac_pvt)->rde_enabled);
13447c478bd9Sstevel@tonic-gate 
13457c478bd9Sstevel@tonic-gate 	/* Default 10 second route timeout on lack of activity */
13467c478bd9Sstevel@tonic-gate 	{
13477c478bd9Sstevel@tonic-gate 	int t = ddi_getprop(DDI_DEV_T_NONE, macinfo->gldm_devinfo, 0,
13487c478bd9Sstevel@tonic-gate 	    "gld_rde_timeout", 10);
13497c478bd9Sstevel@tonic-gate 	if (t < 1)
13507c478bd9Sstevel@tonic-gate 		t = 1;		/* Let's be reasonable */
13517c478bd9Sstevel@tonic-gate 	if (t > 600)
13527c478bd9Sstevel@tonic-gate 		t = 600;	/* Let's be reasonable */
13537c478bd9Sstevel@tonic-gate 	/* We're using ticks (lbolts) for our timeout -- convert from seconds */
13547c478bd9Sstevel@tonic-gate 	t = drv_usectohz(1000000 * t);
13557c478bd9Sstevel@tonic-gate 	((gld_mac_pvt_t *)macinfo->gldm_mac_pvt)->rde_timeout = t;
13567c478bd9Sstevel@tonic-gate 	}
13577c478bd9Sstevel@tonic-gate 
13587c478bd9Sstevel@tonic-gate 	kstat_named_init(&sp->glds_dot5_line_error,
13597c478bd9Sstevel@tonic-gate 	    "line_errors", KSTAT_DATA_UINT32);
13607c478bd9Sstevel@tonic-gate 	kstat_named_init(&sp->glds_dot5_burst_error,
13617c478bd9Sstevel@tonic-gate 	    "burst_errors", KSTAT_DATA_UINT32);
13627c478bd9Sstevel@tonic-gate 	kstat_named_init(&sp->glds_dot5_signal_loss,
13637c478bd9Sstevel@tonic-gate 	    "signal_losses", KSTAT_DATA_UINT32);
13647c478bd9Sstevel@tonic-gate 
13657c478bd9Sstevel@tonic-gate 	/*
13667c478bd9Sstevel@tonic-gate 	 * only initialize the new statistics if the driver
13677c478bd9Sstevel@tonic-gate 	 * knows about them.
13687c478bd9Sstevel@tonic-gate 	 */
13697c478bd9Sstevel@tonic-gate 	if (macinfo->gldm_driver_version != GLD_VERSION_200)
13707c478bd9Sstevel@tonic-gate 		return;
13717c478bd9Sstevel@tonic-gate 
13727c478bd9Sstevel@tonic-gate 	kstat_named_init(&sp->glds_dot5_ace_error,
13737c478bd9Sstevel@tonic-gate 	    "ace_errors", KSTAT_DATA_UINT32);
13747c478bd9Sstevel@tonic-gate 	kstat_named_init(&sp->glds_dot5_internal_error,
13757c478bd9Sstevel@tonic-gate 	    "internal_errors", KSTAT_DATA_UINT32);
13767c478bd9Sstevel@tonic-gate 	kstat_named_init(&sp->glds_dot5_lost_frame_error,
13777c478bd9Sstevel@tonic-gate 	    "lost_frame_errors", KSTAT_DATA_UINT32);
13787c478bd9Sstevel@tonic-gate 	kstat_named_init(&sp->glds_dot5_frame_copied_error,
13797c478bd9Sstevel@tonic-gate 	    "frame_copied_errors", KSTAT_DATA_UINT32);
13807c478bd9Sstevel@tonic-gate 	kstat_named_init(&sp->glds_dot5_token_error,
13817c478bd9Sstevel@tonic-gate 	    "token_errors", KSTAT_DATA_UINT32);
13827c478bd9Sstevel@tonic-gate 	kstat_named_init(&sp->glds_dot5_freq_error,
13837c478bd9Sstevel@tonic-gate 	    "freq_errors", KSTAT_DATA_UINT32);
13847c478bd9Sstevel@tonic-gate }
13857c478bd9Sstevel@tonic-gate 
13867c478bd9Sstevel@tonic-gate void
13877c478bd9Sstevel@tonic-gate gld_uninit_tr(gld_mac_info_t *macinfo)
13887c478bd9Sstevel@tonic-gate {
13897c478bd9Sstevel@tonic-gate 	mutex_destroy(GLD_SR_MUTEX(macinfo));
13907c478bd9Sstevel@tonic-gate 	gld_sr_clear(macinfo);
13917c478bd9Sstevel@tonic-gate 	kmem_free(GLD_SR_VAR(macinfo), sizeof (struct srtab *) * SR_HASH_SIZE);
13927c478bd9Sstevel@tonic-gate }
13937c478bd9Sstevel@tonic-gate 
13947c478bd9Sstevel@tonic-gate int
13957c478bd9Sstevel@tonic-gate gld_interpret_tr(gld_mac_info_t *macinfo, mblk_t *mp, pktinfo_t *pktinfo,
13967c478bd9Sstevel@tonic-gate     packet_flag_t flags)
13977c478bd9Sstevel@tonic-gate {
13987c478bd9Sstevel@tonic-gate 	struct tr_mac_frm *mh;
13997c478bd9Sstevel@tonic-gate 	gld_mac_pvt_t *mac_pvt;
14007c478bd9Sstevel@tonic-gate 	struct llc_snap_hdr *snaphdr;
14017c478bd9Sstevel@tonic-gate 	mblk_t *pmp = NULL;
14027c478bd9Sstevel@tonic-gate 	struct gld_ri *rh;
14037c478bd9Sstevel@tonic-gate 
14047c478bd9Sstevel@tonic-gate 	/*
14057c478bd9Sstevel@tonic-gate 	 * Quickly handle receive fastpath; TR does not support IPQ hack.
14067c478bd9Sstevel@tonic-gate 	 */
14077c478bd9Sstevel@tonic-gate 	if (flags == GLD_RXQUICK) {
14087c478bd9Sstevel@tonic-gate 		pktinfo->pktLen = msgdsize(mp);
14097c478bd9Sstevel@tonic-gate 		return (-1);
14107c478bd9Sstevel@tonic-gate 	}
14117c478bd9Sstevel@tonic-gate 
14127c478bd9Sstevel@tonic-gate 	bzero((void *)pktinfo, sizeof (*pktinfo));
14137c478bd9Sstevel@tonic-gate 
14147c478bd9Sstevel@tonic-gate 	pktinfo->pktLen = msgdsize(mp);
14157c478bd9Sstevel@tonic-gate 
14167c478bd9Sstevel@tonic-gate 	/* make sure packet has at least a whole mac header */
14177c478bd9Sstevel@tonic-gate 	if (pktinfo->pktLen < sizeof (struct tr_mac_frm_nori))
14187c478bd9Sstevel@tonic-gate 		return (-1);
14197c478bd9Sstevel@tonic-gate 
14207c478bd9Sstevel@tonic-gate 	/* make sure the mac header falls into contiguous memory */
14217c478bd9Sstevel@tonic-gate 	if (MBLKL(mp) < sizeof (struct tr_mac_frm_nori)) {
14227c478bd9Sstevel@tonic-gate 		if ((pmp = msgpullup(mp, -1)) == NULL) {
14237c478bd9Sstevel@tonic-gate #ifdef GLD_DEBUG
14247c478bd9Sstevel@tonic-gate 			if (gld_debug & GLDERRS)
14257c478bd9Sstevel@tonic-gate 				cmn_err(CE_WARN,
14267c478bd9Sstevel@tonic-gate 				    "GLD: interpret_tr cannot msgpullup");
14277c478bd9Sstevel@tonic-gate #endif
14287c478bd9Sstevel@tonic-gate 			return (-1);
14297c478bd9Sstevel@tonic-gate 		}
14307c478bd9Sstevel@tonic-gate 		mp = pmp;	/* this mblk contains the whole mac header */
14317c478bd9Sstevel@tonic-gate 	}
14327c478bd9Sstevel@tonic-gate 
14337c478bd9Sstevel@tonic-gate 	mh = (struct tr_mac_frm *)mp->b_rptr;
14347c478bd9Sstevel@tonic-gate 
14357c478bd9Sstevel@tonic-gate 	/* Check to see if the mac is a broadcast or multicast address. */
14367c478bd9Sstevel@tonic-gate 	if (mac_eq(mh->tr_dhost, ether_broadcast, macinfo->gldm_addrlen) ||
14377c478bd9Sstevel@tonic-gate 	    mac_eq(mh->tr_dhost, tokenbroadcastaddr2, macinfo->gldm_addrlen))
14387c478bd9Sstevel@tonic-gate 		pktinfo->isBroadcast = 1;
14397c478bd9Sstevel@tonic-gate 	else if (mh->tr_dhost[0] & 0x80)
14407c478bd9Sstevel@tonic-gate 		pktinfo->isMulticast = 1;
14417c478bd9Sstevel@tonic-gate 
14427c478bd9Sstevel@tonic-gate 	if (flags == GLD_TX)
14437c478bd9Sstevel@tonic-gate 		goto out;	/* Got all info we need for xmit case */
14447c478bd9Sstevel@tonic-gate 
14457c478bd9Sstevel@tonic-gate 	ASSERT(GLDM_LOCK_HELD(macinfo));
14467c478bd9Sstevel@tonic-gate 
14477c478bd9Sstevel@tonic-gate 	/*
14487c478bd9Sstevel@tonic-gate 	 * Deal with the mac header
14497c478bd9Sstevel@tonic-gate 	 */
14507c478bd9Sstevel@tonic-gate 
14517c478bd9Sstevel@tonic-gate 	mac_copy(mh->tr_dhost, pktinfo->dhost, macinfo->gldm_addrlen);
14527c478bd9Sstevel@tonic-gate 	mac_copy(mh->tr_shost, pktinfo->shost, macinfo->gldm_addrlen);
14537c478bd9Sstevel@tonic-gate 	pktinfo->shost[0] &= ~0x80;	/* turn off RIF indicator */
14547c478bd9Sstevel@tonic-gate 
14557c478bd9Sstevel@tonic-gate 	mac_pvt = (gld_mac_pvt_t *)macinfo->gldm_mac_pvt;
14567c478bd9Sstevel@tonic-gate 	pktinfo->isLooped = mac_eq(pktinfo->shost,
14577c478bd9Sstevel@tonic-gate 	    mac_pvt->curr_macaddr, macinfo->gldm_addrlen);
14587c478bd9Sstevel@tonic-gate 	pktinfo->isForMe = mac_eq(pktinfo->dhost,
14597c478bd9Sstevel@tonic-gate 	    mac_pvt->curr_macaddr, macinfo->gldm_addrlen);
14607c478bd9Sstevel@tonic-gate 
14617c478bd9Sstevel@tonic-gate 	rh = (struct gld_ri *)NULL;
14627c478bd9Sstevel@tonic-gate 	pktinfo->macLen = sizeof (struct tr_mac_frm_nori);
14637c478bd9Sstevel@tonic-gate 
14647c478bd9Sstevel@tonic-gate 	/*
14657c478bd9Sstevel@tonic-gate 	 * Before trying to look beyond the MAC header, make sure the data
14667c478bd9Sstevel@tonic-gate 	 * structures are all contiguously where we can conveniently look at
14677c478bd9Sstevel@tonic-gate 	 * them.  We'll use a worst-case estimate of how many bytes into the
14687c478bd9Sstevel@tonic-gate 	 * packet data we'll be needing to look.  Things will be more efficient
14697c478bd9Sstevel@tonic-gate 	 * if the driver puts at least this much into the first mblk.
14707c478bd9Sstevel@tonic-gate 	 *
14717c478bd9Sstevel@tonic-gate 	 * Even after this, we still will have to do checks against the total
14727c478bd9Sstevel@tonic-gate 	 * length of the packet.  A bad incoming packet may not hold all the
14737c478bd9Sstevel@tonic-gate 	 * data structures it says it does.
14747c478bd9Sstevel@tonic-gate 	 */
14757c478bd9Sstevel@tonic-gate 	if (MBLKL(mp) < sizeof (struct tr_mac_frm) +
14767c478bd9Sstevel@tonic-gate 	    LLC_HDR1_LEN + sizeof (struct rde_pdu) &&
14777c478bd9Sstevel@tonic-gate 	    MBLKL(mp) < pktinfo->pktLen) {
14787c478bd9Sstevel@tonic-gate 		/*
14797c478bd9Sstevel@tonic-gate 		 * we don't have the entire packet within the first mblk (and
14807c478bd9Sstevel@tonic-gate 		 * therefore we didn't do the msgpullup above), AND the first
14817c478bd9Sstevel@tonic-gate 		 * mblk may not contain all the data we need to look at.
14827c478bd9Sstevel@tonic-gate 		 */
14837c478bd9Sstevel@tonic-gate 		ASSERT(pmp == NULL);	/* couldn't have done msgpullup above */
14847c478bd9Sstevel@tonic-gate 		if ((pmp = msgpullup(mp, -1)) == NULL) {
14857c478bd9Sstevel@tonic-gate #ifdef GLD_DEBUG
14867c478bd9Sstevel@tonic-gate 			if (gld_debug & GLDERRS)
14877c478bd9Sstevel@tonic-gate 				cmn_err(CE_WARN,
14887c478bd9Sstevel@tonic-gate 				    "GLD: interpret_tr cannot msgpullup2");
14897c478bd9Sstevel@tonic-gate #endif
14907c478bd9Sstevel@tonic-gate 			goto out;	/* can't interpret this pkt further */
14917c478bd9Sstevel@tonic-gate 		}
14927c478bd9Sstevel@tonic-gate 		mp = pmp;	/* this mblk should contain everything needed */
14937c478bd9Sstevel@tonic-gate 		mh = (struct tr_mac_frm *)mp->b_rptr;	/* to look at RIF */
14947c478bd9Sstevel@tonic-gate 	}
14957c478bd9Sstevel@tonic-gate 
14967c478bd9Sstevel@tonic-gate 	if (mh->tr_shost[0] & 0x80) {
14977c478bd9Sstevel@tonic-gate 		/* Routing Information Field (RIF) is present */
14987c478bd9Sstevel@tonic-gate 		if (pktinfo->pktLen < sizeof (struct tr_mac_frm_nori) + 2)
14997c478bd9Sstevel@tonic-gate 			goto out;	/* RIF should have been there! */
15007c478bd9Sstevel@tonic-gate 		rh = (struct gld_ri *)&mh->tr_ri;
15017c478bd9Sstevel@tonic-gate 		if ((rh->len & 1) || rh->len < 2) {
15027c478bd9Sstevel@tonic-gate 			/* Bogus RIF, don't handle this packet */
15037c478bd9Sstevel@tonic-gate #ifdef GLD_DEBUG
15047c478bd9Sstevel@tonic-gate 			if (gld_debug & GLDERRS)
15057c478bd9Sstevel@tonic-gate 				cmn_err(CE_WARN,
15067c478bd9Sstevel@tonic-gate 				    "GLD: received TR packet with "
15077c478bd9Sstevel@tonic-gate 				    "bogus RIF length %d",
15087c478bd9Sstevel@tonic-gate 				    rh->len);
15097c478bd9Sstevel@tonic-gate #endif
15107c478bd9Sstevel@tonic-gate 			goto out;
15117c478bd9Sstevel@tonic-gate 		}
15127c478bd9Sstevel@tonic-gate 		if (pktinfo->pktLen < sizeof (struct tr_mac_frm_nori) + rh->len)
15137c478bd9Sstevel@tonic-gate 			goto out;	/* RIF should have been there! */
15147c478bd9Sstevel@tonic-gate 		pktinfo->macLen += rh->len;
15157c478bd9Sstevel@tonic-gate 	}
15167c478bd9Sstevel@tonic-gate 
15177c478bd9Sstevel@tonic-gate 	if ((mh->tr_fc & 0xc0) == 0x40) {
15187c478bd9Sstevel@tonic-gate 		if (pktinfo->pktLen < pktinfo->macLen + LLC_HDR1_LEN)
15197c478bd9Sstevel@tonic-gate 			goto out;
15207c478bd9Sstevel@tonic-gate 
15217c478bd9Sstevel@tonic-gate 		pktinfo->isLLC = 1;
15227c478bd9Sstevel@tonic-gate 
15237c478bd9Sstevel@tonic-gate 		if (pktinfo->pktLen < pktinfo->macLen + LLC_SNAP_HDR_LEN)
15247c478bd9Sstevel@tonic-gate 			goto out;
15257c478bd9Sstevel@tonic-gate 
15267c478bd9Sstevel@tonic-gate 		snaphdr = (struct llc_snap_hdr *)(mp->b_rptr + pktinfo->macLen);
15277c478bd9Sstevel@tonic-gate 		if (ISETHERTYPE(snaphdr)) {
15287c478bd9Sstevel@tonic-gate 			pktinfo->ethertype = REF_NET_USHORT(snaphdr->type);
15297c478bd9Sstevel@tonic-gate 			pktinfo->hdrLen = LLC_SNAP_HDR_LEN;
15307c478bd9Sstevel@tonic-gate 		}
15317c478bd9Sstevel@tonic-gate 
15327c478bd9Sstevel@tonic-gate 		/* Inform the Route Control Component of received LLC frame */
15337c478bd9Sstevel@tonic-gate 		gld_rcc_receive(macinfo, pktinfo, rh,
15347c478bd9Sstevel@tonic-gate 		    mp->b_rptr + pktinfo->macLen,
15357c478bd9Sstevel@tonic-gate 		    pktinfo->pktLen - pktinfo->macLen);
15367c478bd9Sstevel@tonic-gate 	}
15377c478bd9Sstevel@tonic-gate out:
15387c478bd9Sstevel@tonic-gate 	if (pmp != NULL)
15397c478bd9Sstevel@tonic-gate 		freemsg(pmp);
15407c478bd9Sstevel@tonic-gate 
15417c478bd9Sstevel@tonic-gate 	return (0);
15427c478bd9Sstevel@tonic-gate }
15437c478bd9Sstevel@tonic-gate 
15447c478bd9Sstevel@tonic-gate mblk_t *
15457c478bd9Sstevel@tonic-gate gld_unitdata_tr(gld_t *gld, mblk_t *mp)
15467c478bd9Sstevel@tonic-gate {
15477c478bd9Sstevel@tonic-gate 	gld_mac_info_t *macinfo = gld->gld_mac_info;
15487c478bd9Sstevel@tonic-gate 	dl_unitdata_req_t *dlp = (dl_unitdata_req_t *)mp->b_rptr;
15497c478bd9Sstevel@tonic-gate 	struct gld_dlsap *gldp = DLSAP(dlp, dlp->dl_dest_addr_offset);
15507c478bd9Sstevel@tonic-gate 	mac_addr_t dhost;
15517c478bd9Sstevel@tonic-gate 	unsigned short type;
15527c478bd9Sstevel@tonic-gate 	mblk_t *nmp, *llcmp, *pmp = NULL;
15537c478bd9Sstevel@tonic-gate 	struct tr_mac_frm_nori *mh;
15547c478bd9Sstevel@tonic-gate 	int hdrlen;
15557c478bd9Sstevel@tonic-gate 	struct gld_ri *rh;
15567c478bd9Sstevel@tonic-gate 
15577c478bd9Sstevel@tonic-gate 	ASSERT(macinfo);
15587c478bd9Sstevel@tonic-gate 
15597c478bd9Sstevel@tonic-gate 	/* extract needed info from the mblk before we maybe reuse it */
15607c478bd9Sstevel@tonic-gate 	mac_copy(gldp->glda_addr, dhost, macinfo->gldm_addrlen);
15617c478bd9Sstevel@tonic-gate 
15627c478bd9Sstevel@tonic-gate 	/* look in the unitdata request for a sap, else use bound one */
15637c478bd9Sstevel@tonic-gate 	if (dlp->dl_dest_addr_length >= DLSAPLENGTH(macinfo) &&
15647c478bd9Sstevel@tonic-gate 	    REF_HOST_USHORT(gldp->glda_sap) != 0)
15657c478bd9Sstevel@tonic-gate 		type = REF_HOST_USHORT(gldp->glda_sap);
15667c478bd9Sstevel@tonic-gate 	else
15677c478bd9Sstevel@tonic-gate 		type = gld->gld_sap;
15687c478bd9Sstevel@tonic-gate 
15697c478bd9Sstevel@tonic-gate 	/* includes maximum possible Routing Information Field (RIF) size */
15707c478bd9Sstevel@tonic-gate 	hdrlen = sizeof (struct tr_mac_frm);
15717c478bd9Sstevel@tonic-gate 
15727c478bd9Sstevel@tonic-gate 	/*
15737c478bd9Sstevel@tonic-gate 	 * Check whether we need to do EtherType encoding or whether the packet
15747c478bd9Sstevel@tonic-gate 	 * is LLC.
15757c478bd9Sstevel@tonic-gate 	 */
15767c478bd9Sstevel@tonic-gate 	if (type > GLD_MAX_802_SAP)
15777c478bd9Sstevel@tonic-gate 		hdrlen += sizeof (struct llc_snap_hdr);
15787c478bd9Sstevel@tonic-gate 
15797c478bd9Sstevel@tonic-gate 	/* need a buffer big enough for the headers */
15807c478bd9Sstevel@tonic-gate 	llcmp = nmp = mp->b_cont; /* where the packet payload M_DATA is */
15817c478bd9Sstevel@tonic-gate 
15827c478bd9Sstevel@tonic-gate 	/*
15837c478bd9Sstevel@tonic-gate 	 * We are going to need to look at the LLC header, so make sure it
15847c478bd9Sstevel@tonic-gate 	 * is contiguously in a single mblk.  If we're the ones who create
15857c478bd9Sstevel@tonic-gate 	 * the LLC header (below, in the case where sap > 0xff) then we don't
15867c478bd9Sstevel@tonic-gate 	 * have to worry about it here.
15877c478bd9Sstevel@tonic-gate 	 */
15887c478bd9Sstevel@tonic-gate 	ASSERT(nmp != NULL);	/* gld_unitdata guarantees msgdsize > 0 */
15897c478bd9Sstevel@tonic-gate 	if (type <= GLD_MAX_802_SAP) {
15907c478bd9Sstevel@tonic-gate 		if (MBLKL(llcmp) < LLC_HDR1_LEN) {
15917c478bd9Sstevel@tonic-gate 			llcmp = pmp = msgpullup(nmp, LLC_HDR1_LEN);
15927c478bd9Sstevel@tonic-gate 			if (pmp == NULL) {
15937c478bd9Sstevel@tonic-gate #ifdef GLD_DEBUG
15947c478bd9Sstevel@tonic-gate 				if (gld_debug & GLDERRS)
15957c478bd9Sstevel@tonic-gate 					cmn_err(CE_WARN,
15967c478bd9Sstevel@tonic-gate 					    "GLD: unitdata_tr "
15977c478bd9Sstevel@tonic-gate 					    "cannot msgpullup");
15987c478bd9Sstevel@tonic-gate #endif
15997c478bd9Sstevel@tonic-gate 				return (NULL);
16007c478bd9Sstevel@tonic-gate 			}
16017c478bd9Sstevel@tonic-gate 		}
16027c478bd9Sstevel@tonic-gate 	}
16037c478bd9Sstevel@tonic-gate 
16047c478bd9Sstevel@tonic-gate 	if (DB_REF(nmp) == 1 && MBLKHEAD(nmp) >= hdrlen) {
16057c478bd9Sstevel@tonic-gate 		/* it fits at the beginning of the first M_DATA block */
16067c478bd9Sstevel@tonic-gate 		freeb(mp);	/* don't need the M_PROTO anymore */
16077c478bd9Sstevel@tonic-gate 	} else if (DB_REF(mp) == 1 && MBLKSIZE(mp) >= hdrlen) {
16087c478bd9Sstevel@tonic-gate 		/* we can reuse the dl_unitdata_req M_PROTO mblk */
16097c478bd9Sstevel@tonic-gate 		nmp = mp;
16107c478bd9Sstevel@tonic-gate 		DB_TYPE(nmp) = M_DATA;
16117c478bd9Sstevel@tonic-gate 		nmp->b_rptr = nmp->b_wptr = DB_LIM(nmp);
16127c478bd9Sstevel@tonic-gate 	} else {
16137c478bd9Sstevel@tonic-gate 		/* we need to allocate one */
16147c478bd9Sstevel@tonic-gate 		if ((nmp = allocb(hdrlen, BPRI_MED)) == NULL) {
16157c478bd9Sstevel@tonic-gate 			if (pmp != NULL)
16167c478bd9Sstevel@tonic-gate 				freemsg(pmp);
16177c478bd9Sstevel@tonic-gate 			return (NULL);
16187c478bd9Sstevel@tonic-gate 		}
16197c478bd9Sstevel@tonic-gate 		nmp->b_rptr = nmp->b_wptr = DB_LIM(nmp);
16207c478bd9Sstevel@tonic-gate 		linkb(nmp, mp->b_cont);
16217c478bd9Sstevel@tonic-gate 		freeb(mp);
16227c478bd9Sstevel@tonic-gate 	}
16237c478bd9Sstevel@tonic-gate 
16247c478bd9Sstevel@tonic-gate 	/* Got the space, now copy in the header components */
16257c478bd9Sstevel@tonic-gate 	if (type > GLD_MAX_802_SAP) {
16267c478bd9Sstevel@tonic-gate 		/* create the snap header */
16277c478bd9Sstevel@tonic-gate 		struct llc_snap_hdr *snap;
16287c478bd9Sstevel@tonic-gate 		llcmp = nmp;	/* LLC header is going to be in this mblk */
16297c478bd9Sstevel@tonic-gate 		nmp->b_rptr -= sizeof (struct llc_snap_hdr);
16307c478bd9Sstevel@tonic-gate 		snap  = (struct llc_snap_hdr *)(nmp->b_rptr);
16317c478bd9Sstevel@tonic-gate 		*snap = llc_snap_def;
16327c478bd9Sstevel@tonic-gate 		SET_NET_USHORT(snap->type, type);
16337c478bd9Sstevel@tonic-gate 	}
16347c478bd9Sstevel@tonic-gate 
16357c478bd9Sstevel@tonic-gate 	/* Hold SR tables still while we maybe point at an entry */
16367c478bd9Sstevel@tonic-gate 	mutex_enter(GLD_SR_MUTEX(macinfo));
16377c478bd9Sstevel@tonic-gate 
16387c478bd9Sstevel@tonic-gate 	gld_rcc_send(macinfo, WR(gld->gld_qptr), dhost, &rh, llcmp->b_rptr);
16397c478bd9Sstevel@tonic-gate 
16407c478bd9Sstevel@tonic-gate 	if (rh != NULL) {
16417c478bd9Sstevel@tonic-gate 		/* copy in the RIF */
16427c478bd9Sstevel@tonic-gate 		ASSERT(rh->len <= sizeof (struct gld_ri));
16437c478bd9Sstevel@tonic-gate 		nmp->b_rptr -= rh->len;
16447c478bd9Sstevel@tonic-gate 		bcopy((caddr_t)rh, (caddr_t)nmp->b_rptr, rh->len);
16457c478bd9Sstevel@tonic-gate 	}
16467c478bd9Sstevel@tonic-gate 
16477c478bd9Sstevel@tonic-gate 	mutex_exit(GLD_SR_MUTEX(macinfo));
16487c478bd9Sstevel@tonic-gate 
16497c478bd9Sstevel@tonic-gate 	/* no longer need the pulled-up mblk */
16507c478bd9Sstevel@tonic-gate 	if (pmp != NULL)
16517c478bd9Sstevel@tonic-gate 		freemsg(pmp);
16527c478bd9Sstevel@tonic-gate 
16537c478bd9Sstevel@tonic-gate 	/*
16547c478bd9Sstevel@tonic-gate 	 * fill in token ring header
16557c478bd9Sstevel@tonic-gate 	 */
16567c478bd9Sstevel@tonic-gate 	nmp->b_rptr -= sizeof (struct tr_mac_frm_nori);
16577c478bd9Sstevel@tonic-gate 	mh = (struct tr_mac_frm_nori *)nmp->b_rptr;
16587c478bd9Sstevel@tonic-gate 	mh->tr_ac = 0x10;
16597c478bd9Sstevel@tonic-gate 	mh->tr_fc = 0x40;
16607c478bd9Sstevel@tonic-gate 	mac_copy(dhost, mh->tr_dhost, macinfo->gldm_addrlen);
16617c478bd9Sstevel@tonic-gate 
16627c478bd9Sstevel@tonic-gate 	/*
16637c478bd9Sstevel@tonic-gate 	 * We access the mac address without the mutex to prevent
16647c478bd9Sstevel@tonic-gate 	 * mutex contention (BUG 4211361)
16657c478bd9Sstevel@tonic-gate 	 */
16667c478bd9Sstevel@tonic-gate 	mac_copy(((gld_mac_pvt_t *)macinfo->gldm_mac_pvt)->curr_macaddr,
16677c478bd9Sstevel@tonic-gate 	    mh->tr_shost, macinfo->gldm_addrlen);
16687c478bd9Sstevel@tonic-gate 
16697c478bd9Sstevel@tonic-gate 	if (rh != NULL)
16707c478bd9Sstevel@tonic-gate 		mh->tr_shost[0] |= 0x80;
16717c478bd9Sstevel@tonic-gate 	else
16727c478bd9Sstevel@tonic-gate 		mh->tr_shost[0] &= ~0x80;
16737c478bd9Sstevel@tonic-gate 
16747c478bd9Sstevel@tonic-gate 	return (nmp);
16757c478bd9Sstevel@tonic-gate }
16767c478bd9Sstevel@tonic-gate 
16777c478bd9Sstevel@tonic-gate /*
16787c478bd9Sstevel@tonic-gate  * We cannot have our client sending us "fastpath" M_DATA messages,
16797c478bd9Sstevel@tonic-gate  * because to do that we must provide to him a fixed MAC header to
16807c478bd9Sstevel@tonic-gate  * be prepended to each outgoing packet.  But with Source Routing
16817c478bd9Sstevel@tonic-gate  * media, the length and content of the MAC header changes as the
16827c478bd9Sstevel@tonic-gate  * routes change, so there is no fixed header we can provide.  So
16837c478bd9Sstevel@tonic-gate  * we decline to accept M_DATA messages if Source Routing is enabled.
16847c478bd9Sstevel@tonic-gate  */
16857c478bd9Sstevel@tonic-gate mblk_t *
16867c478bd9Sstevel@tonic-gate gld_fastpath_tr(gld_t *gld, mblk_t *mp)
16877c478bd9Sstevel@tonic-gate {
16887c478bd9Sstevel@tonic-gate 	gld_mac_info_t *macinfo = gld->gld_mac_info;
16897c478bd9Sstevel@tonic-gate 	dl_unitdata_req_t *dlp = (dl_unitdata_req_t *)mp->b_cont->b_rptr;
16907c478bd9Sstevel@tonic-gate 	struct gld_dlsap *gldp = DLSAP(dlp, dlp->dl_dest_addr_offset);
16917c478bd9Sstevel@tonic-gate 	unsigned short type;
16927c478bd9Sstevel@tonic-gate 	mblk_t *nmp;
16937c478bd9Sstevel@tonic-gate 	struct tr_mac_frm_nori *mh;
16947c478bd9Sstevel@tonic-gate 	int hdrlen;
16957c478bd9Sstevel@tonic-gate 
16967c478bd9Sstevel@tonic-gate 	ASSERT(macinfo);
16977c478bd9Sstevel@tonic-gate 
16987c478bd9Sstevel@tonic-gate 	/*
16997c478bd9Sstevel@tonic-gate 	 * If we are doing Source Routing, then we cannot provide a fixed
17007c478bd9Sstevel@tonic-gate 	 * MAC header, so fail.
17017c478bd9Sstevel@tonic-gate 	 */
17027c478bd9Sstevel@tonic-gate 	if (((gld_mac_pvt_t *)macinfo->gldm_mac_pvt)->rde_enabled)
17037c478bd9Sstevel@tonic-gate 		return (NULL);
17047c478bd9Sstevel@tonic-gate 
17057c478bd9Sstevel@tonic-gate 	/* look in the unitdata request for a sap, else use bound one */
17067c478bd9Sstevel@tonic-gate 	if (dlp->dl_dest_addr_length >= DLSAPLENGTH(macinfo) &&
17077c478bd9Sstevel@tonic-gate 	    REF_HOST_USHORT(gldp->glda_sap) != 0)
17087c478bd9Sstevel@tonic-gate 		type = REF_HOST_USHORT(gldp->glda_sap);
17097c478bd9Sstevel@tonic-gate 	else
17107c478bd9Sstevel@tonic-gate 		type = gld->gld_sap;
17117c478bd9Sstevel@tonic-gate 
17127c478bd9Sstevel@tonic-gate 	hdrlen = sizeof (struct tr_mac_frm_nori);
17137c478bd9Sstevel@tonic-gate 
17147c478bd9Sstevel@tonic-gate 	if (((gld_mac_pvt_t *)macinfo->gldm_mac_pvt)->rde_str_indicator_ste)
17157c478bd9Sstevel@tonic-gate 		hdrlen += ri_ste_def.len;
17167c478bd9Sstevel@tonic-gate 
17177c478bd9Sstevel@tonic-gate 	/*
17187c478bd9Sstevel@tonic-gate 	 * Check whether we need to do EtherType encoding or whether the packet
17197c478bd9Sstevel@tonic-gate 	 * will be LLC.
17207c478bd9Sstevel@tonic-gate 	 */
17217c478bd9Sstevel@tonic-gate 	if (type > GLD_MAX_802_SAP)
17227c478bd9Sstevel@tonic-gate 		hdrlen += sizeof (struct llc_snap_hdr);
17237c478bd9Sstevel@tonic-gate 
17247c478bd9Sstevel@tonic-gate 	if ((nmp = allocb(hdrlen, BPRI_MED)) == NULL)
17257c478bd9Sstevel@tonic-gate 		return (NULL);
17267c478bd9Sstevel@tonic-gate 
17277c478bd9Sstevel@tonic-gate 	nmp->b_rptr = nmp->b_wptr = DB_LIM(nmp);
17287c478bd9Sstevel@tonic-gate 
17297c478bd9Sstevel@tonic-gate 	/* Got the space, now copy in the header components */
17307c478bd9Sstevel@tonic-gate 
17317c478bd9Sstevel@tonic-gate 	if (type > GLD_MAX_802_SAP) {
17327c478bd9Sstevel@tonic-gate 		/* create the snap header */
17337c478bd9Sstevel@tonic-gate 		struct llc_snap_hdr *snap;
17347c478bd9Sstevel@tonic-gate 		nmp->b_rptr -= sizeof (struct llc_snap_hdr);
17357c478bd9Sstevel@tonic-gate 		snap  = (struct llc_snap_hdr *)(nmp->b_rptr);
17367c478bd9Sstevel@tonic-gate 		*snap = llc_snap_def;
17377c478bd9Sstevel@tonic-gate 		snap->type = htons(type);	/* we know it's aligned */
17387c478bd9Sstevel@tonic-gate 	}
17397c478bd9Sstevel@tonic-gate 
17407c478bd9Sstevel@tonic-gate 	/* RDE is disabled, use NULL RIF, or STE RIF */
17417c478bd9Sstevel@tonic-gate 	if (((gld_mac_pvt_t *)macinfo->gldm_mac_pvt)->rde_str_indicator_ste) {
17427c478bd9Sstevel@tonic-gate 		nmp->b_rptr -= ri_ste_def.len;
17437c478bd9Sstevel@tonic-gate 		bcopy((caddr_t)&ri_ste_def, (caddr_t)nmp->b_rptr,
17447c478bd9Sstevel@tonic-gate 		    ri_ste_def.len);
17457c478bd9Sstevel@tonic-gate 	}
17467c478bd9Sstevel@tonic-gate 
17477c478bd9Sstevel@tonic-gate 	/*
17487c478bd9Sstevel@tonic-gate 	 * fill in token ring header
17497c478bd9Sstevel@tonic-gate 	 */
17507c478bd9Sstevel@tonic-gate 	nmp->b_rptr -= sizeof (struct tr_mac_frm_nori);
17517c478bd9Sstevel@tonic-gate 	mh = (struct tr_mac_frm_nori *)nmp->b_rptr;
17527c478bd9Sstevel@tonic-gate 	mh->tr_ac = 0x10;
17537c478bd9Sstevel@tonic-gate 	mh->tr_fc = 0x40;
17547c478bd9Sstevel@tonic-gate 	mac_copy(gldp->glda_addr, mh->tr_dhost, macinfo->gldm_addrlen);
17557c478bd9Sstevel@tonic-gate 
17567c478bd9Sstevel@tonic-gate 	GLDM_LOCK(macinfo, RW_WRITER);
17577c478bd9Sstevel@tonic-gate 	mac_copy(((gld_mac_pvt_t *)macinfo->gldm_mac_pvt)->curr_macaddr,
17587c478bd9Sstevel@tonic-gate 	    mh->tr_shost, macinfo->gldm_addrlen);
17597c478bd9Sstevel@tonic-gate 	GLDM_UNLOCK(macinfo);
17607c478bd9Sstevel@tonic-gate 
17617c478bd9Sstevel@tonic-gate 	if (((gld_mac_pvt_t *)macinfo->gldm_mac_pvt)->rde_str_indicator_ste)
17627c478bd9Sstevel@tonic-gate 		mh->tr_shost[0] |= 0x80;
17637c478bd9Sstevel@tonic-gate 	else
17647c478bd9Sstevel@tonic-gate 		mh->tr_shost[0] &= ~0x80;
17657c478bd9Sstevel@tonic-gate 
17667c478bd9Sstevel@tonic-gate 	return (nmp);
17677c478bd9Sstevel@tonic-gate }
17687c478bd9Sstevel@tonic-gate 
17697c478bd9Sstevel@tonic-gate /*
17707c478bd9Sstevel@tonic-gate  * Route Determination Entity (ISO 8802-2 / IEEE 802.2 : 1994, Section 9)
17717c478bd9Sstevel@tonic-gate  *
17727c478bd9Sstevel@tonic-gate  * RDE is an LLC layer entity.  GLD is a MAC layer entity.  The proper
17737c478bd9Sstevel@tonic-gate  * solution to this architectural anomaly is to move RDE support out of GLD
17747c478bd9Sstevel@tonic-gate  * and into LLC where it belongs.  In particular, only LLC has the knowledge
17757c478bd9Sstevel@tonic-gate  * necessary to reply to XID and TEST packets.  If and when it comes time to
17767c478bd9Sstevel@tonic-gate  * move RDE out of GLD to LLC, the LLC-to-GLD interface should be modified
17777c478bd9Sstevel@tonic-gate  * to use MA_UNITDATA structures rather than DL_UNITDATA structures.  Of
17787c478bd9Sstevel@tonic-gate  * course, GLD will still have to continue to also support the DL_ structures
17797c478bd9Sstevel@tonic-gate  * as long as IP is not layered over LLC.  Another, perhaps better, idea
17807c478bd9Sstevel@tonic-gate  * would be to make RDE an autopush module on top of the token ring drivers:
17817c478bd9Sstevel@tonic-gate  * RDE would sit between LLC and GLD.  It would then also sit between IP and
17827c478bd9Sstevel@tonic-gate  * GLD, providing services to all clients of GLD/tokenring.  In that case,
17837c478bd9Sstevel@tonic-gate  * GLD would still have to continue to support the DL_ interface for non-
17847c478bd9Sstevel@tonic-gate  * Token Ring interfaces, using the MA_ interface only for media supporting
17857c478bd9Sstevel@tonic-gate  * Source Routing media.
17867c478bd9Sstevel@tonic-gate  *
17877c478bd9Sstevel@tonic-gate  * At present, Token Ring is the only source routing medium we support.
17887c478bd9Sstevel@tonic-gate  * Since Token Ring is not at this time a strategic network medium for Sun,
17897c478bd9Sstevel@tonic-gate  * rather than devote a large amount of resources to creating a proper
17907c478bd9Sstevel@tonic-gate  * architecture and implementation of RDE, we do the minimum necessary to
17917c478bd9Sstevel@tonic-gate  * get it to work.  The interface between the above token ring code and the
17927c478bd9Sstevel@tonic-gate  * below RDE code is designed to make it relatively easy to change to an
17937c478bd9Sstevel@tonic-gate  * MA_UNITDATA model later should this ever become a priority.
17947c478bd9Sstevel@tonic-gate  */
17957c478bd9Sstevel@tonic-gate 
17967c478bd9Sstevel@tonic-gate static void gld_send_rqr(gld_mac_info_t *, uchar_t *, struct gld_ri *,
17977c478bd9Sstevel@tonic-gate     struct rde_pdu *, int);
17987c478bd9Sstevel@tonic-gate static void gld_rde_pdu_req(gld_mac_info_t *, queue_t *, uchar_t *,
17997c478bd9Sstevel@tonic-gate     struct gld_ri *, uchar_t, uchar_t, uchar_t);
18007c478bd9Sstevel@tonic-gate static void gld_get_route(gld_mac_info_t *, queue_t *, uchar_t *,
18017c478bd9Sstevel@tonic-gate     struct gld_ri **, uchar_t, uchar_t);
18027c478bd9Sstevel@tonic-gate static void gld_reset_route(gld_mac_info_t *, queue_t *,
18037c478bd9Sstevel@tonic-gate     uchar_t *, uchar_t, uchar_t);
18047c478bd9Sstevel@tonic-gate static void gld_rde_pdu_ind(gld_mac_info_t *, struct gld_ri *, struct rde_pdu *,
18057c478bd9Sstevel@tonic-gate     int);
18067c478bd9Sstevel@tonic-gate static void gld_rif_ind(gld_mac_info_t *, struct gld_ri *, uchar_t *,
18077c478bd9Sstevel@tonic-gate     uchar_t, uchar_t);
18087c478bd9Sstevel@tonic-gate static struct srtab **gld_sr_hash(struct srtab **, uchar_t *, int);
18097c478bd9Sstevel@tonic-gate static struct srtab *gld_sr_lookup_entry(gld_mac_info_t *, uchar_t *);
18107c478bd9Sstevel@tonic-gate static struct srtab *gld_sr_create_entry(gld_mac_info_t *, uchar_t *);
18117c478bd9Sstevel@tonic-gate 
18127c478bd9Sstevel@tonic-gate /*
18137c478bd9Sstevel@tonic-gate  * This routine implements a modified subset of the 802.2 RDE RCC receive
18147c478bd9Sstevel@tonic-gate  * actions:
18157c478bd9Sstevel@tonic-gate  *   we implement RCC receive events 3 to 12 (ISO 8802-2:1994 9.6.3.4);
18167c478bd9Sstevel@tonic-gate  *   we omit special handling for the NULL SAP;
18177c478bd9Sstevel@tonic-gate  *   we omit XID/TEST handling;
18187c478bd9Sstevel@tonic-gate  *   we pass all packets (including RDE) upstream to LLC.
18197c478bd9Sstevel@tonic-gate  */
18207c478bd9Sstevel@tonic-gate static void
18217c478bd9Sstevel@tonic-gate gld_rcc_receive(gld_mac_info_t *macinfo, pktinfo_t *pktinfo, struct gld_ri *rh,
18227c478bd9Sstevel@tonic-gate     uchar_t *llcpkt, int llcpktlen)
18237c478bd9Sstevel@tonic-gate {
18247c478bd9Sstevel@tonic-gate 	struct llc_snap_hdr *snaphdr = (struct llc_snap_hdr *)(llcpkt);
18257c478bd9Sstevel@tonic-gate 
18267c478bd9Sstevel@tonic-gate 	if (!((gld_mac_pvt_t *)macinfo->gldm_mac_pvt)->rde_enabled)
18277c478bd9Sstevel@tonic-gate 		return;
18287c478bd9Sstevel@tonic-gate 
18297c478bd9Sstevel@tonic-gate 	/*
18307c478bd9Sstevel@tonic-gate 	 * First, ensure this packet wasn't something we received just
18317c478bd9Sstevel@tonic-gate 	 * because we were in promiscuous mode.  Since none of the below
18327c478bd9Sstevel@tonic-gate 	 * code wants to see group addressed packets anyway, we can do
18337c478bd9Sstevel@tonic-gate 	 * this check up front.  Since we're doing that, we can omit the
18347c478bd9Sstevel@tonic-gate 	 * checks for group addressed packets below.
18357c478bd9Sstevel@tonic-gate 	 */
18367c478bd9Sstevel@tonic-gate 	if (!pktinfo->isForMe)
18377c478bd9Sstevel@tonic-gate 		return;		/* Event 6 */
18387c478bd9Sstevel@tonic-gate 
18397c478bd9Sstevel@tonic-gate 	/* Process a subset of Route Determination Entity (RDE) packets */
18407c478bd9Sstevel@tonic-gate 	if (snaphdr->d_lsap == LSAP_RDE) {
18417c478bd9Sstevel@tonic-gate 		struct rde_pdu *pdu = (struct rde_pdu *)(llcpkt + LLC_HDR1_LEN);
18427c478bd9Sstevel@tonic-gate 		int pdulen = llcpktlen - LLC_HDR1_LEN;
18437c478bd9Sstevel@tonic-gate 
18447c478bd9Sstevel@tonic-gate 		/* sanity check the PDU */
18457c478bd9Sstevel@tonic-gate 		if ((pdulen < sizeof (struct rde_pdu)) ||
18467c478bd9Sstevel@tonic-gate 		    (snaphdr->s_lsap != LSAP_RDE))
18477c478bd9Sstevel@tonic-gate 			return;
18487c478bd9Sstevel@tonic-gate 
18497c478bd9Sstevel@tonic-gate 		/* we only handle route discovery PDUs, not XID/TEST/other */
18507c478bd9Sstevel@tonic-gate 		if (snaphdr->control != CNTL_LLC_UI)
18517c478bd9Sstevel@tonic-gate 			return;
18527c478bd9Sstevel@tonic-gate 
18537c478bd9Sstevel@tonic-gate 		switch (pdu->rde_ptype) {
18547c478bd9Sstevel@tonic-gate 		case RDE_RQC:	/* Route Query Command; Events 8 - 11 */
18557c478bd9Sstevel@tonic-gate 			gld_send_rqr(macinfo, pktinfo->shost, rh, pdu, pdulen);
18567c478bd9Sstevel@tonic-gate 			/* FALLTHROUGH */
18577c478bd9Sstevel@tonic-gate 		case RDE_RQR:	/* Route Query Response; Event 12 */
18587c478bd9Sstevel@tonic-gate 		case RDE_RS:	/* Route Selected; Event 7 */
18597c478bd9Sstevel@tonic-gate 			gld_rde_pdu_ind(macinfo, rh, pdu, pdulen);
18607c478bd9Sstevel@tonic-gate 			break;
18617c478bd9Sstevel@tonic-gate 		default:	/* ignore if unrecognized ptype */
18627c478bd9Sstevel@tonic-gate 			return;
18637c478bd9Sstevel@tonic-gate 		}
18647c478bd9Sstevel@tonic-gate 
18657c478bd9Sstevel@tonic-gate 		return;
18667c478bd9Sstevel@tonic-gate 	}
18677c478bd9Sstevel@tonic-gate 
18687c478bd9Sstevel@tonic-gate 	/* Consider routes seen in other IA SRF packets */
18697c478bd9Sstevel@tonic-gate 
18707c478bd9Sstevel@tonic-gate 	if (rh == NULL)
18717c478bd9Sstevel@tonic-gate 		return;		/* no RIF; Event 3 */
18727c478bd9Sstevel@tonic-gate 
18737c478bd9Sstevel@tonic-gate 	if ((rh->rt & 0x04) != 0)
18747c478bd9Sstevel@tonic-gate 		return;		/* not SRF; Event 5 */
18757c478bd9Sstevel@tonic-gate 
18767c478bd9Sstevel@tonic-gate 	gld_rif_ind(macinfo, rh, pktinfo->shost, snaphdr->s_lsap,
18777c478bd9Sstevel@tonic-gate 	    snaphdr->d_lsap);	/* Event 4 */
18787c478bd9Sstevel@tonic-gate }
18797c478bd9Sstevel@tonic-gate 
18807c478bd9Sstevel@tonic-gate /*
18817c478bd9Sstevel@tonic-gate  * Send RQR: 802.2 9.6.3.4.2(9) RCC Receive Events 8-11
18827c478bd9Sstevel@tonic-gate  *
18837c478bd9Sstevel@tonic-gate  * The routing processing really doesn't belong here; it should be handled in
18847c478bd9Sstevel@tonic-gate  * the LLC layer above.  If that were the case then RDE could just send down
18857c478bd9Sstevel@tonic-gate  * an extra MA_UNITDATA_REQ with the info needed to construct the packet.  But
18867c478bd9Sstevel@tonic-gate  * at the time we get control here, it's not a particularly good time to be
18877c478bd9Sstevel@tonic-gate  * constructing packets and trying to send them.  Specifically, at this layer
18887c478bd9Sstevel@tonic-gate  * we need to construct the full media packet, which means the below routine
18897c478bd9Sstevel@tonic-gate  * knows that it is dealing with Token Ring media.  If this were instead done
18907c478bd9Sstevel@tonic-gate  * via a proper MA_UNITDATA interface, the RDE stuff could all be completely
18917c478bd9Sstevel@tonic-gate  * media independent.  But since TR is the only source routing medium we
18927c478bd9Sstevel@tonic-gate  * support, this works even though it is not clean.
18937c478bd9Sstevel@tonic-gate  *
18947c478bd9Sstevel@tonic-gate  * We "know" that the only time we can get here is from the "interpret"
18957c478bd9Sstevel@tonic-gate  * routine, and only when it was called at receive time.
18967c478bd9Sstevel@tonic-gate  */
18977c478bd9Sstevel@tonic-gate static void
18987c478bd9Sstevel@tonic-gate gld_send_rqr(gld_mac_info_t *macinfo, uchar_t *shost, struct gld_ri *rh,
18997c478bd9Sstevel@tonic-gate     struct rde_pdu *pdu, int pdulen)
19007c478bd9Sstevel@tonic-gate {
19017c478bd9Sstevel@tonic-gate 	mblk_t *nmp;
19027c478bd9Sstevel@tonic-gate 	int nlen;
19037c478bd9Sstevel@tonic-gate 	struct tr_mac_frm_nori *nmh;
19047c478bd9Sstevel@tonic-gate 	struct gld_ri *nrh;
19057c478bd9Sstevel@tonic-gate 	struct llc_snap_hdr *nsnaphdr;
19067c478bd9Sstevel@tonic-gate 	struct rde_pdu *npdu;
19077c478bd9Sstevel@tonic-gate 
19087c478bd9Sstevel@tonic-gate 	/* We know and assume we're on the receive path */
19097c478bd9Sstevel@tonic-gate 	ASSERT(GLDM_LOCK_HELD(macinfo));
19107c478bd9Sstevel@tonic-gate 
19117c478bd9Sstevel@tonic-gate 	if (pdulen < sizeof (struct rde_pdu))
19127c478bd9Sstevel@tonic-gate 		return;		/* Bad incoming PDU */
19137c478bd9Sstevel@tonic-gate 
19147c478bd9Sstevel@tonic-gate 	nlen = sizeof (struct tr_mac_frm) + LLC_HDR1_LEN +
19157c478bd9Sstevel@tonic-gate 	    sizeof (struct rde_pdu);
19167c478bd9Sstevel@tonic-gate 
19177c478bd9Sstevel@tonic-gate 	if ((nmp = allocb(nlen, BPRI_MED)) == NULL)
19187c478bd9Sstevel@tonic-gate 		return;
19197c478bd9Sstevel@tonic-gate 
19207c478bd9Sstevel@tonic-gate 	nmp->b_rptr = nmp->b_wptr = DB_LIM(nmp);
19217c478bd9Sstevel@tonic-gate 
19227c478bd9Sstevel@tonic-gate 	nmp->b_rptr -= sizeof (struct rde_pdu);
19237c478bd9Sstevel@tonic-gate 	npdu = (struct rde_pdu *)(nmp->b_rptr);
19247c478bd9Sstevel@tonic-gate 	*npdu = *pdu;	/* copy orig/target macaddr/saps */
19257c478bd9Sstevel@tonic-gate 	npdu->rde_ver = 1;
19267c478bd9Sstevel@tonic-gate 	npdu->rde_ptype = RDE_RQR;
19277c478bd9Sstevel@tonic-gate 	mac_copy(((gld_mac_pvt_t *)macinfo->gldm_mac_pvt)->curr_macaddr,
19287c478bd9Sstevel@tonic-gate 	    npdu->rde_target_mac, macinfo->gldm_addrlen);
19297c478bd9Sstevel@tonic-gate 
19307c478bd9Sstevel@tonic-gate 	nmp->b_rptr -= LLC_HDR1_LEN;
19317c478bd9Sstevel@tonic-gate 	nsnaphdr = (struct llc_snap_hdr *)(nmp->b_rptr);
19327c478bd9Sstevel@tonic-gate 	nsnaphdr->s_lsap = nsnaphdr->d_lsap = LSAP_RDE;
19337c478bd9Sstevel@tonic-gate 	nsnaphdr->control = CNTL_LLC_UI;
19347c478bd9Sstevel@tonic-gate 
19357c478bd9Sstevel@tonic-gate 	if (rh == NULL || (rh->rt & 0x06) == 0x06 ||
19367c478bd9Sstevel@tonic-gate 	    rh->len > sizeof (struct gld_ri)) {
19377c478bd9Sstevel@tonic-gate 		/* no RIF (Event 8), or RIF type STE (Event 9): send ARE RQR */
19387c478bd9Sstevel@tonic-gate 		nmp->b_rptr -= 2;
19397c478bd9Sstevel@tonic-gate 		nrh = (struct gld_ri *)(nmp->b_rptr);
19407c478bd9Sstevel@tonic-gate 		nrh->len = 2;
19417c478bd9Sstevel@tonic-gate 		nrh->rt = RT_ARE;
19427c478bd9Sstevel@tonic-gate 		nrh->dir = 0;
19437c478bd9Sstevel@tonic-gate 		nrh->res = 0;
19447c478bd9Sstevel@tonic-gate 		nrh->mtu = RT_MTU_MAX;
19457c478bd9Sstevel@tonic-gate 	} else {
19467c478bd9Sstevel@tonic-gate 		/*
19477c478bd9Sstevel@tonic-gate 		 * RIF must be ARE (Event 10) or SRF (Event 11):
19487c478bd9Sstevel@tonic-gate 		 * send SRF (reverse) RQR
19497c478bd9Sstevel@tonic-gate 		 */
19507c478bd9Sstevel@tonic-gate 		ASSERT(rh->len <= sizeof (struct gld_ri));
19517c478bd9Sstevel@tonic-gate 		nmp->b_rptr -= rh->len;
19527c478bd9Sstevel@tonic-gate 		nrh = (struct gld_ri *)(nmp->b_rptr);
19537c478bd9Sstevel@tonic-gate 		bcopy(rh, nrh, rh->len);	/* copy incoming RIF */
19547c478bd9Sstevel@tonic-gate 		nrh->rt = RT_SRF;		/* make it SRF */
19557c478bd9Sstevel@tonic-gate 		nrh->dir ^= 1;			/* reverse direction */
19567c478bd9Sstevel@tonic-gate 	}
19577c478bd9Sstevel@tonic-gate 
19587c478bd9Sstevel@tonic-gate 	nmp->b_rptr -= sizeof (struct tr_mac_frm_nori);
19597c478bd9Sstevel@tonic-gate 	nmh = (struct tr_mac_frm_nori *)(nmp->b_rptr);
19607c478bd9Sstevel@tonic-gate 	nmh->tr_ac = 0x10;
19617c478bd9Sstevel@tonic-gate 	nmh->tr_fc = 0x40;
19627c478bd9Sstevel@tonic-gate 	mac_copy(shost, nmh->tr_dhost, macinfo->gldm_addrlen);
19637c478bd9Sstevel@tonic-gate 	mac_copy(((gld_mac_pvt_t *)macinfo->gldm_mac_pvt)->curr_macaddr,
19647c478bd9Sstevel@tonic-gate 	    nmh->tr_shost, macinfo->gldm_addrlen);
19657c478bd9Sstevel@tonic-gate 	nmh->tr_shost[0] |= 0x80;		/* indicate RIF present */
19667c478bd9Sstevel@tonic-gate 
19677c478bd9Sstevel@tonic-gate 	/*
19687c478bd9Sstevel@tonic-gate 	 * Packet assembled; send it.
19697c478bd9Sstevel@tonic-gate 	 *
19707c478bd9Sstevel@tonic-gate 	 * As noted before, this is not really a good time to be trying to
19717c478bd9Sstevel@tonic-gate 	 * send out packets.  We have no obvious queue to use if the packet
19727c478bd9Sstevel@tonic-gate 	 * can't be sent right away.  We pick one arbitrarily.
19737c478bd9Sstevel@tonic-gate 	 */
19747c478bd9Sstevel@tonic-gate 	{
19757c478bd9Sstevel@tonic-gate 	gld_vlan_t *vlan;
19767c478bd9Sstevel@tonic-gate 	queue_t *q;
19777c478bd9Sstevel@tonic-gate 
19787c478bd9Sstevel@tonic-gate 	if ((vlan = gld_find_vlan(macinfo, VLAN_VID_NONE)) == NULL) {
19797c478bd9Sstevel@tonic-gate 		/* oops, no vlan on the list for this macinfo! */
19807c478bd9Sstevel@tonic-gate 		/* this should not happen */
19817c478bd9Sstevel@tonic-gate 		freeb(nmp);
19827c478bd9Sstevel@tonic-gate 		return;
19837c478bd9Sstevel@tonic-gate 	}
19847c478bd9Sstevel@tonic-gate 	q = vlan->gldv_str_next->gld_qptr;
19857c478bd9Sstevel@tonic-gate 
19867c478bd9Sstevel@tonic-gate 	/*
19877c478bd9Sstevel@tonic-gate 	 * Queue the packet and let gld_wsrv
19887c478bd9Sstevel@tonic-gate 	 * handle it, thus preventing a panic
19897c478bd9Sstevel@tonic-gate 	 * caused by v2 TR in promiscuous mode
19907c478bd9Sstevel@tonic-gate 	 * where it attempts to get the mutex
19917c478bd9Sstevel@tonic-gate 	 * in this thread while already holding
19927c478bd9Sstevel@tonic-gate 	 * it.
19937c478bd9Sstevel@tonic-gate 	 */
19947c478bd9Sstevel@tonic-gate 	(void) putbq(WR(q), nmp);
19957c478bd9Sstevel@tonic-gate 	qenable(WR(q));
19967c478bd9Sstevel@tonic-gate 	}
19977c478bd9Sstevel@tonic-gate }
19987c478bd9Sstevel@tonic-gate 
19997c478bd9Sstevel@tonic-gate /*
20007c478bd9Sstevel@tonic-gate  * This routine implements a modified subset of the 802.2 RDE RCC send actions:
20017c478bd9Sstevel@tonic-gate  *   we implement RCC send events 5 to 10 (ISO 8802-2:1994 9.6.3.5);
20027c478bd9Sstevel@tonic-gate  *   we omit special handling for the NULL SAP;
20037c478bd9Sstevel@tonic-gate  *   events 11 to 12 are handled by gld_rde_pdu_req below;
20047c478bd9Sstevel@tonic-gate  *   we require an immediate response to our GET_ROUTE_REQUEST.
20057c478bd9Sstevel@tonic-gate  */
20067c478bd9Sstevel@tonic-gate static void
20077c478bd9Sstevel@tonic-gate gld_rcc_send(gld_mac_info_t *macinfo, queue_t *q, uchar_t *dhost,
20087c478bd9Sstevel@tonic-gate     struct gld_ri **rhp, uchar_t *llcpkt)
20097c478bd9Sstevel@tonic-gate {
20107c478bd9Sstevel@tonic-gate 	struct llc_snap_hdr *snaphdr = (struct llc_snap_hdr *)(llcpkt);
20117c478bd9Sstevel@tonic-gate 
20127c478bd9Sstevel@tonic-gate 	/*
20137c478bd9Sstevel@tonic-gate 	 * Our caller has to take the mutex because: to avoid an extra bcopy
20147c478bd9Sstevel@tonic-gate 	 * of the RIF on every transmit, we pass back a pointer to our sr
20157c478bd9Sstevel@tonic-gate 	 * table entry via rhp.  He has to keep the mutex until he has a
20167c478bd9Sstevel@tonic-gate 	 * chance to copy the RIF out into the outgoing packet, so that we
20177c478bd9Sstevel@tonic-gate 	 * don't modify the entry while he's trying to copy it.  This is a
20187c478bd9Sstevel@tonic-gate 	 * little ugly, but saves the extra bcopy.
20197c478bd9Sstevel@tonic-gate 	 */
20207c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(GLD_SR_MUTEX(macinfo)));
20217c478bd9Sstevel@tonic-gate 
20227c478bd9Sstevel@tonic-gate 	*rhp = (struct gld_ri *)NULL;	/* start off clean (no RIF) */
20237c478bd9Sstevel@tonic-gate 
20247c478bd9Sstevel@tonic-gate 	if (!((gld_mac_pvt_t *)macinfo->gldm_mac_pvt)->rde_enabled) {
20257c478bd9Sstevel@tonic-gate 		/* RDE is disabled -- use NULL or STE always */
20267c478bd9Sstevel@tonic-gate 		if (((gld_mac_pvt_t *)macinfo->gldm_mac_pvt)->
20277c478bd9Sstevel@tonic-gate 		    rde_str_indicator_ste)
20287c478bd9Sstevel@tonic-gate 			*rhp = &ri_ste_def;	/* STE option */
20297c478bd9Sstevel@tonic-gate 		return;
20307c478bd9Sstevel@tonic-gate 	}
20317c478bd9Sstevel@tonic-gate 
20327c478bd9Sstevel@tonic-gate 	if (!(dhost[0] & 0x80)) {
20337c478bd9Sstevel@tonic-gate 		/* individual address; Events 7 - 10 */
20347c478bd9Sstevel@tonic-gate 		if ((snaphdr->control & 0xef) == 0xe3) {
20357c478bd9Sstevel@tonic-gate 			/* TEST command, reset the route */
20367c478bd9Sstevel@tonic-gate 			gld_reset_route(macinfo, q,
20377c478bd9Sstevel@tonic-gate 			    dhost, snaphdr->d_lsap, snaphdr->s_lsap);
20387c478bd9Sstevel@tonic-gate 		}
20397c478bd9Sstevel@tonic-gate 		gld_get_route(macinfo, q,
20407c478bd9Sstevel@tonic-gate 		    dhost, rhp, snaphdr->d_lsap, snaphdr->s_lsap);
20417c478bd9Sstevel@tonic-gate 	}
20427c478bd9Sstevel@tonic-gate 
20437c478bd9Sstevel@tonic-gate 	if (*rhp == NULL) {
20447c478bd9Sstevel@tonic-gate 		/*
20457c478bd9Sstevel@tonic-gate 		 * group address (Events 5 - 6),
20467c478bd9Sstevel@tonic-gate 		 * or no route available (Events 8 - 9):
20477c478bd9Sstevel@tonic-gate 		 * Need to send NSR or STE, as configured.
20487c478bd9Sstevel@tonic-gate 		 */
20497c478bd9Sstevel@tonic-gate 		if (((gld_mac_pvt_t *)macinfo->gldm_mac_pvt)->
20507c478bd9Sstevel@tonic-gate 		    rde_str_indicator_ste)
20517c478bd9Sstevel@tonic-gate 			*rhp = &ri_ste_def;	/* STE option */
20527c478bd9Sstevel@tonic-gate 	}
20537c478bd9Sstevel@tonic-gate }
20547c478bd9Sstevel@tonic-gate 
20557c478bd9Sstevel@tonic-gate /*
20567c478bd9Sstevel@tonic-gate  * RCC send events 11 - 12
20577c478bd9Sstevel@tonic-gate  *
20587c478bd9Sstevel@tonic-gate  * At present we only handle the RQC ptype.
20597c478bd9Sstevel@tonic-gate  *
20607c478bd9Sstevel@tonic-gate  * We "know" that the only time we can get here is from the "unitdata"
20617c478bd9Sstevel@tonic-gate  * routine, called at wsrv time.
20627c478bd9Sstevel@tonic-gate  *
20637c478bd9Sstevel@tonic-gate  * If we ever implement the RS ptype (Event 13), this may no longer be true!
20647c478bd9Sstevel@tonic-gate  */
20657c478bd9Sstevel@tonic-gate static void
20667c478bd9Sstevel@tonic-gate gld_rde_pdu_req(gld_mac_info_t *macinfo, queue_t *q, uchar_t *dhost,
20677c478bd9Sstevel@tonic-gate     struct gld_ri *rh, uchar_t dsap, uchar_t ssap, uchar_t ptype)
20687c478bd9Sstevel@tonic-gate {
20697c478bd9Sstevel@tonic-gate 	mblk_t *nmp;
20707c478bd9Sstevel@tonic-gate 	int nlen;
20717c478bd9Sstevel@tonic-gate 	struct tr_mac_frm_nori *nmh;
20727c478bd9Sstevel@tonic-gate 	struct gld_ri *nrh;
20737c478bd9Sstevel@tonic-gate 	struct llc_snap_hdr *nsnaphdr;
20747c478bd9Sstevel@tonic-gate 	struct rde_pdu *npdu;
20757c478bd9Sstevel@tonic-gate 	int srpresent = 0;
20767c478bd9Sstevel@tonic-gate 
20777c478bd9Sstevel@tonic-gate 	/* if you change this to process other types, review all code below */
20787c478bd9Sstevel@tonic-gate 	ASSERT(ptype == RDE_RQC);
20797c478bd9Sstevel@tonic-gate 	ASSERT(rh == NULL);	/* RQC never uses SRF */
20807c478bd9Sstevel@tonic-gate 
20817c478bd9Sstevel@tonic-gate 	nlen = sizeof (struct tr_mac_frm) + LLC_HDR1_LEN +
20827c478bd9Sstevel@tonic-gate 	    sizeof (struct rde_pdu);
20837c478bd9Sstevel@tonic-gate 
20847c478bd9Sstevel@tonic-gate 	if ((nmp = allocb(nlen, BPRI_MED)) == NULL)
20857c478bd9Sstevel@tonic-gate 		return;
20867c478bd9Sstevel@tonic-gate 
20877c478bd9Sstevel@tonic-gate 	nmp->b_rptr = nmp->b_wptr = DB_LIM(nmp);
20887c478bd9Sstevel@tonic-gate 
20897c478bd9Sstevel@tonic-gate 	nmp->b_rptr -= sizeof (struct rde_pdu);
20907c478bd9Sstevel@tonic-gate 	npdu = (struct rde_pdu *)(nmp->b_rptr);
20917c478bd9Sstevel@tonic-gate 	npdu->rde_ver = 1;
20927c478bd9Sstevel@tonic-gate 	npdu->rde_ptype = ptype;
20937c478bd9Sstevel@tonic-gate 	mac_copy(dhost, &npdu->rde_target_mac, 6);
20947c478bd9Sstevel@tonic-gate 
20957c478bd9Sstevel@tonic-gate 	/*
20967c478bd9Sstevel@tonic-gate 	 * access the mac address without a mutex - take a risk -
20977c478bd9Sstevel@tonic-gate 	 * to prevent mutex contention (BUG 4211361)
20987c478bd9Sstevel@tonic-gate 	 */
20997c478bd9Sstevel@tonic-gate 	mac_copy(((gld_mac_pvt_t *)macinfo->gldm_mac_pvt)->curr_macaddr,
21007c478bd9Sstevel@tonic-gate 	    &npdu->rde_orig_mac, 6);
21017c478bd9Sstevel@tonic-gate 	npdu->rde_target_sap = dsap;
21027c478bd9Sstevel@tonic-gate 	npdu->rde_orig_sap = ssap;
21037c478bd9Sstevel@tonic-gate 
21047c478bd9Sstevel@tonic-gate 	nmp->b_rptr -= LLC_HDR1_LEN;
21057c478bd9Sstevel@tonic-gate 	nsnaphdr = (struct llc_snap_hdr *)(nmp->b_rptr);
21067c478bd9Sstevel@tonic-gate 	nsnaphdr->s_lsap = nsnaphdr->d_lsap = LSAP_RDE;
21077c478bd9Sstevel@tonic-gate 	nsnaphdr->control = CNTL_LLC_UI;
21087c478bd9Sstevel@tonic-gate 
21097c478bd9Sstevel@tonic-gate #if 0	/* we don't need this for now */
21107c478bd9Sstevel@tonic-gate 	if (rh != NULL) {
21117c478bd9Sstevel@tonic-gate 		/* send an SRF frame with specified RIF */
21127c478bd9Sstevel@tonic-gate 		ASSERT(rh->len <= sizeof (struct gld_ri));
21137c478bd9Sstevel@tonic-gate 		nmp->b_rptr -= rh->len;
21147c478bd9Sstevel@tonic-gate 		nrh = (struct gld_ri *)(nmp->b_rptr);
21157c478bd9Sstevel@tonic-gate 		bcopy(rh, nrh, rh->len);
21167c478bd9Sstevel@tonic-gate 		ASSERT(nrh->rt == RT_SRF);
21177c478bd9Sstevel@tonic-gate 		srpresent = 1;
21187c478bd9Sstevel@tonic-gate 	} else
21197c478bd9Sstevel@tonic-gate #endif
21207c478bd9Sstevel@tonic-gate 
21217c478bd9Sstevel@tonic-gate 	/* Need to send NSR or STE, as configured.  */
21227c478bd9Sstevel@tonic-gate 	if (((gld_mac_pvt_t *)macinfo->gldm_mac_pvt)->rde_str_indicator_ste) {
21237c478bd9Sstevel@tonic-gate 		/* send an STE frame */
21247c478bd9Sstevel@tonic-gate 		nmp->b_rptr -= 2;
21257c478bd9Sstevel@tonic-gate 		nrh = (struct gld_ri *)(nmp->b_rptr);
21267c478bd9Sstevel@tonic-gate 		nrh->len = 2;
21277c478bd9Sstevel@tonic-gate 		nrh->rt = RT_STE;
21287c478bd9Sstevel@tonic-gate 		nrh->dir = 0;
21297c478bd9Sstevel@tonic-gate 		nrh->res = 0;
21307c478bd9Sstevel@tonic-gate 		nrh->mtu = RT_MTU_MAX;
21317c478bd9Sstevel@tonic-gate 		srpresent = 1;
21327c478bd9Sstevel@tonic-gate 	} /* else send an NSR frame */
21337c478bd9Sstevel@tonic-gate 
21347c478bd9Sstevel@tonic-gate 	nmp->b_rptr -= sizeof (struct tr_mac_frm_nori);
21357c478bd9Sstevel@tonic-gate 	nmh = (struct tr_mac_frm_nori *)(nmp->b_rptr);
21367c478bd9Sstevel@tonic-gate 	nmh->tr_ac = 0x10;
21377c478bd9Sstevel@tonic-gate 	nmh->tr_fc = 0x40;
21387c478bd9Sstevel@tonic-gate 	mac_copy(dhost, nmh->tr_dhost, macinfo->gldm_addrlen);
21397c478bd9Sstevel@tonic-gate 	/*
21407c478bd9Sstevel@tonic-gate 	 * access the mac address without a mutex - take a risk -
21417c478bd9Sstevel@tonic-gate 	 * to prevent mutex contention  - BUG 4211361
21427c478bd9Sstevel@tonic-gate 	 */
21437c478bd9Sstevel@tonic-gate 	mac_copy(((gld_mac_pvt_t *)macinfo->gldm_mac_pvt)->curr_macaddr,
21447c478bd9Sstevel@tonic-gate 	    nmh->tr_shost, macinfo->gldm_addrlen);
21457c478bd9Sstevel@tonic-gate 
21467c478bd9Sstevel@tonic-gate 	if (srpresent)
21477c478bd9Sstevel@tonic-gate 		nmh->tr_shost[0] |= 0x80;
21487c478bd9Sstevel@tonic-gate 	else
21497c478bd9Sstevel@tonic-gate 		nmh->tr_shost[0] &= ~0x80;
21507c478bd9Sstevel@tonic-gate 
21517c478bd9Sstevel@tonic-gate 	/*
21527c478bd9Sstevel@tonic-gate 	 * Packet assembled; send it.
21537c478bd9Sstevel@tonic-gate 	 *
21547c478bd9Sstevel@tonic-gate 	 * Since we own the SR_MUTEX, we don't want to take the maclock
21557c478bd9Sstevel@tonic-gate 	 * mutex (since they are acquired in the opposite order on the
21567c478bd9Sstevel@tonic-gate 	 * receive path, so deadlock could occur).  We could rearrange
21577c478bd9Sstevel@tonic-gate 	 * the code in gld_get_route() and drop the SR_MUTEX around the
21587c478bd9Sstevel@tonic-gate 	 * call to gld_rde_pdu_req(), but that's kind of ugly.  Rather,
21597c478bd9Sstevel@tonic-gate 	 * we just refrain from calling gld_start() from here, and
21607c478bd9Sstevel@tonic-gate 	 * instead just queue the packet for wsrv to send next.  Besides,
21617c478bd9Sstevel@tonic-gate 	 * it's more important to get the packet we're working on out
21627c478bd9Sstevel@tonic-gate 	 * quickly than this RQC.
21637c478bd9Sstevel@tonic-gate 	 */
21647c478bd9Sstevel@tonic-gate 	(void) putbq(WR(q), nmp);
21657c478bd9Sstevel@tonic-gate 	qenable(WR(q));
21667c478bd9Sstevel@tonic-gate }
21677c478bd9Sstevel@tonic-gate 
21687c478bd9Sstevel@tonic-gate /*
21697c478bd9Sstevel@tonic-gate  * Route Determination Component (RDC)
21707c478bd9Sstevel@tonic-gate  *
21717c478bd9Sstevel@tonic-gate  * We do not implement separate routes for each SAP, as specified by
21727c478bd9Sstevel@tonic-gate  * ISO 8802-2; instead we implement only one route per remote mac address.
21737c478bd9Sstevel@tonic-gate  */
21747c478bd9Sstevel@tonic-gate static void
21757c478bd9Sstevel@tonic-gate gld_get_route(gld_mac_info_t *macinfo, queue_t *q, uchar_t *dhost,
21767c478bd9Sstevel@tonic-gate     struct gld_ri **rhp, uchar_t dsap, uchar_t ssap)
21777c478bd9Sstevel@tonic-gate {
21787c478bd9Sstevel@tonic-gate 	struct srtab *sr;
21797c478bd9Sstevel@tonic-gate 	clock_t t = ddi_get_lbolt();
21807c478bd9Sstevel@tonic-gate 
21817c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(GLD_SR_MUTEX(macinfo)));
21827c478bd9Sstevel@tonic-gate 
21837c478bd9Sstevel@tonic-gate 	sr = gld_sr_lookup_entry(macinfo, dhost);
21847c478bd9Sstevel@tonic-gate 
21857c478bd9Sstevel@tonic-gate 	if (sr == NULL) {
21867c478bd9Sstevel@tonic-gate 		/*
21877c478bd9Sstevel@tonic-gate 		 * we have no entry -- never heard of this address:
21887c478bd9Sstevel@tonic-gate 		 * create an empty entry and initiate RQC
21897c478bd9Sstevel@tonic-gate 		 */
21907c478bd9Sstevel@tonic-gate 		sr = gld_sr_create_entry(macinfo, dhost);
21917c478bd9Sstevel@tonic-gate 		gld_rde_pdu_req(macinfo, q, dhost, (struct gld_ri *)NULL,
21927c478bd9Sstevel@tonic-gate 		    dsap, ssap, RDE_RQC);
21937c478bd9Sstevel@tonic-gate 		if (sr)
21947c478bd9Sstevel@tonic-gate 			sr->sr_timer = t;
21957c478bd9Sstevel@tonic-gate 		*rhp = NULL;		/* we have no route yet */
21967c478bd9Sstevel@tonic-gate 		return;
21977c478bd9Sstevel@tonic-gate 	}
21987c478bd9Sstevel@tonic-gate 
21997c478bd9Sstevel@tonic-gate 	/* we have an entry; see if we know a route yet */
22007c478bd9Sstevel@tonic-gate 
22017c478bd9Sstevel@tonic-gate 	if (sr->sr_ri.len == 0) {
22027c478bd9Sstevel@tonic-gate 		/* Have asked RQC, but no reply (yet) */
22037c478bd9Sstevel@tonic-gate 		if (t - sr->sr_timer >
22047c478bd9Sstevel@tonic-gate 		    ((gld_mac_pvt_t *)macinfo->gldm_mac_pvt)->rde_timeout) {
22057c478bd9Sstevel@tonic-gate 			/* RQR overdue, resend RQC */
22067c478bd9Sstevel@tonic-gate 			gld_rde_pdu_req(macinfo, q, dhost,
22077c478bd9Sstevel@tonic-gate 			    (struct gld_ri *)NULL, dsap, ssap, RDE_RQC);
22087c478bd9Sstevel@tonic-gate 			sr->sr_timer = t;
22097c478bd9Sstevel@tonic-gate 		}
22107c478bd9Sstevel@tonic-gate 		*rhp = NULL;		/* we have no route yet */
22117c478bd9Sstevel@tonic-gate 		return;
22127c478bd9Sstevel@tonic-gate 	}
22137c478bd9Sstevel@tonic-gate 
22147c478bd9Sstevel@tonic-gate 	/* we know a route, or it's local */
22157c478bd9Sstevel@tonic-gate 
22167c478bd9Sstevel@tonic-gate 	/* if it might be stale, reset and get a new one */
22177c478bd9Sstevel@tonic-gate 	if (t - sr->sr_timer >
22187c478bd9Sstevel@tonic-gate 	    ((gld_mac_pvt_t *)macinfo->gldm_mac_pvt)->rde_timeout) {
22197c478bd9Sstevel@tonic-gate 		gld_rde_pdu_req(macinfo, q, dhost,
22207c478bd9Sstevel@tonic-gate 		    (struct gld_ri *)NULL, dsap, ssap, RDE_RQC);
22217c478bd9Sstevel@tonic-gate 		sr->sr_ri.len = 0;
22227c478bd9Sstevel@tonic-gate 		sr->sr_timer = t;
22237c478bd9Sstevel@tonic-gate 		*rhp = NULL;		/* we have no route */
22247c478bd9Sstevel@tonic-gate 		return;
22257c478bd9Sstevel@tonic-gate 	}
22267c478bd9Sstevel@tonic-gate 
22277c478bd9Sstevel@tonic-gate 	if (sr->sr_ri.len == 2) {
22287c478bd9Sstevel@tonic-gate 		/* the remote site is on our local ring -- no route needed */
22297c478bd9Sstevel@tonic-gate 		*rhp = NULL;
22307c478bd9Sstevel@tonic-gate 		return;
22317c478bd9Sstevel@tonic-gate 	}
22327c478bd9Sstevel@tonic-gate 
22337c478bd9Sstevel@tonic-gate 	*rhp = &sr->sr_ri;	/* we have a route, return it */
22347c478bd9Sstevel@tonic-gate }
22357c478bd9Sstevel@tonic-gate 
22367c478bd9Sstevel@tonic-gate /*
22377c478bd9Sstevel@tonic-gate  * zap the specified entry and reinitiate RQC
22387c478bd9Sstevel@tonic-gate  */
22397c478bd9Sstevel@tonic-gate static void
22407c478bd9Sstevel@tonic-gate gld_reset_route(gld_mac_info_t *macinfo, queue_t *q,
22417c478bd9Sstevel@tonic-gate     uchar_t *dhost, uchar_t dsap, uchar_t ssap)
22427c478bd9Sstevel@tonic-gate {
22437c478bd9Sstevel@tonic-gate 	struct srtab *sr;
22447c478bd9Sstevel@tonic-gate 
22457c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(GLD_SR_MUTEX(macinfo)));
22467c478bd9Sstevel@tonic-gate 
22477c478bd9Sstevel@tonic-gate 	sr = gld_sr_create_entry(macinfo, dhost);
22487c478bd9Sstevel@tonic-gate 	gld_rde_pdu_req(macinfo, q, dhost, (struct gld_ri *)NULL,
22497c478bd9Sstevel@tonic-gate 	    dsap, ssap, RDE_RQC);
22507c478bd9Sstevel@tonic-gate 	if (sr == NULL)
22517c478bd9Sstevel@tonic-gate 		return;
22527c478bd9Sstevel@tonic-gate 
22537c478bd9Sstevel@tonic-gate 	sr->sr_ri.len = 0;
22547c478bd9Sstevel@tonic-gate 	sr->sr_timer = ddi_get_lbolt();
22557c478bd9Sstevel@tonic-gate }
22567c478bd9Sstevel@tonic-gate 
22577c478bd9Sstevel@tonic-gate /*
22587c478bd9Sstevel@tonic-gate  * This routine is called when an RDE PDU is received from our peer.
22597c478bd9Sstevel@tonic-gate  * If it is an RS (Route Selected) PDU, we adopt the specified route.
22607c478bd9Sstevel@tonic-gate  * If it is an RQR (reply to our previous RQC), we evaluate the
22617c478bd9Sstevel@tonic-gate  * specified route in comparison with our current known route, if any,
22627c478bd9Sstevel@tonic-gate  * and we keep the "better" of the two routes.
22637c478bd9Sstevel@tonic-gate  */
22647c478bd9Sstevel@tonic-gate static void
22657c478bd9Sstevel@tonic-gate gld_rde_pdu_ind(gld_mac_info_t *macinfo, struct gld_ri *rh, struct rde_pdu *pdu,
22667c478bd9Sstevel@tonic-gate     int pdulen)
22677c478bd9Sstevel@tonic-gate {
22687c478bd9Sstevel@tonic-gate 	struct srtab *sr;
22697c478bd9Sstevel@tonic-gate 	uchar_t *otherhost;
22707c478bd9Sstevel@tonic-gate 
22717c478bd9Sstevel@tonic-gate 	if (pdulen < sizeof (struct rde_pdu))
22727c478bd9Sstevel@tonic-gate 		return;		/* Bad incoming PDU */
22737c478bd9Sstevel@tonic-gate 
22747c478bd9Sstevel@tonic-gate 	if (pdu->rde_ptype == RDE_RQC)
22757c478bd9Sstevel@tonic-gate 		return;			/* ignore RQC */
22767c478bd9Sstevel@tonic-gate 
22777c478bd9Sstevel@tonic-gate 	if (pdu->rde_ptype != RDE_RQR && pdu->rde_ptype != RDE_RS) {
22787c478bd9Sstevel@tonic-gate #ifdef GLD_DEBUG
22797c478bd9Sstevel@tonic-gate 		if (gld_debug & GLDERRS)
22807c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "gld: bogus RDE ptype 0x%x received",
22817c478bd9Sstevel@tonic-gate 			    pdu->rde_ptype);
22827c478bd9Sstevel@tonic-gate #endif
22837c478bd9Sstevel@tonic-gate 		return;
22847c478bd9Sstevel@tonic-gate 	}
22857c478bd9Sstevel@tonic-gate 
22867c478bd9Sstevel@tonic-gate 	if (rh == NULL) {
22877c478bd9Sstevel@tonic-gate #ifdef GLD_DEBUG
22887c478bd9Sstevel@tonic-gate 		if (gld_debug & GLDERRS)
22897c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN,
22907c478bd9Sstevel@tonic-gate 			    "gld: bogus NULL RIF, ptype 0x%x received",
22917c478bd9Sstevel@tonic-gate 			    pdu->rde_ptype);
22927c478bd9Sstevel@tonic-gate #endif
22937c478bd9Sstevel@tonic-gate 		return;
22947c478bd9Sstevel@tonic-gate 	}
22957c478bd9Sstevel@tonic-gate 
22967c478bd9Sstevel@tonic-gate 	ASSERT(rh->len >= 2);
22977c478bd9Sstevel@tonic-gate 	ASSERT(rh->len <= sizeof (struct gld_ri));
22987c478bd9Sstevel@tonic-gate 	ASSERT((rh->len & 1) == 0);
22997c478bd9Sstevel@tonic-gate 
23007c478bd9Sstevel@tonic-gate 	if (pdu->rde_ptype == RDE_RQR) {
23017c478bd9Sstevel@tonic-gate 		/* A reply to our RQC has his address as target mac */
23027c478bd9Sstevel@tonic-gate 		otherhost = pdu->rde_target_mac;
23037c478bd9Sstevel@tonic-gate 	} else {
23047c478bd9Sstevel@tonic-gate 		ASSERT(pdu->rde_ptype == RDE_RS);
23057c478bd9Sstevel@tonic-gate 		/* An RS has his address as orig mac */
23067c478bd9Sstevel@tonic-gate 		otherhost = pdu->rde_orig_mac;
23077c478bd9Sstevel@tonic-gate 	}
23087c478bd9Sstevel@tonic-gate 
23097c478bd9Sstevel@tonic-gate 	mutex_enter(GLD_SR_MUTEX(macinfo));
23107c478bd9Sstevel@tonic-gate 
23117c478bd9Sstevel@tonic-gate 	if ((sr = gld_sr_create_entry(macinfo, otherhost)) == NULL) {
23127c478bd9Sstevel@tonic-gate 		mutex_exit(GLD_SR_MUTEX(macinfo));
23137c478bd9Sstevel@tonic-gate 		return;		/* oh well, out of memory */
23147c478bd9Sstevel@tonic-gate 	}
23157c478bd9Sstevel@tonic-gate 
23167c478bd9Sstevel@tonic-gate 	if (pdu->rde_ptype == RDE_RQR) {
23177c478bd9Sstevel@tonic-gate 		/* see if new route is better than what we may already have */
23187c478bd9Sstevel@tonic-gate 		if (sr->sr_ri.len != 0 &&
23197c478bd9Sstevel@tonic-gate 		    sr->sr_ri.len <= rh->len) {
23207c478bd9Sstevel@tonic-gate 			mutex_exit(GLD_SR_MUTEX(macinfo));
23217c478bd9Sstevel@tonic-gate 			return;	/* we have one, and new one is no shorter */
23227c478bd9Sstevel@tonic-gate 		}
23237c478bd9Sstevel@tonic-gate 	}
23247c478bd9Sstevel@tonic-gate 
23257c478bd9Sstevel@tonic-gate 	/* adopt the new route */
23267c478bd9Sstevel@tonic-gate 	bcopy((caddr_t)rh, (caddr_t)&sr->sr_ri, rh->len); /* copy incom RIF */
23277c478bd9Sstevel@tonic-gate 	sr->sr_ri.rt = RT_SRF;	/* make it a clean SRF */
23287c478bd9Sstevel@tonic-gate 	sr->sr_ri.dir ^= 1;	/* reverse direction */
23297c478bd9Sstevel@tonic-gate 	sr->sr_timer = ddi_get_lbolt();
23307c478bd9Sstevel@tonic-gate 
23317c478bd9Sstevel@tonic-gate 	mutex_exit(GLD_SR_MUTEX(macinfo));
23327c478bd9Sstevel@tonic-gate }
23337c478bd9Sstevel@tonic-gate 
23347c478bd9Sstevel@tonic-gate /*
23357c478bd9Sstevel@tonic-gate  * This routine is called when a packet with a RIF is received.  Our
23367c478bd9Sstevel@tonic-gate  * policy is to adopt the route.
23377c478bd9Sstevel@tonic-gate  */
23387c478bd9Sstevel@tonic-gate /* ARGSUSED3 */
23397c478bd9Sstevel@tonic-gate static void
23407c478bd9Sstevel@tonic-gate gld_rif_ind(gld_mac_info_t *macinfo, struct gld_ri *rh, uchar_t *shost,
23417c478bd9Sstevel@tonic-gate     uchar_t ssap, uchar_t dsap)
23427c478bd9Sstevel@tonic-gate {
23437c478bd9Sstevel@tonic-gate 	struct srtab *sr;
23447c478bd9Sstevel@tonic-gate 
23457c478bd9Sstevel@tonic-gate 	ASSERT(rh != NULL);		/* ensure RIF */
23467c478bd9Sstevel@tonic-gate 	ASSERT((rh->rt & 0x04) == 0);	/* ensure SRF */
23477c478bd9Sstevel@tonic-gate 	ASSERT(rh->len >= 2);
23487c478bd9Sstevel@tonic-gate 	ASSERT(rh->len <= sizeof (struct gld_ri));
23497c478bd9Sstevel@tonic-gate 	ASSERT((rh->len & 1) == 0);
23507c478bd9Sstevel@tonic-gate 
23517c478bd9Sstevel@tonic-gate 	mutex_enter(GLD_SR_MUTEX(macinfo));
23527c478bd9Sstevel@tonic-gate 
23537c478bd9Sstevel@tonic-gate 	if ((sr = gld_sr_create_entry(macinfo, shost)) == NULL) {
23547c478bd9Sstevel@tonic-gate 		mutex_exit(GLD_SR_MUTEX(macinfo));
23557c478bd9Sstevel@tonic-gate 		return;		/* oh well, out of memory */
23567c478bd9Sstevel@tonic-gate 	}
23577c478bd9Sstevel@tonic-gate 
23587c478bd9Sstevel@tonic-gate 	/* we have an entry; fill it in */
23597c478bd9Sstevel@tonic-gate 	bcopy((caddr_t)rh, (caddr_t)&sr->sr_ri, rh->len); /* copy incom RIF */
23607c478bd9Sstevel@tonic-gate 	sr->sr_ri.rt = RT_SRF;	/* make it a clean SRF */
23617c478bd9Sstevel@tonic-gate 	sr->sr_ri.dir ^= 1;	/* reverse direction */
23627c478bd9Sstevel@tonic-gate 	sr->sr_timer = ddi_get_lbolt();
23637c478bd9Sstevel@tonic-gate 
23647c478bd9Sstevel@tonic-gate 	mutex_exit(GLD_SR_MUTEX(macinfo));
23657c478bd9Sstevel@tonic-gate }
23667c478bd9Sstevel@tonic-gate 
23677c478bd9Sstevel@tonic-gate static struct srtab **
23687c478bd9Sstevel@tonic-gate gld_sr_hash(struct srtab **sr_hash_tbl, uchar_t *addr, int addr_length)
23697c478bd9Sstevel@tonic-gate {
23707c478bd9Sstevel@tonic-gate 	uint_t hashval = 0;
23717c478bd9Sstevel@tonic-gate 
23727c478bd9Sstevel@tonic-gate 	while (--addr_length >= 0)
23737c478bd9Sstevel@tonic-gate 		hashval ^= *addr++;
23747c478bd9Sstevel@tonic-gate 
23757c478bd9Sstevel@tonic-gate 	return (&sr_hash_tbl[hashval % SR_HASH_SIZE]);
23767c478bd9Sstevel@tonic-gate }
23777c478bd9Sstevel@tonic-gate 
23787c478bd9Sstevel@tonic-gate static struct srtab *
23797c478bd9Sstevel@tonic-gate gld_sr_lookup_entry(gld_mac_info_t *macinfo, uchar_t *macaddr)
23807c478bd9Sstevel@tonic-gate {
23817c478bd9Sstevel@tonic-gate 	struct srtab *sr;
23827c478bd9Sstevel@tonic-gate 
23837c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(GLD_SR_MUTEX(macinfo)));
23847c478bd9Sstevel@tonic-gate 
23857c478bd9Sstevel@tonic-gate 	for (sr = *gld_sr_hash(GLD_SR_HASH(macinfo), macaddr,
23867c478bd9Sstevel@tonic-gate 	    macinfo->gldm_addrlen); sr; sr = sr->sr_next)
23877c478bd9Sstevel@tonic-gate 		if (mac_eq(macaddr, sr->sr_mac, macinfo->gldm_addrlen))
23887c478bd9Sstevel@tonic-gate 			return (sr);
23897c478bd9Sstevel@tonic-gate 
23907c478bd9Sstevel@tonic-gate 	return ((struct srtab *)0);
23917c478bd9Sstevel@tonic-gate }
23927c478bd9Sstevel@tonic-gate 
23937c478bd9Sstevel@tonic-gate static struct srtab *
23947c478bd9Sstevel@tonic-gate gld_sr_create_entry(gld_mac_info_t *macinfo, uchar_t *macaddr)
23957c478bd9Sstevel@tonic-gate {
23967c478bd9Sstevel@tonic-gate 	struct srtab *sr;
23977c478bd9Sstevel@tonic-gate 	struct srtab **srp;
23987c478bd9Sstevel@tonic-gate 
23997c478bd9Sstevel@tonic-gate 	ASSERT(!(macaddr[0] & 0x80));	/* no group addresses here */
24007c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(GLD_SR_MUTEX(macinfo)));
24017c478bd9Sstevel@tonic-gate 
24027c478bd9Sstevel@tonic-gate 	srp = gld_sr_hash(GLD_SR_HASH(macinfo), macaddr, macinfo->gldm_addrlen);
24037c478bd9Sstevel@tonic-gate 
24047c478bd9Sstevel@tonic-gate 	for (sr = *srp; sr; sr = sr->sr_next)
24057c478bd9Sstevel@tonic-gate 		if (mac_eq(macaddr, sr->sr_mac, macinfo->gldm_addrlen))
24067c478bd9Sstevel@tonic-gate 			return (sr);
24077c478bd9Sstevel@tonic-gate 
24087c478bd9Sstevel@tonic-gate 	if (!(sr = kmem_zalloc(sizeof (struct srtab), KM_NOSLEEP))) {
24097c478bd9Sstevel@tonic-gate #ifdef GLD_DEBUG
24107c478bd9Sstevel@tonic-gate 		if (gld_debug & GLDERRS)
24117c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN,
24127c478bd9Sstevel@tonic-gate 			    "gld: gld_sr_create_entry kmem_alloc failed");
24137c478bd9Sstevel@tonic-gate #endif
24147c478bd9Sstevel@tonic-gate 		return ((struct srtab *)0);
24157c478bd9Sstevel@tonic-gate 	}
24167c478bd9Sstevel@tonic-gate 
24177c478bd9Sstevel@tonic-gate 	bcopy((caddr_t)macaddr, (caddr_t)sr->sr_mac, macinfo->gldm_addrlen);
24187c478bd9Sstevel@tonic-gate 
24197c478bd9Sstevel@tonic-gate 	sr->sr_next = *srp;
24207c478bd9Sstevel@tonic-gate 	*srp = sr;
24217c478bd9Sstevel@tonic-gate 	return (sr);
24227c478bd9Sstevel@tonic-gate }
24237c478bd9Sstevel@tonic-gate 
24247c478bd9Sstevel@tonic-gate static void
24257c478bd9Sstevel@tonic-gate gld_sr_clear(gld_mac_info_t *macinfo)
24267c478bd9Sstevel@tonic-gate {
24277c478bd9Sstevel@tonic-gate 	int i;
24287c478bd9Sstevel@tonic-gate 	struct srtab **sr_hash_tbl = GLD_SR_HASH(macinfo);
24297c478bd9Sstevel@tonic-gate 	struct srtab **srp, *sr;
24307c478bd9Sstevel@tonic-gate 
24317c478bd9Sstevel@tonic-gate 	/*
24327c478bd9Sstevel@tonic-gate 	 * Walk through the table, deleting all entries.
24337c478bd9Sstevel@tonic-gate 	 *
24347c478bd9Sstevel@tonic-gate 	 * Only called from uninit, so don't need the mutex.
24357c478bd9Sstevel@tonic-gate 	 */
24367c478bd9Sstevel@tonic-gate 	for (i = 0; i < SR_HASH_SIZE; i++) {
24377c478bd9Sstevel@tonic-gate 		for (srp = &sr_hash_tbl[i]; (sr = *srp) != NULL; ) {
24387c478bd9Sstevel@tonic-gate 			*srp = sr->sr_next;
24397c478bd9Sstevel@tonic-gate 			kmem_free((char *)sr, sizeof (struct srtab));
24407c478bd9Sstevel@tonic-gate 		}
24417c478bd9Sstevel@tonic-gate 	}
24427c478bd9Sstevel@tonic-gate }
24437c478bd9Sstevel@tonic-gate 
24447c478bd9Sstevel@tonic-gate #ifdef	DEBUG
24457c478bd9Sstevel@tonic-gate void
24467c478bd9Sstevel@tonic-gate gld_sr_dump(gld_mac_info_t *macinfo)
24477c478bd9Sstevel@tonic-gate {
24487c478bd9Sstevel@tonic-gate 	int i, j;
24497c478bd9Sstevel@tonic-gate 	struct srtab **sr_hash_tbl;
24507c478bd9Sstevel@tonic-gate 	struct srtab *sr;
24517c478bd9Sstevel@tonic-gate 
24527c478bd9Sstevel@tonic-gate 	sr_hash_tbl = GLD_SR_HASH(macinfo);
24537c478bd9Sstevel@tonic-gate 	if (sr_hash_tbl == NULL)
24547c478bd9Sstevel@tonic-gate 		return;
24557c478bd9Sstevel@tonic-gate 
24567c478bd9Sstevel@tonic-gate 	mutex_enter(GLD_SR_MUTEX(macinfo));
24577c478bd9Sstevel@tonic-gate 
24587c478bd9Sstevel@tonic-gate 	/*
24597c478bd9Sstevel@tonic-gate 	 * Walk through the table, printing all entries
24607c478bd9Sstevel@tonic-gate 	 */
24617c478bd9Sstevel@tonic-gate 	cmn_err(CE_NOTE, "GLD Source Routing Table (0x%p):", (void *)macinfo);
24627c478bd9Sstevel@tonic-gate 	cmn_err(CE_CONT, "Addr len,rt,dir,mtu,res rng,brg0 rng,brg1...\n");
24637c478bd9Sstevel@tonic-gate 	for (i = 0; i < SR_HASH_SIZE; i++) {
24647c478bd9Sstevel@tonic-gate 		for (sr = sr_hash_tbl[i]; sr; sr = sr->sr_next) {
24657c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT,
24667c478bd9Sstevel@tonic-gate 			    "%x:%x:%x:%x:%x:%x %d,%x,%x,%x,%x ",
24677c478bd9Sstevel@tonic-gate 			    sr->sr_mac[0], sr->sr_mac[1], sr->sr_mac[2],
24687c478bd9Sstevel@tonic-gate 			    sr->sr_mac[3], sr->sr_mac[4], sr->sr_mac[5],
24697c478bd9Sstevel@tonic-gate 			    sr->sr_ri.len, sr->sr_ri.rt, sr->sr_ri.dir,
24707c478bd9Sstevel@tonic-gate 			    sr->sr_ri.mtu, sr->sr_ri.res);
24717c478bd9Sstevel@tonic-gate 			if (sr->sr_ri.len)
24727c478bd9Sstevel@tonic-gate 				for (j = 0; j < (sr->sr_ri.len - 2) / 2; j++)
24737c478bd9Sstevel@tonic-gate 					cmn_err(CE_CONT, "%x ",
24747c478bd9Sstevel@tonic-gate 					    REF_NET_USHORT(*(unsigned short *)
24757c478bd9Sstevel@tonic-gate 					    &sr->sr_ri.rd[j]));
24767c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT, "\n");
24777c478bd9Sstevel@tonic-gate 		}
24787c478bd9Sstevel@tonic-gate 	}
24797c478bd9Sstevel@tonic-gate 
24807c478bd9Sstevel@tonic-gate 	mutex_exit(GLD_SR_MUTEX(macinfo));
24817c478bd9Sstevel@tonic-gate }
24827c478bd9Sstevel@tonic-gate #endif
2483