xref: /onnv-gate/usr/src/uts/common/io/strplumb.c (revision 307:a4c0aa8eedcf)
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
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
230Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include	<sys/param.h>
300Sstevel@tonic-gate #include	<sys/types.h>
310Sstevel@tonic-gate #include	<sys/user.h>
320Sstevel@tonic-gate #include	<sys/vfs.h>
330Sstevel@tonic-gate #include	<sys/vnode.h>
340Sstevel@tonic-gate #include	<sys/file.h>
350Sstevel@tonic-gate #include	<sys/stream.h>
360Sstevel@tonic-gate #include	<sys/stropts.h>
370Sstevel@tonic-gate #include	<sys/strsubr.h>
380Sstevel@tonic-gate #include	<sys/dlpi.h>
390Sstevel@tonic-gate #include	<sys/vnode.h>
400Sstevel@tonic-gate #include	<sys/socket.h>
410Sstevel@tonic-gate #include	<sys/sockio.h>
420Sstevel@tonic-gate #include	<net/if.h>
430Sstevel@tonic-gate 
440Sstevel@tonic-gate #include	<sys/cred.h>
450Sstevel@tonic-gate #include	<sys/sysmacros.h>
460Sstevel@tonic-gate 
470Sstevel@tonic-gate #include	<sys/sad.h>
480Sstevel@tonic-gate #include	<sys/kstr.h>
490Sstevel@tonic-gate #include	<sys/bootconf.h>
500Sstevel@tonic-gate #include	<sys/bootprops.h>
510Sstevel@tonic-gate 
520Sstevel@tonic-gate #include	<sys/errno.h>
530Sstevel@tonic-gate #include	<sys/modctl.h>
540Sstevel@tonic-gate #include	<sys/sunddi.h>
550Sstevel@tonic-gate #include	<sys/sunldi.h>
560Sstevel@tonic-gate #include	<sys/esunddi.h>
570Sstevel@tonic-gate #include	<sys/promif.h>
580Sstevel@tonic-gate 
590Sstevel@tonic-gate #include	<netinet/in.h>
600Sstevel@tonic-gate #include	<netinet/ip6.h>
610Sstevel@tonic-gate #include	<netinet/icmp6.h>
620Sstevel@tonic-gate #include	<netinet/sctp.h>
630Sstevel@tonic-gate #include	<inet/common.h>
640Sstevel@tonic-gate #include	<inet/ip.h>
650Sstevel@tonic-gate #include	<inet/ip6.h>
660Sstevel@tonic-gate #include	<inet/tcp.h>
670Sstevel@tonic-gate #include	<inet/sctp_ip.h>
680Sstevel@tonic-gate 
690Sstevel@tonic-gate #include	<sys/strlog.h>
700Sstevel@tonic-gate #include	<sys/log.h>
710Sstevel@tonic-gate #include	<sys/ethernet.h>
720Sstevel@tonic-gate #include	<sys/ddi_implfuncs.h>
730Sstevel@tonic-gate 
740Sstevel@tonic-gate #include	<sys/dld.h>
750Sstevel@tonic-gate 
760Sstevel@tonic-gate /*
770Sstevel@tonic-gate  * Debug Macros
780Sstevel@tonic-gate  */
790Sstevel@tonic-gate int	strplumbdebug = 0;
800Sstevel@tonic-gate 
810Sstevel@tonic-gate #define	DBG0(_f) \
820Sstevel@tonic-gate 	if (strplumbdebug != 0) \
830Sstevel@tonic-gate 		printf("strplumb: " _f)
840Sstevel@tonic-gate 
850Sstevel@tonic-gate #define	DBG1(_f, _a) \
860Sstevel@tonic-gate 	if (strplumbdebug != 0) \
870Sstevel@tonic-gate 		printf("strplumb: " _f, (_a))
880Sstevel@tonic-gate 
890Sstevel@tonic-gate #define	DBG2(_f, _a, _b) \
900Sstevel@tonic-gate 	if (strplumbdebug != 0) \
910Sstevel@tonic-gate 		printf("strplumb: " _f, (_a), (_b))
920Sstevel@tonic-gate 
930Sstevel@tonic-gate #define	DBG3(_f, _a, _b, _c) \
940Sstevel@tonic-gate 	if (strplumbdebug != 0) \
950Sstevel@tonic-gate 		printf("strplumb: " _f, (_a), (_b), (_c))
960Sstevel@tonic-gate 
970Sstevel@tonic-gate /*
980Sstevel@tonic-gate  * Module linkage information for the kernel.
990Sstevel@tonic-gate  */
1000Sstevel@tonic-gate #define	STRPLUMB_IDENT	"STREAMS Plumbing Module v%I%"
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate static struct modlmisc modlmisc = {
1030Sstevel@tonic-gate 	&mod_miscops,
1040Sstevel@tonic-gate 	STRPLUMB_IDENT
1050Sstevel@tonic-gate };
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate static struct modlinkage modlinkage = {
1080Sstevel@tonic-gate 	MODREV_1,
1090Sstevel@tonic-gate 	&modlmisc,
1100Sstevel@tonic-gate 	NULL
1110Sstevel@tonic-gate };
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate int
1140Sstevel@tonic-gate _init(void)
1150Sstevel@tonic-gate {
1160Sstevel@tonic-gate 	return (mod_install(&modlinkage));
1170Sstevel@tonic-gate }
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate int
1200Sstevel@tonic-gate _fini(void)
1210Sstevel@tonic-gate {
1220Sstevel@tonic-gate 	return (mod_remove(&modlinkage));
1230Sstevel@tonic-gate }
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate int
1260Sstevel@tonic-gate _info(struct modinfo *modinfop)
1270Sstevel@tonic-gate {
1280Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
1290Sstevel@tonic-gate }
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate #define	ARP		"arp"
1320Sstevel@tonic-gate #define	TCP		"tcp"
1330Sstevel@tonic-gate #define	TCP6		"tcp6"
1340Sstevel@tonic-gate #define	UDP		"udp"
1350Sstevel@tonic-gate #define	UDP6		"udp6"
1360Sstevel@tonic-gate #define	SCTP		"sctp"
1370Sstevel@tonic-gate #define	SCTP6		"sctp6"
1380Sstevel@tonic-gate #define	ICMP		"icmp"
1390Sstevel@tonic-gate #define	ICMP6		"icmp6"
1400Sstevel@tonic-gate #define	IP		"ip"
1410Sstevel@tonic-gate #define	IP6		"ip6"
1420Sstevel@tonic-gate #define	TIMOD		"timod"
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate #define	UDPDEV		"/devices/pseudo/udp@0:udp"
1450Sstevel@tonic-gate #define	TCP6DEV		"/devices/pseudo/tcp6@0:tcp6"
1460Sstevel@tonic-gate #define	SCTP6DEV	"/devices/pseudo/sctp6@0:sctp6"
1470Sstevel@tonic-gate #define	IP6DEV		"/devices/pseudo/ip6@0:ip6"
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate typedef struct strplumb_modspec {
1500Sstevel@tonic-gate 	char	*sm_type;
1510Sstevel@tonic-gate 	char	*sm_name;
1520Sstevel@tonic-gate } strplumb_modspec_t;
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate static strplumb_modspec_t	strplumb_modlist[] = {
1550Sstevel@tonic-gate 	{ "drv", DLD_DRIVER_NAME },
1560Sstevel@tonic-gate 	{ "drv", IP },
1570Sstevel@tonic-gate 	{ "drv", IP6 },
1580Sstevel@tonic-gate 	{ "drv", TCP },
1590Sstevel@tonic-gate 	{ "drv", TCP6 },
1600Sstevel@tonic-gate 	{ "drv", UDP },
1610Sstevel@tonic-gate 	{ "drv", UDP6 },
1620Sstevel@tonic-gate 	{ "drv", SCTP },
1630Sstevel@tonic-gate 	{ "drv", SCTP6 },
1640Sstevel@tonic-gate 	{ "drv", ICMP },
1650Sstevel@tonic-gate 	{ "drv", ICMP6 },
1660Sstevel@tonic-gate 	{ "drv", ARP },
1670Sstevel@tonic-gate 	{ "strmod", TIMOD }
1680Sstevel@tonic-gate };
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate /*
1710Sstevel@tonic-gate  * Called from swapgeneric.c:loadrootmodules() in the network boot case.
1720Sstevel@tonic-gate  */
1730Sstevel@tonic-gate int
1740Sstevel@tonic-gate strplumb_load(void)
1750Sstevel@tonic-gate {
1760Sstevel@tonic-gate 	uint_t			i;
1770Sstevel@tonic-gate 	strplumb_modspec_t	*p;
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate 	DBG0("loading modules\n");
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 	for (i = 0, p = strplumb_modlist;
1820Sstevel@tonic-gate 	    i < sizeof (strplumb_modlist) / sizeof (strplumb_modlist[0]);
1830Sstevel@tonic-gate 	    i++, p++) {
1840Sstevel@tonic-gate 		if (modloadonly(p->sm_type, p->sm_name) < 0) {
1850Sstevel@tonic-gate 			printf("strplumb: failed to load %s/%s\n",
1860Sstevel@tonic-gate 			    p->sm_type, p->sm_name);
1870Sstevel@tonic-gate 			return (EFAULT);
1880Sstevel@tonic-gate 		}
1890Sstevel@tonic-gate 	}
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 	return (0);
1920Sstevel@tonic-gate }
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate static int
1950Sstevel@tonic-gate strplumb_init(void)
1960Sstevel@tonic-gate {
1970Sstevel@tonic-gate 	uint_t			i;
1980Sstevel@tonic-gate 	strplumb_modspec_t	*p;
1990Sstevel@tonic-gate 	int			err;
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate 	DBG0("initializing modules\n");
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate 	for (i = 0, p = strplumb_modlist;
2040Sstevel@tonic-gate 	    i < sizeof (strplumb_modlist) / sizeof (strplumb_modlist[0]);
2050Sstevel@tonic-gate 	    i++, p++) {
2060Sstevel@tonic-gate 		if (strcmp(p->sm_type, "drv") == 0)
2070Sstevel@tonic-gate 			err = (i_ddi_attach_pseudo_node(p->sm_name) != NULL) ?
2080Sstevel@tonic-gate 			    0 : EFAULT;
2090Sstevel@tonic-gate 		else
2100Sstevel@tonic-gate 			err = (modload(p->sm_type, p->sm_name) < 0) ?
2110Sstevel@tonic-gate 			    EFAULT : 0;
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate 		if (err != 0)  {
2140Sstevel@tonic-gate 			printf("strplumb: failed to initialize %s/%s\n",
2150Sstevel@tonic-gate 			    p->sm_type, p->sm_name);
2160Sstevel@tonic-gate 			return (err);
2170Sstevel@tonic-gate 		}
2180Sstevel@tonic-gate 	}
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate 	return (0);
2210Sstevel@tonic-gate }
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate static int
2240Sstevel@tonic-gate strplumb_autopush(void)
2250Sstevel@tonic-gate {
2260Sstevel@tonic-gate 	major_t		maj;
2270Sstevel@tonic-gate 	minor_t		min;
2280Sstevel@tonic-gate 	char		*mods[5];
2290Sstevel@tonic-gate 	uint_t		anchor = 1;
2300Sstevel@tonic-gate 	int		err;
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 	min = (minor_t)-1;
2330Sstevel@tonic-gate 	mods[1] = NULL;
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate 	/*
2360Sstevel@tonic-gate 	 * UDP
2370Sstevel@tonic-gate 	 */
2380Sstevel@tonic-gate 	DBG0("setting up udp autopush\n");
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 	mods[0] = UDP;
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 	maj = ddi_name_to_major(UDP);
2430Sstevel@tonic-gate 	if ((err = kstr_autopush(SET_AUTOPUSH, &maj, &min, NULL, &anchor,
2440Sstevel@tonic-gate 	    mods)) != 0) {
2450Sstevel@tonic-gate 		printf("strplumb: kstr_autopush(SET/UDP) failed: %d\n", err);
2460Sstevel@tonic-gate 		return (err);
2470Sstevel@tonic-gate 	}
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate 	maj = ddi_name_to_major(UDP6);
2500Sstevel@tonic-gate 	if ((err = kstr_autopush(SET_AUTOPUSH, &maj, &min, NULL, &anchor,
2510Sstevel@tonic-gate 	    mods)) != 0) {
2520Sstevel@tonic-gate 		printf("strplumb: kstr_autopush(SET/UDP6) failed: %d\n", err);
2530Sstevel@tonic-gate 		return (err);
2540Sstevel@tonic-gate 	}
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate 	/*
2570Sstevel@tonic-gate 	 * ICMP
2580Sstevel@tonic-gate 	 */
2590Sstevel@tonic-gate 	DBG0("setting up icmp autopush\n");
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 	mods[0] = ICMP;
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate 	maj = ddi_name_to_major(ICMP);
2640Sstevel@tonic-gate 	if ((err = kstr_autopush(SET_AUTOPUSH, &maj, &min, NULL, NULL,
2650Sstevel@tonic-gate 	    mods)) != 0) {
2660Sstevel@tonic-gate 		printf("strplumb: kstr_autopush(SET/ICMP) failed: %d\n", err);
2670Sstevel@tonic-gate 		return (err);
2680Sstevel@tonic-gate 	}
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate 	maj = ddi_name_to_major(ICMP6);
2710Sstevel@tonic-gate 	if ((err = kstr_autopush(SET_AUTOPUSH, &maj, &min, NULL, NULL,
2720Sstevel@tonic-gate 	    mods)) != 0) {
2730Sstevel@tonic-gate 		printf("strplumb: kstr_autopush(SET/ICMP6) failed: %d\n", err);
2740Sstevel@tonic-gate 		return (err);
2750Sstevel@tonic-gate 	}
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 	/*
2780Sstevel@tonic-gate 	 * ARP
2790Sstevel@tonic-gate 	 */
2800Sstevel@tonic-gate 	DBG0("setting up arp autopush\n");
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate 	mods[0] = ARP;
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate 	maj = ddi_name_to_major(ARP);
2850Sstevel@tonic-gate 	if ((err = kstr_autopush(SET_AUTOPUSH, &maj, &min, NULL, &anchor,
2860Sstevel@tonic-gate 	    mods)) != 0) {
2870Sstevel@tonic-gate 		printf("strplumb: kstr_autopush(SET/ARP) failed: %d\n", err);
2880Sstevel@tonic-gate 		return (err);
2890Sstevel@tonic-gate 	}
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate 	return (0);
2920Sstevel@tonic-gate }
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate static int
2950Sstevel@tonic-gate strplumb_sctpq(ldi_ident_t li)
2960Sstevel@tonic-gate {
2970Sstevel@tonic-gate 	ldi_handle_t	lh = NULL;
2980Sstevel@tonic-gate 	int		err;
2990Sstevel@tonic-gate 	int		rval;
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate 	DBG0("configuring SCTP default queue\n");
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate 	if ((err = ldi_open_by_name(SCTP6DEV, FREAD|FWRITE, CRED(), &lh,
3040Sstevel@tonic-gate 	    li)) != 0) {
3050Sstevel@tonic-gate 		printf("strplumb: open of SCTP6DEV failed: %d\n", err);
3060Sstevel@tonic-gate 		return (err);
3070Sstevel@tonic-gate 	}
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate 	if ((err = ldi_ioctl(lh, SCTP_IOC_DEFAULT_Q, (intptr_t)0, FKIOCTL,
3100Sstevel@tonic-gate 	    CRED(), &rval)) != 0) {
3110Sstevel@tonic-gate 		printf("strplumb: failed to set SCTP default queue: %d\n",
3120Sstevel@tonic-gate 		    err);
3130Sstevel@tonic-gate 		(void) ldi_close(lh, FREAD|FWRITE, CRED());
3140Sstevel@tonic-gate 		return (err);
3150Sstevel@tonic-gate 	}
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate 	return (0);
3180Sstevel@tonic-gate }
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate static int
3210Sstevel@tonic-gate strplumb_tcpq(ldi_ident_t li)
3220Sstevel@tonic-gate {
3230Sstevel@tonic-gate 	ldi_handle_t	lh = NULL;
3240Sstevel@tonic-gate 	ldi_handle_t	ip_lh = NULL;
3250Sstevel@tonic-gate 	int		err;
3260Sstevel@tonic-gate 	int		rval;
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate 	DBG0("configuring TCP default queue\n");
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate 	/*
3310Sstevel@tonic-gate 	 * We open IP6DEV here because we need to have it open to in
3320Sstevel@tonic-gate 	 * order to open TCP6DEV successfully.
3330Sstevel@tonic-gate 	 */
3340Sstevel@tonic-gate 	if ((err = ldi_open_by_name(IP6DEV, FREAD|FWRITE, CRED(), &ip_lh,
3350Sstevel@tonic-gate 	    li)) != 0) {
3360Sstevel@tonic-gate 		printf("strplumb: open of IP6DEV failed: %d\n", err);
3370Sstevel@tonic-gate 		return (err);
3380Sstevel@tonic-gate 	}
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate 	/*
3410Sstevel@tonic-gate 	 * We set the tcp default queue to IPv6 because IPv4 falls back to
3420Sstevel@tonic-gate 	 * IPv6 when it can't find a client, but IPv6 does not fall back to
3430Sstevel@tonic-gate 	 * IPv4.
3440Sstevel@tonic-gate 	 */
3450Sstevel@tonic-gate 	if ((err = ldi_open_by_name(TCP6DEV, FREAD|FWRITE, CRED(), &lh,
3460Sstevel@tonic-gate 	    li)) != 0) {
3470Sstevel@tonic-gate 		printf("strplumb: open of TCP6DEV failed: %d\n", err);
3480Sstevel@tonic-gate 		goto done;
3490Sstevel@tonic-gate 	}
3500Sstevel@tonic-gate 
3510Sstevel@tonic-gate 	if ((err = ldi_ioctl(lh, TCP_IOC_DEFAULT_Q, (intptr_t)0, FKIOCTL,
3520Sstevel@tonic-gate 	    CRED(), &rval)) != 0) {
3530Sstevel@tonic-gate 		printf("strplumb: failed to set TCP default queue: %d\n",
3540Sstevel@tonic-gate 		    err);
3550Sstevel@tonic-gate 		goto done;
3560Sstevel@tonic-gate 	}
3570Sstevel@tonic-gate 
3580Sstevel@tonic-gate done:
3590Sstevel@tonic-gate 	(void) ldi_close(ip_lh, FREAD|FWRITE, CRED());
3600Sstevel@tonic-gate 	return (err);
3610Sstevel@tonic-gate }
3620Sstevel@tonic-gate 
3630Sstevel@tonic-gate /*
3640Sstevel@tonic-gate  * Can be set in /etc/system in the case of local booting. See comment below.
3650Sstevel@tonic-gate  */
3660Sstevel@tonic-gate char	*ndev_name = 0;
3670Sstevel@tonic-gate int	ndev_unit = 0;
3680Sstevel@tonic-gate 
3690Sstevel@tonic-gate /*
3700Sstevel@tonic-gate  * If we booted diskless then strplumb() will have been called from
3710Sstevel@tonic-gate  * swapgeneric.c:rootconf(). All we can do in that case is plumb the
3720Sstevel@tonic-gate  * network device that we booted from.
3730Sstevel@tonic-gate  *
3740Sstevel@tonic-gate  * If we booted from a local disk, we will have been called from main(),
3750Sstevel@tonic-gate  * and normally we defer the plumbing of interfaces until network/physical.
3760Sstevel@tonic-gate  * This can be overridden by setting "ndev_name" in /etc/system.
3770Sstevel@tonic-gate  */
3780Sstevel@tonic-gate static int
379269Sericheng resolve_boot_path(void)
3800Sstevel@tonic-gate {
3810Sstevel@tonic-gate 	char			*devpath = NULL;
3820Sstevel@tonic-gate 	dev_info_t		*dip;
3830Sstevel@tonic-gate 	const char		*driver;
3840Sstevel@tonic-gate 	int			instance;
3850Sstevel@tonic-gate 
3860Sstevel@tonic-gate 	if (strncmp(rootfs.bo_fstype, "nfs", 3) == 0)
3870Sstevel@tonic-gate 		devpath = rootfs.bo_name;
3880Sstevel@tonic-gate #ifndef __sparc
3890Sstevel@tonic-gate 	else
3900Sstevel@tonic-gate 		devpath = strplumb_get_netdev_path();
3910Sstevel@tonic-gate #endif
3920Sstevel@tonic-gate 
3930Sstevel@tonic-gate 	if (devpath != NULL) {
3940Sstevel@tonic-gate 		DBG1("resolving boot-path: %s\n", devpath);
3950Sstevel@tonic-gate 
3960Sstevel@tonic-gate 		/*
3970Sstevel@tonic-gate 		 * Hold the devi since this is the root device.
3980Sstevel@tonic-gate 		 */
3990Sstevel@tonic-gate 		if ((dip = e_ddi_hold_devi_by_path(devpath, 0)) == NULL) {
4000Sstevel@tonic-gate 			printf("strplumb: unable to hold root device: %s\n",
4010Sstevel@tonic-gate 			    devpath);
4020Sstevel@tonic-gate 			return (ENXIO);
4030Sstevel@tonic-gate 		}
4040Sstevel@tonic-gate 
4050Sstevel@tonic-gate 		driver = ddi_driver_name(dip);
4060Sstevel@tonic-gate 		instance = ddi_get_instance(dip);
4070Sstevel@tonic-gate 	} else {
4080Sstevel@tonic-gate 		if (ndev_name == NULL)
4090Sstevel@tonic-gate 			return (ENODEV);
4100Sstevel@tonic-gate 
4110Sstevel@tonic-gate 		DBG2("using ndev_name (%s) ndev_unit (%d)\n", ndev_name,
4120Sstevel@tonic-gate 		    ndev_unit);
4130Sstevel@tonic-gate 
4140Sstevel@tonic-gate 		if (i_ddi_attach_hw_nodes(ndev_name) != DDI_SUCCESS) {
4150Sstevel@tonic-gate 			printf("strplumb: cannot load ndev_name '%s'\n",
4160Sstevel@tonic-gate 			    ndev_name);
4170Sstevel@tonic-gate 			return (ENXIO);
4180Sstevel@tonic-gate 		}
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate 		driver = ndev_name;
4210Sstevel@tonic-gate 		instance = ndev_unit;
4220Sstevel@tonic-gate 	}
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate 	(void) snprintf(rootfs.bo_devname, BO_MAXOBJNAME,
425269Sericheng 	    "/devices/pseudo/clone@0:%s", driver);
4260Sstevel@tonic-gate 	(void) snprintf(rootfs.bo_ifname, BO_MAXOBJNAME, "%s%d",
4270Sstevel@tonic-gate 	    driver, instance);
4280Sstevel@tonic-gate 	rootfs.bo_ppa = instance;
429269Sericheng 	return (0);
4300Sstevel@tonic-gate }
4310Sstevel@tonic-gate 
4320Sstevel@tonic-gate static int
4330Sstevel@tonic-gate getifflags(ldi_handle_t lh, struct lifreq *lifrp)
4340Sstevel@tonic-gate {
4350Sstevel@tonic-gate 	struct strioctl	iocb;
4360Sstevel@tonic-gate 	int		rval;
4370Sstevel@tonic-gate 
4380Sstevel@tonic-gate 	iocb.ic_cmd = SIOCGLIFFLAGS;
4390Sstevel@tonic-gate 	iocb.ic_timout = 15;
4400Sstevel@tonic-gate 	iocb.ic_len = sizeof (struct lifreq);
4410Sstevel@tonic-gate 	iocb.ic_dp = (char *)lifrp;
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate 	return (ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, CRED(), &rval));
4440Sstevel@tonic-gate 
4450Sstevel@tonic-gate }
4460Sstevel@tonic-gate 
4470Sstevel@tonic-gate static int
4480Sstevel@tonic-gate setifname(ldi_handle_t lh, struct lifreq *lifrp)
4490Sstevel@tonic-gate {
4500Sstevel@tonic-gate 	struct strioctl	iocb;
4510Sstevel@tonic-gate 	int		rval;
4520Sstevel@tonic-gate 
4530Sstevel@tonic-gate 	iocb.ic_cmd = SIOCSLIFNAME;
4540Sstevel@tonic-gate 	iocb.ic_timout = 15;
4550Sstevel@tonic-gate 	iocb.ic_len = sizeof (struct lifreq);
4560Sstevel@tonic-gate 	iocb.ic_dp = (char *)lifrp;
4570Sstevel@tonic-gate 
4580Sstevel@tonic-gate 	return (ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, CRED(), &rval));
4590Sstevel@tonic-gate }
4600Sstevel@tonic-gate 
4610Sstevel@tonic-gate static int
4620Sstevel@tonic-gate strplumb_dev(ldi_ident_t li)
4630Sstevel@tonic-gate {
4640Sstevel@tonic-gate 	ldi_handle_t	lh = NULL;
4650Sstevel@tonic-gate 	ldi_handle_t	mux_lh = NULL;
4660Sstevel@tonic-gate 	int		err;
4670Sstevel@tonic-gate 	struct lifreq	lifr;
4680Sstevel@tonic-gate 	struct ifreq	ifr;
4690Sstevel@tonic-gate 	int		rval;
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate 	bzero(&lifr, sizeof (struct lifreq));
4720Sstevel@tonic-gate 	bzero(&ifr, sizeof (ifr));
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate 	/*
4750Sstevel@tonic-gate 	 * Now set up the links. Ultimately, we should have two streams
4760Sstevel@tonic-gate 	 * permanently linked underneath UDP (which is actually IP with UDP
4770Sstevel@tonic-gate 	 * autopushed). One stream consists of the ARP-[ifname] combination,
4780Sstevel@tonic-gate 	 * while the other consists of ARP-IP-[ifname]. The second combination
4790Sstevel@tonic-gate 	 * seems a little weird, but is linked underneath UDP just to keep it
4800Sstevel@tonic-gate 	 * around.
4810Sstevel@tonic-gate 	 *
4820Sstevel@tonic-gate 	 * We pin underneath UDP here to match what is done in ifconfig(1m);
4830Sstevel@tonic-gate 	 * otherwise, ifconfig will be unable to unplumb the stream (the major
4840Sstevel@tonic-gate 	 * number and mux id must both match for a successful I_PUNLINK).
4850Sstevel@tonic-gate 	 *
4860Sstevel@tonic-gate 	 * There are subtleties in the plumbing which make it essential to
4870Sstevel@tonic-gate 	 * follow the logic used in ifconfig(1m) very closely.
4880Sstevel@tonic-gate 	 */
4890Sstevel@tonic-gate 
4900Sstevel@tonic-gate 	/*
4910Sstevel@tonic-gate 	 * Plumb UDP-ARP-IP-<dev>
4920Sstevel@tonic-gate 	 */
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate 	if ((err = ldi_open_by_name(rootfs.bo_devname, FREAD|FWRITE, CRED(),
4950Sstevel@tonic-gate 	    &lh, li)) != 0) {
4960Sstevel@tonic-gate 		printf("strplumb: open %s failed: %d\n", rootfs.bo_devname,
4970Sstevel@tonic-gate 		    err);
4980Sstevel@tonic-gate 		goto done;
4990Sstevel@tonic-gate 	}
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate 
5020Sstevel@tonic-gate 	if ((err = ldi_ioctl(lh, I_PUSH, (intptr_t)IP, FKIOCTL, CRED(),
5030Sstevel@tonic-gate 	    &rval)) != 0) {
5040Sstevel@tonic-gate 		printf("strplumb: push IP failed: %d\n", err);
5050Sstevel@tonic-gate 		goto done;
5060Sstevel@tonic-gate 	}
5070Sstevel@tonic-gate 
5080Sstevel@tonic-gate 	if ((err = getifflags(lh, &lifr)) != 0)
5090Sstevel@tonic-gate 		goto done;
5100Sstevel@tonic-gate 
5110Sstevel@tonic-gate 	lifr.lifr_flags |= IFF_IPV4;
5120Sstevel@tonic-gate 	lifr.lifr_flags &= ~IFF_IPV6;
5130Sstevel@tonic-gate 
5140Sstevel@tonic-gate 	if ((err = ldi_ioctl(lh, I_PUSH, (intptr_t)ARP, FKIOCTL, CRED(),
5150Sstevel@tonic-gate 	    &rval)) != 0) {
5160Sstevel@tonic-gate 		printf("strplumb: push ARP failed: %d\n", err);
5170Sstevel@tonic-gate 		goto done;
5180Sstevel@tonic-gate 	}
5190Sstevel@tonic-gate 
5200Sstevel@tonic-gate 	(void) strlcpy(lifr.lifr_name, rootfs.bo_ifname,
5210Sstevel@tonic-gate 	    sizeof (lifr.lifr_name));
5220Sstevel@tonic-gate 	lifr.lifr_ppa = rootfs.bo_ppa;
5230Sstevel@tonic-gate 
5240Sstevel@tonic-gate 	if ((err = setifname(lh, &lifr)) != 0)
5250Sstevel@tonic-gate 		goto done;
5260Sstevel@tonic-gate 
5270Sstevel@tonic-gate 	/* Get the flags and check if ARP is needed */
5280Sstevel@tonic-gate 	if ((err = getifflags(lh, &lifr)) != 0) {
5290Sstevel@tonic-gate 		printf("strplumb: getifflags %s IP failed, error %d\n",
5300Sstevel@tonic-gate 		    lifr.lifr_name, err);
5310Sstevel@tonic-gate 		goto done;
5320Sstevel@tonic-gate 	}
5330Sstevel@tonic-gate 
5340Sstevel@tonic-gate 	/* Pop out ARP if not needed */
5350Sstevel@tonic-gate 	if (lifr.lifr_flags & IFF_NOARP) {
5360Sstevel@tonic-gate 		err = ldi_ioctl(lh, I_POP, (intptr_t)0, FKIOCTL, CRED(),
5370Sstevel@tonic-gate 		    &rval);
5380Sstevel@tonic-gate 		if (err != 0) {
5390Sstevel@tonic-gate 			printf("strplumb: pop ARP failed, error %d\n", err);
5400Sstevel@tonic-gate 			goto done;
5410Sstevel@tonic-gate 		}
5420Sstevel@tonic-gate 	}
5430Sstevel@tonic-gate 
5440Sstevel@tonic-gate 	if ((err = ldi_open_by_name(UDPDEV, FREAD|FWRITE, CRED(), &mux_lh,
5450Sstevel@tonic-gate 	    li)) != 0) {
5460Sstevel@tonic-gate 		printf("strplumb: open of UDPDEV failed: %d\n", err);
5470Sstevel@tonic-gate 		goto done;
5480Sstevel@tonic-gate 	}
5490Sstevel@tonic-gate 
5500Sstevel@tonic-gate 	if ((err = ldi_ioctl(mux_lh, I_PLINK, (intptr_t)lh,
5510Sstevel@tonic-gate 	    FREAD|FWRITE|FNOCTTY|FKIOCTL, CRED(),
5520Sstevel@tonic-gate 	    &(ifr.ifr_ip_muxid))) != 0) {
5530Sstevel@tonic-gate 		printf("strplumb: plink UDP-ARP-IP-%s failed: %d\n",
5540Sstevel@tonic-gate 		    rootfs.bo_ifname, err);
5550Sstevel@tonic-gate 		goto done;
5560Sstevel@tonic-gate 	}
5570Sstevel@tonic-gate 
5580Sstevel@tonic-gate 	DBG2("UDP-ARP-IP-%s muxid: %d\n", rootfs.bo_ifname, ifr.ifr_ip_muxid);
5590Sstevel@tonic-gate 
5600Sstevel@tonic-gate 	(void) ldi_close(lh, FREAD|FWRITE, CRED());
5610Sstevel@tonic-gate 	lh = NULL;
5620Sstevel@tonic-gate 
5630Sstevel@tonic-gate 	/*
5640Sstevel@tonic-gate 	 * Plumb UDP-ARP-<dev>
5650Sstevel@tonic-gate 	 */
5660Sstevel@tonic-gate 
5670Sstevel@tonic-gate 	if ((err = ldi_open_by_name(rootfs.bo_devname, FREAD|FWRITE, CRED(),
5680Sstevel@tonic-gate 	    &lh, li)) != 0) {
5690Sstevel@tonic-gate 		printf("strplumb: open %s failed: %d\n", rootfs.bo_devname,
5700Sstevel@tonic-gate 		    err);
5710Sstevel@tonic-gate 		goto done;
5720Sstevel@tonic-gate 	}
5730Sstevel@tonic-gate 
5740Sstevel@tonic-gate 	if ((err = ldi_ioctl(lh, I_PUSH, (intptr_t)ARP, FKIOCTL, CRED(),
5750Sstevel@tonic-gate 	    &rval)) != 0) {
5760Sstevel@tonic-gate 		printf("strplumb: push ARP failed: %d\n", err);
5770Sstevel@tonic-gate 		goto done;
5780Sstevel@tonic-gate 	}
5790Sstevel@tonic-gate 
5800Sstevel@tonic-gate 	if ((err = setifname(lh, &lifr)) != 0)
5810Sstevel@tonic-gate 		goto done;
5820Sstevel@tonic-gate 
5830Sstevel@tonic-gate 	if ((err = ldi_ioctl(mux_lh, I_PLINK, (intptr_t)lh,
5840Sstevel@tonic-gate 	    FREAD|FWRITE|FNOCTTY|FKIOCTL, CRED(),
5850Sstevel@tonic-gate 	    &(ifr.ifr_arp_muxid))) != 0) {
5860Sstevel@tonic-gate 		printf("strplumb: plink UDP-ARP-%s failed: %d\n",
5870Sstevel@tonic-gate 		    rootfs.bo_ifname, err);
5880Sstevel@tonic-gate 		goto done;
5890Sstevel@tonic-gate 	}
5900Sstevel@tonic-gate 
5910Sstevel@tonic-gate 	DBG2("UDP-ARP-%s muxid: %d\n", rootfs.bo_ifname, ifr.ifr_arp_muxid);
5920Sstevel@tonic-gate 
5930Sstevel@tonic-gate 	/*
5940Sstevel@tonic-gate 	 * Cache the mux ids.
5950Sstevel@tonic-gate 	 */
5960Sstevel@tonic-gate 	(void) strlcpy(ifr.ifr_name, rootfs.bo_ifname, sizeof (ifr.ifr_name));
5970Sstevel@tonic-gate 
5980Sstevel@tonic-gate 	if ((err = ldi_ioctl(mux_lh, SIOCSIFMUXID, (intptr_t)&ifr, FKIOCTL,
5990Sstevel@tonic-gate 	    CRED(), &rval)) != 0) {
6000Sstevel@tonic-gate 		printf("strplumb: SIOCSIFMUXID failed: %d\n", err);
6010Sstevel@tonic-gate 		goto done;
6020Sstevel@tonic-gate 	}
6030Sstevel@tonic-gate 
6040Sstevel@tonic-gate done:
6050Sstevel@tonic-gate 	if (lh != NULL)
6060Sstevel@tonic-gate 		(void) ldi_close(lh, FREAD|FWRITE, CRED());
6070Sstevel@tonic-gate 
6080Sstevel@tonic-gate 	if (mux_lh != NULL)
6090Sstevel@tonic-gate 		(void) ldi_close(mux_lh, FREAD|FWRITE, CRED());
6100Sstevel@tonic-gate 
6110Sstevel@tonic-gate 	return (err);
6120Sstevel@tonic-gate }
6130Sstevel@tonic-gate 
6140Sstevel@tonic-gate /*
6150Sstevel@tonic-gate  * Do streams plumbing for internet protocols.
6160Sstevel@tonic-gate  */
6170Sstevel@tonic-gate int
6180Sstevel@tonic-gate strplumb(void)
6190Sstevel@tonic-gate {
6200Sstevel@tonic-gate 	ldi_ident_t	li;
6210Sstevel@tonic-gate 	int		err;
6220Sstevel@tonic-gate 
6230Sstevel@tonic-gate 	if ((err = strplumb_init()) != 0)
6240Sstevel@tonic-gate 		return (err);
6250Sstevel@tonic-gate 
6260Sstevel@tonic-gate 	if ((err = strplumb_autopush()) != 0)
6270Sstevel@tonic-gate 		return (err);
6280Sstevel@tonic-gate 
6290Sstevel@tonic-gate 	if ((err = ldi_ident_from_mod(&modlinkage, &li)) != 0)
6300Sstevel@tonic-gate 		return (err);
6310Sstevel@tonic-gate 
6320Sstevel@tonic-gate 	if ((err = strplumb_sctpq(li)) != 0)
6330Sstevel@tonic-gate 		goto done;
6340Sstevel@tonic-gate 
6350Sstevel@tonic-gate 	if ((err = strplumb_tcpq(li)) != 0)
6360Sstevel@tonic-gate 		goto done;
6370Sstevel@tonic-gate 
638269Sericheng 	if ((err = resolve_boot_path()) != 0)
6390Sstevel@tonic-gate 		goto done;
6400Sstevel@tonic-gate 
6410Sstevel@tonic-gate 	DBG1("rootfs.bo_devname: %s\n", rootfs.bo_devname);
6420Sstevel@tonic-gate 	DBG1("rootfs.bo_ifname: %s\n", rootfs.bo_ifname);
6430Sstevel@tonic-gate 	DBG1("rootfs.bo_ppa: %d\n", rootfs.bo_ppa);
6440Sstevel@tonic-gate 
6450Sstevel@tonic-gate 	if ((err = strplumb_dev(li)) != 0)
6460Sstevel@tonic-gate 		goto done;
6470Sstevel@tonic-gate 
6480Sstevel@tonic-gate done:
6490Sstevel@tonic-gate 	ldi_ident_release(li);
6500Sstevel@tonic-gate 
6510Sstevel@tonic-gate 	return (err);
6520Sstevel@tonic-gate }
6530Sstevel@tonic-gate 
6540Sstevel@tonic-gate /* multiboot:  diskless boot interface discovery */
6550Sstevel@tonic-gate 
6560Sstevel@tonic-gate #ifndef	__sparc
6570Sstevel@tonic-gate 
6580Sstevel@tonic-gate static uchar_t boot_macaddr[16];
6590Sstevel@tonic-gate static int boot_maclen;
6600Sstevel@tonic-gate static uchar_t *getmacaddr(dev_info_t *dip, int *maclen);
6610Sstevel@tonic-gate static int matchmac(dev_info_t *dip, void *arg);
662*307Sszhou int dl_attach(ldi_handle_t lh, int unit);
663*307Sszhou int dl_bind(ldi_handle_t lh, uint_t sap, uint_t max_conn,
6640Sstevel@tonic-gate     uint_t service, uint_t conn_mgmt);
665*307Sszhou int dl_phys_addr(ldi_handle_t lh, struct ether_addr *eaddr);
6660Sstevel@tonic-gate 
6670Sstevel@tonic-gate char *
6680Sstevel@tonic-gate strplumb_get_netdev_path(void)
6690Sstevel@tonic-gate {
6700Sstevel@tonic-gate 	char *macstr, *devpath = NULL;
6710Sstevel@tonic-gate 	uchar_t *bootp;
6720Sstevel@tonic-gate 	uint_t bootp_len, len;
6730Sstevel@tonic-gate 
6740Sstevel@tonic-gate 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
6750Sstevel@tonic-gate 	    DDI_PROP_DONTPASS, BP_BOOT_MAC, &macstr) == DDI_SUCCESS) {
6760Sstevel@tonic-gate 		/*
6770Sstevel@tonic-gate 		 * hard coded ether mac len for booting floppy on
6780Sstevel@tonic-gate 		 * machines with old cards
6790Sstevel@tonic-gate 		 */
6800Sstevel@tonic-gate 		boot_maclen = ether_aton(macstr, boot_macaddr);
6810Sstevel@tonic-gate 		if (boot_maclen != 6) {
6820Sstevel@tonic-gate 			cmn_err(CE_WARN,
6830Sstevel@tonic-gate 			    "malformed boot_mac property, %d bytes",
6840Sstevel@tonic-gate 			    boot_maclen);
6850Sstevel@tonic-gate 		}
6860Sstevel@tonic-gate 		ddi_prop_free(macstr);
6870Sstevel@tonic-gate 	} else if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, ddi_root_node(),
6880Sstevel@tonic-gate 	    DDI_PROP_DONTPASS, BP_BOOTP_RESPONSE, &bootp, &bootp_len)
6890Sstevel@tonic-gate 	    == DDI_SUCCESS) {
6900Sstevel@tonic-gate 
6910Sstevel@tonic-gate 		/*
6920Sstevel@tonic-gate 		 * These offsets are defined by dhcp standard
6930Sstevel@tonic-gate 		 * Should use structure offsets
6940Sstevel@tonic-gate 		 */
6950Sstevel@tonic-gate 		boot_maclen = *(bootp + 2);
6960Sstevel@tonic-gate 		ASSERT(boot_maclen <= 16);
6970Sstevel@tonic-gate 		(void) bcopy(bootp + 28, boot_macaddr, boot_maclen);
6980Sstevel@tonic-gate 
6990Sstevel@tonic-gate 		/* encode to ascii string to match what sparc OBP exports */
700129Ssetje 		dhcack = kmem_zalloc(bootp_len * 2 + IFNAMSIZ + 2, KM_SLEEP);
7010Sstevel@tonic-gate 		(void) octet_to_hexascii(bootp, bootp_len, dhcack + IFNAMSIZ,
7020Sstevel@tonic-gate 		    &len);
7030Sstevel@tonic-gate 		ASSERT(len < bootp_len * 2 + 2);
7040Sstevel@tonic-gate 		ddi_prop_free(bootp);
7050Sstevel@tonic-gate 	} else
7060Sstevel@tonic-gate 		return (NULL);
7070Sstevel@tonic-gate 
7080Sstevel@tonic-gate 	ddi_walk_devs(ddi_root_node(), matchmac, (void *)&devpath);
7090Sstevel@tonic-gate 	return (devpath);
7100Sstevel@tonic-gate }
7110Sstevel@tonic-gate 
7120Sstevel@tonic-gate /*
7130Sstevel@tonic-gate  * Get boot path from the boot_mac address
7140Sstevel@tonic-gate  */
7150Sstevel@tonic-gate /*ARGSUSED*/
7160Sstevel@tonic-gate static int
7170Sstevel@tonic-gate matchmac(dev_info_t *dip, void *arg)
7180Sstevel@tonic-gate {
7190Sstevel@tonic-gate 	char **devpathp = (char **)arg;
7200Sstevel@tonic-gate 	char *model_str;
7210Sstevel@tonic-gate 	uchar_t *macaddr;
7220Sstevel@tonic-gate 	int maclen;
7230Sstevel@tonic-gate 
7240Sstevel@tonic-gate 	/* XXX Should use "device-type" per IEEE 1275 */
7250Sstevel@tonic-gate 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, dip, 0,
7260Sstevel@tonic-gate 	    "model", &model_str) != DDI_SUCCESS)
7270Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
7280Sstevel@tonic-gate 
7290Sstevel@tonic-gate 	if (strcmp(model_str, "Ethernet controller") != 0) {
7300Sstevel@tonic-gate 		ddi_prop_free(model_str);
7310Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
7320Sstevel@tonic-gate 	}
7330Sstevel@tonic-gate 	ddi_prop_free(model_str);
7340Sstevel@tonic-gate 
7350Sstevel@tonic-gate 	/* We have a network device now */
7360Sstevel@tonic-gate 	if (i_ddi_attach_node_hierarchy(dip) != DDI_SUCCESS) {
7370Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
7380Sstevel@tonic-gate 	}
7390Sstevel@tonic-gate 
7400Sstevel@tonic-gate 	ASSERT(boot_maclen != 0);
7410Sstevel@tonic-gate 	macaddr = getmacaddr(dip, &maclen);
7420Sstevel@tonic-gate 	if (macaddr == NULL)
7430Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
7440Sstevel@tonic-gate 
7450Sstevel@tonic-gate 	if (maclen != boot_maclen ||
7460Sstevel@tonic-gate 	    bcmp(macaddr, boot_macaddr, maclen) != 0) {
7470Sstevel@tonic-gate 		kmem_free(macaddr, maclen);
7480Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
7490Sstevel@tonic-gate 	}
7500Sstevel@tonic-gate 
7510Sstevel@tonic-gate 	/* found hardware with the mac address */
7520Sstevel@tonic-gate 	(void) localetheraddr((struct ether_addr *)macaddr, NULL);
7530Sstevel@tonic-gate 	kmem_free(macaddr, maclen);
7540Sstevel@tonic-gate 
7550Sstevel@tonic-gate 	*devpathp = kmem_alloc(MAXPATHLEN, KM_SLEEP);
7560Sstevel@tonic-gate 	(void) ddi_pathname(dip, *devpathp);
7570Sstevel@tonic-gate 
7580Sstevel@tonic-gate 	/* fill in the name portion of dhcack */
7590Sstevel@tonic-gate 	if (dhcack)
7600Sstevel@tonic-gate 		(void) snprintf(dhcack, IFNAMSIZ, "%s%d",
7610Sstevel@tonic-gate 		    ddi_driver_name(dip), i_ddi_devi_get_ppa(dip));
7620Sstevel@tonic-gate 	return (DDI_WALK_TERMINATE);
7630Sstevel@tonic-gate }
7640Sstevel@tonic-gate 
7650Sstevel@tonic-gate static uchar_t *
7660Sstevel@tonic-gate getmacaddr_gldv3(char *drv, int inst, int *maclenp)
7670Sstevel@tonic-gate {
7680Sstevel@tonic-gate 	char ifname[16];
7690Sstevel@tonic-gate 	mac_handle_t mh;
7700Sstevel@tonic-gate 	uchar_t *macaddr;
7710Sstevel@tonic-gate 
7720Sstevel@tonic-gate 	(void) snprintf(ifname, sizeof (ifname), "%s%d", drv, inst);
7730Sstevel@tonic-gate 	if (mac_open(ifname, 0, &mh) < 0) {
7740Sstevel@tonic-gate 		return (NULL);
7750Sstevel@tonic-gate 	}
7760Sstevel@tonic-gate 	*maclenp = sizeof (struct ether_addr);
7770Sstevel@tonic-gate 	macaddr = kmem_alloc(*maclenp, KM_SLEEP);
7780Sstevel@tonic-gate 	mac_unicst_get(mh, macaddr);
7790Sstevel@tonic-gate 	mac_close(mh);
7800Sstevel@tonic-gate 
7810Sstevel@tonic-gate 	return (macaddr);
7820Sstevel@tonic-gate }
7830Sstevel@tonic-gate 
7840Sstevel@tonic-gate static uchar_t *
7850Sstevel@tonic-gate getmacaddr(dev_info_t *dip, int *maclenp)
7860Sstevel@tonic-gate {
7870Sstevel@tonic-gate 	int rc, ppa;
7880Sstevel@tonic-gate 	ldi_ident_t li;
7890Sstevel@tonic-gate 	ldi_handle_t lh;
7900Sstevel@tonic-gate 	char *drv_name = (char *)ddi_driver_name(dip);
7910Sstevel@tonic-gate 	char *clonepath;
7920Sstevel@tonic-gate 	uchar_t *macaddr = NULL;
7930Sstevel@tonic-gate 
7940Sstevel@tonic-gate 	/* a simpler way to get mac address for GLDv3 drivers */
7950Sstevel@tonic-gate 	if (GLDV3_DRV(ddi_name_to_major(drv_name))) {
7960Sstevel@tonic-gate 		return (getmacaddr_gldv3(drv_name, ddi_get_instance(dip),
7970Sstevel@tonic-gate 		    maclenp));
7980Sstevel@tonic-gate 	}
7990Sstevel@tonic-gate 
8000Sstevel@tonic-gate 	if (rc = ldi_ident_from_mod(&modlinkage, &li)) {
8010Sstevel@tonic-gate 		cmn_err(CE_WARN,
8020Sstevel@tonic-gate 		    "getmacaddr: ldi_ident_from_mod failed: %d\n", rc);
8030Sstevel@tonic-gate 		return (NULL);
8040Sstevel@tonic-gate 	}
8050Sstevel@tonic-gate 
8060Sstevel@tonic-gate 	clonepath = kmem_alloc(MAXPATHLEN, KM_SLEEP);
8070Sstevel@tonic-gate 	(void) snprintf(clonepath, MAXPATHLEN,
8080Sstevel@tonic-gate 	    "/devices/pseudo/clone@0:%s", drv_name);
8090Sstevel@tonic-gate 
8100Sstevel@tonic-gate 	rc = ldi_open_by_name(clonepath, FREAD|FWRITE, CRED(), &lh, li);
8110Sstevel@tonic-gate 	ldi_ident_release(li);
8120Sstevel@tonic-gate 	if (rc) {
8130Sstevel@tonic-gate 		cmn_err(CE_WARN,
8140Sstevel@tonic-gate 		    "getmacaddr: ldi_open_by_name(%s) failed: %d\n",
8150Sstevel@tonic-gate 		    clonepath, rc);
8160Sstevel@tonic-gate 		kmem_free(clonepath, MAXPATHLEN);
8170Sstevel@tonic-gate 		return (NULL);
8180Sstevel@tonic-gate 	}
8190Sstevel@tonic-gate 	kmem_free(clonepath, MAXPATHLEN);
8200Sstevel@tonic-gate 
8210Sstevel@tonic-gate 	ppa = i_ddi_devi_get_ppa(dip);
8220Sstevel@tonic-gate 	if ((dl_attach(lh, ppa) != 0) ||
8230Sstevel@tonic-gate 	    (dl_bind(lh, ETHERTYPE_IP, 0, DL_CLDLS, 0) != 0)) {
8240Sstevel@tonic-gate 		(void) ldi_close(lh, FREAD|FWRITE, CRED());
8250Sstevel@tonic-gate 		cmn_err(CE_WARN,
8260Sstevel@tonic-gate 		    "getmacaddr: dl_attach/bind(%s%d) failed: %d\n",
8270Sstevel@tonic-gate 		    drv_name, ppa, rc);
8280Sstevel@tonic-gate 		return (NULL);
8290Sstevel@tonic-gate 	}
8300Sstevel@tonic-gate 	*maclenp = sizeof (struct ether_addr);
8310Sstevel@tonic-gate 	macaddr = kmem_alloc(*maclenp, KM_SLEEP);
8320Sstevel@tonic-gate 	if (dl_phys_addr(lh, (struct ether_addr *)macaddr) != 0) {
8330Sstevel@tonic-gate 		kmem_free(macaddr, *maclenp);
8340Sstevel@tonic-gate 		macaddr = NULL;
8350Sstevel@tonic-gate 		*maclenp = 0;
8360Sstevel@tonic-gate 		cmn_err(CE_WARN,
8370Sstevel@tonic-gate 		    "getmacaddr: dl_macaddr(%s%d) failed: %d\n",
8380Sstevel@tonic-gate 		    drv_name, ppa, rc);
8390Sstevel@tonic-gate 	}
8400Sstevel@tonic-gate 	(void) ldi_close(lh, FREAD|FWRITE, CRED());
8410Sstevel@tonic-gate 	return (macaddr);
8420Sstevel@tonic-gate }
8430Sstevel@tonic-gate 
8440Sstevel@tonic-gate #endif	/* !__sparc */
845*307Sszhou 
846*307Sszhou int
847*307Sszhou dl_attach(ldi_handle_t lh, int unit)
848*307Sszhou {
849*307Sszhou 	dl_attach_req_t *attach_req;
850*307Sszhou 	dl_error_ack_t *error_ack;
851*307Sszhou 	union DL_primitives *dl_prim;
852*307Sszhou 	mblk_t *mp;
853*307Sszhou 	int error;
854*307Sszhou 
855*307Sszhou 	if ((mp = allocb(sizeof (dl_attach_req_t), BPRI_MED)) == NULL) {
856*307Sszhou 		cmn_err(CE_WARN, "dl_attach: allocb failed");
857*307Sszhou 		return (ENOSR);
858*307Sszhou 	}
859*307Sszhou 	mp->b_datap->db_type = M_PROTO;
860*307Sszhou 	mp->b_wptr += sizeof (dl_attach_req_t);
861*307Sszhou 
862*307Sszhou 	attach_req = (dl_attach_req_t *)mp->b_rptr;
863*307Sszhou 	attach_req->dl_primitive = DL_ATTACH_REQ;
864*307Sszhou 	attach_req->dl_ppa = unit;
865*307Sszhou 
866*307Sszhou 	(void) ldi_putmsg(lh, mp);
867*307Sszhou 	if ((error = ldi_getmsg(lh, &mp, (timestruc_t *)NULL)) != 0) {
868*307Sszhou 		printf("dl_attach: ldi_getmsg failed: %d\n", error);
869*307Sszhou 		return (error);
870*307Sszhou 	}
871*307Sszhou 
872*307Sszhou 	dl_prim = (union DL_primitives *)mp->b_rptr;
873*307Sszhou 	switch (dl_prim->dl_primitive) {
874*307Sszhou 	case DL_OK_ACK:
875*307Sszhou 		if ((mp->b_wptr-mp->b_rptr) < sizeof (dl_ok_ack_t)) {
876*307Sszhou 			printf("dl_attach: DL_OK_ACK protocol error\n");
877*307Sszhou 			break;
878*307Sszhou 		}
879*307Sszhou 		if (((dl_ok_ack_t *)dl_prim)->dl_correct_primitive !=
880*307Sszhou 		    DL_ATTACH_REQ) {
881*307Sszhou 			printf("dl_attach: DL_OK_ACK rtnd prim %u\n",
882*307Sszhou 			    ((dl_ok_ack_t *)dl_prim)->dl_correct_primitive);
883*307Sszhou 			break;
884*307Sszhou 		}
885*307Sszhou 		freemsg(mp);
886*307Sszhou 		return (0);
887*307Sszhou 
888*307Sszhou 	case DL_ERROR_ACK:
889*307Sszhou 		if ((mp->b_wptr-mp->b_rptr) < sizeof (dl_error_ack_t)) {
890*307Sszhou 			printf("dl_attach: DL_ERROR_ACK protocol error\n");
891*307Sszhou 			break;
892*307Sszhou 		}
893*307Sszhou 
894*307Sszhou 		error_ack = (dl_error_ack_t *)dl_prim;
895*307Sszhou 		switch (error_ack->dl_errno) {
896*307Sszhou 		case DL_BADPPA:
897*307Sszhou 			printf("dl_attach: DL_ERROR_ACK bad PPA\n");
898*307Sszhou 			break;
899*307Sszhou 
900*307Sszhou 		case DL_ACCESS:
901*307Sszhou 			printf("dl_attach: DL_ERROR_ACK access error\n");
902*307Sszhou 			break;
903*307Sszhou 
904*307Sszhou 		default:
905*307Sszhou 			printf("dl_attach: DLPI error %u\n",
906*307Sszhou 			    error_ack->dl_errno);
907*307Sszhou 			break;
908*307Sszhou 		}
909*307Sszhou 		break;
910*307Sszhou 
911*307Sszhou 	default:
912*307Sszhou 		printf("dl_attach: bad ACK header %u\n", dl_prim->dl_primitive);
913*307Sszhou 		break;
914*307Sszhou 	}
915*307Sszhou 
916*307Sszhou 	/*
917*307Sszhou 	 * Error return only.
918*307Sszhou 	 */
919*307Sszhou 	freemsg(mp);
920*307Sszhou 	return (-1);
921*307Sszhou }
922*307Sszhou 
923*307Sszhou int
924*307Sszhou dl_bind(ldi_handle_t lh, uint_t sap, uint_t max_conn, uint_t service,
925*307Sszhou 	uint_t conn_mgmt)
926*307Sszhou {
927*307Sszhou 	dl_bind_req_t *bind_req;
928*307Sszhou 	dl_error_ack_t *error_ack;
929*307Sszhou 	union DL_primitives *dl_prim;
930*307Sszhou 	mblk_t *mp;
931*307Sszhou 	int error;
932*307Sszhou 
933*307Sszhou 	if ((mp = allocb(sizeof (dl_bind_req_t), BPRI_MED)) == NULL) {
934*307Sszhou 		cmn_err(CE_WARN, "dl_bind: allocb failed");
935*307Sszhou 		return (ENOSR);
936*307Sszhou 	}
937*307Sszhou 	mp->b_datap->db_type = M_PROTO;
938*307Sszhou 
939*307Sszhou 	bind_req = (dl_bind_req_t *)mp->b_wptr;
940*307Sszhou 	mp->b_wptr += sizeof (dl_bind_req_t);
941*307Sszhou 	bind_req->dl_primitive = DL_BIND_REQ;
942*307Sszhou 	bind_req->dl_sap = sap;
943*307Sszhou 	bind_req->dl_max_conind = max_conn;
944*307Sszhou 	bind_req->dl_service_mode = service;
945*307Sszhou 	bind_req->dl_conn_mgmt = conn_mgmt;
946*307Sszhou 	bind_req->dl_xidtest_flg = 0;
947*307Sszhou 
948*307Sszhou 	(void) ldi_putmsg(lh, mp);
949*307Sszhou 	if ((error = ldi_getmsg(lh, &mp, (timestruc_t *)NULL)) != 0) {
950*307Sszhou 		printf("dl_bind: ldi_getmsg failed: %d\n", error);
951*307Sszhou 		return (error);
952*307Sszhou 	}
953*307Sszhou 
954*307Sszhou 	dl_prim = (union DL_primitives *)mp->b_rptr;
955*307Sszhou 	switch (dl_prim->dl_primitive) {
956*307Sszhou 	case DL_BIND_ACK:
957*307Sszhou 		if ((mp->b_wptr-mp->b_rptr) < sizeof (dl_bind_ack_t)) {
958*307Sszhou 			printf("dl_bind: DL_BIND_ACK protocol error\n");
959*307Sszhou 			break;
960*307Sszhou 		}
961*307Sszhou 		if (((dl_bind_ack_t *)dl_prim)->dl_sap != sap) {
962*307Sszhou 			printf("dl_bind: DL_BIND_ACK bad sap %u\n",
963*307Sszhou 			    ((dl_bind_ack_t *)dl_prim)->dl_sap);
964*307Sszhou 			break;
965*307Sszhou 		}
966*307Sszhou 		freemsg(mp);
967*307Sszhou 		return (0);
968*307Sszhou 
969*307Sszhou 	case DL_ERROR_ACK:
970*307Sszhou 		if ((mp->b_wptr-mp->b_rptr) < sizeof (dl_error_ack_t)) {
971*307Sszhou 			printf("dl_bind: DL_ERROR_ACK protocol error\n");
972*307Sszhou 			break;
973*307Sszhou 		}
974*307Sszhou 
975*307Sszhou 		error_ack = (dl_error_ack_t *)dl_prim;
976*307Sszhou 		printf("dl_bind: DLPI error %u\n", error_ack->dl_errno);
977*307Sszhou 		break;
978*307Sszhou 
979*307Sszhou 	default:
980*307Sszhou 		printf("dl_bind: bad ACK header %u\n", dl_prim->dl_primitive);
981*307Sszhou 		break;
982*307Sszhou 	}
983*307Sszhou 
984*307Sszhou 	/*
985*307Sszhou 	 * Error return only.
986*307Sszhou 	 */
987*307Sszhou 	freemsg(mp);
988*307Sszhou 	return (-1);
989*307Sszhou }
990*307Sszhou 
991*307Sszhou int
992*307Sszhou dl_phys_addr(ldi_handle_t lh, struct ether_addr *eaddr)
993*307Sszhou {
994*307Sszhou 	dl_phys_addr_req_t *phys_addr_req;
995*307Sszhou 	dl_phys_addr_ack_t *phys_addr_ack;
996*307Sszhou 	dl_error_ack_t *error_ack;
997*307Sszhou 	union DL_primitives *dl_prim;
998*307Sszhou 	mblk_t *mp;
999*307Sszhou 	int error;
1000*307Sszhou 	uchar_t *addrp;
1001*307Sszhou 	timestruc_t tv;
1002*307Sszhou 
1003*307Sszhou 	if ((mp = allocb(sizeof (dl_phys_addr_req_t), BPRI_MED)) ==
1004*307Sszhou 	    (mblk_t *)NULL) {
1005*307Sszhou 		cmn_err(CE_WARN, "dl_phys_addr: allocb failed");
1006*307Sszhou 		return (ENOSR);
1007*307Sszhou 	}
1008*307Sszhou 	mp->b_datap->db_type = M_PROTO;
1009*307Sszhou 	mp->b_wptr += sizeof (dl_phys_addr_req_t);
1010*307Sszhou 
1011*307Sszhou 	phys_addr_req = (dl_phys_addr_req_t *)mp->b_rptr;
1012*307Sszhou 	phys_addr_req->dl_primitive = DL_PHYS_ADDR_REQ;
1013*307Sszhou 	phys_addr_req->dl_addr_type = DL_CURR_PHYS_ADDR;
1014*307Sszhou 
1015*307Sszhou 	/*
1016*307Sszhou 	 * In case some provider doesn't implement or nack the
1017*307Sszhou 	 * request just wait for 15 seconds.
1018*307Sszhou 	 */
1019*307Sszhou 	tv.tv_sec = 15;
1020*307Sszhou 	tv.tv_nsec = 0;
1021*307Sszhou 
1022*307Sszhou 	(void) ldi_putmsg(lh, mp);
1023*307Sszhou 	error = ldi_getmsg(lh, &mp, &tv);
1024*307Sszhou 	if (error == ETIME) {
1025*307Sszhou 		printf("dl_phys_addr: timed out\n");
1026*307Sszhou 		return (-1);
1027*307Sszhou 	} else if (error != 0) {
1028*307Sszhou 		printf("dl_phys_addr: ldi_getmsg failed: %d\n", error);
1029*307Sszhou 		return (error);
1030*307Sszhou 	}
1031*307Sszhou 
1032*307Sszhou 	dl_prim = (union DL_primitives *)mp->b_rptr;
1033*307Sszhou 	switch (dl_prim->dl_primitive) {
1034*307Sszhou 	case DL_PHYS_ADDR_ACK:
1035*307Sszhou 		if ((mp->b_wptr-mp->b_rptr) < sizeof (dl_phys_addr_ack_t)) {
1036*307Sszhou 			printf("dl_phys_addr: "
1037*307Sszhou 				"DL_PHYS_ADDR_ACK protocol error\n");
1038*307Sszhou 			break;
1039*307Sszhou 		}
1040*307Sszhou 		phys_addr_ack = &dl_prim->physaddr_ack;
1041*307Sszhou 		if (phys_addr_ack->dl_addr_length != sizeof (*eaddr)) {
1042*307Sszhou 			printf("dl_phys_addr: DL_PHYS_ADDR_ACK bad len %u\n",
1043*307Sszhou 				phys_addr_ack->dl_addr_length);
1044*307Sszhou 			break;
1045*307Sszhou 		}
1046*307Sszhou 		if (phys_addr_ack->dl_addr_length +
1047*307Sszhou 		    phys_addr_ack->dl_addr_offset > (mp->b_wptr-mp->b_rptr)) {
1048*307Sszhou 			printf("dl_phys_addr: DL_PHYS_ADDR_ACK bad len %u\n",
1049*307Sszhou 				phys_addr_ack->dl_addr_length);
1050*307Sszhou 			break;
1051*307Sszhou 		}
1052*307Sszhou 		addrp = mp->b_rptr + phys_addr_ack->dl_addr_offset;
1053*307Sszhou 		bcopy(addrp, eaddr, sizeof (*eaddr));
1054*307Sszhou 		freemsg(mp);
1055*307Sszhou 		return (0);
1056*307Sszhou 
1057*307Sszhou 	case DL_ERROR_ACK:
1058*307Sszhou 		if ((mp->b_wptr-mp->b_rptr) < sizeof (dl_error_ack_t)) {
1059*307Sszhou 			printf("dl_phys_addr: DL_ERROR_ACK protocol error\n");
1060*307Sszhou 			break;
1061*307Sszhou 		}
1062*307Sszhou 
1063*307Sszhou 		error_ack = (dl_error_ack_t *)dl_prim;
1064*307Sszhou 		printf("dl_phys_addr: DLPI error %u\n",
1065*307Sszhou 		    error_ack->dl_errno);
1066*307Sszhou 		break;
1067*307Sszhou 
1068*307Sszhou 	default:
1069*307Sszhou 		printf("dl_phys_addr: bad ACK header %u\n",
1070*307Sszhou 			dl_prim->dl_primitive);
1071*307Sszhou 		break;
1072*307Sszhou 	}
1073*307Sszhou 
1074*307Sszhou 	/*
1075*307Sszhou 	 * Error return only.
1076*307Sszhou 	 */
1077*307Sszhou 	freemsg(mp);
1078*307Sszhou 	return (-1);
1079*307Sszhou }
1080