xref: /titanic_50/usr/src/uts/common/os/instance.c (revision 328d222be1e6f8291e6805ec610012ec96e249ec)
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 
132*328d222bSChris Horne int	instance_searchme = 0;	/* testing: use complex code path */
13394c894bbSVikram Hegde 
1347c478bd9Sstevel@tonic-gate /*
1357c478bd9Sstevel@tonic-gate  * Path to instance file magic string used for first time boot after
1367c478bd9Sstevel@tonic-gate  * an install.  If this is the first string in the file we will
1377c478bd9Sstevel@tonic-gate  * automatically rebuild the file.
1387c478bd9Sstevel@tonic-gate  */
1397c478bd9Sstevel@tonic-gate #define	PTI_MAGIC_STR		"#path_to_inst_bootstrap_1"
1407c478bd9Sstevel@tonic-gate #define	PTI_MAGIC_STR_LEN	(sizeof (PTI_MAGIC_STR) - 1)
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate void
1437c478bd9Sstevel@tonic-gate e_ddi_instance_init(void)
1447c478bd9Sstevel@tonic-gate {
1457c478bd9Sstevel@tonic-gate 	char *file;
1467c478bd9Sstevel@tonic-gate 	int rebuild = 1;
1477c478bd9Sstevel@tonic-gate 	struct in_drv *dp;
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	mutex_init(&e_ddi_inst_state.ins_serial, NULL, MUTEX_DEFAULT, NULL);
1507c478bd9Sstevel@tonic-gate 	cv_init(&e_ddi_inst_state.ins_serial_cv, NULL, CV_DEFAULT, NULL);
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	/*
1537c478bd9Sstevel@tonic-gate 	 * Only one thread is allowed to change the state of the instance
1547c478bd9Sstevel@tonic-gate 	 * number assignments on the system at any given time.
1557c478bd9Sstevel@tonic-gate 	 * Note that this is not really necessary, as we are single-threaded
1567c478bd9Sstevel@tonic-gate 	 * here, but it won't hurt, and it allows us to keep ASSERTS for
1577c478bd9Sstevel@tonic-gate 	 * our assumptions in the code.
1587c478bd9Sstevel@tonic-gate 	 */
1597c478bd9Sstevel@tonic-gate 	e_ddi_enter_instance();
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 	/*
16294c894bbSVikram Hegde 	 * Init the ioaliases if the platform supports it
16394c894bbSVikram Hegde 	 */
16494c894bbSVikram Hegde 	if (&plat_ioaliases_init)
16594c894bbSVikram Hegde 		plat_ioaliases_init();
16694c894bbSVikram Hegde 
16794c894bbSVikram Hegde 	/*
1687c478bd9Sstevel@tonic-gate 	 * Create the root node, instance zallocs to 0.
1697c478bd9Sstevel@tonic-gate 	 * The name and address of this node never get examined, we always
1707c478bd9Sstevel@tonic-gate 	 * start searching with its first child.
1717c478bd9Sstevel@tonic-gate 	 */
1727c478bd9Sstevel@tonic-gate 	ASSERT(e_ddi_inst_state.ins_root == NULL);
1737c478bd9Sstevel@tonic-gate 	e_ddi_inst_state.ins_root = in_alloc_node(NULL, NULL);
1747c478bd9Sstevel@tonic-gate 	dp = in_alloc_drv("rootnex");
1757c478bd9Sstevel@tonic-gate 	in_endrv(e_ddi_inst_state.ins_root, dp);
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	file = instance_file;
1787c478bd9Sstevel@tonic-gate 	switch (in_get_infile(file)) {
1797c478bd9Sstevel@tonic-gate 	default:
1807c478bd9Sstevel@tonic-gate 	case PTI_NOT_FOUND:
1817c478bd9Sstevel@tonic-gate 		/* make sure path_to_inst is recreated */
1827c478bd9Sstevel@tonic-gate 		boothowto |= RB_RECONFIG;
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 		/*
1857c478bd9Sstevel@tonic-gate 		 * Something is wrong. First try the backup file.
1867c478bd9Sstevel@tonic-gate 		 * If not found, rebuild path_to_inst. Emit a
1877c478bd9Sstevel@tonic-gate 		 * message about the problem.
1887c478bd9Sstevel@tonic-gate 		 */
1897c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s empty or not found", file);
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 		file = instance_file_backup;
1927c478bd9Sstevel@tonic-gate 		if (in_get_infile(file) != PTI_FOUND) {
1937c478bd9Sstevel@tonic-gate 			cmn_err(CE_NOTE, "rebuilding device instance data");
1947c478bd9Sstevel@tonic-gate 			break;
1957c478bd9Sstevel@tonic-gate 		}
1967c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "using backup instance data in %s", file);
1977c478bd9Sstevel@tonic-gate 		/*FALLTHROUGH*/
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 	case PTI_FOUND:
2007c478bd9Sstevel@tonic-gate 		/*
2017c478bd9Sstevel@tonic-gate 		 * We've got a readable file
2027c478bd9Sstevel@tonic-gate 		 * parse the file into the instance tree
2037c478bd9Sstevel@tonic-gate 		 */
2047c478bd9Sstevel@tonic-gate 		(void) read_binding_file(file, NULL, in_pathin);
2057c478bd9Sstevel@tonic-gate 		rebuild = 0;
2067c478bd9Sstevel@tonic-gate 		break;
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	case PTI_REBUILD:
20994c894bbSVikram Hegde 		/*
21094c894bbSVikram Hegde 		 * path_to_inst has magic str requesting a create
21194c894bbSVikram Hegde 		 * Convert boot to reconfig boot to ensure /dev is
21294c894bbSVikram Hegde 		 * in sync with new path_to_inst.
21394c894bbSVikram Hegde 		 */
21494c894bbSVikram Hegde 		boothowto |= RB_RECONFIG;
2157c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT,
2167c478bd9Sstevel@tonic-gate 		    "?Using default device instance data\n");
2177c478bd9Sstevel@tonic-gate 		break;
2187c478bd9Sstevel@tonic-gate 	}
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 	/*
2217c478bd9Sstevel@tonic-gate 	 * The OBP device tree has been copied to the kernel and
2227c478bd9Sstevel@tonic-gate 	 * bound to drivers at this point. We walk the per-driver
2237c478bd9Sstevel@tonic-gate 	 * list to preassign instances. Since the bus addr is
2247c478bd9Sstevel@tonic-gate 	 * unknown at this point, we cannot place the instance
2257c478bd9Sstevel@tonic-gate 	 * number in the instance tree. This will be done at
2267c478bd9Sstevel@tonic-gate 	 * a later time.
2277c478bd9Sstevel@tonic-gate 	 */
2287c478bd9Sstevel@tonic-gate 	if (rebuild)
2297c478bd9Sstevel@tonic-gate 		in_preassign_instance();
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 	e_ddi_exit_instance();
2327c478bd9Sstevel@tonic-gate }
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate static void
2357c478bd9Sstevel@tonic-gate in_preassign_instance()
2367c478bd9Sstevel@tonic-gate {
2377c478bd9Sstevel@tonic-gate 	major_t		m;
238*328d222bSChris Horne 	struct devnames	*dnp;
239*328d222bSChris Horne 	dev_info_t	*dip;
2407c478bd9Sstevel@tonic-gate 	extern major_t	devcnt;
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 	for (m = 0; m < devcnt; m++) {
243*328d222bSChris Horne 		dnp = &devnamesp[m];
244*328d222bSChris Horne 		dip = dnp->dn_head;
2457c478bd9Sstevel@tonic-gate 		while (dip) {
2467c478bd9Sstevel@tonic-gate 			DEVI(dip)->devi_instance = dnp->dn_instance;
2477c478bd9Sstevel@tonic-gate 			dnp->dn_instance++;
2487c478bd9Sstevel@tonic-gate 			dip = ddi_get_next(dip);
2497c478bd9Sstevel@tonic-gate 		}
250*328d222bSChris Horne 
251*328d222bSChris Horne 		/*
252*328d222bSChris Horne 		 * The preassign instance numbers are not fully
253*328d222bSChris Horne 		 * accounted for until e_ddi_assign_instance().
254*328d222bSChris Horne 		 * We can't fully account for them now because we
255*328d222bSChris Horne 		 * don't currently have a unit-address. Because of
256*328d222bSChris Horne 		 * this, we need to remember the preassign boundary
257*328d222bSChris Horne 		 * to avoid ordering issues related to
258*328d222bSChris Horne 		 * e_ddi_assign_instance of a preassigned value .vs.
259*328d222bSChris Horne 		 * re-assignment of the same value for a dynamic
260*328d222bSChris Horne 		 * SID node created by bus_config.
261*328d222bSChris Horne 		 */
262*328d222bSChris Horne 		dnp->dn_pinstance = dnp->dn_instance;
2637c478bd9Sstevel@tonic-gate 	}
2647c478bd9Sstevel@tonic-gate }
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate /*
2677c478bd9Sstevel@tonic-gate  * Checks to see if the /etc/path_to_inst file exists and whether or not
2687c478bd9Sstevel@tonic-gate  * it has the magic string in it.
2697c478bd9Sstevel@tonic-gate  *
2707c478bd9Sstevel@tonic-gate  * Returns one of the following:
2717c478bd9Sstevel@tonic-gate  *
2727c478bd9Sstevel@tonic-gate  *	PTI_FOUND	- We have found the /etc/path_to_inst file
2737c478bd9Sstevel@tonic-gate  *	PTI_REBUILD	- We have found the /etc/path_to_inst file and the
2747c478bd9Sstevel@tonic-gate  *			  first line was PTI_MAGIC_STR.
2757c478bd9Sstevel@tonic-gate  *	PTI_NOT_FOUND	- We did not find the /etc/path_to_inst file
2767c478bd9Sstevel@tonic-gate  *
2777c478bd9Sstevel@tonic-gate  */
2787c478bd9Sstevel@tonic-gate static int
2797c478bd9Sstevel@tonic-gate in_get_infile(char *filename)
2807c478bd9Sstevel@tonic-gate {
281986fd29aSsetje 	struct _buf *file;
2827c478bd9Sstevel@tonic-gate 	int return_val;
2837c478bd9Sstevel@tonic-gate 	char buf[PTI_MAGIC_STR_LEN];
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate 	/*
2867c478bd9Sstevel@tonic-gate 	 * Try to open the file.
2877c478bd9Sstevel@tonic-gate 	 */
288986fd29aSsetje 	if ((file = kobj_open_file(filename)) == (struct _buf *)-1) {
2897c478bd9Sstevel@tonic-gate 		return (PTI_NOT_FOUND);
2907c478bd9Sstevel@tonic-gate 	}
2917c478bd9Sstevel@tonic-gate 	return_val = PTI_FOUND;
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 	/*
2947c478bd9Sstevel@tonic-gate 	 * Read the first PTI_MAGIC_STR_LEN bytes from the file to see if
2957c478bd9Sstevel@tonic-gate 	 * it contains the magic string.  If there aren't that many bytes
2967c478bd9Sstevel@tonic-gate 	 * in the file, then assume file is correct and no magic string
2977c478bd9Sstevel@tonic-gate 	 * and move on.
2987c478bd9Sstevel@tonic-gate 	 */
299986fd29aSsetje 	switch (kobj_read_file(file, buf, PTI_MAGIC_STR_LEN, 0)) {
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 	case PTI_MAGIC_STR_LEN:
3027c478bd9Sstevel@tonic-gate 		/*
3037c478bd9Sstevel@tonic-gate 		 * If the first PTI_MAGIC_STR_LEN bytes are the magic string
3047c478bd9Sstevel@tonic-gate 		 * then return PTI_REBUILD.
3057c478bd9Sstevel@tonic-gate 		 */
3067c478bd9Sstevel@tonic-gate 		if (strncmp(PTI_MAGIC_STR, buf, PTI_MAGIC_STR_LEN) == 0)
3077c478bd9Sstevel@tonic-gate 			return_val = PTI_REBUILD;
3087c478bd9Sstevel@tonic-gate 		break;
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate 	case 0:
3117c478bd9Sstevel@tonic-gate 		/*
3127c478bd9Sstevel@tonic-gate 		 * If the file is zero bytes in length, then consider the
3137c478bd9Sstevel@tonic-gate 		 * file to not be found
3147c478bd9Sstevel@tonic-gate 		 */
3157c478bd9Sstevel@tonic-gate 		return_val = PTI_NOT_FOUND;
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 	default: /* Do nothing we have a good file */
3187c478bd9Sstevel@tonic-gate 		break;
3197c478bd9Sstevel@tonic-gate 	}
3207c478bd9Sstevel@tonic-gate 
321986fd29aSsetje 	kobj_close_file(file);
3227c478bd9Sstevel@tonic-gate 	return (return_val);
3237c478bd9Sstevel@tonic-gate }
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate int
3267c478bd9Sstevel@tonic-gate is_pseudo_device(dev_info_t *dip)
3277c478bd9Sstevel@tonic-gate {
3287c478bd9Sstevel@tonic-gate 	dev_info_t	*pdip;
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 	for (pdip = ddi_get_parent(dip); pdip && pdip != ddi_root_node();
3317c478bd9Sstevel@tonic-gate 	    pdip = ddi_get_parent(pdip)) {
3327c478bd9Sstevel@tonic-gate 		if (strcmp(ddi_get_name(pdip), DEVI_PSEUDO_NEXNAME) == 0)
3337c478bd9Sstevel@tonic-gate 			return (1);
3347c478bd9Sstevel@tonic-gate 	}
3357c478bd9Sstevel@tonic-gate 	return (0);
3367c478bd9Sstevel@tonic-gate }
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate static void
3407c478bd9Sstevel@tonic-gate in_set_instance(dev_info_t *dip, in_drv_t *dp, major_t major)
3417c478bd9Sstevel@tonic-gate {
3427c478bd9Sstevel@tonic-gate 	/* use preassigned instance if available */
3437c478bd9Sstevel@tonic-gate 	if (DEVI(dip)->devi_instance != -1)
3447c478bd9Sstevel@tonic-gate 		dp->ind_instance = DEVI(dip)->devi_instance;
3457c478bd9Sstevel@tonic-gate 	else
3467c478bd9Sstevel@tonic-gate 		dp->ind_instance = in_next_instance(major);
3477c478bd9Sstevel@tonic-gate }
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate /*
35016bd7258Scth  * Return 1 if instance block was assigned for the path.
35116bd7258Scth  *
35216bd7258Scth  * For multi-port NIC cards, sequential instance assignment across all
35362a24de0SChris Horne  * ports on a card is highly desirable since the ppa is typically the
35416bd7258Scth  * same as the instance number, and the ppa is used in the NIC's public
35516bd7258Scth  * /dev name. This sequential assignment typically occurs as a result
35616bd7258Scth  * of in_preassign_instance() after initial install, or by
35716bd7258Scth  * i_ndi_init_hw_children() for NIC ports that share a common parent.
35816bd7258Scth  *
35916bd7258Scth  * Some NIC cards however use multi-function bridge chips, and to
36016bd7258Scth  * support sequential instance assignment accross all ports, without
36116bd7258Scth  * disabling multi-threaded attach, we have a (currently) undocumented
36216bd7258Scth  * hack to allocate instance numbers in contiguous blocks based on
36316bd7258Scth  * driver.conf properties.
36416bd7258Scth  *
36516bd7258Scth  *                       ^
36616bd7258Scth  *           /----------   ------------\
36716bd7258Scth  *        pci@0                      pci@0,1	MULTI-FUNCTION BRIDGE CHIP
36816bd7258Scth  *       /     \                    /       \
36916bd7258Scth  * FJSV,e4ta@4  FJSV,e4ta@4,1   FJSV,e4ta@6 FJSV,e4ta@6,1	NIC PORTS
37016bd7258Scth  *      n            n+2             n+2         n+3		INSTANCE
37116bd7258Scth  *
37216bd7258Scth  * For the above example, the following driver.conf properties would be
37316bd7258Scth  * used to guarantee sequential instance number assignment.
37416bd7258Scth  *
37516bd7258Scth  * ddi-instance-blocks ="ib-FJSVe4ca", "ib-FJSVe4ta", "ib-generic";
37616bd7258Scth  * ib-FJSVe4ca =	"/pci@0/FJSV,e4ca@4", "/pci@0/FJSV,e4ca@4,1",
37716bd7258Scth  *			"/pci@0,1/FJSV,e4ca@6", "/pci@0,1/FJSV,e4ca@6,1";
37816bd7258Scth  * ib-FJSVe4ta =	"/pci@0/FJSV,e4ta@4", "/pci@0/FJSV,e4ta@4,1",
37916bd7258Scth  *			"/pci@0,1/FJSV,e4ta@6", "/pci@0,1/FJSV,e4ta@6,1";
38016bd7258Scth  * ib-generic =		"/pci@0/network@4", "/pci@0/network@4,1",
38116bd7258Scth  *			"/pci@0,1/network@6", "/pci@0,1/network@6,1";
38216bd7258Scth  *
38316bd7258Scth  * The value of the 'ddi-instance-blocks' property references a series
38416bd7258Scth  * of card specific properties, like 'ib-FJSV-e4ta', who's value
38516bd7258Scth  * defines a single 'instance block'.  The 'instance block' describes
38616bd7258Scth  * all the paths below a multi-function bridge, where each path is
38716bd7258Scth  * called an 'instance path'.  The 'instance block' property value is a
38816bd7258Scth  * series of 'instance paths'.  The number of 'instance paths' in an
38916bd7258Scth  * 'instance block' defines the size of the instance block, and the
39016bd7258Scth  * ordering of the 'instance paths' defines the instance number
39116bd7258Scth  * assignment order for paths going through the 'instance block'.
39216bd7258Scth  *
39316bd7258Scth  * In the instance assignment code below, if a (path, driver) that
39416bd7258Scth  * currently has no instance number has a path that goes through an
39516bd7258Scth  * 'instance block', then block instance number allocation occurs.  The
39616bd7258Scth  * block allocation code will find a sequential set of unused instance
39716bd7258Scth  * numbers, and assign instance numbers for all the paths in the
39816bd7258Scth  * 'instance block'.  Each path is assigned a persistent instance
39916bd7258Scth  * number, even paths that don't exist in the device tree or fail
40016bd7258Scth  * probe(9E).
40116bd7258Scth  */
40216bd7258Scth static int
40316bd7258Scth in_assign_instance_block(dev_info_t *dip)
40416bd7258Scth {
40516bd7258Scth 	char		**ibn;		/* instance block names */
40616bd7258Scth 	uint_t		nibn;		/* number of instance block names */
40716bd7258Scth 	uint_t		ibni;		/* ibn index */
40816bd7258Scth 	char		*driver;
40916bd7258Scth 	major_t		major;
41016bd7258Scth 	char		*path;
41116bd7258Scth 	char		*addr;
41216bd7258Scth 	int		plen;
41316bd7258Scth 	char		**ibp;		/* instance block paths */
41416bd7258Scth 	uint_t		nibp;		/* number of paths in instance block */
41516bd7258Scth 	uint_t		ibpi;		/* ibp index */
41616bd7258Scth 	int		ibplen;		/* length of instance block path */
41716bd7258Scth 	char		*ipath;
41816bd7258Scth 	int		instance_base;
41916bd7258Scth 	int		splice;
42016bd7258Scth 	int		i;
42116bd7258Scth 
42216bd7258Scth 	/* check for fresh install case (in miniroot) */
42316bd7258Scth 	if (DEVI(dip)->devi_instance != -1)
42416bd7258Scth 		return (0);			/* already assigned */
42516bd7258Scth 
42616bd7258Scth 	/*
42716bd7258Scth 	 * Check to see if we need to allocate a block of contiguous instance
42816bd7258Scth 	 * numbers by looking for the 'ddi-instance-blocks' property.
42916bd7258Scth 	 */
43016bd7258Scth 	if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
43116bd7258Scth 	    "ddi-instance-blocks", &ibn, &nibn) != DDI_SUCCESS)
43216bd7258Scth 		return (0);			/* no instance block needed */
43316bd7258Scth 
43416bd7258Scth 	/*
43516bd7258Scth 	 * Get information out about node we are processing.
43616bd7258Scth 	 *
43716bd7258Scth 	 * NOTE: Since the node is not yet at DS_INITIALIZED, ddi_pathname()
43816bd7258Scth 	 * will not return the unit-address of the final path component even
43916bd7258Scth 	 * though the node has an established devi_addr unit-address - so we
44016bd7258Scth 	 * need to add the unit-address by hand.
44116bd7258Scth 	 */
44216bd7258Scth 	driver = (char *)ddi_driver_name(dip);
44316bd7258Scth 	major = ddi_driver_major(dip);
44416bd7258Scth 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
44516bd7258Scth 	(void) ddi_pathname(dip, path);
44616bd7258Scth 	if ((addr =  ddi_get_name_addr(dip)) != NULL) {
44716bd7258Scth 		(void) strcat(path, "@");
44816bd7258Scth 		(void) strcat(path, addr);
44916bd7258Scth 	}
45016bd7258Scth 	plen = strlen(path);
45116bd7258Scth 
45216bd7258Scth 	/* loop through instance block names */
45316bd7258Scth 	for (ibni = 0; ibni < nibn; ibni++) {
45416bd7258Scth 		if (ibn[ibni] == NULL)
45516bd7258Scth 			continue;
45616bd7258Scth 
45716bd7258Scth 		/* lookup instance block */
45816bd7258Scth 		if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, dip,
45916bd7258Scth 		    DDI_PROP_DONTPASS, ibn[ibni],
46016bd7258Scth 		    &ibp, &nibp) != DDI_SUCCESS) {
46116bd7258Scth 			cmn_err(CE_WARN,
46216bd7258Scth 			    "no devinition for instance block '%s' in %s.conf",
46316bd7258Scth 			    ibn[ibni], driver);
46416bd7258Scth 			continue;
46516bd7258Scth 		}
46616bd7258Scth 
46716bd7258Scth 		/* Does 'path' go through this instance block? */
46816bd7258Scth 		for (ibpi = 0; ibpi < nibp; ibpi++) {
46916bd7258Scth 			if (ibp[ibpi] == NULL)
47016bd7258Scth 				continue;
47116bd7258Scth 			ibplen = strlen(ibp[ibpi]);
47216bd7258Scth 			if ((ibplen <= plen) &&
47316bd7258Scth 			    (strcmp(ibp[ibpi], path + plen - ibplen) == 0))
47416bd7258Scth 				break;
47516bd7258Scth 
47616bd7258Scth 		}
47716bd7258Scth 		if (ibpi >= nibp) {
47816bd7258Scth 			ddi_prop_free(ibp);
47916bd7258Scth 			continue;		/* no try next instance block */
48016bd7258Scth 		}
48116bd7258Scth 
48216bd7258Scth 		/* yes, allocate and assign instances for all paths in block */
48316bd7258Scth 
48416bd7258Scth 		/*
48516bd7258Scth 		 * determine where we splice in instance paths and verify
48616bd7258Scth 		 * that none of the paths are too long.
48716bd7258Scth 		 */
48816bd7258Scth 		splice = plen - ibplen;
48916bd7258Scth 		for (i = 0; i < nibp; i++) {
49016bd7258Scth 			if ((splice + strlen(ibp[i])+ 1) >= MAXPATHLEN) {
49116bd7258Scth 				cmn_err(CE_WARN,
49216bd7258Scth 				    "path %d through instance block '%s' from "
49316bd7258Scth 				    "%s.conf too long", i, ibn[ibni], driver);
49416bd7258Scth 				break;
49516bd7258Scth 			}
49616bd7258Scth 		}
49716bd7258Scth 		if (i < nibp) {
49816bd7258Scth 			ddi_prop_free(ibp);
49916bd7258Scth 			continue;		/* too long */
50016bd7258Scth 		}
50116bd7258Scth 
50216bd7258Scth 		/* allocate the instance block - no more failures */
50316bd7258Scth 		instance_base = in_next_instance_block(major, nibp);
50416bd7258Scth 
50516bd7258Scth 		ipath = kmem_alloc(MAXPATHLEN, KM_SLEEP);
50616bd7258Scth 		for (ibpi = 0; ibpi < nibp; ibpi++) {
50716bd7258Scth 			if (ibp[ibpi] == NULL)
50816bd7258Scth 				continue;
50916bd7258Scth 			(void) strcpy(ipath, path);
51016bd7258Scth 			(void) strcpy(ipath + splice, ibp[ibpi]);
51116bd7258Scth 			(void) in_pathin(ipath,
51216bd7258Scth 			    instance_base + ibpi, driver, NULL);
51316bd7258Scth 		}
51416bd7258Scth 
51516bd7258Scth 		/* free allocations */
51616bd7258Scth 		kmem_free(ipath, MAXPATHLEN);
51716bd7258Scth 		ddi_prop_free(ibp);
51816bd7258Scth 		kmem_free(path, MAXPATHLEN);
51916bd7258Scth 		ddi_prop_free(ibn);
52016bd7258Scth 
52116bd7258Scth 		/* notify devfsadmd to sync of path_to_inst file */
52216bd7258Scth 		mutex_enter(&e_ddi_inst_state.ins_serial);
52316bd7258Scth 		i_log_devfs_instance_mod();
52494c894bbSVikram Hegde 		e_ddi_inst_state.ins_dirty = B_TRUE;
52516bd7258Scth 		mutex_exit(&e_ddi_inst_state.ins_serial);
52616bd7258Scth 		return (1);
52716bd7258Scth 	}
52816bd7258Scth 
52916bd7258Scth 	/* our path did not go through any of of the instance blocks */
53016bd7258Scth 	kmem_free(path, MAXPATHLEN);
53116bd7258Scth 	ddi_prop_free(ibn);
53216bd7258Scth 	return (0);
53316bd7258Scth }
53416bd7258Scth 
53516bd7258Scth /*
5367c478bd9Sstevel@tonic-gate  * Look up an instance number for a dev_info node, and assign one if it does
5377c478bd9Sstevel@tonic-gate  * not have one (the dev_info node has devi_name and devi_addr already set).
5387c478bd9Sstevel@tonic-gate  */
5397c478bd9Sstevel@tonic-gate uint_t
5407c478bd9Sstevel@tonic-gate e_ddi_assign_instance(dev_info_t *dip)
5417c478bd9Sstevel@tonic-gate {
5427c478bd9Sstevel@tonic-gate 	char *name;
5437c478bd9Sstevel@tonic-gate 	in_node_t *ap, *np;
5447c478bd9Sstevel@tonic-gate 	in_drv_t *dp;
5457c478bd9Sstevel@tonic-gate 	major_t major;
5467c478bd9Sstevel@tonic-gate 	uint_t ret;
5477c478bd9Sstevel@tonic-gate 	char *bname;
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate 	/*
5507c478bd9Sstevel@tonic-gate 	 * Allow implementation to override
5517c478bd9Sstevel@tonic-gate 	 */
5527c478bd9Sstevel@tonic-gate 	if ((ret = impl_assign_instance(dip)) != (uint_t)-1)
5537c478bd9Sstevel@tonic-gate 		return (ret);
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate 	/*
5567c478bd9Sstevel@tonic-gate 	 * If this is a pseudo-device, use the instance number
5577c478bd9Sstevel@tonic-gate 	 * assigned by the pseudo nexus driver. The mutex is
5587c478bd9Sstevel@tonic-gate 	 * not needed since the instance tree is not used.
5597c478bd9Sstevel@tonic-gate 	 */
5607c478bd9Sstevel@tonic-gate 	if (is_pseudo_device(dip)) {
5617c478bd9Sstevel@tonic-gate 		return (ddi_get_instance(dip));
5627c478bd9Sstevel@tonic-gate 	}
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate 	/*
5657c478bd9Sstevel@tonic-gate 	 * Only one thread is allowed to change the state of the instance
5667c478bd9Sstevel@tonic-gate 	 * number assignments on the system at any given time.
5677c478bd9Sstevel@tonic-gate 	 */
5687c478bd9Sstevel@tonic-gate 	e_ddi_enter_instance();
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate 	/*
5717c478bd9Sstevel@tonic-gate 	 * Look for instance node, allocate one if not found
5727c478bd9Sstevel@tonic-gate 	 */
5737c478bd9Sstevel@tonic-gate 	np = in_devwalk(dip, &ap, NULL);
5747c478bd9Sstevel@tonic-gate 	if (np == NULL) {
57516bd7258Scth 		if (in_assign_instance_block(dip)) {
57616bd7258Scth 			np = in_devwalk(dip, &ap, NULL);
57716bd7258Scth 		} else {
5787c478bd9Sstevel@tonic-gate 			name = ddi_node_name(dip);
5797c478bd9Sstevel@tonic-gate 			np = in_alloc_node(name, ddi_get_name_addr(dip));
5807c478bd9Sstevel@tonic-gate 			ASSERT(np != NULL);
5817c478bd9Sstevel@tonic-gate 			in_enlist(ap, np);	/* insert into tree */
5827c478bd9Sstevel@tonic-gate 		}
58316bd7258Scth 	}
5847c478bd9Sstevel@tonic-gate 	ASSERT(np == in_devwalk(dip, &ap, NULL));
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate 	/*
58794c894bbSVikram Hegde 	 * Link the devinfo node and in_node_t
58894c894bbSVikram Hegde 	 */
58994c894bbSVikram Hegde 	if (DEVI(dip)->devi_in_node || np->in_devi) {
59094c894bbSVikram Hegde 		ddi_err(DER_MODE, dip, "devinfo and  instance node (%p) "
59194c894bbSVikram Hegde 		    "interlink fields are not NULL", (void *)np);
59294c894bbSVikram Hegde 	}
59394c894bbSVikram Hegde 	DEVI(dip)->devi_in_node = np;
59494c894bbSVikram Hegde 	np->in_devi = dip;
59594c894bbSVikram Hegde 
59694c894bbSVikram Hegde 	/*
5977c478bd9Sstevel@tonic-gate 	 * Look for driver entry, allocate one if not found
5987c478bd9Sstevel@tonic-gate 	 */
5997c478bd9Sstevel@tonic-gate 	bname = (char *)ddi_driver_name(dip);
6007c478bd9Sstevel@tonic-gate 	dp = in_drvwalk(np, bname);
6017c478bd9Sstevel@tonic-gate 	if (dp == NULL) {
60294c894bbSVikram Hegde 
60394c894bbSVikram Hegde 		if (ddi_aliases_present == B_TRUE) {
60494c894bbSVikram Hegde 			e_ddi_borrow_instance(dip, np);
60594c894bbSVikram Hegde 		}
60694c894bbSVikram Hegde 
60794c894bbSVikram Hegde 		if ((dp = in_drvwalk(np, bname)) == NULL) {
6087c478bd9Sstevel@tonic-gate 			dp = in_alloc_drv(bname);
6097c478bd9Sstevel@tonic-gate 			ASSERT(dp != NULL);
6107c478bd9Sstevel@tonic-gate 			major = ddi_driver_major(dip);
611a204de77Scth 			ASSERT(major != DDI_MAJOR_T_NONE);
6127c478bd9Sstevel@tonic-gate 			in_endrv(np, dp);
6137c478bd9Sstevel@tonic-gate 			in_set_instance(dip, dp, major);
6147c478bd9Sstevel@tonic-gate 			dp->ind_state = IN_PROVISIONAL;
6157c478bd9Sstevel@tonic-gate 			in_hashdrv(dp);
61694c894bbSVikram Hegde 		} else {
61794c894bbSVikram Hegde 			dp->ind_state = IN_BORROWED;
61894c894bbSVikram Hegde 		}
6197c478bd9Sstevel@tonic-gate 	}
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate 	ret = dp->ind_instance;
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate 	e_ddi_exit_instance();
6247c478bd9Sstevel@tonic-gate 	return (ret);
6257c478bd9Sstevel@tonic-gate }
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate static int
6287c478bd9Sstevel@tonic-gate mkpathname(char *path, in_node_t *np, int len)
6297c478bd9Sstevel@tonic-gate {
6307c478bd9Sstevel@tonic-gate 	int len_needed;
6317c478bd9Sstevel@tonic-gate 
6327c478bd9Sstevel@tonic-gate 	if (np == e_ddi_inst_state.ins_root)
6337c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
6347c478bd9Sstevel@tonic-gate 
6357c478bd9Sstevel@tonic-gate 	if (mkpathname(path, np->in_parent, len) == DDI_FAILURE)
6367c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate 	len_needed = strlen(path);
6397c478bd9Sstevel@tonic-gate 	len_needed += strlen(np->in_node_name) + 1;	/* for '/' */
6407c478bd9Sstevel@tonic-gate 	if (np->in_unit_addr) {
6417c478bd9Sstevel@tonic-gate 		len_needed += strlen(np->in_unit_addr) + 1;  /* for '@' */
6427c478bd9Sstevel@tonic-gate 	}
6437c478bd9Sstevel@tonic-gate 	len_needed += 1; /* for '\0' */
6447c478bd9Sstevel@tonic-gate 
6457c478bd9Sstevel@tonic-gate 	/*
6467c478bd9Sstevel@tonic-gate 	 * XX complain
6477c478bd9Sstevel@tonic-gate 	 */
6487c478bd9Sstevel@tonic-gate 	if (len_needed > len)
6497c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate 	if (np->in_unit_addr[0] == '\0')
6527c478bd9Sstevel@tonic-gate 		(void) sprintf(path+strlen(path), "/%s", np->in_node_name);
6537c478bd9Sstevel@tonic-gate 	else
6547c478bd9Sstevel@tonic-gate 		(void) sprintf(path+strlen(path), "/%s@%s", np->in_node_name,
6557c478bd9Sstevel@tonic-gate 		    np->in_unit_addr);
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
6587c478bd9Sstevel@tonic-gate }
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate /*
6617c478bd9Sstevel@tonic-gate  * produce the path to the given instance of a major number.
6627c478bd9Sstevel@tonic-gate  * path must hold MAXPATHLEN string
6637c478bd9Sstevel@tonic-gate  */
6647c478bd9Sstevel@tonic-gate int
6657c478bd9Sstevel@tonic-gate e_ddi_instance_majorinstance_to_path(major_t major, uint_t inst, char *path)
6667c478bd9Sstevel@tonic-gate {
6677c478bd9Sstevel@tonic-gate 	struct devnames	*dnp;
6687c478bd9Sstevel@tonic-gate 	in_drv_t	*dp;
6697c478bd9Sstevel@tonic-gate 	int		ret;
6707c478bd9Sstevel@tonic-gate 
6717c478bd9Sstevel@tonic-gate 	e_ddi_enter_instance();
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate 	/* look for the instance threaded off major */
6747c478bd9Sstevel@tonic-gate 	dnp = &devnamesp[major];
6757c478bd9Sstevel@tonic-gate 	for (dp = dnp->dn_inlist; dp != NULL; dp = dp->ind_next)
6767c478bd9Sstevel@tonic-gate 		if (dp->ind_instance == inst)
6777c478bd9Sstevel@tonic-gate 			break;
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate 	/* produce path from the node that uses the instance */
6807c478bd9Sstevel@tonic-gate 	if (dp) {
6817c478bd9Sstevel@tonic-gate 		*path = 0;
6827c478bd9Sstevel@tonic-gate 		ret = mkpathname(path, dp->ind_node, MAXPATHLEN);
6837c478bd9Sstevel@tonic-gate 	} else
6847c478bd9Sstevel@tonic-gate 		ret = DDI_FAILURE;
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate 	e_ddi_exit_instance();
6877c478bd9Sstevel@tonic-gate 	return (ret);
6887c478bd9Sstevel@tonic-gate }
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate /*
69116bd7258Scth  * Allocate a sequential block of instance numbers for the specified driver,
69216bd7258Scth  * and return the base instance number of the block.  The implementation
69316bd7258Scth  * depends on the list being sorted in ascending instance number sequence.
69416bd7258Scth  * When there are no 'holes' in the allocation sequence, dn_instance is the
69516bd7258Scth  * next available instance number. When dn_instance is IN_SEARCHME, hole(s)
69616bd7258Scth  * exists and a slower code path executes which tries to fill holes.
697*328d222bSChris Horne  *
698*328d222bSChris Horne  * The block returned can't be in the preassigned range.
6997c478bd9Sstevel@tonic-gate  */
7007c478bd9Sstevel@tonic-gate static int
70116bd7258Scth in_next_instance_block(major_t major, int block_size)
7027c478bd9Sstevel@tonic-gate {
703*328d222bSChris Horne 	int		prev;
7047c478bd9Sstevel@tonic-gate 	struct devnames	*dnp;
7057c478bd9Sstevel@tonic-gate 	in_drv_t	*dp;
70616bd7258Scth 	int		base;
70716bd7258Scth 	int		hole;
7087c478bd9Sstevel@tonic-gate 
7097c478bd9Sstevel@tonic-gate 	dnp = &devnamesp[major];
710a204de77Scth 	ASSERT(major != DDI_MAJOR_T_NONE);
7117c478bd9Sstevel@tonic-gate 	ASSERT(e_ddi_inst_state.ins_busy);
71216bd7258Scth 	ASSERT(block_size);
71316bd7258Scth 
71416bd7258Scth 	/* check to see if we can do a quick allocation */
715*328d222bSChris Horne 	if (!instance_searchme && (dnp->dn_instance != IN_SEARCHME)) {
71616bd7258Scth 		base = dnp->dn_instance;
71716bd7258Scth 		dnp->dn_instance += block_size;
71816bd7258Scth 		return (base);
71916bd7258Scth 	}
720*328d222bSChris Horne 
721*328d222bSChris Horne 	/* use more complex code path */
7227c478bd9Sstevel@tonic-gate 	dp = dnp->dn_inlist;
7237c478bd9Sstevel@tonic-gate 
724*328d222bSChris Horne 	/* no existing entries, allocate block (after preassigns) */
7257c478bd9Sstevel@tonic-gate 	if (dp == NULL) {
726*328d222bSChris Horne 		base = dnp->dn_pinstance;
727*328d222bSChris Horne 		dnp->dn_instance = dnp->dn_pinstance + block_size;
728*328d222bSChris Horne 		return (base);
7297c478bd9Sstevel@tonic-gate 	}
7307c478bd9Sstevel@tonic-gate 
731*328d222bSChris Horne 	/* see if we fit in hole at beginning (after preassigns) */
7327c478bd9Sstevel@tonic-gate 	prev = dp->ind_instance;
733*328d222bSChris Horne 	if ((prev - dnp->dn_pinstance) >= block_size)
734*328d222bSChris Horne 		return (dnp->dn_pinstance);	/* we fit in beginning hole */
7357c478bd9Sstevel@tonic-gate 
73616bd7258Scth 	/* search the list for a large enough hole */
73716bd7258Scth 	for (dp = dp->ind_next, hole = 0; dp; dp = dp->ind_next) {
738*328d222bSChris Horne 		if (dp->ind_instance >= dnp->dn_pinstance) {
739*328d222bSChris Horne 			/* not a preassign */
74016bd7258Scth 			if (dp->ind_instance != (prev + 1))
74116bd7258Scth 				hole++;			/* we have a hole */
74216bd7258Scth 			if (dp->ind_instance >= (prev + block_size + 1))
74316bd7258Scth 				break;			/* we fit in hole */
744*328d222bSChris Horne 		} else
745*328d222bSChris Horne 			hole++;		/* preassign hole */
746*328d222bSChris Horne 
74716bd7258Scth 		prev = dp->ind_instance;
74816bd7258Scth 	}
74916bd7258Scth 
75016bd7258Scth 	/*
75116bd7258Scth 	 * If hole is zero then all holes are patched and we can resume
75216bd7258Scth 	 * quick allocations.
75316bd7258Scth 	 */
75416bd7258Scth 	if (hole == 0)
75516bd7258Scth 		dnp->dn_instance = prev + 1 + block_size;
75616bd7258Scth 
75716bd7258Scth 	return (prev + 1);
75816bd7258Scth }
75916bd7258Scth 
76016bd7258Scth /* assign instance block of size 1 */
76116bd7258Scth static int
76216bd7258Scth in_next_instance(major_t major)
76316bd7258Scth {
76416bd7258Scth 	return (in_next_instance_block(major, 1));
7657c478bd9Sstevel@tonic-gate }
7667c478bd9Sstevel@tonic-gate 
7677c478bd9Sstevel@tonic-gate /*
7687c478bd9Sstevel@tonic-gate  * This call causes us to *forget* the instance number we've generated
7697c478bd9Sstevel@tonic-gate  * for a given device if it was not permanent.
7707c478bd9Sstevel@tonic-gate  */
7717c478bd9Sstevel@tonic-gate void
7727c478bd9Sstevel@tonic-gate e_ddi_free_instance(dev_info_t *dip, char *addr)
7737c478bd9Sstevel@tonic-gate {
7747c478bd9Sstevel@tonic-gate 	char *name;
7757c478bd9Sstevel@tonic-gate 	in_node_t *np;
7767c478bd9Sstevel@tonic-gate 	in_node_t *ap;	/* ancestor node */
7777c478bd9Sstevel@tonic-gate 	major_t major;
7787c478bd9Sstevel@tonic-gate 	struct devnames *dnp;
7797c478bd9Sstevel@tonic-gate 	in_drv_t *dp;	/* in_drv entry */
7807c478bd9Sstevel@tonic-gate 
7817c478bd9Sstevel@tonic-gate 	/*
7827c478bd9Sstevel@tonic-gate 	 * Allow implementation override
7837c478bd9Sstevel@tonic-gate 	 */
7847c478bd9Sstevel@tonic-gate 	if (impl_free_instance(dip) == DDI_SUCCESS)
7857c478bd9Sstevel@tonic-gate 		return;
7867c478bd9Sstevel@tonic-gate 
7877c478bd9Sstevel@tonic-gate 	/*
7887c478bd9Sstevel@tonic-gate 	 * If this is a pseudo-device, no instance number
7897c478bd9Sstevel@tonic-gate 	 * was assigned.
7907c478bd9Sstevel@tonic-gate 	 */
7917c478bd9Sstevel@tonic-gate 	if (is_pseudo_device(dip)) {
7927c478bd9Sstevel@tonic-gate 		return;
7937c478bd9Sstevel@tonic-gate 	}
7947c478bd9Sstevel@tonic-gate 
7957c478bd9Sstevel@tonic-gate 	name = (char *)ddi_driver_name(dip);
7967c478bd9Sstevel@tonic-gate 	major = ddi_driver_major(dip);
797a204de77Scth 	ASSERT(major != DDI_MAJOR_T_NONE);
7987c478bd9Sstevel@tonic-gate 	dnp = &devnamesp[major];
7997c478bd9Sstevel@tonic-gate 	/*
8007c478bd9Sstevel@tonic-gate 	 * Only one thread is allowed to change the state of the instance
8017c478bd9Sstevel@tonic-gate 	 * number assignments on the system at any given time.
8027c478bd9Sstevel@tonic-gate 	 */
8037c478bd9Sstevel@tonic-gate 	e_ddi_enter_instance();
8047c478bd9Sstevel@tonic-gate 	np = in_devwalk(dip, &ap, addr);
8057c478bd9Sstevel@tonic-gate 	ASSERT(np);
80694c894bbSVikram Hegde 
80794c894bbSVikram Hegde 	/*
80894c894bbSVikram Hegde 	 * Break the interlink between dip and np
80994c894bbSVikram Hegde 	 */
81094c894bbSVikram Hegde 	if (DEVI(dip)->devi_in_node != np || np->in_devi != dip) {
81194c894bbSVikram Hegde 		ddi_err(DER_MODE, dip, "devinfo node linked to "
81294c894bbSVikram Hegde 		    "wrong instance node: %p", (void *)np);
81394c894bbSVikram Hegde 	}
81494c894bbSVikram Hegde 	DEVI(dip)->devi_in_node = NULL;
81594c894bbSVikram Hegde 	np->in_devi = NULL;
81694c894bbSVikram Hegde 
8177c478bd9Sstevel@tonic-gate 	dp = in_drvwalk(np, name);
8187c478bd9Sstevel@tonic-gate 	ASSERT(dp);
8197c478bd9Sstevel@tonic-gate 	if (dp->ind_state == IN_PROVISIONAL) {
8207c478bd9Sstevel@tonic-gate 		in_removedrv(dnp, dp);
821f1ebe3a2SVikram Hegde 	} else if (dp->ind_state == IN_BORROWED) {
82294c894bbSVikram Hegde 		dp->ind_state = IN_PERMANENT;
82394c894bbSVikram Hegde 		e_ddi_return_instance(dip, addr, np);
82494c894bbSVikram Hegde 	}
8257c478bd9Sstevel@tonic-gate 	if (np->in_drivers == NULL) {
8267c478bd9Sstevel@tonic-gate 		in_removenode(dnp, np, ap);
8277c478bd9Sstevel@tonic-gate 	}
8287c478bd9Sstevel@tonic-gate 	e_ddi_exit_instance();
8297c478bd9Sstevel@tonic-gate }
8307c478bd9Sstevel@tonic-gate 
8317c478bd9Sstevel@tonic-gate /*
8327c478bd9Sstevel@tonic-gate  * This makes our memory of an instance assignment permanent
8337c478bd9Sstevel@tonic-gate  */
8347c478bd9Sstevel@tonic-gate void
8357c478bd9Sstevel@tonic-gate e_ddi_keep_instance(dev_info_t *dip)
8367c478bd9Sstevel@tonic-gate {
8377c478bd9Sstevel@tonic-gate 	in_node_t *np, *ap;
8387c478bd9Sstevel@tonic-gate 	in_drv_t *dp;
8397c478bd9Sstevel@tonic-gate 
84062a24de0SChris Horne 	/* Don't make nulldriver instance assignments permanent */
84162a24de0SChris Horne 	if (ddi_driver_major(dip) == nulldriver_major)
84262a24de0SChris Horne 		return;
84362a24de0SChris Horne 
8447c478bd9Sstevel@tonic-gate 	/*
8457c478bd9Sstevel@tonic-gate 	 * Allow implementation override
8467c478bd9Sstevel@tonic-gate 	 */
8477c478bd9Sstevel@tonic-gate 	if (impl_keep_instance(dip) == DDI_SUCCESS)
8487c478bd9Sstevel@tonic-gate 		return;
8497c478bd9Sstevel@tonic-gate 
8507c478bd9Sstevel@tonic-gate 	/*
8517c478bd9Sstevel@tonic-gate 	 * Nothing to do for pseudo devices.
8527c478bd9Sstevel@tonic-gate 	 */
8537c478bd9Sstevel@tonic-gate 	if (is_pseudo_device(dip))
8547c478bd9Sstevel@tonic-gate 		return;
8557c478bd9Sstevel@tonic-gate 
8567c478bd9Sstevel@tonic-gate 	/*
8577c478bd9Sstevel@tonic-gate 	 * Only one thread is allowed to change the state of the instance
8587c478bd9Sstevel@tonic-gate 	 * number assignments on the system at any given time.
8597c478bd9Sstevel@tonic-gate 	 */
8607c478bd9Sstevel@tonic-gate 	e_ddi_enter_instance();
8617c478bd9Sstevel@tonic-gate 	np = in_devwalk(dip, &ap, NULL);
8627c478bd9Sstevel@tonic-gate 	ASSERT(np);
8637c478bd9Sstevel@tonic-gate 	dp = in_drvwalk(np, (char *)ddi_driver_name(dip));
8647c478bd9Sstevel@tonic-gate 	ASSERT(dp);
8657c478bd9Sstevel@tonic-gate 
8667c478bd9Sstevel@tonic-gate 	mutex_enter(&e_ddi_inst_state.ins_serial);
86794c894bbSVikram Hegde 	if (dp->ind_state == IN_PROVISIONAL || dp->ind_state == IN_BORROWED) {
8687c478bd9Sstevel@tonic-gate 		dp->ind_state = IN_PERMANENT;
8697c478bd9Sstevel@tonic-gate 		i_log_devfs_instance_mod();
87094c894bbSVikram Hegde 		e_ddi_inst_state.ins_dirty = B_TRUE;
8717c478bd9Sstevel@tonic-gate 	}
8727c478bd9Sstevel@tonic-gate 	mutex_exit(&e_ddi_inst_state.ins_serial);
8737c478bd9Sstevel@tonic-gate 	e_ddi_exit_instance();
8747c478bd9Sstevel@tonic-gate }
8757c478bd9Sstevel@tonic-gate 
8767c478bd9Sstevel@tonic-gate /*
8777c478bd9Sstevel@tonic-gate  * A new major has been added to the system.  Run through the orphan list
8787c478bd9Sstevel@tonic-gate  * and try to attach each one to a driver's list.
8797c478bd9Sstevel@tonic-gate  */
8807c478bd9Sstevel@tonic-gate void
8817c478bd9Sstevel@tonic-gate e_ddi_unorphan_instance_nos()
8827c478bd9Sstevel@tonic-gate {
8837c478bd9Sstevel@tonic-gate 	in_drv_t *dp, *ndp;
8847c478bd9Sstevel@tonic-gate 
8857c478bd9Sstevel@tonic-gate 	/*
8867c478bd9Sstevel@tonic-gate 	 * disconnect the orphan list, and call in_hashdrv for each item
8877c478bd9Sstevel@tonic-gate 	 * on it
8887c478bd9Sstevel@tonic-gate 	 */
8897c478bd9Sstevel@tonic-gate 
8907c478bd9Sstevel@tonic-gate 	/*
8917c478bd9Sstevel@tonic-gate 	 * Only one thread is allowed to change the state of the instance
8927c478bd9Sstevel@tonic-gate 	 * number assignments on the system at any given time.
8937c478bd9Sstevel@tonic-gate 	 */
8947c478bd9Sstevel@tonic-gate 	e_ddi_enter_instance();
8957c478bd9Sstevel@tonic-gate 	if (e_ddi_inst_state.ins_no_major == NULL) {
8967c478bd9Sstevel@tonic-gate 		e_ddi_exit_instance();
8977c478bd9Sstevel@tonic-gate 		return;
8987c478bd9Sstevel@tonic-gate 	}
8997c478bd9Sstevel@tonic-gate 	/*
9007c478bd9Sstevel@tonic-gate 	 * Hash instance list to devnames structure of major.
9017c478bd9Sstevel@tonic-gate 	 * Note that if there is not a valid major number for the
9027c478bd9Sstevel@tonic-gate 	 * node, in_hashdrv will put it back on the no_major list.
9037c478bd9Sstevel@tonic-gate 	 */
9047c478bd9Sstevel@tonic-gate 	dp = e_ddi_inst_state.ins_no_major;
9057c478bd9Sstevel@tonic-gate 	e_ddi_inst_state.ins_no_major = NULL;
9067c478bd9Sstevel@tonic-gate 	while (dp) {
9077c478bd9Sstevel@tonic-gate 		ndp = dp->ind_next;
9087c478bd9Sstevel@tonic-gate 		ASSERT(dp->ind_state != IN_UNKNOWN);
9097c478bd9Sstevel@tonic-gate 		dp->ind_next = NULL;
9107c478bd9Sstevel@tonic-gate 		in_hashdrv(dp);
9117c478bd9Sstevel@tonic-gate 		dp = ndp;
9127c478bd9Sstevel@tonic-gate 	}
9137c478bd9Sstevel@tonic-gate 	e_ddi_exit_instance();
9147c478bd9Sstevel@tonic-gate }
9157c478bd9Sstevel@tonic-gate 
9167c478bd9Sstevel@tonic-gate static void
9177c478bd9Sstevel@tonic-gate in_removenode(struct devnames *dnp, in_node_t *mp, in_node_t *ap)
9187c478bd9Sstevel@tonic-gate {
9197c478bd9Sstevel@tonic-gate 	in_node_t *np;
9207c478bd9Sstevel@tonic-gate 
9217c478bd9Sstevel@tonic-gate 	ASSERT(e_ddi_inst_state.ins_busy);
92294c894bbSVikram Hegde 
9237c478bd9Sstevel@tonic-gate 	/*
9247c478bd9Sstevel@tonic-gate 	 * Assertion: parents are always instantiated by the framework
9257c478bd9Sstevel@tonic-gate 	 * before their children, destroyed after them
9267c478bd9Sstevel@tonic-gate 	 */
9277c478bd9Sstevel@tonic-gate 	ASSERT(mp->in_child == NULL);
9287c478bd9Sstevel@tonic-gate 	/*
9297c478bd9Sstevel@tonic-gate 	 * Assertion: drv entries are always removed before their owning nodes
9307c478bd9Sstevel@tonic-gate 	 */
9317c478bd9Sstevel@tonic-gate 	ASSERT(mp->in_drivers == NULL);
9327c478bd9Sstevel@tonic-gate 	/*
9337c478bd9Sstevel@tonic-gate 	 * Take the node out of the tree
9347c478bd9Sstevel@tonic-gate 	 */
9357c478bd9Sstevel@tonic-gate 	if (ap->in_child == mp) {
9367c478bd9Sstevel@tonic-gate 		ap->in_child = mp->in_sibling;
9377c478bd9Sstevel@tonic-gate 		in_dealloc_node(mp);
9387c478bd9Sstevel@tonic-gate 		return;
9397c478bd9Sstevel@tonic-gate 	} else {
9407c478bd9Sstevel@tonic-gate 		for (np = ap->in_child; np; np = np->in_sibling) {
9417c478bd9Sstevel@tonic-gate 			if (np->in_sibling == mp) {
9427c478bd9Sstevel@tonic-gate 				np->in_sibling = mp->in_sibling;
9437c478bd9Sstevel@tonic-gate 				in_dealloc_node(mp);
9447c478bd9Sstevel@tonic-gate 				return;
9457c478bd9Sstevel@tonic-gate 			}
9467c478bd9Sstevel@tonic-gate 		}
9477c478bd9Sstevel@tonic-gate 	}
9487c478bd9Sstevel@tonic-gate 	panic("in_removenode dnp %p mp %p", (void *)dnp, (void *)mp);
9497c478bd9Sstevel@tonic-gate }
9507c478bd9Sstevel@tonic-gate 
9517c478bd9Sstevel@tonic-gate /*
9527c478bd9Sstevel@tonic-gate  * Recursive ascent
9537c478bd9Sstevel@tonic-gate  *
9547c478bd9Sstevel@tonic-gate  * This now only does half the job.  It finds the node, then the caller
9557c478bd9Sstevel@tonic-gate  * has to search the node for the binding name
9567c478bd9Sstevel@tonic-gate  */
9577c478bd9Sstevel@tonic-gate static in_node_t *
9587c478bd9Sstevel@tonic-gate in_devwalk(dev_info_t *dip, in_node_t **ap, char *addr)
9597c478bd9Sstevel@tonic-gate {
9607c478bd9Sstevel@tonic-gate 	in_node_t *np;
9617c478bd9Sstevel@tonic-gate 	char *name;
9627c478bd9Sstevel@tonic-gate 
9637c478bd9Sstevel@tonic-gate 	ASSERT(dip);
9647c478bd9Sstevel@tonic-gate 	ASSERT(e_ddi_inst_state.ins_busy);
9657c478bd9Sstevel@tonic-gate 	if (dip == ddi_root_node()) {
9667c478bd9Sstevel@tonic-gate 		*ap = NULL;
9677c478bd9Sstevel@tonic-gate 		return (e_ddi_inst_state.ins_root);
9687c478bd9Sstevel@tonic-gate 	}
9697c478bd9Sstevel@tonic-gate 	/*
9707c478bd9Sstevel@tonic-gate 	 * call up to find parent, then look through the list of kids
9717c478bd9Sstevel@tonic-gate 	 * for a match
9727c478bd9Sstevel@tonic-gate 	 */
9737c478bd9Sstevel@tonic-gate 	np = in_devwalk(ddi_get_parent(dip), ap, NULL);
9747c478bd9Sstevel@tonic-gate 	if (np == NULL)
9757c478bd9Sstevel@tonic-gate 		return (np);
9767c478bd9Sstevel@tonic-gate 	*ap = np;
9777c478bd9Sstevel@tonic-gate 	np = np->in_child;
9787c478bd9Sstevel@tonic-gate 	name = ddi_node_name(dip);
9797c478bd9Sstevel@tonic-gate 	if (addr == NULL)
9807c478bd9Sstevel@tonic-gate 		addr = ddi_get_name_addr(dip);
9817c478bd9Sstevel@tonic-gate 
9827c478bd9Sstevel@tonic-gate 	while (np) {
9837c478bd9Sstevel@tonic-gate 		if (in_eqstr(np->in_node_name, name) &&
9847c478bd9Sstevel@tonic-gate 		    in_eqstr(np->in_unit_addr, addr)) {
9857c478bd9Sstevel@tonic-gate 			return (np);
9867c478bd9Sstevel@tonic-gate 		}
9877c478bd9Sstevel@tonic-gate 		np = np->in_sibling;
9887c478bd9Sstevel@tonic-gate 	}
98994c894bbSVikram Hegde 
9907c478bd9Sstevel@tonic-gate 	return (np);
9917c478bd9Sstevel@tonic-gate }
9927c478bd9Sstevel@tonic-gate 
9937c478bd9Sstevel@tonic-gate /*
9947c478bd9Sstevel@tonic-gate  * Create a node specified by cp and assign it the given instance no.
9957c478bd9Sstevel@tonic-gate  */
9967c478bd9Sstevel@tonic-gate static int
9977c478bd9Sstevel@tonic-gate in_pathin(char *cp, int instance, char *bname, struct bind **args)
9987c478bd9Sstevel@tonic-gate {
9997c478bd9Sstevel@tonic-gate 	in_node_t *np;
10007c478bd9Sstevel@tonic-gate 	in_drv_t *dp;
10017c478bd9Sstevel@tonic-gate 	char *name;
10027c478bd9Sstevel@tonic-gate 
10037c478bd9Sstevel@tonic-gate 	ASSERT(e_ddi_inst_state.ins_busy);
10047c478bd9Sstevel@tonic-gate 	ASSERT(args == NULL);
10057c478bd9Sstevel@tonic-gate 
10067c478bd9Sstevel@tonic-gate 	/*
10077c478bd9Sstevel@tonic-gate 	 * Give a warning to the console.
10087c478bd9Sstevel@tonic-gate 	 * return value ignored
10097c478bd9Sstevel@tonic-gate 	 */
10107c478bd9Sstevel@tonic-gate 	if (cp[0] != '/' || instance == -1 || bname == NULL) {
10117c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN,
10127c478bd9Sstevel@tonic-gate 		    "invalid instance file entry %s %d",
10137c478bd9Sstevel@tonic-gate 		    cp, instance);
10147c478bd9Sstevel@tonic-gate 		return (0);
10157c478bd9Sstevel@tonic-gate 	}
10167c478bd9Sstevel@tonic-gate 
10177c478bd9Sstevel@tonic-gate 	if ((name  = i_binding_to_drv_name(bname)) != NULL)
10187c478bd9Sstevel@tonic-gate 		bname = name;
10197c478bd9Sstevel@tonic-gate 
10207c478bd9Sstevel@tonic-gate 	np = in_make_path(cp);
10217c478bd9Sstevel@tonic-gate 	ASSERT(np);
102294c894bbSVikram Hegde 
10237c478bd9Sstevel@tonic-gate 	dp = in_drvwalk(np, bname);
10247c478bd9Sstevel@tonic-gate 	if (dp != NULL) {
10257c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN,
10267c478bd9Sstevel@tonic-gate 		    "multiple instance number assignments for "
10277c478bd9Sstevel@tonic-gate 		    "'%s' (driver %s), %d used",
10287c478bd9Sstevel@tonic-gate 		    cp, bname, dp->ind_instance);
10297c478bd9Sstevel@tonic-gate 		return (0);
10307c478bd9Sstevel@tonic-gate 	}
103194c894bbSVikram Hegde 
103294c894bbSVikram Hegde 	if (in_inuse(instance, bname)) {
103394c894bbSVikram Hegde 		cmn_err(CE_WARN,
103494c894bbSVikram Hegde 		    "instance already in use: %s %d", cp, instance);
103594c894bbSVikram Hegde 		return (0);
103694c894bbSVikram Hegde 	}
103794c894bbSVikram Hegde 
10387c478bd9Sstevel@tonic-gate 	dp = in_alloc_drv(bname);
10397c478bd9Sstevel@tonic-gate 	in_endrv(np, dp);
10407c478bd9Sstevel@tonic-gate 	dp->ind_instance = instance;
10417c478bd9Sstevel@tonic-gate 	dp->ind_state = IN_PERMANENT;
10427c478bd9Sstevel@tonic-gate 	in_hashdrv(dp);
10437c478bd9Sstevel@tonic-gate 
10447c478bd9Sstevel@tonic-gate 	return (0);
10457c478bd9Sstevel@tonic-gate }
10467c478bd9Sstevel@tonic-gate 
10477c478bd9Sstevel@tonic-gate /*
10487c478bd9Sstevel@tonic-gate  * Create (or find) the node named by path by recursively descending from the
10497c478bd9Sstevel@tonic-gate  * root's first child (we ignore the root, which is never named)
10507c478bd9Sstevel@tonic-gate  */
10517c478bd9Sstevel@tonic-gate static in_node_t *
10527c478bd9Sstevel@tonic-gate in_make_path(char *path)
10537c478bd9Sstevel@tonic-gate {
10547c478bd9Sstevel@tonic-gate 	in_node_t *ap;		/* ancestor pointer */
10557c478bd9Sstevel@tonic-gate 	in_node_t *np;		/* working node pointer */
10567c478bd9Sstevel@tonic-gate 	in_node_t *rp;		/* return node pointer */
10577c478bd9Sstevel@tonic-gate 	char buf[MAXPATHLEN];	/* copy of string so we can change it */
10587c478bd9Sstevel@tonic-gate 	char *cp, *name, *addr;
10597c478bd9Sstevel@tonic-gate 
10607c478bd9Sstevel@tonic-gate 	ASSERT(e_ddi_inst_state.ins_busy);
106194c894bbSVikram Hegde 
10627c478bd9Sstevel@tonic-gate 	if (path == NULL || path[0] != '/')
10637c478bd9Sstevel@tonic-gate 		return (NULL);
106494c894bbSVikram Hegde 
10657c478bd9Sstevel@tonic-gate 	(void) snprintf(buf, sizeof (buf), "%s", path);
10667c478bd9Sstevel@tonic-gate 	cp = buf + 1;	/* skip over initial '/' in path */
10677c478bd9Sstevel@tonic-gate 	name = in_name_addr(&cp, &addr);
10687c478bd9Sstevel@tonic-gate 
10697c478bd9Sstevel@tonic-gate 	/*
10707c478bd9Sstevel@tonic-gate 	 * In S9 and earlier releases, the path_to_inst file
10717c478bd9Sstevel@tonic-gate 	 * SunCluster was prepended with "/node@#". This was
10727c478bd9Sstevel@tonic-gate 	 * removed in S10. We skip the prefix if the prefix
10737c478bd9Sstevel@tonic-gate 	 * still exists in /etc/path_to_inst. It is needed for
10747c478bd9Sstevel@tonic-gate 	 * various forms of Solaris upgrade to work properly
10757c478bd9Sstevel@tonic-gate 	 * in the SunCluster environment.
10767c478bd9Sstevel@tonic-gate 	 */
10777c478bd9Sstevel@tonic-gate 	if ((cluster_bootflags & CLUSTER_CONFIGURED) &&
10787c478bd9Sstevel@tonic-gate 	    (strcmp(name, "node") == 0))
10797c478bd9Sstevel@tonic-gate 		name = in_name_addr(&cp, &addr);
10807c478bd9Sstevel@tonic-gate 
10817c478bd9Sstevel@tonic-gate 	ap = e_ddi_inst_state.ins_root;
108294c894bbSVikram Hegde 	np = e_ddi_inst_state.ins_root->in_child;
108394c894bbSVikram Hegde 	rp = np;
10847c478bd9Sstevel@tonic-gate 	while (name) {
10857c478bd9Sstevel@tonic-gate 		while (name && np) {
10867c478bd9Sstevel@tonic-gate 			if (in_eqstr(name, np->in_node_name) &&
10877c478bd9Sstevel@tonic-gate 			    in_eqstr(addr, np->in_unit_addr)) {
10887c478bd9Sstevel@tonic-gate 				name = in_name_addr(&cp, &addr);
10897c478bd9Sstevel@tonic-gate 				if (name == NULL)
10907c478bd9Sstevel@tonic-gate 					return (np);
10917c478bd9Sstevel@tonic-gate 				ap = np;
10927c478bd9Sstevel@tonic-gate 				np = np->in_child;
10937c478bd9Sstevel@tonic-gate 			} else {
10947c478bd9Sstevel@tonic-gate 				np = np->in_sibling;
10957c478bd9Sstevel@tonic-gate 			}
10967c478bd9Sstevel@tonic-gate 		}
10977c478bd9Sstevel@tonic-gate 		np = in_alloc_node(name, addr);
10987c478bd9Sstevel@tonic-gate 		in_enlist(ap, np);	/* insert into tree */
10997c478bd9Sstevel@tonic-gate 		rp = np;	/* value to return if we quit */
11007c478bd9Sstevel@tonic-gate 		ap = np;	/* new parent */
11017c478bd9Sstevel@tonic-gate 		np = NULL;	/* can have no children */
11027c478bd9Sstevel@tonic-gate 		name = in_name_addr(&cp, &addr);
11037c478bd9Sstevel@tonic-gate 	}
110494c894bbSVikram Hegde 
11057c478bd9Sstevel@tonic-gate 	return (rp);
11067c478bd9Sstevel@tonic-gate }
11077c478bd9Sstevel@tonic-gate 
11087c478bd9Sstevel@tonic-gate /*
11097c478bd9Sstevel@tonic-gate  * Insert node np into the tree as one of ap's children.
11107c478bd9Sstevel@tonic-gate  */
11117c478bd9Sstevel@tonic-gate static void
11127c478bd9Sstevel@tonic-gate in_enlist(in_node_t *ap, in_node_t *np)
11137c478bd9Sstevel@tonic-gate {
11147c478bd9Sstevel@tonic-gate 	in_node_t *mp;
11157c478bd9Sstevel@tonic-gate 	ASSERT(e_ddi_inst_state.ins_busy);
11167c478bd9Sstevel@tonic-gate 	/*
11177c478bd9Sstevel@tonic-gate 	 * Make this node some other node's child or child's sibling
11187c478bd9Sstevel@tonic-gate 	 */
11197c478bd9Sstevel@tonic-gate 	ASSERT(ap && np);
11207c478bd9Sstevel@tonic-gate 	if (ap->in_child == NULL) {
11217c478bd9Sstevel@tonic-gate 		ap->in_child = np;
11227c478bd9Sstevel@tonic-gate 	} else {
11237c478bd9Sstevel@tonic-gate 		for (mp = ap->in_child; mp; mp = mp->in_sibling)
11247c478bd9Sstevel@tonic-gate 			if (mp->in_sibling == NULL) {
11257c478bd9Sstevel@tonic-gate 				mp->in_sibling = np;
11267c478bd9Sstevel@tonic-gate 				break;
11277c478bd9Sstevel@tonic-gate 			}
11287c478bd9Sstevel@tonic-gate 	}
11297c478bd9Sstevel@tonic-gate 	np->in_parent = ap;
11307c478bd9Sstevel@tonic-gate }
11317c478bd9Sstevel@tonic-gate 
11327c478bd9Sstevel@tonic-gate /*
11337c478bd9Sstevel@tonic-gate  * Insert drv entry dp onto a node's driver list
11347c478bd9Sstevel@tonic-gate  */
11357c478bd9Sstevel@tonic-gate static void
11367c478bd9Sstevel@tonic-gate in_endrv(in_node_t *np, in_drv_t *dp)
11377c478bd9Sstevel@tonic-gate {
11387c478bd9Sstevel@tonic-gate 	in_drv_t *mp;
11397c478bd9Sstevel@tonic-gate 	ASSERT(e_ddi_inst_state.ins_busy);
11407c478bd9Sstevel@tonic-gate 	ASSERT(np && dp);
11417c478bd9Sstevel@tonic-gate 	mp = np->in_drivers;
11427c478bd9Sstevel@tonic-gate 	np->in_drivers = dp;
11437c478bd9Sstevel@tonic-gate 	dp->ind_next_drv = mp;
11447c478bd9Sstevel@tonic-gate 	dp->ind_node = np;
11457c478bd9Sstevel@tonic-gate }
11467c478bd9Sstevel@tonic-gate 
11477c478bd9Sstevel@tonic-gate /*
11487c478bd9Sstevel@tonic-gate  * Parse the next name out of the path, null terminate it and update cp.
11497c478bd9Sstevel@tonic-gate  * caller has copied string so we can mess with it.
11507c478bd9Sstevel@tonic-gate  * Upon return *cpp points to the next section to be parsed, *addrp points
11517c478bd9Sstevel@tonic-gate  * to the current address substring (or NULL if none) and we return the
11527c478bd9Sstevel@tonic-gate  * current name substring (or NULL if none).  name and address substrings
11537c478bd9Sstevel@tonic-gate  * are null terminated in place.
11547c478bd9Sstevel@tonic-gate  */
11557c478bd9Sstevel@tonic-gate 
11567c478bd9Sstevel@tonic-gate static char *
11577c478bd9Sstevel@tonic-gate in_name_addr(char **cpp, char **addrp)
11587c478bd9Sstevel@tonic-gate {
11597c478bd9Sstevel@tonic-gate 	char *namep;	/* return value holder */
11607c478bd9Sstevel@tonic-gate 	char *ap;	/* pointer to '@' in string */
11617c478bd9Sstevel@tonic-gate 	char *sp;	/* pointer to '/' in string */
11627c478bd9Sstevel@tonic-gate 
11637c478bd9Sstevel@tonic-gate 	if (*cpp == NULL || **cpp == '\0') {
11647c478bd9Sstevel@tonic-gate 		*addrp = NULL;
11657c478bd9Sstevel@tonic-gate 		return (NULL);
11667c478bd9Sstevel@tonic-gate 	}
11677c478bd9Sstevel@tonic-gate 	namep = *cpp;
11687c478bd9Sstevel@tonic-gate 	sp = strchr(*cpp, '/');
11697c478bd9Sstevel@tonic-gate 	if (sp != NULL) {	/* more to follow */
11707c478bd9Sstevel@tonic-gate 		*sp = '\0';
11717c478bd9Sstevel@tonic-gate 		*cpp = sp + 1;
11727c478bd9Sstevel@tonic-gate 	} else {		/* this is last component. */
11737c478bd9Sstevel@tonic-gate 		*cpp = NULL;
11747c478bd9Sstevel@tonic-gate 	}
11757c478bd9Sstevel@tonic-gate 	ap = strchr(namep, '@');
11767c478bd9Sstevel@tonic-gate 	if (ap == NULL) {
11777c478bd9Sstevel@tonic-gate 		*addrp = NULL;
11787c478bd9Sstevel@tonic-gate 	} else {
11797c478bd9Sstevel@tonic-gate 		*ap = '\0';		/* terminate the name */
11807c478bd9Sstevel@tonic-gate 		*addrp = ap + 1;
11817c478bd9Sstevel@tonic-gate 	}
11827c478bd9Sstevel@tonic-gate 	return (namep);
11837c478bd9Sstevel@tonic-gate }
11847c478bd9Sstevel@tonic-gate 
11857c478bd9Sstevel@tonic-gate /*
11867c478bd9Sstevel@tonic-gate  * Allocate a node and storage for name and addr strings, and fill them in.
11877c478bd9Sstevel@tonic-gate  */
11887c478bd9Sstevel@tonic-gate static in_node_t *
11897c478bd9Sstevel@tonic-gate in_alloc_node(char *name, char *addr)
11907c478bd9Sstevel@tonic-gate {
11917c478bd9Sstevel@tonic-gate 	in_node_t *np;
11927c478bd9Sstevel@tonic-gate 	char *cp;
11937c478bd9Sstevel@tonic-gate 	size_t namelen;
11947c478bd9Sstevel@tonic-gate 
11957c478bd9Sstevel@tonic-gate 	ASSERT(e_ddi_inst_state.ins_busy);
11967c478bd9Sstevel@tonic-gate 	/*
11977c478bd9Sstevel@tonic-gate 	 * Has name or will become root
11987c478bd9Sstevel@tonic-gate 	 */
11997c478bd9Sstevel@tonic-gate 	ASSERT(name || e_ddi_inst_state.ins_root == NULL);
12007c478bd9Sstevel@tonic-gate 	if (addr == NULL)
12017c478bd9Sstevel@tonic-gate 		addr = "";
12027c478bd9Sstevel@tonic-gate 	if (name == NULL)
12037c478bd9Sstevel@tonic-gate 		namelen = 0;
12047c478bd9Sstevel@tonic-gate 	else
12057c478bd9Sstevel@tonic-gate 		namelen = strlen(name) + 1;
12067c478bd9Sstevel@tonic-gate 	cp = kmem_zalloc(sizeof (in_node_t) + namelen + strlen(addr) + 1,
12077c478bd9Sstevel@tonic-gate 	    KM_SLEEP);
12087c478bd9Sstevel@tonic-gate 	np = (in_node_t *)cp;
12097c478bd9Sstevel@tonic-gate 	if (name) {
12107c478bd9Sstevel@tonic-gate 		np->in_node_name = cp + sizeof (in_node_t);
12117c478bd9Sstevel@tonic-gate 		(void) strcpy(np->in_node_name, name);
12127c478bd9Sstevel@tonic-gate 	}
12137c478bd9Sstevel@tonic-gate 	np->in_unit_addr = cp + sizeof (in_node_t) + namelen;
12147c478bd9Sstevel@tonic-gate 	(void) strcpy(np->in_unit_addr, addr);
12157c478bd9Sstevel@tonic-gate 	return (np);
12167c478bd9Sstevel@tonic-gate }
12177c478bd9Sstevel@tonic-gate 
12187c478bd9Sstevel@tonic-gate /*
12197c478bd9Sstevel@tonic-gate  * Allocate a drv entry and storage for binding name string, and fill it in.
12207c478bd9Sstevel@tonic-gate  */
12217c478bd9Sstevel@tonic-gate static in_drv_t *
12227c478bd9Sstevel@tonic-gate in_alloc_drv(char *bindingname)
12237c478bd9Sstevel@tonic-gate {
12247c478bd9Sstevel@tonic-gate 	in_drv_t *dp;
12257c478bd9Sstevel@tonic-gate 	char *cp;
12267c478bd9Sstevel@tonic-gate 	size_t namelen;
12277c478bd9Sstevel@tonic-gate 
12287c478bd9Sstevel@tonic-gate 	ASSERT(e_ddi_inst_state.ins_busy);
12297c478bd9Sstevel@tonic-gate 	/*
12307c478bd9Sstevel@tonic-gate 	 * Has name or will become root
12317c478bd9Sstevel@tonic-gate 	 */
12327c478bd9Sstevel@tonic-gate 	ASSERT(bindingname || e_ddi_inst_state.ins_root == NULL);
12337c478bd9Sstevel@tonic-gate 	if (bindingname == NULL)
12347c478bd9Sstevel@tonic-gate 		namelen = 0;
12357c478bd9Sstevel@tonic-gate 	else
12367c478bd9Sstevel@tonic-gate 		namelen = strlen(bindingname) + 1;
12377c478bd9Sstevel@tonic-gate 	cp = kmem_zalloc(sizeof (in_drv_t) + namelen, KM_SLEEP);
12387c478bd9Sstevel@tonic-gate 	dp = (in_drv_t *)cp;
12397c478bd9Sstevel@tonic-gate 	if (bindingname) {
12407c478bd9Sstevel@tonic-gate 		dp->ind_driver_name = cp + sizeof (in_drv_t);
12417c478bd9Sstevel@tonic-gate 		(void) strcpy(dp->ind_driver_name, bindingname);
12427c478bd9Sstevel@tonic-gate 	}
12437c478bd9Sstevel@tonic-gate 	dp->ind_state = IN_UNKNOWN;
12447c478bd9Sstevel@tonic-gate 	dp->ind_instance = -1;
12457c478bd9Sstevel@tonic-gate 	return (dp);
12467c478bd9Sstevel@tonic-gate }
12477c478bd9Sstevel@tonic-gate 
12487c478bd9Sstevel@tonic-gate static void
12497c478bd9Sstevel@tonic-gate in_dealloc_node(in_node_t *np)
12507c478bd9Sstevel@tonic-gate {
12517c478bd9Sstevel@tonic-gate 	/*
12527c478bd9Sstevel@tonic-gate 	 * The root node can never be de-allocated
12537c478bd9Sstevel@tonic-gate 	 */
12547c478bd9Sstevel@tonic-gate 	ASSERT(np->in_node_name && np->in_unit_addr);
12557c478bd9Sstevel@tonic-gate 	ASSERT(e_ddi_inst_state.ins_busy);
12567c478bd9Sstevel@tonic-gate 	kmem_free(np, sizeof (in_node_t) + strlen(np->in_node_name)
12577c478bd9Sstevel@tonic-gate 	    + strlen(np->in_unit_addr) + 2);
12587c478bd9Sstevel@tonic-gate }
12597c478bd9Sstevel@tonic-gate 
12607c478bd9Sstevel@tonic-gate static void
12617c478bd9Sstevel@tonic-gate in_dealloc_drv(in_drv_t *dp)
12627c478bd9Sstevel@tonic-gate {
12637c478bd9Sstevel@tonic-gate 	ASSERT(dp->ind_driver_name);
12647c478bd9Sstevel@tonic-gate 	ASSERT(e_ddi_inst_state.ins_busy);
12657c478bd9Sstevel@tonic-gate 	kmem_free(dp, sizeof (in_drv_t) + strlen(dp->ind_driver_name)
12667c478bd9Sstevel@tonic-gate 	    + 1);
12677c478bd9Sstevel@tonic-gate }
12687c478bd9Sstevel@tonic-gate 
12697c478bd9Sstevel@tonic-gate /*
12707c478bd9Sstevel@tonic-gate  * Handle the various possible versions of "no address"
12717c478bd9Sstevel@tonic-gate  */
12727c478bd9Sstevel@tonic-gate static int
12737c478bd9Sstevel@tonic-gate in_eqstr(char *a, char *b)
12747c478bd9Sstevel@tonic-gate {
12757c478bd9Sstevel@tonic-gate 	if (a == b)	/* covers case where both are nulls */
12767c478bd9Sstevel@tonic-gate 		return (1);
12777c478bd9Sstevel@tonic-gate 	if (a == NULL && *b == 0)
12787c478bd9Sstevel@tonic-gate 		return (1);
12797c478bd9Sstevel@tonic-gate 	if (b == NULL && *a == 0)
12807c478bd9Sstevel@tonic-gate 		return (1);
12817c478bd9Sstevel@tonic-gate 	if (a == NULL || b == NULL)
12827c478bd9Sstevel@tonic-gate 		return (0);
12837c478bd9Sstevel@tonic-gate 	return (strcmp(a, b) == 0);
12847c478bd9Sstevel@tonic-gate }
12857c478bd9Sstevel@tonic-gate 
12867c478bd9Sstevel@tonic-gate /*
12877c478bd9Sstevel@tonic-gate  * Returns true if instance no. is already in use by named driver
12887c478bd9Sstevel@tonic-gate  */
12897c478bd9Sstevel@tonic-gate static int
12907c478bd9Sstevel@tonic-gate in_inuse(int instance, char *name)
12917c478bd9Sstevel@tonic-gate {
12927c478bd9Sstevel@tonic-gate 	major_t major;
12937c478bd9Sstevel@tonic-gate 	in_drv_t *dp;
12947c478bd9Sstevel@tonic-gate 	struct devnames *dnp;
12957c478bd9Sstevel@tonic-gate 
12967c478bd9Sstevel@tonic-gate 	ASSERT(e_ddi_inst_state.ins_busy);
12977c478bd9Sstevel@tonic-gate 	/*
12987c478bd9Sstevel@tonic-gate 	 * For now, if we've never heard of this device we assume it is not
12997c478bd9Sstevel@tonic-gate 	 * in use, since we can't tell
13007c478bd9Sstevel@tonic-gate 	 * XXX could do the weaker search through the nomajor list checking
13017c478bd9Sstevel@tonic-gate 	 * XXX for the same name
13027c478bd9Sstevel@tonic-gate 	 */
1303a204de77Scth 	if ((major = ddi_name_to_major(name)) == DDI_MAJOR_T_NONE)
13047c478bd9Sstevel@tonic-gate 		return (0);
13057c478bd9Sstevel@tonic-gate 	dnp = &devnamesp[major];
13067c478bd9Sstevel@tonic-gate 
13077c478bd9Sstevel@tonic-gate 	dp = dnp->dn_inlist;
13087c478bd9Sstevel@tonic-gate 	while (dp) {
13097c478bd9Sstevel@tonic-gate 		if (dp->ind_instance == instance)
13107c478bd9Sstevel@tonic-gate 			return (1);
13117c478bd9Sstevel@tonic-gate 		dp = dp->ind_next;
13127c478bd9Sstevel@tonic-gate 	}
13137c478bd9Sstevel@tonic-gate 	return (0);
13147c478bd9Sstevel@tonic-gate }
13157c478bd9Sstevel@tonic-gate 
13167c478bd9Sstevel@tonic-gate static void
13177c478bd9Sstevel@tonic-gate in_hashdrv(in_drv_t *dp)
13187c478bd9Sstevel@tonic-gate {
13197c478bd9Sstevel@tonic-gate 	struct devnames *dnp;
13207c478bd9Sstevel@tonic-gate 	in_drv_t *mp, *pp;
13217c478bd9Sstevel@tonic-gate 	major_t major;
13227c478bd9Sstevel@tonic-gate 
13237c478bd9Sstevel@tonic-gate 	/* hash to no major list */
1324a204de77Scth 	major = ddi_name_to_major(dp->ind_driver_name);
1325a204de77Scth 	if (major == DDI_MAJOR_T_NONE) {
13267c478bd9Sstevel@tonic-gate 		dp->ind_next = e_ddi_inst_state.ins_no_major;
13277c478bd9Sstevel@tonic-gate 		e_ddi_inst_state.ins_no_major = dp;
13287c478bd9Sstevel@tonic-gate 		return;
13297c478bd9Sstevel@tonic-gate 	}
13307c478bd9Sstevel@tonic-gate 
13317c478bd9Sstevel@tonic-gate 	/*
13327c478bd9Sstevel@tonic-gate 	 * dnp->dn_inlist is sorted by instance number.
13337c478bd9Sstevel@tonic-gate 	 * Adding a new instance entry may introduce holes,
13347c478bd9Sstevel@tonic-gate 	 * set dn_instance to IN_SEARCHME so the next instance
13357c478bd9Sstevel@tonic-gate 	 * assignment may fill in holes.
13367c478bd9Sstevel@tonic-gate 	 */
13377c478bd9Sstevel@tonic-gate 	dnp = &devnamesp[major];
13387c478bd9Sstevel@tonic-gate 	pp = mp = dnp->dn_inlist;
13397c478bd9Sstevel@tonic-gate 	if (mp == NULL || dp->ind_instance < mp->ind_instance) {
13407c478bd9Sstevel@tonic-gate 		/* prepend as the first entry, turn on IN_SEARCHME */
13417c478bd9Sstevel@tonic-gate 		dnp->dn_instance = IN_SEARCHME;
13427c478bd9Sstevel@tonic-gate 		dp->ind_next = mp;
13437c478bd9Sstevel@tonic-gate 		dnp->dn_inlist = dp;
13447c478bd9Sstevel@tonic-gate 		return;
13457c478bd9Sstevel@tonic-gate 	}
13467c478bd9Sstevel@tonic-gate 
13477c478bd9Sstevel@tonic-gate 	ASSERT(mp->ind_instance != dp->ind_instance);
13487c478bd9Sstevel@tonic-gate 	while (mp->ind_instance < dp->ind_instance && mp->ind_next) {
13497c478bd9Sstevel@tonic-gate 		pp = mp;
13507c478bd9Sstevel@tonic-gate 		mp = mp->ind_next;
13517c478bd9Sstevel@tonic-gate 		ASSERT(mp->ind_instance != dp->ind_instance);
13527c478bd9Sstevel@tonic-gate 	}
13537c478bd9Sstevel@tonic-gate 
13547c478bd9Sstevel@tonic-gate 	if (mp->ind_instance < dp->ind_instance) { /* end of list */
13557c478bd9Sstevel@tonic-gate 		dp->ind_next = NULL;
13567c478bd9Sstevel@tonic-gate 		mp->ind_next = dp;
13577c478bd9Sstevel@tonic-gate 	} else {
13587c478bd9Sstevel@tonic-gate 		ASSERT(dnp->dn_instance == IN_SEARCHME);
13597c478bd9Sstevel@tonic-gate 		dp->ind_next = pp->ind_next;
13607c478bd9Sstevel@tonic-gate 		pp->ind_next = dp;
13617c478bd9Sstevel@tonic-gate 	}
13627c478bd9Sstevel@tonic-gate }
13637c478bd9Sstevel@tonic-gate 
13647c478bd9Sstevel@tonic-gate /*
13657c478bd9Sstevel@tonic-gate  * Remove a driver entry from the list, given a previous pointer
13667c478bd9Sstevel@tonic-gate  */
13677c478bd9Sstevel@tonic-gate static void
13687c478bd9Sstevel@tonic-gate in_removedrv(struct devnames *dnp, in_drv_t *mp)
13697c478bd9Sstevel@tonic-gate {
13707c478bd9Sstevel@tonic-gate 	in_drv_t *dp;
13717c478bd9Sstevel@tonic-gate 	in_drv_t *prevp;
13727c478bd9Sstevel@tonic-gate 
13737c478bd9Sstevel@tonic-gate 	if (dnp->dn_inlist == mp) {	/* head of list */
13747c478bd9Sstevel@tonic-gate 		dnp->dn_inlist = mp->ind_next;
13757c478bd9Sstevel@tonic-gate 		dnp->dn_instance = IN_SEARCHME;
13767c478bd9Sstevel@tonic-gate 		in_dq_drv(mp);
13777c478bd9Sstevel@tonic-gate 		in_dealloc_drv(mp);
13787c478bd9Sstevel@tonic-gate 		return;
13797c478bd9Sstevel@tonic-gate 	}
13807c478bd9Sstevel@tonic-gate 	prevp = dnp->dn_inlist;
13817c478bd9Sstevel@tonic-gate 	for (dp = prevp->ind_next; dp; dp = dp->ind_next) {
13827c478bd9Sstevel@tonic-gate 		if (dp == mp) {		/* found it */
13837c478bd9Sstevel@tonic-gate 			break;
13847c478bd9Sstevel@tonic-gate 		}
13857c478bd9Sstevel@tonic-gate 		prevp = dp;
13867c478bd9Sstevel@tonic-gate 	}
13877c478bd9Sstevel@tonic-gate 
13887c478bd9Sstevel@tonic-gate 	ASSERT(dp == mp);
13897c478bd9Sstevel@tonic-gate 	dnp->dn_instance = IN_SEARCHME;
13907c478bd9Sstevel@tonic-gate 	prevp->ind_next = mp->ind_next;
13917c478bd9Sstevel@tonic-gate 	in_dq_drv(mp);
13927c478bd9Sstevel@tonic-gate 	in_dealloc_drv(mp);
13937c478bd9Sstevel@tonic-gate }
13947c478bd9Sstevel@tonic-gate 
13957c478bd9Sstevel@tonic-gate static void
13967c478bd9Sstevel@tonic-gate in_dq_drv(in_drv_t *mp)
13977c478bd9Sstevel@tonic-gate {
13987c478bd9Sstevel@tonic-gate 	struct in_node *node = mp->ind_node;
13997c478bd9Sstevel@tonic-gate 	in_drv_t *ptr, *prev;
14007c478bd9Sstevel@tonic-gate 
14017c478bd9Sstevel@tonic-gate 	if (mp == node->in_drivers) {
14027c478bd9Sstevel@tonic-gate 		node->in_drivers = mp->ind_next_drv;
14037c478bd9Sstevel@tonic-gate 		return;
14047c478bd9Sstevel@tonic-gate 	}
14057c478bd9Sstevel@tonic-gate 	prev = node->in_drivers;
14067c478bd9Sstevel@tonic-gate 	for (ptr = prev->ind_next_drv; ptr != (struct in_drv *)NULL;
14077c478bd9Sstevel@tonic-gate 	    ptr = ptr->ind_next_drv) {
14087c478bd9Sstevel@tonic-gate 		if (ptr == mp) {
14097c478bd9Sstevel@tonic-gate 			prev->ind_next_drv = ptr->ind_next_drv;
14107c478bd9Sstevel@tonic-gate 			return;
14117c478bd9Sstevel@tonic-gate 		}
1412ffc89d77Svikram 		prev = ptr;
14137c478bd9Sstevel@tonic-gate 	}
14147c478bd9Sstevel@tonic-gate 	panic("in_dq_drv: in_drv not found on node driver list");
14157c478bd9Sstevel@tonic-gate }
14167c478bd9Sstevel@tonic-gate 
14177c478bd9Sstevel@tonic-gate 
14187c478bd9Sstevel@tonic-gate in_drv_t *
14197c478bd9Sstevel@tonic-gate in_drvwalk(in_node_t *np, char *binding_name)
14207c478bd9Sstevel@tonic-gate {
14217c478bd9Sstevel@tonic-gate 	char *name;
14227c478bd9Sstevel@tonic-gate 	in_drv_t *dp = np->in_drivers;
14237c478bd9Sstevel@tonic-gate 	while (dp) {
14247c478bd9Sstevel@tonic-gate 		if ((name = i_binding_to_drv_name(dp->ind_driver_name))
14257c478bd9Sstevel@tonic-gate 		    == NULL) {
14267c478bd9Sstevel@tonic-gate 			name = dp->ind_driver_name;
14277c478bd9Sstevel@tonic-gate 		}
14287c478bd9Sstevel@tonic-gate 		if (strcmp(binding_name, name) == 0) {
14297c478bd9Sstevel@tonic-gate 			break;
14307c478bd9Sstevel@tonic-gate 		}
14317c478bd9Sstevel@tonic-gate 		dp = dp->ind_next_drv;
14327c478bd9Sstevel@tonic-gate 	}
14337c478bd9Sstevel@tonic-gate 	return (dp);
14347c478bd9Sstevel@tonic-gate }
14357c478bd9Sstevel@tonic-gate 
14367c478bd9Sstevel@tonic-gate 
14377c478bd9Sstevel@tonic-gate 
14387c478bd9Sstevel@tonic-gate static void
14397c478bd9Sstevel@tonic-gate i_log_devfs_instance_mod(void)
14407c478bd9Sstevel@tonic-gate {
14417c478bd9Sstevel@tonic-gate 	sysevent_t	*ev;
14427c478bd9Sstevel@tonic-gate 	sysevent_id_t	eid;
144316bd7258Scth 	static int	sent_one = 0;
14447c478bd9Sstevel@tonic-gate 
14457c478bd9Sstevel@tonic-gate 	/*
144616bd7258Scth 	 * Prevent unnecessary event generation.  Do not generate more than
144716bd7258Scth 	 * one event during boot.
14487c478bd9Sstevel@tonic-gate 	 */
144916bd7258Scth 	if (sent_one && !i_ddi_io_initialized())
14507c478bd9Sstevel@tonic-gate 		return;
14517c478bd9Sstevel@tonic-gate 
14527c478bd9Sstevel@tonic-gate 	ev = sysevent_alloc(EC_DEVFS, ESC_DEVFS_INSTANCE_MOD, EP_DDI,
14537c478bd9Sstevel@tonic-gate 	    SE_NOSLEEP);
14547c478bd9Sstevel@tonic-gate 	if (ev == NULL) {
14557c478bd9Sstevel@tonic-gate 		return;
14567c478bd9Sstevel@tonic-gate 	}
14577c478bd9Sstevel@tonic-gate 	if (log_sysevent(ev, SE_NOSLEEP, &eid) != 0) {
14587c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "i_log_devfs_instance_mod: failed to post "
14597c478bd9Sstevel@tonic-gate 		    "event");
146016bd7258Scth 	} else {
146116bd7258Scth 		sent_one = 1;
14627c478bd9Sstevel@tonic-gate 	}
14637c478bd9Sstevel@tonic-gate 	sysevent_free(ev);
14647c478bd9Sstevel@tonic-gate }
14657c478bd9Sstevel@tonic-gate 
14667c478bd9Sstevel@tonic-gate void
146794c894bbSVikram Hegde e_ddi_enter_instance(void)
14687c478bd9Sstevel@tonic-gate {
14697c478bd9Sstevel@tonic-gate 	mutex_enter(&e_ddi_inst_state.ins_serial);
14707c478bd9Sstevel@tonic-gate 	if (e_ddi_inst_state.ins_thread == curthread)
14717c478bd9Sstevel@tonic-gate 		e_ddi_inst_state.ins_busy++;
14727c478bd9Sstevel@tonic-gate 	else {
14737c478bd9Sstevel@tonic-gate 		while (e_ddi_inst_state.ins_busy)
14747c478bd9Sstevel@tonic-gate 			cv_wait(&e_ddi_inst_state.ins_serial_cv,
14757c478bd9Sstevel@tonic-gate 			    &e_ddi_inst_state.ins_serial);
14767c478bd9Sstevel@tonic-gate 		e_ddi_inst_state.ins_thread = curthread;
14777c478bd9Sstevel@tonic-gate 		e_ddi_inst_state.ins_busy = 1;
14787c478bd9Sstevel@tonic-gate 	}
14797c478bd9Sstevel@tonic-gate 	mutex_exit(&e_ddi_inst_state.ins_serial);
14807c478bd9Sstevel@tonic-gate }
14817c478bd9Sstevel@tonic-gate 
14827c478bd9Sstevel@tonic-gate void
148394c894bbSVikram Hegde e_ddi_exit_instance(void)
14847c478bd9Sstevel@tonic-gate {
14857c478bd9Sstevel@tonic-gate 	mutex_enter(&e_ddi_inst_state.ins_serial);
14867c478bd9Sstevel@tonic-gate 	e_ddi_inst_state.ins_busy--;
14877c478bd9Sstevel@tonic-gate 	if (e_ddi_inst_state.ins_busy == 0) {
14887c478bd9Sstevel@tonic-gate 		cv_broadcast(&e_ddi_inst_state.ins_serial_cv);
14897c478bd9Sstevel@tonic-gate 		e_ddi_inst_state.ins_thread = NULL;
14907c478bd9Sstevel@tonic-gate 	}
14917c478bd9Sstevel@tonic-gate 	mutex_exit(&e_ddi_inst_state.ins_serial);
14927c478bd9Sstevel@tonic-gate }
14937c478bd9Sstevel@tonic-gate 
14947c478bd9Sstevel@tonic-gate int
149594c894bbSVikram Hegde e_ddi_instance_is_clean(void)
14967c478bd9Sstevel@tonic-gate {
149794c894bbSVikram Hegde 	return (e_ddi_inst_state.ins_dirty == B_FALSE);
14987c478bd9Sstevel@tonic-gate }
14997c478bd9Sstevel@tonic-gate 
15007c478bd9Sstevel@tonic-gate void
150194c894bbSVikram Hegde e_ddi_instance_set_clean(void)
15027c478bd9Sstevel@tonic-gate {
150394c894bbSVikram Hegde 	e_ddi_inst_state.ins_dirty = B_FALSE;
15047c478bd9Sstevel@tonic-gate }
15057c478bd9Sstevel@tonic-gate 
15067c478bd9Sstevel@tonic-gate in_node_t *
150794c894bbSVikram Hegde e_ddi_instance_root(void)
15087c478bd9Sstevel@tonic-gate {
15097c478bd9Sstevel@tonic-gate 	return (e_ddi_inst_state.ins_root);
15107c478bd9Sstevel@tonic-gate }
15117c478bd9Sstevel@tonic-gate 
15127c478bd9Sstevel@tonic-gate /*
15137c478bd9Sstevel@tonic-gate  * Visit a node in the instance tree
15147c478bd9Sstevel@tonic-gate  */
15157c478bd9Sstevel@tonic-gate static int
15167c478bd9Sstevel@tonic-gate in_walk_instances(in_node_t *np, char *path, char *this,
15177c478bd9Sstevel@tonic-gate     int (*f)(const char *, in_node_t *, in_drv_t *, void *), void *arg)
15187c478bd9Sstevel@tonic-gate {
15197c478bd9Sstevel@tonic-gate 	in_drv_t *dp;
15207c478bd9Sstevel@tonic-gate 	int rval = INST_WALK_CONTINUE;
15217c478bd9Sstevel@tonic-gate 	char *next;
15227c478bd9Sstevel@tonic-gate 
15237c478bd9Sstevel@tonic-gate 	while (np != NULL) {
15247c478bd9Sstevel@tonic-gate 
15257c478bd9Sstevel@tonic-gate 		if (np->in_unit_addr[0] == 0)
15267c478bd9Sstevel@tonic-gate 			(void) sprintf(this, "/%s", np->in_node_name);
15277c478bd9Sstevel@tonic-gate 		else
15287c478bd9Sstevel@tonic-gate 			(void) sprintf(this, "/%s@%s", np->in_node_name,
15297c478bd9Sstevel@tonic-gate 			    np->in_unit_addr);
15307c478bd9Sstevel@tonic-gate 		next = this + strlen(this);
15317c478bd9Sstevel@tonic-gate 
15327c478bd9Sstevel@tonic-gate 		for (dp = np->in_drivers; dp; dp = dp->ind_next_drv) {
15337c478bd9Sstevel@tonic-gate 			if (dp->ind_state == IN_PERMANENT) {
15347c478bd9Sstevel@tonic-gate 				rval = (*f)(path, np, dp, arg);
15357c478bd9Sstevel@tonic-gate 				if (rval == INST_WALK_TERMINATE)
15367c478bd9Sstevel@tonic-gate 					break;
15377c478bd9Sstevel@tonic-gate 			}
15387c478bd9Sstevel@tonic-gate 		}
153994c894bbSVikram Hegde 
15407c478bd9Sstevel@tonic-gate 		if (np->in_child) {
15417c478bd9Sstevel@tonic-gate 			rval = in_walk_instances(np->in_child,
15427c478bd9Sstevel@tonic-gate 			    path, next, f, arg);
15437c478bd9Sstevel@tonic-gate 			if (rval == INST_WALK_TERMINATE)
15447c478bd9Sstevel@tonic-gate 				break;
15457c478bd9Sstevel@tonic-gate 		}
15467c478bd9Sstevel@tonic-gate 
15477c478bd9Sstevel@tonic-gate 		np = np->in_sibling;
15487c478bd9Sstevel@tonic-gate 	}
15497c478bd9Sstevel@tonic-gate 
15507c478bd9Sstevel@tonic-gate 	return (rval);
15517c478bd9Sstevel@tonic-gate }
15527c478bd9Sstevel@tonic-gate 
15537c478bd9Sstevel@tonic-gate /*
15547c478bd9Sstevel@tonic-gate  * A general interface for walking the instance tree,
15557c478bd9Sstevel@tonic-gate  * calling a user-supplied callback for each node.
15567c478bd9Sstevel@tonic-gate  */
15577c478bd9Sstevel@tonic-gate int
15587c478bd9Sstevel@tonic-gate e_ddi_walk_instances(int (*f)(const char *,
15597c478bd9Sstevel@tonic-gate 	in_node_t *, in_drv_t *, void *), void *arg)
15607c478bd9Sstevel@tonic-gate {
15617c478bd9Sstevel@tonic-gate 	in_node_t *root;
15627c478bd9Sstevel@tonic-gate 	int rval;
15637c478bd9Sstevel@tonic-gate 	char *path;
15647c478bd9Sstevel@tonic-gate 
15657c478bd9Sstevel@tonic-gate 	path = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
15667c478bd9Sstevel@tonic-gate 
15677c478bd9Sstevel@tonic-gate 	e_ddi_enter_instance();
15687c478bd9Sstevel@tonic-gate 	root = e_ddi_instance_root();
15697c478bd9Sstevel@tonic-gate 	rval = in_walk_instances(root->in_child, path, path, f, arg);
157094c894bbSVikram Hegde 
15717c478bd9Sstevel@tonic-gate 	e_ddi_exit_instance();
15727c478bd9Sstevel@tonic-gate 
15737c478bd9Sstevel@tonic-gate 	kmem_free(path, MAXPATHLEN);
15747c478bd9Sstevel@tonic-gate 	return (rval);
15757c478bd9Sstevel@tonic-gate }
157694c894bbSVikram Hegde 
157794c894bbSVikram Hegde in_node_t *
157894c894bbSVikram Hegde e_ddi_path_to_instance(char *path)
157994c894bbSVikram Hegde {
158094c894bbSVikram Hegde 	in_node_t *np;
158194c894bbSVikram Hegde 
158294c894bbSVikram Hegde 	np = in_make_path(path);
158394c894bbSVikram Hegde 	if (np && np->in_drivers && np->in_drivers->ind_state == IN_PERMANENT) {
158494c894bbSVikram Hegde 		return (np);
158594c894bbSVikram Hegde 	}
158694c894bbSVikram Hegde 	return (NULL);
158794c894bbSVikram Hegde }
158894c894bbSVikram Hegde 
158994c894bbSVikram Hegde void
159094c894bbSVikram Hegde e_ddi_borrow_instance(dev_info_t *cdip, in_node_t *cnp)
159194c894bbSVikram Hegde {
159294c894bbSVikram Hegde 	char		*alias;
159394c894bbSVikram Hegde 	in_node_t	*anp;
159494c894bbSVikram Hegde 	char		*curr = kmem_alloc(MAXPATHLEN, KM_NOSLEEP);
159594c894bbSVikram Hegde 
159694c894bbSVikram Hegde 	if (curr == NULL) {
159794c894bbSVikram Hegde 		ddi_err(DER_PANIC, cdip, "curr alloc failed");
159894c894bbSVikram Hegde 		/*NOTREACHED*/
159994c894bbSVikram Hegde 	}
160094c894bbSVikram Hegde 
160194c894bbSVikram Hegde 	(void) ddi_pathname(cdip, curr);
160294c894bbSVikram Hegde 
160394c894bbSVikram Hegde 	if (cnp->in_drivers) {
160494c894bbSVikram Hegde 		ddi_err(DER_PANIC, cdip, "cnp has instance: %p", cnp);
160594c894bbSVikram Hegde 		/*NOTREACHED*/
160694c894bbSVikram Hegde 	}
160794c894bbSVikram Hegde 
160894c894bbSVikram Hegde 	alias = ddi_curr_redirect(curr);
160994c894bbSVikram Hegde 	kmem_free(curr, MAXPATHLEN);
161094c894bbSVikram Hegde 
161194c894bbSVikram Hegde 	if (alias && (anp = e_ddi_path_to_instance(alias)) != NULL) {
161294c894bbSVikram Hegde 		cnp->in_drivers = anp->in_drivers;
161394c894bbSVikram Hegde 		anp->in_drivers = NULL;
161494c894bbSVikram Hegde 	}
161594c894bbSVikram Hegde }
161694c894bbSVikram Hegde 
161794c894bbSVikram Hegde void
161894c894bbSVikram Hegde e_ddi_return_instance(dev_info_t *cdip, char *addr, in_node_t *cnp)
161994c894bbSVikram Hegde {
162094c894bbSVikram Hegde 	in_node_t	*anp;
162194c894bbSVikram Hegde 	char 		*alias;
162294c894bbSVikram Hegde 	char		*curr = kmem_alloc(MAXPATHLEN, KM_NOSLEEP);
162394c894bbSVikram Hegde 
162494c894bbSVikram Hegde 	if (curr == NULL) {
162594c894bbSVikram Hegde 		ddi_err(DER_PANIC, cdip, "alloc of curr failed");
162694c894bbSVikram Hegde 		/*NOTREACHED*/
162794c894bbSVikram Hegde 	}
162894c894bbSVikram Hegde 
162994c894bbSVikram Hegde 	(void) ddi_pathname(cdip, curr);
163094c894bbSVikram Hegde 	if (addr) {
163194c894bbSVikram Hegde 		(void) strlcat(curr, "@", MAXPATHLEN);
163294c894bbSVikram Hegde 		(void) strlcat(curr, addr, MAXPATHLEN);
163394c894bbSVikram Hegde 
163494c894bbSVikram Hegde 	}
163594c894bbSVikram Hegde 	if (cnp->in_drivers == NULL) {
163694c894bbSVikram Hegde 		ddi_err(DER_PANIC, cdip, "cnp has no inst: %p", cnp);
163794c894bbSVikram Hegde 		/*NOTREACHED*/
163894c894bbSVikram Hegde 	}
163994c894bbSVikram Hegde 
164094c894bbSVikram Hegde 	alias = ddi_curr_redirect(curr);
164194c894bbSVikram Hegde 	kmem_free(curr, MAXPATHLEN);
164294c894bbSVikram Hegde 
164394c894bbSVikram Hegde 	if (alias && (anp = e_ddi_path_to_instance(alias)) != NULL) {
164494c894bbSVikram Hegde 		ASSERT(anp->in_drivers == NULL);
164594c894bbSVikram Hegde 		anp->in_drivers = cnp->in_drivers;
164694c894bbSVikram Hegde 		cnp->in_drivers = NULL;
164794c894bbSVikram Hegde 	}
164894c894bbSVikram Hegde }
1649