xref: /titanic_50/usr/src/uts/common/os/instance.c (revision ffc89d770ba895ae3788935db305c2fc9263cf3c)
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
516bd7258Scth  * Common Development and Distribution License (the "License").
616bd7258Scth  * 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 /*
22a204de77Scth  * Copyright 2008 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 /*
297c478bd9Sstevel@tonic-gate  * Instance number assignment code
307c478bd9Sstevel@tonic-gate  */
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include <sys/types.h>
337c478bd9Sstevel@tonic-gate #include <sys/param.h>
347c478bd9Sstevel@tonic-gate #include <sys/errno.h>
357c478bd9Sstevel@tonic-gate #include <sys/systm.h>
367c478bd9Sstevel@tonic-gate #include <sys/kobj.h>
377c478bd9Sstevel@tonic-gate #include <sys/t_lock.h>
387c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
397c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
407c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
417c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
427c478bd9Sstevel@tonic-gate #include <sys/autoconf.h>
437c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h>
447c478bd9Sstevel@tonic-gate #include <sys/hwconf.h>
457c478bd9Sstevel@tonic-gate #include <sys/reboot.h>
467c478bd9Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
477c478bd9Sstevel@tonic-gate #include <sys/instance.h>
487c478bd9Sstevel@tonic-gate #include <sys/debug.h>
497c478bd9Sstevel@tonic-gate #include <sys/sysevent.h>
507c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
517c478bd9Sstevel@tonic-gate #include <sys/console.h>
527c478bd9Sstevel@tonic-gate #include <sys/cladm.h>
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate static void in_preassign_instance(void);
557c478bd9Sstevel@tonic-gate static void i_log_devfs_instance_mod(void);
567c478bd9Sstevel@tonic-gate static int in_get_infile(char *);
577c478bd9Sstevel@tonic-gate static void in_removenode(struct devnames *dnp, in_node_t *mp, in_node_t *ap);
587c478bd9Sstevel@tonic-gate static in_node_t *in_alloc_node(char *name, char *addr);
597c478bd9Sstevel@tonic-gate static int in_eqstr(char *a, char *b);
607c478bd9Sstevel@tonic-gate static char *in_name_addr(char **cpp, char **addrp);
617c478bd9Sstevel@tonic-gate static in_node_t *in_devwalk(dev_info_t *dip, in_node_t **ap, char *addr);
627c478bd9Sstevel@tonic-gate static void in_dealloc_node(in_node_t *np);
637c478bd9Sstevel@tonic-gate static in_node_t *in_make_path(char *path);
647c478bd9Sstevel@tonic-gate static void in_enlist(in_node_t *ap, in_node_t *np);
657c478bd9Sstevel@tonic-gate static int in_inuse(int instance, char *name);
667c478bd9Sstevel@tonic-gate static void in_hashdrv(in_drv_t *dp);
677c478bd9Sstevel@tonic-gate static in_drv_t *in_drvwalk(in_node_t *np, char *binding_name);
687c478bd9Sstevel@tonic-gate static in_drv_t *in_alloc_drv(char *bindingname);
697c478bd9Sstevel@tonic-gate static void in_endrv(in_node_t *np, in_drv_t *dp);
707c478bd9Sstevel@tonic-gate static void in_dq_drv(in_drv_t *np);
717c478bd9Sstevel@tonic-gate static void in_removedrv(struct devnames *dnp, in_drv_t *mp);
727c478bd9Sstevel@tonic-gate static int in_pathin(char *cp, int instance, char *bname, struct bind **args);
7316bd7258Scth static int in_next_instance_block(major_t, int);
747c478bd9Sstevel@tonic-gate static int in_next_instance(major_t);
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate /* external functions */
777c478bd9Sstevel@tonic-gate extern char *i_binding_to_drv_name(char *bname);
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate /*
807c478bd9Sstevel@tonic-gate  * This plus devnames defines the entire software state of the instance world.
817c478bd9Sstevel@tonic-gate  */
827c478bd9Sstevel@tonic-gate typedef struct in_softstate {
837c478bd9Sstevel@tonic-gate 	in_node_t	*ins_root;	/* the root of our instance tree */
847c478bd9Sstevel@tonic-gate 	in_drv_t	*ins_no_major;	/* majorless drv entries */
857c478bd9Sstevel@tonic-gate 	/*
867c478bd9Sstevel@tonic-gate 	 * Used to serialize access to data structures
877c478bd9Sstevel@tonic-gate 	 */
887c478bd9Sstevel@tonic-gate 	void		*ins_thread;
897c478bd9Sstevel@tonic-gate 	kmutex_t	ins_serial;
907c478bd9Sstevel@tonic-gate 	kcondvar_t	ins_serial_cv;
917c478bd9Sstevel@tonic-gate 	int		ins_busy;
927c478bd9Sstevel@tonic-gate 	char		ins_dirty;	/* need flush */
937c478bd9Sstevel@tonic-gate } in_softstate_t;
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate static in_softstate_t e_ddi_inst_state;
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate /*
987c478bd9Sstevel@tonic-gate  * State transition information:
997c478bd9Sstevel@tonic-gate  * e_ddi_inst_state contains, among other things, the root of a tree of
1007c478bd9Sstevel@tonic-gate  * device nodes used to track instance number assignments.
1017c478bd9Sstevel@tonic-gate  * Each device node may contain multiple driver bindings, represented
1027c478bd9Sstevel@tonic-gate  * by a linked list of in_drv_t nodes, each with an instance assignment
1037c478bd9Sstevel@tonic-gate  * (except for root node). Each in_drv node can be in one of 3 states,
1047c478bd9Sstevel@tonic-gate  * indicated by ind_state:
1057c478bd9Sstevel@tonic-gate  *
1067c478bd9Sstevel@tonic-gate  * IN_UNKNOWN:	Each node created in this state.  The instance number of
1077c478bd9Sstevel@tonic-gate  *	this node is not known.  ind_instance is set to -1.
1087c478bd9Sstevel@tonic-gate  * IN_PROVISIONAL:  When a node is assigned an instance number in
1097c478bd9Sstevel@tonic-gate  *	e_ddi_assign_instance(), its state is set to IN_PROVISIONAL.
1107c478bd9Sstevel@tonic-gate  *	Subsequently, the framework will always call either
1117c478bd9Sstevel@tonic-gate  *	e_ddi_keep_instance() which makes the node IN_PERMANENT,
1127c478bd9Sstevel@tonic-gate  *	or e_ddi_free_instance(), which deletes the node.
1137c478bd9Sstevel@tonic-gate  * IN_PERMANENT:
1147c478bd9Sstevel@tonic-gate  *	If e_ddi_keep_instance() is called on an IN_PROVISIONAL node,
1157c478bd9Sstevel@tonic-gate  *	its state is set to IN_PERMANENT.
1167c478bd9Sstevel@tonic-gate  */
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate static char *instance_file = INSTANCE_FILE;
1197c478bd9Sstevel@tonic-gate static char *instance_file_backup = INSTANCE_FILE INSTANCE_FILE_SUFFIX;
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate /*
1227c478bd9Sstevel@tonic-gate  * Return values for in_get_infile().
1237c478bd9Sstevel@tonic-gate  */
1247c478bd9Sstevel@tonic-gate #define	PTI_FOUND	0
1257c478bd9Sstevel@tonic-gate #define	PTI_NOT_FOUND	1
1267c478bd9Sstevel@tonic-gate #define	PTI_REBUILD	2
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate /*
1297c478bd9Sstevel@tonic-gate  * Path to instance file magic string used for first time boot after
1307c478bd9Sstevel@tonic-gate  * an install.  If this is the first string in the file we will
1317c478bd9Sstevel@tonic-gate  * automatically rebuild the file.
1327c478bd9Sstevel@tonic-gate  */
1337c478bd9Sstevel@tonic-gate #define	PTI_MAGIC_STR		"#path_to_inst_bootstrap_1"
1347c478bd9Sstevel@tonic-gate #define	PTI_MAGIC_STR_LEN	(sizeof (PTI_MAGIC_STR) - 1)
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate void
1377c478bd9Sstevel@tonic-gate e_ddi_instance_init(void)
1387c478bd9Sstevel@tonic-gate {
1397c478bd9Sstevel@tonic-gate 	char *file;
1407c478bd9Sstevel@tonic-gate 	int rebuild = 1;
1417c478bd9Sstevel@tonic-gate 	struct in_drv *dp;
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	mutex_init(&e_ddi_inst_state.ins_serial, NULL, MUTEX_DEFAULT, NULL);
1447c478bd9Sstevel@tonic-gate 	cv_init(&e_ddi_inst_state.ins_serial_cv, NULL, CV_DEFAULT, NULL);
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	/*
1477c478bd9Sstevel@tonic-gate 	 * Only one thread is allowed to change the state of the instance
1487c478bd9Sstevel@tonic-gate 	 * number assignments on the system at any given time.
1497c478bd9Sstevel@tonic-gate 	 * Note that this is not really necessary, as we are single-threaded
1507c478bd9Sstevel@tonic-gate 	 * here, but it won't hurt, and it allows us to keep ASSERTS for
1517c478bd9Sstevel@tonic-gate 	 * our assumptions in the code.
1527c478bd9Sstevel@tonic-gate 	 */
1537c478bd9Sstevel@tonic-gate 	e_ddi_enter_instance();
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 	/*
1567c478bd9Sstevel@tonic-gate 	 * Create the root node, instance zallocs to 0.
1577c478bd9Sstevel@tonic-gate 	 * The name and address of this node never get examined, we always
1587c478bd9Sstevel@tonic-gate 	 * start searching with its first child.
1597c478bd9Sstevel@tonic-gate 	 */
1607c478bd9Sstevel@tonic-gate 	ASSERT(e_ddi_inst_state.ins_root == NULL);
1617c478bd9Sstevel@tonic-gate 	e_ddi_inst_state.ins_root = in_alloc_node(NULL, NULL);
1627c478bd9Sstevel@tonic-gate 	dp = in_alloc_drv("rootnex");
1637c478bd9Sstevel@tonic-gate 	in_endrv(e_ddi_inst_state.ins_root, dp);
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	file = instance_file;
1667c478bd9Sstevel@tonic-gate 	switch (in_get_infile(file)) {
1677c478bd9Sstevel@tonic-gate 	default:
1687c478bd9Sstevel@tonic-gate 	case PTI_NOT_FOUND:
1697c478bd9Sstevel@tonic-gate 		/* make sure path_to_inst is recreated */
1707c478bd9Sstevel@tonic-gate 		boothowto |= RB_RECONFIG;
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 		/*
1737c478bd9Sstevel@tonic-gate 		 * Something is wrong. First try the backup file.
1747c478bd9Sstevel@tonic-gate 		 * If not found, rebuild path_to_inst. Emit a
1757c478bd9Sstevel@tonic-gate 		 * message about the problem.
1767c478bd9Sstevel@tonic-gate 		 */
1777c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s empty or not found", file);
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 		file = instance_file_backup;
1807c478bd9Sstevel@tonic-gate 		if (in_get_infile(file) != PTI_FOUND) {
1817c478bd9Sstevel@tonic-gate 			cmn_err(CE_NOTE, "rebuilding device instance data");
1827c478bd9Sstevel@tonic-gate 			break;
1837c478bd9Sstevel@tonic-gate 		}
1847c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "using backup instance data in %s", file);
1857c478bd9Sstevel@tonic-gate 		/*FALLTHROUGH*/
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	case PTI_FOUND:
1887c478bd9Sstevel@tonic-gate 		/*
1897c478bd9Sstevel@tonic-gate 		 * We've got a readable file
1907c478bd9Sstevel@tonic-gate 		 * parse the file into the instance tree
1917c478bd9Sstevel@tonic-gate 		 */
1927c478bd9Sstevel@tonic-gate 		(void) read_binding_file(file, NULL, in_pathin);
1937c478bd9Sstevel@tonic-gate 		rebuild = 0;
1947c478bd9Sstevel@tonic-gate 		break;
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 	case PTI_REBUILD:
1977c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT,
1987c478bd9Sstevel@tonic-gate 		    "?Using default device instance data\n");
1997c478bd9Sstevel@tonic-gate 		break;
2007c478bd9Sstevel@tonic-gate 	}
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 	/*
2037c478bd9Sstevel@tonic-gate 	 * The OBP device tree has been copied to the kernel and
2047c478bd9Sstevel@tonic-gate 	 * bound to drivers at this point. We walk the per-driver
2057c478bd9Sstevel@tonic-gate 	 * list to preassign instances. Since the bus addr is
2067c478bd9Sstevel@tonic-gate 	 * unknown at this point, we cannot place the instance
2077c478bd9Sstevel@tonic-gate 	 * number in the instance tree. This will be done at
2087c478bd9Sstevel@tonic-gate 	 * a later time.
2097c478bd9Sstevel@tonic-gate 	 */
2107c478bd9Sstevel@tonic-gate 	if (rebuild)
2117c478bd9Sstevel@tonic-gate 		in_preassign_instance();
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 	e_ddi_exit_instance();
2147c478bd9Sstevel@tonic-gate }
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate static void
2177c478bd9Sstevel@tonic-gate in_preassign_instance()
2187c478bd9Sstevel@tonic-gate {
2197c478bd9Sstevel@tonic-gate 	major_t m;
2207c478bd9Sstevel@tonic-gate 	extern major_t devcnt;
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	for (m = 0; m < devcnt; m++) {
2237c478bd9Sstevel@tonic-gate 		struct devnames *dnp = &devnamesp[m];
2247c478bd9Sstevel@tonic-gate 		dev_info_t *dip = dnp->dn_head;
2257c478bd9Sstevel@tonic-gate 		while (dip) {
2267c478bd9Sstevel@tonic-gate 			DEVI(dip)->devi_instance = dnp->dn_instance;
2277c478bd9Sstevel@tonic-gate 			dnp->dn_instance++;
2287c478bd9Sstevel@tonic-gate 			dip = ddi_get_next(dip);
2297c478bd9Sstevel@tonic-gate 		}
2307c478bd9Sstevel@tonic-gate 	}
2317c478bd9Sstevel@tonic-gate }
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate /*
2347c478bd9Sstevel@tonic-gate  * Checks to see if the /etc/path_to_inst file exists and whether or not
2357c478bd9Sstevel@tonic-gate  * it has the magic string in it.
2367c478bd9Sstevel@tonic-gate  *
2377c478bd9Sstevel@tonic-gate  * Returns one of the following:
2387c478bd9Sstevel@tonic-gate  *
2397c478bd9Sstevel@tonic-gate  *	PTI_FOUND	- We have found the /etc/path_to_inst file
2407c478bd9Sstevel@tonic-gate  *	PTI_REBUILD	- We have found the /etc/path_to_inst file and the
2417c478bd9Sstevel@tonic-gate  *			  first line was PTI_MAGIC_STR.
2427c478bd9Sstevel@tonic-gate  *	PTI_NOT_FOUND	- We did not find the /etc/path_to_inst file
2437c478bd9Sstevel@tonic-gate  *
2447c478bd9Sstevel@tonic-gate  */
2457c478bd9Sstevel@tonic-gate static int
2467c478bd9Sstevel@tonic-gate in_get_infile(char *filename)
2477c478bd9Sstevel@tonic-gate {
248986fd29aSsetje 	struct _buf *file;
2497c478bd9Sstevel@tonic-gate 	int return_val;
2507c478bd9Sstevel@tonic-gate 	char buf[PTI_MAGIC_STR_LEN];
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 	/*
2537c478bd9Sstevel@tonic-gate 	 * Try to open the file.
2547c478bd9Sstevel@tonic-gate 	 */
255986fd29aSsetje 	if ((file = kobj_open_file(filename)) == (struct _buf *)-1) {
2567c478bd9Sstevel@tonic-gate 		return (PTI_NOT_FOUND);
2577c478bd9Sstevel@tonic-gate 	}
2587c478bd9Sstevel@tonic-gate 	return_val = PTI_FOUND;
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 	/*
2617c478bd9Sstevel@tonic-gate 	 * Read the first PTI_MAGIC_STR_LEN bytes from the file to see if
2627c478bd9Sstevel@tonic-gate 	 * it contains the magic string.  If there aren't that many bytes
2637c478bd9Sstevel@tonic-gate 	 * in the file, then assume file is correct and no magic string
2647c478bd9Sstevel@tonic-gate 	 * and move on.
2657c478bd9Sstevel@tonic-gate 	 */
266986fd29aSsetje 	switch (kobj_read_file(file, buf, PTI_MAGIC_STR_LEN, 0)) {
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 	case PTI_MAGIC_STR_LEN:
2697c478bd9Sstevel@tonic-gate 		/*
2707c478bd9Sstevel@tonic-gate 		 * If the first PTI_MAGIC_STR_LEN bytes are the magic string
2717c478bd9Sstevel@tonic-gate 		 * then return PTI_REBUILD.
2727c478bd9Sstevel@tonic-gate 		 */
2737c478bd9Sstevel@tonic-gate 		if (strncmp(PTI_MAGIC_STR, buf, PTI_MAGIC_STR_LEN) == 0)
2747c478bd9Sstevel@tonic-gate 			return_val = PTI_REBUILD;
2757c478bd9Sstevel@tonic-gate 		break;
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 	case 0:
2787c478bd9Sstevel@tonic-gate 		/*
2797c478bd9Sstevel@tonic-gate 		 * If the file is zero bytes in length, then consider the
2807c478bd9Sstevel@tonic-gate 		 * file to not be found
2817c478bd9Sstevel@tonic-gate 		 */
2827c478bd9Sstevel@tonic-gate 		return_val = PTI_NOT_FOUND;
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 	default: /* Do nothing we have a good file */
2857c478bd9Sstevel@tonic-gate 		break;
2867c478bd9Sstevel@tonic-gate 	}
2877c478bd9Sstevel@tonic-gate 
288986fd29aSsetje 	kobj_close_file(file);
2897c478bd9Sstevel@tonic-gate 	return (return_val);
2907c478bd9Sstevel@tonic-gate }
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate int
2937c478bd9Sstevel@tonic-gate is_pseudo_device(dev_info_t *dip)
2947c478bd9Sstevel@tonic-gate {
2957c478bd9Sstevel@tonic-gate 	dev_info_t	*pdip;
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 	for (pdip = ddi_get_parent(dip); pdip && pdip != ddi_root_node();
2987c478bd9Sstevel@tonic-gate 	    pdip = ddi_get_parent(pdip)) {
2997c478bd9Sstevel@tonic-gate 		if (strcmp(ddi_get_name(pdip), DEVI_PSEUDO_NEXNAME) == 0)
3007c478bd9Sstevel@tonic-gate 			return (1);
3017c478bd9Sstevel@tonic-gate 	}
3027c478bd9Sstevel@tonic-gate 	return (0);
3037c478bd9Sstevel@tonic-gate }
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate static void
3077c478bd9Sstevel@tonic-gate in_set_instance(dev_info_t *dip, in_drv_t *dp, major_t major)
3087c478bd9Sstevel@tonic-gate {
3097c478bd9Sstevel@tonic-gate 	/* use preassigned instance if available */
3107c478bd9Sstevel@tonic-gate 	if (DEVI(dip)->devi_instance != -1)
3117c478bd9Sstevel@tonic-gate 		dp->ind_instance = DEVI(dip)->devi_instance;
3127c478bd9Sstevel@tonic-gate 	else
3137c478bd9Sstevel@tonic-gate 		dp->ind_instance = in_next_instance(major);
3147c478bd9Sstevel@tonic-gate }
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate /*
31716bd7258Scth  * Return 1 if instance block was assigned for the path.
31816bd7258Scth  *
31916bd7258Scth  * For multi-port NIC cards, sequential instance assignment across all
32016bd7258Scth  * ports on a card is highly deseriable since the ppa is typically the
32116bd7258Scth  * same as the instance number, and the ppa is used in the NIC's public
32216bd7258Scth  * /dev name. This sequential assignment typically occurs as a result
32316bd7258Scth  * of in_preassign_instance() after initial install, or by
32416bd7258Scth  * i_ndi_init_hw_children() for NIC ports that share a common parent.
32516bd7258Scth  *
32616bd7258Scth  * Some NIC cards however use multi-function bridge chips, and to
32716bd7258Scth  * support sequential instance assignment accross all ports, without
32816bd7258Scth  * disabling multi-threaded attach, we have a (currently) undocumented
32916bd7258Scth  * hack to allocate instance numbers in contiguous blocks based on
33016bd7258Scth  * driver.conf properties.
33116bd7258Scth  *
33216bd7258Scth  *                       ^
33316bd7258Scth  *           /----------   ------------\
33416bd7258Scth  *        pci@0                      pci@0,1	MULTI-FUNCTION BRIDGE CHIP
33516bd7258Scth  *       /     \                    /       \
33616bd7258Scth  * FJSV,e4ta@4  FJSV,e4ta@4,1   FJSV,e4ta@6 FJSV,e4ta@6,1	NIC PORTS
33716bd7258Scth  *      n            n+2             n+2         n+3		INSTANCE
33816bd7258Scth  *
33916bd7258Scth  * For the above example, the following driver.conf properties would be
34016bd7258Scth  * used to guarantee sequential instance number assignment.
34116bd7258Scth  *
34216bd7258Scth  * ddi-instance-blocks ="ib-FJSVe4ca", "ib-FJSVe4ta", "ib-generic";
34316bd7258Scth  * ib-FJSVe4ca =	"/pci@0/FJSV,e4ca@4", "/pci@0/FJSV,e4ca@4,1",
34416bd7258Scth  *			"/pci@0,1/FJSV,e4ca@6", "/pci@0,1/FJSV,e4ca@6,1";
34516bd7258Scth  * ib-FJSVe4ta =	"/pci@0/FJSV,e4ta@4", "/pci@0/FJSV,e4ta@4,1",
34616bd7258Scth  *			"/pci@0,1/FJSV,e4ta@6", "/pci@0,1/FJSV,e4ta@6,1";
34716bd7258Scth  * ib-generic =		"/pci@0/network@4", "/pci@0/network@4,1",
34816bd7258Scth  *			"/pci@0,1/network@6", "/pci@0,1/network@6,1";
34916bd7258Scth  *
35016bd7258Scth  * The value of the 'ddi-instance-blocks' property references a series
35116bd7258Scth  * of card specific properties, like 'ib-FJSV-e4ta', who's value
35216bd7258Scth  * defines a single 'instance block'.  The 'instance block' describes
35316bd7258Scth  * all the paths below a multi-function bridge, where each path is
35416bd7258Scth  * called an 'instance path'.  The 'instance block' property value is a
35516bd7258Scth  * series of 'instance paths'.  The number of 'instance paths' in an
35616bd7258Scth  * 'instance block' defines the size of the instance block, and the
35716bd7258Scth  * ordering of the 'instance paths' defines the instance number
35816bd7258Scth  * assignment order for paths going through the 'instance block'.
35916bd7258Scth  *
36016bd7258Scth  * In the instance assignment code below, if a (path, driver) that
36116bd7258Scth  * currently has no instance number has a path that goes through an
36216bd7258Scth  * 'instance block', then block instance number allocation occurs.  The
36316bd7258Scth  * block allocation code will find a sequential set of unused instance
36416bd7258Scth  * numbers, and assign instance numbers for all the paths in the
36516bd7258Scth  * 'instance block'.  Each path is assigned a persistent instance
36616bd7258Scth  * number, even paths that don't exist in the device tree or fail
36716bd7258Scth  * probe(9E).
36816bd7258Scth  */
36916bd7258Scth static int
37016bd7258Scth in_assign_instance_block(dev_info_t *dip)
37116bd7258Scth {
37216bd7258Scth 	char		**ibn;		/* instance block names */
37316bd7258Scth 	uint_t		nibn;		/* number of instance block names */
37416bd7258Scth 	uint_t		ibni;		/* ibn index */
37516bd7258Scth 	char		*driver;
37616bd7258Scth 	major_t		major;
37716bd7258Scth 	char		*path;
37816bd7258Scth 	char		*addr;
37916bd7258Scth 	int		plen;
38016bd7258Scth 	char		**ibp;		/* instance block paths */
38116bd7258Scth 	uint_t		nibp;		/* number of paths in instance block */
38216bd7258Scth 	uint_t		ibpi;		/* ibp index */
38316bd7258Scth 	int		ibplen;		/* length of instance block path */
38416bd7258Scth 	char		*ipath;
38516bd7258Scth 	int		instance_base;
38616bd7258Scth 	int		splice;
38716bd7258Scth 	int		i;
38816bd7258Scth 
38916bd7258Scth 	/* check for fresh install case (in miniroot) */
39016bd7258Scth 	if (DEVI(dip)->devi_instance != -1)
39116bd7258Scth 		return (0);			/* already assigned */
39216bd7258Scth 
39316bd7258Scth 	/*
39416bd7258Scth 	 * Check to see if we need to allocate a block of contiguous instance
39516bd7258Scth 	 * numbers by looking for the 'ddi-instance-blocks' property.
39616bd7258Scth 	 */
39716bd7258Scth 	if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
39816bd7258Scth 	    "ddi-instance-blocks", &ibn, &nibn) != DDI_SUCCESS)
39916bd7258Scth 		return (0);			/* no instance block needed */
40016bd7258Scth 
40116bd7258Scth 	/*
40216bd7258Scth 	 * Get information out about node we are processing.
40316bd7258Scth 	 *
40416bd7258Scth 	 * NOTE: Since the node is not yet at DS_INITIALIZED, ddi_pathname()
40516bd7258Scth 	 * will not return the unit-address of the final path component even
40616bd7258Scth 	 * though the node has an established devi_addr unit-address - so we
40716bd7258Scth 	 * need to add the unit-address by hand.
40816bd7258Scth 	 */
40916bd7258Scth 	driver = (char *)ddi_driver_name(dip);
41016bd7258Scth 	major = ddi_driver_major(dip);
41116bd7258Scth 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
41216bd7258Scth 	(void) ddi_pathname(dip, path);
41316bd7258Scth 	if ((addr =  ddi_get_name_addr(dip)) != NULL) {
41416bd7258Scth 		(void) strcat(path, "@");
41516bd7258Scth 		(void) strcat(path, addr);
41616bd7258Scth 	}
41716bd7258Scth 	plen = strlen(path);
41816bd7258Scth 
41916bd7258Scth 	/* loop through instance block names */
42016bd7258Scth 	for (ibni = 0; ibni < nibn; ibni++) {
42116bd7258Scth 		if (ibn[ibni] == NULL)
42216bd7258Scth 			continue;
42316bd7258Scth 
42416bd7258Scth 		/* lookup instance block */
42516bd7258Scth 		if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, dip,
42616bd7258Scth 		    DDI_PROP_DONTPASS, ibn[ibni],
42716bd7258Scth 		    &ibp, &nibp) != DDI_SUCCESS) {
42816bd7258Scth 			cmn_err(CE_WARN,
42916bd7258Scth 			    "no devinition for instance block '%s' in %s.conf",
43016bd7258Scth 			    ibn[ibni], driver);
43116bd7258Scth 			continue;
43216bd7258Scth 		}
43316bd7258Scth 
43416bd7258Scth 		/* Does 'path' go through this instance block? */
43516bd7258Scth 		for (ibpi = 0; ibpi < nibp; ibpi++) {
43616bd7258Scth 			if (ibp[ibpi] == NULL)
43716bd7258Scth 				continue;
43816bd7258Scth 			ibplen = strlen(ibp[ibpi]);
43916bd7258Scth 			if ((ibplen <= plen) &&
44016bd7258Scth 			    (strcmp(ibp[ibpi], path + plen - ibplen) == 0))
44116bd7258Scth 				break;
44216bd7258Scth 
44316bd7258Scth 		}
44416bd7258Scth 		if (ibpi >= nibp) {
44516bd7258Scth 			ddi_prop_free(ibp);
44616bd7258Scth 			continue;		/* no try next instance block */
44716bd7258Scth 		}
44816bd7258Scth 
44916bd7258Scth 		/* yes, allocate and assign instances for all paths in block */
45016bd7258Scth 
45116bd7258Scth 		/*
45216bd7258Scth 		 * determine where we splice in instance paths and verify
45316bd7258Scth 		 * that none of the paths are too long.
45416bd7258Scth 		 */
45516bd7258Scth 		splice = plen - ibplen;
45616bd7258Scth 		for (i = 0; i < nibp; i++) {
45716bd7258Scth 			if ((splice + strlen(ibp[i])+ 1) >= MAXPATHLEN) {
45816bd7258Scth 				cmn_err(CE_WARN,
45916bd7258Scth 				    "path %d through instance block '%s' from "
46016bd7258Scth 				    "%s.conf too long", i, ibn[ibni], driver);
46116bd7258Scth 				break;
46216bd7258Scth 			}
46316bd7258Scth 		}
46416bd7258Scth 		if (i < nibp) {
46516bd7258Scth 			ddi_prop_free(ibp);
46616bd7258Scth 			continue;		/* too long */
46716bd7258Scth 		}
46816bd7258Scth 
46916bd7258Scth 		/* allocate the instance block - no more failures */
47016bd7258Scth 		instance_base = in_next_instance_block(major, nibp);
47116bd7258Scth 
47216bd7258Scth 		ipath = kmem_alloc(MAXPATHLEN, KM_SLEEP);
47316bd7258Scth 		for (ibpi = 0; ibpi < nibp; ibpi++) {
47416bd7258Scth 			if (ibp[ibpi] == NULL)
47516bd7258Scth 				continue;
47616bd7258Scth 			(void) strcpy(ipath, path);
47716bd7258Scth 			(void) strcpy(ipath + splice, ibp[ibpi]);
47816bd7258Scth 			(void) in_pathin(ipath,
47916bd7258Scth 			    instance_base + ibpi, driver, NULL);
48016bd7258Scth 		}
48116bd7258Scth 
48216bd7258Scth 		/* free allocations */
48316bd7258Scth 		kmem_free(ipath, MAXPATHLEN);
48416bd7258Scth 		ddi_prop_free(ibp);
48516bd7258Scth 		kmem_free(path, MAXPATHLEN);
48616bd7258Scth 		ddi_prop_free(ibn);
48716bd7258Scth 
48816bd7258Scth 		/* notify devfsadmd to sync of path_to_inst file */
48916bd7258Scth 		mutex_enter(&e_ddi_inst_state.ins_serial);
49016bd7258Scth 		i_log_devfs_instance_mod();
49116bd7258Scth 		e_ddi_inst_state.ins_dirty = 1;
49216bd7258Scth 		mutex_exit(&e_ddi_inst_state.ins_serial);
49316bd7258Scth 		return (1);
49416bd7258Scth 	}
49516bd7258Scth 
49616bd7258Scth 	/* our path did not go through any of of the instance blocks */
49716bd7258Scth 	kmem_free(path, MAXPATHLEN);
49816bd7258Scth 	ddi_prop_free(ibn);
49916bd7258Scth 	return (0);
50016bd7258Scth }
50116bd7258Scth 
50216bd7258Scth /*
5037c478bd9Sstevel@tonic-gate  * Look up an instance number for a dev_info node, and assign one if it does
5047c478bd9Sstevel@tonic-gate  * not have one (the dev_info node has devi_name and devi_addr already set).
5057c478bd9Sstevel@tonic-gate  */
5067c478bd9Sstevel@tonic-gate uint_t
5077c478bd9Sstevel@tonic-gate e_ddi_assign_instance(dev_info_t *dip)
5087c478bd9Sstevel@tonic-gate {
5097c478bd9Sstevel@tonic-gate 	char *name;
5107c478bd9Sstevel@tonic-gate 	in_node_t *ap, *np;
5117c478bd9Sstevel@tonic-gate 	in_drv_t *dp;
5127c478bd9Sstevel@tonic-gate 	major_t major;
5137c478bd9Sstevel@tonic-gate 	uint_t ret;
5147c478bd9Sstevel@tonic-gate 	char *bname;
5157c478bd9Sstevel@tonic-gate 
5167c478bd9Sstevel@tonic-gate 	/*
5177c478bd9Sstevel@tonic-gate 	 * Allow implementation to override
5187c478bd9Sstevel@tonic-gate 	 */
5197c478bd9Sstevel@tonic-gate 	if ((ret = impl_assign_instance(dip)) != (uint_t)-1)
5207c478bd9Sstevel@tonic-gate 		return (ret);
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate 	/*
5237c478bd9Sstevel@tonic-gate 	 * If this is a pseudo-device, use the instance number
5247c478bd9Sstevel@tonic-gate 	 * assigned by the pseudo nexus driver. The mutex is
5257c478bd9Sstevel@tonic-gate 	 * not needed since the instance tree is not used.
5267c478bd9Sstevel@tonic-gate 	 */
5277c478bd9Sstevel@tonic-gate 	if (is_pseudo_device(dip)) {
5287c478bd9Sstevel@tonic-gate 		return (ddi_get_instance(dip));
5297c478bd9Sstevel@tonic-gate 	}
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate 	/*
5327c478bd9Sstevel@tonic-gate 	 * Only one thread is allowed to change the state of the instance
5337c478bd9Sstevel@tonic-gate 	 * number assignments on the system at any given time.
5347c478bd9Sstevel@tonic-gate 	 */
5357c478bd9Sstevel@tonic-gate 	e_ddi_enter_instance();
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate 	/*
5387c478bd9Sstevel@tonic-gate 	 * Look for instance node, allocate one if not found
5397c478bd9Sstevel@tonic-gate 	 */
5407c478bd9Sstevel@tonic-gate 	np = in_devwalk(dip, &ap, NULL);
5417c478bd9Sstevel@tonic-gate 	if (np == NULL) {
54216bd7258Scth 		if (in_assign_instance_block(dip)) {
54316bd7258Scth 			np = in_devwalk(dip, &ap, NULL);
54416bd7258Scth 		} else {
5457c478bd9Sstevel@tonic-gate 			name = ddi_node_name(dip);
5467c478bd9Sstevel@tonic-gate 			np = in_alloc_node(name, ddi_get_name_addr(dip));
5477c478bd9Sstevel@tonic-gate 			ASSERT(np != NULL);
5487c478bd9Sstevel@tonic-gate 			in_enlist(ap, np);	/* insert into tree */
5497c478bd9Sstevel@tonic-gate 		}
55016bd7258Scth 	}
5517c478bd9Sstevel@tonic-gate 	ASSERT(np == in_devwalk(dip, &ap, NULL));
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate 	/*
5547c478bd9Sstevel@tonic-gate 	 * Look for driver entry, allocate one if not found
5557c478bd9Sstevel@tonic-gate 	 */
5567c478bd9Sstevel@tonic-gate 	bname = (char *)ddi_driver_name(dip);
5577c478bd9Sstevel@tonic-gate 	dp = in_drvwalk(np, bname);
5587c478bd9Sstevel@tonic-gate 	if (dp == NULL) {
5597c478bd9Sstevel@tonic-gate 		dp = in_alloc_drv(bname);
5607c478bd9Sstevel@tonic-gate 		ASSERT(dp != NULL);
5617c478bd9Sstevel@tonic-gate 		major = ddi_driver_major(dip);
562a204de77Scth 		ASSERT(major != DDI_MAJOR_T_NONE);
5637c478bd9Sstevel@tonic-gate 		in_endrv(np, dp);
5647c478bd9Sstevel@tonic-gate 		in_set_instance(dip, dp, major);
5657c478bd9Sstevel@tonic-gate 		dp->ind_state = IN_PROVISIONAL;
5667c478bd9Sstevel@tonic-gate 		in_hashdrv(dp);
5677c478bd9Sstevel@tonic-gate 	}
5687c478bd9Sstevel@tonic-gate 
5697c478bd9Sstevel@tonic-gate 	ret = dp->ind_instance;
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate 	e_ddi_exit_instance();
5727c478bd9Sstevel@tonic-gate 	return (ret);
5737c478bd9Sstevel@tonic-gate }
5747c478bd9Sstevel@tonic-gate 
5757c478bd9Sstevel@tonic-gate static int
5767c478bd9Sstevel@tonic-gate mkpathname(char *path, in_node_t *np, int len)
5777c478bd9Sstevel@tonic-gate {
5787c478bd9Sstevel@tonic-gate 	int len_needed;
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate 	if (np == e_ddi_inst_state.ins_root)
5817c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate 	if (mkpathname(path, np->in_parent, len) == DDI_FAILURE)
5847c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate 	len_needed = strlen(path);
5877c478bd9Sstevel@tonic-gate 	len_needed += strlen(np->in_node_name) + 1;	/* for '/' */
5887c478bd9Sstevel@tonic-gate 	if (np->in_unit_addr) {
5897c478bd9Sstevel@tonic-gate 		len_needed += strlen(np->in_unit_addr) + 1;  /* for '@' */
5907c478bd9Sstevel@tonic-gate 	}
5917c478bd9Sstevel@tonic-gate 	len_needed += 1; /* for '\0' */
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate 	/*
5947c478bd9Sstevel@tonic-gate 	 * XX complain
5957c478bd9Sstevel@tonic-gate 	 */
5967c478bd9Sstevel@tonic-gate 	if (len_needed > len)
5977c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate 	if (np->in_unit_addr[0] == '\0')
6007c478bd9Sstevel@tonic-gate 		(void) sprintf(path+strlen(path), "/%s", np->in_node_name);
6017c478bd9Sstevel@tonic-gate 	else
6027c478bd9Sstevel@tonic-gate 		(void) sprintf(path+strlen(path), "/%s@%s", np->in_node_name,
6037c478bd9Sstevel@tonic-gate 		    np->in_unit_addr);
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
6067c478bd9Sstevel@tonic-gate }
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate /*
6097c478bd9Sstevel@tonic-gate  * produce the path to the given instance of a major number.
6107c478bd9Sstevel@tonic-gate  * path must hold MAXPATHLEN string
6117c478bd9Sstevel@tonic-gate  */
6127c478bd9Sstevel@tonic-gate int
6137c478bd9Sstevel@tonic-gate e_ddi_instance_majorinstance_to_path(major_t major, uint_t inst, char *path)
6147c478bd9Sstevel@tonic-gate {
6157c478bd9Sstevel@tonic-gate 	struct devnames	*dnp;
6167c478bd9Sstevel@tonic-gate 	in_drv_t	*dp;
6177c478bd9Sstevel@tonic-gate 	int		ret;
6187c478bd9Sstevel@tonic-gate 
6197c478bd9Sstevel@tonic-gate 	e_ddi_enter_instance();
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate 	/* look for the instance threaded off major */
6227c478bd9Sstevel@tonic-gate 	dnp = &devnamesp[major];
6237c478bd9Sstevel@tonic-gate 	for (dp = dnp->dn_inlist; dp != NULL; dp = dp->ind_next)
6247c478bd9Sstevel@tonic-gate 		if (dp->ind_instance == inst)
6257c478bd9Sstevel@tonic-gate 			break;
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate 	/* produce path from the node that uses the instance */
6287c478bd9Sstevel@tonic-gate 	if (dp) {
6297c478bd9Sstevel@tonic-gate 		*path = 0;
6307c478bd9Sstevel@tonic-gate 		ret = mkpathname(path, dp->ind_node, MAXPATHLEN);
6317c478bd9Sstevel@tonic-gate 	} else
6327c478bd9Sstevel@tonic-gate 		ret = DDI_FAILURE;
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate 	e_ddi_exit_instance();
6357c478bd9Sstevel@tonic-gate 	return (ret);
6367c478bd9Sstevel@tonic-gate }
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate /*
63916bd7258Scth  * Allocate a sequential block of instance numbers for the specified driver,
64016bd7258Scth  * and return the base instance number of the block.  The implementation
64116bd7258Scth  * depends on the list being sorted in ascending instance number sequence.
64216bd7258Scth  * When there are no 'holes' in the allocation sequence, dn_instance is the
64316bd7258Scth  * next available instance number. When dn_instance is IN_SEARCHME, hole(s)
64416bd7258Scth  * exists and a slower code path executes which tries to fill holes.
6457c478bd9Sstevel@tonic-gate  */
6467c478bd9Sstevel@tonic-gate static int
64716bd7258Scth in_next_instance_block(major_t major, int block_size)
6487c478bd9Sstevel@tonic-gate {
6497c478bd9Sstevel@tonic-gate 	unsigned int	prev;
6507c478bd9Sstevel@tonic-gate 	struct devnames	*dnp;
6517c478bd9Sstevel@tonic-gate 	in_drv_t	*dp;
65216bd7258Scth 	int		base;
65316bd7258Scth 	int		hole;
6547c478bd9Sstevel@tonic-gate 
6557c478bd9Sstevel@tonic-gate 	dnp = &devnamesp[major];
656a204de77Scth 	ASSERT(major != DDI_MAJOR_T_NONE);
6577c478bd9Sstevel@tonic-gate 	ASSERT(e_ddi_inst_state.ins_busy);
65816bd7258Scth 	ASSERT(block_size);
65916bd7258Scth 
66016bd7258Scth 	/* check to see if we can do a quick allocation */
66116bd7258Scth 	if (dnp->dn_instance != IN_SEARCHME) {
66216bd7258Scth 		base = dnp->dn_instance;
66316bd7258Scth 		dnp->dn_instance += block_size;
66416bd7258Scth 		return (base);
66516bd7258Scth 	}
6667c478bd9Sstevel@tonic-gate 	dp = dnp->dn_inlist;
6677c478bd9Sstevel@tonic-gate 
66816bd7258Scth 	/* no existing entries, allocate block at 0 */
6697c478bd9Sstevel@tonic-gate 	if (dp == NULL) {
67016bd7258Scth 		dnp->dn_instance = block_size;
6717c478bd9Sstevel@tonic-gate 		return (0);
6727c478bd9Sstevel@tonic-gate 	}
6737c478bd9Sstevel@tonic-gate 
6747c478bd9Sstevel@tonic-gate 	prev = dp->ind_instance;
67516bd7258Scth 	if (prev >= block_size)
67616bd7258Scth 		return (0);		/* we fit in hole at beginning */
6777c478bd9Sstevel@tonic-gate 
67816bd7258Scth 	/* search the list for a large enough hole */
67916bd7258Scth 	for (dp = dp->ind_next, hole = 0; dp; dp = dp->ind_next) {
68016bd7258Scth 		if (dp->ind_instance != (prev + 1))
68116bd7258Scth 			hole++;			/* we have a hole */
68216bd7258Scth 		if (dp->ind_instance >= (prev + block_size + 1))
68316bd7258Scth 			break;			/* we fit in hole */
68416bd7258Scth 		prev = dp->ind_instance;
68516bd7258Scth 	}
68616bd7258Scth 
68716bd7258Scth 	/*
68816bd7258Scth 	 * If hole is zero then all holes are patched and we can resume
68916bd7258Scth 	 * quick allocations.
69016bd7258Scth 	 */
69116bd7258Scth 	if (hole == 0)
69216bd7258Scth 		dnp->dn_instance = prev + 1 + block_size;
69316bd7258Scth 
69416bd7258Scth 	return (prev + 1);
69516bd7258Scth }
69616bd7258Scth 
69716bd7258Scth /* assign instance block of size 1 */
69816bd7258Scth static int
69916bd7258Scth in_next_instance(major_t major)
70016bd7258Scth {
70116bd7258Scth 	return (in_next_instance_block(major, 1));
7027c478bd9Sstevel@tonic-gate }
7037c478bd9Sstevel@tonic-gate 
7047c478bd9Sstevel@tonic-gate /*
7057c478bd9Sstevel@tonic-gate  * This call causes us to *forget* the instance number we've generated
7067c478bd9Sstevel@tonic-gate  * for a given device if it was not permanent.
7077c478bd9Sstevel@tonic-gate  */
7087c478bd9Sstevel@tonic-gate void
7097c478bd9Sstevel@tonic-gate e_ddi_free_instance(dev_info_t *dip, char *addr)
7107c478bd9Sstevel@tonic-gate {
7117c478bd9Sstevel@tonic-gate 	char *name;
7127c478bd9Sstevel@tonic-gate 	in_node_t *np;
7137c478bd9Sstevel@tonic-gate 	in_node_t *ap;	/* ancestor node */
7147c478bd9Sstevel@tonic-gate 	major_t major;
7157c478bd9Sstevel@tonic-gate 	struct devnames *dnp;
7167c478bd9Sstevel@tonic-gate 	in_drv_t *dp;	/* in_drv entry */
7177c478bd9Sstevel@tonic-gate 
7187c478bd9Sstevel@tonic-gate 	/*
7197c478bd9Sstevel@tonic-gate 	 * Allow implementation override
7207c478bd9Sstevel@tonic-gate 	 */
7217c478bd9Sstevel@tonic-gate 	if (impl_free_instance(dip) == DDI_SUCCESS)
7227c478bd9Sstevel@tonic-gate 		return;
7237c478bd9Sstevel@tonic-gate 
7247c478bd9Sstevel@tonic-gate 	/*
7257c478bd9Sstevel@tonic-gate 	 * If this is a pseudo-device, no instance number
7267c478bd9Sstevel@tonic-gate 	 * was assigned.
7277c478bd9Sstevel@tonic-gate 	 */
7287c478bd9Sstevel@tonic-gate 	if (is_pseudo_device(dip)) {
7297c478bd9Sstevel@tonic-gate 		return;
7307c478bd9Sstevel@tonic-gate 	}
7317c478bd9Sstevel@tonic-gate 
7327c478bd9Sstevel@tonic-gate 	name = (char *)ddi_driver_name(dip);
7337c478bd9Sstevel@tonic-gate 	major = ddi_driver_major(dip);
734a204de77Scth 	ASSERT(major != DDI_MAJOR_T_NONE);
7357c478bd9Sstevel@tonic-gate 	dnp = &devnamesp[major];
7367c478bd9Sstevel@tonic-gate 	/*
7377c478bd9Sstevel@tonic-gate 	 * Only one thread is allowed to change the state of the instance
7387c478bd9Sstevel@tonic-gate 	 * number assignments on the system at any given time.
7397c478bd9Sstevel@tonic-gate 	 */
7407c478bd9Sstevel@tonic-gate 	e_ddi_enter_instance();
7417c478bd9Sstevel@tonic-gate 	np = in_devwalk(dip, &ap, addr);
7427c478bd9Sstevel@tonic-gate 	ASSERT(np);
7437c478bd9Sstevel@tonic-gate 	dp = in_drvwalk(np, name);
7447c478bd9Sstevel@tonic-gate 	ASSERT(dp);
7457c478bd9Sstevel@tonic-gate 	if (dp->ind_state == IN_PROVISIONAL) {
7467c478bd9Sstevel@tonic-gate 		in_removedrv(dnp, dp);
7477c478bd9Sstevel@tonic-gate 	}
7487c478bd9Sstevel@tonic-gate 	if (np->in_drivers == NULL) {
7497c478bd9Sstevel@tonic-gate 		in_removenode(dnp, np, ap);
7507c478bd9Sstevel@tonic-gate 	}
7517c478bd9Sstevel@tonic-gate 	e_ddi_exit_instance();
7527c478bd9Sstevel@tonic-gate }
7537c478bd9Sstevel@tonic-gate 
7547c478bd9Sstevel@tonic-gate /*
7557c478bd9Sstevel@tonic-gate  * This makes our memory of an instance assignment permanent
7567c478bd9Sstevel@tonic-gate  */
7577c478bd9Sstevel@tonic-gate void
7587c478bd9Sstevel@tonic-gate e_ddi_keep_instance(dev_info_t *dip)
7597c478bd9Sstevel@tonic-gate {
7607c478bd9Sstevel@tonic-gate 	in_node_t *np, *ap;
7617c478bd9Sstevel@tonic-gate 	in_drv_t *dp;
7627c478bd9Sstevel@tonic-gate 
7637c478bd9Sstevel@tonic-gate 	/*
7647c478bd9Sstevel@tonic-gate 	 * Allow implementation override
7657c478bd9Sstevel@tonic-gate 	 */
7667c478bd9Sstevel@tonic-gate 	if (impl_keep_instance(dip) == DDI_SUCCESS)
7677c478bd9Sstevel@tonic-gate 		return;
7687c478bd9Sstevel@tonic-gate 
7697c478bd9Sstevel@tonic-gate 	/*
7707c478bd9Sstevel@tonic-gate 	 * Nothing to do for pseudo devices.
7717c478bd9Sstevel@tonic-gate 	 */
7727c478bd9Sstevel@tonic-gate 	if (is_pseudo_device(dip))
7737c478bd9Sstevel@tonic-gate 		return;
7747c478bd9Sstevel@tonic-gate 
7757c478bd9Sstevel@tonic-gate 	/*
7767c478bd9Sstevel@tonic-gate 	 * Only one thread is allowed to change the state of the instance
7777c478bd9Sstevel@tonic-gate 	 * number assignments on the system at any given time.
7787c478bd9Sstevel@tonic-gate 	 */
7797c478bd9Sstevel@tonic-gate 	e_ddi_enter_instance();
7807c478bd9Sstevel@tonic-gate 	np = in_devwalk(dip, &ap, NULL);
7817c478bd9Sstevel@tonic-gate 	ASSERT(np);
7827c478bd9Sstevel@tonic-gate 	dp = in_drvwalk(np, (char *)ddi_driver_name(dip));
7837c478bd9Sstevel@tonic-gate 	ASSERT(dp);
7847c478bd9Sstevel@tonic-gate 
7857c478bd9Sstevel@tonic-gate 	mutex_enter(&e_ddi_inst_state.ins_serial);
7867c478bd9Sstevel@tonic-gate 	if (dp->ind_state == IN_PROVISIONAL) {
7877c478bd9Sstevel@tonic-gate 		dp->ind_state = IN_PERMANENT;
7887c478bd9Sstevel@tonic-gate 		i_log_devfs_instance_mod();
7897c478bd9Sstevel@tonic-gate 		e_ddi_inst_state.ins_dirty = 1;
7907c478bd9Sstevel@tonic-gate 	}
7917c478bd9Sstevel@tonic-gate 	mutex_exit(&e_ddi_inst_state.ins_serial);
7927c478bd9Sstevel@tonic-gate 	e_ddi_exit_instance();
7937c478bd9Sstevel@tonic-gate }
7947c478bd9Sstevel@tonic-gate 
7957c478bd9Sstevel@tonic-gate /*
7967c478bd9Sstevel@tonic-gate  * A new major has been added to the system.  Run through the orphan list
7977c478bd9Sstevel@tonic-gate  * and try to attach each one to a driver's list.
7987c478bd9Sstevel@tonic-gate  */
7997c478bd9Sstevel@tonic-gate void
8007c478bd9Sstevel@tonic-gate e_ddi_unorphan_instance_nos()
8017c478bd9Sstevel@tonic-gate {
8027c478bd9Sstevel@tonic-gate 	in_drv_t *dp, *ndp;
8037c478bd9Sstevel@tonic-gate 
8047c478bd9Sstevel@tonic-gate 	/*
8057c478bd9Sstevel@tonic-gate 	 * disconnect the orphan list, and call in_hashdrv for each item
8067c478bd9Sstevel@tonic-gate 	 * on it
8077c478bd9Sstevel@tonic-gate 	 */
8087c478bd9Sstevel@tonic-gate 
8097c478bd9Sstevel@tonic-gate 	/*
8107c478bd9Sstevel@tonic-gate 	 * Only one thread is allowed to change the state of the instance
8117c478bd9Sstevel@tonic-gate 	 * number assignments on the system at any given time.
8127c478bd9Sstevel@tonic-gate 	 */
8137c478bd9Sstevel@tonic-gate 	e_ddi_enter_instance();
8147c478bd9Sstevel@tonic-gate 	if (e_ddi_inst_state.ins_no_major == NULL) {
8157c478bd9Sstevel@tonic-gate 		e_ddi_exit_instance();
8167c478bd9Sstevel@tonic-gate 		return;
8177c478bd9Sstevel@tonic-gate 	}
8187c478bd9Sstevel@tonic-gate 	/*
8197c478bd9Sstevel@tonic-gate 	 * Hash instance list to devnames structure of major.
8207c478bd9Sstevel@tonic-gate 	 * Note that if there is not a valid major number for the
8217c478bd9Sstevel@tonic-gate 	 * node, in_hashdrv will put it back on the no_major list.
8227c478bd9Sstevel@tonic-gate 	 */
8237c478bd9Sstevel@tonic-gate 	dp = e_ddi_inst_state.ins_no_major;
8247c478bd9Sstevel@tonic-gate 	e_ddi_inst_state.ins_no_major = NULL;
8257c478bd9Sstevel@tonic-gate 	while (dp) {
8267c478bd9Sstevel@tonic-gate 		ndp = dp->ind_next;
8277c478bd9Sstevel@tonic-gate 		ASSERT(dp->ind_state != IN_UNKNOWN);
8287c478bd9Sstevel@tonic-gate 		dp->ind_next = NULL;
8297c478bd9Sstevel@tonic-gate 		in_hashdrv(dp);
8307c478bd9Sstevel@tonic-gate 		dp = ndp;
8317c478bd9Sstevel@tonic-gate 	}
8327c478bd9Sstevel@tonic-gate 	e_ddi_exit_instance();
8337c478bd9Sstevel@tonic-gate }
8347c478bd9Sstevel@tonic-gate 
8357c478bd9Sstevel@tonic-gate static void
8367c478bd9Sstevel@tonic-gate in_removenode(struct devnames *dnp, in_node_t *mp, in_node_t *ap)
8377c478bd9Sstevel@tonic-gate {
8387c478bd9Sstevel@tonic-gate 	in_node_t *np;
8397c478bd9Sstevel@tonic-gate 
8407c478bd9Sstevel@tonic-gate 	ASSERT(e_ddi_inst_state.ins_busy);
8417c478bd9Sstevel@tonic-gate 	/*
8427c478bd9Sstevel@tonic-gate 	 * Assertion: parents are always instantiated by the framework
8437c478bd9Sstevel@tonic-gate 	 * before their children, destroyed after them
8447c478bd9Sstevel@tonic-gate 	 */
8457c478bd9Sstevel@tonic-gate 	ASSERT(mp->in_child == NULL);
8467c478bd9Sstevel@tonic-gate 	/*
8477c478bd9Sstevel@tonic-gate 	 * Assertion: drv entries are always removed before their owning nodes
8487c478bd9Sstevel@tonic-gate 	 */
8497c478bd9Sstevel@tonic-gate 	ASSERT(mp->in_drivers == NULL);
8507c478bd9Sstevel@tonic-gate 	/*
8517c478bd9Sstevel@tonic-gate 	 * Take the node out of the tree
8527c478bd9Sstevel@tonic-gate 	 */
8537c478bd9Sstevel@tonic-gate 	if (ap->in_child == mp) {
8547c478bd9Sstevel@tonic-gate 		ap->in_child = mp->in_sibling;
8557c478bd9Sstevel@tonic-gate 		in_dealloc_node(mp);
8567c478bd9Sstevel@tonic-gate 		return;
8577c478bd9Sstevel@tonic-gate 	} else {
8587c478bd9Sstevel@tonic-gate 		for (np = ap->in_child; np; np = np->in_sibling) {
8597c478bd9Sstevel@tonic-gate 			if (np->in_sibling == mp) {
8607c478bd9Sstevel@tonic-gate 				np->in_sibling = mp->in_sibling;
8617c478bd9Sstevel@tonic-gate 				in_dealloc_node(mp);
8627c478bd9Sstevel@tonic-gate 				return;
8637c478bd9Sstevel@tonic-gate 			}
8647c478bd9Sstevel@tonic-gate 		}
8657c478bd9Sstevel@tonic-gate 	}
8667c478bd9Sstevel@tonic-gate 	panic("in_removenode dnp %p mp %p", (void *)dnp, (void *)mp);
8677c478bd9Sstevel@tonic-gate }
8687c478bd9Sstevel@tonic-gate 
8697c478bd9Sstevel@tonic-gate /*
8707c478bd9Sstevel@tonic-gate  * Recursive ascent
8717c478bd9Sstevel@tonic-gate  *
8727c478bd9Sstevel@tonic-gate  * This now only does half the job.  It finds the node, then the caller
8737c478bd9Sstevel@tonic-gate  * has to search the node for the binding name
8747c478bd9Sstevel@tonic-gate  */
8757c478bd9Sstevel@tonic-gate static in_node_t *
8767c478bd9Sstevel@tonic-gate in_devwalk(dev_info_t *dip, in_node_t **ap, char *addr)
8777c478bd9Sstevel@tonic-gate {
8787c478bd9Sstevel@tonic-gate 	in_node_t *np;
8797c478bd9Sstevel@tonic-gate 	char *name;
8807c478bd9Sstevel@tonic-gate 
8817c478bd9Sstevel@tonic-gate 	ASSERT(dip);
8827c478bd9Sstevel@tonic-gate 	ASSERT(e_ddi_inst_state.ins_busy);
8837c478bd9Sstevel@tonic-gate 	if (dip == ddi_root_node()) {
8847c478bd9Sstevel@tonic-gate 		*ap = NULL;
8857c478bd9Sstevel@tonic-gate 		return (e_ddi_inst_state.ins_root);
8867c478bd9Sstevel@tonic-gate 	}
8877c478bd9Sstevel@tonic-gate 	/*
8887c478bd9Sstevel@tonic-gate 	 * call up to find parent, then look through the list of kids
8897c478bd9Sstevel@tonic-gate 	 * for a match
8907c478bd9Sstevel@tonic-gate 	 */
8917c478bd9Sstevel@tonic-gate 	np = in_devwalk(ddi_get_parent(dip), ap, NULL);
8927c478bd9Sstevel@tonic-gate 	if (np == NULL)
8937c478bd9Sstevel@tonic-gate 		return (np);
8947c478bd9Sstevel@tonic-gate 	*ap = np;
8957c478bd9Sstevel@tonic-gate 	np = np->in_child;
8967c478bd9Sstevel@tonic-gate 	name = ddi_node_name(dip);
8977c478bd9Sstevel@tonic-gate 	if (addr == NULL)
8987c478bd9Sstevel@tonic-gate 		addr = ddi_get_name_addr(dip);
8997c478bd9Sstevel@tonic-gate 
9007c478bd9Sstevel@tonic-gate 	while (np) {
9017c478bd9Sstevel@tonic-gate 		if (in_eqstr(np->in_node_name, name) &&
9027c478bd9Sstevel@tonic-gate 		    in_eqstr(np->in_unit_addr, addr)) {
9037c478bd9Sstevel@tonic-gate 			return (np);
9047c478bd9Sstevel@tonic-gate 		}
9057c478bd9Sstevel@tonic-gate 		np = np->in_sibling;
9067c478bd9Sstevel@tonic-gate 	}
9077c478bd9Sstevel@tonic-gate 	return (np);
9087c478bd9Sstevel@tonic-gate }
9097c478bd9Sstevel@tonic-gate 
9107c478bd9Sstevel@tonic-gate /*
9117c478bd9Sstevel@tonic-gate  * Create a node specified by cp and assign it the given instance no.
9127c478bd9Sstevel@tonic-gate  */
9137c478bd9Sstevel@tonic-gate static int
9147c478bd9Sstevel@tonic-gate in_pathin(char *cp, int instance, char *bname, struct bind **args)
9157c478bd9Sstevel@tonic-gate {
9167c478bd9Sstevel@tonic-gate 	in_node_t *np;
9177c478bd9Sstevel@tonic-gate 	in_drv_t *dp;
9187c478bd9Sstevel@tonic-gate 	char *name;
9197c478bd9Sstevel@tonic-gate 
9207c478bd9Sstevel@tonic-gate 	ASSERT(e_ddi_inst_state.ins_busy);
9217c478bd9Sstevel@tonic-gate 	ASSERT(args == NULL);
9227c478bd9Sstevel@tonic-gate 
9237c478bd9Sstevel@tonic-gate 	/*
9247c478bd9Sstevel@tonic-gate 	 * Give a warning to the console.
9257c478bd9Sstevel@tonic-gate 	 * return value ignored
9267c478bd9Sstevel@tonic-gate 	 */
9277c478bd9Sstevel@tonic-gate 	if (cp[0] != '/' || instance == -1 || bname == NULL) {
9287c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN,
9297c478bd9Sstevel@tonic-gate 		    "invalid instance file entry %s %d",
9307c478bd9Sstevel@tonic-gate 		    cp, instance);
9317c478bd9Sstevel@tonic-gate 
9327c478bd9Sstevel@tonic-gate 		return (0);
9337c478bd9Sstevel@tonic-gate 	}
9347c478bd9Sstevel@tonic-gate 
9357c478bd9Sstevel@tonic-gate 	if ((name  = i_binding_to_drv_name(bname)) != NULL)
9367c478bd9Sstevel@tonic-gate 		bname = name;
9377c478bd9Sstevel@tonic-gate 
9387c478bd9Sstevel@tonic-gate 	np = in_make_path(cp);
9397c478bd9Sstevel@tonic-gate 	ASSERT(np);
9407c478bd9Sstevel@tonic-gate 	if (in_inuse(instance, bname)) {
9417c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN,
9427c478bd9Sstevel@tonic-gate 		    "instance already in use: %s %d", cp, instance);
9437c478bd9Sstevel@tonic-gate 		return (0);
9447c478bd9Sstevel@tonic-gate 	}
9457c478bd9Sstevel@tonic-gate 	dp = in_drvwalk(np, bname);
9467c478bd9Sstevel@tonic-gate 	if (dp != NULL) {
9477c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN,
9487c478bd9Sstevel@tonic-gate 		    "multiple instance number assignments for "
9497c478bd9Sstevel@tonic-gate 		    "'%s' (driver %s), %d used",
9507c478bd9Sstevel@tonic-gate 		    cp, bname, dp->ind_instance);
9517c478bd9Sstevel@tonic-gate 		return (0);
9527c478bd9Sstevel@tonic-gate 	}
9537c478bd9Sstevel@tonic-gate 	dp = in_alloc_drv(bname);
9547c478bd9Sstevel@tonic-gate 	in_endrv(np, dp);
9557c478bd9Sstevel@tonic-gate 	dp->ind_instance = instance;
9567c478bd9Sstevel@tonic-gate 	dp->ind_state = IN_PERMANENT;
9577c478bd9Sstevel@tonic-gate 	in_hashdrv(dp);
9587c478bd9Sstevel@tonic-gate 
9597c478bd9Sstevel@tonic-gate 	return (0);
9607c478bd9Sstevel@tonic-gate }
9617c478bd9Sstevel@tonic-gate 
9627c478bd9Sstevel@tonic-gate /*
9637c478bd9Sstevel@tonic-gate  * Create (or find) the node named by path by recursively descending from the
9647c478bd9Sstevel@tonic-gate  * root's first child (we ignore the root, which is never named)
9657c478bd9Sstevel@tonic-gate  */
9667c478bd9Sstevel@tonic-gate static in_node_t *
9677c478bd9Sstevel@tonic-gate in_make_path(char *path)
9687c478bd9Sstevel@tonic-gate {
9697c478bd9Sstevel@tonic-gate 	in_node_t *ap;		/* ancestor pointer */
9707c478bd9Sstevel@tonic-gate 	in_node_t *np;		/* working node pointer */
9717c478bd9Sstevel@tonic-gate 	in_node_t *rp;		/* return node pointer */
9727c478bd9Sstevel@tonic-gate 	char buf[MAXPATHLEN];	/* copy of string so we can change it */
9737c478bd9Sstevel@tonic-gate 	char *cp, *name, *addr;
9747c478bd9Sstevel@tonic-gate 
9757c478bd9Sstevel@tonic-gate 	ASSERT(e_ddi_inst_state.ins_busy);
9767c478bd9Sstevel@tonic-gate 	if (path == NULL || path[0] != '/')
9777c478bd9Sstevel@tonic-gate 		return (NULL);
9787c478bd9Sstevel@tonic-gate 	(void) snprintf(buf, sizeof (buf), "%s", path);
9797c478bd9Sstevel@tonic-gate 	cp = buf + 1;	/* skip over initial '/' in path */
9807c478bd9Sstevel@tonic-gate 	name = in_name_addr(&cp, &addr);
9817c478bd9Sstevel@tonic-gate 
9827c478bd9Sstevel@tonic-gate 	/*
9837c478bd9Sstevel@tonic-gate 	 * In S9 and earlier releases, the path_to_inst file
9847c478bd9Sstevel@tonic-gate 	 * SunCluster was prepended with "/node@#". This was
9857c478bd9Sstevel@tonic-gate 	 * removed in S10. We skip the prefix if the prefix
9867c478bd9Sstevel@tonic-gate 	 * still exists in /etc/path_to_inst. It is needed for
9877c478bd9Sstevel@tonic-gate 	 * various forms of Solaris upgrade to work properly
9887c478bd9Sstevel@tonic-gate 	 * in the SunCluster environment.
9897c478bd9Sstevel@tonic-gate 	 */
9907c478bd9Sstevel@tonic-gate 	if ((cluster_bootflags & CLUSTER_CONFIGURED) &&
9917c478bd9Sstevel@tonic-gate 	    (strcmp(name, "node") == 0))
9927c478bd9Sstevel@tonic-gate 		name = in_name_addr(&cp, &addr);
9937c478bd9Sstevel@tonic-gate 
9947c478bd9Sstevel@tonic-gate 	ap = e_ddi_inst_state.ins_root;
9957c478bd9Sstevel@tonic-gate 	rp = np = e_ddi_inst_state.ins_root->in_child;
9967c478bd9Sstevel@tonic-gate 	while (name) {
9977c478bd9Sstevel@tonic-gate 		while (name && np) {
9987c478bd9Sstevel@tonic-gate 			if (in_eqstr(name, np->in_node_name) &&
9997c478bd9Sstevel@tonic-gate 			    in_eqstr(addr, np->in_unit_addr)) {
10007c478bd9Sstevel@tonic-gate 				name = in_name_addr(&cp, &addr);
10017c478bd9Sstevel@tonic-gate 				if (name == NULL)
10027c478bd9Sstevel@tonic-gate 					return (np);
10037c478bd9Sstevel@tonic-gate 				ap = np;
10047c478bd9Sstevel@tonic-gate 				np = np->in_child;
10057c478bd9Sstevel@tonic-gate 				continue;
10067c478bd9Sstevel@tonic-gate 			} else {
10077c478bd9Sstevel@tonic-gate 				np = np->in_sibling;
10087c478bd9Sstevel@tonic-gate 			}
10097c478bd9Sstevel@tonic-gate 		}
10107c478bd9Sstevel@tonic-gate 		np = in_alloc_node(name, addr);
10117c478bd9Sstevel@tonic-gate 		in_enlist(ap, np);	/* insert into tree */
10127c478bd9Sstevel@tonic-gate 		rp = np;	/* value to return if we quit */
10137c478bd9Sstevel@tonic-gate 		ap = np;	/* new parent */
10147c478bd9Sstevel@tonic-gate 		np = NULL;	/* can have no children */
10157c478bd9Sstevel@tonic-gate 		name = in_name_addr(&cp, &addr);
10167c478bd9Sstevel@tonic-gate 	}
10177c478bd9Sstevel@tonic-gate 	return (rp);
10187c478bd9Sstevel@tonic-gate }
10197c478bd9Sstevel@tonic-gate 
10207c478bd9Sstevel@tonic-gate /*
10217c478bd9Sstevel@tonic-gate  * Insert node np into the tree as one of ap's children.
10227c478bd9Sstevel@tonic-gate  */
10237c478bd9Sstevel@tonic-gate static void
10247c478bd9Sstevel@tonic-gate in_enlist(in_node_t *ap, in_node_t *np)
10257c478bd9Sstevel@tonic-gate {
10267c478bd9Sstevel@tonic-gate 	in_node_t *mp;
10277c478bd9Sstevel@tonic-gate 	ASSERT(e_ddi_inst_state.ins_busy);
10287c478bd9Sstevel@tonic-gate 	/*
10297c478bd9Sstevel@tonic-gate 	 * Make this node some other node's child or child's sibling
10307c478bd9Sstevel@tonic-gate 	 */
10317c478bd9Sstevel@tonic-gate 	ASSERT(ap && np);
10327c478bd9Sstevel@tonic-gate 	if (ap->in_child == NULL) {
10337c478bd9Sstevel@tonic-gate 		ap->in_child = np;
10347c478bd9Sstevel@tonic-gate 	} else {
10357c478bd9Sstevel@tonic-gate 		for (mp = ap->in_child; mp; mp = mp->in_sibling)
10367c478bd9Sstevel@tonic-gate 			if (mp->in_sibling == NULL) {
10377c478bd9Sstevel@tonic-gate 				mp->in_sibling = np;
10387c478bd9Sstevel@tonic-gate 				break;
10397c478bd9Sstevel@tonic-gate 			}
10407c478bd9Sstevel@tonic-gate 	}
10417c478bd9Sstevel@tonic-gate 	np->in_parent = ap;
10427c478bd9Sstevel@tonic-gate }
10437c478bd9Sstevel@tonic-gate 
10447c478bd9Sstevel@tonic-gate /*
10457c478bd9Sstevel@tonic-gate  * Insert drv entry dp onto a node's driver list
10467c478bd9Sstevel@tonic-gate  */
10477c478bd9Sstevel@tonic-gate static void
10487c478bd9Sstevel@tonic-gate in_endrv(in_node_t *np, in_drv_t *dp)
10497c478bd9Sstevel@tonic-gate {
10507c478bd9Sstevel@tonic-gate 	in_drv_t *mp;
10517c478bd9Sstevel@tonic-gate 	ASSERT(e_ddi_inst_state.ins_busy);
10527c478bd9Sstevel@tonic-gate 	ASSERT(np && dp);
10537c478bd9Sstevel@tonic-gate 	mp = np->in_drivers;
10547c478bd9Sstevel@tonic-gate 	np->in_drivers = dp;
10557c478bd9Sstevel@tonic-gate 	dp->ind_next_drv = mp;
10567c478bd9Sstevel@tonic-gate 	dp->ind_node = np;
10577c478bd9Sstevel@tonic-gate }
10587c478bd9Sstevel@tonic-gate 
10597c478bd9Sstevel@tonic-gate /*
10607c478bd9Sstevel@tonic-gate  * Parse the next name out of the path, null terminate it and update cp.
10617c478bd9Sstevel@tonic-gate  * caller has copied string so we can mess with it.
10627c478bd9Sstevel@tonic-gate  * Upon return *cpp points to the next section to be parsed, *addrp points
10637c478bd9Sstevel@tonic-gate  * to the current address substring (or NULL if none) and we return the
10647c478bd9Sstevel@tonic-gate  * current name substring (or NULL if none).  name and address substrings
10657c478bd9Sstevel@tonic-gate  * are null terminated in place.
10667c478bd9Sstevel@tonic-gate  */
10677c478bd9Sstevel@tonic-gate 
10687c478bd9Sstevel@tonic-gate static char *
10697c478bd9Sstevel@tonic-gate in_name_addr(char **cpp, char **addrp)
10707c478bd9Sstevel@tonic-gate {
10717c478bd9Sstevel@tonic-gate 	char *namep;	/* return value holder */
10727c478bd9Sstevel@tonic-gate 	char *ap;	/* pointer to '@' in string */
10737c478bd9Sstevel@tonic-gate 	char *sp;	/* pointer to '/' in string */
10747c478bd9Sstevel@tonic-gate 
10757c478bd9Sstevel@tonic-gate 	if (*cpp == NULL || **cpp == '\0') {
10767c478bd9Sstevel@tonic-gate 		*addrp = NULL;
10777c478bd9Sstevel@tonic-gate 		return (NULL);
10787c478bd9Sstevel@tonic-gate 	}
10797c478bd9Sstevel@tonic-gate 	namep = *cpp;
10807c478bd9Sstevel@tonic-gate 	sp = strchr(*cpp, '/');
10817c478bd9Sstevel@tonic-gate 	if (sp != NULL) {	/* more to follow */
10827c478bd9Sstevel@tonic-gate 		*sp = '\0';
10837c478bd9Sstevel@tonic-gate 		*cpp = sp + 1;
10847c478bd9Sstevel@tonic-gate 	} else {		/* this is last component. */
10857c478bd9Sstevel@tonic-gate 		*cpp = NULL;
10867c478bd9Sstevel@tonic-gate 	}
10877c478bd9Sstevel@tonic-gate 	ap = strchr(namep, '@');
10887c478bd9Sstevel@tonic-gate 	if (ap == NULL) {
10897c478bd9Sstevel@tonic-gate 		*addrp = NULL;
10907c478bd9Sstevel@tonic-gate 	} else {
10917c478bd9Sstevel@tonic-gate 		*ap = '\0';		/* terminate the name */
10927c478bd9Sstevel@tonic-gate 		*addrp = ap + 1;
10937c478bd9Sstevel@tonic-gate 	}
10947c478bd9Sstevel@tonic-gate 	return (namep);
10957c478bd9Sstevel@tonic-gate }
10967c478bd9Sstevel@tonic-gate 
10977c478bd9Sstevel@tonic-gate /*
10987c478bd9Sstevel@tonic-gate  * Allocate a node and storage for name and addr strings, and fill them in.
10997c478bd9Sstevel@tonic-gate  */
11007c478bd9Sstevel@tonic-gate static in_node_t *
11017c478bd9Sstevel@tonic-gate in_alloc_node(char *name, char *addr)
11027c478bd9Sstevel@tonic-gate {
11037c478bd9Sstevel@tonic-gate 	in_node_t *np;
11047c478bd9Sstevel@tonic-gate 	char *cp;
11057c478bd9Sstevel@tonic-gate 	size_t namelen;
11067c478bd9Sstevel@tonic-gate 
11077c478bd9Sstevel@tonic-gate 	ASSERT(e_ddi_inst_state.ins_busy);
11087c478bd9Sstevel@tonic-gate 	/*
11097c478bd9Sstevel@tonic-gate 	 * Has name or will become root
11107c478bd9Sstevel@tonic-gate 	 */
11117c478bd9Sstevel@tonic-gate 	ASSERT(name || e_ddi_inst_state.ins_root == NULL);
11127c478bd9Sstevel@tonic-gate 	if (addr == NULL)
11137c478bd9Sstevel@tonic-gate 		addr = "";
11147c478bd9Sstevel@tonic-gate 	if (name == NULL)
11157c478bd9Sstevel@tonic-gate 		namelen = 0;
11167c478bd9Sstevel@tonic-gate 	else
11177c478bd9Sstevel@tonic-gate 		namelen = strlen(name) + 1;
11187c478bd9Sstevel@tonic-gate 	cp = kmem_zalloc(sizeof (in_node_t) + namelen + strlen(addr) + 1,
11197c478bd9Sstevel@tonic-gate 	    KM_SLEEP);
11207c478bd9Sstevel@tonic-gate 	np = (in_node_t *)cp;
11217c478bd9Sstevel@tonic-gate 	if (name) {
11227c478bd9Sstevel@tonic-gate 		np->in_node_name = cp + sizeof (in_node_t);
11237c478bd9Sstevel@tonic-gate 		(void) strcpy(np->in_node_name, name);
11247c478bd9Sstevel@tonic-gate 	}
11257c478bd9Sstevel@tonic-gate 	np->in_unit_addr = cp + sizeof (in_node_t) + namelen;
11267c478bd9Sstevel@tonic-gate 	(void) strcpy(np->in_unit_addr, addr);
11277c478bd9Sstevel@tonic-gate 	return (np);
11287c478bd9Sstevel@tonic-gate }
11297c478bd9Sstevel@tonic-gate 
11307c478bd9Sstevel@tonic-gate /*
11317c478bd9Sstevel@tonic-gate  * Allocate a drv entry and storage for binding name string, and fill it in.
11327c478bd9Sstevel@tonic-gate  */
11337c478bd9Sstevel@tonic-gate static in_drv_t *
11347c478bd9Sstevel@tonic-gate in_alloc_drv(char *bindingname)
11357c478bd9Sstevel@tonic-gate {
11367c478bd9Sstevel@tonic-gate 	in_drv_t *dp;
11377c478bd9Sstevel@tonic-gate 	char *cp;
11387c478bd9Sstevel@tonic-gate 	size_t namelen;
11397c478bd9Sstevel@tonic-gate 
11407c478bd9Sstevel@tonic-gate 	ASSERT(e_ddi_inst_state.ins_busy);
11417c478bd9Sstevel@tonic-gate 	/*
11427c478bd9Sstevel@tonic-gate 	 * Has name or will become root
11437c478bd9Sstevel@tonic-gate 	 */
11447c478bd9Sstevel@tonic-gate 	ASSERT(bindingname || e_ddi_inst_state.ins_root == NULL);
11457c478bd9Sstevel@tonic-gate 	if (bindingname == NULL)
11467c478bd9Sstevel@tonic-gate 		namelen = 0;
11477c478bd9Sstevel@tonic-gate 	else
11487c478bd9Sstevel@tonic-gate 		namelen = strlen(bindingname) + 1;
11497c478bd9Sstevel@tonic-gate 	cp = kmem_zalloc(sizeof (in_drv_t) + namelen, KM_SLEEP);
11507c478bd9Sstevel@tonic-gate 	dp = (in_drv_t *)cp;
11517c478bd9Sstevel@tonic-gate 	if (bindingname) {
11527c478bd9Sstevel@tonic-gate 		dp->ind_driver_name = cp + sizeof (in_drv_t);
11537c478bd9Sstevel@tonic-gate 		(void) strcpy(dp->ind_driver_name, bindingname);
11547c478bd9Sstevel@tonic-gate 	}
11557c478bd9Sstevel@tonic-gate 	dp->ind_state = IN_UNKNOWN;
11567c478bd9Sstevel@tonic-gate 	dp->ind_instance = -1;
11577c478bd9Sstevel@tonic-gate 	return (dp);
11587c478bd9Sstevel@tonic-gate }
11597c478bd9Sstevel@tonic-gate 
11607c478bd9Sstevel@tonic-gate static void
11617c478bd9Sstevel@tonic-gate in_dealloc_node(in_node_t *np)
11627c478bd9Sstevel@tonic-gate {
11637c478bd9Sstevel@tonic-gate 	/*
11647c478bd9Sstevel@tonic-gate 	 * The root node can never be de-allocated
11657c478bd9Sstevel@tonic-gate 	 */
11667c478bd9Sstevel@tonic-gate 	ASSERT(np->in_node_name && np->in_unit_addr);
11677c478bd9Sstevel@tonic-gate 	ASSERT(e_ddi_inst_state.ins_busy);
11687c478bd9Sstevel@tonic-gate 	kmem_free(np, sizeof (in_node_t) + strlen(np->in_node_name)
11697c478bd9Sstevel@tonic-gate 	    + strlen(np->in_unit_addr) + 2);
11707c478bd9Sstevel@tonic-gate }
11717c478bd9Sstevel@tonic-gate 
11727c478bd9Sstevel@tonic-gate static void
11737c478bd9Sstevel@tonic-gate in_dealloc_drv(in_drv_t *dp)
11747c478bd9Sstevel@tonic-gate {
11757c478bd9Sstevel@tonic-gate 	ASSERT(dp->ind_driver_name);
11767c478bd9Sstevel@tonic-gate 	ASSERT(e_ddi_inst_state.ins_busy);
11777c478bd9Sstevel@tonic-gate 	kmem_free(dp, sizeof (in_drv_t) + strlen(dp->ind_driver_name)
11787c478bd9Sstevel@tonic-gate 	    + 1);
11797c478bd9Sstevel@tonic-gate }
11807c478bd9Sstevel@tonic-gate 
11817c478bd9Sstevel@tonic-gate /*
11827c478bd9Sstevel@tonic-gate  * Handle the various possible versions of "no address"
11837c478bd9Sstevel@tonic-gate  */
11847c478bd9Sstevel@tonic-gate static int
11857c478bd9Sstevel@tonic-gate in_eqstr(char *a, char *b)
11867c478bd9Sstevel@tonic-gate {
11877c478bd9Sstevel@tonic-gate 	if (a == b)	/* covers case where both are nulls */
11887c478bd9Sstevel@tonic-gate 		return (1);
11897c478bd9Sstevel@tonic-gate 	if (a == NULL && *b == 0)
11907c478bd9Sstevel@tonic-gate 		return (1);
11917c478bd9Sstevel@tonic-gate 	if (b == NULL && *a == 0)
11927c478bd9Sstevel@tonic-gate 		return (1);
11937c478bd9Sstevel@tonic-gate 	if (a == NULL || b == NULL)
11947c478bd9Sstevel@tonic-gate 		return (0);
11957c478bd9Sstevel@tonic-gate 	return (strcmp(a, b) == 0);
11967c478bd9Sstevel@tonic-gate }
11977c478bd9Sstevel@tonic-gate 
11987c478bd9Sstevel@tonic-gate /*
11997c478bd9Sstevel@tonic-gate  * Returns true if instance no. is already in use by named driver
12007c478bd9Sstevel@tonic-gate  */
12017c478bd9Sstevel@tonic-gate static int
12027c478bd9Sstevel@tonic-gate in_inuse(int instance, char *name)
12037c478bd9Sstevel@tonic-gate {
12047c478bd9Sstevel@tonic-gate 	major_t major;
12057c478bd9Sstevel@tonic-gate 	in_drv_t *dp;
12067c478bd9Sstevel@tonic-gate 	struct devnames *dnp;
12077c478bd9Sstevel@tonic-gate 
12087c478bd9Sstevel@tonic-gate 	ASSERT(e_ddi_inst_state.ins_busy);
12097c478bd9Sstevel@tonic-gate 	/*
12107c478bd9Sstevel@tonic-gate 	 * For now, if we've never heard of this device we assume it is not
12117c478bd9Sstevel@tonic-gate 	 * in use, since we can't tell
12127c478bd9Sstevel@tonic-gate 	 * XXX could do the weaker search through the nomajor list checking
12137c478bd9Sstevel@tonic-gate 	 * XXX for the same name
12147c478bd9Sstevel@tonic-gate 	 */
1215a204de77Scth 	if ((major = ddi_name_to_major(name)) == DDI_MAJOR_T_NONE)
12167c478bd9Sstevel@tonic-gate 		return (0);
12177c478bd9Sstevel@tonic-gate 	dnp = &devnamesp[major];
12187c478bd9Sstevel@tonic-gate 
12197c478bd9Sstevel@tonic-gate 	dp = dnp->dn_inlist;
12207c478bd9Sstevel@tonic-gate 	while (dp) {
12217c478bd9Sstevel@tonic-gate 		if (dp->ind_instance == instance)
12227c478bd9Sstevel@tonic-gate 			return (1);
12237c478bd9Sstevel@tonic-gate 		dp = dp->ind_next;
12247c478bd9Sstevel@tonic-gate 	}
12257c478bd9Sstevel@tonic-gate 	return (0);
12267c478bd9Sstevel@tonic-gate }
12277c478bd9Sstevel@tonic-gate 
12287c478bd9Sstevel@tonic-gate static void
12297c478bd9Sstevel@tonic-gate in_hashdrv(in_drv_t *dp)
12307c478bd9Sstevel@tonic-gate {
12317c478bd9Sstevel@tonic-gate 	struct devnames *dnp;
12327c478bd9Sstevel@tonic-gate 	in_drv_t *mp, *pp;
12337c478bd9Sstevel@tonic-gate 	major_t major;
12347c478bd9Sstevel@tonic-gate 
12357c478bd9Sstevel@tonic-gate 	/* hash to no major list */
1236a204de77Scth 	major = ddi_name_to_major(dp->ind_driver_name);
1237a204de77Scth 	if (major == DDI_MAJOR_T_NONE) {
12387c478bd9Sstevel@tonic-gate 		dp->ind_next = e_ddi_inst_state.ins_no_major;
12397c478bd9Sstevel@tonic-gate 		e_ddi_inst_state.ins_no_major = dp;
12407c478bd9Sstevel@tonic-gate 		return;
12417c478bd9Sstevel@tonic-gate 	}
12427c478bd9Sstevel@tonic-gate 
12437c478bd9Sstevel@tonic-gate 	/*
12447c478bd9Sstevel@tonic-gate 	 * dnp->dn_inlist is sorted by instance number.
12457c478bd9Sstevel@tonic-gate 	 * Adding a new instance entry may introduce holes,
12467c478bd9Sstevel@tonic-gate 	 * set dn_instance to IN_SEARCHME so the next instance
12477c478bd9Sstevel@tonic-gate 	 * assignment may fill in holes.
12487c478bd9Sstevel@tonic-gate 	 */
12497c478bd9Sstevel@tonic-gate 	dnp = &devnamesp[major];
12507c478bd9Sstevel@tonic-gate 	pp = mp = dnp->dn_inlist;
12517c478bd9Sstevel@tonic-gate 	if (mp == NULL || dp->ind_instance < mp->ind_instance) {
12527c478bd9Sstevel@tonic-gate 		/* prepend as the first entry, turn on IN_SEARCHME */
12537c478bd9Sstevel@tonic-gate 		dnp->dn_instance = IN_SEARCHME;
12547c478bd9Sstevel@tonic-gate 		dp->ind_next = mp;
12557c478bd9Sstevel@tonic-gate 		dnp->dn_inlist = dp;
12567c478bd9Sstevel@tonic-gate 		return;
12577c478bd9Sstevel@tonic-gate 	}
12587c478bd9Sstevel@tonic-gate 
12597c478bd9Sstevel@tonic-gate 	ASSERT(mp->ind_instance != dp->ind_instance);
12607c478bd9Sstevel@tonic-gate 	while (mp->ind_instance < dp->ind_instance && mp->ind_next) {
12617c478bd9Sstevel@tonic-gate 		pp = mp;
12627c478bd9Sstevel@tonic-gate 		mp = mp->ind_next;
12637c478bd9Sstevel@tonic-gate 		ASSERT(mp->ind_instance != dp->ind_instance);
12647c478bd9Sstevel@tonic-gate 	}
12657c478bd9Sstevel@tonic-gate 
12667c478bd9Sstevel@tonic-gate 	if (mp->ind_instance < dp->ind_instance) { /* end of list */
12677c478bd9Sstevel@tonic-gate 		dp->ind_next = NULL;
12687c478bd9Sstevel@tonic-gate 		mp->ind_next = dp;
12697c478bd9Sstevel@tonic-gate 	} else {
12707c478bd9Sstevel@tonic-gate 		ASSERT(dnp->dn_instance == IN_SEARCHME);
12717c478bd9Sstevel@tonic-gate 		dp->ind_next = pp->ind_next;
12727c478bd9Sstevel@tonic-gate 		pp->ind_next = dp;
12737c478bd9Sstevel@tonic-gate 	}
12747c478bd9Sstevel@tonic-gate }
12757c478bd9Sstevel@tonic-gate 
12767c478bd9Sstevel@tonic-gate /*
12777c478bd9Sstevel@tonic-gate  * Remove a driver entry from the list, given a previous pointer
12787c478bd9Sstevel@tonic-gate  */
12797c478bd9Sstevel@tonic-gate static void
12807c478bd9Sstevel@tonic-gate in_removedrv(struct devnames *dnp, in_drv_t *mp)
12817c478bd9Sstevel@tonic-gate {
12827c478bd9Sstevel@tonic-gate 	in_drv_t *dp;
12837c478bd9Sstevel@tonic-gate 	in_drv_t *prevp;
12847c478bd9Sstevel@tonic-gate 
12857c478bd9Sstevel@tonic-gate 	if (dnp->dn_inlist == mp) {	/* head of list */
12867c478bd9Sstevel@tonic-gate 		dnp->dn_inlist = mp->ind_next;
12877c478bd9Sstevel@tonic-gate 		dnp->dn_instance = IN_SEARCHME;
12887c478bd9Sstevel@tonic-gate 		in_dq_drv(mp);
12897c478bd9Sstevel@tonic-gate 		in_dealloc_drv(mp);
12907c478bd9Sstevel@tonic-gate 		return;
12917c478bd9Sstevel@tonic-gate 	}
12927c478bd9Sstevel@tonic-gate 	prevp = dnp->dn_inlist;
12937c478bd9Sstevel@tonic-gate 	for (dp = prevp->ind_next; dp; dp = dp->ind_next) {
12947c478bd9Sstevel@tonic-gate 		if (dp == mp) {		/* found it */
12957c478bd9Sstevel@tonic-gate 			break;
12967c478bd9Sstevel@tonic-gate 		}
12977c478bd9Sstevel@tonic-gate 		prevp = dp;
12987c478bd9Sstevel@tonic-gate 	}
12997c478bd9Sstevel@tonic-gate 
13007c478bd9Sstevel@tonic-gate 	ASSERT(dp == mp);
13017c478bd9Sstevel@tonic-gate 	dnp->dn_instance = IN_SEARCHME;
13027c478bd9Sstevel@tonic-gate 	prevp->ind_next = mp->ind_next;
13037c478bd9Sstevel@tonic-gate 	in_dq_drv(mp);
13047c478bd9Sstevel@tonic-gate 	in_dealloc_drv(mp);
13057c478bd9Sstevel@tonic-gate }
13067c478bd9Sstevel@tonic-gate 
13077c478bd9Sstevel@tonic-gate static void
13087c478bd9Sstevel@tonic-gate in_dq_drv(in_drv_t *mp)
13097c478bd9Sstevel@tonic-gate {
13107c478bd9Sstevel@tonic-gate 	struct in_node *node = mp->ind_node;
13117c478bd9Sstevel@tonic-gate 	in_drv_t *ptr, *prev;
13127c478bd9Sstevel@tonic-gate 
13137c478bd9Sstevel@tonic-gate 	if (mp == node->in_drivers) {
13147c478bd9Sstevel@tonic-gate 		node->in_drivers = mp->ind_next_drv;
13157c478bd9Sstevel@tonic-gate 		return;
13167c478bd9Sstevel@tonic-gate 	}
13177c478bd9Sstevel@tonic-gate 	prev = node->in_drivers;
13187c478bd9Sstevel@tonic-gate 	for (ptr = prev->ind_next_drv; ptr != (struct in_drv *)NULL;
13197c478bd9Sstevel@tonic-gate 	    ptr = ptr->ind_next_drv) {
13207c478bd9Sstevel@tonic-gate 		if (ptr == mp) {
13217c478bd9Sstevel@tonic-gate 			prev->ind_next_drv = ptr->ind_next_drv;
13227c478bd9Sstevel@tonic-gate 			return;
13237c478bd9Sstevel@tonic-gate 		}
1324*ffc89d77Svikram 		prev = ptr;
13257c478bd9Sstevel@tonic-gate 	}
13267c478bd9Sstevel@tonic-gate 	panic("in_dq_drv: in_drv not found on node driver list");
13277c478bd9Sstevel@tonic-gate }
13287c478bd9Sstevel@tonic-gate 
13297c478bd9Sstevel@tonic-gate 
13307c478bd9Sstevel@tonic-gate in_drv_t *
13317c478bd9Sstevel@tonic-gate in_drvwalk(in_node_t *np, char *binding_name)
13327c478bd9Sstevel@tonic-gate {
13337c478bd9Sstevel@tonic-gate 	char *name;
13347c478bd9Sstevel@tonic-gate 	in_drv_t *dp = np->in_drivers;
13357c478bd9Sstevel@tonic-gate 	while (dp) {
13367c478bd9Sstevel@tonic-gate 		if ((name = i_binding_to_drv_name(dp->ind_driver_name))
13377c478bd9Sstevel@tonic-gate 		    == NULL) {
13387c478bd9Sstevel@tonic-gate 			name = dp->ind_driver_name;
13397c478bd9Sstevel@tonic-gate 		}
13407c478bd9Sstevel@tonic-gate 		if (strcmp(binding_name, name) == 0) {
13417c478bd9Sstevel@tonic-gate 			break;
13427c478bd9Sstevel@tonic-gate 		}
13437c478bd9Sstevel@tonic-gate 		dp = dp->ind_next_drv;
13447c478bd9Sstevel@tonic-gate 	}
13457c478bd9Sstevel@tonic-gate 	return (dp);
13467c478bd9Sstevel@tonic-gate }
13477c478bd9Sstevel@tonic-gate 
13487c478bd9Sstevel@tonic-gate 
13497c478bd9Sstevel@tonic-gate 
13507c478bd9Sstevel@tonic-gate static void
13517c478bd9Sstevel@tonic-gate i_log_devfs_instance_mod(void)
13527c478bd9Sstevel@tonic-gate {
13537c478bd9Sstevel@tonic-gate 	sysevent_t	*ev;
13547c478bd9Sstevel@tonic-gate 	sysevent_id_t	eid;
135516bd7258Scth 	static int	sent_one = 0;
13567c478bd9Sstevel@tonic-gate 
13577c478bd9Sstevel@tonic-gate 	/*
135816bd7258Scth 	 * Prevent unnecessary event generation.  Do not generate more than
135916bd7258Scth 	 * one event during boot.
13607c478bd9Sstevel@tonic-gate 	 */
136116bd7258Scth 	if (sent_one && !i_ddi_io_initialized())
13627c478bd9Sstevel@tonic-gate 		return;
13637c478bd9Sstevel@tonic-gate 
13647c478bd9Sstevel@tonic-gate 	ev = sysevent_alloc(EC_DEVFS, ESC_DEVFS_INSTANCE_MOD, EP_DDI,
13657c478bd9Sstevel@tonic-gate 	    SE_NOSLEEP);
13667c478bd9Sstevel@tonic-gate 	if (ev == NULL) {
13677c478bd9Sstevel@tonic-gate 		return;
13687c478bd9Sstevel@tonic-gate 	}
13697c478bd9Sstevel@tonic-gate 	if (log_sysevent(ev, SE_NOSLEEP, &eid) != 0) {
13707c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "i_log_devfs_instance_mod: failed to post "
13717c478bd9Sstevel@tonic-gate 		    "event");
137216bd7258Scth 	} else {
137316bd7258Scth 		sent_one = 1;
13747c478bd9Sstevel@tonic-gate 	}
13757c478bd9Sstevel@tonic-gate 	sysevent_free(ev);
13767c478bd9Sstevel@tonic-gate }
13777c478bd9Sstevel@tonic-gate 
13787c478bd9Sstevel@tonic-gate void
13797c478bd9Sstevel@tonic-gate e_ddi_enter_instance()
13807c478bd9Sstevel@tonic-gate {
13817c478bd9Sstevel@tonic-gate 	mutex_enter(&e_ddi_inst_state.ins_serial);
13827c478bd9Sstevel@tonic-gate 	if (e_ddi_inst_state.ins_thread == curthread)
13837c478bd9Sstevel@tonic-gate 		e_ddi_inst_state.ins_busy++;
13847c478bd9Sstevel@tonic-gate 	else {
13857c478bd9Sstevel@tonic-gate 		while (e_ddi_inst_state.ins_busy)
13867c478bd9Sstevel@tonic-gate 			cv_wait(&e_ddi_inst_state.ins_serial_cv,
13877c478bd9Sstevel@tonic-gate 			    &e_ddi_inst_state.ins_serial);
13887c478bd9Sstevel@tonic-gate 		e_ddi_inst_state.ins_thread = curthread;
13897c478bd9Sstevel@tonic-gate 		e_ddi_inst_state.ins_busy = 1;
13907c478bd9Sstevel@tonic-gate 	}
13917c478bd9Sstevel@tonic-gate 	mutex_exit(&e_ddi_inst_state.ins_serial);
13927c478bd9Sstevel@tonic-gate }
13937c478bd9Sstevel@tonic-gate 
13947c478bd9Sstevel@tonic-gate void
13957c478bd9Sstevel@tonic-gate e_ddi_exit_instance()
13967c478bd9Sstevel@tonic-gate {
13977c478bd9Sstevel@tonic-gate 	mutex_enter(&e_ddi_inst_state.ins_serial);
13987c478bd9Sstevel@tonic-gate 	e_ddi_inst_state.ins_busy--;
13997c478bd9Sstevel@tonic-gate 	if (e_ddi_inst_state.ins_busy == 0) {
14007c478bd9Sstevel@tonic-gate 		cv_broadcast(&e_ddi_inst_state.ins_serial_cv);
14017c478bd9Sstevel@tonic-gate 		e_ddi_inst_state.ins_thread = NULL;
14027c478bd9Sstevel@tonic-gate 	}
14037c478bd9Sstevel@tonic-gate 	mutex_exit(&e_ddi_inst_state.ins_serial);
14047c478bd9Sstevel@tonic-gate }
14057c478bd9Sstevel@tonic-gate 
14067c478bd9Sstevel@tonic-gate int
14077c478bd9Sstevel@tonic-gate e_ddi_instance_is_clean()
14087c478bd9Sstevel@tonic-gate {
14097c478bd9Sstevel@tonic-gate 	return (e_ddi_inst_state.ins_dirty == 0);
14107c478bd9Sstevel@tonic-gate }
14117c478bd9Sstevel@tonic-gate 
14127c478bd9Sstevel@tonic-gate void
14137c478bd9Sstevel@tonic-gate e_ddi_instance_set_clean()
14147c478bd9Sstevel@tonic-gate {
14157c478bd9Sstevel@tonic-gate 	e_ddi_inst_state.ins_dirty = 0;
14167c478bd9Sstevel@tonic-gate }
14177c478bd9Sstevel@tonic-gate 
14187c478bd9Sstevel@tonic-gate in_node_t *
14197c478bd9Sstevel@tonic-gate e_ddi_instance_root()
14207c478bd9Sstevel@tonic-gate {
14217c478bd9Sstevel@tonic-gate 	return (e_ddi_inst_state.ins_root);
14227c478bd9Sstevel@tonic-gate }
14237c478bd9Sstevel@tonic-gate 
14247c478bd9Sstevel@tonic-gate /*
14257c478bd9Sstevel@tonic-gate  * Visit a node in the instance tree
14267c478bd9Sstevel@tonic-gate  */
14277c478bd9Sstevel@tonic-gate static int
14287c478bd9Sstevel@tonic-gate in_walk_instances(in_node_t *np, char *path, char *this,
14297c478bd9Sstevel@tonic-gate     int (*f)(const char *, in_node_t *, in_drv_t *, void *), void *arg)
14307c478bd9Sstevel@tonic-gate {
14317c478bd9Sstevel@tonic-gate 	in_drv_t *dp;
14327c478bd9Sstevel@tonic-gate 	int rval = INST_WALK_CONTINUE;
14337c478bd9Sstevel@tonic-gate 	char *next;
14347c478bd9Sstevel@tonic-gate 
14357c478bd9Sstevel@tonic-gate 	while (np != NULL) {
14367c478bd9Sstevel@tonic-gate 
14377c478bd9Sstevel@tonic-gate 		if (np->in_unit_addr[0] == 0)
14387c478bd9Sstevel@tonic-gate 			(void) sprintf(this, "/%s", np->in_node_name);
14397c478bd9Sstevel@tonic-gate 		else
14407c478bd9Sstevel@tonic-gate 			(void) sprintf(this, "/%s@%s", np->in_node_name,
14417c478bd9Sstevel@tonic-gate 			    np->in_unit_addr);
14427c478bd9Sstevel@tonic-gate 		next = this + strlen(this);
14437c478bd9Sstevel@tonic-gate 
14447c478bd9Sstevel@tonic-gate 		for (dp = np->in_drivers; dp; dp = dp->ind_next_drv) {
14457c478bd9Sstevel@tonic-gate 			if (dp->ind_state == IN_PERMANENT) {
14467c478bd9Sstevel@tonic-gate 				rval = (*f)(path, np, dp, arg);
14477c478bd9Sstevel@tonic-gate 				if (rval == INST_WALK_TERMINATE)
14487c478bd9Sstevel@tonic-gate 					break;
14497c478bd9Sstevel@tonic-gate 			}
14507c478bd9Sstevel@tonic-gate 		}
14517c478bd9Sstevel@tonic-gate 		if (np->in_child) {
14527c478bd9Sstevel@tonic-gate 			rval = in_walk_instances(np->in_child,
14537c478bd9Sstevel@tonic-gate 			    path, next, f, arg);
14547c478bd9Sstevel@tonic-gate 			if (rval == INST_WALK_TERMINATE)
14557c478bd9Sstevel@tonic-gate 				break;
14567c478bd9Sstevel@tonic-gate 		}
14577c478bd9Sstevel@tonic-gate 
14587c478bd9Sstevel@tonic-gate 		np = np->in_sibling;
14597c478bd9Sstevel@tonic-gate 	}
14607c478bd9Sstevel@tonic-gate 
14617c478bd9Sstevel@tonic-gate 	return (rval);
14627c478bd9Sstevel@tonic-gate }
14637c478bd9Sstevel@tonic-gate 
14647c478bd9Sstevel@tonic-gate /*
14657c478bd9Sstevel@tonic-gate  * A general interface for walking the instance tree,
14667c478bd9Sstevel@tonic-gate  * calling a user-supplied callback for each node.
14677c478bd9Sstevel@tonic-gate  */
14687c478bd9Sstevel@tonic-gate int
14697c478bd9Sstevel@tonic-gate e_ddi_walk_instances(int (*f)(const char *,
14707c478bd9Sstevel@tonic-gate 	in_node_t *, in_drv_t *, void *), void *arg)
14717c478bd9Sstevel@tonic-gate {
14727c478bd9Sstevel@tonic-gate 	in_node_t *root;
14737c478bd9Sstevel@tonic-gate 	int rval;
14747c478bd9Sstevel@tonic-gate 	char *path;
14757c478bd9Sstevel@tonic-gate 
14767c478bd9Sstevel@tonic-gate 	path = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
14777c478bd9Sstevel@tonic-gate 
14787c478bd9Sstevel@tonic-gate 	e_ddi_enter_instance();
14797c478bd9Sstevel@tonic-gate 	root = e_ddi_instance_root();
14807c478bd9Sstevel@tonic-gate 	rval = in_walk_instances(root->in_child, path, path, f, arg);
14817c478bd9Sstevel@tonic-gate 	e_ddi_exit_instance();
14827c478bd9Sstevel@tonic-gate 
14837c478bd9Sstevel@tonic-gate 	kmem_free(path, MAXPATHLEN);
14847c478bd9Sstevel@tonic-gate 	return (rval);
14857c478bd9Sstevel@tonic-gate }
1486