xref: /titanic_50/usr/src/uts/common/os/instance.c (revision f1ebe3a2bf61bc66e875e9906aa69d023c8e10fe)
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 /*
2294c894bbSVikram Hegde  * Copyright (c) 1992, 2010, Oracle and/or its affiliates. All rights reserved.
237c478bd9Sstevel@tonic-gate  */
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate /*
267c478bd9Sstevel@tonic-gate  * Instance number assignment code
277c478bd9Sstevel@tonic-gate  */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <sys/types.h>
307c478bd9Sstevel@tonic-gate #include <sys/param.h>
317c478bd9Sstevel@tonic-gate #include <sys/errno.h>
327c478bd9Sstevel@tonic-gate #include <sys/systm.h>
337c478bd9Sstevel@tonic-gate #include <sys/kobj.h>
347c478bd9Sstevel@tonic-gate #include <sys/t_lock.h>
357c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
367c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
377c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
387c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
397c478bd9Sstevel@tonic-gate #include <sys/autoconf.h>
407c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h>
417c478bd9Sstevel@tonic-gate #include <sys/hwconf.h>
427c478bd9Sstevel@tonic-gate #include <sys/reboot.h>
437c478bd9Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
447c478bd9Sstevel@tonic-gate #include <sys/instance.h>
457c478bd9Sstevel@tonic-gate #include <sys/debug.h>
467c478bd9Sstevel@tonic-gate #include <sys/sysevent.h>
477c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
487c478bd9Sstevel@tonic-gate #include <sys/console.h>
497c478bd9Sstevel@tonic-gate #include <sys/cladm.h>
5094c894bbSVikram Hegde #include <sys/sysmacros.h>
5194c894bbSVikram Hegde #include <sys/crc32.h>
5294c894bbSVikram Hegde 
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 
7694c894bbSVikram Hegde #pragma weak plat_ioaliases_init
7794c894bbSVikram Hegde 
7894c894bbSVikram Hegde 
797c478bd9Sstevel@tonic-gate /* external functions */
807c478bd9Sstevel@tonic-gate extern char *i_binding_to_drv_name(char *bname);
8194c894bbSVikram Hegde extern void plat_ioaliases_init(void);
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate /*
847c478bd9Sstevel@tonic-gate  * This plus devnames defines the entire software state of the instance world.
857c478bd9Sstevel@tonic-gate  */
867c478bd9Sstevel@tonic-gate typedef struct in_softstate {
877c478bd9Sstevel@tonic-gate 	in_node_t	*ins_root;	/* the root of our instance tree */
887c478bd9Sstevel@tonic-gate 	in_drv_t	*ins_no_major;	/* majorless drv entries */
897c478bd9Sstevel@tonic-gate 	/*
907c478bd9Sstevel@tonic-gate 	 * Used to serialize access to data structures
917c478bd9Sstevel@tonic-gate 	 */
927c478bd9Sstevel@tonic-gate 	void		*ins_thread;
937c478bd9Sstevel@tonic-gate 	kmutex_t	ins_serial;
947c478bd9Sstevel@tonic-gate 	kcondvar_t	ins_serial_cv;
957c478bd9Sstevel@tonic-gate 	int		ins_busy;
9694c894bbSVikram Hegde 	boolean_t	ins_dirty;	/* instance info needs flush */
977c478bd9Sstevel@tonic-gate } in_softstate_t;
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate static in_softstate_t e_ddi_inst_state;
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate /*
1027c478bd9Sstevel@tonic-gate  * State transition information:
1037c478bd9Sstevel@tonic-gate  * e_ddi_inst_state contains, among other things, the root of a tree of
1047c478bd9Sstevel@tonic-gate  * device nodes used to track instance number assignments.
1057c478bd9Sstevel@tonic-gate  * Each device node may contain multiple driver bindings, represented
1067c478bd9Sstevel@tonic-gate  * by a linked list of in_drv_t nodes, each with an instance assignment
1077c478bd9Sstevel@tonic-gate  * (except for root node). Each in_drv node can be in one of 3 states,
1087c478bd9Sstevel@tonic-gate  * indicated by ind_state:
1097c478bd9Sstevel@tonic-gate  *
1107c478bd9Sstevel@tonic-gate  * IN_UNKNOWN:	Each node created in this state.  The instance number of
1117c478bd9Sstevel@tonic-gate  *	this node is not known.  ind_instance is set to -1.
1127c478bd9Sstevel@tonic-gate  * IN_PROVISIONAL:  When a node is assigned an instance number in
1137c478bd9Sstevel@tonic-gate  *	e_ddi_assign_instance(), its state is set to IN_PROVISIONAL.
1147c478bd9Sstevel@tonic-gate  *	Subsequently, the framework will always call either
11594c894bbSVikram Hegde  *	e_ddi_keep_instance() which makes the node IN_PERMANENT
1167c478bd9Sstevel@tonic-gate  *	or e_ddi_free_instance(), which deletes the node.
1177c478bd9Sstevel@tonic-gate  * IN_PERMANENT:
1187c478bd9Sstevel@tonic-gate  *	If e_ddi_keep_instance() is called on an IN_PROVISIONAL node,
1197c478bd9Sstevel@tonic-gate  *	its state is set to IN_PERMANENT.
1207c478bd9Sstevel@tonic-gate  */
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate static char *instance_file = INSTANCE_FILE;
1237c478bd9Sstevel@tonic-gate static char *instance_file_backup = INSTANCE_FILE INSTANCE_FILE_SUFFIX;
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate /*
1267c478bd9Sstevel@tonic-gate  * Return values for in_get_infile().
1277c478bd9Sstevel@tonic-gate  */
1287c478bd9Sstevel@tonic-gate #define	PTI_FOUND	0
1297c478bd9Sstevel@tonic-gate #define	PTI_NOT_FOUND	1
1307c478bd9Sstevel@tonic-gate #define	PTI_REBUILD	2
1317c478bd9Sstevel@tonic-gate 
13294c894bbSVikram Hegde 
1337c478bd9Sstevel@tonic-gate /*
1347c478bd9Sstevel@tonic-gate  * Path to instance file magic string used for first time boot after
1357c478bd9Sstevel@tonic-gate  * an install.  If this is the first string in the file we will
1367c478bd9Sstevel@tonic-gate  * automatically rebuild the file.
1377c478bd9Sstevel@tonic-gate  */
1387c478bd9Sstevel@tonic-gate #define	PTI_MAGIC_STR		"#path_to_inst_bootstrap_1"
1397c478bd9Sstevel@tonic-gate #define	PTI_MAGIC_STR_LEN	(sizeof (PTI_MAGIC_STR) - 1)
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate void
1427c478bd9Sstevel@tonic-gate e_ddi_instance_init(void)
1437c478bd9Sstevel@tonic-gate {
1447c478bd9Sstevel@tonic-gate 	char *file;
1457c478bd9Sstevel@tonic-gate 	int rebuild = 1;
1467c478bd9Sstevel@tonic-gate 	struct in_drv *dp;
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 	mutex_init(&e_ddi_inst_state.ins_serial, NULL, MUTEX_DEFAULT, NULL);
1497c478bd9Sstevel@tonic-gate 	cv_init(&e_ddi_inst_state.ins_serial_cv, NULL, CV_DEFAULT, NULL);
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 	/*
1527c478bd9Sstevel@tonic-gate 	 * Only one thread is allowed to change the state of the instance
1537c478bd9Sstevel@tonic-gate 	 * number assignments on the system at any given time.
1547c478bd9Sstevel@tonic-gate 	 * Note that this is not really necessary, as we are single-threaded
1557c478bd9Sstevel@tonic-gate 	 * here, but it won't hurt, and it allows us to keep ASSERTS for
1567c478bd9Sstevel@tonic-gate 	 * our assumptions in the code.
1577c478bd9Sstevel@tonic-gate 	 */
1587c478bd9Sstevel@tonic-gate 	e_ddi_enter_instance();
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 	/*
16194c894bbSVikram Hegde 	 * Init the ioaliases if the platform supports it
16294c894bbSVikram Hegde 	 */
16394c894bbSVikram Hegde 	if (&plat_ioaliases_init)
16494c894bbSVikram Hegde 		plat_ioaliases_init();
16594c894bbSVikram Hegde 
16694c894bbSVikram Hegde 	/*
1677c478bd9Sstevel@tonic-gate 	 * Create the root node, instance zallocs to 0.
1687c478bd9Sstevel@tonic-gate 	 * The name and address of this node never get examined, we always
1697c478bd9Sstevel@tonic-gate 	 * start searching with its first child.
1707c478bd9Sstevel@tonic-gate 	 */
1717c478bd9Sstevel@tonic-gate 	ASSERT(e_ddi_inst_state.ins_root == NULL);
1727c478bd9Sstevel@tonic-gate 	e_ddi_inst_state.ins_root = in_alloc_node(NULL, NULL);
1737c478bd9Sstevel@tonic-gate 	dp = in_alloc_drv("rootnex");
1747c478bd9Sstevel@tonic-gate 	in_endrv(e_ddi_inst_state.ins_root, dp);
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	file = instance_file;
1777c478bd9Sstevel@tonic-gate 	switch (in_get_infile(file)) {
1787c478bd9Sstevel@tonic-gate 	default:
1797c478bd9Sstevel@tonic-gate 	case PTI_NOT_FOUND:
1807c478bd9Sstevel@tonic-gate 		/* make sure path_to_inst is recreated */
1817c478bd9Sstevel@tonic-gate 		boothowto |= RB_RECONFIG;
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 		/*
1847c478bd9Sstevel@tonic-gate 		 * Something is wrong. First try the backup file.
1857c478bd9Sstevel@tonic-gate 		 * If not found, rebuild path_to_inst. Emit a
1867c478bd9Sstevel@tonic-gate 		 * message about the problem.
1877c478bd9Sstevel@tonic-gate 		 */
1887c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s empty or not found", file);
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 		file = instance_file_backup;
1917c478bd9Sstevel@tonic-gate 		if (in_get_infile(file) != PTI_FOUND) {
1927c478bd9Sstevel@tonic-gate 			cmn_err(CE_NOTE, "rebuilding device instance data");
1937c478bd9Sstevel@tonic-gate 			break;
1947c478bd9Sstevel@tonic-gate 		}
1957c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "using backup instance data in %s", file);
1967c478bd9Sstevel@tonic-gate 		/*FALLTHROUGH*/
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 	case PTI_FOUND:
1997c478bd9Sstevel@tonic-gate 		/*
2007c478bd9Sstevel@tonic-gate 		 * We've got a readable file
2017c478bd9Sstevel@tonic-gate 		 * parse the file into the instance tree
2027c478bd9Sstevel@tonic-gate 		 */
2037c478bd9Sstevel@tonic-gate 		(void) read_binding_file(file, NULL, in_pathin);
2047c478bd9Sstevel@tonic-gate 		rebuild = 0;
2057c478bd9Sstevel@tonic-gate 		break;
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 	case PTI_REBUILD:
20894c894bbSVikram Hegde 		/*
20994c894bbSVikram Hegde 		 * path_to_inst has magic str requesting a create
21094c894bbSVikram Hegde 		 * Convert boot to reconfig boot to ensure /dev is
21194c894bbSVikram Hegde 		 * in sync with new path_to_inst.
21294c894bbSVikram Hegde 		 */
21394c894bbSVikram Hegde 		boothowto |= RB_RECONFIG;
2147c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT,
2157c478bd9Sstevel@tonic-gate 		    "?Using default device instance data\n");
2167c478bd9Sstevel@tonic-gate 		break;
2177c478bd9Sstevel@tonic-gate 	}
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	/*
2207c478bd9Sstevel@tonic-gate 	 * The OBP device tree has been copied to the kernel and
2217c478bd9Sstevel@tonic-gate 	 * bound to drivers at this point. We walk the per-driver
2227c478bd9Sstevel@tonic-gate 	 * list to preassign instances. Since the bus addr is
2237c478bd9Sstevel@tonic-gate 	 * unknown at this point, we cannot place the instance
2247c478bd9Sstevel@tonic-gate 	 * number in the instance tree. This will be done at
2257c478bd9Sstevel@tonic-gate 	 * a later time.
2267c478bd9Sstevel@tonic-gate 	 */
2277c478bd9Sstevel@tonic-gate 	if (rebuild)
2287c478bd9Sstevel@tonic-gate 		in_preassign_instance();
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 	e_ddi_exit_instance();
2317c478bd9Sstevel@tonic-gate }
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate static void
2347c478bd9Sstevel@tonic-gate in_preassign_instance()
2357c478bd9Sstevel@tonic-gate {
2367c478bd9Sstevel@tonic-gate 	major_t m;
2377c478bd9Sstevel@tonic-gate 	extern major_t devcnt;
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 	for (m = 0; m < devcnt; m++) {
2407c478bd9Sstevel@tonic-gate 		struct devnames *dnp = &devnamesp[m];
2417c478bd9Sstevel@tonic-gate 		dev_info_t *dip = dnp->dn_head;
2427c478bd9Sstevel@tonic-gate 		while (dip) {
2437c478bd9Sstevel@tonic-gate 			DEVI(dip)->devi_instance = dnp->dn_instance;
2447c478bd9Sstevel@tonic-gate 			dnp->dn_instance++;
2457c478bd9Sstevel@tonic-gate 			dip = ddi_get_next(dip);
2467c478bd9Sstevel@tonic-gate 		}
2477c478bd9Sstevel@tonic-gate 	}
2487c478bd9Sstevel@tonic-gate }
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate /*
2517c478bd9Sstevel@tonic-gate  * Checks to see if the /etc/path_to_inst file exists and whether or not
2527c478bd9Sstevel@tonic-gate  * it has the magic string in it.
2537c478bd9Sstevel@tonic-gate  *
2547c478bd9Sstevel@tonic-gate  * Returns one of the following:
2557c478bd9Sstevel@tonic-gate  *
2567c478bd9Sstevel@tonic-gate  *	PTI_FOUND	- We have found the /etc/path_to_inst file
2577c478bd9Sstevel@tonic-gate  *	PTI_REBUILD	- We have found the /etc/path_to_inst file and the
2587c478bd9Sstevel@tonic-gate  *			  first line was PTI_MAGIC_STR.
2597c478bd9Sstevel@tonic-gate  *	PTI_NOT_FOUND	- We did not find the /etc/path_to_inst file
2607c478bd9Sstevel@tonic-gate  *
2617c478bd9Sstevel@tonic-gate  */
2627c478bd9Sstevel@tonic-gate static int
2637c478bd9Sstevel@tonic-gate in_get_infile(char *filename)
2647c478bd9Sstevel@tonic-gate {
265986fd29aSsetje 	struct _buf *file;
2667c478bd9Sstevel@tonic-gate 	int return_val;
2677c478bd9Sstevel@tonic-gate 	char buf[PTI_MAGIC_STR_LEN];
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate 	/*
2707c478bd9Sstevel@tonic-gate 	 * Try to open the file.
2717c478bd9Sstevel@tonic-gate 	 */
272986fd29aSsetje 	if ((file = kobj_open_file(filename)) == (struct _buf *)-1) {
2737c478bd9Sstevel@tonic-gate 		return (PTI_NOT_FOUND);
2747c478bd9Sstevel@tonic-gate 	}
2757c478bd9Sstevel@tonic-gate 	return_val = PTI_FOUND;
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 	/*
2787c478bd9Sstevel@tonic-gate 	 * Read the first PTI_MAGIC_STR_LEN bytes from the file to see if
2797c478bd9Sstevel@tonic-gate 	 * it contains the magic string.  If there aren't that many bytes
2807c478bd9Sstevel@tonic-gate 	 * in the file, then assume file is correct and no magic string
2817c478bd9Sstevel@tonic-gate 	 * and move on.
2827c478bd9Sstevel@tonic-gate 	 */
283986fd29aSsetje 	switch (kobj_read_file(file, buf, PTI_MAGIC_STR_LEN, 0)) {
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate 	case PTI_MAGIC_STR_LEN:
2867c478bd9Sstevel@tonic-gate 		/*
2877c478bd9Sstevel@tonic-gate 		 * If the first PTI_MAGIC_STR_LEN bytes are the magic string
2887c478bd9Sstevel@tonic-gate 		 * then return PTI_REBUILD.
2897c478bd9Sstevel@tonic-gate 		 */
2907c478bd9Sstevel@tonic-gate 		if (strncmp(PTI_MAGIC_STR, buf, PTI_MAGIC_STR_LEN) == 0)
2917c478bd9Sstevel@tonic-gate 			return_val = PTI_REBUILD;
2927c478bd9Sstevel@tonic-gate 		break;
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 	case 0:
2957c478bd9Sstevel@tonic-gate 		/*
2967c478bd9Sstevel@tonic-gate 		 * If the file is zero bytes in length, then consider the
2977c478bd9Sstevel@tonic-gate 		 * file to not be found
2987c478bd9Sstevel@tonic-gate 		 */
2997c478bd9Sstevel@tonic-gate 		return_val = PTI_NOT_FOUND;
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 	default: /* Do nothing we have a good file */
3027c478bd9Sstevel@tonic-gate 		break;
3037c478bd9Sstevel@tonic-gate 	}
3047c478bd9Sstevel@tonic-gate 
305986fd29aSsetje 	kobj_close_file(file);
3067c478bd9Sstevel@tonic-gate 	return (return_val);
3077c478bd9Sstevel@tonic-gate }
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate int
3107c478bd9Sstevel@tonic-gate is_pseudo_device(dev_info_t *dip)
3117c478bd9Sstevel@tonic-gate {
3127c478bd9Sstevel@tonic-gate 	dev_info_t	*pdip;
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 	for (pdip = ddi_get_parent(dip); pdip && pdip != ddi_root_node();
3157c478bd9Sstevel@tonic-gate 	    pdip = ddi_get_parent(pdip)) {
3167c478bd9Sstevel@tonic-gate 		if (strcmp(ddi_get_name(pdip), DEVI_PSEUDO_NEXNAME) == 0)
3177c478bd9Sstevel@tonic-gate 			return (1);
3187c478bd9Sstevel@tonic-gate 	}
3197c478bd9Sstevel@tonic-gate 	return (0);
3207c478bd9Sstevel@tonic-gate }
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate static void
3247c478bd9Sstevel@tonic-gate in_set_instance(dev_info_t *dip, in_drv_t *dp, major_t major)
3257c478bd9Sstevel@tonic-gate {
3267c478bd9Sstevel@tonic-gate 	/* use preassigned instance if available */
3277c478bd9Sstevel@tonic-gate 	if (DEVI(dip)->devi_instance != -1)
3287c478bd9Sstevel@tonic-gate 		dp->ind_instance = DEVI(dip)->devi_instance;
3297c478bd9Sstevel@tonic-gate 	else
3307c478bd9Sstevel@tonic-gate 		dp->ind_instance = in_next_instance(major);
3317c478bd9Sstevel@tonic-gate }
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate /*
33416bd7258Scth  * Return 1 if instance block was assigned for the path.
33516bd7258Scth  *
33616bd7258Scth  * For multi-port NIC cards, sequential instance assignment across all
33762a24de0SChris Horne  * ports on a card is highly desirable since the ppa is typically the
33816bd7258Scth  * same as the instance number, and the ppa is used in the NIC's public
33916bd7258Scth  * /dev name. This sequential assignment typically occurs as a result
34016bd7258Scth  * of in_preassign_instance() after initial install, or by
34116bd7258Scth  * i_ndi_init_hw_children() for NIC ports that share a common parent.
34216bd7258Scth  *
34316bd7258Scth  * Some NIC cards however use multi-function bridge chips, and to
34416bd7258Scth  * support sequential instance assignment accross all ports, without
34516bd7258Scth  * disabling multi-threaded attach, we have a (currently) undocumented
34616bd7258Scth  * hack to allocate instance numbers in contiguous blocks based on
34716bd7258Scth  * driver.conf properties.
34816bd7258Scth  *
34916bd7258Scth  *                       ^
35016bd7258Scth  *           /----------   ------------\
35116bd7258Scth  *        pci@0                      pci@0,1	MULTI-FUNCTION BRIDGE CHIP
35216bd7258Scth  *       /     \                    /       \
35316bd7258Scth  * FJSV,e4ta@4  FJSV,e4ta@4,1   FJSV,e4ta@6 FJSV,e4ta@6,1	NIC PORTS
35416bd7258Scth  *      n            n+2             n+2         n+3		INSTANCE
35516bd7258Scth  *
35616bd7258Scth  * For the above example, the following driver.conf properties would be
35716bd7258Scth  * used to guarantee sequential instance number assignment.
35816bd7258Scth  *
35916bd7258Scth  * ddi-instance-blocks ="ib-FJSVe4ca", "ib-FJSVe4ta", "ib-generic";
36016bd7258Scth  * ib-FJSVe4ca =	"/pci@0/FJSV,e4ca@4", "/pci@0/FJSV,e4ca@4,1",
36116bd7258Scth  *			"/pci@0,1/FJSV,e4ca@6", "/pci@0,1/FJSV,e4ca@6,1";
36216bd7258Scth  * ib-FJSVe4ta =	"/pci@0/FJSV,e4ta@4", "/pci@0/FJSV,e4ta@4,1",
36316bd7258Scth  *			"/pci@0,1/FJSV,e4ta@6", "/pci@0,1/FJSV,e4ta@6,1";
36416bd7258Scth  * ib-generic =		"/pci@0/network@4", "/pci@0/network@4,1",
36516bd7258Scth  *			"/pci@0,1/network@6", "/pci@0,1/network@6,1";
36616bd7258Scth  *
36716bd7258Scth  * The value of the 'ddi-instance-blocks' property references a series
36816bd7258Scth  * of card specific properties, like 'ib-FJSV-e4ta', who's value
36916bd7258Scth  * defines a single 'instance block'.  The 'instance block' describes
37016bd7258Scth  * all the paths below a multi-function bridge, where each path is
37116bd7258Scth  * called an 'instance path'.  The 'instance block' property value is a
37216bd7258Scth  * series of 'instance paths'.  The number of 'instance paths' in an
37316bd7258Scth  * 'instance block' defines the size of the instance block, and the
37416bd7258Scth  * ordering of the 'instance paths' defines the instance number
37516bd7258Scth  * assignment order for paths going through the 'instance block'.
37616bd7258Scth  *
37716bd7258Scth  * In the instance assignment code below, if a (path, driver) that
37816bd7258Scth  * currently has no instance number has a path that goes through an
37916bd7258Scth  * 'instance block', then block instance number allocation occurs.  The
38016bd7258Scth  * block allocation code will find a sequential set of unused instance
38116bd7258Scth  * numbers, and assign instance numbers for all the paths in the
38216bd7258Scth  * 'instance block'.  Each path is assigned a persistent instance
38316bd7258Scth  * number, even paths that don't exist in the device tree or fail
38416bd7258Scth  * probe(9E).
38516bd7258Scth  */
38616bd7258Scth static int
38716bd7258Scth in_assign_instance_block(dev_info_t *dip)
38816bd7258Scth {
38916bd7258Scth 	char		**ibn;		/* instance block names */
39016bd7258Scth 	uint_t		nibn;		/* number of instance block names */
39116bd7258Scth 	uint_t		ibni;		/* ibn index */
39216bd7258Scth 	char		*driver;
39316bd7258Scth 	major_t		major;
39416bd7258Scth 	char		*path;
39516bd7258Scth 	char		*addr;
39616bd7258Scth 	int		plen;
39716bd7258Scth 	char		**ibp;		/* instance block paths */
39816bd7258Scth 	uint_t		nibp;		/* number of paths in instance block */
39916bd7258Scth 	uint_t		ibpi;		/* ibp index */
40016bd7258Scth 	int		ibplen;		/* length of instance block path */
40116bd7258Scth 	char		*ipath;
40216bd7258Scth 	int		instance_base;
40316bd7258Scth 	int		splice;
40416bd7258Scth 	int		i;
40516bd7258Scth 
40616bd7258Scth 	/* check for fresh install case (in miniroot) */
40716bd7258Scth 	if (DEVI(dip)->devi_instance != -1)
40816bd7258Scth 		return (0);			/* already assigned */
40916bd7258Scth 
41016bd7258Scth 	/*
41116bd7258Scth 	 * Check to see if we need to allocate a block of contiguous instance
41216bd7258Scth 	 * numbers by looking for the 'ddi-instance-blocks' property.
41316bd7258Scth 	 */
41416bd7258Scth 	if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
41516bd7258Scth 	    "ddi-instance-blocks", &ibn, &nibn) != DDI_SUCCESS)
41616bd7258Scth 		return (0);			/* no instance block needed */
41716bd7258Scth 
41816bd7258Scth 	/*
41916bd7258Scth 	 * Get information out about node we are processing.
42016bd7258Scth 	 *
42116bd7258Scth 	 * NOTE: Since the node is not yet at DS_INITIALIZED, ddi_pathname()
42216bd7258Scth 	 * will not return the unit-address of the final path component even
42316bd7258Scth 	 * though the node has an established devi_addr unit-address - so we
42416bd7258Scth 	 * need to add the unit-address by hand.
42516bd7258Scth 	 */
42616bd7258Scth 	driver = (char *)ddi_driver_name(dip);
42716bd7258Scth 	major = ddi_driver_major(dip);
42816bd7258Scth 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
42916bd7258Scth 	(void) ddi_pathname(dip, path);
43016bd7258Scth 	if ((addr =  ddi_get_name_addr(dip)) != NULL) {
43116bd7258Scth 		(void) strcat(path, "@");
43216bd7258Scth 		(void) strcat(path, addr);
43316bd7258Scth 	}
43416bd7258Scth 	plen = strlen(path);
43516bd7258Scth 
43616bd7258Scth 	/* loop through instance block names */
43716bd7258Scth 	for (ibni = 0; ibni < nibn; ibni++) {
43816bd7258Scth 		if (ibn[ibni] == NULL)
43916bd7258Scth 			continue;
44016bd7258Scth 
44116bd7258Scth 		/* lookup instance block */
44216bd7258Scth 		if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, dip,
44316bd7258Scth 		    DDI_PROP_DONTPASS, ibn[ibni],
44416bd7258Scth 		    &ibp, &nibp) != DDI_SUCCESS) {
44516bd7258Scth 			cmn_err(CE_WARN,
44616bd7258Scth 			    "no devinition for instance block '%s' in %s.conf",
44716bd7258Scth 			    ibn[ibni], driver);
44816bd7258Scth 			continue;
44916bd7258Scth 		}
45016bd7258Scth 
45116bd7258Scth 		/* Does 'path' go through this instance block? */
45216bd7258Scth 		for (ibpi = 0; ibpi < nibp; ibpi++) {
45316bd7258Scth 			if (ibp[ibpi] == NULL)
45416bd7258Scth 				continue;
45516bd7258Scth 			ibplen = strlen(ibp[ibpi]);
45616bd7258Scth 			if ((ibplen <= plen) &&
45716bd7258Scth 			    (strcmp(ibp[ibpi], path + plen - ibplen) == 0))
45816bd7258Scth 				break;
45916bd7258Scth 
46016bd7258Scth 		}
46116bd7258Scth 		if (ibpi >= nibp) {
46216bd7258Scth 			ddi_prop_free(ibp);
46316bd7258Scth 			continue;		/* no try next instance block */
46416bd7258Scth 		}
46516bd7258Scth 
46616bd7258Scth 		/* yes, allocate and assign instances for all paths in block */
46716bd7258Scth 
46816bd7258Scth 		/*
46916bd7258Scth 		 * determine where we splice in instance paths and verify
47016bd7258Scth 		 * that none of the paths are too long.
47116bd7258Scth 		 */
47216bd7258Scth 		splice = plen - ibplen;
47316bd7258Scth 		for (i = 0; i < nibp; i++) {
47416bd7258Scth 			if ((splice + strlen(ibp[i])+ 1) >= MAXPATHLEN) {
47516bd7258Scth 				cmn_err(CE_WARN,
47616bd7258Scth 				    "path %d through instance block '%s' from "
47716bd7258Scth 				    "%s.conf too long", i, ibn[ibni], driver);
47816bd7258Scth 				break;
47916bd7258Scth 			}
48016bd7258Scth 		}
48116bd7258Scth 		if (i < nibp) {
48216bd7258Scth 			ddi_prop_free(ibp);
48316bd7258Scth 			continue;		/* too long */
48416bd7258Scth 		}
48516bd7258Scth 
48616bd7258Scth 		/* allocate the instance block - no more failures */
48716bd7258Scth 		instance_base = in_next_instance_block(major, nibp);
48816bd7258Scth 
48916bd7258Scth 		ipath = kmem_alloc(MAXPATHLEN, KM_SLEEP);
49016bd7258Scth 		for (ibpi = 0; ibpi < nibp; ibpi++) {
49116bd7258Scth 			if (ibp[ibpi] == NULL)
49216bd7258Scth 				continue;
49316bd7258Scth 			(void) strcpy(ipath, path);
49416bd7258Scth 			(void) strcpy(ipath + splice, ibp[ibpi]);
49516bd7258Scth 			(void) in_pathin(ipath,
49616bd7258Scth 			    instance_base + ibpi, driver, NULL);
49716bd7258Scth 		}
49816bd7258Scth 
49916bd7258Scth 		/* free allocations */
50016bd7258Scth 		kmem_free(ipath, MAXPATHLEN);
50116bd7258Scth 		ddi_prop_free(ibp);
50216bd7258Scth 		kmem_free(path, MAXPATHLEN);
50316bd7258Scth 		ddi_prop_free(ibn);
50416bd7258Scth 
50516bd7258Scth 		/* notify devfsadmd to sync of path_to_inst file */
50616bd7258Scth 		mutex_enter(&e_ddi_inst_state.ins_serial);
50716bd7258Scth 		i_log_devfs_instance_mod();
50894c894bbSVikram Hegde 		e_ddi_inst_state.ins_dirty = B_TRUE;
50916bd7258Scth 		mutex_exit(&e_ddi_inst_state.ins_serial);
51016bd7258Scth 		return (1);
51116bd7258Scth 	}
51216bd7258Scth 
51316bd7258Scth 	/* our path did not go through any of of the instance blocks */
51416bd7258Scth 	kmem_free(path, MAXPATHLEN);
51516bd7258Scth 	ddi_prop_free(ibn);
51616bd7258Scth 	return (0);
51716bd7258Scth }
51816bd7258Scth 
51916bd7258Scth /*
5207c478bd9Sstevel@tonic-gate  * Look up an instance number for a dev_info node, and assign one if it does
5217c478bd9Sstevel@tonic-gate  * not have one (the dev_info node has devi_name and devi_addr already set).
5227c478bd9Sstevel@tonic-gate  */
5237c478bd9Sstevel@tonic-gate uint_t
5247c478bd9Sstevel@tonic-gate e_ddi_assign_instance(dev_info_t *dip)
5257c478bd9Sstevel@tonic-gate {
5267c478bd9Sstevel@tonic-gate 	char *name;
5277c478bd9Sstevel@tonic-gate 	in_node_t *ap, *np;
5287c478bd9Sstevel@tonic-gate 	in_drv_t *dp;
5297c478bd9Sstevel@tonic-gate 	major_t major;
5307c478bd9Sstevel@tonic-gate 	uint_t ret;
5317c478bd9Sstevel@tonic-gate 	char *bname;
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate 	/*
5347c478bd9Sstevel@tonic-gate 	 * Allow implementation to override
5357c478bd9Sstevel@tonic-gate 	 */
5367c478bd9Sstevel@tonic-gate 	if ((ret = impl_assign_instance(dip)) != (uint_t)-1)
5377c478bd9Sstevel@tonic-gate 		return (ret);
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate 	/*
5407c478bd9Sstevel@tonic-gate 	 * If this is a pseudo-device, use the instance number
5417c478bd9Sstevel@tonic-gate 	 * assigned by the pseudo nexus driver. The mutex is
5427c478bd9Sstevel@tonic-gate 	 * not needed since the instance tree is not used.
5437c478bd9Sstevel@tonic-gate 	 */
5447c478bd9Sstevel@tonic-gate 	if (is_pseudo_device(dip)) {
5457c478bd9Sstevel@tonic-gate 		return (ddi_get_instance(dip));
5467c478bd9Sstevel@tonic-gate 	}
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate 	/*
5497c478bd9Sstevel@tonic-gate 	 * Only one thread is allowed to change the state of the instance
5507c478bd9Sstevel@tonic-gate 	 * number assignments on the system at any given time.
5517c478bd9Sstevel@tonic-gate 	 */
5527c478bd9Sstevel@tonic-gate 	e_ddi_enter_instance();
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 	/*
5557c478bd9Sstevel@tonic-gate 	 * Look for instance node, allocate one if not found
5567c478bd9Sstevel@tonic-gate 	 */
5577c478bd9Sstevel@tonic-gate 	np = in_devwalk(dip, &ap, NULL);
5587c478bd9Sstevel@tonic-gate 	if (np == NULL) {
55916bd7258Scth 		if (in_assign_instance_block(dip)) {
56016bd7258Scth 			np = in_devwalk(dip, &ap, NULL);
56116bd7258Scth 		} else {
5627c478bd9Sstevel@tonic-gate 			name = ddi_node_name(dip);
5637c478bd9Sstevel@tonic-gate 			np = in_alloc_node(name, ddi_get_name_addr(dip));
5647c478bd9Sstevel@tonic-gate 			ASSERT(np != NULL);
5657c478bd9Sstevel@tonic-gate 			in_enlist(ap, np);	/* insert into tree */
5667c478bd9Sstevel@tonic-gate 		}
56716bd7258Scth 	}
5687c478bd9Sstevel@tonic-gate 	ASSERT(np == in_devwalk(dip, &ap, NULL));
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate 	/*
57194c894bbSVikram Hegde 	 * Link the devinfo node and in_node_t
57294c894bbSVikram Hegde 	 */
57394c894bbSVikram Hegde 	if (DEVI(dip)->devi_in_node || np->in_devi) {
57494c894bbSVikram Hegde 		ddi_err(DER_MODE, dip, "devinfo and  instance node (%p) "
57594c894bbSVikram Hegde 		    "interlink fields are not NULL", (void *)np);
57694c894bbSVikram Hegde 	}
57794c894bbSVikram Hegde 	DEVI(dip)->devi_in_node = np;
57894c894bbSVikram Hegde 	np->in_devi = dip;
57994c894bbSVikram Hegde 
58094c894bbSVikram Hegde 	/*
5817c478bd9Sstevel@tonic-gate 	 * Look for driver entry, allocate one if not found
5827c478bd9Sstevel@tonic-gate 	 */
5837c478bd9Sstevel@tonic-gate 	bname = (char *)ddi_driver_name(dip);
5847c478bd9Sstevel@tonic-gate 	dp = in_drvwalk(np, bname);
5857c478bd9Sstevel@tonic-gate 	if (dp == NULL) {
58694c894bbSVikram Hegde 
58794c894bbSVikram Hegde 		if (ddi_aliases_present == B_TRUE) {
58894c894bbSVikram Hegde 			e_ddi_borrow_instance(dip, np);
58994c894bbSVikram Hegde 		}
59094c894bbSVikram Hegde 
59194c894bbSVikram Hegde 		if ((dp = in_drvwalk(np, bname)) == NULL) {
5927c478bd9Sstevel@tonic-gate 			dp = in_alloc_drv(bname);
5937c478bd9Sstevel@tonic-gate 			ASSERT(dp != NULL);
5947c478bd9Sstevel@tonic-gate 			major = ddi_driver_major(dip);
595a204de77Scth 			ASSERT(major != DDI_MAJOR_T_NONE);
5967c478bd9Sstevel@tonic-gate 			in_endrv(np, dp);
5977c478bd9Sstevel@tonic-gate 			in_set_instance(dip, dp, major);
5987c478bd9Sstevel@tonic-gate 			dp->ind_state = IN_PROVISIONAL;
5997c478bd9Sstevel@tonic-gate 			in_hashdrv(dp);
60094c894bbSVikram Hegde 		} else {
60194c894bbSVikram Hegde 			dp->ind_state = IN_BORROWED;
60294c894bbSVikram Hegde 		}
6037c478bd9Sstevel@tonic-gate 	}
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate 	ret = dp->ind_instance;
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate 	e_ddi_exit_instance();
6087c478bd9Sstevel@tonic-gate 	return (ret);
6097c478bd9Sstevel@tonic-gate }
6107c478bd9Sstevel@tonic-gate 
6117c478bd9Sstevel@tonic-gate static int
6127c478bd9Sstevel@tonic-gate mkpathname(char *path, in_node_t *np, int len)
6137c478bd9Sstevel@tonic-gate {
6147c478bd9Sstevel@tonic-gate 	int len_needed;
6157c478bd9Sstevel@tonic-gate 
6167c478bd9Sstevel@tonic-gate 	if (np == e_ddi_inst_state.ins_root)
6177c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
6187c478bd9Sstevel@tonic-gate 
6197c478bd9Sstevel@tonic-gate 	if (mkpathname(path, np->in_parent, len) == DDI_FAILURE)
6207c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate 	len_needed = strlen(path);
6237c478bd9Sstevel@tonic-gate 	len_needed += strlen(np->in_node_name) + 1;	/* for '/' */
6247c478bd9Sstevel@tonic-gate 	if (np->in_unit_addr) {
6257c478bd9Sstevel@tonic-gate 		len_needed += strlen(np->in_unit_addr) + 1;  /* for '@' */
6267c478bd9Sstevel@tonic-gate 	}
6277c478bd9Sstevel@tonic-gate 	len_needed += 1; /* for '\0' */
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate 	/*
6307c478bd9Sstevel@tonic-gate 	 * XX complain
6317c478bd9Sstevel@tonic-gate 	 */
6327c478bd9Sstevel@tonic-gate 	if (len_needed > len)
6337c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
6347c478bd9Sstevel@tonic-gate 
6357c478bd9Sstevel@tonic-gate 	if (np->in_unit_addr[0] == '\0')
6367c478bd9Sstevel@tonic-gate 		(void) sprintf(path+strlen(path), "/%s", np->in_node_name);
6377c478bd9Sstevel@tonic-gate 	else
6387c478bd9Sstevel@tonic-gate 		(void) sprintf(path+strlen(path), "/%s@%s", np->in_node_name,
6397c478bd9Sstevel@tonic-gate 		    np->in_unit_addr);
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
6427c478bd9Sstevel@tonic-gate }
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate /*
6457c478bd9Sstevel@tonic-gate  * produce the path to the given instance of a major number.
6467c478bd9Sstevel@tonic-gate  * path must hold MAXPATHLEN string
6477c478bd9Sstevel@tonic-gate  */
6487c478bd9Sstevel@tonic-gate int
6497c478bd9Sstevel@tonic-gate e_ddi_instance_majorinstance_to_path(major_t major, uint_t inst, char *path)
6507c478bd9Sstevel@tonic-gate {
6517c478bd9Sstevel@tonic-gate 	struct devnames	*dnp;
6527c478bd9Sstevel@tonic-gate 	in_drv_t	*dp;
6537c478bd9Sstevel@tonic-gate 	int		ret;
6547c478bd9Sstevel@tonic-gate 
6557c478bd9Sstevel@tonic-gate 	e_ddi_enter_instance();
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate 	/* look for the instance threaded off major */
6587c478bd9Sstevel@tonic-gate 	dnp = &devnamesp[major];
6597c478bd9Sstevel@tonic-gate 	for (dp = dnp->dn_inlist; dp != NULL; dp = dp->ind_next)
6607c478bd9Sstevel@tonic-gate 		if (dp->ind_instance == inst)
6617c478bd9Sstevel@tonic-gate 			break;
6627c478bd9Sstevel@tonic-gate 
6637c478bd9Sstevel@tonic-gate 	/* produce path from the node that uses the instance */
6647c478bd9Sstevel@tonic-gate 	if (dp) {
6657c478bd9Sstevel@tonic-gate 		*path = 0;
6667c478bd9Sstevel@tonic-gate 		ret = mkpathname(path, dp->ind_node, MAXPATHLEN);
6677c478bd9Sstevel@tonic-gate 	} else
6687c478bd9Sstevel@tonic-gate 		ret = DDI_FAILURE;
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate 	e_ddi_exit_instance();
6717c478bd9Sstevel@tonic-gate 	return (ret);
6727c478bd9Sstevel@tonic-gate }
6737c478bd9Sstevel@tonic-gate 
6747c478bd9Sstevel@tonic-gate /*
67516bd7258Scth  * Allocate a sequential block of instance numbers for the specified driver,
67616bd7258Scth  * and return the base instance number of the block.  The implementation
67716bd7258Scth  * depends on the list being sorted in ascending instance number sequence.
67816bd7258Scth  * When there are no 'holes' in the allocation sequence, dn_instance is the
67916bd7258Scth  * next available instance number. When dn_instance is IN_SEARCHME, hole(s)
68016bd7258Scth  * exists and a slower code path executes which tries to fill holes.
6817c478bd9Sstevel@tonic-gate  */
6827c478bd9Sstevel@tonic-gate static int
68316bd7258Scth in_next_instance_block(major_t major, int block_size)
6847c478bd9Sstevel@tonic-gate {
6857c478bd9Sstevel@tonic-gate 	unsigned int	prev;
6867c478bd9Sstevel@tonic-gate 	struct devnames	*dnp;
6877c478bd9Sstevel@tonic-gate 	in_drv_t	*dp;
68816bd7258Scth 	int		base;
68916bd7258Scth 	int		hole;
6907c478bd9Sstevel@tonic-gate 
6917c478bd9Sstevel@tonic-gate 	dnp = &devnamesp[major];
692a204de77Scth 	ASSERT(major != DDI_MAJOR_T_NONE);
6937c478bd9Sstevel@tonic-gate 	ASSERT(e_ddi_inst_state.ins_busy);
69416bd7258Scth 	ASSERT(block_size);
69516bd7258Scth 
69616bd7258Scth 	/* check to see if we can do a quick allocation */
69716bd7258Scth 	if (dnp->dn_instance != IN_SEARCHME) {
69816bd7258Scth 		base = dnp->dn_instance;
69916bd7258Scth 		dnp->dn_instance += block_size;
70016bd7258Scth 		return (base);
70116bd7258Scth 	}
7027c478bd9Sstevel@tonic-gate 	dp = dnp->dn_inlist;
7037c478bd9Sstevel@tonic-gate 
70416bd7258Scth 	/* no existing entries, allocate block at 0 */
7057c478bd9Sstevel@tonic-gate 	if (dp == NULL) {
70616bd7258Scth 		dnp->dn_instance = block_size;
7077c478bd9Sstevel@tonic-gate 		return (0);
7087c478bd9Sstevel@tonic-gate 	}
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate 	prev = dp->ind_instance;
71116bd7258Scth 	if (prev >= block_size)
71216bd7258Scth 		return (0);		/* we fit in hole at beginning */
7137c478bd9Sstevel@tonic-gate 
71416bd7258Scth 	/* search the list for a large enough hole */
71516bd7258Scth 	for (dp = dp->ind_next, hole = 0; dp; dp = dp->ind_next) {
71616bd7258Scth 		if (dp->ind_instance != (prev + 1))
71716bd7258Scth 			hole++;			/* we have a hole */
71816bd7258Scth 		if (dp->ind_instance >= (prev + block_size + 1))
71916bd7258Scth 			break;			/* we fit in hole */
72016bd7258Scth 		prev = dp->ind_instance;
72116bd7258Scth 	}
72216bd7258Scth 
72316bd7258Scth 	/*
72416bd7258Scth 	 * If hole is zero then all holes are patched and we can resume
72516bd7258Scth 	 * quick allocations.
72616bd7258Scth 	 */
72716bd7258Scth 	if (hole == 0)
72816bd7258Scth 		dnp->dn_instance = prev + 1 + block_size;
72916bd7258Scth 
73016bd7258Scth 	return (prev + 1);
73116bd7258Scth }
73216bd7258Scth 
73316bd7258Scth /* assign instance block of size 1 */
73416bd7258Scth static int
73516bd7258Scth in_next_instance(major_t major)
73616bd7258Scth {
73716bd7258Scth 	return (in_next_instance_block(major, 1));
7387c478bd9Sstevel@tonic-gate }
7397c478bd9Sstevel@tonic-gate 
7407c478bd9Sstevel@tonic-gate /*
7417c478bd9Sstevel@tonic-gate  * This call causes us to *forget* the instance number we've generated
7427c478bd9Sstevel@tonic-gate  * for a given device if it was not permanent.
7437c478bd9Sstevel@tonic-gate  */
7447c478bd9Sstevel@tonic-gate void
7457c478bd9Sstevel@tonic-gate e_ddi_free_instance(dev_info_t *dip, char *addr)
7467c478bd9Sstevel@tonic-gate {
7477c478bd9Sstevel@tonic-gate 	char *name;
7487c478bd9Sstevel@tonic-gate 	in_node_t *np;
7497c478bd9Sstevel@tonic-gate 	in_node_t *ap;	/* ancestor node */
7507c478bd9Sstevel@tonic-gate 	major_t major;
7517c478bd9Sstevel@tonic-gate 	struct devnames *dnp;
7527c478bd9Sstevel@tonic-gate 	in_drv_t *dp;	/* in_drv entry */
7537c478bd9Sstevel@tonic-gate 
7547c478bd9Sstevel@tonic-gate 	/*
7557c478bd9Sstevel@tonic-gate 	 * Allow implementation override
7567c478bd9Sstevel@tonic-gate 	 */
7577c478bd9Sstevel@tonic-gate 	if (impl_free_instance(dip) == DDI_SUCCESS)
7587c478bd9Sstevel@tonic-gate 		return;
7597c478bd9Sstevel@tonic-gate 
7607c478bd9Sstevel@tonic-gate 	/*
7617c478bd9Sstevel@tonic-gate 	 * If this is a pseudo-device, no instance number
7627c478bd9Sstevel@tonic-gate 	 * was assigned.
7637c478bd9Sstevel@tonic-gate 	 */
7647c478bd9Sstevel@tonic-gate 	if (is_pseudo_device(dip)) {
7657c478bd9Sstevel@tonic-gate 		return;
7667c478bd9Sstevel@tonic-gate 	}
7677c478bd9Sstevel@tonic-gate 
7687c478bd9Sstevel@tonic-gate 	name = (char *)ddi_driver_name(dip);
7697c478bd9Sstevel@tonic-gate 	major = ddi_driver_major(dip);
770a204de77Scth 	ASSERT(major != DDI_MAJOR_T_NONE);
7717c478bd9Sstevel@tonic-gate 	dnp = &devnamesp[major];
7727c478bd9Sstevel@tonic-gate 	/*
7737c478bd9Sstevel@tonic-gate 	 * Only one thread is allowed to change the state of the instance
7747c478bd9Sstevel@tonic-gate 	 * number assignments on the system at any given time.
7757c478bd9Sstevel@tonic-gate 	 */
7767c478bd9Sstevel@tonic-gate 	e_ddi_enter_instance();
7777c478bd9Sstevel@tonic-gate 	np = in_devwalk(dip, &ap, addr);
7787c478bd9Sstevel@tonic-gate 	ASSERT(np);
77994c894bbSVikram Hegde 
78094c894bbSVikram Hegde 	/*
78194c894bbSVikram Hegde 	 * Break the interlink between dip and np
78294c894bbSVikram Hegde 	 */
78394c894bbSVikram Hegde 	if (DEVI(dip)->devi_in_node != np || np->in_devi != dip) {
78494c894bbSVikram Hegde 		ddi_err(DER_MODE, dip, "devinfo node linked to "
78594c894bbSVikram Hegde 		    "wrong instance node: %p", (void *)np);
78694c894bbSVikram Hegde 	}
78794c894bbSVikram Hegde 	DEVI(dip)->devi_in_node = NULL;
78894c894bbSVikram Hegde 	np->in_devi = NULL;
78994c894bbSVikram Hegde 
7907c478bd9Sstevel@tonic-gate 	dp = in_drvwalk(np, name);
7917c478bd9Sstevel@tonic-gate 	ASSERT(dp);
7927c478bd9Sstevel@tonic-gate 	if (dp->ind_state == IN_PROVISIONAL) {
7937c478bd9Sstevel@tonic-gate 		in_removedrv(dnp, dp);
794*f1ebe3a2SVikram Hegde 	} else if (dp->ind_state == IN_BORROWED) {
79594c894bbSVikram Hegde 		dp->ind_state = IN_PERMANENT;
79694c894bbSVikram Hegde 		e_ddi_return_instance(dip, addr, np);
79794c894bbSVikram Hegde 	}
7987c478bd9Sstevel@tonic-gate 	if (np->in_drivers == NULL) {
7997c478bd9Sstevel@tonic-gate 		in_removenode(dnp, np, ap);
8007c478bd9Sstevel@tonic-gate 	}
8017c478bd9Sstevel@tonic-gate 	e_ddi_exit_instance();
8027c478bd9Sstevel@tonic-gate }
8037c478bd9Sstevel@tonic-gate 
8047c478bd9Sstevel@tonic-gate /*
8057c478bd9Sstevel@tonic-gate  * This makes our memory of an instance assignment permanent
8067c478bd9Sstevel@tonic-gate  */
8077c478bd9Sstevel@tonic-gate void
8087c478bd9Sstevel@tonic-gate e_ddi_keep_instance(dev_info_t *dip)
8097c478bd9Sstevel@tonic-gate {
8107c478bd9Sstevel@tonic-gate 	in_node_t *np, *ap;
8117c478bd9Sstevel@tonic-gate 	in_drv_t *dp;
8127c478bd9Sstevel@tonic-gate 
81362a24de0SChris Horne 	/* Don't make nulldriver instance assignments permanent */
81462a24de0SChris Horne 	if (ddi_driver_major(dip) == nulldriver_major)
81562a24de0SChris Horne 		return;
81662a24de0SChris Horne 
8177c478bd9Sstevel@tonic-gate 	/*
8187c478bd9Sstevel@tonic-gate 	 * Allow implementation override
8197c478bd9Sstevel@tonic-gate 	 */
8207c478bd9Sstevel@tonic-gate 	if (impl_keep_instance(dip) == DDI_SUCCESS)
8217c478bd9Sstevel@tonic-gate 		return;
8227c478bd9Sstevel@tonic-gate 
8237c478bd9Sstevel@tonic-gate 	/*
8247c478bd9Sstevel@tonic-gate 	 * Nothing to do for pseudo devices.
8257c478bd9Sstevel@tonic-gate 	 */
8267c478bd9Sstevel@tonic-gate 	if (is_pseudo_device(dip))
8277c478bd9Sstevel@tonic-gate 		return;
8287c478bd9Sstevel@tonic-gate 
8297c478bd9Sstevel@tonic-gate 	/*
8307c478bd9Sstevel@tonic-gate 	 * Only one thread is allowed to change the state of the instance
8317c478bd9Sstevel@tonic-gate 	 * number assignments on the system at any given time.
8327c478bd9Sstevel@tonic-gate 	 */
8337c478bd9Sstevel@tonic-gate 	e_ddi_enter_instance();
8347c478bd9Sstevel@tonic-gate 	np = in_devwalk(dip, &ap, NULL);
8357c478bd9Sstevel@tonic-gate 	ASSERT(np);
8367c478bd9Sstevel@tonic-gate 	dp = in_drvwalk(np, (char *)ddi_driver_name(dip));
8377c478bd9Sstevel@tonic-gate 	ASSERT(dp);
8387c478bd9Sstevel@tonic-gate 
8397c478bd9Sstevel@tonic-gate 	mutex_enter(&e_ddi_inst_state.ins_serial);
84094c894bbSVikram Hegde 	if (dp->ind_state == IN_PROVISIONAL || dp->ind_state == IN_BORROWED) {
8417c478bd9Sstevel@tonic-gate 		dp->ind_state = IN_PERMANENT;
8427c478bd9Sstevel@tonic-gate 		i_log_devfs_instance_mod();
84394c894bbSVikram Hegde 		e_ddi_inst_state.ins_dirty = B_TRUE;
8447c478bd9Sstevel@tonic-gate 	}
8457c478bd9Sstevel@tonic-gate 	mutex_exit(&e_ddi_inst_state.ins_serial);
8467c478bd9Sstevel@tonic-gate 	e_ddi_exit_instance();
8477c478bd9Sstevel@tonic-gate }
8487c478bd9Sstevel@tonic-gate 
8497c478bd9Sstevel@tonic-gate /*
8507c478bd9Sstevel@tonic-gate  * A new major has been added to the system.  Run through the orphan list
8517c478bd9Sstevel@tonic-gate  * and try to attach each one to a driver's list.
8527c478bd9Sstevel@tonic-gate  */
8537c478bd9Sstevel@tonic-gate void
8547c478bd9Sstevel@tonic-gate e_ddi_unorphan_instance_nos()
8557c478bd9Sstevel@tonic-gate {
8567c478bd9Sstevel@tonic-gate 	in_drv_t *dp, *ndp;
8577c478bd9Sstevel@tonic-gate 
8587c478bd9Sstevel@tonic-gate 	/*
8597c478bd9Sstevel@tonic-gate 	 * disconnect the orphan list, and call in_hashdrv for each item
8607c478bd9Sstevel@tonic-gate 	 * on it
8617c478bd9Sstevel@tonic-gate 	 */
8627c478bd9Sstevel@tonic-gate 
8637c478bd9Sstevel@tonic-gate 	/*
8647c478bd9Sstevel@tonic-gate 	 * Only one thread is allowed to change the state of the instance
8657c478bd9Sstevel@tonic-gate 	 * number assignments on the system at any given time.
8667c478bd9Sstevel@tonic-gate 	 */
8677c478bd9Sstevel@tonic-gate 	e_ddi_enter_instance();
8687c478bd9Sstevel@tonic-gate 	if (e_ddi_inst_state.ins_no_major == NULL) {
8697c478bd9Sstevel@tonic-gate 		e_ddi_exit_instance();
8707c478bd9Sstevel@tonic-gate 		return;
8717c478bd9Sstevel@tonic-gate 	}
8727c478bd9Sstevel@tonic-gate 	/*
8737c478bd9Sstevel@tonic-gate 	 * Hash instance list to devnames structure of major.
8747c478bd9Sstevel@tonic-gate 	 * Note that if there is not a valid major number for the
8757c478bd9Sstevel@tonic-gate 	 * node, in_hashdrv will put it back on the no_major list.
8767c478bd9Sstevel@tonic-gate 	 */
8777c478bd9Sstevel@tonic-gate 	dp = e_ddi_inst_state.ins_no_major;
8787c478bd9Sstevel@tonic-gate 	e_ddi_inst_state.ins_no_major = NULL;
8797c478bd9Sstevel@tonic-gate 	while (dp) {
8807c478bd9Sstevel@tonic-gate 		ndp = dp->ind_next;
8817c478bd9Sstevel@tonic-gate 		ASSERT(dp->ind_state != IN_UNKNOWN);
8827c478bd9Sstevel@tonic-gate 		dp->ind_next = NULL;
8837c478bd9Sstevel@tonic-gate 		in_hashdrv(dp);
8847c478bd9Sstevel@tonic-gate 		dp = ndp;
8857c478bd9Sstevel@tonic-gate 	}
8867c478bd9Sstevel@tonic-gate 	e_ddi_exit_instance();
8877c478bd9Sstevel@tonic-gate }
8887c478bd9Sstevel@tonic-gate 
8897c478bd9Sstevel@tonic-gate static void
8907c478bd9Sstevel@tonic-gate in_removenode(struct devnames *dnp, in_node_t *mp, in_node_t *ap)
8917c478bd9Sstevel@tonic-gate {
8927c478bd9Sstevel@tonic-gate 	in_node_t *np;
8937c478bd9Sstevel@tonic-gate 
8947c478bd9Sstevel@tonic-gate 	ASSERT(e_ddi_inst_state.ins_busy);
89594c894bbSVikram Hegde 
8967c478bd9Sstevel@tonic-gate 	/*
8977c478bd9Sstevel@tonic-gate 	 * Assertion: parents are always instantiated by the framework
8987c478bd9Sstevel@tonic-gate 	 * before their children, destroyed after them
8997c478bd9Sstevel@tonic-gate 	 */
9007c478bd9Sstevel@tonic-gate 	ASSERT(mp->in_child == NULL);
9017c478bd9Sstevel@tonic-gate 	/*
9027c478bd9Sstevel@tonic-gate 	 * Assertion: drv entries are always removed before their owning nodes
9037c478bd9Sstevel@tonic-gate 	 */
9047c478bd9Sstevel@tonic-gate 	ASSERT(mp->in_drivers == NULL);
9057c478bd9Sstevel@tonic-gate 	/*
9067c478bd9Sstevel@tonic-gate 	 * Take the node out of the tree
9077c478bd9Sstevel@tonic-gate 	 */
9087c478bd9Sstevel@tonic-gate 	if (ap->in_child == mp) {
9097c478bd9Sstevel@tonic-gate 		ap->in_child = mp->in_sibling;
9107c478bd9Sstevel@tonic-gate 		in_dealloc_node(mp);
9117c478bd9Sstevel@tonic-gate 		return;
9127c478bd9Sstevel@tonic-gate 	} else {
9137c478bd9Sstevel@tonic-gate 		for (np = ap->in_child; np; np = np->in_sibling) {
9147c478bd9Sstevel@tonic-gate 			if (np->in_sibling == mp) {
9157c478bd9Sstevel@tonic-gate 				np->in_sibling = mp->in_sibling;
9167c478bd9Sstevel@tonic-gate 				in_dealloc_node(mp);
9177c478bd9Sstevel@tonic-gate 				return;
9187c478bd9Sstevel@tonic-gate 			}
9197c478bd9Sstevel@tonic-gate 		}
9207c478bd9Sstevel@tonic-gate 	}
9217c478bd9Sstevel@tonic-gate 	panic("in_removenode dnp %p mp %p", (void *)dnp, (void *)mp);
9227c478bd9Sstevel@tonic-gate }
9237c478bd9Sstevel@tonic-gate 
9247c478bd9Sstevel@tonic-gate /*
9257c478bd9Sstevel@tonic-gate  * Recursive ascent
9267c478bd9Sstevel@tonic-gate  *
9277c478bd9Sstevel@tonic-gate  * This now only does half the job.  It finds the node, then the caller
9287c478bd9Sstevel@tonic-gate  * has to search the node for the binding name
9297c478bd9Sstevel@tonic-gate  */
9307c478bd9Sstevel@tonic-gate static in_node_t *
9317c478bd9Sstevel@tonic-gate in_devwalk(dev_info_t *dip, in_node_t **ap, char *addr)
9327c478bd9Sstevel@tonic-gate {
9337c478bd9Sstevel@tonic-gate 	in_node_t *np;
9347c478bd9Sstevel@tonic-gate 	char *name;
9357c478bd9Sstevel@tonic-gate 
9367c478bd9Sstevel@tonic-gate 	ASSERT(dip);
9377c478bd9Sstevel@tonic-gate 	ASSERT(e_ddi_inst_state.ins_busy);
9387c478bd9Sstevel@tonic-gate 	if (dip == ddi_root_node()) {
9397c478bd9Sstevel@tonic-gate 		*ap = NULL;
9407c478bd9Sstevel@tonic-gate 		return (e_ddi_inst_state.ins_root);
9417c478bd9Sstevel@tonic-gate 	}
9427c478bd9Sstevel@tonic-gate 	/*
9437c478bd9Sstevel@tonic-gate 	 * call up to find parent, then look through the list of kids
9447c478bd9Sstevel@tonic-gate 	 * for a match
9457c478bd9Sstevel@tonic-gate 	 */
9467c478bd9Sstevel@tonic-gate 	np = in_devwalk(ddi_get_parent(dip), ap, NULL);
9477c478bd9Sstevel@tonic-gate 	if (np == NULL)
9487c478bd9Sstevel@tonic-gate 		return (np);
9497c478bd9Sstevel@tonic-gate 	*ap = np;
9507c478bd9Sstevel@tonic-gate 	np = np->in_child;
9517c478bd9Sstevel@tonic-gate 	name = ddi_node_name(dip);
9527c478bd9Sstevel@tonic-gate 	if (addr == NULL)
9537c478bd9Sstevel@tonic-gate 		addr = ddi_get_name_addr(dip);
9547c478bd9Sstevel@tonic-gate 
9557c478bd9Sstevel@tonic-gate 	while (np) {
9567c478bd9Sstevel@tonic-gate 		if (in_eqstr(np->in_node_name, name) &&
9577c478bd9Sstevel@tonic-gate 		    in_eqstr(np->in_unit_addr, addr)) {
9587c478bd9Sstevel@tonic-gate 			return (np);
9597c478bd9Sstevel@tonic-gate 		}
9607c478bd9Sstevel@tonic-gate 		np = np->in_sibling;
9617c478bd9Sstevel@tonic-gate 	}
96294c894bbSVikram Hegde 
9637c478bd9Sstevel@tonic-gate 	return (np);
9647c478bd9Sstevel@tonic-gate }
9657c478bd9Sstevel@tonic-gate 
9667c478bd9Sstevel@tonic-gate /*
9677c478bd9Sstevel@tonic-gate  * Create a node specified by cp and assign it the given instance no.
9687c478bd9Sstevel@tonic-gate  */
9697c478bd9Sstevel@tonic-gate static int
9707c478bd9Sstevel@tonic-gate in_pathin(char *cp, int instance, char *bname, struct bind **args)
9717c478bd9Sstevel@tonic-gate {
9727c478bd9Sstevel@tonic-gate 	in_node_t *np;
9737c478bd9Sstevel@tonic-gate 	in_drv_t *dp;
9747c478bd9Sstevel@tonic-gate 	char *name;
9757c478bd9Sstevel@tonic-gate 
9767c478bd9Sstevel@tonic-gate 	ASSERT(e_ddi_inst_state.ins_busy);
9777c478bd9Sstevel@tonic-gate 	ASSERT(args == NULL);
9787c478bd9Sstevel@tonic-gate 
9797c478bd9Sstevel@tonic-gate 	/*
9807c478bd9Sstevel@tonic-gate 	 * Give a warning to the console.
9817c478bd9Sstevel@tonic-gate 	 * return value ignored
9827c478bd9Sstevel@tonic-gate 	 */
9837c478bd9Sstevel@tonic-gate 	if (cp[0] != '/' || instance == -1 || bname == NULL) {
9847c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN,
9857c478bd9Sstevel@tonic-gate 		    "invalid instance file entry %s %d",
9867c478bd9Sstevel@tonic-gate 		    cp, instance);
9877c478bd9Sstevel@tonic-gate 		return (0);
9887c478bd9Sstevel@tonic-gate 	}
9897c478bd9Sstevel@tonic-gate 
9907c478bd9Sstevel@tonic-gate 	if ((name  = i_binding_to_drv_name(bname)) != NULL)
9917c478bd9Sstevel@tonic-gate 		bname = name;
9927c478bd9Sstevel@tonic-gate 
9937c478bd9Sstevel@tonic-gate 	np = in_make_path(cp);
9947c478bd9Sstevel@tonic-gate 	ASSERT(np);
99594c894bbSVikram Hegde 
9967c478bd9Sstevel@tonic-gate 	dp = in_drvwalk(np, bname);
9977c478bd9Sstevel@tonic-gate 	if (dp != NULL) {
9987c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN,
9997c478bd9Sstevel@tonic-gate 		    "multiple instance number assignments for "
10007c478bd9Sstevel@tonic-gate 		    "'%s' (driver %s), %d used",
10017c478bd9Sstevel@tonic-gate 		    cp, bname, dp->ind_instance);
10027c478bd9Sstevel@tonic-gate 		return (0);
10037c478bd9Sstevel@tonic-gate 	}
100494c894bbSVikram Hegde 
100594c894bbSVikram Hegde 	if (in_inuse(instance, bname)) {
100694c894bbSVikram Hegde 		cmn_err(CE_WARN,
100794c894bbSVikram Hegde 		    "instance already in use: %s %d", cp, instance);
100894c894bbSVikram Hegde 		return (0);
100994c894bbSVikram Hegde 	}
101094c894bbSVikram Hegde 
10117c478bd9Sstevel@tonic-gate 	dp = in_alloc_drv(bname);
10127c478bd9Sstevel@tonic-gate 	in_endrv(np, dp);
10137c478bd9Sstevel@tonic-gate 	dp->ind_instance = instance;
10147c478bd9Sstevel@tonic-gate 	dp->ind_state = IN_PERMANENT;
10157c478bd9Sstevel@tonic-gate 	in_hashdrv(dp);
10167c478bd9Sstevel@tonic-gate 
10177c478bd9Sstevel@tonic-gate 	return (0);
10187c478bd9Sstevel@tonic-gate }
10197c478bd9Sstevel@tonic-gate 
10207c478bd9Sstevel@tonic-gate /*
10217c478bd9Sstevel@tonic-gate  * Create (or find) the node named by path by recursively descending from the
10227c478bd9Sstevel@tonic-gate  * root's first child (we ignore the root, which is never named)
10237c478bd9Sstevel@tonic-gate  */
10247c478bd9Sstevel@tonic-gate static in_node_t *
10257c478bd9Sstevel@tonic-gate in_make_path(char *path)
10267c478bd9Sstevel@tonic-gate {
10277c478bd9Sstevel@tonic-gate 	in_node_t *ap;		/* ancestor pointer */
10287c478bd9Sstevel@tonic-gate 	in_node_t *np;		/* working node pointer */
10297c478bd9Sstevel@tonic-gate 	in_node_t *rp;		/* return node pointer */
10307c478bd9Sstevel@tonic-gate 	char buf[MAXPATHLEN];	/* copy of string so we can change it */
10317c478bd9Sstevel@tonic-gate 	char *cp, *name, *addr;
10327c478bd9Sstevel@tonic-gate 
10337c478bd9Sstevel@tonic-gate 	ASSERT(e_ddi_inst_state.ins_busy);
103494c894bbSVikram Hegde 
10357c478bd9Sstevel@tonic-gate 	if (path == NULL || path[0] != '/')
10367c478bd9Sstevel@tonic-gate 		return (NULL);
103794c894bbSVikram Hegde 
10387c478bd9Sstevel@tonic-gate 	(void) snprintf(buf, sizeof (buf), "%s", path);
10397c478bd9Sstevel@tonic-gate 	cp = buf + 1;	/* skip over initial '/' in path */
10407c478bd9Sstevel@tonic-gate 	name = in_name_addr(&cp, &addr);
10417c478bd9Sstevel@tonic-gate 
10427c478bd9Sstevel@tonic-gate 	/*
10437c478bd9Sstevel@tonic-gate 	 * In S9 and earlier releases, the path_to_inst file
10447c478bd9Sstevel@tonic-gate 	 * SunCluster was prepended with "/node@#". This was
10457c478bd9Sstevel@tonic-gate 	 * removed in S10. We skip the prefix if the prefix
10467c478bd9Sstevel@tonic-gate 	 * still exists in /etc/path_to_inst. It is needed for
10477c478bd9Sstevel@tonic-gate 	 * various forms of Solaris upgrade to work properly
10487c478bd9Sstevel@tonic-gate 	 * in the SunCluster environment.
10497c478bd9Sstevel@tonic-gate 	 */
10507c478bd9Sstevel@tonic-gate 	if ((cluster_bootflags & CLUSTER_CONFIGURED) &&
10517c478bd9Sstevel@tonic-gate 	    (strcmp(name, "node") == 0))
10527c478bd9Sstevel@tonic-gate 		name = in_name_addr(&cp, &addr);
10537c478bd9Sstevel@tonic-gate 
10547c478bd9Sstevel@tonic-gate 	ap = e_ddi_inst_state.ins_root;
105594c894bbSVikram Hegde 	np = e_ddi_inst_state.ins_root->in_child;
105694c894bbSVikram Hegde 	rp = np;
10577c478bd9Sstevel@tonic-gate 	while (name) {
10587c478bd9Sstevel@tonic-gate 		while (name && np) {
10597c478bd9Sstevel@tonic-gate 			if (in_eqstr(name, np->in_node_name) &&
10607c478bd9Sstevel@tonic-gate 			    in_eqstr(addr, np->in_unit_addr)) {
10617c478bd9Sstevel@tonic-gate 				name = in_name_addr(&cp, &addr);
10627c478bd9Sstevel@tonic-gate 				if (name == NULL)
10637c478bd9Sstevel@tonic-gate 					return (np);
10647c478bd9Sstevel@tonic-gate 				ap = np;
10657c478bd9Sstevel@tonic-gate 				np = np->in_child;
10667c478bd9Sstevel@tonic-gate 			} else {
10677c478bd9Sstevel@tonic-gate 				np = np->in_sibling;
10687c478bd9Sstevel@tonic-gate 			}
10697c478bd9Sstevel@tonic-gate 		}
10707c478bd9Sstevel@tonic-gate 		np = in_alloc_node(name, addr);
10717c478bd9Sstevel@tonic-gate 		in_enlist(ap, np);	/* insert into tree */
10727c478bd9Sstevel@tonic-gate 		rp = np;	/* value to return if we quit */
10737c478bd9Sstevel@tonic-gate 		ap = np;	/* new parent */
10747c478bd9Sstevel@tonic-gate 		np = NULL;	/* can have no children */
10757c478bd9Sstevel@tonic-gate 		name = in_name_addr(&cp, &addr);
10767c478bd9Sstevel@tonic-gate 	}
107794c894bbSVikram Hegde 
10787c478bd9Sstevel@tonic-gate 	return (rp);
10797c478bd9Sstevel@tonic-gate }
10807c478bd9Sstevel@tonic-gate 
10817c478bd9Sstevel@tonic-gate /*
10827c478bd9Sstevel@tonic-gate  * Insert node np into the tree as one of ap's children.
10837c478bd9Sstevel@tonic-gate  */
10847c478bd9Sstevel@tonic-gate static void
10857c478bd9Sstevel@tonic-gate in_enlist(in_node_t *ap, in_node_t *np)
10867c478bd9Sstevel@tonic-gate {
10877c478bd9Sstevel@tonic-gate 	in_node_t *mp;
10887c478bd9Sstevel@tonic-gate 	ASSERT(e_ddi_inst_state.ins_busy);
10897c478bd9Sstevel@tonic-gate 	/*
10907c478bd9Sstevel@tonic-gate 	 * Make this node some other node's child or child's sibling
10917c478bd9Sstevel@tonic-gate 	 */
10927c478bd9Sstevel@tonic-gate 	ASSERT(ap && np);
10937c478bd9Sstevel@tonic-gate 	if (ap->in_child == NULL) {
10947c478bd9Sstevel@tonic-gate 		ap->in_child = np;
10957c478bd9Sstevel@tonic-gate 	} else {
10967c478bd9Sstevel@tonic-gate 		for (mp = ap->in_child; mp; mp = mp->in_sibling)
10977c478bd9Sstevel@tonic-gate 			if (mp->in_sibling == NULL) {
10987c478bd9Sstevel@tonic-gate 				mp->in_sibling = np;
10997c478bd9Sstevel@tonic-gate 				break;
11007c478bd9Sstevel@tonic-gate 			}
11017c478bd9Sstevel@tonic-gate 	}
11027c478bd9Sstevel@tonic-gate 	np->in_parent = ap;
11037c478bd9Sstevel@tonic-gate }
11047c478bd9Sstevel@tonic-gate 
11057c478bd9Sstevel@tonic-gate /*
11067c478bd9Sstevel@tonic-gate  * Insert drv entry dp onto a node's driver list
11077c478bd9Sstevel@tonic-gate  */
11087c478bd9Sstevel@tonic-gate static void
11097c478bd9Sstevel@tonic-gate in_endrv(in_node_t *np, in_drv_t *dp)
11107c478bd9Sstevel@tonic-gate {
11117c478bd9Sstevel@tonic-gate 	in_drv_t *mp;
11127c478bd9Sstevel@tonic-gate 	ASSERT(e_ddi_inst_state.ins_busy);
11137c478bd9Sstevel@tonic-gate 	ASSERT(np && dp);
11147c478bd9Sstevel@tonic-gate 	mp = np->in_drivers;
11157c478bd9Sstevel@tonic-gate 	np->in_drivers = dp;
11167c478bd9Sstevel@tonic-gate 	dp->ind_next_drv = mp;
11177c478bd9Sstevel@tonic-gate 	dp->ind_node = np;
11187c478bd9Sstevel@tonic-gate }
11197c478bd9Sstevel@tonic-gate 
11207c478bd9Sstevel@tonic-gate /*
11217c478bd9Sstevel@tonic-gate  * Parse the next name out of the path, null terminate it and update cp.
11227c478bd9Sstevel@tonic-gate  * caller has copied string so we can mess with it.
11237c478bd9Sstevel@tonic-gate  * Upon return *cpp points to the next section to be parsed, *addrp points
11247c478bd9Sstevel@tonic-gate  * to the current address substring (or NULL if none) and we return the
11257c478bd9Sstevel@tonic-gate  * current name substring (or NULL if none).  name and address substrings
11267c478bd9Sstevel@tonic-gate  * are null terminated in place.
11277c478bd9Sstevel@tonic-gate  */
11287c478bd9Sstevel@tonic-gate 
11297c478bd9Sstevel@tonic-gate static char *
11307c478bd9Sstevel@tonic-gate in_name_addr(char **cpp, char **addrp)
11317c478bd9Sstevel@tonic-gate {
11327c478bd9Sstevel@tonic-gate 	char *namep;	/* return value holder */
11337c478bd9Sstevel@tonic-gate 	char *ap;	/* pointer to '@' in string */
11347c478bd9Sstevel@tonic-gate 	char *sp;	/* pointer to '/' in string */
11357c478bd9Sstevel@tonic-gate 
11367c478bd9Sstevel@tonic-gate 	if (*cpp == NULL || **cpp == '\0') {
11377c478bd9Sstevel@tonic-gate 		*addrp = NULL;
11387c478bd9Sstevel@tonic-gate 		return (NULL);
11397c478bd9Sstevel@tonic-gate 	}
11407c478bd9Sstevel@tonic-gate 	namep = *cpp;
11417c478bd9Sstevel@tonic-gate 	sp = strchr(*cpp, '/');
11427c478bd9Sstevel@tonic-gate 	if (sp != NULL) {	/* more to follow */
11437c478bd9Sstevel@tonic-gate 		*sp = '\0';
11447c478bd9Sstevel@tonic-gate 		*cpp = sp + 1;
11457c478bd9Sstevel@tonic-gate 	} else {		/* this is last component. */
11467c478bd9Sstevel@tonic-gate 		*cpp = NULL;
11477c478bd9Sstevel@tonic-gate 	}
11487c478bd9Sstevel@tonic-gate 	ap = strchr(namep, '@');
11497c478bd9Sstevel@tonic-gate 	if (ap == NULL) {
11507c478bd9Sstevel@tonic-gate 		*addrp = NULL;
11517c478bd9Sstevel@tonic-gate 	} else {
11527c478bd9Sstevel@tonic-gate 		*ap = '\0';		/* terminate the name */
11537c478bd9Sstevel@tonic-gate 		*addrp = ap + 1;
11547c478bd9Sstevel@tonic-gate 	}
11557c478bd9Sstevel@tonic-gate 	return (namep);
11567c478bd9Sstevel@tonic-gate }
11577c478bd9Sstevel@tonic-gate 
11587c478bd9Sstevel@tonic-gate /*
11597c478bd9Sstevel@tonic-gate  * Allocate a node and storage for name and addr strings, and fill them in.
11607c478bd9Sstevel@tonic-gate  */
11617c478bd9Sstevel@tonic-gate static in_node_t *
11627c478bd9Sstevel@tonic-gate in_alloc_node(char *name, char *addr)
11637c478bd9Sstevel@tonic-gate {
11647c478bd9Sstevel@tonic-gate 	in_node_t *np;
11657c478bd9Sstevel@tonic-gate 	char *cp;
11667c478bd9Sstevel@tonic-gate 	size_t namelen;
11677c478bd9Sstevel@tonic-gate 
11687c478bd9Sstevel@tonic-gate 	ASSERT(e_ddi_inst_state.ins_busy);
11697c478bd9Sstevel@tonic-gate 	/*
11707c478bd9Sstevel@tonic-gate 	 * Has name or will become root
11717c478bd9Sstevel@tonic-gate 	 */
11727c478bd9Sstevel@tonic-gate 	ASSERT(name || e_ddi_inst_state.ins_root == NULL);
11737c478bd9Sstevel@tonic-gate 	if (addr == NULL)
11747c478bd9Sstevel@tonic-gate 		addr = "";
11757c478bd9Sstevel@tonic-gate 	if (name == NULL)
11767c478bd9Sstevel@tonic-gate 		namelen = 0;
11777c478bd9Sstevel@tonic-gate 	else
11787c478bd9Sstevel@tonic-gate 		namelen = strlen(name) + 1;
11797c478bd9Sstevel@tonic-gate 	cp = kmem_zalloc(sizeof (in_node_t) + namelen + strlen(addr) + 1,
11807c478bd9Sstevel@tonic-gate 	    KM_SLEEP);
11817c478bd9Sstevel@tonic-gate 	np = (in_node_t *)cp;
11827c478bd9Sstevel@tonic-gate 	if (name) {
11837c478bd9Sstevel@tonic-gate 		np->in_node_name = cp + sizeof (in_node_t);
11847c478bd9Sstevel@tonic-gate 		(void) strcpy(np->in_node_name, name);
11857c478bd9Sstevel@tonic-gate 	}
11867c478bd9Sstevel@tonic-gate 	np->in_unit_addr = cp + sizeof (in_node_t) + namelen;
11877c478bd9Sstevel@tonic-gate 	(void) strcpy(np->in_unit_addr, addr);
11887c478bd9Sstevel@tonic-gate 	return (np);
11897c478bd9Sstevel@tonic-gate }
11907c478bd9Sstevel@tonic-gate 
11917c478bd9Sstevel@tonic-gate /*
11927c478bd9Sstevel@tonic-gate  * Allocate a drv entry and storage for binding name string, and fill it in.
11937c478bd9Sstevel@tonic-gate  */
11947c478bd9Sstevel@tonic-gate static in_drv_t *
11957c478bd9Sstevel@tonic-gate in_alloc_drv(char *bindingname)
11967c478bd9Sstevel@tonic-gate {
11977c478bd9Sstevel@tonic-gate 	in_drv_t *dp;
11987c478bd9Sstevel@tonic-gate 	char *cp;
11997c478bd9Sstevel@tonic-gate 	size_t namelen;
12007c478bd9Sstevel@tonic-gate 
12017c478bd9Sstevel@tonic-gate 	ASSERT(e_ddi_inst_state.ins_busy);
12027c478bd9Sstevel@tonic-gate 	/*
12037c478bd9Sstevel@tonic-gate 	 * Has name or will become root
12047c478bd9Sstevel@tonic-gate 	 */
12057c478bd9Sstevel@tonic-gate 	ASSERT(bindingname || e_ddi_inst_state.ins_root == NULL);
12067c478bd9Sstevel@tonic-gate 	if (bindingname == NULL)
12077c478bd9Sstevel@tonic-gate 		namelen = 0;
12087c478bd9Sstevel@tonic-gate 	else
12097c478bd9Sstevel@tonic-gate 		namelen = strlen(bindingname) + 1;
12107c478bd9Sstevel@tonic-gate 	cp = kmem_zalloc(sizeof (in_drv_t) + namelen, KM_SLEEP);
12117c478bd9Sstevel@tonic-gate 	dp = (in_drv_t *)cp;
12127c478bd9Sstevel@tonic-gate 	if (bindingname) {
12137c478bd9Sstevel@tonic-gate 		dp->ind_driver_name = cp + sizeof (in_drv_t);
12147c478bd9Sstevel@tonic-gate 		(void) strcpy(dp->ind_driver_name, bindingname);
12157c478bd9Sstevel@tonic-gate 	}
12167c478bd9Sstevel@tonic-gate 	dp->ind_state = IN_UNKNOWN;
12177c478bd9Sstevel@tonic-gate 	dp->ind_instance = -1;
12187c478bd9Sstevel@tonic-gate 	return (dp);
12197c478bd9Sstevel@tonic-gate }
12207c478bd9Sstevel@tonic-gate 
12217c478bd9Sstevel@tonic-gate static void
12227c478bd9Sstevel@tonic-gate in_dealloc_node(in_node_t *np)
12237c478bd9Sstevel@tonic-gate {
12247c478bd9Sstevel@tonic-gate 	/*
12257c478bd9Sstevel@tonic-gate 	 * The root node can never be de-allocated
12267c478bd9Sstevel@tonic-gate 	 */
12277c478bd9Sstevel@tonic-gate 	ASSERT(np->in_node_name && np->in_unit_addr);
12287c478bd9Sstevel@tonic-gate 	ASSERT(e_ddi_inst_state.ins_busy);
12297c478bd9Sstevel@tonic-gate 	kmem_free(np, sizeof (in_node_t) + strlen(np->in_node_name)
12307c478bd9Sstevel@tonic-gate 	    + strlen(np->in_unit_addr) + 2);
12317c478bd9Sstevel@tonic-gate }
12327c478bd9Sstevel@tonic-gate 
12337c478bd9Sstevel@tonic-gate static void
12347c478bd9Sstevel@tonic-gate in_dealloc_drv(in_drv_t *dp)
12357c478bd9Sstevel@tonic-gate {
12367c478bd9Sstevel@tonic-gate 	ASSERT(dp->ind_driver_name);
12377c478bd9Sstevel@tonic-gate 	ASSERT(e_ddi_inst_state.ins_busy);
12387c478bd9Sstevel@tonic-gate 	kmem_free(dp, sizeof (in_drv_t) + strlen(dp->ind_driver_name)
12397c478bd9Sstevel@tonic-gate 	    + 1);
12407c478bd9Sstevel@tonic-gate }
12417c478bd9Sstevel@tonic-gate 
12427c478bd9Sstevel@tonic-gate /*
12437c478bd9Sstevel@tonic-gate  * Handle the various possible versions of "no address"
12447c478bd9Sstevel@tonic-gate  */
12457c478bd9Sstevel@tonic-gate static int
12467c478bd9Sstevel@tonic-gate in_eqstr(char *a, char *b)
12477c478bd9Sstevel@tonic-gate {
12487c478bd9Sstevel@tonic-gate 	if (a == b)	/* covers case where both are nulls */
12497c478bd9Sstevel@tonic-gate 		return (1);
12507c478bd9Sstevel@tonic-gate 	if (a == NULL && *b == 0)
12517c478bd9Sstevel@tonic-gate 		return (1);
12527c478bd9Sstevel@tonic-gate 	if (b == NULL && *a == 0)
12537c478bd9Sstevel@tonic-gate 		return (1);
12547c478bd9Sstevel@tonic-gate 	if (a == NULL || b == NULL)
12557c478bd9Sstevel@tonic-gate 		return (0);
12567c478bd9Sstevel@tonic-gate 	return (strcmp(a, b) == 0);
12577c478bd9Sstevel@tonic-gate }
12587c478bd9Sstevel@tonic-gate 
12597c478bd9Sstevel@tonic-gate /*
12607c478bd9Sstevel@tonic-gate  * Returns true if instance no. is already in use by named driver
12617c478bd9Sstevel@tonic-gate  */
12627c478bd9Sstevel@tonic-gate static int
12637c478bd9Sstevel@tonic-gate in_inuse(int instance, char *name)
12647c478bd9Sstevel@tonic-gate {
12657c478bd9Sstevel@tonic-gate 	major_t major;
12667c478bd9Sstevel@tonic-gate 	in_drv_t *dp;
12677c478bd9Sstevel@tonic-gate 	struct devnames *dnp;
12687c478bd9Sstevel@tonic-gate 
12697c478bd9Sstevel@tonic-gate 	ASSERT(e_ddi_inst_state.ins_busy);
12707c478bd9Sstevel@tonic-gate 	/*
12717c478bd9Sstevel@tonic-gate 	 * For now, if we've never heard of this device we assume it is not
12727c478bd9Sstevel@tonic-gate 	 * in use, since we can't tell
12737c478bd9Sstevel@tonic-gate 	 * XXX could do the weaker search through the nomajor list checking
12747c478bd9Sstevel@tonic-gate 	 * XXX for the same name
12757c478bd9Sstevel@tonic-gate 	 */
1276a204de77Scth 	if ((major = ddi_name_to_major(name)) == DDI_MAJOR_T_NONE)
12777c478bd9Sstevel@tonic-gate 		return (0);
12787c478bd9Sstevel@tonic-gate 	dnp = &devnamesp[major];
12797c478bd9Sstevel@tonic-gate 
12807c478bd9Sstevel@tonic-gate 	dp = dnp->dn_inlist;
12817c478bd9Sstevel@tonic-gate 	while (dp) {
12827c478bd9Sstevel@tonic-gate 		if (dp->ind_instance == instance)
12837c478bd9Sstevel@tonic-gate 			return (1);
12847c478bd9Sstevel@tonic-gate 		dp = dp->ind_next;
12857c478bd9Sstevel@tonic-gate 	}
12867c478bd9Sstevel@tonic-gate 	return (0);
12877c478bd9Sstevel@tonic-gate }
12887c478bd9Sstevel@tonic-gate 
12897c478bd9Sstevel@tonic-gate static void
12907c478bd9Sstevel@tonic-gate in_hashdrv(in_drv_t *dp)
12917c478bd9Sstevel@tonic-gate {
12927c478bd9Sstevel@tonic-gate 	struct devnames *dnp;
12937c478bd9Sstevel@tonic-gate 	in_drv_t *mp, *pp;
12947c478bd9Sstevel@tonic-gate 	major_t major;
12957c478bd9Sstevel@tonic-gate 
12967c478bd9Sstevel@tonic-gate 	/* hash to no major list */
1297a204de77Scth 	major = ddi_name_to_major(dp->ind_driver_name);
1298a204de77Scth 	if (major == DDI_MAJOR_T_NONE) {
12997c478bd9Sstevel@tonic-gate 		dp->ind_next = e_ddi_inst_state.ins_no_major;
13007c478bd9Sstevel@tonic-gate 		e_ddi_inst_state.ins_no_major = dp;
13017c478bd9Sstevel@tonic-gate 		return;
13027c478bd9Sstevel@tonic-gate 	}
13037c478bd9Sstevel@tonic-gate 
13047c478bd9Sstevel@tonic-gate 	/*
13057c478bd9Sstevel@tonic-gate 	 * dnp->dn_inlist is sorted by instance number.
13067c478bd9Sstevel@tonic-gate 	 * Adding a new instance entry may introduce holes,
13077c478bd9Sstevel@tonic-gate 	 * set dn_instance to IN_SEARCHME so the next instance
13087c478bd9Sstevel@tonic-gate 	 * assignment may fill in holes.
13097c478bd9Sstevel@tonic-gate 	 */
13107c478bd9Sstevel@tonic-gate 	dnp = &devnamesp[major];
13117c478bd9Sstevel@tonic-gate 	pp = mp = dnp->dn_inlist;
13127c478bd9Sstevel@tonic-gate 	if (mp == NULL || dp->ind_instance < mp->ind_instance) {
13137c478bd9Sstevel@tonic-gate 		/* prepend as the first entry, turn on IN_SEARCHME */
13147c478bd9Sstevel@tonic-gate 		dnp->dn_instance = IN_SEARCHME;
13157c478bd9Sstevel@tonic-gate 		dp->ind_next = mp;
13167c478bd9Sstevel@tonic-gate 		dnp->dn_inlist = dp;
13177c478bd9Sstevel@tonic-gate 		return;
13187c478bd9Sstevel@tonic-gate 	}
13197c478bd9Sstevel@tonic-gate 
13207c478bd9Sstevel@tonic-gate 	ASSERT(mp->ind_instance != dp->ind_instance);
13217c478bd9Sstevel@tonic-gate 	while (mp->ind_instance < dp->ind_instance && mp->ind_next) {
13227c478bd9Sstevel@tonic-gate 		pp = mp;
13237c478bd9Sstevel@tonic-gate 		mp = mp->ind_next;
13247c478bd9Sstevel@tonic-gate 		ASSERT(mp->ind_instance != dp->ind_instance);
13257c478bd9Sstevel@tonic-gate 	}
13267c478bd9Sstevel@tonic-gate 
13277c478bd9Sstevel@tonic-gate 	if (mp->ind_instance < dp->ind_instance) { /* end of list */
13287c478bd9Sstevel@tonic-gate 		dp->ind_next = NULL;
13297c478bd9Sstevel@tonic-gate 		mp->ind_next = dp;
13307c478bd9Sstevel@tonic-gate 	} else {
13317c478bd9Sstevel@tonic-gate 		ASSERT(dnp->dn_instance == IN_SEARCHME);
13327c478bd9Sstevel@tonic-gate 		dp->ind_next = pp->ind_next;
13337c478bd9Sstevel@tonic-gate 		pp->ind_next = dp;
13347c478bd9Sstevel@tonic-gate 	}
13357c478bd9Sstevel@tonic-gate }
13367c478bd9Sstevel@tonic-gate 
13377c478bd9Sstevel@tonic-gate /*
13387c478bd9Sstevel@tonic-gate  * Remove a driver entry from the list, given a previous pointer
13397c478bd9Sstevel@tonic-gate  */
13407c478bd9Sstevel@tonic-gate static void
13417c478bd9Sstevel@tonic-gate in_removedrv(struct devnames *dnp, in_drv_t *mp)
13427c478bd9Sstevel@tonic-gate {
13437c478bd9Sstevel@tonic-gate 	in_drv_t *dp;
13447c478bd9Sstevel@tonic-gate 	in_drv_t *prevp;
13457c478bd9Sstevel@tonic-gate 
13467c478bd9Sstevel@tonic-gate 	if (dnp->dn_inlist == mp) {	/* head of list */
13477c478bd9Sstevel@tonic-gate 		dnp->dn_inlist = mp->ind_next;
13487c478bd9Sstevel@tonic-gate 		dnp->dn_instance = IN_SEARCHME;
13497c478bd9Sstevel@tonic-gate 		in_dq_drv(mp);
13507c478bd9Sstevel@tonic-gate 		in_dealloc_drv(mp);
13517c478bd9Sstevel@tonic-gate 		return;
13527c478bd9Sstevel@tonic-gate 	}
13537c478bd9Sstevel@tonic-gate 	prevp = dnp->dn_inlist;
13547c478bd9Sstevel@tonic-gate 	for (dp = prevp->ind_next; dp; dp = dp->ind_next) {
13557c478bd9Sstevel@tonic-gate 		if (dp == mp) {		/* found it */
13567c478bd9Sstevel@tonic-gate 			break;
13577c478bd9Sstevel@tonic-gate 		}
13587c478bd9Sstevel@tonic-gate 		prevp = dp;
13597c478bd9Sstevel@tonic-gate 	}
13607c478bd9Sstevel@tonic-gate 
13617c478bd9Sstevel@tonic-gate 	ASSERT(dp == mp);
13627c478bd9Sstevel@tonic-gate 	dnp->dn_instance = IN_SEARCHME;
13637c478bd9Sstevel@tonic-gate 	prevp->ind_next = mp->ind_next;
13647c478bd9Sstevel@tonic-gate 	in_dq_drv(mp);
13657c478bd9Sstevel@tonic-gate 	in_dealloc_drv(mp);
13667c478bd9Sstevel@tonic-gate }
13677c478bd9Sstevel@tonic-gate 
13687c478bd9Sstevel@tonic-gate static void
13697c478bd9Sstevel@tonic-gate in_dq_drv(in_drv_t *mp)
13707c478bd9Sstevel@tonic-gate {
13717c478bd9Sstevel@tonic-gate 	struct in_node *node = mp->ind_node;
13727c478bd9Sstevel@tonic-gate 	in_drv_t *ptr, *prev;
13737c478bd9Sstevel@tonic-gate 
13747c478bd9Sstevel@tonic-gate 	if (mp == node->in_drivers) {
13757c478bd9Sstevel@tonic-gate 		node->in_drivers = mp->ind_next_drv;
13767c478bd9Sstevel@tonic-gate 		return;
13777c478bd9Sstevel@tonic-gate 	}
13787c478bd9Sstevel@tonic-gate 	prev = node->in_drivers;
13797c478bd9Sstevel@tonic-gate 	for (ptr = prev->ind_next_drv; ptr != (struct in_drv *)NULL;
13807c478bd9Sstevel@tonic-gate 	    ptr = ptr->ind_next_drv) {
13817c478bd9Sstevel@tonic-gate 		if (ptr == mp) {
13827c478bd9Sstevel@tonic-gate 			prev->ind_next_drv = ptr->ind_next_drv;
13837c478bd9Sstevel@tonic-gate 			return;
13847c478bd9Sstevel@tonic-gate 		}
1385ffc89d77Svikram 		prev = ptr;
13867c478bd9Sstevel@tonic-gate 	}
13877c478bd9Sstevel@tonic-gate 	panic("in_dq_drv: in_drv not found on node driver list");
13887c478bd9Sstevel@tonic-gate }
13897c478bd9Sstevel@tonic-gate 
13907c478bd9Sstevel@tonic-gate 
13917c478bd9Sstevel@tonic-gate in_drv_t *
13927c478bd9Sstevel@tonic-gate in_drvwalk(in_node_t *np, char *binding_name)
13937c478bd9Sstevel@tonic-gate {
13947c478bd9Sstevel@tonic-gate 	char *name;
13957c478bd9Sstevel@tonic-gate 	in_drv_t *dp = np->in_drivers;
13967c478bd9Sstevel@tonic-gate 	while (dp) {
13977c478bd9Sstevel@tonic-gate 		if ((name = i_binding_to_drv_name(dp->ind_driver_name))
13987c478bd9Sstevel@tonic-gate 		    == NULL) {
13997c478bd9Sstevel@tonic-gate 			name = dp->ind_driver_name;
14007c478bd9Sstevel@tonic-gate 		}
14017c478bd9Sstevel@tonic-gate 		if (strcmp(binding_name, name) == 0) {
14027c478bd9Sstevel@tonic-gate 			break;
14037c478bd9Sstevel@tonic-gate 		}
14047c478bd9Sstevel@tonic-gate 		dp = dp->ind_next_drv;
14057c478bd9Sstevel@tonic-gate 	}
14067c478bd9Sstevel@tonic-gate 	return (dp);
14077c478bd9Sstevel@tonic-gate }
14087c478bd9Sstevel@tonic-gate 
14097c478bd9Sstevel@tonic-gate 
14107c478bd9Sstevel@tonic-gate 
14117c478bd9Sstevel@tonic-gate static void
14127c478bd9Sstevel@tonic-gate i_log_devfs_instance_mod(void)
14137c478bd9Sstevel@tonic-gate {
14147c478bd9Sstevel@tonic-gate 	sysevent_t	*ev;
14157c478bd9Sstevel@tonic-gate 	sysevent_id_t	eid;
141616bd7258Scth 	static int	sent_one = 0;
14177c478bd9Sstevel@tonic-gate 
14187c478bd9Sstevel@tonic-gate 	/*
141916bd7258Scth 	 * Prevent unnecessary event generation.  Do not generate more than
142016bd7258Scth 	 * one event during boot.
14217c478bd9Sstevel@tonic-gate 	 */
142216bd7258Scth 	if (sent_one && !i_ddi_io_initialized())
14237c478bd9Sstevel@tonic-gate 		return;
14247c478bd9Sstevel@tonic-gate 
14257c478bd9Sstevel@tonic-gate 	ev = sysevent_alloc(EC_DEVFS, ESC_DEVFS_INSTANCE_MOD, EP_DDI,
14267c478bd9Sstevel@tonic-gate 	    SE_NOSLEEP);
14277c478bd9Sstevel@tonic-gate 	if (ev == NULL) {
14287c478bd9Sstevel@tonic-gate 		return;
14297c478bd9Sstevel@tonic-gate 	}
14307c478bd9Sstevel@tonic-gate 	if (log_sysevent(ev, SE_NOSLEEP, &eid) != 0) {
14317c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "i_log_devfs_instance_mod: failed to post "
14327c478bd9Sstevel@tonic-gate 		    "event");
143316bd7258Scth 	} else {
143416bd7258Scth 		sent_one = 1;
14357c478bd9Sstevel@tonic-gate 	}
14367c478bd9Sstevel@tonic-gate 	sysevent_free(ev);
14377c478bd9Sstevel@tonic-gate }
14387c478bd9Sstevel@tonic-gate 
14397c478bd9Sstevel@tonic-gate void
144094c894bbSVikram Hegde e_ddi_enter_instance(void)
14417c478bd9Sstevel@tonic-gate {
14427c478bd9Sstevel@tonic-gate 	mutex_enter(&e_ddi_inst_state.ins_serial);
14437c478bd9Sstevel@tonic-gate 	if (e_ddi_inst_state.ins_thread == curthread)
14447c478bd9Sstevel@tonic-gate 		e_ddi_inst_state.ins_busy++;
14457c478bd9Sstevel@tonic-gate 	else {
14467c478bd9Sstevel@tonic-gate 		while (e_ddi_inst_state.ins_busy)
14477c478bd9Sstevel@tonic-gate 			cv_wait(&e_ddi_inst_state.ins_serial_cv,
14487c478bd9Sstevel@tonic-gate 			    &e_ddi_inst_state.ins_serial);
14497c478bd9Sstevel@tonic-gate 		e_ddi_inst_state.ins_thread = curthread;
14507c478bd9Sstevel@tonic-gate 		e_ddi_inst_state.ins_busy = 1;
14517c478bd9Sstevel@tonic-gate 	}
14527c478bd9Sstevel@tonic-gate 	mutex_exit(&e_ddi_inst_state.ins_serial);
14537c478bd9Sstevel@tonic-gate }
14547c478bd9Sstevel@tonic-gate 
14557c478bd9Sstevel@tonic-gate void
145694c894bbSVikram Hegde e_ddi_exit_instance(void)
14577c478bd9Sstevel@tonic-gate {
14587c478bd9Sstevel@tonic-gate 	mutex_enter(&e_ddi_inst_state.ins_serial);
14597c478bd9Sstevel@tonic-gate 	e_ddi_inst_state.ins_busy--;
14607c478bd9Sstevel@tonic-gate 	if (e_ddi_inst_state.ins_busy == 0) {
14617c478bd9Sstevel@tonic-gate 		cv_broadcast(&e_ddi_inst_state.ins_serial_cv);
14627c478bd9Sstevel@tonic-gate 		e_ddi_inst_state.ins_thread = NULL;
14637c478bd9Sstevel@tonic-gate 	}
14647c478bd9Sstevel@tonic-gate 	mutex_exit(&e_ddi_inst_state.ins_serial);
14657c478bd9Sstevel@tonic-gate }
14667c478bd9Sstevel@tonic-gate 
14677c478bd9Sstevel@tonic-gate int
146894c894bbSVikram Hegde e_ddi_instance_is_clean(void)
14697c478bd9Sstevel@tonic-gate {
147094c894bbSVikram Hegde 	return (e_ddi_inst_state.ins_dirty == B_FALSE);
14717c478bd9Sstevel@tonic-gate }
14727c478bd9Sstevel@tonic-gate 
14737c478bd9Sstevel@tonic-gate void
147494c894bbSVikram Hegde e_ddi_instance_set_clean(void)
14757c478bd9Sstevel@tonic-gate {
147694c894bbSVikram Hegde 	e_ddi_inst_state.ins_dirty = B_FALSE;
14777c478bd9Sstevel@tonic-gate }
14787c478bd9Sstevel@tonic-gate 
14797c478bd9Sstevel@tonic-gate in_node_t *
148094c894bbSVikram Hegde e_ddi_instance_root(void)
14817c478bd9Sstevel@tonic-gate {
14827c478bd9Sstevel@tonic-gate 	return (e_ddi_inst_state.ins_root);
14837c478bd9Sstevel@tonic-gate }
14847c478bd9Sstevel@tonic-gate 
14857c478bd9Sstevel@tonic-gate /*
14867c478bd9Sstevel@tonic-gate  * Visit a node in the instance tree
14877c478bd9Sstevel@tonic-gate  */
14887c478bd9Sstevel@tonic-gate static int
14897c478bd9Sstevel@tonic-gate in_walk_instances(in_node_t *np, char *path, char *this,
14907c478bd9Sstevel@tonic-gate     int (*f)(const char *, in_node_t *, in_drv_t *, void *), void *arg)
14917c478bd9Sstevel@tonic-gate {
14927c478bd9Sstevel@tonic-gate 	in_drv_t *dp;
14937c478bd9Sstevel@tonic-gate 	int rval = INST_WALK_CONTINUE;
14947c478bd9Sstevel@tonic-gate 	char *next;
14957c478bd9Sstevel@tonic-gate 
14967c478bd9Sstevel@tonic-gate 	while (np != NULL) {
14977c478bd9Sstevel@tonic-gate 
14987c478bd9Sstevel@tonic-gate 		if (np->in_unit_addr[0] == 0)
14997c478bd9Sstevel@tonic-gate 			(void) sprintf(this, "/%s", np->in_node_name);
15007c478bd9Sstevel@tonic-gate 		else
15017c478bd9Sstevel@tonic-gate 			(void) sprintf(this, "/%s@%s", np->in_node_name,
15027c478bd9Sstevel@tonic-gate 			    np->in_unit_addr);
15037c478bd9Sstevel@tonic-gate 		next = this + strlen(this);
15047c478bd9Sstevel@tonic-gate 
15057c478bd9Sstevel@tonic-gate 		for (dp = np->in_drivers; dp; dp = dp->ind_next_drv) {
15067c478bd9Sstevel@tonic-gate 			if (dp->ind_state == IN_PERMANENT) {
15077c478bd9Sstevel@tonic-gate 				rval = (*f)(path, np, dp, arg);
15087c478bd9Sstevel@tonic-gate 				if (rval == INST_WALK_TERMINATE)
15097c478bd9Sstevel@tonic-gate 					break;
15107c478bd9Sstevel@tonic-gate 			}
15117c478bd9Sstevel@tonic-gate 		}
151294c894bbSVikram Hegde 
15137c478bd9Sstevel@tonic-gate 		if (np->in_child) {
15147c478bd9Sstevel@tonic-gate 			rval = in_walk_instances(np->in_child,
15157c478bd9Sstevel@tonic-gate 			    path, next, f, arg);
15167c478bd9Sstevel@tonic-gate 			if (rval == INST_WALK_TERMINATE)
15177c478bd9Sstevel@tonic-gate 				break;
15187c478bd9Sstevel@tonic-gate 		}
15197c478bd9Sstevel@tonic-gate 
15207c478bd9Sstevel@tonic-gate 		np = np->in_sibling;
15217c478bd9Sstevel@tonic-gate 	}
15227c478bd9Sstevel@tonic-gate 
15237c478bd9Sstevel@tonic-gate 	return (rval);
15247c478bd9Sstevel@tonic-gate }
15257c478bd9Sstevel@tonic-gate 
15267c478bd9Sstevel@tonic-gate /*
15277c478bd9Sstevel@tonic-gate  * A general interface for walking the instance tree,
15287c478bd9Sstevel@tonic-gate  * calling a user-supplied callback for each node.
15297c478bd9Sstevel@tonic-gate  */
15307c478bd9Sstevel@tonic-gate int
15317c478bd9Sstevel@tonic-gate e_ddi_walk_instances(int (*f)(const char *,
15327c478bd9Sstevel@tonic-gate 	in_node_t *, in_drv_t *, void *), void *arg)
15337c478bd9Sstevel@tonic-gate {
15347c478bd9Sstevel@tonic-gate 	in_node_t *root;
15357c478bd9Sstevel@tonic-gate 	int rval;
15367c478bd9Sstevel@tonic-gate 	char *path;
15377c478bd9Sstevel@tonic-gate 
15387c478bd9Sstevel@tonic-gate 	path = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
15397c478bd9Sstevel@tonic-gate 
15407c478bd9Sstevel@tonic-gate 	e_ddi_enter_instance();
15417c478bd9Sstevel@tonic-gate 	root = e_ddi_instance_root();
15427c478bd9Sstevel@tonic-gate 	rval = in_walk_instances(root->in_child, path, path, f, arg);
154394c894bbSVikram Hegde 
15447c478bd9Sstevel@tonic-gate 	e_ddi_exit_instance();
15457c478bd9Sstevel@tonic-gate 
15467c478bd9Sstevel@tonic-gate 	kmem_free(path, MAXPATHLEN);
15477c478bd9Sstevel@tonic-gate 	return (rval);
15487c478bd9Sstevel@tonic-gate }
154994c894bbSVikram Hegde 
155094c894bbSVikram Hegde in_node_t *
155194c894bbSVikram Hegde e_ddi_path_to_instance(char *path)
155294c894bbSVikram Hegde {
155394c894bbSVikram Hegde 	in_node_t *np;
155494c894bbSVikram Hegde 
155594c894bbSVikram Hegde 	np = in_make_path(path);
155694c894bbSVikram Hegde 	if (np && np->in_drivers && np->in_drivers->ind_state == IN_PERMANENT) {
155794c894bbSVikram Hegde 		return (np);
155894c894bbSVikram Hegde 	}
155994c894bbSVikram Hegde 	return (NULL);
156094c894bbSVikram Hegde }
156194c894bbSVikram Hegde 
156294c894bbSVikram Hegde void
156394c894bbSVikram Hegde e_ddi_borrow_instance(dev_info_t *cdip, in_node_t *cnp)
156494c894bbSVikram Hegde {
156594c894bbSVikram Hegde 	char		*alias;
156694c894bbSVikram Hegde 	in_node_t	*anp;
156794c894bbSVikram Hegde 	char		*curr = kmem_alloc(MAXPATHLEN, KM_NOSLEEP);
156894c894bbSVikram Hegde 
156994c894bbSVikram Hegde 	if (curr == NULL) {
157094c894bbSVikram Hegde 		ddi_err(DER_PANIC, cdip, "curr alloc failed");
157194c894bbSVikram Hegde 		/*NOTREACHED*/
157294c894bbSVikram Hegde 	}
157394c894bbSVikram Hegde 
157494c894bbSVikram Hegde 	(void) ddi_pathname(cdip, curr);
157594c894bbSVikram Hegde 
157694c894bbSVikram Hegde 	if (cnp->in_drivers) {
157794c894bbSVikram Hegde 		ddi_err(DER_PANIC, cdip, "cnp has instance: %p", cnp);
157894c894bbSVikram Hegde 		/*NOTREACHED*/
157994c894bbSVikram Hegde 	}
158094c894bbSVikram Hegde 
158194c894bbSVikram Hegde 	alias = ddi_curr_redirect(curr);
158294c894bbSVikram Hegde 	kmem_free(curr, MAXPATHLEN);
158394c894bbSVikram Hegde 
158494c894bbSVikram Hegde 	if (alias && (anp = e_ddi_path_to_instance(alias)) != NULL) {
158594c894bbSVikram Hegde 		cnp->in_drivers = anp->in_drivers;
158694c894bbSVikram Hegde 		anp->in_drivers = NULL;
158794c894bbSVikram Hegde 	}
158894c894bbSVikram Hegde }
158994c894bbSVikram Hegde 
159094c894bbSVikram Hegde void
159194c894bbSVikram Hegde e_ddi_return_instance(dev_info_t *cdip, char *addr, in_node_t *cnp)
159294c894bbSVikram Hegde {
159394c894bbSVikram Hegde 	in_node_t	*anp;
159494c894bbSVikram Hegde 	char 		*alias;
159594c894bbSVikram Hegde 	char		*curr = kmem_alloc(MAXPATHLEN, KM_NOSLEEP);
159694c894bbSVikram Hegde 
159794c894bbSVikram Hegde 	if (curr == NULL) {
159894c894bbSVikram Hegde 		ddi_err(DER_PANIC, cdip, "alloc of curr failed");
159994c894bbSVikram Hegde 		/*NOTREACHED*/
160094c894bbSVikram Hegde 	}
160194c894bbSVikram Hegde 
160294c894bbSVikram Hegde 	(void) ddi_pathname(cdip, curr);
160394c894bbSVikram Hegde 	if (addr) {
160494c894bbSVikram Hegde 		(void) strlcat(curr, "@", MAXPATHLEN);
160594c894bbSVikram Hegde 		(void) strlcat(curr, addr, MAXPATHLEN);
160694c894bbSVikram Hegde 
160794c894bbSVikram Hegde 	}
160894c894bbSVikram Hegde 	if (cnp->in_drivers == NULL) {
160994c894bbSVikram Hegde 		ddi_err(DER_PANIC, cdip, "cnp has no inst: %p", cnp);
161094c894bbSVikram Hegde 		/*NOTREACHED*/
161194c894bbSVikram Hegde 	}
161294c894bbSVikram Hegde 
161394c894bbSVikram Hegde 	alias = ddi_curr_redirect(curr);
161494c894bbSVikram Hegde 	kmem_free(curr, MAXPATHLEN);
161594c894bbSVikram Hegde 
161694c894bbSVikram Hegde 	if (alias && (anp = e_ddi_path_to_instance(alias)) != NULL) {
161794c894bbSVikram Hegde 		ASSERT(anp->in_drivers == NULL);
161894c894bbSVikram Hegde 		anp->in_drivers = cnp->in_drivers;
161994c894bbSVikram Hegde 		cnp->in_drivers = NULL;
162094c894bbSVikram Hegde 	}
162194c894bbSVikram Hegde }
1622