xref: /titanic_53/usr/src/uts/common/io/strplumb.c (revision 986fd29a0dc13f7608ef7f508f6e700bd7bc2720)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5ca45b50aSseb  * Common Development and Distribution License (the "License").
6ca45b50aSseb  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22f4b3ec61Sdh155122  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include	<sys/param.h>
297c478bd9Sstevel@tonic-gate #include	<sys/types.h>
307c478bd9Sstevel@tonic-gate #include	<sys/user.h>
317c478bd9Sstevel@tonic-gate #include	<sys/vfs.h>
327c478bd9Sstevel@tonic-gate #include	<sys/vnode.h>
337c478bd9Sstevel@tonic-gate #include	<sys/file.h>
347c478bd9Sstevel@tonic-gate #include	<sys/stream.h>
357c478bd9Sstevel@tonic-gate #include	<sys/stropts.h>
367c478bd9Sstevel@tonic-gate #include	<sys/strsubr.h>
377c478bd9Sstevel@tonic-gate #include	<sys/dlpi.h>
387c478bd9Sstevel@tonic-gate #include	<sys/vnode.h>
397c478bd9Sstevel@tonic-gate #include	<sys/socket.h>
407c478bd9Sstevel@tonic-gate #include	<sys/sockio.h>
417c478bd9Sstevel@tonic-gate #include	<net/if.h>
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate #include	<sys/cred.h>
447c478bd9Sstevel@tonic-gate #include	<sys/sysmacros.h>
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate #include	<sys/sad.h>
477c478bd9Sstevel@tonic-gate #include	<sys/kstr.h>
487c478bd9Sstevel@tonic-gate #include	<sys/bootconf.h>
497c478bd9Sstevel@tonic-gate #include	<sys/bootprops.h>
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate #include	<sys/errno.h>
527c478bd9Sstevel@tonic-gate #include	<sys/modctl.h>
537c478bd9Sstevel@tonic-gate #include	<sys/sunddi.h>
547c478bd9Sstevel@tonic-gate #include	<sys/sunldi.h>
557c478bd9Sstevel@tonic-gate #include	<sys/esunddi.h>
567c478bd9Sstevel@tonic-gate #include	<sys/promif.h>
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate #include	<netinet/in.h>
597c478bd9Sstevel@tonic-gate #include	<netinet/ip6.h>
607c478bd9Sstevel@tonic-gate #include	<netinet/icmp6.h>
617c478bd9Sstevel@tonic-gate #include	<netinet/sctp.h>
627c478bd9Sstevel@tonic-gate #include	<inet/common.h>
637c478bd9Sstevel@tonic-gate #include	<inet/ip.h>
647c478bd9Sstevel@tonic-gate #include	<inet/ip6.h>
657c478bd9Sstevel@tonic-gate #include	<inet/tcp.h>
667c478bd9Sstevel@tonic-gate #include	<inet/sctp_ip.h>
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate #include	<sys/strlog.h>
697c478bd9Sstevel@tonic-gate #include	<sys/log.h>
707c478bd9Sstevel@tonic-gate #include	<sys/ethernet.h>
717c478bd9Sstevel@tonic-gate #include	<sys/ddi_implfuncs.h>
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate #include	<sys/dld.h>
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate /*
767c478bd9Sstevel@tonic-gate  * Debug Macros
777c478bd9Sstevel@tonic-gate  */
787c478bd9Sstevel@tonic-gate int	strplumbdebug = 0;
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate #define	DBG0(_f) \
817c478bd9Sstevel@tonic-gate 	if (strplumbdebug != 0) \
827c478bd9Sstevel@tonic-gate 		printf("strplumb: " _f)
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate #define	DBG1(_f, _a) \
857c478bd9Sstevel@tonic-gate 	if (strplumbdebug != 0) \
867c478bd9Sstevel@tonic-gate 		printf("strplumb: " _f, (_a))
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate #define	DBG2(_f, _a, _b) \
897c478bd9Sstevel@tonic-gate 	if (strplumbdebug != 0) \
907c478bd9Sstevel@tonic-gate 		printf("strplumb: " _f, (_a), (_b))
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate #define	DBG3(_f, _a, _b, _c) \
937c478bd9Sstevel@tonic-gate 	if (strplumbdebug != 0) \
947c478bd9Sstevel@tonic-gate 		printf("strplumb: " _f, (_a), (_b), (_c))
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate /*
977c478bd9Sstevel@tonic-gate  * Module linkage information for the kernel.
987c478bd9Sstevel@tonic-gate  */
99*986fd29aSsetje #define	STRPLUMB_IDENT	"STREAMS Plumbing Module"
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate static struct modlmisc modlmisc = {
1027c478bd9Sstevel@tonic-gate 	&mod_miscops,
1037c478bd9Sstevel@tonic-gate 	STRPLUMB_IDENT
1047c478bd9Sstevel@tonic-gate };
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
1077c478bd9Sstevel@tonic-gate 	MODREV_1,
1087c478bd9Sstevel@tonic-gate 	&modlmisc,
1097c478bd9Sstevel@tonic-gate 	NULL
1107c478bd9Sstevel@tonic-gate };
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate int
1137c478bd9Sstevel@tonic-gate _init(void)
1147c478bd9Sstevel@tonic-gate {
1157c478bd9Sstevel@tonic-gate 	return (mod_install(&modlinkage));
1167c478bd9Sstevel@tonic-gate }
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate int
1197c478bd9Sstevel@tonic-gate _fini(void)
1207c478bd9Sstevel@tonic-gate {
1217c478bd9Sstevel@tonic-gate 	return (mod_remove(&modlinkage));
1227c478bd9Sstevel@tonic-gate }
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate int
1257c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
1267c478bd9Sstevel@tonic-gate {
1277c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
1287c478bd9Sstevel@tonic-gate }
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate #define	ARP		"arp"
1317c478bd9Sstevel@tonic-gate #define	TCP		"tcp"
1327c478bd9Sstevel@tonic-gate #define	TCP6		"tcp6"
1337c478bd9Sstevel@tonic-gate #define	UDP		"udp"
1347c478bd9Sstevel@tonic-gate #define	UDP6		"udp6"
1357c478bd9Sstevel@tonic-gate #define	SCTP		"sctp"
1367c478bd9Sstevel@tonic-gate #define	SCTP6		"sctp6"
1377c478bd9Sstevel@tonic-gate #define	ICMP		"icmp"
1387c478bd9Sstevel@tonic-gate #define	ICMP6		"icmp6"
1397c478bd9Sstevel@tonic-gate #define	IP		"ip"
1407c478bd9Sstevel@tonic-gate #define	IP6		"ip6"
1417c478bd9Sstevel@tonic-gate #define	TIMOD		"timod"
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate #define	UDPDEV		"/devices/pseudo/udp@0:udp"
1447c478bd9Sstevel@tonic-gate #define	TCP6DEV		"/devices/pseudo/tcp6@0:tcp6"
1457c478bd9Sstevel@tonic-gate #define	SCTP6DEV	"/devices/pseudo/sctp6@0:sctp6"
1467c478bd9Sstevel@tonic-gate #define	IP6DEV		"/devices/pseudo/ip6@0:ip6"
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate typedef struct strplumb_modspec {
1497c478bd9Sstevel@tonic-gate 	char	*sm_type;
1507c478bd9Sstevel@tonic-gate 	char	*sm_name;
1517c478bd9Sstevel@tonic-gate } strplumb_modspec_t;
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate static strplumb_modspec_t	strplumb_modlist[] = {
1547c478bd9Sstevel@tonic-gate 	{ "drv", DLD_DRIVER_NAME },
1557c478bd9Sstevel@tonic-gate 	{ "drv", IP },
1567c478bd9Sstevel@tonic-gate 	{ "drv", IP6 },
1577c478bd9Sstevel@tonic-gate 	{ "drv", TCP },
1587c478bd9Sstevel@tonic-gate 	{ "drv", TCP6 },
1597c478bd9Sstevel@tonic-gate 	{ "drv", UDP },
1607c478bd9Sstevel@tonic-gate 	{ "drv", UDP6 },
1617c478bd9Sstevel@tonic-gate 	{ "drv", SCTP },
1627c478bd9Sstevel@tonic-gate 	{ "drv", SCTP6 },
1637c478bd9Sstevel@tonic-gate 	{ "drv", ICMP },
1647c478bd9Sstevel@tonic-gate 	{ "drv", ICMP6 },
1657c478bd9Sstevel@tonic-gate 	{ "drv", ARP },
1667c478bd9Sstevel@tonic-gate 	{ "strmod", TIMOD }
1677c478bd9Sstevel@tonic-gate };
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate /*
1707c478bd9Sstevel@tonic-gate  * Called from swapgeneric.c:loadrootmodules() in the network boot case.
1717c478bd9Sstevel@tonic-gate  */
1727c478bd9Sstevel@tonic-gate int
1737c478bd9Sstevel@tonic-gate strplumb_load(void)
1747c478bd9Sstevel@tonic-gate {
1757c478bd9Sstevel@tonic-gate 	uint_t			i;
1767c478bd9Sstevel@tonic-gate 	strplumb_modspec_t	*p;
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 	DBG0("loading modules\n");
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	for (i = 0, p = strplumb_modlist;
1817c478bd9Sstevel@tonic-gate 	    i < sizeof (strplumb_modlist) / sizeof (strplumb_modlist[0]);
1827c478bd9Sstevel@tonic-gate 	    i++, p++) {
1837c478bd9Sstevel@tonic-gate 		if (modloadonly(p->sm_type, p->sm_name) < 0) {
1847c478bd9Sstevel@tonic-gate 			printf("strplumb: failed to load %s/%s\n",
1857c478bd9Sstevel@tonic-gate 			    p->sm_type, p->sm_name);
1867c478bd9Sstevel@tonic-gate 			return (EFAULT);
1877c478bd9Sstevel@tonic-gate 		}
1887c478bd9Sstevel@tonic-gate 	}
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 	return (0);
1917c478bd9Sstevel@tonic-gate }
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate static int
1947c478bd9Sstevel@tonic-gate strplumb_init(void)
1957c478bd9Sstevel@tonic-gate {
1967c478bd9Sstevel@tonic-gate 	uint_t			i;
1977c478bd9Sstevel@tonic-gate 	strplumb_modspec_t	*p;
1987c478bd9Sstevel@tonic-gate 	int			err;
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 	DBG0("initializing modules\n");
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 	for (i = 0, p = strplumb_modlist;
2037c478bd9Sstevel@tonic-gate 	    i < sizeof (strplumb_modlist) / sizeof (strplumb_modlist[0]);
2047c478bd9Sstevel@tonic-gate 	    i++, p++) {
2057c478bd9Sstevel@tonic-gate 		if (strcmp(p->sm_type, "drv") == 0)
2067c478bd9Sstevel@tonic-gate 			err = (i_ddi_attach_pseudo_node(p->sm_name) != NULL) ?
2077c478bd9Sstevel@tonic-gate 			    0 : EFAULT;
2087c478bd9Sstevel@tonic-gate 		else
2097c478bd9Sstevel@tonic-gate 			err = (modload(p->sm_type, p->sm_name) < 0) ?
2107c478bd9Sstevel@tonic-gate 			    EFAULT : 0;
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 		if (err != 0)  {
2137c478bd9Sstevel@tonic-gate 			printf("strplumb: failed to initialize %s/%s\n",
2147c478bd9Sstevel@tonic-gate 			    p->sm_type, p->sm_name);
2157c478bd9Sstevel@tonic-gate 			return (err);
2167c478bd9Sstevel@tonic-gate 		}
2177c478bd9Sstevel@tonic-gate 	}
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	return (0);
2207c478bd9Sstevel@tonic-gate }
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate static int
2237c478bd9Sstevel@tonic-gate strplumb_autopush(void)
2247c478bd9Sstevel@tonic-gate {
2257c478bd9Sstevel@tonic-gate 	major_t		maj;
2267c478bd9Sstevel@tonic-gate 	minor_t		min;
2277c478bd9Sstevel@tonic-gate 	char		*mods[5];
2287c478bd9Sstevel@tonic-gate 	uint_t		anchor = 1;
2297c478bd9Sstevel@tonic-gate 	int		err;
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 	min = (minor_t)-1;
2327c478bd9Sstevel@tonic-gate 	mods[1] = NULL;
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 	/*
2357c478bd9Sstevel@tonic-gate 	 * ARP
2367c478bd9Sstevel@tonic-gate 	 */
2377c478bd9Sstevel@tonic-gate 	DBG0("setting up arp autopush\n");
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 	mods[0] = ARP;
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	maj = ddi_name_to_major(ARP);
2427c478bd9Sstevel@tonic-gate 	if ((err = kstr_autopush(SET_AUTOPUSH, &maj, &min, NULL, &anchor,
2437c478bd9Sstevel@tonic-gate 	    mods)) != 0) {
2447c478bd9Sstevel@tonic-gate 		printf("strplumb: kstr_autopush(SET/ARP) failed: %d\n", err);
2457c478bd9Sstevel@tonic-gate 		return (err);
2467c478bd9Sstevel@tonic-gate 	}
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 	return (0);
2497c478bd9Sstevel@tonic-gate }
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate static int
2527c478bd9Sstevel@tonic-gate strplumb_sctpq(ldi_ident_t li)
2537c478bd9Sstevel@tonic-gate {
2547c478bd9Sstevel@tonic-gate 	ldi_handle_t	lh = NULL;
2557c478bd9Sstevel@tonic-gate 	int		err;
2567c478bd9Sstevel@tonic-gate 	int		rval;
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 	DBG0("configuring SCTP default queue\n");
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 	if ((err = ldi_open_by_name(SCTP6DEV, FREAD|FWRITE, CRED(), &lh,
2617c478bd9Sstevel@tonic-gate 	    li)) != 0) {
2627c478bd9Sstevel@tonic-gate 		printf("strplumb: open of SCTP6DEV failed: %d\n", err);
2637c478bd9Sstevel@tonic-gate 		return (err);
2647c478bd9Sstevel@tonic-gate 	}
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 	if ((err = ldi_ioctl(lh, SCTP_IOC_DEFAULT_Q, (intptr_t)0, FKIOCTL,
2677c478bd9Sstevel@tonic-gate 	    CRED(), &rval)) != 0) {
2687c478bd9Sstevel@tonic-gate 		printf("strplumb: failed to set SCTP default queue: %d\n",
2697c478bd9Sstevel@tonic-gate 		    err);
2707c478bd9Sstevel@tonic-gate 		(void) ldi_close(lh, FREAD|FWRITE, CRED());
2717c478bd9Sstevel@tonic-gate 		return (err);
2727c478bd9Sstevel@tonic-gate 	}
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 	return (0);
2757c478bd9Sstevel@tonic-gate }
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate static int
2787c478bd9Sstevel@tonic-gate strplumb_tcpq(ldi_ident_t li)
2797c478bd9Sstevel@tonic-gate {
2807c478bd9Sstevel@tonic-gate 	ldi_handle_t	lh = NULL;
2817c478bd9Sstevel@tonic-gate 	ldi_handle_t	ip_lh = NULL;
2827c478bd9Sstevel@tonic-gate 	int		err;
2837c478bd9Sstevel@tonic-gate 	int		rval;
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate 	DBG0("configuring TCP default queue\n");
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 	/*
2887c478bd9Sstevel@tonic-gate 	 * We open IP6DEV here because we need to have it open to in
2897c478bd9Sstevel@tonic-gate 	 * order to open TCP6DEV successfully.
2907c478bd9Sstevel@tonic-gate 	 */
2917c478bd9Sstevel@tonic-gate 	if ((err = ldi_open_by_name(IP6DEV, FREAD|FWRITE, CRED(), &ip_lh,
2927c478bd9Sstevel@tonic-gate 	    li)) != 0) {
2937c478bd9Sstevel@tonic-gate 		printf("strplumb: open of IP6DEV failed: %d\n", err);
2947c478bd9Sstevel@tonic-gate 		return (err);
2957c478bd9Sstevel@tonic-gate 	}
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 	/*
2987c478bd9Sstevel@tonic-gate 	 * We set the tcp default queue to IPv6 because IPv4 falls back to
2997c478bd9Sstevel@tonic-gate 	 * IPv6 when it can't find a client, but IPv6 does not fall back to
3007c478bd9Sstevel@tonic-gate 	 * IPv4.
3017c478bd9Sstevel@tonic-gate 	 */
3027c478bd9Sstevel@tonic-gate 	if ((err = ldi_open_by_name(TCP6DEV, FREAD|FWRITE, CRED(), &lh,
3037c478bd9Sstevel@tonic-gate 	    li)) != 0) {
3047c478bd9Sstevel@tonic-gate 		printf("strplumb: open of TCP6DEV failed: %d\n", err);
3057c478bd9Sstevel@tonic-gate 		goto done;
3067c478bd9Sstevel@tonic-gate 	}
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 	if ((err = ldi_ioctl(lh, TCP_IOC_DEFAULT_Q, (intptr_t)0, FKIOCTL,
3097c478bd9Sstevel@tonic-gate 	    CRED(), &rval)) != 0) {
3107c478bd9Sstevel@tonic-gate 		printf("strplumb: failed to set TCP default queue: %d\n",
3117c478bd9Sstevel@tonic-gate 		    err);
3127c478bd9Sstevel@tonic-gate 		goto done;
3137c478bd9Sstevel@tonic-gate 	}
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate done:
3167c478bd9Sstevel@tonic-gate 	(void) ldi_close(ip_lh, FREAD|FWRITE, CRED());
3177c478bd9Sstevel@tonic-gate 	return (err);
3187c478bd9Sstevel@tonic-gate }
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate /*
3217c478bd9Sstevel@tonic-gate  * Can be set in /etc/system in the case of local booting. See comment below.
3227c478bd9Sstevel@tonic-gate  */
3237c478bd9Sstevel@tonic-gate char	*ndev_name = 0;
3247c478bd9Sstevel@tonic-gate int	ndev_unit = 0;
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate /*
3277c478bd9Sstevel@tonic-gate  * If we booted diskless then strplumb() will have been called from
3287c478bd9Sstevel@tonic-gate  * swapgeneric.c:rootconf(). All we can do in that case is plumb the
3297c478bd9Sstevel@tonic-gate  * network device that we booted from.
3307c478bd9Sstevel@tonic-gate  *
3317c478bd9Sstevel@tonic-gate  * If we booted from a local disk, we will have been called from main(),
3327c478bd9Sstevel@tonic-gate  * and normally we defer the plumbing of interfaces until network/physical.
3337c478bd9Sstevel@tonic-gate  * This can be overridden by setting "ndev_name" in /etc/system.
3347c478bd9Sstevel@tonic-gate  */
3357c478bd9Sstevel@tonic-gate static int
336210db224Sericheng resolve_boot_path(void)
3377c478bd9Sstevel@tonic-gate {
3387c478bd9Sstevel@tonic-gate 	char			*devpath = NULL;
3397c478bd9Sstevel@tonic-gate 	dev_info_t		*dip;
3407c478bd9Sstevel@tonic-gate 	const char		*driver;
3417c478bd9Sstevel@tonic-gate 	int			instance;
342*986fd29aSsetje #ifdef	_OBP
343*986fd29aSsetje 	char			stripped_path[OBP_MAXPATHLEN];
344*986fd29aSsetje #endif
3457c478bd9Sstevel@tonic-gate 
346*986fd29aSsetje #ifdef _OBP
347*986fd29aSsetje 	/*
348*986fd29aSsetje 	 * OBP passes options e.g, "net:dhcp"
349*986fd29aSsetje 	 * remove them here
350*986fd29aSsetje 	 */
351*986fd29aSsetje 	if (strncmp(rootfs.bo_fstype, "nfs", 3) == 0) {
352*986fd29aSsetje 		prom_strip_options(rootfs.bo_name, stripped_path);
353*986fd29aSsetje 		devpath = stripped_path;
354*986fd29aSsetje 	}
355*986fd29aSsetje #else
3567c478bd9Sstevel@tonic-gate 	if (strncmp(rootfs.bo_fstype, "nfs", 3) == 0)
3577c478bd9Sstevel@tonic-gate 		devpath = rootfs.bo_name;
3587c478bd9Sstevel@tonic-gate 	else
3597c478bd9Sstevel@tonic-gate 		devpath = strplumb_get_netdev_path();
3607c478bd9Sstevel@tonic-gate #endif
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 	if (devpath != NULL) {
3637c478bd9Sstevel@tonic-gate 		DBG1("resolving boot-path: %s\n", devpath);
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 		/*
3667c478bd9Sstevel@tonic-gate 		 * Hold the devi since this is the root device.
3677c478bd9Sstevel@tonic-gate 		 */
3687c478bd9Sstevel@tonic-gate 		if ((dip = e_ddi_hold_devi_by_path(devpath, 0)) == NULL) {
3697c478bd9Sstevel@tonic-gate 			printf("strplumb: unable to hold root device: %s\n",
3707c478bd9Sstevel@tonic-gate 			    devpath);
3717c478bd9Sstevel@tonic-gate 			return (ENXIO);
3727c478bd9Sstevel@tonic-gate 		}
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 		driver = ddi_driver_name(dip);
3757c478bd9Sstevel@tonic-gate 		instance = ddi_get_instance(dip);
3767c478bd9Sstevel@tonic-gate 	} else {
3777c478bd9Sstevel@tonic-gate 		if (ndev_name == NULL)
3787c478bd9Sstevel@tonic-gate 			return (ENODEV);
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 		DBG2("using ndev_name (%s) ndev_unit (%d)\n", ndev_name,
3817c478bd9Sstevel@tonic-gate 		    ndev_unit);
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate 		if (i_ddi_attach_hw_nodes(ndev_name) != DDI_SUCCESS) {
3847c478bd9Sstevel@tonic-gate 			printf("strplumb: cannot load ndev_name '%s'\n",
3857c478bd9Sstevel@tonic-gate 			    ndev_name);
3867c478bd9Sstevel@tonic-gate 			return (ENXIO);
3877c478bd9Sstevel@tonic-gate 		}
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 		driver = ndev_name;
3907c478bd9Sstevel@tonic-gate 		instance = ndev_unit;
3917c478bd9Sstevel@tonic-gate 	}
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 	(void) snprintf(rootfs.bo_devname, BO_MAXOBJNAME,
3947c478bd9Sstevel@tonic-gate 	    "/devices/pseudo/clone@0:%s", driver);
3957c478bd9Sstevel@tonic-gate 	(void) snprintf(rootfs.bo_ifname, BO_MAXOBJNAME, "%s%d",
3967c478bd9Sstevel@tonic-gate 	    driver, instance);
3977c478bd9Sstevel@tonic-gate 	rootfs.bo_ppa = instance;
3987c478bd9Sstevel@tonic-gate 	return (0);
3997c478bd9Sstevel@tonic-gate }
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate static int
4027c478bd9Sstevel@tonic-gate getifflags(ldi_handle_t lh, struct lifreq *lifrp)
4037c478bd9Sstevel@tonic-gate {
4047c478bd9Sstevel@tonic-gate 	struct strioctl	iocb;
4057c478bd9Sstevel@tonic-gate 	int		rval;
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate 	iocb.ic_cmd = SIOCGLIFFLAGS;
4087c478bd9Sstevel@tonic-gate 	iocb.ic_timout = 15;
4097c478bd9Sstevel@tonic-gate 	iocb.ic_len = sizeof (struct lifreq);
4107c478bd9Sstevel@tonic-gate 	iocb.ic_dp = (char *)lifrp;
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate 	return (ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, CRED(), &rval));
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate }
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate static int
4177c478bd9Sstevel@tonic-gate setifname(ldi_handle_t lh, struct lifreq *lifrp)
4187c478bd9Sstevel@tonic-gate {
4197c478bd9Sstevel@tonic-gate 	struct strioctl	iocb;
4207c478bd9Sstevel@tonic-gate 	int		rval;
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 	iocb.ic_cmd = SIOCSLIFNAME;
4237c478bd9Sstevel@tonic-gate 	iocb.ic_timout = 15;
4247c478bd9Sstevel@tonic-gate 	iocb.ic_len = sizeof (struct lifreq);
4257c478bd9Sstevel@tonic-gate 	iocb.ic_dp = (char *)lifrp;
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate 	return (ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, CRED(), &rval));
4287c478bd9Sstevel@tonic-gate }
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate static int
4317c478bd9Sstevel@tonic-gate strplumb_dev(ldi_ident_t li)
4327c478bd9Sstevel@tonic-gate {
4337c478bd9Sstevel@tonic-gate 	ldi_handle_t	lh = NULL;
4347c478bd9Sstevel@tonic-gate 	ldi_handle_t	mux_lh = NULL;
4357c478bd9Sstevel@tonic-gate 	int		err;
4367c478bd9Sstevel@tonic-gate 	struct lifreq	lifr;
4377c478bd9Sstevel@tonic-gate 	struct ifreq	ifr;
4387c478bd9Sstevel@tonic-gate 	int		rval;
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate 	bzero(&lifr, sizeof (struct lifreq));
4417c478bd9Sstevel@tonic-gate 	bzero(&ifr, sizeof (ifr));
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate 	/*
4447c478bd9Sstevel@tonic-gate 	 * Now set up the links. Ultimately, we should have two streams
4457c478bd9Sstevel@tonic-gate 	 * permanently linked underneath UDP (which is actually IP with UDP
4467c478bd9Sstevel@tonic-gate 	 * autopushed). One stream consists of the ARP-[ifname] combination,
4477c478bd9Sstevel@tonic-gate 	 * while the other consists of ARP-IP-[ifname]. The second combination
4487c478bd9Sstevel@tonic-gate 	 * seems a little weird, but is linked underneath UDP just to keep it
4497c478bd9Sstevel@tonic-gate 	 * around.
4507c478bd9Sstevel@tonic-gate 	 *
4517c478bd9Sstevel@tonic-gate 	 * We pin underneath UDP here to match what is done in ifconfig(1m);
4527c478bd9Sstevel@tonic-gate 	 * otherwise, ifconfig will be unable to unplumb the stream (the major
4537c478bd9Sstevel@tonic-gate 	 * number and mux id must both match for a successful I_PUNLINK).
4547c478bd9Sstevel@tonic-gate 	 *
4557c478bd9Sstevel@tonic-gate 	 * There are subtleties in the plumbing which make it essential to
4567c478bd9Sstevel@tonic-gate 	 * follow the logic used in ifconfig(1m) very closely.
4577c478bd9Sstevel@tonic-gate 	 */
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate 	/*
4607c478bd9Sstevel@tonic-gate 	 * Plumb UDP-ARP-IP-<dev>
4617c478bd9Sstevel@tonic-gate 	 */
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 	if ((err = ldi_open_by_name(rootfs.bo_devname, FREAD|FWRITE, CRED(),
4647c478bd9Sstevel@tonic-gate 	    &lh, li)) != 0) {
4657c478bd9Sstevel@tonic-gate 		printf("strplumb: open %s failed: %d\n", rootfs.bo_devname,
4667c478bd9Sstevel@tonic-gate 		    err);
4677c478bd9Sstevel@tonic-gate 		goto done;
4687c478bd9Sstevel@tonic-gate 	}
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate 	if ((err = ldi_ioctl(lh, I_PUSH, (intptr_t)IP, FKIOCTL, CRED(),
4727c478bd9Sstevel@tonic-gate 	    &rval)) != 0) {
4737c478bd9Sstevel@tonic-gate 		printf("strplumb: push IP failed: %d\n", err);
4747c478bd9Sstevel@tonic-gate 		goto done;
4757c478bd9Sstevel@tonic-gate 	}
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate 	if ((err = getifflags(lh, &lifr)) != 0)
4787c478bd9Sstevel@tonic-gate 		goto done;
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate 	lifr.lifr_flags |= IFF_IPV4;
4817c478bd9Sstevel@tonic-gate 	lifr.lifr_flags &= ~IFF_IPV6;
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 	if ((err = ldi_ioctl(lh, I_PUSH, (intptr_t)ARP, FKIOCTL, CRED(),
4847c478bd9Sstevel@tonic-gate 	    &rval)) != 0) {
4857c478bd9Sstevel@tonic-gate 		printf("strplumb: push ARP failed: %d\n", err);
4867c478bd9Sstevel@tonic-gate 		goto done;
4877c478bd9Sstevel@tonic-gate 	}
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate 	(void) strlcpy(lifr.lifr_name, rootfs.bo_ifname,
4907c478bd9Sstevel@tonic-gate 	    sizeof (lifr.lifr_name));
4917c478bd9Sstevel@tonic-gate 	lifr.lifr_ppa = rootfs.bo_ppa;
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate 	if ((err = setifname(lh, &lifr)) != 0)
4947c478bd9Sstevel@tonic-gate 		goto done;
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate 	/* Get the flags and check if ARP is needed */
4977c478bd9Sstevel@tonic-gate 	if ((err = getifflags(lh, &lifr)) != 0) {
4987c478bd9Sstevel@tonic-gate 		printf("strplumb: getifflags %s IP failed, error %d\n",
4997c478bd9Sstevel@tonic-gate 		    lifr.lifr_name, err);
5007c478bd9Sstevel@tonic-gate 		goto done;
5017c478bd9Sstevel@tonic-gate 	}
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate 	/* Pop out ARP if not needed */
5047c478bd9Sstevel@tonic-gate 	if (lifr.lifr_flags & IFF_NOARP) {
5057c478bd9Sstevel@tonic-gate 		err = ldi_ioctl(lh, I_POP, (intptr_t)0, FKIOCTL, CRED(),
5067c478bd9Sstevel@tonic-gate 		    &rval);
5077c478bd9Sstevel@tonic-gate 		if (err != 0) {
5087c478bd9Sstevel@tonic-gate 			printf("strplumb: pop ARP failed, error %d\n", err);
5097c478bd9Sstevel@tonic-gate 			goto done;
5107c478bd9Sstevel@tonic-gate 		}
5117c478bd9Sstevel@tonic-gate 	}
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate 	if ((err = ldi_open_by_name(UDPDEV, FREAD|FWRITE, CRED(), &mux_lh,
5147c478bd9Sstevel@tonic-gate 	    li)) != 0) {
5157c478bd9Sstevel@tonic-gate 		printf("strplumb: open of UDPDEV failed: %d\n", err);
5167c478bd9Sstevel@tonic-gate 		goto done;
5177c478bd9Sstevel@tonic-gate 	}
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate 	if ((err = ldi_ioctl(mux_lh, I_PLINK, (intptr_t)lh,
5207c478bd9Sstevel@tonic-gate 	    FREAD|FWRITE|FNOCTTY|FKIOCTL, CRED(),
5217c478bd9Sstevel@tonic-gate 	    &(ifr.ifr_ip_muxid))) != 0) {
5227c478bd9Sstevel@tonic-gate 		printf("strplumb: plink UDP-ARP-IP-%s failed: %d\n",
5237c478bd9Sstevel@tonic-gate 		    rootfs.bo_ifname, err);
5247c478bd9Sstevel@tonic-gate 		goto done;
5257c478bd9Sstevel@tonic-gate 	}
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate 	DBG2("UDP-ARP-IP-%s muxid: %d\n", rootfs.bo_ifname, ifr.ifr_ip_muxid);
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate 	(void) ldi_close(lh, FREAD|FWRITE, CRED());
5307c478bd9Sstevel@tonic-gate 	lh = NULL;
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate 	/*
5337c478bd9Sstevel@tonic-gate 	 * Plumb UDP-ARP-<dev>
5347c478bd9Sstevel@tonic-gate 	 */
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate 	if ((err = ldi_open_by_name(rootfs.bo_devname, FREAD|FWRITE, CRED(),
5377c478bd9Sstevel@tonic-gate 	    &lh, li)) != 0) {
5387c478bd9Sstevel@tonic-gate 		printf("strplumb: open %s failed: %d\n", rootfs.bo_devname,
5397c478bd9Sstevel@tonic-gate 		    err);
5407c478bd9Sstevel@tonic-gate 		goto done;
5417c478bd9Sstevel@tonic-gate 	}
5427c478bd9Sstevel@tonic-gate 
5437c478bd9Sstevel@tonic-gate 	if ((err = ldi_ioctl(lh, I_PUSH, (intptr_t)ARP, FKIOCTL, CRED(),
5447c478bd9Sstevel@tonic-gate 	    &rval)) != 0) {
5457c478bd9Sstevel@tonic-gate 		printf("strplumb: push ARP failed: %d\n", err);
5467c478bd9Sstevel@tonic-gate 		goto done;
5477c478bd9Sstevel@tonic-gate 	}
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate 	if ((err = setifname(lh, &lifr)) != 0)
5507c478bd9Sstevel@tonic-gate 		goto done;
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate 	if ((err = ldi_ioctl(mux_lh, I_PLINK, (intptr_t)lh,
5537c478bd9Sstevel@tonic-gate 	    FREAD|FWRITE|FNOCTTY|FKIOCTL, CRED(),
5547c478bd9Sstevel@tonic-gate 	    &(ifr.ifr_arp_muxid))) != 0) {
5557c478bd9Sstevel@tonic-gate 		printf("strplumb: plink UDP-ARP-%s failed: %d\n",
5567c478bd9Sstevel@tonic-gate 		    rootfs.bo_ifname, err);
5577c478bd9Sstevel@tonic-gate 		goto done;
5587c478bd9Sstevel@tonic-gate 	}
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate 	DBG2("UDP-ARP-%s muxid: %d\n", rootfs.bo_ifname, ifr.ifr_arp_muxid);
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate 	/*
5637c478bd9Sstevel@tonic-gate 	 * Cache the mux ids.
5647c478bd9Sstevel@tonic-gate 	 */
5657c478bd9Sstevel@tonic-gate 	(void) strlcpy(ifr.ifr_name, rootfs.bo_ifname, sizeof (ifr.ifr_name));
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate 	if ((err = ldi_ioctl(mux_lh, SIOCSIFMUXID, (intptr_t)&ifr, FKIOCTL,
5687c478bd9Sstevel@tonic-gate 	    CRED(), &rval)) != 0) {
5697c478bd9Sstevel@tonic-gate 		printf("strplumb: SIOCSIFMUXID failed: %d\n", err);
5707c478bd9Sstevel@tonic-gate 		goto done;
5717c478bd9Sstevel@tonic-gate 	}
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate done:
5747c478bd9Sstevel@tonic-gate 	if (lh != NULL)
5757c478bd9Sstevel@tonic-gate 		(void) ldi_close(lh, FREAD|FWRITE, CRED());
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate 	if (mux_lh != NULL)
5787c478bd9Sstevel@tonic-gate 		(void) ldi_close(mux_lh, FREAD|FWRITE, CRED());
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate 	return (err);
5817c478bd9Sstevel@tonic-gate }
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate /*
5847c478bd9Sstevel@tonic-gate  * Do streams plumbing for internet protocols.
5857c478bd9Sstevel@tonic-gate  */
5867c478bd9Sstevel@tonic-gate int
5877c478bd9Sstevel@tonic-gate strplumb(void)
5887c478bd9Sstevel@tonic-gate {
5897c478bd9Sstevel@tonic-gate 	ldi_ident_t	li;
5907c478bd9Sstevel@tonic-gate 	int		err;
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate 	if ((err = strplumb_init()) != 0)
5937c478bd9Sstevel@tonic-gate 		return (err);
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 	if ((err = strplumb_autopush()) != 0)
5967c478bd9Sstevel@tonic-gate 		return (err);
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate 	if ((err = ldi_ident_from_mod(&modlinkage, &li)) != 0)
5997c478bd9Sstevel@tonic-gate 		return (err);
6007c478bd9Sstevel@tonic-gate 
601f4b3ec61Sdh155122 	/*
602f4b3ec61Sdh155122 	 * Setup the TCP and SCTP default queues for the global stack.
603f4b3ec61Sdh155122 	 * tcp/sctp_stack_init will do this for additional stack instances.
604f4b3ec61Sdh155122 	 */
6057c478bd9Sstevel@tonic-gate 	if ((err = strplumb_sctpq(li)) != 0)
6067c478bd9Sstevel@tonic-gate 		goto done;
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate 	if ((err = strplumb_tcpq(li)) != 0)
6097c478bd9Sstevel@tonic-gate 		goto done;
6107c478bd9Sstevel@tonic-gate 
611210db224Sericheng 	if ((err = resolve_boot_path()) != 0)
6127c478bd9Sstevel@tonic-gate 		goto done;
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate 	DBG1("rootfs.bo_devname: %s\n", rootfs.bo_devname);
6157c478bd9Sstevel@tonic-gate 	DBG1("rootfs.bo_ifname: %s\n", rootfs.bo_ifname);
6167c478bd9Sstevel@tonic-gate 	DBG1("rootfs.bo_ppa: %d\n", rootfs.bo_ppa);
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate 	if ((err = strplumb_dev(li)) != 0)
6197c478bd9Sstevel@tonic-gate 		goto done;
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate done:
6227c478bd9Sstevel@tonic-gate 	ldi_ident_release(li);
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate 	return (err);
6257c478bd9Sstevel@tonic-gate }
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate /* multiboot:  diskless boot interface discovery */
6287c478bd9Sstevel@tonic-gate 
629*986fd29aSsetje #ifndef	_OBP
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate static uchar_t boot_macaddr[16];
6327c478bd9Sstevel@tonic-gate static int boot_maclen;
6337c478bd9Sstevel@tonic-gate static uchar_t *getmacaddr(dev_info_t *dip, int *maclen);
6347c478bd9Sstevel@tonic-gate static int matchmac(dev_info_t *dip, void *arg);
635266095caSszhou int dl_attach(ldi_handle_t lh, int unit);
636266095caSszhou int dl_bind(ldi_handle_t lh, uint_t sap, uint_t max_conn,
6377c478bd9Sstevel@tonic-gate     uint_t service, uint_t conn_mgmt);
638266095caSszhou int dl_phys_addr(ldi_handle_t lh, struct ether_addr *eaddr);
6397c478bd9Sstevel@tonic-gate 
640*986fd29aSsetje #endif  /* !_OBP */
641a9ddc71cSsetje 
6427c478bd9Sstevel@tonic-gate char *
6437c478bd9Sstevel@tonic-gate strplumb_get_netdev_path(void)
6447c478bd9Sstevel@tonic-gate {
645*986fd29aSsetje #ifndef	_OBP
6467c478bd9Sstevel@tonic-gate 	char *macstr, *devpath = NULL;
6477c478bd9Sstevel@tonic-gate 	uchar_t *bootp;
648*986fd29aSsetje 	uint_t bootp_len;
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
6517c478bd9Sstevel@tonic-gate 	    DDI_PROP_DONTPASS, BP_BOOT_MAC, &macstr) == DDI_SUCCESS) {
6527c478bd9Sstevel@tonic-gate 		/*
6537c478bd9Sstevel@tonic-gate 		 * hard coded ether mac len for booting floppy on
6547c478bd9Sstevel@tonic-gate 		 * machines with old cards
6557c478bd9Sstevel@tonic-gate 		 */
6567c478bd9Sstevel@tonic-gate 		boot_maclen = ether_aton(macstr, boot_macaddr);
6577c478bd9Sstevel@tonic-gate 		if (boot_maclen != 6) {
6587c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN,
6597c478bd9Sstevel@tonic-gate 			    "malformed boot_mac property, %d bytes",
6607c478bd9Sstevel@tonic-gate 			    boot_maclen);
6617c478bd9Sstevel@tonic-gate 		}
6627c478bd9Sstevel@tonic-gate 		ddi_prop_free(macstr);
6637c478bd9Sstevel@tonic-gate 	} else if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, ddi_root_node(),
6647c478bd9Sstevel@tonic-gate 	    DDI_PROP_DONTPASS, BP_BOOTP_RESPONSE, &bootp, &bootp_len)
6657c478bd9Sstevel@tonic-gate 	    == DDI_SUCCESS) {
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate 		/*
6687c478bd9Sstevel@tonic-gate 		 * These offsets are defined by dhcp standard
6697c478bd9Sstevel@tonic-gate 		 * Should use structure offsets
6707c478bd9Sstevel@tonic-gate 		 */
6717c478bd9Sstevel@tonic-gate 		boot_maclen = *(bootp + 2);
6727c478bd9Sstevel@tonic-gate 		ASSERT(boot_maclen <= 16);
673*986fd29aSsetje 		bcopy(bootp + 28, boot_macaddr, boot_maclen);
6747c478bd9Sstevel@tonic-gate 
675*986fd29aSsetje 		dhcack = kmem_alloc(bootp_len, KM_SLEEP);
676*986fd29aSsetje 		bcopy(bootp, dhcack, bootp_len);
677*986fd29aSsetje 		dhcacklen = bootp_len;
678*986fd29aSsetje 
6797c478bd9Sstevel@tonic-gate 		ddi_prop_free(bootp);
6807c478bd9Sstevel@tonic-gate 	} else
6817c478bd9Sstevel@tonic-gate 		return (NULL);
6827c478bd9Sstevel@tonic-gate 
6837c478bd9Sstevel@tonic-gate 	ddi_walk_devs(ddi_root_node(), matchmac, (void *)&devpath);
6847c478bd9Sstevel@tonic-gate 	return (devpath);
685a9ddc71cSsetje 
686a9ddc71cSsetje #else
687a9ddc71cSsetje 	return (NULL);
688*986fd29aSsetje #endif  /* !_OBP */
6897c478bd9Sstevel@tonic-gate }
6907c478bd9Sstevel@tonic-gate 
691*986fd29aSsetje #ifndef _OBP
692a9ddc71cSsetje 
6937c478bd9Sstevel@tonic-gate /*
6947c478bd9Sstevel@tonic-gate  * Get boot path from the boot_mac address
6957c478bd9Sstevel@tonic-gate  */
6967c478bd9Sstevel@tonic-gate /*ARGSUSED*/
6977c478bd9Sstevel@tonic-gate static int
6987c478bd9Sstevel@tonic-gate matchmac(dev_info_t *dip, void *arg)
6997c478bd9Sstevel@tonic-gate {
7007c478bd9Sstevel@tonic-gate 	char **devpathp = (char **)arg;
7017c478bd9Sstevel@tonic-gate 	char *model_str;
7027c478bd9Sstevel@tonic-gate 	uchar_t *macaddr;
7037c478bd9Sstevel@tonic-gate 	int maclen;
7047c478bd9Sstevel@tonic-gate 
7057c478bd9Sstevel@tonic-gate 	/* XXX Should use "device-type" per IEEE 1275 */
7067c478bd9Sstevel@tonic-gate 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, dip, 0,
7077c478bd9Sstevel@tonic-gate 	    "model", &model_str) != DDI_SUCCESS)
7087c478bd9Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate 	if (strcmp(model_str, "Ethernet controller") != 0) {
7117c478bd9Sstevel@tonic-gate 		ddi_prop_free(model_str);
7127c478bd9Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
7137c478bd9Sstevel@tonic-gate 	}
7147c478bd9Sstevel@tonic-gate 	ddi_prop_free(model_str);
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate 	/* We have a network device now */
7177c478bd9Sstevel@tonic-gate 	if (i_ddi_attach_node_hierarchy(dip) != DDI_SUCCESS) {
7187c478bd9Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
7197c478bd9Sstevel@tonic-gate 	}
7207c478bd9Sstevel@tonic-gate 
7217c478bd9Sstevel@tonic-gate 	ASSERT(boot_maclen != 0);
7227c478bd9Sstevel@tonic-gate 	macaddr = getmacaddr(dip, &maclen);
7237c478bd9Sstevel@tonic-gate 	if (macaddr == NULL)
7247c478bd9Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
7257c478bd9Sstevel@tonic-gate 
7267c478bd9Sstevel@tonic-gate 	if (maclen != boot_maclen ||
7277c478bd9Sstevel@tonic-gate 	    bcmp(macaddr, boot_macaddr, maclen) != 0) {
7287c478bd9Sstevel@tonic-gate 		kmem_free(macaddr, maclen);
7297c478bd9Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
7307c478bd9Sstevel@tonic-gate 	}
7317c478bd9Sstevel@tonic-gate 
7327c478bd9Sstevel@tonic-gate 	/* found hardware with the mac address */
7337c478bd9Sstevel@tonic-gate 	(void) localetheraddr((struct ether_addr *)macaddr, NULL);
7347c478bd9Sstevel@tonic-gate 	kmem_free(macaddr, maclen);
7357c478bd9Sstevel@tonic-gate 
7367c478bd9Sstevel@tonic-gate 	*devpathp = kmem_alloc(MAXPATHLEN, KM_SLEEP);
7377c478bd9Sstevel@tonic-gate 	(void) ddi_pathname(dip, *devpathp);
7387c478bd9Sstevel@tonic-gate 
739*986fd29aSsetje 	/* fill in dhcifname */
740*986fd29aSsetje 	if (dhcack) {
741*986fd29aSsetje 		(void) snprintf(dhcifname, IFNAMSIZ, "%s%d",
7427c478bd9Sstevel@tonic-gate 		    ddi_driver_name(dip), i_ddi_devi_get_ppa(dip));
743*986fd29aSsetje 	}
7447c478bd9Sstevel@tonic-gate 	return (DDI_WALK_TERMINATE);
7457c478bd9Sstevel@tonic-gate }
7467c478bd9Sstevel@tonic-gate 
7477c478bd9Sstevel@tonic-gate static uchar_t *
7487c478bd9Sstevel@tonic-gate getmacaddr_gldv3(char *drv, int inst, int *maclenp)
7497c478bd9Sstevel@tonic-gate {
7507c478bd9Sstevel@tonic-gate 	char ifname[16];
7517c478bd9Sstevel@tonic-gate 	mac_handle_t mh;
7527c478bd9Sstevel@tonic-gate 	uchar_t *macaddr;
7537c478bd9Sstevel@tonic-gate 
7547c478bd9Sstevel@tonic-gate 	(void) snprintf(ifname, sizeof (ifname), "%s%d", drv, inst);
755ca45b50aSseb 	if (mac_open(ifname, inst, &mh) < 0) {
7567c478bd9Sstevel@tonic-gate 		return (NULL);
7577c478bd9Sstevel@tonic-gate 	}
7587c478bd9Sstevel@tonic-gate 	*maclenp = sizeof (struct ether_addr);
7597c478bd9Sstevel@tonic-gate 	macaddr = kmem_alloc(*maclenp, KM_SLEEP);
7607c478bd9Sstevel@tonic-gate 	mac_unicst_get(mh, macaddr);
7617c478bd9Sstevel@tonic-gate 	mac_close(mh);
7627c478bd9Sstevel@tonic-gate 
7637c478bd9Sstevel@tonic-gate 	return (macaddr);
7647c478bd9Sstevel@tonic-gate }
7657c478bd9Sstevel@tonic-gate 
7667c478bd9Sstevel@tonic-gate static uchar_t *
7677c478bd9Sstevel@tonic-gate getmacaddr(dev_info_t *dip, int *maclenp)
7687c478bd9Sstevel@tonic-gate {
7697c478bd9Sstevel@tonic-gate 	int rc, ppa;
7707c478bd9Sstevel@tonic-gate 	ldi_ident_t li;
7717c478bd9Sstevel@tonic-gate 	ldi_handle_t lh;
7727c478bd9Sstevel@tonic-gate 	char *drv_name = (char *)ddi_driver_name(dip);
7737c478bd9Sstevel@tonic-gate 	char *clonepath;
7747c478bd9Sstevel@tonic-gate 	uchar_t *macaddr = NULL;
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate 	/* a simpler way to get mac address for GLDv3 drivers */
7777c478bd9Sstevel@tonic-gate 	if (GLDV3_DRV(ddi_name_to_major(drv_name))) {
7787c478bd9Sstevel@tonic-gate 		return (getmacaddr_gldv3(drv_name, ddi_get_instance(dip),
7797c478bd9Sstevel@tonic-gate 		    maclenp));
7807c478bd9Sstevel@tonic-gate 	}
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate 	if (rc = ldi_ident_from_mod(&modlinkage, &li)) {
7837c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN,
7847c478bd9Sstevel@tonic-gate 		    "getmacaddr: ldi_ident_from_mod failed: %d\n", rc);
7857c478bd9Sstevel@tonic-gate 		return (NULL);
7867c478bd9Sstevel@tonic-gate 	}
7877c478bd9Sstevel@tonic-gate 
7887c478bd9Sstevel@tonic-gate 	clonepath = kmem_alloc(MAXPATHLEN, KM_SLEEP);
7897c478bd9Sstevel@tonic-gate 	(void) snprintf(clonepath, MAXPATHLEN,
7907c478bd9Sstevel@tonic-gate 	    "/devices/pseudo/clone@0:%s", drv_name);
7917c478bd9Sstevel@tonic-gate 
7927c478bd9Sstevel@tonic-gate 	rc = ldi_open_by_name(clonepath, FREAD|FWRITE, CRED(), &lh, li);
7937c478bd9Sstevel@tonic-gate 	ldi_ident_release(li);
7947c478bd9Sstevel@tonic-gate 	if (rc) {
7957c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN,
7967c478bd9Sstevel@tonic-gate 		    "getmacaddr: ldi_open_by_name(%s) failed: %d\n",
7977c478bd9Sstevel@tonic-gate 		    clonepath, rc);
7987c478bd9Sstevel@tonic-gate 		kmem_free(clonepath, MAXPATHLEN);
7997c478bd9Sstevel@tonic-gate 		return (NULL);
8007c478bd9Sstevel@tonic-gate 	}
8017c478bd9Sstevel@tonic-gate 	kmem_free(clonepath, MAXPATHLEN);
8027c478bd9Sstevel@tonic-gate 
8037c478bd9Sstevel@tonic-gate 	ppa = i_ddi_devi_get_ppa(dip);
8047c478bd9Sstevel@tonic-gate 	if ((dl_attach(lh, ppa) != 0) ||
8057c478bd9Sstevel@tonic-gate 	    (dl_bind(lh, ETHERTYPE_IP, 0, DL_CLDLS, 0) != 0)) {
8067c478bd9Sstevel@tonic-gate 		(void) ldi_close(lh, FREAD|FWRITE, CRED());
8077c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN,
8087c478bd9Sstevel@tonic-gate 		    "getmacaddr: dl_attach/bind(%s%d) failed: %d\n",
8097c478bd9Sstevel@tonic-gate 		    drv_name, ppa, rc);
8107c478bd9Sstevel@tonic-gate 		return (NULL);
8117c478bd9Sstevel@tonic-gate 	}
8127c478bd9Sstevel@tonic-gate 	*maclenp = sizeof (struct ether_addr);
8137c478bd9Sstevel@tonic-gate 	macaddr = kmem_alloc(*maclenp, KM_SLEEP);
8147c478bd9Sstevel@tonic-gate 	if (dl_phys_addr(lh, (struct ether_addr *)macaddr) != 0) {
8157c478bd9Sstevel@tonic-gate 		kmem_free(macaddr, *maclenp);
8167c478bd9Sstevel@tonic-gate 		macaddr = NULL;
8177c478bd9Sstevel@tonic-gate 		*maclenp = 0;
8187c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN,
8197c478bd9Sstevel@tonic-gate 		    "getmacaddr: dl_macaddr(%s%d) failed: %d\n",
8207c478bd9Sstevel@tonic-gate 		    drv_name, ppa, rc);
8217c478bd9Sstevel@tonic-gate 	}
8227c478bd9Sstevel@tonic-gate 	(void) ldi_close(lh, FREAD|FWRITE, CRED());
8237c478bd9Sstevel@tonic-gate 	return (macaddr);
8247c478bd9Sstevel@tonic-gate }
8257c478bd9Sstevel@tonic-gate 
826*986fd29aSsetje #endif	/* !_OBP */
827266095caSszhou 
828266095caSszhou int
829266095caSszhou dl_attach(ldi_handle_t lh, int unit)
830266095caSszhou {
831266095caSszhou 	dl_attach_req_t *attach_req;
832266095caSszhou 	union DL_primitives *dl_prim;
833266095caSszhou 	mblk_t *mp;
834266095caSszhou 	int error;
835266095caSszhou 
836266095caSszhou 	if ((mp = allocb(sizeof (dl_attach_req_t), BPRI_MED)) == NULL) {
837266095caSszhou 		cmn_err(CE_WARN, "dl_attach: allocb failed");
838266095caSszhou 		return (ENOSR);
839266095caSszhou 	}
840266095caSszhou 	mp->b_datap->db_type = M_PROTO;
841266095caSszhou 	mp->b_wptr += sizeof (dl_attach_req_t);
842266095caSszhou 
843266095caSszhou 	attach_req = (dl_attach_req_t *)mp->b_rptr;
844266095caSszhou 	attach_req->dl_primitive = DL_ATTACH_REQ;
845266095caSszhou 	attach_req->dl_ppa = unit;
846266095caSszhou 
847266095caSszhou 	(void) ldi_putmsg(lh, mp);
848266095caSszhou 	if ((error = ldi_getmsg(lh, &mp, (timestruc_t *)NULL)) != 0) {
8490466663dSyz147064 		cmn_err(CE_NOTE, "!dl_attach: ldi_getmsg failed: %d", error);
850266095caSszhou 		return (error);
851266095caSszhou 	}
852266095caSszhou 
853266095caSszhou 	dl_prim = (union DL_primitives *)mp->b_rptr;
854266095caSszhou 	switch (dl_prim->dl_primitive) {
855266095caSszhou 	case DL_OK_ACK:
856266095caSszhou 		if ((mp->b_wptr-mp->b_rptr) < sizeof (dl_ok_ack_t)) {
8570466663dSyz147064 			cmn_err(CE_NOTE,
8580466663dSyz147064 			    "!dl_attach: DL_OK_ACK protocol error");
859266095caSszhou 			break;
860266095caSszhou 		}
861266095caSszhou 		if (((dl_ok_ack_t *)dl_prim)->dl_correct_primitive !=
862266095caSszhou 		    DL_ATTACH_REQ) {
8630466663dSyz147064 			cmn_err(CE_NOTE, "!dl_attach: DL_OK_ACK rtnd prim %u",
864266095caSszhou 			    ((dl_ok_ack_t *)dl_prim)->dl_correct_primitive);
865266095caSszhou 			break;
866266095caSszhou 		}
867266095caSszhou 		freemsg(mp);
868266095caSszhou 		return (0);
869266095caSszhou 
870266095caSszhou 	case DL_ERROR_ACK:
871266095caSszhou 		if ((mp->b_wptr-mp->b_rptr) < sizeof (dl_error_ack_t)) {
8720466663dSyz147064 			cmn_err(CE_NOTE,
8730466663dSyz147064 			    "!dl_attach: DL_ERROR_ACK protocol error");
874266095caSszhou 			break;
875266095caSszhou 		}
876266095caSszhou 		break;
877266095caSszhou 
878266095caSszhou 	default:
8790466663dSyz147064 		cmn_err(CE_NOTE, "!dl_attach: bad ACK header %u",
8800466663dSyz147064 		    dl_prim->dl_primitive);
881266095caSszhou 		break;
882266095caSszhou 	}
883266095caSszhou 
884266095caSszhou 	/*
885266095caSszhou 	 * Error return only.
886266095caSszhou 	 */
887266095caSszhou 	freemsg(mp);
888266095caSszhou 	return (-1);
889266095caSszhou }
890266095caSszhou 
891266095caSszhou int
892266095caSszhou dl_bind(ldi_handle_t lh, uint_t sap, uint_t max_conn, uint_t service,
893266095caSszhou 	uint_t conn_mgmt)
894266095caSszhou {
895266095caSszhou 	dl_bind_req_t *bind_req;
896266095caSszhou 	union DL_primitives *dl_prim;
897266095caSszhou 	mblk_t *mp;
898266095caSszhou 	int error;
899266095caSszhou 
900266095caSszhou 	if ((mp = allocb(sizeof (dl_bind_req_t), BPRI_MED)) == NULL) {
901266095caSszhou 		cmn_err(CE_WARN, "dl_bind: allocb failed");
902266095caSszhou 		return (ENOSR);
903266095caSszhou 	}
904266095caSszhou 	mp->b_datap->db_type = M_PROTO;
905266095caSszhou 
906266095caSszhou 	bind_req = (dl_bind_req_t *)mp->b_wptr;
907266095caSszhou 	mp->b_wptr += sizeof (dl_bind_req_t);
908266095caSszhou 	bind_req->dl_primitive = DL_BIND_REQ;
909266095caSszhou 	bind_req->dl_sap = sap;
910266095caSszhou 	bind_req->dl_max_conind = max_conn;
911266095caSszhou 	bind_req->dl_service_mode = service;
912266095caSszhou 	bind_req->dl_conn_mgmt = conn_mgmt;
913266095caSszhou 	bind_req->dl_xidtest_flg = 0;
914266095caSszhou 
915266095caSszhou 	(void) ldi_putmsg(lh, mp);
916266095caSszhou 	if ((error = ldi_getmsg(lh, &mp, (timestruc_t *)NULL)) != 0) {
9170466663dSyz147064 		cmn_err(CE_NOTE, "!dl_bind: ldi_getmsg failed: %d", error);
918266095caSszhou 		return (error);
919266095caSszhou 	}
920266095caSszhou 
921266095caSszhou 	dl_prim = (union DL_primitives *)mp->b_rptr;
922266095caSszhou 	switch (dl_prim->dl_primitive) {
923266095caSszhou 	case DL_BIND_ACK:
924266095caSszhou 		if ((mp->b_wptr-mp->b_rptr) < sizeof (dl_bind_ack_t)) {
9250466663dSyz147064 			cmn_err(CE_NOTE,
9260466663dSyz147064 			    "!dl_bind: DL_BIND_ACK protocol error");
927266095caSszhou 			break;
928266095caSszhou 		}
929266095caSszhou 		if (((dl_bind_ack_t *)dl_prim)->dl_sap != sap) {
9300466663dSyz147064 			cmn_err(CE_NOTE, "!dl_bind: DL_BIND_ACK bad sap %u",
931266095caSszhou 			    ((dl_bind_ack_t *)dl_prim)->dl_sap);
932266095caSszhou 			break;
933266095caSszhou 		}
934266095caSszhou 		freemsg(mp);
935266095caSszhou 		return (0);
936266095caSszhou 
937266095caSszhou 	case DL_ERROR_ACK:
938266095caSszhou 		if ((mp->b_wptr-mp->b_rptr) < sizeof (dl_error_ack_t)) {
9390466663dSyz147064 			cmn_err(CE_NOTE,
9400466663dSyz147064 			    "!dl_bind: DL_ERROR_ACK protocol error");
941266095caSszhou 			break;
942266095caSszhou 		}
943266095caSszhou 		break;
944266095caSszhou 
945266095caSszhou 	default:
9460466663dSyz147064 		cmn_err(CE_NOTE, "!dl_bind: bad ACK header %u",
9470466663dSyz147064 		    dl_prim->dl_primitive);
948266095caSszhou 		break;
949266095caSszhou 	}
950266095caSszhou 
951266095caSszhou 	/*
952266095caSszhou 	 * Error return only.
953266095caSszhou 	 */
954266095caSszhou 	freemsg(mp);
955266095caSszhou 	return (-1);
956266095caSszhou }
957266095caSszhou 
958266095caSszhou int
959266095caSszhou dl_phys_addr(ldi_handle_t lh, struct ether_addr *eaddr)
960266095caSszhou {
961266095caSszhou 	dl_phys_addr_req_t *phys_addr_req;
962266095caSszhou 	dl_phys_addr_ack_t *phys_addr_ack;
963266095caSszhou 	union DL_primitives *dl_prim;
964266095caSszhou 	mblk_t *mp;
965266095caSszhou 	int error;
966266095caSszhou 	uchar_t *addrp;
967266095caSszhou 	timestruc_t tv;
968266095caSszhou 
969266095caSszhou 	if ((mp = allocb(sizeof (dl_phys_addr_req_t), BPRI_MED)) ==
970266095caSszhou 	    (mblk_t *)NULL) {
971266095caSszhou 		cmn_err(CE_WARN, "dl_phys_addr: allocb failed");
972266095caSszhou 		return (ENOSR);
973266095caSszhou 	}
974266095caSszhou 	mp->b_datap->db_type = M_PROTO;
975266095caSszhou 	mp->b_wptr += sizeof (dl_phys_addr_req_t);
976266095caSszhou 
977266095caSszhou 	phys_addr_req = (dl_phys_addr_req_t *)mp->b_rptr;
978266095caSszhou 	phys_addr_req->dl_primitive = DL_PHYS_ADDR_REQ;
979266095caSszhou 	phys_addr_req->dl_addr_type = DL_CURR_PHYS_ADDR;
980266095caSszhou 
981266095caSszhou 	/*
982266095caSszhou 	 * In case some provider doesn't implement or nack the
983266095caSszhou 	 * request just wait for 15 seconds.
984266095caSszhou 	 */
985266095caSszhou 	tv.tv_sec = 15;
986266095caSszhou 	tv.tv_nsec = 0;
987266095caSszhou 
988266095caSszhou 	(void) ldi_putmsg(lh, mp);
989266095caSszhou 	error = ldi_getmsg(lh, &mp, &tv);
990266095caSszhou 	if (error == ETIME) {
9910466663dSyz147064 		cmn_err(CE_NOTE, "!dl_phys_addr: timed out");
992266095caSszhou 		return (-1);
993266095caSszhou 	} else if (error != 0) {
9940466663dSyz147064 		cmn_err(CE_NOTE, "!dl_phys_addr: ldi_getmsg failed: %d", error);
995266095caSszhou 		return (error);
996266095caSszhou 	}
997266095caSszhou 
998266095caSszhou 	dl_prim = (union DL_primitives *)mp->b_rptr;
999266095caSszhou 	switch (dl_prim->dl_primitive) {
1000266095caSszhou 	case DL_PHYS_ADDR_ACK:
1001266095caSszhou 		if ((mp->b_wptr-mp->b_rptr) < sizeof (dl_phys_addr_ack_t)) {
10020466663dSyz147064 			cmn_err(CE_NOTE, "!dl_phys_addr: "
10030466663dSyz147064 			    "DL_PHYS_ADDR_ACK protocol error");
1004266095caSszhou 			break;
1005266095caSszhou 		}
1006266095caSszhou 		phys_addr_ack = &dl_prim->physaddr_ack;
1007266095caSszhou 		if (phys_addr_ack->dl_addr_length != sizeof (*eaddr)) {
10080466663dSyz147064 			cmn_err(CE_NOTE,
10090466663dSyz147064 			    "!dl_phys_addr: DL_PHYS_ADDR_ACK bad len %u",
1010266095caSszhou 			    phys_addr_ack->dl_addr_length);
1011266095caSszhou 			break;
1012266095caSszhou 		}
1013266095caSszhou 		if (phys_addr_ack->dl_addr_length +
1014266095caSszhou 		    phys_addr_ack->dl_addr_offset > (mp->b_wptr-mp->b_rptr)) {
10150466663dSyz147064 			cmn_err(CE_NOTE,
10160466663dSyz147064 			    "!dl_phys_addr: DL_PHYS_ADDR_ACK bad len %u",
1017266095caSszhou 			    phys_addr_ack->dl_addr_length);
1018266095caSszhou 			break;
1019266095caSszhou 		}
1020266095caSszhou 		addrp = mp->b_rptr + phys_addr_ack->dl_addr_offset;
1021266095caSszhou 		bcopy(addrp, eaddr, sizeof (*eaddr));
1022266095caSszhou 		freemsg(mp);
1023266095caSszhou 		return (0);
1024266095caSszhou 
1025266095caSszhou 	case DL_ERROR_ACK:
1026266095caSszhou 		if ((mp->b_wptr-mp->b_rptr) < sizeof (dl_error_ack_t)) {
10270466663dSyz147064 			cmn_err(CE_NOTE,
10280466663dSyz147064 			    "!dl_phys_addr: DL_ERROR_ACK protocol error");
1029266095caSszhou 			break;
1030266095caSszhou 		}
1031266095caSszhou 
1032266095caSszhou 		break;
1033266095caSszhou 
1034266095caSszhou 	default:
10350466663dSyz147064 		cmn_err(CE_NOTE, "!dl_phys_addr: bad ACK header %u",
1036266095caSszhou 		    dl_prim->dl_primitive);
1037266095caSszhou 		break;
1038266095caSszhou 	}
1039266095caSszhou 
1040266095caSszhou 	/*
1041266095caSszhou 	 * Error return only.
1042266095caSszhou 	 */
1043266095caSszhou 	freemsg(mp);
1044266095caSszhou 	return (-1);
1045266095caSszhou }
1046