xref: /illumos-gate/usr/src/uts/common/os/autoconf.c (revision 3ce53722)
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
5f4da9be0Scth  * Common Development and Distribution License (the "License").
6f4da9be0Scth  * 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 /*
225febcb4aSScott Carter, SD IOSW  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
25508a0e8cSRob Johnston /*
26508a0e8cSRob Johnston  * Copyright 2019 Joyent, Inc.
27508a0e8cSRob Johnston  */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * This file contains ddi functions needed during boot and DR.
317c478bd9Sstevel@tonic-gate  * Many functions in swapgeneric.c can be moved here.
327c478bd9Sstevel@tonic-gate  *
337c478bd9Sstevel@tonic-gate  * The object file is currently linked into unix.
347c478bd9Sstevel@tonic-gate  */
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #include <sys/bootconf.h>
377c478bd9Sstevel@tonic-gate #include <sys/conf.h>
387c478bd9Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
397c478bd9Sstevel@tonic-gate #include <sys/ddi_implfuncs.h>
407c478bd9Sstevel@tonic-gate #include <sys/hwconf.h>
417c478bd9Sstevel@tonic-gate #include <sys/instance.h>
427c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
437c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
447c478bd9Sstevel@tonic-gate #include <sys/promif.h>
457c478bd9Sstevel@tonic-gate #include <sys/sunndi.h>
467c478bd9Sstevel@tonic-gate #include <sys/ndi_impldefs.h>
477c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h>
487c478bd9Sstevel@tonic-gate #include <sys/hwconf.h>
497c478bd9Sstevel@tonic-gate #include <sys/sysevent_impl.h>
507c478bd9Sstevel@tonic-gate #include <sys/sunldi_impl.h>
517c478bd9Sstevel@tonic-gate #include <sys/disp.h>
52986fd29aSsetje #include <sys/bootconf.h>
537c478bd9Sstevel@tonic-gate #include <sys/fm/util.h>
54a5667d81SCheng Sean Ye #include <sys/ddifm_impl.h>
55508a0e8cSRob Johnston #include <sys/ddi_ufm_impl.h>
56*3ce53722SRobert Mustacchi #include <sys/ksensor_impl.h>
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate extern dev_info_t *top_devinfo;
597c478bd9Sstevel@tonic-gate extern dev_info_t *scsi_vhci_dip;
607c478bd9Sstevel@tonic-gate extern struct hwc_class *hcl_head;
617c478bd9Sstevel@tonic-gate static char *rootname;		/* node name of top_devinfo */
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate /*
647c478bd9Sstevel@tonic-gate  * This lock must be held while updating devi_sibling pointers of
657c478bd9Sstevel@tonic-gate  * rootnex immediate children
667c478bd9Sstevel@tonic-gate  */
677c478bd9Sstevel@tonic-gate kmutex_t global_vhci_lock;
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate major_t mm_major;
7062a24de0SChris Horne major_t	nulldriver_major;
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate /*
737c478bd9Sstevel@tonic-gate  * Forward declarations
747c478bd9Sstevel@tonic-gate  */
757c478bd9Sstevel@tonic-gate static void impl_create_root_class(void);
767c478bd9Sstevel@tonic-gate static void create_devinfo_tree(void);
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate #if defined(__x86)
797c478bd9Sstevel@tonic-gate char *bootpath_prop = NULL;
807c478bd9Sstevel@tonic-gate char *fstype_prop = NULL;
817c478bd9Sstevel@tonic-gate #endif
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate /*
847c478bd9Sstevel@tonic-gate  * Setup the DDI but don't necessarily init the DDI.  This will happen
857c478bd9Sstevel@tonic-gate  * later once /boot is released.
867c478bd9Sstevel@tonic-gate  */
877c478bd9Sstevel@tonic-gate void
setup_ddi(void)887c478bd9Sstevel@tonic-gate setup_ddi(void)
897c478bd9Sstevel@tonic-gate {
907c478bd9Sstevel@tonic-gate 	impl_ddi_init_nodeid();
917c478bd9Sstevel@tonic-gate 	impl_create_root_class();
927c478bd9Sstevel@tonic-gate 	create_devinfo_tree();
937c478bd9Sstevel@tonic-gate 	e_ddi_instance_init();
947c478bd9Sstevel@tonic-gate 	impl_ddi_callback_init();
957c478bd9Sstevel@tonic-gate 	log_event_init();
967c478bd9Sstevel@tonic-gate 	fm_init();
97a5667d81SCheng Sean Ye 	ndi_fm_init();
985febcb4aSScott Carter, SD IOSW 	irm_init();
99508a0e8cSRob Johnston 	ufm_init();
100*3ce53722SRobert Mustacchi 	ksensor_init();
1017c478bd9Sstevel@tonic-gate 
102a204de77Scth 	(void) i_ddi_load_drvconf(DDI_MAJOR_T_NONE);
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 	ldi_init();
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 	i_ddi_devices_init();
1077c478bd9Sstevel@tonic-gate 	i_ddi_read_devices_files();
1087c478bd9Sstevel@tonic-gate }
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate /*
1117c478bd9Sstevel@tonic-gate  * Perform setup actions post startup (i_ddi_io_initialized)
1127c478bd9Sstevel@tonic-gate  */
1137c478bd9Sstevel@tonic-gate void
setup_ddi_poststartup(void)1147c478bd9Sstevel@tonic-gate setup_ddi_poststartup(void)
1157c478bd9Sstevel@tonic-gate {
1167c478bd9Sstevel@tonic-gate 	extern void i_ddi_start_flush_daemon(void);
1175febcb4aSScott Carter, SD IOSW 	extern void i_ddi_irm_poststartup(void);
1187c478bd9Sstevel@tonic-gate 	extern void i_ddi_intr_redist_all_cpus(void);
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 	i_ddi_start_flush_daemon();
1217c478bd9Sstevel@tonic-gate 
1225febcb4aSScott Carter, SD IOSW 	/* Startup Interrupt Resource Management (IRM) */
1235febcb4aSScott Carter, SD IOSW 	i_ddi_irm_poststartup();
1245febcb4aSScott Carter, SD IOSW 
1257c478bd9Sstevel@tonic-gate 	/*
1267c478bd9Sstevel@tonic-gate 	 * For platforms that support INTR_WEIGHTED_DIST, we perform a
1277c478bd9Sstevel@tonic-gate 	 * redistribution at this point (after NICs configured) so that
1287c478bd9Sstevel@tonic-gate 	 * "isolation" relative to "ddi-intr-weight" occurs.
1297c478bd9Sstevel@tonic-gate 	 */
1307c478bd9Sstevel@tonic-gate 	i_ddi_intr_redist_all_cpus();
1317c478bd9Sstevel@tonic-gate }
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate /*
1347c478bd9Sstevel@tonic-gate  * Create classes and major number bindings for the name of my root.
1357c478bd9Sstevel@tonic-gate  * Called immediately before 'loadrootmodules'
1367c478bd9Sstevel@tonic-gate  */
1377c478bd9Sstevel@tonic-gate static void
impl_create_root_class(void)1387c478bd9Sstevel@tonic-gate impl_create_root_class(void)
1397c478bd9Sstevel@tonic-gate {
1407c478bd9Sstevel@tonic-gate 	major_t major;
1417c478bd9Sstevel@tonic-gate 	size_t size;
1427c478bd9Sstevel@tonic-gate 	char *cp;
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	/*
1457c478bd9Sstevel@tonic-gate 	 * The name for the root nexus is exactly as the manufacturer
1467c478bd9Sstevel@tonic-gate 	 * placed it in the prom name property.  No translation.
1477c478bd9Sstevel@tonic-gate 	 */
148a204de77Scth 	if ((major = ddi_name_to_major("rootnex")) == DDI_MAJOR_T_NONE)
1497c478bd9Sstevel@tonic-gate 		panic("Couldn't find major number for 'rootnex'");
1507c478bd9Sstevel@tonic-gate 
15179755401Ssetje 	/*
15279755401Ssetje 	 * C OBP (Serengeti) does not include the NULL when returning
15379755401Ssetje 	 * the length of the name property, while this violates 1275,
15479755401Ssetje 	 * Solaris needs to work around this by allocating space for
15579755401Ssetje 	 * an extra character.
15679755401Ssetje 	 */
15779755401Ssetje 	size = (size_t)BOP_GETPROPLEN(bootops, "mfg-name") + 1;
1587c478bd9Sstevel@tonic-gate 	rootname = kmem_zalloc(size, KM_SLEEP);
1597c478bd9Sstevel@tonic-gate 	(void) BOP_GETPROP(bootops, "mfg-name", rootname);
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 	/*
1627c478bd9Sstevel@tonic-gate 	 * Fix conflict between OBP names and filesystem names.
1637c478bd9Sstevel@tonic-gate 	 * Substitute '_' for '/' in the name.  Ick.  This is only
1647c478bd9Sstevel@tonic-gate 	 * needed for the root node since '/' is not a legal name
1657c478bd9Sstevel@tonic-gate 	 * character in an OBP device name.
1667c478bd9Sstevel@tonic-gate 	 */
1677c478bd9Sstevel@tonic-gate 	for (cp = rootname; *cp; cp++)
1687c478bd9Sstevel@tonic-gate 		if (*cp == '/')
1697c478bd9Sstevel@tonic-gate 			*cp = '_';
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	/*
1727c478bd9Sstevel@tonic-gate 	 * Bind rootname to rootnex driver
1737c478bd9Sstevel@tonic-gate 	 */
1747c478bd9Sstevel@tonic-gate 	if (make_mbind(rootname, major, NULL, mb_hashtab) != 0) {
1757c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "A driver or driver alias has already "
1767c478bd9Sstevel@tonic-gate 		    "registered the name \"%s\".  The root nexus needs to "
1777c478bd9Sstevel@tonic-gate 		    "use this name, and will override the existing entry. "
1787c478bd9Sstevel@tonic-gate 		    "Please correct /etc/name_to_major and/or "
1797c478bd9Sstevel@tonic-gate 		    "/etc/driver_aliases and reboot.", rootname);
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 		/*
1827c478bd9Sstevel@tonic-gate 		 * Resort to the emergency measure of blowing away the
1837c478bd9Sstevel@tonic-gate 		 * existing hash entry and replacing it with rootname's.
1847c478bd9Sstevel@tonic-gate 		 */
1857c478bd9Sstevel@tonic-gate 		delete_mbind(rootname, mb_hashtab);
1867c478bd9Sstevel@tonic-gate 		if (make_mbind(rootname, major, NULL, mb_hashtab) != 0)
1877c478bd9Sstevel@tonic-gate 			panic("mb_hashtab: inconsistent state.");
1887c478bd9Sstevel@tonic-gate 	}
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 	/*
1917c478bd9Sstevel@tonic-gate 	 * The `platform' or `implementation architecture' name has been
1927c478bd9Sstevel@tonic-gate 	 * translated by boot to be proper for file system use.  It is
1937c478bd9Sstevel@tonic-gate 	 * the `name' of the platform actually booted.  Note the assumption
1947c478bd9Sstevel@tonic-gate 	 * is that the name will `fit' in the buffer platform (which is
1957c478bd9Sstevel@tonic-gate 	 * of size SYS_NMLN, which is far bigger than will actually ever
1967c478bd9Sstevel@tonic-gate 	 * be needed).
1977c478bd9Sstevel@tonic-gate 	 */
1987c478bd9Sstevel@tonic-gate 	(void) BOP_GETPROP(bootops, "impl-arch-name", platform);
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate #if defined(__x86)
2017c478bd9Sstevel@tonic-gate 	/*
2027c478bd9Sstevel@tonic-gate 	 * Retrieve and honor the bootpath and optional fstype properties
2037c478bd9Sstevel@tonic-gate 	 */
2047c478bd9Sstevel@tonic-gate 	size = (size_t)BOP_GETPROPLEN(bootops, "bootpath");
2057c478bd9Sstevel@tonic-gate 	if (size != -1) {
2067c478bd9Sstevel@tonic-gate 		bootpath_prop = kmem_zalloc(size, KM_SLEEP);
2077c478bd9Sstevel@tonic-gate 		(void) BOP_GETPROP(bootops, "bootpath", bootpath_prop);
2087c478bd9Sstevel@tonic-gate 		setbootpath(bootpath_prop);
2097c478bd9Sstevel@tonic-gate 	}
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	size = (size_t)BOP_GETPROPLEN(bootops, "fstype");
2127c478bd9Sstevel@tonic-gate 	if (size != -1) {
2137c478bd9Sstevel@tonic-gate 		fstype_prop = kmem_zalloc(size, KM_SLEEP);
2147c478bd9Sstevel@tonic-gate 		(void) BOP_GETPROP(bootops, "fstype", fstype_prop);
2157c478bd9Sstevel@tonic-gate 		setbootfstype(fstype_prop);
2167c478bd9Sstevel@tonic-gate 	}
2177c478bd9Sstevel@tonic-gate #endif
2187c478bd9Sstevel@tonic-gate }
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate /*
2217c478bd9Sstevel@tonic-gate  * Note that this routine does not take into account the endianness
2227c478bd9Sstevel@tonic-gate  * of the host or the device (or PROM) when retrieving properties.
2237c478bd9Sstevel@tonic-gate  */
2247c478bd9Sstevel@tonic-gate static int
getlongprop_buf(int id,char * name,char * buf,int maxlen)2257c478bd9Sstevel@tonic-gate getlongprop_buf(int id, char *name, char *buf, int maxlen)
2267c478bd9Sstevel@tonic-gate {
2277c478bd9Sstevel@tonic-gate 	int size;
2287c478bd9Sstevel@tonic-gate 
229fa9e4066Sahrens 	size = prom_getproplen((pnode_t)id, name);
2307c478bd9Sstevel@tonic-gate 	if (size <= 0 || (size > maxlen - 1))
2317c478bd9Sstevel@tonic-gate 		return (-1);
2327c478bd9Sstevel@tonic-gate 
233fa9e4066Sahrens 	if (-1 == prom_getprop((pnode_t)id, name, buf))
2347c478bd9Sstevel@tonic-gate 		return (-1);
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 	/*
2377c478bd9Sstevel@tonic-gate 	 * Workaround for bugid 1085575 - OBP may return a "name" property
2387c478bd9Sstevel@tonic-gate 	 * without null terminating the string with '\0'.  When this occurs,
2397c478bd9Sstevel@tonic-gate 	 * append a '\0' and return (size + 1).
2407c478bd9Sstevel@tonic-gate 	 */
2417c478bd9Sstevel@tonic-gate 	if (strcmp("name", name) == 0) {
2427c478bd9Sstevel@tonic-gate 		if (buf[size - 1] != '\0') {
2437c478bd9Sstevel@tonic-gate 			buf[size] = '\0';
2447c478bd9Sstevel@tonic-gate 			size += 1;
2457c478bd9Sstevel@tonic-gate 		}
2467c478bd9Sstevel@tonic-gate 	}
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 	return (size);
2497c478bd9Sstevel@tonic-gate }
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate /*ARGSUSED1*/
2527c478bd9Sstevel@tonic-gate static int
get_neighbors(dev_info_t * di,int flag)2537c478bd9Sstevel@tonic-gate get_neighbors(dev_info_t *di, int flag)
2547c478bd9Sstevel@tonic-gate {
2557c478bd9Sstevel@tonic-gate 	register int nid, snid, cnid;
2567c478bd9Sstevel@tonic-gate 	dev_info_t *parent;
2577c478bd9Sstevel@tonic-gate 	char buf[OBP_MAXPROPNAME];
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 	if (di == NULL)
2607c478bd9Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 	nid = ddi_get_nodeid(di);
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 	snid = cnid = 0;
2657c478bd9Sstevel@tonic-gate 	switch (flag) {
2667c478bd9Sstevel@tonic-gate 		case DDI_WALK_PRUNESIB:
267fa9e4066Sahrens 			cnid = (int)prom_childnode((pnode_t)nid);
2687c478bd9Sstevel@tonic-gate 			break;
2697c478bd9Sstevel@tonic-gate 		case DDI_WALK_PRUNECHILD:
270fa9e4066Sahrens 			snid = (int)prom_nextnode((pnode_t)nid);
2717c478bd9Sstevel@tonic-gate 			break;
2727c478bd9Sstevel@tonic-gate 		case 0:
273fa9e4066Sahrens 			snid = (int)prom_nextnode((pnode_t)nid);
274fa9e4066Sahrens 			cnid = (int)prom_childnode((pnode_t)nid);
2757c478bd9Sstevel@tonic-gate 			break;
2767c478bd9Sstevel@tonic-gate 		default:
2777c478bd9Sstevel@tonic-gate 			return (DDI_WALK_TERMINATE);
2787c478bd9Sstevel@tonic-gate 	}
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 	if (snid && (snid != -1) && ((parent = ddi_get_parent(di)) != NULL)) {
2827c478bd9Sstevel@tonic-gate 		/*
2837c478bd9Sstevel@tonic-gate 		 * add the first sibling that passes check_status()
2847c478bd9Sstevel@tonic-gate 		 */
2857c478bd9Sstevel@tonic-gate 		for (; snid && (snid != -1);
286fa9e4066Sahrens 		    snid = (int)prom_nextnode((pnode_t)snid)) {
2877c478bd9Sstevel@tonic-gate 			if (getlongprop_buf(snid, OBP_NAME, buf,
2887c478bd9Sstevel@tonic-gate 			    sizeof (buf)) > 0) {
2897c478bd9Sstevel@tonic-gate 				if (check_status(snid, buf, parent) ==
2907c478bd9Sstevel@tonic-gate 				    DDI_SUCCESS) {
2917c478bd9Sstevel@tonic-gate 					(void) ddi_add_child(parent, buf,
2927c478bd9Sstevel@tonic-gate 					    snid, -1);
2937c478bd9Sstevel@tonic-gate 					break;
2947c478bd9Sstevel@tonic-gate 				}
2957c478bd9Sstevel@tonic-gate 			}
2967c478bd9Sstevel@tonic-gate 		}
2977c478bd9Sstevel@tonic-gate 	}
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 	if (cnid && (cnid != -1)) {
3007c478bd9Sstevel@tonic-gate 		/*
3017c478bd9Sstevel@tonic-gate 		 * add the first child that passes check_status()
3027c478bd9Sstevel@tonic-gate 		 */
3037c478bd9Sstevel@tonic-gate 		if (getlongprop_buf(cnid, OBP_NAME, buf, sizeof (buf)) > 0) {
3047c478bd9Sstevel@tonic-gate 			if (check_status(cnid, buf, di) == DDI_SUCCESS) {
3057c478bd9Sstevel@tonic-gate 				(void) ddi_add_child(di, buf, cnid, -1);
3067c478bd9Sstevel@tonic-gate 			} else {
307fa9e4066Sahrens 				for (cnid = (int)prom_nextnode((pnode_t)cnid);
3087c478bd9Sstevel@tonic-gate 				    cnid && (cnid != -1);
309fa9e4066Sahrens 				    cnid = (int)prom_nextnode((pnode_t)cnid)) {
3107c478bd9Sstevel@tonic-gate 					if (getlongprop_buf(cnid, OBP_NAME,
3117c478bd9Sstevel@tonic-gate 					    buf, sizeof (buf)) > 0) {
3127c478bd9Sstevel@tonic-gate 						if (check_status(cnid, buf, di)
3137c478bd9Sstevel@tonic-gate 						    == DDI_SUCCESS) {
3147c478bd9Sstevel@tonic-gate 							(void) ddi_add_child(
3157c478bd9Sstevel@tonic-gate 							    di, buf, cnid, -1);
3167c478bd9Sstevel@tonic-gate 							break;
3177c478bd9Sstevel@tonic-gate 						}
3187c478bd9Sstevel@tonic-gate 					}
3197c478bd9Sstevel@tonic-gate 				}
3207c478bd9Sstevel@tonic-gate 			}
3217c478bd9Sstevel@tonic-gate 		}
3227c478bd9Sstevel@tonic-gate 	}
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 	return (DDI_WALK_CONTINUE);
3257c478bd9Sstevel@tonic-gate }
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate static void
di_dfs(dev_info_t * devi,int (* f)(dev_info_t *,int),caddr_t arg)3287c478bd9Sstevel@tonic-gate di_dfs(dev_info_t *devi, int (*f)(dev_info_t *, int), caddr_t arg)
3297c478bd9Sstevel@tonic-gate {
3307c478bd9Sstevel@tonic-gate 	(void) (*f)(devi, 0);
3317c478bd9Sstevel@tonic-gate 	if (devi) {
3327c478bd9Sstevel@tonic-gate 		di_dfs((dev_info_t *)DEVI(devi)->devi_child, f, arg);
3337c478bd9Sstevel@tonic-gate 		di_dfs((dev_info_t *)DEVI(devi)->devi_sibling, f, arg);
3347c478bd9Sstevel@tonic-gate 	}
3357c478bd9Sstevel@tonic-gate }
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate dev_info_t *
i_ddi_create_branch(dev_info_t * pdip,int nid)3387c478bd9Sstevel@tonic-gate i_ddi_create_branch(dev_info_t *pdip, int nid)
3397c478bd9Sstevel@tonic-gate {
3407c478bd9Sstevel@tonic-gate 	char *buf;
3417c478bd9Sstevel@tonic-gate 	dev_info_t *dip = NULL;
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 	if (pdip == NULL || nid == OBP_NONODE || nid == OBP_BADNODE)
3447c478bd9Sstevel@tonic-gate 		return (NULL);
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate 	buf = kmem_alloc(OBP_MAXPROPNAME, KM_SLEEP);
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 	if (getlongprop_buf(nid, OBP_NAME, buf, OBP_MAXPROPNAME) > 0) {
3497c478bd9Sstevel@tonic-gate 		if (check_status(nid, buf, pdip) == DDI_SUCCESS)
3507c478bd9Sstevel@tonic-gate 			dip = ddi_add_child(pdip, buf, nid, -1);
3517c478bd9Sstevel@tonic-gate 	}
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate 	kmem_free(buf, OBP_MAXPROPNAME);
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate 	if (dip == NULL)
3567c478bd9Sstevel@tonic-gate 		return (NULL);
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 	/*
3597c478bd9Sstevel@tonic-gate 	 * Don't create any siblings of the branch root, just
3607c478bd9Sstevel@tonic-gate 	 * children.
3617c478bd9Sstevel@tonic-gate 	 */
3627c478bd9Sstevel@tonic-gate 	(void) get_neighbors(dip, DDI_WALK_PRUNESIB);
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 	di_dfs(ddi_get_child(dip), get_neighbors, 0);
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate 	return (dip);
3677c478bd9Sstevel@tonic-gate }
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate static void
create_devinfo_tree(void)3707c478bd9Sstevel@tonic-gate create_devinfo_tree(void)
3717c478bd9Sstevel@tonic-gate {
3727c478bd9Sstevel@tonic-gate 	major_t major;
373fa9e4066Sahrens 	pnode_t nodeid;
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 	i_ddi_node_cache_init();
3767c478bd9Sstevel@tonic-gate #if defined(__sparc)
3777c478bd9Sstevel@tonic-gate 	nodeid = prom_nextnode(0);
3787c478bd9Sstevel@tonic-gate #else /* x86 */
3797c478bd9Sstevel@tonic-gate 	nodeid = DEVI_SID_NODEID;
3807c478bd9Sstevel@tonic-gate #endif
3817c478bd9Sstevel@tonic-gate 	top_devinfo = i_ddi_alloc_node(NULL, rootname,
3827c478bd9Sstevel@tonic-gate 	    nodeid, -1, NULL, KM_SLEEP);
3837c478bd9Sstevel@tonic-gate 	ndi_hold_devi(top_devinfo);	/* never release the root */
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 	i_ddi_add_devimap(top_devinfo);
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate 	/*
3887c478bd9Sstevel@tonic-gate 	 * Bind root node.
3897c478bd9Sstevel@tonic-gate 	 * This code is special because root node has no parent
3907c478bd9Sstevel@tonic-gate 	 */
3917c478bd9Sstevel@tonic-gate 	major = ddi_name_to_major("rootnex");
392a204de77Scth 	ASSERT(major != DDI_MAJOR_T_NONE);
3937c478bd9Sstevel@tonic-gate 	DEVI(top_devinfo)->devi_major = major;
3947c478bd9Sstevel@tonic-gate 	devnamesp[major].dn_head = top_devinfo;
3957c478bd9Sstevel@tonic-gate 	i_ddi_set_binding_name(top_devinfo, rootname);
3967c478bd9Sstevel@tonic-gate 	i_ddi_set_node_state(top_devinfo, DS_BOUND);
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 	/*
3997c478bd9Sstevel@tonic-gate 	 * Record that devinfos have been made for "rootnex."
4007c478bd9Sstevel@tonic-gate 	 * di_dfs() is used to read the prom because it doesn't get the
4017c478bd9Sstevel@tonic-gate 	 * next sibling until the function returns, unlike ddi_walk_devs().
4027c478bd9Sstevel@tonic-gate 	 */
4037c478bd9Sstevel@tonic-gate 	di_dfs(ddi_root_node(), get_neighbors, 0);
40404a42e3eSszhou 
40504a42e3eSszhou #if !defined(__sparc)
40604a42e3eSszhou 	/*
40704a42e3eSszhou 	 * On x86, there is no prom. Create device tree by
40804a42e3eSszhou 	 * probing pci config space
40904a42e3eSszhou 	 */
41004a42e3eSszhou 	{
41104a42e3eSszhou 		extern void impl_setup_ddi(void);
41204a42e3eSszhou 		impl_setup_ddi();
41304a42e3eSszhou 	}
41404a42e3eSszhou #endif /* x86 */
4157c478bd9Sstevel@tonic-gate }
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate /*
4187c478bd9Sstevel@tonic-gate  * Init and attach the root node. root node is the first one to be
4197c478bd9Sstevel@tonic-gate  * attached, so the process is somewhat "handcrafted".
4207c478bd9Sstevel@tonic-gate  */
4217c478bd9Sstevel@tonic-gate void
i_ddi_init_root()4227c478bd9Sstevel@tonic-gate i_ddi_init_root()
4237c478bd9Sstevel@tonic-gate {
4247c478bd9Sstevel@tonic-gate #ifdef  DDI_PROP_DEBUG
4257c478bd9Sstevel@tonic-gate 	(void) ddi_prop_debug(1);	/* Enable property debugging */
4267c478bd9Sstevel@tonic-gate #endif  /* DDI_PROP_DEBUG */
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate 	/*
4297c478bd9Sstevel@tonic-gate 	 * Initialize root node
4307c478bd9Sstevel@tonic-gate 	 */
4317c478bd9Sstevel@tonic-gate 	if (impl_ddi_sunbus_initchild(top_devinfo) != DDI_SUCCESS)
4327c478bd9Sstevel@tonic-gate 		panic("Could not initialize root nexus");
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 	/*
4357c478bd9Sstevel@tonic-gate 	 * Attach root node (no need to probe)
4367c478bd9Sstevel@tonic-gate 	 * Hold both devinfo and rootnex driver so they can't go away.
4377c478bd9Sstevel@tonic-gate 	 */
4387c478bd9Sstevel@tonic-gate 	DEVI(top_devinfo)->devi_ops = ndi_hold_driver(top_devinfo);
4397c478bd9Sstevel@tonic-gate 	ASSERT(DEV_OPS_HELD(DEVI(top_devinfo)->devi_ops));
4407c478bd9Sstevel@tonic-gate 	DEVI(top_devinfo)->devi_instance = e_ddi_assign_instance(top_devinfo);
4417c478bd9Sstevel@tonic-gate 
44265cf7c95SVikram Hegde 	(void) i_ddi_load_drvconf(DEVI(top_devinfo)->devi_major);
44365cf7c95SVikram Hegde 
44416747f41Scth 	mutex_enter(&(DEVI(top_devinfo)->devi_lock));
4457c478bd9Sstevel@tonic-gate 	DEVI_SET_ATTACHING(top_devinfo);
44616747f41Scth 	mutex_exit(&(DEVI(top_devinfo)->devi_lock));
44716747f41Scth 
4487c478bd9Sstevel@tonic-gate 	if (devi_attach(top_devinfo, DDI_ATTACH) != DDI_SUCCESS)
4497c478bd9Sstevel@tonic-gate 		panic("Could not attach root nexus");
45016747f41Scth 
45116747f41Scth 	mutex_enter(&(DEVI(top_devinfo)->devi_lock));
4527c478bd9Sstevel@tonic-gate 	DEVI_CLR_ATTACHING(top_devinfo);
45316747f41Scth 	mutex_exit(&(DEVI(top_devinfo)->devi_lock));
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 	mutex_init(&global_vhci_lock, NULL, MUTEX_DEFAULT, NULL);
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate 	ndi_hold_devi(top_devinfo);	/* hold it forever */
4587c478bd9Sstevel@tonic-gate 	i_ddi_set_node_state(top_devinfo, DS_READY);
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 	/*
4617c478bd9Sstevel@tonic-gate 	 * Now, expand .conf children of root
4627c478bd9Sstevel@tonic-gate 	 */
4637c478bd9Sstevel@tonic-gate 	(void) i_ndi_make_spec_children(top_devinfo, 0);
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate 	/*
4667c478bd9Sstevel@tonic-gate 	 * Must be set up before attaching root or pseudo drivers
4677c478bd9Sstevel@tonic-gate 	 */
4687c478bd9Sstevel@tonic-gate 	pm_init_locks();
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 	/*
4717c478bd9Sstevel@tonic-gate 	 * Attach options dip
4727c478bd9Sstevel@tonic-gate 	 */
4737c478bd9Sstevel@tonic-gate 	options_dip = i_ddi_attach_pseudo_node("options");
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate 	/*
4767c478bd9Sstevel@tonic-gate 	 * Attach pseudo nexus and enumerate its children
4777c478bd9Sstevel@tonic-gate 	 */
4787c478bd9Sstevel@tonic-gate 	pseudo_dip = i_ddi_attach_pseudo_node(DEVI_PSEUDO_NEXNAME);
4797c478bd9Sstevel@tonic-gate 	(void) i_ndi_make_spec_children(pseudo_dip, 0);
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 	/*
4827c478bd9Sstevel@tonic-gate 	 * Attach and hold clone dip
4837c478bd9Sstevel@tonic-gate 	 */
4847c478bd9Sstevel@tonic-gate 	clone_dip = i_ddi_attach_pseudo_node("clone");
4857c478bd9Sstevel@tonic-gate 	clone_major = ddi_driver_major(clone_dip);
4867c478bd9Sstevel@tonic-gate 	mm_major = ddi_name_to_major("mm");
48762a24de0SChris Horne 	nulldriver_major = ddi_name_to_major("nulldriver");
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate 	/*
4907c478bd9Sstevel@tonic-gate 	 * Attach scsi_vhci for MPXIO, this registers scsi vhci class
4917c478bd9Sstevel@tonic-gate 	 * with the MPXIO framework.
4927c478bd9Sstevel@tonic-gate 	 */
4937c478bd9Sstevel@tonic-gate 	scsi_vhci_dip = i_ddi_attach_pseudo_node("scsi_vhci");
4947c478bd9Sstevel@tonic-gate }
495