xref: /onnv-gate/usr/src/uts/common/io/strplumb.c (revision 4172:a08c5d71a686)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
52343Sseb  * Common Development and Distribution License (the "License").
62343Sseb  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
223448Sdh155122  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include	<sys/param.h>
290Sstevel@tonic-gate #include	<sys/types.h>
300Sstevel@tonic-gate #include	<sys/user.h>
310Sstevel@tonic-gate #include	<sys/vfs.h>
320Sstevel@tonic-gate #include	<sys/vnode.h>
330Sstevel@tonic-gate #include	<sys/file.h>
340Sstevel@tonic-gate #include	<sys/stream.h>
350Sstevel@tonic-gate #include	<sys/stropts.h>
360Sstevel@tonic-gate #include	<sys/strsubr.h>
370Sstevel@tonic-gate #include	<sys/dlpi.h>
380Sstevel@tonic-gate #include	<sys/vnode.h>
390Sstevel@tonic-gate #include	<sys/socket.h>
400Sstevel@tonic-gate #include	<sys/sockio.h>
410Sstevel@tonic-gate #include	<net/if.h>
420Sstevel@tonic-gate 
430Sstevel@tonic-gate #include	<sys/cred.h>
440Sstevel@tonic-gate #include	<sys/sysmacros.h>
450Sstevel@tonic-gate 
460Sstevel@tonic-gate #include	<sys/sad.h>
470Sstevel@tonic-gate #include	<sys/kstr.h>
480Sstevel@tonic-gate #include	<sys/bootconf.h>
490Sstevel@tonic-gate #include	<sys/bootprops.h>
500Sstevel@tonic-gate 
510Sstevel@tonic-gate #include	<sys/errno.h>
520Sstevel@tonic-gate #include	<sys/modctl.h>
530Sstevel@tonic-gate #include	<sys/sunddi.h>
540Sstevel@tonic-gate #include	<sys/sunldi.h>
550Sstevel@tonic-gate #include	<sys/esunddi.h>
560Sstevel@tonic-gate #include	<sys/promif.h>
570Sstevel@tonic-gate 
580Sstevel@tonic-gate #include	<netinet/in.h>
590Sstevel@tonic-gate #include	<netinet/ip6.h>
600Sstevel@tonic-gate #include	<netinet/icmp6.h>
610Sstevel@tonic-gate #include	<netinet/sctp.h>
620Sstevel@tonic-gate #include	<inet/common.h>
630Sstevel@tonic-gate #include	<inet/ip.h>
640Sstevel@tonic-gate #include	<inet/ip6.h>
650Sstevel@tonic-gate #include	<inet/tcp.h>
660Sstevel@tonic-gate #include	<inet/sctp_ip.h>
670Sstevel@tonic-gate 
680Sstevel@tonic-gate #include	<sys/strlog.h>
690Sstevel@tonic-gate #include	<sys/log.h>
700Sstevel@tonic-gate #include	<sys/ethernet.h>
710Sstevel@tonic-gate #include	<sys/ddi_implfuncs.h>
720Sstevel@tonic-gate 
730Sstevel@tonic-gate #include	<sys/dld.h>
740Sstevel@tonic-gate 
750Sstevel@tonic-gate /*
760Sstevel@tonic-gate  * Debug Macros
770Sstevel@tonic-gate  */
780Sstevel@tonic-gate int	strplumbdebug = 0;
790Sstevel@tonic-gate 
800Sstevel@tonic-gate #define	DBG0(_f) \
810Sstevel@tonic-gate 	if (strplumbdebug != 0) \
820Sstevel@tonic-gate 		printf("strplumb: " _f)
830Sstevel@tonic-gate 
840Sstevel@tonic-gate #define	DBG1(_f, _a) \
850Sstevel@tonic-gate 	if (strplumbdebug != 0) \
860Sstevel@tonic-gate 		printf("strplumb: " _f, (_a))
870Sstevel@tonic-gate 
880Sstevel@tonic-gate #define	DBG2(_f, _a, _b) \
890Sstevel@tonic-gate 	if (strplumbdebug != 0) \
900Sstevel@tonic-gate 		printf("strplumb: " _f, (_a), (_b))
910Sstevel@tonic-gate 
920Sstevel@tonic-gate #define	DBG3(_f, _a, _b, _c) \
930Sstevel@tonic-gate 	if (strplumbdebug != 0) \
940Sstevel@tonic-gate 		printf("strplumb: " _f, (_a), (_b), (_c))
950Sstevel@tonic-gate 
960Sstevel@tonic-gate /*
970Sstevel@tonic-gate  * Module linkage information for the kernel.
980Sstevel@tonic-gate  */
990Sstevel@tonic-gate #define	STRPLUMB_IDENT	"STREAMS Plumbing Module v%I%"
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate static struct modlmisc modlmisc = {
1020Sstevel@tonic-gate 	&mod_miscops,
1030Sstevel@tonic-gate 	STRPLUMB_IDENT
1040Sstevel@tonic-gate };
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate static struct modlinkage modlinkage = {
1070Sstevel@tonic-gate 	MODREV_1,
1080Sstevel@tonic-gate 	&modlmisc,
1090Sstevel@tonic-gate 	NULL
1100Sstevel@tonic-gate };
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate int
1130Sstevel@tonic-gate _init(void)
1140Sstevel@tonic-gate {
1150Sstevel@tonic-gate 	return (mod_install(&modlinkage));
1160Sstevel@tonic-gate }
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate int
1190Sstevel@tonic-gate _fini(void)
1200Sstevel@tonic-gate {
1210Sstevel@tonic-gate 	return (mod_remove(&modlinkage));
1220Sstevel@tonic-gate }
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate int
1250Sstevel@tonic-gate _info(struct modinfo *modinfop)
1260Sstevel@tonic-gate {
1270Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
1280Sstevel@tonic-gate }
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate #define	ARP		"arp"
1310Sstevel@tonic-gate #define	TCP		"tcp"
1320Sstevel@tonic-gate #define	TCP6		"tcp6"
1330Sstevel@tonic-gate #define	UDP		"udp"
1340Sstevel@tonic-gate #define	UDP6		"udp6"
1350Sstevel@tonic-gate #define	SCTP		"sctp"
1360Sstevel@tonic-gate #define	SCTP6		"sctp6"
1370Sstevel@tonic-gate #define	ICMP		"icmp"
1380Sstevel@tonic-gate #define	ICMP6		"icmp6"
1390Sstevel@tonic-gate #define	IP		"ip"
1400Sstevel@tonic-gate #define	IP6		"ip6"
1410Sstevel@tonic-gate #define	TIMOD		"timod"
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate #define	UDPDEV		"/devices/pseudo/udp@0:udp"
1440Sstevel@tonic-gate #define	TCP6DEV		"/devices/pseudo/tcp6@0:tcp6"
1450Sstevel@tonic-gate #define	SCTP6DEV	"/devices/pseudo/sctp6@0:sctp6"
1460Sstevel@tonic-gate #define	IP6DEV		"/devices/pseudo/ip6@0:ip6"
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate typedef struct strplumb_modspec {
1490Sstevel@tonic-gate 	char	*sm_type;
1500Sstevel@tonic-gate 	char	*sm_name;
1510Sstevel@tonic-gate } strplumb_modspec_t;
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate static strplumb_modspec_t	strplumb_modlist[] = {
1540Sstevel@tonic-gate 	{ "drv", DLD_DRIVER_NAME },
1550Sstevel@tonic-gate 	{ "drv", IP },
1560Sstevel@tonic-gate 	{ "drv", IP6 },
1570Sstevel@tonic-gate 	{ "drv", TCP },
1580Sstevel@tonic-gate 	{ "drv", TCP6 },
1590Sstevel@tonic-gate 	{ "drv", UDP },
1600Sstevel@tonic-gate 	{ "drv", UDP6 },
1610Sstevel@tonic-gate 	{ "drv", SCTP },
1620Sstevel@tonic-gate 	{ "drv", SCTP6 },
1630Sstevel@tonic-gate 	{ "drv", ICMP },
1640Sstevel@tonic-gate 	{ "drv", ICMP6 },
1650Sstevel@tonic-gate 	{ "drv", ARP },
1660Sstevel@tonic-gate 	{ "strmod", TIMOD }
1670Sstevel@tonic-gate };
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate /*
1700Sstevel@tonic-gate  * Called from swapgeneric.c:loadrootmodules() in the network boot case.
1710Sstevel@tonic-gate  */
1720Sstevel@tonic-gate int
1730Sstevel@tonic-gate strplumb_load(void)
1740Sstevel@tonic-gate {
1750Sstevel@tonic-gate 	uint_t			i;
1760Sstevel@tonic-gate 	strplumb_modspec_t	*p;
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate 	DBG0("loading modules\n");
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate 	for (i = 0, p = strplumb_modlist;
1810Sstevel@tonic-gate 	    i < sizeof (strplumb_modlist) / sizeof (strplumb_modlist[0]);
1820Sstevel@tonic-gate 	    i++, p++) {
1830Sstevel@tonic-gate 		if (modloadonly(p->sm_type, p->sm_name) < 0) {
1840Sstevel@tonic-gate 			printf("strplumb: failed to load %s/%s\n",
1850Sstevel@tonic-gate 			    p->sm_type, p->sm_name);
1860Sstevel@tonic-gate 			return (EFAULT);
1870Sstevel@tonic-gate 		}
1880Sstevel@tonic-gate 	}
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate 	return (0);
1910Sstevel@tonic-gate }
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate static int
1940Sstevel@tonic-gate strplumb_init(void)
1950Sstevel@tonic-gate {
1960Sstevel@tonic-gate 	uint_t			i;
1970Sstevel@tonic-gate 	strplumb_modspec_t	*p;
1980Sstevel@tonic-gate 	int			err;
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate 	DBG0("initializing modules\n");
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate 	for (i = 0, p = strplumb_modlist;
2030Sstevel@tonic-gate 	    i < sizeof (strplumb_modlist) / sizeof (strplumb_modlist[0]);
2040Sstevel@tonic-gate 	    i++, p++) {
2050Sstevel@tonic-gate 		if (strcmp(p->sm_type, "drv") == 0)
2060Sstevel@tonic-gate 			err = (i_ddi_attach_pseudo_node(p->sm_name) != NULL) ?
2070Sstevel@tonic-gate 			    0 : EFAULT;
2080Sstevel@tonic-gate 		else
2090Sstevel@tonic-gate 			err = (modload(p->sm_type, p->sm_name) < 0) ?
2100Sstevel@tonic-gate 			    EFAULT : 0;
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate 		if (err != 0)  {
2130Sstevel@tonic-gate 			printf("strplumb: failed to initialize %s/%s\n",
2140Sstevel@tonic-gate 			    p->sm_type, p->sm_name);
2150Sstevel@tonic-gate 			return (err);
2160Sstevel@tonic-gate 		}
2170Sstevel@tonic-gate 	}
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 	return (0);
2200Sstevel@tonic-gate }
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate static int
2230Sstevel@tonic-gate strplumb_autopush(void)
2240Sstevel@tonic-gate {
2250Sstevel@tonic-gate 	major_t		maj;
2260Sstevel@tonic-gate 	minor_t		min;
2270Sstevel@tonic-gate 	char		*mods[5];
2280Sstevel@tonic-gate 	uint_t		anchor = 1;
2290Sstevel@tonic-gate 	int		err;
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate 	min = (minor_t)-1;
2320Sstevel@tonic-gate 	mods[1] = NULL;
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate 	/*
2350Sstevel@tonic-gate 	 * UDP
2360Sstevel@tonic-gate 	 */
2370Sstevel@tonic-gate 	DBG0("setting up udp autopush\n");
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 	mods[0] = UDP;
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate 	maj = ddi_name_to_major(UDP);
2420Sstevel@tonic-gate 	if ((err = kstr_autopush(SET_AUTOPUSH, &maj, &min, NULL, &anchor,
2430Sstevel@tonic-gate 	    mods)) != 0) {
2440Sstevel@tonic-gate 		printf("strplumb: kstr_autopush(SET/UDP) failed: %d\n", err);
2450Sstevel@tonic-gate 		return (err);
2460Sstevel@tonic-gate 	}
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate 	maj = ddi_name_to_major(UDP6);
2490Sstevel@tonic-gate 	if ((err = kstr_autopush(SET_AUTOPUSH, &maj, &min, NULL, &anchor,
2500Sstevel@tonic-gate 	    mods)) != 0) {
2510Sstevel@tonic-gate 		printf("strplumb: kstr_autopush(SET/UDP6) failed: %d\n", err);
2520Sstevel@tonic-gate 		return (err);
2530Sstevel@tonic-gate 	}
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate 	/*
2560Sstevel@tonic-gate 	 * ICMP
2570Sstevel@tonic-gate 	 */
2580Sstevel@tonic-gate 	DBG0("setting up icmp autopush\n");
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate 	mods[0] = ICMP;
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate 	maj = ddi_name_to_major(ICMP);
2630Sstevel@tonic-gate 	if ((err = kstr_autopush(SET_AUTOPUSH, &maj, &min, NULL, NULL,
2640Sstevel@tonic-gate 	    mods)) != 0) {
2650Sstevel@tonic-gate 		printf("strplumb: kstr_autopush(SET/ICMP) failed: %d\n", err);
2660Sstevel@tonic-gate 		return (err);
2670Sstevel@tonic-gate 	}
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate 	maj = ddi_name_to_major(ICMP6);
2700Sstevel@tonic-gate 	if ((err = kstr_autopush(SET_AUTOPUSH, &maj, &min, NULL, NULL,
2710Sstevel@tonic-gate 	    mods)) != 0) {
2720Sstevel@tonic-gate 		printf("strplumb: kstr_autopush(SET/ICMP6) failed: %d\n", err);
2730Sstevel@tonic-gate 		return (err);
2740Sstevel@tonic-gate 	}
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate 	/*
2770Sstevel@tonic-gate 	 * ARP
2780Sstevel@tonic-gate 	 */
2790Sstevel@tonic-gate 	DBG0("setting up arp autopush\n");
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate 	mods[0] = ARP;
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate 	maj = ddi_name_to_major(ARP);
2840Sstevel@tonic-gate 	if ((err = kstr_autopush(SET_AUTOPUSH, &maj, &min, NULL, &anchor,
2850Sstevel@tonic-gate 	    mods)) != 0) {
2860Sstevel@tonic-gate 		printf("strplumb: kstr_autopush(SET/ARP) failed: %d\n", err);
2870Sstevel@tonic-gate 		return (err);
2880Sstevel@tonic-gate 	}
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate 	return (0);
2910Sstevel@tonic-gate }
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate static int
2940Sstevel@tonic-gate strplumb_sctpq(ldi_ident_t li)
2950Sstevel@tonic-gate {
2960Sstevel@tonic-gate 	ldi_handle_t	lh = NULL;
2970Sstevel@tonic-gate 	int		err;
2980Sstevel@tonic-gate 	int		rval;
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate 	DBG0("configuring SCTP default queue\n");
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate 	if ((err = ldi_open_by_name(SCTP6DEV, FREAD|FWRITE, CRED(), &lh,
3030Sstevel@tonic-gate 	    li)) != 0) {
3040Sstevel@tonic-gate 		printf("strplumb: open of SCTP6DEV failed: %d\n", err);
3050Sstevel@tonic-gate 		return (err);
3060Sstevel@tonic-gate 	}
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate 	if ((err = ldi_ioctl(lh, SCTP_IOC_DEFAULT_Q, (intptr_t)0, FKIOCTL,
3090Sstevel@tonic-gate 	    CRED(), &rval)) != 0) {
3100Sstevel@tonic-gate 		printf("strplumb: failed to set SCTP default queue: %d\n",
3110Sstevel@tonic-gate 		    err);
3120Sstevel@tonic-gate 		(void) ldi_close(lh, FREAD|FWRITE, CRED());
3130Sstevel@tonic-gate 		return (err);
3140Sstevel@tonic-gate 	}
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate 	return (0);
3170Sstevel@tonic-gate }
3180Sstevel@tonic-gate 
3190Sstevel@tonic-gate static int
3200Sstevel@tonic-gate strplumb_tcpq(ldi_ident_t li)
3210Sstevel@tonic-gate {
3220Sstevel@tonic-gate 	ldi_handle_t	lh = NULL;
3230Sstevel@tonic-gate 	ldi_handle_t	ip_lh = NULL;
3240Sstevel@tonic-gate 	int		err;
3250Sstevel@tonic-gate 	int		rval;
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate 	DBG0("configuring TCP default queue\n");
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate 	/*
3300Sstevel@tonic-gate 	 * We open IP6DEV here because we need to have it open to in
3310Sstevel@tonic-gate 	 * order to open TCP6DEV successfully.
3320Sstevel@tonic-gate 	 */
3330Sstevel@tonic-gate 	if ((err = ldi_open_by_name(IP6DEV, FREAD|FWRITE, CRED(), &ip_lh,
3340Sstevel@tonic-gate 	    li)) != 0) {
3350Sstevel@tonic-gate 		printf("strplumb: open of IP6DEV failed: %d\n", err);
3360Sstevel@tonic-gate 		return (err);
3370Sstevel@tonic-gate 	}
3380Sstevel@tonic-gate 
3390Sstevel@tonic-gate 	/*
3400Sstevel@tonic-gate 	 * We set the tcp default queue to IPv6 because IPv4 falls back to
3410Sstevel@tonic-gate 	 * IPv6 when it can't find a client, but IPv6 does not fall back to
3420Sstevel@tonic-gate 	 * IPv4.
3430Sstevel@tonic-gate 	 */
3440Sstevel@tonic-gate 	if ((err = ldi_open_by_name(TCP6DEV, FREAD|FWRITE, CRED(), &lh,
3450Sstevel@tonic-gate 	    li)) != 0) {
3460Sstevel@tonic-gate 		printf("strplumb: open of TCP6DEV failed: %d\n", err);
3470Sstevel@tonic-gate 		goto done;
3480Sstevel@tonic-gate 	}
3490Sstevel@tonic-gate 
3500Sstevel@tonic-gate 	if ((err = ldi_ioctl(lh, TCP_IOC_DEFAULT_Q, (intptr_t)0, FKIOCTL,
3510Sstevel@tonic-gate 	    CRED(), &rval)) != 0) {
3520Sstevel@tonic-gate 		printf("strplumb: failed to set TCP default queue: %d\n",
3530Sstevel@tonic-gate 		    err);
3540Sstevel@tonic-gate 		goto done;
3550Sstevel@tonic-gate 	}
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate done:
3580Sstevel@tonic-gate 	(void) ldi_close(ip_lh, FREAD|FWRITE, CRED());
3590Sstevel@tonic-gate 	return (err);
3600Sstevel@tonic-gate }
3610Sstevel@tonic-gate 
3620Sstevel@tonic-gate /*
3630Sstevel@tonic-gate  * Can be set in /etc/system in the case of local booting. See comment below.
3640Sstevel@tonic-gate  */
3650Sstevel@tonic-gate char	*ndev_name = 0;
3660Sstevel@tonic-gate int	ndev_unit = 0;
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate /*
3690Sstevel@tonic-gate  * If we booted diskless then strplumb() will have been called from
3700Sstevel@tonic-gate  * swapgeneric.c:rootconf(). All we can do in that case is plumb the
3710Sstevel@tonic-gate  * network device that we booted from.
3720Sstevel@tonic-gate  *
3730Sstevel@tonic-gate  * If we booted from a local disk, we will have been called from main(),
3740Sstevel@tonic-gate  * and normally we defer the plumbing of interfaces until network/physical.
3750Sstevel@tonic-gate  * This can be overridden by setting "ndev_name" in /etc/system.
3760Sstevel@tonic-gate  */
3770Sstevel@tonic-gate static int
378269Sericheng resolve_boot_path(void)
3790Sstevel@tonic-gate {
3800Sstevel@tonic-gate 	char			*devpath = NULL;
3810Sstevel@tonic-gate 	dev_info_t		*dip;
3820Sstevel@tonic-gate 	const char		*driver;
3830Sstevel@tonic-gate 	int			instance;
3840Sstevel@tonic-gate 
3850Sstevel@tonic-gate 	if (strncmp(rootfs.bo_fstype, "nfs", 3) == 0)
3860Sstevel@tonic-gate 		devpath = rootfs.bo_name;
3870Sstevel@tonic-gate #ifndef __sparc
3880Sstevel@tonic-gate 	else
3890Sstevel@tonic-gate 		devpath = strplumb_get_netdev_path();
3900Sstevel@tonic-gate #endif
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate 	if (devpath != NULL) {
3930Sstevel@tonic-gate 		DBG1("resolving boot-path: %s\n", devpath);
3940Sstevel@tonic-gate 
3950Sstevel@tonic-gate 		/*
3960Sstevel@tonic-gate 		 * Hold the devi since this is the root device.
3970Sstevel@tonic-gate 		 */
3980Sstevel@tonic-gate 		if ((dip = e_ddi_hold_devi_by_path(devpath, 0)) == NULL) {
3990Sstevel@tonic-gate 			printf("strplumb: unable to hold root device: %s\n",
4000Sstevel@tonic-gate 			    devpath);
4010Sstevel@tonic-gate 			return (ENXIO);
4020Sstevel@tonic-gate 		}
4030Sstevel@tonic-gate 
4040Sstevel@tonic-gate 		driver = ddi_driver_name(dip);
4050Sstevel@tonic-gate 		instance = ddi_get_instance(dip);
4060Sstevel@tonic-gate 	} else {
4070Sstevel@tonic-gate 		if (ndev_name == NULL)
4080Sstevel@tonic-gate 			return (ENODEV);
4090Sstevel@tonic-gate 
4100Sstevel@tonic-gate 		DBG2("using ndev_name (%s) ndev_unit (%d)\n", ndev_name,
4110Sstevel@tonic-gate 		    ndev_unit);
4120Sstevel@tonic-gate 
4130Sstevel@tonic-gate 		if (i_ddi_attach_hw_nodes(ndev_name) != DDI_SUCCESS) {
4140Sstevel@tonic-gate 			printf("strplumb: cannot load ndev_name '%s'\n",
4150Sstevel@tonic-gate 			    ndev_name);
4160Sstevel@tonic-gate 			return (ENXIO);
4170Sstevel@tonic-gate 		}
4180Sstevel@tonic-gate 
4190Sstevel@tonic-gate 		driver = ndev_name;
4200Sstevel@tonic-gate 		instance = ndev_unit;
4210Sstevel@tonic-gate 	}
4220Sstevel@tonic-gate 
4230Sstevel@tonic-gate 	(void) snprintf(rootfs.bo_devname, BO_MAXOBJNAME,
424269Sericheng 	    "/devices/pseudo/clone@0:%s", driver);
4250Sstevel@tonic-gate 	(void) snprintf(rootfs.bo_ifname, BO_MAXOBJNAME, "%s%d",
4260Sstevel@tonic-gate 	    driver, instance);
4270Sstevel@tonic-gate 	rootfs.bo_ppa = instance;
428269Sericheng 	return (0);
4290Sstevel@tonic-gate }
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate static int
4320Sstevel@tonic-gate getifflags(ldi_handle_t lh, struct lifreq *lifrp)
4330Sstevel@tonic-gate {
4340Sstevel@tonic-gate 	struct strioctl	iocb;
4350Sstevel@tonic-gate 	int		rval;
4360Sstevel@tonic-gate 
4370Sstevel@tonic-gate 	iocb.ic_cmd = SIOCGLIFFLAGS;
4380Sstevel@tonic-gate 	iocb.ic_timout = 15;
4390Sstevel@tonic-gate 	iocb.ic_len = sizeof (struct lifreq);
4400Sstevel@tonic-gate 	iocb.ic_dp = (char *)lifrp;
4410Sstevel@tonic-gate 
4420Sstevel@tonic-gate 	return (ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, CRED(), &rval));
4430Sstevel@tonic-gate 
4440Sstevel@tonic-gate }
4450Sstevel@tonic-gate 
4460Sstevel@tonic-gate static int
4470Sstevel@tonic-gate setifname(ldi_handle_t lh, struct lifreq *lifrp)
4480Sstevel@tonic-gate {
4490Sstevel@tonic-gate 	struct strioctl	iocb;
4500Sstevel@tonic-gate 	int		rval;
4510Sstevel@tonic-gate 
4520Sstevel@tonic-gate 	iocb.ic_cmd = SIOCSLIFNAME;
4530Sstevel@tonic-gate 	iocb.ic_timout = 15;
4540Sstevel@tonic-gate 	iocb.ic_len = sizeof (struct lifreq);
4550Sstevel@tonic-gate 	iocb.ic_dp = (char *)lifrp;
4560Sstevel@tonic-gate 
4570Sstevel@tonic-gate 	return (ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, CRED(), &rval));
4580Sstevel@tonic-gate }
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate static int
4610Sstevel@tonic-gate strplumb_dev(ldi_ident_t li)
4620Sstevel@tonic-gate {
4630Sstevel@tonic-gate 	ldi_handle_t	lh = NULL;
4640Sstevel@tonic-gate 	ldi_handle_t	mux_lh = NULL;
4650Sstevel@tonic-gate 	int		err;
4660Sstevel@tonic-gate 	struct lifreq	lifr;
4670Sstevel@tonic-gate 	struct ifreq	ifr;
4680Sstevel@tonic-gate 	int		rval;
4690Sstevel@tonic-gate 
4700Sstevel@tonic-gate 	bzero(&lifr, sizeof (struct lifreq));
4710Sstevel@tonic-gate 	bzero(&ifr, sizeof (ifr));
4720Sstevel@tonic-gate 
4730Sstevel@tonic-gate 	/*
4740Sstevel@tonic-gate 	 * Now set up the links. Ultimately, we should have two streams
4750Sstevel@tonic-gate 	 * permanently linked underneath UDP (which is actually IP with UDP
4760Sstevel@tonic-gate 	 * autopushed). One stream consists of the ARP-[ifname] combination,
4770Sstevel@tonic-gate 	 * while the other consists of ARP-IP-[ifname]. The second combination
4780Sstevel@tonic-gate 	 * seems a little weird, but is linked underneath UDP just to keep it
4790Sstevel@tonic-gate 	 * around.
4800Sstevel@tonic-gate 	 *
4810Sstevel@tonic-gate 	 * We pin underneath UDP here to match what is done in ifconfig(1m);
4820Sstevel@tonic-gate 	 * otherwise, ifconfig will be unable to unplumb the stream (the major
4830Sstevel@tonic-gate 	 * number and mux id must both match for a successful I_PUNLINK).
4840Sstevel@tonic-gate 	 *
4850Sstevel@tonic-gate 	 * There are subtleties in the plumbing which make it essential to
4860Sstevel@tonic-gate 	 * follow the logic used in ifconfig(1m) very closely.
4870Sstevel@tonic-gate 	 */
4880Sstevel@tonic-gate 
4890Sstevel@tonic-gate 	/*
4900Sstevel@tonic-gate 	 * Plumb UDP-ARP-IP-<dev>
4910Sstevel@tonic-gate 	 */
4920Sstevel@tonic-gate 
4930Sstevel@tonic-gate 	if ((err = ldi_open_by_name(rootfs.bo_devname, FREAD|FWRITE, CRED(),
4940Sstevel@tonic-gate 	    &lh, li)) != 0) {
4950Sstevel@tonic-gate 		printf("strplumb: open %s failed: %d\n", rootfs.bo_devname,
4960Sstevel@tonic-gate 		    err);
4970Sstevel@tonic-gate 		goto done;
4980Sstevel@tonic-gate 	}
4990Sstevel@tonic-gate 
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate 	if ((err = ldi_ioctl(lh, I_PUSH, (intptr_t)IP, FKIOCTL, CRED(),
5020Sstevel@tonic-gate 	    &rval)) != 0) {
5030Sstevel@tonic-gate 		printf("strplumb: push IP failed: %d\n", err);
5040Sstevel@tonic-gate 		goto done;
5050Sstevel@tonic-gate 	}
5060Sstevel@tonic-gate 
5070Sstevel@tonic-gate 	if ((err = getifflags(lh, &lifr)) != 0)
5080Sstevel@tonic-gate 		goto done;
5090Sstevel@tonic-gate 
5100Sstevel@tonic-gate 	lifr.lifr_flags |= IFF_IPV4;
5110Sstevel@tonic-gate 	lifr.lifr_flags &= ~IFF_IPV6;
5120Sstevel@tonic-gate 
5130Sstevel@tonic-gate 	if ((err = ldi_ioctl(lh, I_PUSH, (intptr_t)ARP, FKIOCTL, CRED(),
5140Sstevel@tonic-gate 	    &rval)) != 0) {
5150Sstevel@tonic-gate 		printf("strplumb: push ARP failed: %d\n", err);
5160Sstevel@tonic-gate 		goto done;
5170Sstevel@tonic-gate 	}
5180Sstevel@tonic-gate 
5190Sstevel@tonic-gate 	(void) strlcpy(lifr.lifr_name, rootfs.bo_ifname,
5200Sstevel@tonic-gate 	    sizeof (lifr.lifr_name));
5210Sstevel@tonic-gate 	lifr.lifr_ppa = rootfs.bo_ppa;
5220Sstevel@tonic-gate 
5230Sstevel@tonic-gate 	if ((err = setifname(lh, &lifr)) != 0)
5240Sstevel@tonic-gate 		goto done;
5250Sstevel@tonic-gate 
5260Sstevel@tonic-gate 	/* Get the flags and check if ARP is needed */
5270Sstevel@tonic-gate 	if ((err = getifflags(lh, &lifr)) != 0) {
5280Sstevel@tonic-gate 		printf("strplumb: getifflags %s IP failed, error %d\n",
5290Sstevel@tonic-gate 		    lifr.lifr_name, err);
5300Sstevel@tonic-gate 		goto done;
5310Sstevel@tonic-gate 	}
5320Sstevel@tonic-gate 
5330Sstevel@tonic-gate 	/* Pop out ARP if not needed */
5340Sstevel@tonic-gate 	if (lifr.lifr_flags & IFF_NOARP) {
5350Sstevel@tonic-gate 		err = ldi_ioctl(lh, I_POP, (intptr_t)0, FKIOCTL, CRED(),
5360Sstevel@tonic-gate 		    &rval);
5370Sstevel@tonic-gate 		if (err != 0) {
5380Sstevel@tonic-gate 			printf("strplumb: pop ARP failed, error %d\n", err);
5390Sstevel@tonic-gate 			goto done;
5400Sstevel@tonic-gate 		}
5410Sstevel@tonic-gate 	}
5420Sstevel@tonic-gate 
5430Sstevel@tonic-gate 	if ((err = ldi_open_by_name(UDPDEV, FREAD|FWRITE, CRED(), &mux_lh,
5440Sstevel@tonic-gate 	    li)) != 0) {
5450Sstevel@tonic-gate 		printf("strplumb: open of UDPDEV failed: %d\n", err);
5460Sstevel@tonic-gate 		goto done;
5470Sstevel@tonic-gate 	}
5480Sstevel@tonic-gate 
5490Sstevel@tonic-gate 	if ((err = ldi_ioctl(mux_lh, I_PLINK, (intptr_t)lh,
5500Sstevel@tonic-gate 	    FREAD|FWRITE|FNOCTTY|FKIOCTL, CRED(),
5510Sstevel@tonic-gate 	    &(ifr.ifr_ip_muxid))) != 0) {
5520Sstevel@tonic-gate 		printf("strplumb: plink UDP-ARP-IP-%s failed: %d\n",
5530Sstevel@tonic-gate 		    rootfs.bo_ifname, err);
5540Sstevel@tonic-gate 		goto done;
5550Sstevel@tonic-gate 	}
5560Sstevel@tonic-gate 
5570Sstevel@tonic-gate 	DBG2("UDP-ARP-IP-%s muxid: %d\n", rootfs.bo_ifname, ifr.ifr_ip_muxid);
5580Sstevel@tonic-gate 
5590Sstevel@tonic-gate 	(void) ldi_close(lh, FREAD|FWRITE, CRED());
5600Sstevel@tonic-gate 	lh = NULL;
5610Sstevel@tonic-gate 
5620Sstevel@tonic-gate 	/*
5630Sstevel@tonic-gate 	 * Plumb UDP-ARP-<dev>
5640Sstevel@tonic-gate 	 */
5650Sstevel@tonic-gate 
5660Sstevel@tonic-gate 	if ((err = ldi_open_by_name(rootfs.bo_devname, FREAD|FWRITE, CRED(),
5670Sstevel@tonic-gate 	    &lh, li)) != 0) {
5680Sstevel@tonic-gate 		printf("strplumb: open %s failed: %d\n", rootfs.bo_devname,
5690Sstevel@tonic-gate 		    err);
5700Sstevel@tonic-gate 		goto done;
5710Sstevel@tonic-gate 	}
5720Sstevel@tonic-gate 
5730Sstevel@tonic-gate 	if ((err = ldi_ioctl(lh, I_PUSH, (intptr_t)ARP, FKIOCTL, CRED(),
5740Sstevel@tonic-gate 	    &rval)) != 0) {
5750Sstevel@tonic-gate 		printf("strplumb: push ARP failed: %d\n", err);
5760Sstevel@tonic-gate 		goto done;
5770Sstevel@tonic-gate 	}
5780Sstevel@tonic-gate 
5790Sstevel@tonic-gate 	if ((err = setifname(lh, &lifr)) != 0)
5800Sstevel@tonic-gate 		goto done;
5810Sstevel@tonic-gate 
5820Sstevel@tonic-gate 	if ((err = ldi_ioctl(mux_lh, I_PLINK, (intptr_t)lh,
5830Sstevel@tonic-gate 	    FREAD|FWRITE|FNOCTTY|FKIOCTL, CRED(),
5840Sstevel@tonic-gate 	    &(ifr.ifr_arp_muxid))) != 0) {
5850Sstevel@tonic-gate 		printf("strplumb: plink UDP-ARP-%s failed: %d\n",
5860Sstevel@tonic-gate 		    rootfs.bo_ifname, err);
5870Sstevel@tonic-gate 		goto done;
5880Sstevel@tonic-gate 	}
5890Sstevel@tonic-gate 
5900Sstevel@tonic-gate 	DBG2("UDP-ARP-%s muxid: %d\n", rootfs.bo_ifname, ifr.ifr_arp_muxid);
5910Sstevel@tonic-gate 
5920Sstevel@tonic-gate 	/*
5930Sstevel@tonic-gate 	 * Cache the mux ids.
5940Sstevel@tonic-gate 	 */
5950Sstevel@tonic-gate 	(void) strlcpy(ifr.ifr_name, rootfs.bo_ifname, sizeof (ifr.ifr_name));
5960Sstevel@tonic-gate 
5970Sstevel@tonic-gate 	if ((err = ldi_ioctl(mux_lh, SIOCSIFMUXID, (intptr_t)&ifr, FKIOCTL,
5980Sstevel@tonic-gate 	    CRED(), &rval)) != 0) {
5990Sstevel@tonic-gate 		printf("strplumb: SIOCSIFMUXID failed: %d\n", err);
6000Sstevel@tonic-gate 		goto done;
6010Sstevel@tonic-gate 	}
6020Sstevel@tonic-gate 
6030Sstevel@tonic-gate done:
6040Sstevel@tonic-gate 	if (lh != NULL)
6050Sstevel@tonic-gate 		(void) ldi_close(lh, FREAD|FWRITE, CRED());
6060Sstevel@tonic-gate 
6070Sstevel@tonic-gate 	if (mux_lh != NULL)
6080Sstevel@tonic-gate 		(void) ldi_close(mux_lh, FREAD|FWRITE, CRED());
6090Sstevel@tonic-gate 
6100Sstevel@tonic-gate 	return (err);
6110Sstevel@tonic-gate }
6120Sstevel@tonic-gate 
6130Sstevel@tonic-gate /*
6140Sstevel@tonic-gate  * Do streams plumbing for internet protocols.
6150Sstevel@tonic-gate  */
6160Sstevel@tonic-gate int
6170Sstevel@tonic-gate strplumb(void)
6180Sstevel@tonic-gate {
6190Sstevel@tonic-gate 	ldi_ident_t	li;
6200Sstevel@tonic-gate 	int		err;
6210Sstevel@tonic-gate 
6220Sstevel@tonic-gate 	if ((err = strplumb_init()) != 0)
6230Sstevel@tonic-gate 		return (err);
6240Sstevel@tonic-gate 
6250Sstevel@tonic-gate 	if ((err = strplumb_autopush()) != 0)
6260Sstevel@tonic-gate 		return (err);
6270Sstevel@tonic-gate 
6280Sstevel@tonic-gate 	if ((err = ldi_ident_from_mod(&modlinkage, &li)) != 0)
6290Sstevel@tonic-gate 		return (err);
6300Sstevel@tonic-gate 
6313448Sdh155122 	/*
6323448Sdh155122 	 * Setup the TCP and SCTP default queues for the global stack.
6333448Sdh155122 	 * tcp/sctp_stack_init will do this for additional stack instances.
6343448Sdh155122 	 */
6350Sstevel@tonic-gate 	if ((err = strplumb_sctpq(li)) != 0)
6360Sstevel@tonic-gate 		goto done;
6370Sstevel@tonic-gate 
6380Sstevel@tonic-gate 	if ((err = strplumb_tcpq(li)) != 0)
6390Sstevel@tonic-gate 		goto done;
6400Sstevel@tonic-gate 
641269Sericheng 	if ((err = resolve_boot_path()) != 0)
6420Sstevel@tonic-gate 		goto done;
6430Sstevel@tonic-gate 
6440Sstevel@tonic-gate 	DBG1("rootfs.bo_devname: %s\n", rootfs.bo_devname);
6450Sstevel@tonic-gate 	DBG1("rootfs.bo_ifname: %s\n", rootfs.bo_ifname);
6460Sstevel@tonic-gate 	DBG1("rootfs.bo_ppa: %d\n", rootfs.bo_ppa);
6470Sstevel@tonic-gate 
6480Sstevel@tonic-gate 	if ((err = strplumb_dev(li)) != 0)
6490Sstevel@tonic-gate 		goto done;
6500Sstevel@tonic-gate 
6510Sstevel@tonic-gate done:
6520Sstevel@tonic-gate 	ldi_ident_release(li);
6530Sstevel@tonic-gate 
6540Sstevel@tonic-gate 	return (err);
6550Sstevel@tonic-gate }
6560Sstevel@tonic-gate 
6570Sstevel@tonic-gate /* multiboot:  diskless boot interface discovery */
6580Sstevel@tonic-gate 
6590Sstevel@tonic-gate #ifndef	__sparc
6600Sstevel@tonic-gate 
6610Sstevel@tonic-gate static uchar_t boot_macaddr[16];
6620Sstevel@tonic-gate static int boot_maclen;
6630Sstevel@tonic-gate static uchar_t *getmacaddr(dev_info_t *dip, int *maclen);
6640Sstevel@tonic-gate static int matchmac(dev_info_t *dip, void *arg);
665307Sszhou int dl_attach(ldi_handle_t lh, int unit);
666307Sszhou int dl_bind(ldi_handle_t lh, uint_t sap, uint_t max_conn,
6670Sstevel@tonic-gate     uint_t service, uint_t conn_mgmt);
668307Sszhou int dl_phys_addr(ldi_handle_t lh, struct ether_addr *eaddr);
6690Sstevel@tonic-gate 
670*4172Ssetje #endif  /* !__sparc */
671*4172Ssetje 
6720Sstevel@tonic-gate char *
6730Sstevel@tonic-gate strplumb_get_netdev_path(void)
6740Sstevel@tonic-gate {
675*4172Ssetje #ifndef	__sparc
6760Sstevel@tonic-gate 	char *macstr, *devpath = NULL;
6770Sstevel@tonic-gate 	uchar_t *bootp;
6780Sstevel@tonic-gate 	uint_t bootp_len, len;
6790Sstevel@tonic-gate 
6800Sstevel@tonic-gate 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
6810Sstevel@tonic-gate 	    DDI_PROP_DONTPASS, BP_BOOT_MAC, &macstr) == DDI_SUCCESS) {
6820Sstevel@tonic-gate 		/*
6830Sstevel@tonic-gate 		 * hard coded ether mac len for booting floppy on
6840Sstevel@tonic-gate 		 * machines with old cards
6850Sstevel@tonic-gate 		 */
6860Sstevel@tonic-gate 		boot_maclen = ether_aton(macstr, boot_macaddr);
6870Sstevel@tonic-gate 		if (boot_maclen != 6) {
6880Sstevel@tonic-gate 			cmn_err(CE_WARN,
6890Sstevel@tonic-gate 			    "malformed boot_mac property, %d bytes",
6900Sstevel@tonic-gate 			    boot_maclen);
6910Sstevel@tonic-gate 		}
6920Sstevel@tonic-gate 		ddi_prop_free(macstr);
6930Sstevel@tonic-gate 	} else if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, ddi_root_node(),
6940Sstevel@tonic-gate 	    DDI_PROP_DONTPASS, BP_BOOTP_RESPONSE, &bootp, &bootp_len)
6950Sstevel@tonic-gate 	    == DDI_SUCCESS) {
6960Sstevel@tonic-gate 
6970Sstevel@tonic-gate 		/*
6980Sstevel@tonic-gate 		 * These offsets are defined by dhcp standard
6990Sstevel@tonic-gate 		 * Should use structure offsets
7000Sstevel@tonic-gate 		 */
7010Sstevel@tonic-gate 		boot_maclen = *(bootp + 2);
7020Sstevel@tonic-gate 		ASSERT(boot_maclen <= 16);
7030Sstevel@tonic-gate 		(void) bcopy(bootp + 28, boot_macaddr, boot_maclen);
7040Sstevel@tonic-gate 
7050Sstevel@tonic-gate 		/* encode to ascii string to match what sparc OBP exports */
706129Ssetje 		dhcack = kmem_zalloc(bootp_len * 2 + IFNAMSIZ + 2, KM_SLEEP);
7070Sstevel@tonic-gate 		(void) octet_to_hexascii(bootp, bootp_len, dhcack + IFNAMSIZ,
7080Sstevel@tonic-gate 		    &len);
7090Sstevel@tonic-gate 		ASSERT(len < bootp_len * 2 + 2);
7100Sstevel@tonic-gate 		ddi_prop_free(bootp);
7110Sstevel@tonic-gate 	} else
7120Sstevel@tonic-gate 		return (NULL);
7130Sstevel@tonic-gate 
7140Sstevel@tonic-gate 	ddi_walk_devs(ddi_root_node(), matchmac, (void *)&devpath);
7150Sstevel@tonic-gate 	return (devpath);
716*4172Ssetje 
717*4172Ssetje #else
718*4172Ssetje 	return (NULL);
719*4172Ssetje #endif  /* !__sparc */
7200Sstevel@tonic-gate }
7210Sstevel@tonic-gate 
722*4172Ssetje #ifndef __sparc
723*4172Ssetje 
7240Sstevel@tonic-gate /*
7250Sstevel@tonic-gate  * Get boot path from the boot_mac address
7260Sstevel@tonic-gate  */
7270Sstevel@tonic-gate /*ARGSUSED*/
7280Sstevel@tonic-gate static int
7290Sstevel@tonic-gate matchmac(dev_info_t *dip, void *arg)
7300Sstevel@tonic-gate {
7310Sstevel@tonic-gate 	char **devpathp = (char **)arg;
7320Sstevel@tonic-gate 	char *model_str;
7330Sstevel@tonic-gate 	uchar_t *macaddr;
7340Sstevel@tonic-gate 	int maclen;
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate 	/* XXX Should use "device-type" per IEEE 1275 */
7370Sstevel@tonic-gate 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, dip, 0,
7380Sstevel@tonic-gate 	    "model", &model_str) != DDI_SUCCESS)
7390Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
7400Sstevel@tonic-gate 
7410Sstevel@tonic-gate 	if (strcmp(model_str, "Ethernet controller") != 0) {
7420Sstevel@tonic-gate 		ddi_prop_free(model_str);
7430Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
7440Sstevel@tonic-gate 	}
7450Sstevel@tonic-gate 	ddi_prop_free(model_str);
7460Sstevel@tonic-gate 
7470Sstevel@tonic-gate 	/* We have a network device now */
7480Sstevel@tonic-gate 	if (i_ddi_attach_node_hierarchy(dip) != DDI_SUCCESS) {
7490Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
7500Sstevel@tonic-gate 	}
7510Sstevel@tonic-gate 
7520Sstevel@tonic-gate 	ASSERT(boot_maclen != 0);
7530Sstevel@tonic-gate 	macaddr = getmacaddr(dip, &maclen);
7540Sstevel@tonic-gate 	if (macaddr == NULL)
7550Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
7560Sstevel@tonic-gate 
7570Sstevel@tonic-gate 	if (maclen != boot_maclen ||
7580Sstevel@tonic-gate 	    bcmp(macaddr, boot_macaddr, maclen) != 0) {
7590Sstevel@tonic-gate 		kmem_free(macaddr, maclen);
7600Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
7610Sstevel@tonic-gate 	}
7620Sstevel@tonic-gate 
7630Sstevel@tonic-gate 	/* found hardware with the mac address */
7640Sstevel@tonic-gate 	(void) localetheraddr((struct ether_addr *)macaddr, NULL);
7650Sstevel@tonic-gate 	kmem_free(macaddr, maclen);
7660Sstevel@tonic-gate 
7670Sstevel@tonic-gate 	*devpathp = kmem_alloc(MAXPATHLEN, KM_SLEEP);
7680Sstevel@tonic-gate 	(void) ddi_pathname(dip, *devpathp);
7690Sstevel@tonic-gate 
7700Sstevel@tonic-gate 	/* fill in the name portion of dhcack */
7710Sstevel@tonic-gate 	if (dhcack)
7720Sstevel@tonic-gate 		(void) snprintf(dhcack, IFNAMSIZ, "%s%d",
7730Sstevel@tonic-gate 		    ddi_driver_name(dip), i_ddi_devi_get_ppa(dip));
7740Sstevel@tonic-gate 	return (DDI_WALK_TERMINATE);
7750Sstevel@tonic-gate }
7760Sstevel@tonic-gate 
7770Sstevel@tonic-gate static uchar_t *
7780Sstevel@tonic-gate getmacaddr_gldv3(char *drv, int inst, int *maclenp)
7790Sstevel@tonic-gate {
7800Sstevel@tonic-gate 	char ifname[16];
7810Sstevel@tonic-gate 	mac_handle_t mh;
7820Sstevel@tonic-gate 	uchar_t *macaddr;
7830Sstevel@tonic-gate 
7840Sstevel@tonic-gate 	(void) snprintf(ifname, sizeof (ifname), "%s%d", drv, inst);
7852343Sseb 	if (mac_open(ifname, inst, &mh) < 0) {
7860Sstevel@tonic-gate 		return (NULL);
7870Sstevel@tonic-gate 	}
7880Sstevel@tonic-gate 	*maclenp = sizeof (struct ether_addr);
7890Sstevel@tonic-gate 	macaddr = kmem_alloc(*maclenp, KM_SLEEP);
7900Sstevel@tonic-gate 	mac_unicst_get(mh, macaddr);
7910Sstevel@tonic-gate 	mac_close(mh);
7920Sstevel@tonic-gate 
7930Sstevel@tonic-gate 	return (macaddr);
7940Sstevel@tonic-gate }
7950Sstevel@tonic-gate 
7960Sstevel@tonic-gate static uchar_t *
7970Sstevel@tonic-gate getmacaddr(dev_info_t *dip, int *maclenp)
7980Sstevel@tonic-gate {
7990Sstevel@tonic-gate 	int rc, ppa;
8000Sstevel@tonic-gate 	ldi_ident_t li;
8010Sstevel@tonic-gate 	ldi_handle_t lh;
8020Sstevel@tonic-gate 	char *drv_name = (char *)ddi_driver_name(dip);
8030Sstevel@tonic-gate 	char *clonepath;
8040Sstevel@tonic-gate 	uchar_t *macaddr = NULL;
8050Sstevel@tonic-gate 
8060Sstevel@tonic-gate 	/* a simpler way to get mac address for GLDv3 drivers */
8070Sstevel@tonic-gate 	if (GLDV3_DRV(ddi_name_to_major(drv_name))) {
8080Sstevel@tonic-gate 		return (getmacaddr_gldv3(drv_name, ddi_get_instance(dip),
8090Sstevel@tonic-gate 		    maclenp));
8100Sstevel@tonic-gate 	}
8110Sstevel@tonic-gate 
8120Sstevel@tonic-gate 	if (rc = ldi_ident_from_mod(&modlinkage, &li)) {
8130Sstevel@tonic-gate 		cmn_err(CE_WARN,
8140Sstevel@tonic-gate 		    "getmacaddr: ldi_ident_from_mod failed: %d\n", rc);
8150Sstevel@tonic-gate 		return (NULL);
8160Sstevel@tonic-gate 	}
8170Sstevel@tonic-gate 
8180Sstevel@tonic-gate 	clonepath = kmem_alloc(MAXPATHLEN, KM_SLEEP);
8190Sstevel@tonic-gate 	(void) snprintf(clonepath, MAXPATHLEN,
8200Sstevel@tonic-gate 	    "/devices/pseudo/clone@0:%s", drv_name);
8210Sstevel@tonic-gate 
8220Sstevel@tonic-gate 	rc = ldi_open_by_name(clonepath, FREAD|FWRITE, CRED(), &lh, li);
8230Sstevel@tonic-gate 	ldi_ident_release(li);
8240Sstevel@tonic-gate 	if (rc) {
8250Sstevel@tonic-gate 		cmn_err(CE_WARN,
8260Sstevel@tonic-gate 		    "getmacaddr: ldi_open_by_name(%s) failed: %d\n",
8270Sstevel@tonic-gate 		    clonepath, rc);
8280Sstevel@tonic-gate 		kmem_free(clonepath, MAXPATHLEN);
8290Sstevel@tonic-gate 		return (NULL);
8300Sstevel@tonic-gate 	}
8310Sstevel@tonic-gate 	kmem_free(clonepath, MAXPATHLEN);
8320Sstevel@tonic-gate 
8330Sstevel@tonic-gate 	ppa = i_ddi_devi_get_ppa(dip);
8340Sstevel@tonic-gate 	if ((dl_attach(lh, ppa) != 0) ||
8350Sstevel@tonic-gate 	    (dl_bind(lh, ETHERTYPE_IP, 0, DL_CLDLS, 0) != 0)) {
8360Sstevel@tonic-gate 		(void) ldi_close(lh, FREAD|FWRITE, CRED());
8370Sstevel@tonic-gate 		cmn_err(CE_WARN,
8380Sstevel@tonic-gate 		    "getmacaddr: dl_attach/bind(%s%d) failed: %d\n",
8390Sstevel@tonic-gate 		    drv_name, ppa, rc);
8400Sstevel@tonic-gate 		return (NULL);
8410Sstevel@tonic-gate 	}
8420Sstevel@tonic-gate 	*maclenp = sizeof (struct ether_addr);
8430Sstevel@tonic-gate 	macaddr = kmem_alloc(*maclenp, KM_SLEEP);
8440Sstevel@tonic-gate 	if (dl_phys_addr(lh, (struct ether_addr *)macaddr) != 0) {
8450Sstevel@tonic-gate 		kmem_free(macaddr, *maclenp);
8460Sstevel@tonic-gate 		macaddr = NULL;
8470Sstevel@tonic-gate 		*maclenp = 0;
8480Sstevel@tonic-gate 		cmn_err(CE_WARN,
8490Sstevel@tonic-gate 		    "getmacaddr: dl_macaddr(%s%d) failed: %d\n",
8500Sstevel@tonic-gate 		    drv_name, ppa, rc);
8510Sstevel@tonic-gate 	}
8520Sstevel@tonic-gate 	(void) ldi_close(lh, FREAD|FWRITE, CRED());
8530Sstevel@tonic-gate 	return (macaddr);
8540Sstevel@tonic-gate }
8550Sstevel@tonic-gate 
8560Sstevel@tonic-gate #endif	/* !__sparc */
857307Sszhou 
858307Sszhou int
859307Sszhou dl_attach(ldi_handle_t lh, int unit)
860307Sszhou {
861307Sszhou 	dl_attach_req_t *attach_req;
862307Sszhou 	dl_error_ack_t *error_ack;
863307Sszhou 	union DL_primitives *dl_prim;
864307Sszhou 	mblk_t *mp;
865307Sszhou 	int error;
866307Sszhou 
867307Sszhou 	if ((mp = allocb(sizeof (dl_attach_req_t), BPRI_MED)) == NULL) {
868307Sszhou 		cmn_err(CE_WARN, "dl_attach: allocb failed");
869307Sszhou 		return (ENOSR);
870307Sszhou 	}
871307Sszhou 	mp->b_datap->db_type = M_PROTO;
872307Sszhou 	mp->b_wptr += sizeof (dl_attach_req_t);
873307Sszhou 
874307Sszhou 	attach_req = (dl_attach_req_t *)mp->b_rptr;
875307Sszhou 	attach_req->dl_primitive = DL_ATTACH_REQ;
876307Sszhou 	attach_req->dl_ppa = unit;
877307Sszhou 
878307Sszhou 	(void) ldi_putmsg(lh, mp);
879307Sszhou 	if ((error = ldi_getmsg(lh, &mp, (timestruc_t *)NULL)) != 0) {
880307Sszhou 		printf("dl_attach: ldi_getmsg failed: %d\n", error);
881307Sszhou 		return (error);
882307Sszhou 	}
883307Sszhou 
884307Sszhou 	dl_prim = (union DL_primitives *)mp->b_rptr;
885307Sszhou 	switch (dl_prim->dl_primitive) {
886307Sszhou 	case DL_OK_ACK:
887307Sszhou 		if ((mp->b_wptr-mp->b_rptr) < sizeof (dl_ok_ack_t)) {
888307Sszhou 			printf("dl_attach: DL_OK_ACK protocol error\n");
889307Sszhou 			break;
890307Sszhou 		}
891307Sszhou 		if (((dl_ok_ack_t *)dl_prim)->dl_correct_primitive !=
892307Sszhou 		    DL_ATTACH_REQ) {
893307Sszhou 			printf("dl_attach: DL_OK_ACK rtnd prim %u\n",
894307Sszhou 			    ((dl_ok_ack_t *)dl_prim)->dl_correct_primitive);
895307Sszhou 			break;
896307Sszhou 		}
897307Sszhou 		freemsg(mp);
898307Sszhou 		return (0);
899307Sszhou 
900307Sszhou 	case DL_ERROR_ACK:
901307Sszhou 		if ((mp->b_wptr-mp->b_rptr) < sizeof (dl_error_ack_t)) {
902307Sszhou 			printf("dl_attach: DL_ERROR_ACK protocol error\n");
903307Sszhou 			break;
904307Sszhou 		}
905307Sszhou 
906307Sszhou 		error_ack = (dl_error_ack_t *)dl_prim;
907307Sszhou 		switch (error_ack->dl_errno) {
908307Sszhou 		case DL_BADPPA:
909307Sszhou 			printf("dl_attach: DL_ERROR_ACK bad PPA\n");
910307Sszhou 			break;
911307Sszhou 
912307Sszhou 		case DL_ACCESS:
913307Sszhou 			printf("dl_attach: DL_ERROR_ACK access error\n");
914307Sszhou 			break;
915307Sszhou 
916307Sszhou 		default:
917307Sszhou 			printf("dl_attach: DLPI error %u\n",
918307Sszhou 			    error_ack->dl_errno);
919307Sszhou 			break;
920307Sszhou 		}
921307Sszhou 		break;
922307Sszhou 
923307Sszhou 	default:
924307Sszhou 		printf("dl_attach: bad ACK header %u\n", dl_prim->dl_primitive);
925307Sszhou 		break;
926307Sszhou 	}
927307Sszhou 
928307Sszhou 	/*
929307Sszhou 	 * Error return only.
930307Sszhou 	 */
931307Sszhou 	freemsg(mp);
932307Sszhou 	return (-1);
933307Sszhou }
934307Sszhou 
935307Sszhou int
936307Sszhou dl_bind(ldi_handle_t lh, uint_t sap, uint_t max_conn, uint_t service,
937307Sszhou 	uint_t conn_mgmt)
938307Sszhou {
939307Sszhou 	dl_bind_req_t *bind_req;
940307Sszhou 	dl_error_ack_t *error_ack;
941307Sszhou 	union DL_primitives *dl_prim;
942307Sszhou 	mblk_t *mp;
943307Sszhou 	int error;
944307Sszhou 
945307Sszhou 	if ((mp = allocb(sizeof (dl_bind_req_t), BPRI_MED)) == NULL) {
946307Sszhou 		cmn_err(CE_WARN, "dl_bind: allocb failed");
947307Sszhou 		return (ENOSR);
948307Sszhou 	}
949307Sszhou 	mp->b_datap->db_type = M_PROTO;
950307Sszhou 
951307Sszhou 	bind_req = (dl_bind_req_t *)mp->b_wptr;
952307Sszhou 	mp->b_wptr += sizeof (dl_bind_req_t);
953307Sszhou 	bind_req->dl_primitive = DL_BIND_REQ;
954307Sszhou 	bind_req->dl_sap = sap;
955307Sszhou 	bind_req->dl_max_conind = max_conn;
956307Sszhou 	bind_req->dl_service_mode = service;
957307Sszhou 	bind_req->dl_conn_mgmt = conn_mgmt;
958307Sszhou 	bind_req->dl_xidtest_flg = 0;
959307Sszhou 
960307Sszhou 	(void) ldi_putmsg(lh, mp);
961307Sszhou 	if ((error = ldi_getmsg(lh, &mp, (timestruc_t *)NULL)) != 0) {
962307Sszhou 		printf("dl_bind: ldi_getmsg failed: %d\n", error);
963307Sszhou 		return (error);
964307Sszhou 	}
965307Sszhou 
966307Sszhou 	dl_prim = (union DL_primitives *)mp->b_rptr;
967307Sszhou 	switch (dl_prim->dl_primitive) {
968307Sszhou 	case DL_BIND_ACK:
969307Sszhou 		if ((mp->b_wptr-mp->b_rptr) < sizeof (dl_bind_ack_t)) {
970307Sszhou 			printf("dl_bind: DL_BIND_ACK protocol error\n");
971307Sszhou 			break;
972307Sszhou 		}
973307Sszhou 		if (((dl_bind_ack_t *)dl_prim)->dl_sap != sap) {
974307Sszhou 			printf("dl_bind: DL_BIND_ACK bad sap %u\n",
975307Sszhou 			    ((dl_bind_ack_t *)dl_prim)->dl_sap);
976307Sszhou 			break;
977307Sszhou 		}
978307Sszhou 		freemsg(mp);
979307Sszhou 		return (0);
980307Sszhou 
981307Sszhou 	case DL_ERROR_ACK:
982307Sszhou 		if ((mp->b_wptr-mp->b_rptr) < sizeof (dl_error_ack_t)) {
983307Sszhou 			printf("dl_bind: DL_ERROR_ACK protocol error\n");
984307Sszhou 			break;
985307Sszhou 		}
986307Sszhou 
987307Sszhou 		error_ack = (dl_error_ack_t *)dl_prim;
988307Sszhou 		printf("dl_bind: DLPI error %u\n", error_ack->dl_errno);
989307Sszhou 		break;
990307Sszhou 
991307Sszhou 	default:
992307Sszhou 		printf("dl_bind: bad ACK header %u\n", dl_prim->dl_primitive);
993307Sszhou 		break;
994307Sszhou 	}
995307Sszhou 
996307Sszhou 	/*
997307Sszhou 	 * Error return only.
998307Sszhou 	 */
999307Sszhou 	freemsg(mp);
1000307Sszhou 	return (-1);
1001307Sszhou }
1002307Sszhou 
1003307Sszhou int
1004307Sszhou dl_phys_addr(ldi_handle_t lh, struct ether_addr *eaddr)
1005307Sszhou {
1006307Sszhou 	dl_phys_addr_req_t *phys_addr_req;
1007307Sszhou 	dl_phys_addr_ack_t *phys_addr_ack;
1008307Sszhou 	dl_error_ack_t *error_ack;
1009307Sszhou 	union DL_primitives *dl_prim;
1010307Sszhou 	mblk_t *mp;
1011307Sszhou 	int error;
1012307Sszhou 	uchar_t *addrp;
1013307Sszhou 	timestruc_t tv;
1014307Sszhou 
1015307Sszhou 	if ((mp = allocb(sizeof (dl_phys_addr_req_t), BPRI_MED)) ==
1016307Sszhou 	    (mblk_t *)NULL) {
1017307Sszhou 		cmn_err(CE_WARN, "dl_phys_addr: allocb failed");
1018307Sszhou 		return (ENOSR);
1019307Sszhou 	}
1020307Sszhou 	mp->b_datap->db_type = M_PROTO;
1021307Sszhou 	mp->b_wptr += sizeof (dl_phys_addr_req_t);
1022307Sszhou 
1023307Sszhou 	phys_addr_req = (dl_phys_addr_req_t *)mp->b_rptr;
1024307Sszhou 	phys_addr_req->dl_primitive = DL_PHYS_ADDR_REQ;
1025307Sszhou 	phys_addr_req->dl_addr_type = DL_CURR_PHYS_ADDR;
1026307Sszhou 
1027307Sszhou 	/*
1028307Sszhou 	 * In case some provider doesn't implement or nack the
1029307Sszhou 	 * request just wait for 15 seconds.
1030307Sszhou 	 */
1031307Sszhou 	tv.tv_sec = 15;
1032307Sszhou 	tv.tv_nsec = 0;
1033307Sszhou 
1034307Sszhou 	(void) ldi_putmsg(lh, mp);
1035307Sszhou 	error = ldi_getmsg(lh, &mp, &tv);
1036307Sszhou 	if (error == ETIME) {
1037307Sszhou 		printf("dl_phys_addr: timed out\n");
1038307Sszhou 		return (-1);
1039307Sszhou 	} else if (error != 0) {
1040307Sszhou 		printf("dl_phys_addr: ldi_getmsg failed: %d\n", error);
1041307Sszhou 		return (error);
1042307Sszhou 	}
1043307Sszhou 
1044307Sszhou 	dl_prim = (union DL_primitives *)mp->b_rptr;
1045307Sszhou 	switch (dl_prim->dl_primitive) {
1046307Sszhou 	case DL_PHYS_ADDR_ACK:
1047307Sszhou 		if ((mp->b_wptr-mp->b_rptr) < sizeof (dl_phys_addr_ack_t)) {
1048307Sszhou 			printf("dl_phys_addr: "
1049307Sszhou 				"DL_PHYS_ADDR_ACK protocol error\n");
1050307Sszhou 			break;
1051307Sszhou 		}
1052307Sszhou 		phys_addr_ack = &dl_prim->physaddr_ack;
1053307Sszhou 		if (phys_addr_ack->dl_addr_length != sizeof (*eaddr)) {
1054307Sszhou 			printf("dl_phys_addr: DL_PHYS_ADDR_ACK bad len %u\n",
1055307Sszhou 				phys_addr_ack->dl_addr_length);
1056307Sszhou 			break;
1057307Sszhou 		}
1058307Sszhou 		if (phys_addr_ack->dl_addr_length +
1059307Sszhou 		    phys_addr_ack->dl_addr_offset > (mp->b_wptr-mp->b_rptr)) {
1060307Sszhou 			printf("dl_phys_addr: DL_PHYS_ADDR_ACK bad len %u\n",
1061307Sszhou 				phys_addr_ack->dl_addr_length);
1062307Sszhou 			break;
1063307Sszhou 		}
1064307Sszhou 		addrp = mp->b_rptr + phys_addr_ack->dl_addr_offset;
1065307Sszhou 		bcopy(addrp, eaddr, sizeof (*eaddr));
1066307Sszhou 		freemsg(mp);
1067307Sszhou 		return (0);
1068307Sszhou 
1069307Sszhou 	case DL_ERROR_ACK:
1070307Sszhou 		if ((mp->b_wptr-mp->b_rptr) < sizeof (dl_error_ack_t)) {
1071307Sszhou 			printf("dl_phys_addr: DL_ERROR_ACK protocol error\n");
1072307Sszhou 			break;
1073307Sszhou 		}
1074307Sszhou 
1075307Sszhou 		error_ack = (dl_error_ack_t *)dl_prim;
1076307Sszhou 		printf("dl_phys_addr: DLPI error %u\n",
1077307Sszhou 		    error_ack->dl_errno);
1078307Sszhou 		break;
1079307Sszhou 
1080307Sszhou 	default:
1081307Sszhou 		printf("dl_phys_addr: bad ACK header %u\n",
1082307Sszhou 			dl_prim->dl_primitive);
1083307Sszhou 		break;
1084307Sszhou 	}
1085307Sszhou 
1086307Sszhou 	/*
1087307Sszhou 	 * Error return only.
1088307Sszhou 	 */
1089307Sszhou 	freemsg(mp);
1090307Sszhou 	return (-1);
1091307Sszhou }
1092