xref: /onnv-gate/usr/src/uts/common/os/instance.c (revision 7009:a2cd0bb4d403)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
53250Scth  * Common Development and Distribution License (the "License").
63250Scth  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*7009Scth  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate /*
290Sstevel@tonic-gate  * Instance number assignment code
300Sstevel@tonic-gate  */
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #include <sys/types.h>
330Sstevel@tonic-gate #include <sys/param.h>
340Sstevel@tonic-gate #include <sys/errno.h>
350Sstevel@tonic-gate #include <sys/systm.h>
360Sstevel@tonic-gate #include <sys/kobj.h>
370Sstevel@tonic-gate #include <sys/t_lock.h>
380Sstevel@tonic-gate #include <sys/kmem.h>
390Sstevel@tonic-gate #include <sys/cmn_err.h>
400Sstevel@tonic-gate #include <sys/ddi.h>
410Sstevel@tonic-gate #include <sys/sunddi.h>
420Sstevel@tonic-gate #include <sys/autoconf.h>
430Sstevel@tonic-gate #include <sys/systeminfo.h>
440Sstevel@tonic-gate #include <sys/hwconf.h>
450Sstevel@tonic-gate #include <sys/reboot.h>
460Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
470Sstevel@tonic-gate #include <sys/instance.h>
480Sstevel@tonic-gate #include <sys/debug.h>
490Sstevel@tonic-gate #include <sys/sysevent.h>
500Sstevel@tonic-gate #include <sys/modctl.h>
510Sstevel@tonic-gate #include <sys/console.h>
520Sstevel@tonic-gate #include <sys/cladm.h>
530Sstevel@tonic-gate 
540Sstevel@tonic-gate static void in_preassign_instance(void);
550Sstevel@tonic-gate static void i_log_devfs_instance_mod(void);
560Sstevel@tonic-gate static int in_get_infile(char *);
570Sstevel@tonic-gate static void in_removenode(struct devnames *dnp, in_node_t *mp, in_node_t *ap);
580Sstevel@tonic-gate static in_node_t *in_alloc_node(char *name, char *addr);
590Sstevel@tonic-gate static int in_eqstr(char *a, char *b);
600Sstevel@tonic-gate static char *in_name_addr(char **cpp, char **addrp);
610Sstevel@tonic-gate static in_node_t *in_devwalk(dev_info_t *dip, in_node_t **ap, char *addr);
620Sstevel@tonic-gate static void in_dealloc_node(in_node_t *np);
630Sstevel@tonic-gate static in_node_t *in_make_path(char *path);
640Sstevel@tonic-gate static void in_enlist(in_node_t *ap, in_node_t *np);
650Sstevel@tonic-gate static int in_inuse(int instance, char *name);
660Sstevel@tonic-gate static void in_hashdrv(in_drv_t *dp);
670Sstevel@tonic-gate static in_drv_t *in_drvwalk(in_node_t *np, char *binding_name);
680Sstevel@tonic-gate static in_drv_t *in_alloc_drv(char *bindingname);
690Sstevel@tonic-gate static void in_endrv(in_node_t *np, in_drv_t *dp);
700Sstevel@tonic-gate static void in_dq_drv(in_drv_t *np);
710Sstevel@tonic-gate static void in_removedrv(struct devnames *dnp, in_drv_t *mp);
720Sstevel@tonic-gate static int in_pathin(char *cp, int instance, char *bname, struct bind **args);
733250Scth static int in_next_instance_block(major_t, int);
740Sstevel@tonic-gate static int in_next_instance(major_t);
750Sstevel@tonic-gate 
760Sstevel@tonic-gate /* external functions */
770Sstevel@tonic-gate extern char *i_binding_to_drv_name(char *bname);
780Sstevel@tonic-gate 
790Sstevel@tonic-gate /*
800Sstevel@tonic-gate  * This plus devnames defines the entire software state of the instance world.
810Sstevel@tonic-gate  */
820Sstevel@tonic-gate typedef struct in_softstate {
830Sstevel@tonic-gate 	in_node_t	*ins_root;	/* the root of our instance tree */
840Sstevel@tonic-gate 	in_drv_t	*ins_no_major;	/* majorless drv entries */
850Sstevel@tonic-gate 	/*
860Sstevel@tonic-gate 	 * Used to serialize access to data structures
870Sstevel@tonic-gate 	 */
880Sstevel@tonic-gate 	void		*ins_thread;
890Sstevel@tonic-gate 	kmutex_t	ins_serial;
900Sstevel@tonic-gate 	kcondvar_t	ins_serial_cv;
910Sstevel@tonic-gate 	int		ins_busy;
920Sstevel@tonic-gate 	char		ins_dirty;	/* need flush */
930Sstevel@tonic-gate } in_softstate_t;
940Sstevel@tonic-gate 
950Sstevel@tonic-gate static in_softstate_t e_ddi_inst_state;
960Sstevel@tonic-gate 
970Sstevel@tonic-gate /*
980Sstevel@tonic-gate  * State transition information:
990Sstevel@tonic-gate  * e_ddi_inst_state contains, among other things, the root of a tree of
1000Sstevel@tonic-gate  * device nodes used to track instance number assignments.
1010Sstevel@tonic-gate  * Each device node may contain multiple driver bindings, represented
1020Sstevel@tonic-gate  * by a linked list of in_drv_t nodes, each with an instance assignment
1030Sstevel@tonic-gate  * (except for root node). Each in_drv node can be in one of 3 states,
1040Sstevel@tonic-gate  * indicated by ind_state:
1050Sstevel@tonic-gate  *
1060Sstevel@tonic-gate  * IN_UNKNOWN:	Each node created in this state.  The instance number of
1070Sstevel@tonic-gate  *	this node is not known.  ind_instance is set to -1.
1080Sstevel@tonic-gate  * IN_PROVISIONAL:  When a node is assigned an instance number in
1090Sstevel@tonic-gate  *	e_ddi_assign_instance(), its state is set to IN_PROVISIONAL.
1100Sstevel@tonic-gate  *	Subsequently, the framework will always call either
1110Sstevel@tonic-gate  *	e_ddi_keep_instance() which makes the node IN_PERMANENT,
1120Sstevel@tonic-gate  *	or e_ddi_free_instance(), which deletes the node.
1130Sstevel@tonic-gate  * IN_PERMANENT:
1140Sstevel@tonic-gate  *	If e_ddi_keep_instance() is called on an IN_PROVISIONAL node,
1150Sstevel@tonic-gate  *	its state is set to IN_PERMANENT.
1160Sstevel@tonic-gate  */
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate static char *instance_file = INSTANCE_FILE;
1190Sstevel@tonic-gate static char *instance_file_backup = INSTANCE_FILE INSTANCE_FILE_SUFFIX;
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate /*
1220Sstevel@tonic-gate  * Return values for in_get_infile().
1230Sstevel@tonic-gate  */
1240Sstevel@tonic-gate #define	PTI_FOUND	0
1250Sstevel@tonic-gate #define	PTI_NOT_FOUND	1
1260Sstevel@tonic-gate #define	PTI_REBUILD	2
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate /*
1290Sstevel@tonic-gate  * Path to instance file magic string used for first time boot after
1300Sstevel@tonic-gate  * an install.  If this is the first string in the file we will
1310Sstevel@tonic-gate  * automatically rebuild the file.
1320Sstevel@tonic-gate  */
1330Sstevel@tonic-gate #define	PTI_MAGIC_STR		"#path_to_inst_bootstrap_1"
1340Sstevel@tonic-gate #define	PTI_MAGIC_STR_LEN	(sizeof (PTI_MAGIC_STR) - 1)
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate void
1370Sstevel@tonic-gate e_ddi_instance_init(void)
1380Sstevel@tonic-gate {
1390Sstevel@tonic-gate 	char *file;
1400Sstevel@tonic-gate 	int rebuild = 1;
1410Sstevel@tonic-gate 	struct in_drv *dp;
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate 	mutex_init(&e_ddi_inst_state.ins_serial, NULL, MUTEX_DEFAULT, NULL);
1440Sstevel@tonic-gate 	cv_init(&e_ddi_inst_state.ins_serial_cv, NULL, CV_DEFAULT, NULL);
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate 	/*
1470Sstevel@tonic-gate 	 * Only one thread is allowed to change the state of the instance
1480Sstevel@tonic-gate 	 * number assignments on the system at any given time.
1490Sstevel@tonic-gate 	 * Note that this is not really necessary, as we are single-threaded
1500Sstevel@tonic-gate 	 * here, but it won't hurt, and it allows us to keep ASSERTS for
1510Sstevel@tonic-gate 	 * our assumptions in the code.
1520Sstevel@tonic-gate 	 */
1530Sstevel@tonic-gate 	e_ddi_enter_instance();
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate 	/*
1560Sstevel@tonic-gate 	 * Create the root node, instance zallocs to 0.
1570Sstevel@tonic-gate 	 * The name and address of this node never get examined, we always
1580Sstevel@tonic-gate 	 * start searching with its first child.
1590Sstevel@tonic-gate 	 */
1600Sstevel@tonic-gate 	ASSERT(e_ddi_inst_state.ins_root == NULL);
1610Sstevel@tonic-gate 	e_ddi_inst_state.ins_root = in_alloc_node(NULL, NULL);
1620Sstevel@tonic-gate 	dp = in_alloc_drv("rootnex");
1630Sstevel@tonic-gate 	in_endrv(e_ddi_inst_state.ins_root, dp);
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate 	file = instance_file;
1660Sstevel@tonic-gate 	switch (in_get_infile(file)) {
1670Sstevel@tonic-gate 	default:
1680Sstevel@tonic-gate 	case PTI_NOT_FOUND:
1690Sstevel@tonic-gate 		/* make sure path_to_inst is recreated */
1700Sstevel@tonic-gate 		boothowto |= RB_RECONFIG;
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate 		/*
1730Sstevel@tonic-gate 		 * Something is wrong. First try the backup file.
1740Sstevel@tonic-gate 		 * If not found, rebuild path_to_inst. Emit a
1750Sstevel@tonic-gate 		 * message about the problem.
1760Sstevel@tonic-gate 		 */
1770Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s empty or not found", file);
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate 		file = instance_file_backup;
1800Sstevel@tonic-gate 		if (in_get_infile(file) != PTI_FOUND) {
1810Sstevel@tonic-gate 			cmn_err(CE_NOTE, "rebuilding device instance data");
1820Sstevel@tonic-gate 			break;
1830Sstevel@tonic-gate 		}
1840Sstevel@tonic-gate 		cmn_err(CE_NOTE, "using backup instance data in %s", file);
1850Sstevel@tonic-gate 		/*FALLTHROUGH*/
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate 	case PTI_FOUND:
1880Sstevel@tonic-gate 		/*
1890Sstevel@tonic-gate 		 * We've got a readable file
1900Sstevel@tonic-gate 		 * parse the file into the instance tree
1910Sstevel@tonic-gate 		 */
1920Sstevel@tonic-gate 		(void) read_binding_file(file, NULL, in_pathin);
1930Sstevel@tonic-gate 		rebuild = 0;
1940Sstevel@tonic-gate 		break;
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate 	case PTI_REBUILD:
1970Sstevel@tonic-gate 		cmn_err(CE_CONT,
1985648Ssetje 		    "?Using default device instance data\n");
1990Sstevel@tonic-gate 		break;
2000Sstevel@tonic-gate 	}
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate 	/*
2030Sstevel@tonic-gate 	 * The OBP device tree has been copied to the kernel and
2040Sstevel@tonic-gate 	 * bound to drivers at this point. We walk the per-driver
2050Sstevel@tonic-gate 	 * list to preassign instances. Since the bus addr is
2060Sstevel@tonic-gate 	 * unknown at this point, we cannot place the instance
2070Sstevel@tonic-gate 	 * number in the instance tree. This will be done at
2080Sstevel@tonic-gate 	 * a later time.
2090Sstevel@tonic-gate 	 */
2100Sstevel@tonic-gate 	if (rebuild)
2110Sstevel@tonic-gate 		in_preassign_instance();
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate 	e_ddi_exit_instance();
2140Sstevel@tonic-gate }
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate static void
2170Sstevel@tonic-gate in_preassign_instance()
2180Sstevel@tonic-gate {
2190Sstevel@tonic-gate 	major_t m;
2200Sstevel@tonic-gate 	extern major_t devcnt;
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate 	for (m = 0; m < devcnt; m++) {
2230Sstevel@tonic-gate 		struct devnames *dnp = &devnamesp[m];
2240Sstevel@tonic-gate 		dev_info_t *dip = dnp->dn_head;
2250Sstevel@tonic-gate 		while (dip) {
2260Sstevel@tonic-gate 			DEVI(dip)->devi_instance = dnp->dn_instance;
2270Sstevel@tonic-gate 			dnp->dn_instance++;
2280Sstevel@tonic-gate 			dip = ddi_get_next(dip);
2290Sstevel@tonic-gate 		}
2300Sstevel@tonic-gate 	}
2310Sstevel@tonic-gate }
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate /*
2340Sstevel@tonic-gate  * Checks to see if the /etc/path_to_inst file exists and whether or not
2350Sstevel@tonic-gate  * it has the magic string in it.
2360Sstevel@tonic-gate  *
2370Sstevel@tonic-gate  * Returns one of the following:
2380Sstevel@tonic-gate  *
2390Sstevel@tonic-gate  *	PTI_FOUND	- We have found the /etc/path_to_inst file
2400Sstevel@tonic-gate  *	PTI_REBUILD	- We have found the /etc/path_to_inst file and the
2410Sstevel@tonic-gate  *			  first line was PTI_MAGIC_STR.
2420Sstevel@tonic-gate  *	PTI_NOT_FOUND	- We did not find the /etc/path_to_inst file
2430Sstevel@tonic-gate  *
2440Sstevel@tonic-gate  */
2450Sstevel@tonic-gate static int
2460Sstevel@tonic-gate in_get_infile(char *filename)
2470Sstevel@tonic-gate {
2485648Ssetje 	struct _buf *file;
2490Sstevel@tonic-gate 	int return_val;
2500Sstevel@tonic-gate 	char buf[PTI_MAGIC_STR_LEN];
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate 	/*
2530Sstevel@tonic-gate 	 * Try to open the file.
2540Sstevel@tonic-gate 	 */
2555648Ssetje 	if ((file = kobj_open_file(filename)) == (struct _buf *)-1) {
2560Sstevel@tonic-gate 		return (PTI_NOT_FOUND);
2570Sstevel@tonic-gate 	}
2580Sstevel@tonic-gate 	return_val = PTI_FOUND;
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate 	/*
2610Sstevel@tonic-gate 	 * Read the first PTI_MAGIC_STR_LEN bytes from the file to see if
2620Sstevel@tonic-gate 	 * it contains the magic string.  If there aren't that many bytes
2630Sstevel@tonic-gate 	 * in the file, then assume file is correct and no magic string
2640Sstevel@tonic-gate 	 * and move on.
2650Sstevel@tonic-gate 	 */
2665648Ssetje 	switch (kobj_read_file(file, buf, PTI_MAGIC_STR_LEN, 0)) {
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate 	case PTI_MAGIC_STR_LEN:
2690Sstevel@tonic-gate 		/*
2700Sstevel@tonic-gate 		 * If the first PTI_MAGIC_STR_LEN bytes are the magic string
2710Sstevel@tonic-gate 		 * then return PTI_REBUILD.
2720Sstevel@tonic-gate 		 */
2730Sstevel@tonic-gate 		if (strncmp(PTI_MAGIC_STR, buf, PTI_MAGIC_STR_LEN) == 0)
2740Sstevel@tonic-gate 			return_val = PTI_REBUILD;
2750Sstevel@tonic-gate 		break;
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 	case 0:
2780Sstevel@tonic-gate 		/*
2790Sstevel@tonic-gate 		 * If the file is zero bytes in length, then consider the
2800Sstevel@tonic-gate 		 * file to not be found
2810Sstevel@tonic-gate 		 */
2820Sstevel@tonic-gate 		return_val = PTI_NOT_FOUND;
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate 	default: /* Do nothing we have a good file */
2850Sstevel@tonic-gate 		break;
2860Sstevel@tonic-gate 	}
2870Sstevel@tonic-gate 
2885648Ssetje 	kobj_close_file(file);
2890Sstevel@tonic-gate 	return (return_val);
2900Sstevel@tonic-gate }
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate int
2930Sstevel@tonic-gate is_pseudo_device(dev_info_t *dip)
2940Sstevel@tonic-gate {
2950Sstevel@tonic-gate 	dev_info_t	*pdip;
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate 	for (pdip = ddi_get_parent(dip); pdip && pdip != ddi_root_node();
2980Sstevel@tonic-gate 	    pdip = ddi_get_parent(pdip)) {
2990Sstevel@tonic-gate 		if (strcmp(ddi_get_name(pdip), DEVI_PSEUDO_NEXNAME) == 0)
3000Sstevel@tonic-gate 			return (1);
3010Sstevel@tonic-gate 	}
3020Sstevel@tonic-gate 	return (0);
3030Sstevel@tonic-gate }
3040Sstevel@tonic-gate 
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate static void
3070Sstevel@tonic-gate in_set_instance(dev_info_t *dip, in_drv_t *dp, major_t major)
3080Sstevel@tonic-gate {
3090Sstevel@tonic-gate 	/* use preassigned instance if available */
3100Sstevel@tonic-gate 	if (DEVI(dip)->devi_instance != -1)
3110Sstevel@tonic-gate 		dp->ind_instance = DEVI(dip)->devi_instance;
3120Sstevel@tonic-gate 	else
3130Sstevel@tonic-gate 		dp->ind_instance = in_next_instance(major);
3140Sstevel@tonic-gate }
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate /*
3173250Scth  * Return 1 if instance block was assigned for the path.
3183250Scth  *
3193250Scth  * For multi-port NIC cards, sequential instance assignment across all
3203250Scth  * ports on a card is highly deseriable since the ppa is typically the
3213250Scth  * same as the instance number, and the ppa is used in the NIC's public
3223250Scth  * /dev name. This sequential assignment typically occurs as a result
3233250Scth  * of in_preassign_instance() after initial install, or by
3243250Scth  * i_ndi_init_hw_children() for NIC ports that share a common parent.
3253250Scth  *
3263250Scth  * Some NIC cards however use multi-function bridge chips, and to
3273250Scth  * support sequential instance assignment accross all ports, without
3283250Scth  * disabling multi-threaded attach, we have a (currently) undocumented
3293250Scth  * hack to allocate instance numbers in contiguous blocks based on
3303250Scth  * driver.conf properties.
3313250Scth  *
3323250Scth  *                       ^
3333250Scth  *           /----------   ------------\
3343250Scth  *        pci@0                      pci@0,1	MULTI-FUNCTION BRIDGE CHIP
3353250Scth  *       /     \                    /       \
3363250Scth  * FJSV,e4ta@4  FJSV,e4ta@4,1   FJSV,e4ta@6 FJSV,e4ta@6,1	NIC PORTS
3373250Scth  *      n            n+2             n+2         n+3		INSTANCE
3383250Scth  *
3393250Scth  * For the above example, the following driver.conf properties would be
3403250Scth  * used to guarantee sequential instance number assignment.
3413250Scth  *
3423250Scth  * ddi-instance-blocks ="ib-FJSVe4ca", "ib-FJSVe4ta", "ib-generic";
3433250Scth  * ib-FJSVe4ca =	"/pci@0/FJSV,e4ca@4", "/pci@0/FJSV,e4ca@4,1",
3443250Scth  *			"/pci@0,1/FJSV,e4ca@6", "/pci@0,1/FJSV,e4ca@6,1";
3453250Scth  * ib-FJSVe4ta =	"/pci@0/FJSV,e4ta@4", "/pci@0/FJSV,e4ta@4,1",
3463250Scth  *			"/pci@0,1/FJSV,e4ta@6", "/pci@0,1/FJSV,e4ta@6,1";
3473250Scth  * ib-generic =		"/pci@0/network@4", "/pci@0/network@4,1",
3483250Scth  *			"/pci@0,1/network@6", "/pci@0,1/network@6,1";
3493250Scth  *
3503250Scth  * The value of the 'ddi-instance-blocks' property references a series
3513250Scth  * of card specific properties, like 'ib-FJSV-e4ta', who's value
3523250Scth  * defines a single 'instance block'.  The 'instance block' describes
3533250Scth  * all the paths below a multi-function bridge, where each path is
3543250Scth  * called an 'instance path'.  The 'instance block' property value is a
3553250Scth  * series of 'instance paths'.  The number of 'instance paths' in an
3563250Scth  * 'instance block' defines the size of the instance block, and the
3573250Scth  * ordering of the 'instance paths' defines the instance number
3583250Scth  * assignment order for paths going through the 'instance block'.
3593250Scth  *
3603250Scth  * In the instance assignment code below, if a (path, driver) that
3613250Scth  * currently has no instance number has a path that goes through an
3623250Scth  * 'instance block', then block instance number allocation occurs.  The
3633250Scth  * block allocation code will find a sequential set of unused instance
3643250Scth  * numbers, and assign instance numbers for all the paths in the
3653250Scth  * 'instance block'.  Each path is assigned a persistent instance
3663250Scth  * number, even paths that don't exist in the device tree or fail
3673250Scth  * probe(9E).
3683250Scth  */
3693250Scth static int
3703250Scth in_assign_instance_block(dev_info_t *dip)
3713250Scth {
3723250Scth 	char		**ibn;		/* instance block names */
3733250Scth 	uint_t		nibn;		/* number of instance block names */
3743250Scth 	uint_t		ibni;		/* ibn index */
3753250Scth 	char		*driver;
3763250Scth 	major_t		major;
3773250Scth 	char		*path;
3783250Scth 	char		*addr;
3793250Scth 	int		plen;
3803250Scth 	char		**ibp;		/* instance block paths */
3813250Scth 	uint_t		nibp;		/* number of paths in instance block */
3823250Scth 	uint_t		ibpi;		/* ibp index */
3833250Scth 	int		ibplen;		/* length of instance block path */
3843250Scth 	char		*ipath;
3853250Scth 	int		instance_base;
3863250Scth 	int		splice;
3873250Scth 	int		i;
3883250Scth 
3893250Scth 	/* check for fresh install case (in miniroot) */
3903250Scth 	if (DEVI(dip)->devi_instance != -1)
3913250Scth 		return (0);			/* already assigned */
3923250Scth 
3933250Scth 	/*
3943250Scth 	 * Check to see if we need to allocate a block of contiguous instance
3953250Scth 	 * numbers by looking for the 'ddi-instance-blocks' property.
3963250Scth 	 */
3973250Scth 	if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
3983250Scth 	    "ddi-instance-blocks", &ibn, &nibn) != DDI_SUCCESS)
3993250Scth 		return (0);			/* no instance block needed */
4003250Scth 
4013250Scth 	/*
4023250Scth 	 * Get information out about node we are processing.
4033250Scth 	 *
4043250Scth 	 * NOTE: Since the node is not yet at DS_INITIALIZED, ddi_pathname()
4053250Scth 	 * will not return the unit-address of the final path component even
4063250Scth 	 * though the node has an established devi_addr unit-address - so we
4073250Scth 	 * need to add the unit-address by hand.
4083250Scth 	 */
4093250Scth 	driver = (char *)ddi_driver_name(dip);
4103250Scth 	major = ddi_driver_major(dip);
4113250Scth 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
4123250Scth 	(void) ddi_pathname(dip, path);
4133250Scth 	if ((addr =  ddi_get_name_addr(dip)) != NULL) {
4143250Scth 		(void) strcat(path, "@");
4153250Scth 		(void) strcat(path, addr);
4163250Scth 	}
4173250Scth 	plen = strlen(path);
4183250Scth 
4193250Scth 	/* loop through instance block names */
4203250Scth 	for (ibni = 0; ibni < nibn; ibni++) {
4213250Scth 		if (ibn[ibni] == NULL)
4223250Scth 			continue;
4233250Scth 
4243250Scth 		/* lookup instance block */
4253250Scth 		if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, dip,
4263250Scth 		    DDI_PROP_DONTPASS, ibn[ibni],
4273250Scth 		    &ibp, &nibp) != DDI_SUCCESS) {
4283250Scth 			cmn_err(CE_WARN,
4293250Scth 			    "no devinition for instance block '%s' in %s.conf",
4303250Scth 			    ibn[ibni], driver);
4313250Scth 			continue;
4323250Scth 		}
4333250Scth 
4343250Scth 		/* Does 'path' go through this instance block? */
4353250Scth 		for (ibpi = 0; ibpi < nibp; ibpi++) {
4363250Scth 			if (ibp[ibpi] == NULL)
4373250Scth 				continue;
4383250Scth 			ibplen = strlen(ibp[ibpi]);
4393250Scth 			if ((ibplen <= plen) &&
4403250Scth 			    (strcmp(ibp[ibpi], path + plen - ibplen) == 0))
4413250Scth 				break;
4423250Scth 
4433250Scth 		}
4443250Scth 		if (ibpi >= nibp) {
4453250Scth 			ddi_prop_free(ibp);
4463250Scth 			continue;		/* no try next instance block */
4473250Scth 		}
4483250Scth 
4493250Scth 		/* yes, allocate and assign instances for all paths in block */
4503250Scth 
4513250Scth 		/*
4523250Scth 		 * determine where we splice in instance paths and verify
4533250Scth 		 * that none of the paths are too long.
4543250Scth 		 */
4553250Scth 		splice = plen - ibplen;
4563250Scth 		for (i = 0; i < nibp; i++) {
4573250Scth 			if ((splice + strlen(ibp[i])+ 1) >= MAXPATHLEN) {
4583250Scth 				cmn_err(CE_WARN,
4593250Scth 				    "path %d through instance block '%s' from "
4603250Scth 				    "%s.conf too long", i, ibn[ibni], driver);
4613250Scth 				break;
4623250Scth 			}
4633250Scth 		}
4643250Scth 		if (i < nibp) {
4653250Scth 			ddi_prop_free(ibp);
4663250Scth 			continue;		/* too long */
4673250Scth 		}
4683250Scth 
4693250Scth 		/* allocate the instance block - no more failures */
4703250Scth 		instance_base = in_next_instance_block(major, nibp);
4713250Scth 
4723250Scth 		ipath = kmem_alloc(MAXPATHLEN, KM_SLEEP);
4733250Scth 		for (ibpi = 0; ibpi < nibp; ibpi++) {
4743250Scth 			if (ibp[ibpi] == NULL)
4753250Scth 				continue;
4763250Scth 			(void) strcpy(ipath, path);
4773250Scth 			(void) strcpy(ipath + splice, ibp[ibpi]);
4783250Scth 			(void) in_pathin(ipath,
4793250Scth 			    instance_base + ibpi, driver, NULL);
4803250Scth 		}
4813250Scth 
4823250Scth 		/* free allocations */
4833250Scth 		kmem_free(ipath, MAXPATHLEN);
4843250Scth 		ddi_prop_free(ibp);
4853250Scth 		kmem_free(path, MAXPATHLEN);
4863250Scth 		ddi_prop_free(ibn);
4873250Scth 
4883250Scth 		/* notify devfsadmd to sync of path_to_inst file */
4893250Scth 		mutex_enter(&e_ddi_inst_state.ins_serial);
4903250Scth 		i_log_devfs_instance_mod();
4913250Scth 		e_ddi_inst_state.ins_dirty = 1;
4923250Scth 		mutex_exit(&e_ddi_inst_state.ins_serial);
4933250Scth 		return (1);
4943250Scth 	}
4953250Scth 
4963250Scth 	/* our path did not go through any of of the instance blocks */
4973250Scth 	kmem_free(path, MAXPATHLEN);
4983250Scth 	ddi_prop_free(ibn);
4993250Scth 	return (0);
5003250Scth }
5013250Scth 
5023250Scth /*
5030Sstevel@tonic-gate  * Look up an instance number for a dev_info node, and assign one if it does
5040Sstevel@tonic-gate  * not have one (the dev_info node has devi_name and devi_addr already set).
5050Sstevel@tonic-gate  */
5060Sstevel@tonic-gate uint_t
5070Sstevel@tonic-gate e_ddi_assign_instance(dev_info_t *dip)
5080Sstevel@tonic-gate {
5090Sstevel@tonic-gate 	char *name;
5100Sstevel@tonic-gate 	in_node_t *ap, *np;
5110Sstevel@tonic-gate 	in_drv_t *dp;
5120Sstevel@tonic-gate 	major_t major;
5130Sstevel@tonic-gate 	uint_t ret;
5140Sstevel@tonic-gate 	char *bname;
5150Sstevel@tonic-gate 
5160Sstevel@tonic-gate 	/*
5170Sstevel@tonic-gate 	 * Allow implementation to override
5180Sstevel@tonic-gate 	 */
5190Sstevel@tonic-gate 	if ((ret = impl_assign_instance(dip)) != (uint_t)-1)
5200Sstevel@tonic-gate 		return (ret);
5210Sstevel@tonic-gate 
5220Sstevel@tonic-gate 	/*
5230Sstevel@tonic-gate 	 * If this is a pseudo-device, use the instance number
5240Sstevel@tonic-gate 	 * assigned by the pseudo nexus driver. The mutex is
5250Sstevel@tonic-gate 	 * not needed since the instance tree is not used.
5260Sstevel@tonic-gate 	 */
5270Sstevel@tonic-gate 	if (is_pseudo_device(dip)) {
5280Sstevel@tonic-gate 		return (ddi_get_instance(dip));
5290Sstevel@tonic-gate 	}
5300Sstevel@tonic-gate 
5310Sstevel@tonic-gate 	/*
5320Sstevel@tonic-gate 	 * Only one thread is allowed to change the state of the instance
5330Sstevel@tonic-gate 	 * number assignments on the system at any given time.
5340Sstevel@tonic-gate 	 */
5350Sstevel@tonic-gate 	e_ddi_enter_instance();
5360Sstevel@tonic-gate 
5370Sstevel@tonic-gate 	/*
5380Sstevel@tonic-gate 	 * Look for instance node, allocate one if not found
5390Sstevel@tonic-gate 	 */
5400Sstevel@tonic-gate 	np = in_devwalk(dip, &ap, NULL);
5410Sstevel@tonic-gate 	if (np == NULL) {
5423250Scth 		if (in_assign_instance_block(dip)) {
5433250Scth 			np = in_devwalk(dip, &ap, NULL);
5443250Scth 		} else {
5453250Scth 			name = ddi_node_name(dip);
5463250Scth 			np = in_alloc_node(name, ddi_get_name_addr(dip));
5473250Scth 			ASSERT(np != NULL);
5483250Scth 			in_enlist(ap, np);	/* insert into tree */
5493250Scth 		}
5500Sstevel@tonic-gate 	}
5510Sstevel@tonic-gate 	ASSERT(np == in_devwalk(dip, &ap, NULL));
5520Sstevel@tonic-gate 
5530Sstevel@tonic-gate 	/*
5540Sstevel@tonic-gate 	 * Look for driver entry, allocate one if not found
5550Sstevel@tonic-gate 	 */
5560Sstevel@tonic-gate 	bname = (char *)ddi_driver_name(dip);
5570Sstevel@tonic-gate 	dp = in_drvwalk(np, bname);
5580Sstevel@tonic-gate 	if (dp == NULL) {
5590Sstevel@tonic-gate 		dp = in_alloc_drv(bname);
5600Sstevel@tonic-gate 		ASSERT(dp != NULL);
5610Sstevel@tonic-gate 		major = ddi_driver_major(dip);
562*7009Scth 		ASSERT(major != DDI_MAJOR_T_NONE);
5630Sstevel@tonic-gate 		in_endrv(np, dp);
5640Sstevel@tonic-gate 		in_set_instance(dip, dp, major);
5650Sstevel@tonic-gate 		dp->ind_state = IN_PROVISIONAL;
5660Sstevel@tonic-gate 		in_hashdrv(dp);
5670Sstevel@tonic-gate 	}
5680Sstevel@tonic-gate 
5690Sstevel@tonic-gate 	ret = dp->ind_instance;
5700Sstevel@tonic-gate 
5710Sstevel@tonic-gate 	e_ddi_exit_instance();
5720Sstevel@tonic-gate 	return (ret);
5730Sstevel@tonic-gate }
5740Sstevel@tonic-gate 
5750Sstevel@tonic-gate static int
5760Sstevel@tonic-gate mkpathname(char *path, in_node_t *np, int len)
5770Sstevel@tonic-gate {
5780Sstevel@tonic-gate 	int len_needed;
5790Sstevel@tonic-gate 
5800Sstevel@tonic-gate 	if (np == e_ddi_inst_state.ins_root)
5810Sstevel@tonic-gate 		return (DDI_SUCCESS);
5820Sstevel@tonic-gate 
5830Sstevel@tonic-gate 	if (mkpathname(path, np->in_parent, len) == DDI_FAILURE)
5840Sstevel@tonic-gate 		return (DDI_FAILURE);
5850Sstevel@tonic-gate 
5860Sstevel@tonic-gate 	len_needed = strlen(path);
5870Sstevel@tonic-gate 	len_needed += strlen(np->in_node_name) + 1;	/* for '/' */
5880Sstevel@tonic-gate 	if (np->in_unit_addr) {
5890Sstevel@tonic-gate 		len_needed += strlen(np->in_unit_addr) + 1;  /* for '@' */
5900Sstevel@tonic-gate 	}
5910Sstevel@tonic-gate 	len_needed += 1; /* for '\0' */
5920Sstevel@tonic-gate 
5930Sstevel@tonic-gate 	/*
5940Sstevel@tonic-gate 	 * XX complain
5950Sstevel@tonic-gate 	 */
5960Sstevel@tonic-gate 	if (len_needed > len)
5970Sstevel@tonic-gate 		return (DDI_FAILURE);
5980Sstevel@tonic-gate 
5990Sstevel@tonic-gate 	if (np->in_unit_addr[0] == '\0')
6000Sstevel@tonic-gate 		(void) sprintf(path+strlen(path), "/%s", np->in_node_name);
6010Sstevel@tonic-gate 	else
6020Sstevel@tonic-gate 		(void) sprintf(path+strlen(path), "/%s@%s", np->in_node_name,
6030Sstevel@tonic-gate 		    np->in_unit_addr);
6040Sstevel@tonic-gate 
6050Sstevel@tonic-gate 	return (DDI_SUCCESS);
6060Sstevel@tonic-gate }
6070Sstevel@tonic-gate 
6080Sstevel@tonic-gate /*
6090Sstevel@tonic-gate  * produce the path to the given instance of a major number.
6100Sstevel@tonic-gate  * path must hold MAXPATHLEN string
6110Sstevel@tonic-gate  */
6120Sstevel@tonic-gate int
6130Sstevel@tonic-gate e_ddi_instance_majorinstance_to_path(major_t major, uint_t inst, char *path)
6140Sstevel@tonic-gate {
6150Sstevel@tonic-gate 	struct devnames	*dnp;
6160Sstevel@tonic-gate 	in_drv_t	*dp;
6170Sstevel@tonic-gate 	int		ret;
6180Sstevel@tonic-gate 
6190Sstevel@tonic-gate 	e_ddi_enter_instance();
6200Sstevel@tonic-gate 
6210Sstevel@tonic-gate 	/* look for the instance threaded off major */
6220Sstevel@tonic-gate 	dnp = &devnamesp[major];
6230Sstevel@tonic-gate 	for (dp = dnp->dn_inlist; dp != NULL; dp = dp->ind_next)
6240Sstevel@tonic-gate 		if (dp->ind_instance == inst)
6250Sstevel@tonic-gate 			break;
6260Sstevel@tonic-gate 
6270Sstevel@tonic-gate 	/* produce path from the node that uses the instance */
6280Sstevel@tonic-gate 	if (dp) {
6290Sstevel@tonic-gate 		*path = 0;
6300Sstevel@tonic-gate 		ret = mkpathname(path, dp->ind_node, MAXPATHLEN);
6310Sstevel@tonic-gate 	} else
6320Sstevel@tonic-gate 		ret = DDI_FAILURE;
6330Sstevel@tonic-gate 
6340Sstevel@tonic-gate 	e_ddi_exit_instance();
6350Sstevel@tonic-gate 	return (ret);
6360Sstevel@tonic-gate }
6370Sstevel@tonic-gate 
6380Sstevel@tonic-gate /*
6393250Scth  * Allocate a sequential block of instance numbers for the specified driver,
6403250Scth  * and return the base instance number of the block.  The implementation
6413250Scth  * depends on the list being sorted in ascending instance number sequence.
6423250Scth  * When there are no 'holes' in the allocation sequence, dn_instance is the
6433250Scth  * next available instance number. When dn_instance is IN_SEARCHME, hole(s)
6443250Scth  * exists and a slower code path executes which tries to fill holes.
6450Sstevel@tonic-gate  */
6460Sstevel@tonic-gate static int
6473250Scth in_next_instance_block(major_t major, int block_size)
6480Sstevel@tonic-gate {
6493250Scth 	unsigned int	prev;
6503250Scth 	struct devnames	*dnp;
6513250Scth 	in_drv_t	*dp;
6523250Scth 	int		base;
6533250Scth 	int		hole;
6540Sstevel@tonic-gate 
6550Sstevel@tonic-gate 	dnp = &devnamesp[major];
656*7009Scth 	ASSERT(major != DDI_MAJOR_T_NONE);
6570Sstevel@tonic-gate 	ASSERT(e_ddi_inst_state.ins_busy);
6583250Scth 	ASSERT(block_size);
6593250Scth 
6603250Scth 	/* check to see if we can do a quick allocation */
6613250Scth 	if (dnp->dn_instance != IN_SEARCHME) {
6623250Scth 		base = dnp->dn_instance;
6633250Scth 		dnp->dn_instance += block_size;
6643250Scth 		return (base);
6653250Scth 	}
6660Sstevel@tonic-gate 	dp = dnp->dn_inlist;
6670Sstevel@tonic-gate 
6683250Scth 	/* no existing entries, allocate block at 0 */
6690Sstevel@tonic-gate 	if (dp == NULL) {
6703250Scth 		dnp->dn_instance = block_size;
6710Sstevel@tonic-gate 		return (0);
6720Sstevel@tonic-gate 	}
6730Sstevel@tonic-gate 
6740Sstevel@tonic-gate 	prev = dp->ind_instance;
6753250Scth 	if (prev >= block_size)
6763250Scth 		return (0);		/* we fit in hole at beginning */
6773250Scth 
6783250Scth 	/* search the list for a large enough hole */
6793250Scth 	for (dp = dp->ind_next, hole = 0; dp; dp = dp->ind_next) {
6803250Scth 		if (dp->ind_instance != (prev + 1))
6813250Scth 			hole++;			/* we have a hole */
6823250Scth 		if (dp->ind_instance >= (prev + block_size + 1))
6833250Scth 			break;			/* we fit in hole */
6843250Scth 		prev = dp->ind_instance;
6850Sstevel@tonic-gate 	}
6863250Scth 
6870Sstevel@tonic-gate 	/*
6883250Scth 	 * If hole is zero then all holes are patched and we can resume
6893250Scth 	 * quick allocations.
6900Sstevel@tonic-gate 	 */
6913250Scth 	if (hole == 0)
6923250Scth 		dnp->dn_instance = prev + 1 + block_size;
6933250Scth 
6943250Scth 	return (prev + 1);
6953250Scth }
6960Sstevel@tonic-gate 
6973250Scth /* assign instance block of size 1 */
6983250Scth static int
6993250Scth in_next_instance(major_t major)
7003250Scth {
7013250Scth 	return (in_next_instance_block(major, 1));
7020Sstevel@tonic-gate }
7030Sstevel@tonic-gate 
7040Sstevel@tonic-gate /*
7050Sstevel@tonic-gate  * This call causes us to *forget* the instance number we've generated
7060Sstevel@tonic-gate  * for a given device if it was not permanent.
7070Sstevel@tonic-gate  */
7080Sstevel@tonic-gate void
7090Sstevel@tonic-gate e_ddi_free_instance(dev_info_t *dip, char *addr)
7100Sstevel@tonic-gate {
7110Sstevel@tonic-gate 	char *name;
7120Sstevel@tonic-gate 	in_node_t *np;
7130Sstevel@tonic-gate 	in_node_t *ap;	/* ancestor node */
7140Sstevel@tonic-gate 	major_t major;
7150Sstevel@tonic-gate 	struct devnames *dnp;
7160Sstevel@tonic-gate 	in_drv_t *dp;	/* in_drv entry */
7170Sstevel@tonic-gate 
7180Sstevel@tonic-gate 	/*
7190Sstevel@tonic-gate 	 * Allow implementation override
7200Sstevel@tonic-gate 	 */
7210Sstevel@tonic-gate 	if (impl_free_instance(dip) == DDI_SUCCESS)
7220Sstevel@tonic-gate 		return;
7230Sstevel@tonic-gate 
7240Sstevel@tonic-gate 	/*
7250Sstevel@tonic-gate 	 * If this is a pseudo-device, no instance number
7260Sstevel@tonic-gate 	 * was assigned.
7270Sstevel@tonic-gate 	 */
7280Sstevel@tonic-gate 	if (is_pseudo_device(dip)) {
7290Sstevel@tonic-gate 		return;
7300Sstevel@tonic-gate 	}
7310Sstevel@tonic-gate 
7320Sstevel@tonic-gate 	name = (char *)ddi_driver_name(dip);
7330Sstevel@tonic-gate 	major = ddi_driver_major(dip);
734*7009Scth 	ASSERT(major != DDI_MAJOR_T_NONE);
7350Sstevel@tonic-gate 	dnp = &devnamesp[major];
7360Sstevel@tonic-gate 	/*
7370Sstevel@tonic-gate 	 * Only one thread is allowed to change the state of the instance
7380Sstevel@tonic-gate 	 * number assignments on the system at any given time.
7390Sstevel@tonic-gate 	 */
7400Sstevel@tonic-gate 	e_ddi_enter_instance();
7410Sstevel@tonic-gate 	np = in_devwalk(dip, &ap, addr);
7420Sstevel@tonic-gate 	ASSERT(np);
7430Sstevel@tonic-gate 	dp = in_drvwalk(np, name);
7440Sstevel@tonic-gate 	ASSERT(dp);
7450Sstevel@tonic-gate 	if (dp->ind_state == IN_PROVISIONAL) {
7460Sstevel@tonic-gate 		in_removedrv(dnp, dp);
7470Sstevel@tonic-gate 	}
7480Sstevel@tonic-gate 	if (np->in_drivers == NULL) {
7490Sstevel@tonic-gate 		in_removenode(dnp, np, ap);
7500Sstevel@tonic-gate 	}
7510Sstevel@tonic-gate 	e_ddi_exit_instance();
7520Sstevel@tonic-gate }
7530Sstevel@tonic-gate 
7540Sstevel@tonic-gate /*
7550Sstevel@tonic-gate  * This makes our memory of an instance assignment permanent
7560Sstevel@tonic-gate  */
7570Sstevel@tonic-gate void
7580Sstevel@tonic-gate e_ddi_keep_instance(dev_info_t *dip)
7590Sstevel@tonic-gate {
7600Sstevel@tonic-gate 	in_node_t *np, *ap;
7610Sstevel@tonic-gate 	in_drv_t *dp;
7620Sstevel@tonic-gate 
7630Sstevel@tonic-gate 	/*
7640Sstevel@tonic-gate 	 * Allow implementation override
7650Sstevel@tonic-gate 	 */
7660Sstevel@tonic-gate 	if (impl_keep_instance(dip) == DDI_SUCCESS)
7670Sstevel@tonic-gate 		return;
7680Sstevel@tonic-gate 
7690Sstevel@tonic-gate 	/*
7700Sstevel@tonic-gate 	 * Nothing to do for pseudo devices.
7710Sstevel@tonic-gate 	 */
7720Sstevel@tonic-gate 	if (is_pseudo_device(dip))
7730Sstevel@tonic-gate 		return;
7740Sstevel@tonic-gate 
7750Sstevel@tonic-gate 	/*
7760Sstevel@tonic-gate 	 * Only one thread is allowed to change the state of the instance
7770Sstevel@tonic-gate 	 * number assignments on the system at any given time.
7780Sstevel@tonic-gate 	 */
7790Sstevel@tonic-gate 	e_ddi_enter_instance();
7800Sstevel@tonic-gate 	np = in_devwalk(dip, &ap, NULL);
7810Sstevel@tonic-gate 	ASSERT(np);
7820Sstevel@tonic-gate 	dp = in_drvwalk(np, (char *)ddi_driver_name(dip));
7830Sstevel@tonic-gate 	ASSERT(dp);
7840Sstevel@tonic-gate 
7850Sstevel@tonic-gate 	mutex_enter(&e_ddi_inst_state.ins_serial);
7860Sstevel@tonic-gate 	if (dp->ind_state == IN_PROVISIONAL) {
7870Sstevel@tonic-gate 		dp->ind_state = IN_PERMANENT;
7880Sstevel@tonic-gate 		i_log_devfs_instance_mod();
7890Sstevel@tonic-gate 		e_ddi_inst_state.ins_dirty = 1;
7900Sstevel@tonic-gate 	}
7910Sstevel@tonic-gate 	mutex_exit(&e_ddi_inst_state.ins_serial);
7920Sstevel@tonic-gate 	e_ddi_exit_instance();
7930Sstevel@tonic-gate }
7940Sstevel@tonic-gate 
7950Sstevel@tonic-gate /*
7960Sstevel@tonic-gate  * A new major has been added to the system.  Run through the orphan list
7970Sstevel@tonic-gate  * and try to attach each one to a driver's list.
7980Sstevel@tonic-gate  */
7990Sstevel@tonic-gate void
8000Sstevel@tonic-gate e_ddi_unorphan_instance_nos()
8010Sstevel@tonic-gate {
8020Sstevel@tonic-gate 	in_drv_t *dp, *ndp;
8030Sstevel@tonic-gate 
8040Sstevel@tonic-gate 	/*
8050Sstevel@tonic-gate 	 * disconnect the orphan list, and call in_hashdrv for each item
8060Sstevel@tonic-gate 	 * on it
8070Sstevel@tonic-gate 	 */
8080Sstevel@tonic-gate 
8090Sstevel@tonic-gate 	/*
8100Sstevel@tonic-gate 	 * Only one thread is allowed to change the state of the instance
8110Sstevel@tonic-gate 	 * number assignments on the system at any given time.
8120Sstevel@tonic-gate 	 */
8130Sstevel@tonic-gate 	e_ddi_enter_instance();
8140Sstevel@tonic-gate 	if (e_ddi_inst_state.ins_no_major == NULL) {
8150Sstevel@tonic-gate 		e_ddi_exit_instance();
8160Sstevel@tonic-gate 		return;
8170Sstevel@tonic-gate 	}
8180Sstevel@tonic-gate 	/*
8190Sstevel@tonic-gate 	 * Hash instance list to devnames structure of major.
8200Sstevel@tonic-gate 	 * Note that if there is not a valid major number for the
8210Sstevel@tonic-gate 	 * node, in_hashdrv will put it back on the no_major list.
8220Sstevel@tonic-gate 	 */
8230Sstevel@tonic-gate 	dp = e_ddi_inst_state.ins_no_major;
8240Sstevel@tonic-gate 	e_ddi_inst_state.ins_no_major = NULL;
8250Sstevel@tonic-gate 	while (dp) {
8260Sstevel@tonic-gate 		ndp = dp->ind_next;
8270Sstevel@tonic-gate 		ASSERT(dp->ind_state != IN_UNKNOWN);
8280Sstevel@tonic-gate 		dp->ind_next = NULL;
8290Sstevel@tonic-gate 		in_hashdrv(dp);
8300Sstevel@tonic-gate 		dp = ndp;
8310Sstevel@tonic-gate 	}
8320Sstevel@tonic-gate 	e_ddi_exit_instance();
8330Sstevel@tonic-gate }
8340Sstevel@tonic-gate 
8350Sstevel@tonic-gate static void
8360Sstevel@tonic-gate in_removenode(struct devnames *dnp, in_node_t *mp, in_node_t *ap)
8370Sstevel@tonic-gate {
8380Sstevel@tonic-gate 	in_node_t *np;
8390Sstevel@tonic-gate 
8400Sstevel@tonic-gate 	ASSERT(e_ddi_inst_state.ins_busy);
8410Sstevel@tonic-gate 	/*
8420Sstevel@tonic-gate 	 * Assertion: parents are always instantiated by the framework
8430Sstevel@tonic-gate 	 * before their children, destroyed after them
8440Sstevel@tonic-gate 	 */
8450Sstevel@tonic-gate 	ASSERT(mp->in_child == NULL);
8460Sstevel@tonic-gate 	/*
8470Sstevel@tonic-gate 	 * Assertion: drv entries are always removed before their owning nodes
8480Sstevel@tonic-gate 	 */
8490Sstevel@tonic-gate 	ASSERT(mp->in_drivers == NULL);
8500Sstevel@tonic-gate 	/*
8510Sstevel@tonic-gate 	 * Take the node out of the tree
8520Sstevel@tonic-gate 	 */
8530Sstevel@tonic-gate 	if (ap->in_child == mp) {
8540Sstevel@tonic-gate 		ap->in_child = mp->in_sibling;
8550Sstevel@tonic-gate 		in_dealloc_node(mp);
8560Sstevel@tonic-gate 		return;
8570Sstevel@tonic-gate 	} else {
8580Sstevel@tonic-gate 		for (np = ap->in_child; np; np = np->in_sibling) {
8590Sstevel@tonic-gate 			if (np->in_sibling == mp) {
8600Sstevel@tonic-gate 				np->in_sibling = mp->in_sibling;
8610Sstevel@tonic-gate 				in_dealloc_node(mp);
8620Sstevel@tonic-gate 				return;
8630Sstevel@tonic-gate 			}
8640Sstevel@tonic-gate 		}
8650Sstevel@tonic-gate 	}
8660Sstevel@tonic-gate 	panic("in_removenode dnp %p mp %p", (void *)dnp, (void *)mp);
8670Sstevel@tonic-gate }
8680Sstevel@tonic-gate 
8690Sstevel@tonic-gate /*
8700Sstevel@tonic-gate  * Recursive ascent
8710Sstevel@tonic-gate  *
8720Sstevel@tonic-gate  * This now only does half the job.  It finds the node, then the caller
8730Sstevel@tonic-gate  * has to search the node for the binding name
8740Sstevel@tonic-gate  */
8750Sstevel@tonic-gate static in_node_t *
8760Sstevel@tonic-gate in_devwalk(dev_info_t *dip, in_node_t **ap, char *addr)
8770Sstevel@tonic-gate {
8780Sstevel@tonic-gate 	in_node_t *np;
8790Sstevel@tonic-gate 	char *name;
8800Sstevel@tonic-gate 
8810Sstevel@tonic-gate 	ASSERT(dip);
8820Sstevel@tonic-gate 	ASSERT(e_ddi_inst_state.ins_busy);
8830Sstevel@tonic-gate 	if (dip == ddi_root_node()) {
8840Sstevel@tonic-gate 		*ap = NULL;
8850Sstevel@tonic-gate 		return (e_ddi_inst_state.ins_root);
8860Sstevel@tonic-gate 	}
8870Sstevel@tonic-gate 	/*
8880Sstevel@tonic-gate 	 * call up to find parent, then look through the list of kids
8890Sstevel@tonic-gate 	 * for a match
8900Sstevel@tonic-gate 	 */
8910Sstevel@tonic-gate 	np = in_devwalk(ddi_get_parent(dip), ap, NULL);
8920Sstevel@tonic-gate 	if (np == NULL)
8930Sstevel@tonic-gate 		return (np);
8940Sstevel@tonic-gate 	*ap = np;
8950Sstevel@tonic-gate 	np = np->in_child;
8960Sstevel@tonic-gate 	name = ddi_node_name(dip);
8970Sstevel@tonic-gate 	if (addr == NULL)
8980Sstevel@tonic-gate 		addr = ddi_get_name_addr(dip);
8990Sstevel@tonic-gate 
9000Sstevel@tonic-gate 	while (np) {
9010Sstevel@tonic-gate 		if (in_eqstr(np->in_node_name, name) &&
9020Sstevel@tonic-gate 		    in_eqstr(np->in_unit_addr, addr)) {
9030Sstevel@tonic-gate 			return (np);
9040Sstevel@tonic-gate 		}
9050Sstevel@tonic-gate 		np = np->in_sibling;
9060Sstevel@tonic-gate 	}
9070Sstevel@tonic-gate 	return (np);
9080Sstevel@tonic-gate }
9090Sstevel@tonic-gate 
9100Sstevel@tonic-gate /*
9110Sstevel@tonic-gate  * Create a node specified by cp and assign it the given instance no.
9120Sstevel@tonic-gate  */
9130Sstevel@tonic-gate static int
9140Sstevel@tonic-gate in_pathin(char *cp, int instance, char *bname, struct bind **args)
9150Sstevel@tonic-gate {
9160Sstevel@tonic-gate 	in_node_t *np;
9170Sstevel@tonic-gate 	in_drv_t *dp;
9180Sstevel@tonic-gate 	char *name;
9190Sstevel@tonic-gate 
9200Sstevel@tonic-gate 	ASSERT(e_ddi_inst_state.ins_busy);
9210Sstevel@tonic-gate 	ASSERT(args == NULL);
9220Sstevel@tonic-gate 
9230Sstevel@tonic-gate 	/*
9240Sstevel@tonic-gate 	 * Give a warning to the console.
9250Sstevel@tonic-gate 	 * return value ignored
9260Sstevel@tonic-gate 	 */
9270Sstevel@tonic-gate 	if (cp[0] != '/' || instance == -1 || bname == NULL) {
9280Sstevel@tonic-gate 		cmn_err(CE_WARN,
9290Sstevel@tonic-gate 		    "invalid instance file entry %s %d",
9300Sstevel@tonic-gate 		    cp, instance);
9310Sstevel@tonic-gate 
9320Sstevel@tonic-gate 		return (0);
9330Sstevel@tonic-gate 	}
9340Sstevel@tonic-gate 
9350Sstevel@tonic-gate 	if ((name  = i_binding_to_drv_name(bname)) != NULL)
9360Sstevel@tonic-gate 		bname = name;
9370Sstevel@tonic-gate 
9380Sstevel@tonic-gate 	np = in_make_path(cp);
9390Sstevel@tonic-gate 	ASSERT(np);
9400Sstevel@tonic-gate 	if (in_inuse(instance, bname)) {
9410Sstevel@tonic-gate 		cmn_err(CE_WARN,
9420Sstevel@tonic-gate 		    "instance already in use: %s %d", cp, instance);
9430Sstevel@tonic-gate 		return (0);
9440Sstevel@tonic-gate 	}
9450Sstevel@tonic-gate 	dp = in_drvwalk(np, bname);
9460Sstevel@tonic-gate 	if (dp != NULL) {
9470Sstevel@tonic-gate 		cmn_err(CE_WARN,
9480Sstevel@tonic-gate 		    "multiple instance number assignments for "
9490Sstevel@tonic-gate 		    "'%s' (driver %s), %d used",
9500Sstevel@tonic-gate 		    cp, bname, dp->ind_instance);
9510Sstevel@tonic-gate 		return (0);
9520Sstevel@tonic-gate 	}
9530Sstevel@tonic-gate 	dp = in_alloc_drv(bname);
9540Sstevel@tonic-gate 	in_endrv(np, dp);
9550Sstevel@tonic-gate 	dp->ind_instance = instance;
9560Sstevel@tonic-gate 	dp->ind_state = IN_PERMANENT;
9570Sstevel@tonic-gate 	in_hashdrv(dp);
9580Sstevel@tonic-gate 
9590Sstevel@tonic-gate 	return (0);
9600Sstevel@tonic-gate }
9610Sstevel@tonic-gate 
9620Sstevel@tonic-gate /*
9630Sstevel@tonic-gate  * Create (or find) the node named by path by recursively descending from the
9640Sstevel@tonic-gate  * root's first child (we ignore the root, which is never named)
9650Sstevel@tonic-gate  */
9660Sstevel@tonic-gate static in_node_t *
9670Sstevel@tonic-gate in_make_path(char *path)
9680Sstevel@tonic-gate {
9690Sstevel@tonic-gate 	in_node_t *ap;		/* ancestor pointer */
9700Sstevel@tonic-gate 	in_node_t *np;		/* working node pointer */
9710Sstevel@tonic-gate 	in_node_t *rp;		/* return node pointer */
9720Sstevel@tonic-gate 	char buf[MAXPATHLEN];	/* copy of string so we can change it */
9730Sstevel@tonic-gate 	char *cp, *name, *addr;
9740Sstevel@tonic-gate 
9750Sstevel@tonic-gate 	ASSERT(e_ddi_inst_state.ins_busy);
9760Sstevel@tonic-gate 	if (path == NULL || path[0] != '/')
9770Sstevel@tonic-gate 		return (NULL);
9780Sstevel@tonic-gate 	(void) snprintf(buf, sizeof (buf), "%s", path);
9790Sstevel@tonic-gate 	cp = buf + 1;	/* skip over initial '/' in path */
9800Sstevel@tonic-gate 	name = in_name_addr(&cp, &addr);
9810Sstevel@tonic-gate 
9820Sstevel@tonic-gate 	/*
9830Sstevel@tonic-gate 	 * In S9 and earlier releases, the path_to_inst file
9840Sstevel@tonic-gate 	 * SunCluster was prepended with "/node@#". This was
9850Sstevel@tonic-gate 	 * removed in S10. We skip the prefix if the prefix
9860Sstevel@tonic-gate 	 * still exists in /etc/path_to_inst. It is needed for
9870Sstevel@tonic-gate 	 * various forms of Solaris upgrade to work properly
9880Sstevel@tonic-gate 	 * in the SunCluster environment.
9890Sstevel@tonic-gate 	 */
9900Sstevel@tonic-gate 	if ((cluster_bootflags & CLUSTER_CONFIGURED) &&
9910Sstevel@tonic-gate 	    (strcmp(name, "node") == 0))
9920Sstevel@tonic-gate 		name = in_name_addr(&cp, &addr);
9930Sstevel@tonic-gate 
9940Sstevel@tonic-gate 	ap = e_ddi_inst_state.ins_root;
9950Sstevel@tonic-gate 	rp = np = e_ddi_inst_state.ins_root->in_child;
9960Sstevel@tonic-gate 	while (name) {
9970Sstevel@tonic-gate 		while (name && np) {
9980Sstevel@tonic-gate 			if (in_eqstr(name, np->in_node_name) &&
9990Sstevel@tonic-gate 			    in_eqstr(addr, np->in_unit_addr)) {
10000Sstevel@tonic-gate 				name = in_name_addr(&cp, &addr);
10010Sstevel@tonic-gate 				if (name == NULL)
10020Sstevel@tonic-gate 					return (np);
10030Sstevel@tonic-gate 				ap = np;
10040Sstevel@tonic-gate 				np = np->in_child;
10050Sstevel@tonic-gate 				continue;
10060Sstevel@tonic-gate 			} else {
10070Sstevel@tonic-gate 				np = np->in_sibling;
10080Sstevel@tonic-gate 			}
10090Sstevel@tonic-gate 		}
10100Sstevel@tonic-gate 		np = in_alloc_node(name, addr);
10110Sstevel@tonic-gate 		in_enlist(ap, np);	/* insert into tree */
10120Sstevel@tonic-gate 		rp = np;	/* value to return if we quit */
10130Sstevel@tonic-gate 		ap = np;	/* new parent */
10140Sstevel@tonic-gate 		np = NULL;	/* can have no children */
10150Sstevel@tonic-gate 		name = in_name_addr(&cp, &addr);
10160Sstevel@tonic-gate 	}
10170Sstevel@tonic-gate 	return (rp);
10180Sstevel@tonic-gate }
10190Sstevel@tonic-gate 
10200Sstevel@tonic-gate /*
10210Sstevel@tonic-gate  * Insert node np into the tree as one of ap's children.
10220Sstevel@tonic-gate  */
10230Sstevel@tonic-gate static void
10240Sstevel@tonic-gate in_enlist(in_node_t *ap, in_node_t *np)
10250Sstevel@tonic-gate {
10260Sstevel@tonic-gate 	in_node_t *mp;
10270Sstevel@tonic-gate 	ASSERT(e_ddi_inst_state.ins_busy);
10280Sstevel@tonic-gate 	/*
10290Sstevel@tonic-gate 	 * Make this node some other node's child or child's sibling
10300Sstevel@tonic-gate 	 */
10310Sstevel@tonic-gate 	ASSERT(ap && np);
10320Sstevel@tonic-gate 	if (ap->in_child == NULL) {
10330Sstevel@tonic-gate 		ap->in_child = np;
10340Sstevel@tonic-gate 	} else {
10350Sstevel@tonic-gate 		for (mp = ap->in_child; mp; mp = mp->in_sibling)
10360Sstevel@tonic-gate 			if (mp->in_sibling == NULL) {
10370Sstevel@tonic-gate 				mp->in_sibling = np;
10380Sstevel@tonic-gate 				break;
10390Sstevel@tonic-gate 			}
10400Sstevel@tonic-gate 	}
10410Sstevel@tonic-gate 	np->in_parent = ap;
10420Sstevel@tonic-gate }
10430Sstevel@tonic-gate 
10440Sstevel@tonic-gate /*
10450Sstevel@tonic-gate  * Insert drv entry dp onto a node's driver list
10460Sstevel@tonic-gate  */
10470Sstevel@tonic-gate static void
10480Sstevel@tonic-gate in_endrv(in_node_t *np, in_drv_t *dp)
10490Sstevel@tonic-gate {
10500Sstevel@tonic-gate 	in_drv_t *mp;
10510Sstevel@tonic-gate 	ASSERT(e_ddi_inst_state.ins_busy);
10520Sstevel@tonic-gate 	ASSERT(np && dp);
10530Sstevel@tonic-gate 	mp = np->in_drivers;
10540Sstevel@tonic-gate 	np->in_drivers = dp;
10550Sstevel@tonic-gate 	dp->ind_next_drv = mp;
10560Sstevel@tonic-gate 	dp->ind_node = np;
10570Sstevel@tonic-gate }
10580Sstevel@tonic-gate 
10590Sstevel@tonic-gate /*
10600Sstevel@tonic-gate  * Parse the next name out of the path, null terminate it and update cp.
10610Sstevel@tonic-gate  * caller has copied string so we can mess with it.
10620Sstevel@tonic-gate  * Upon return *cpp points to the next section to be parsed, *addrp points
10630Sstevel@tonic-gate  * to the current address substring (or NULL if none) and we return the
10640Sstevel@tonic-gate  * current name substring (or NULL if none).  name and address substrings
10650Sstevel@tonic-gate  * are null terminated in place.
10660Sstevel@tonic-gate  */
10670Sstevel@tonic-gate 
10680Sstevel@tonic-gate static char *
10690Sstevel@tonic-gate in_name_addr(char **cpp, char **addrp)
10700Sstevel@tonic-gate {
10710Sstevel@tonic-gate 	char *namep;	/* return value holder */
10720Sstevel@tonic-gate 	char *ap;	/* pointer to '@' in string */
10730Sstevel@tonic-gate 	char *sp;	/* pointer to '/' in string */
10740Sstevel@tonic-gate 
10750Sstevel@tonic-gate 	if (*cpp == NULL || **cpp == '\0') {
10760Sstevel@tonic-gate 		*addrp = NULL;
10770Sstevel@tonic-gate 		return (NULL);
10780Sstevel@tonic-gate 	}
10790Sstevel@tonic-gate 	namep = *cpp;
10800Sstevel@tonic-gate 	sp = strchr(*cpp, '/');
10810Sstevel@tonic-gate 	if (sp != NULL) {	/* more to follow */
10820Sstevel@tonic-gate 		*sp = '\0';
10830Sstevel@tonic-gate 		*cpp = sp + 1;
10840Sstevel@tonic-gate 	} else {		/* this is last component. */
10850Sstevel@tonic-gate 		*cpp = NULL;
10860Sstevel@tonic-gate 	}
10870Sstevel@tonic-gate 	ap = strchr(namep, '@');
10880Sstevel@tonic-gate 	if (ap == NULL) {
10890Sstevel@tonic-gate 		*addrp = NULL;
10900Sstevel@tonic-gate 	} else {
10910Sstevel@tonic-gate 		*ap = '\0';		/* terminate the name */
10920Sstevel@tonic-gate 		*addrp = ap + 1;
10930Sstevel@tonic-gate 	}
10940Sstevel@tonic-gate 	return (namep);
10950Sstevel@tonic-gate }
10960Sstevel@tonic-gate 
10970Sstevel@tonic-gate /*
10980Sstevel@tonic-gate  * Allocate a node and storage for name and addr strings, and fill them in.
10990Sstevel@tonic-gate  */
11000Sstevel@tonic-gate static in_node_t *
11010Sstevel@tonic-gate in_alloc_node(char *name, char *addr)
11020Sstevel@tonic-gate {
11030Sstevel@tonic-gate 	in_node_t *np;
11040Sstevel@tonic-gate 	char *cp;
11050Sstevel@tonic-gate 	size_t namelen;
11060Sstevel@tonic-gate 
11070Sstevel@tonic-gate 	ASSERT(e_ddi_inst_state.ins_busy);
11080Sstevel@tonic-gate 	/*
11090Sstevel@tonic-gate 	 * Has name or will become root
11100Sstevel@tonic-gate 	 */
11110Sstevel@tonic-gate 	ASSERT(name || e_ddi_inst_state.ins_root == NULL);
11120Sstevel@tonic-gate 	if (addr == NULL)
11130Sstevel@tonic-gate 		addr = "";
11140Sstevel@tonic-gate 	if (name == NULL)
11150Sstevel@tonic-gate 		namelen = 0;
11160Sstevel@tonic-gate 	else
11170Sstevel@tonic-gate 		namelen = strlen(name) + 1;
11180Sstevel@tonic-gate 	cp = kmem_zalloc(sizeof (in_node_t) + namelen + strlen(addr) + 1,
11190Sstevel@tonic-gate 	    KM_SLEEP);
11200Sstevel@tonic-gate 	np = (in_node_t *)cp;
11210Sstevel@tonic-gate 	if (name) {
11220Sstevel@tonic-gate 		np->in_node_name = cp + sizeof (in_node_t);
11230Sstevel@tonic-gate 		(void) strcpy(np->in_node_name, name);
11240Sstevel@tonic-gate 	}
11250Sstevel@tonic-gate 	np->in_unit_addr = cp + sizeof (in_node_t) + namelen;
11260Sstevel@tonic-gate 	(void) strcpy(np->in_unit_addr, addr);
11270Sstevel@tonic-gate 	return (np);
11280Sstevel@tonic-gate }
11290Sstevel@tonic-gate 
11300Sstevel@tonic-gate /*
11310Sstevel@tonic-gate  * Allocate a drv entry and storage for binding name string, and fill it in.
11320Sstevel@tonic-gate  */
11330Sstevel@tonic-gate static in_drv_t *
11340Sstevel@tonic-gate in_alloc_drv(char *bindingname)
11350Sstevel@tonic-gate {
11360Sstevel@tonic-gate 	in_drv_t *dp;
11370Sstevel@tonic-gate 	char *cp;
11380Sstevel@tonic-gate 	size_t namelen;
11390Sstevel@tonic-gate 
11400Sstevel@tonic-gate 	ASSERT(e_ddi_inst_state.ins_busy);
11410Sstevel@tonic-gate 	/*
11420Sstevel@tonic-gate 	 * Has name or will become root
11430Sstevel@tonic-gate 	 */
11440Sstevel@tonic-gate 	ASSERT(bindingname || e_ddi_inst_state.ins_root == NULL);
11450Sstevel@tonic-gate 	if (bindingname == NULL)
11460Sstevel@tonic-gate 		namelen = 0;
11470Sstevel@tonic-gate 	else
11480Sstevel@tonic-gate 		namelen = strlen(bindingname) + 1;
11490Sstevel@tonic-gate 	cp = kmem_zalloc(sizeof (in_drv_t) + namelen, KM_SLEEP);
11500Sstevel@tonic-gate 	dp = (in_drv_t *)cp;
11510Sstevel@tonic-gate 	if (bindingname) {
11520Sstevel@tonic-gate 		dp->ind_driver_name = cp + sizeof (in_drv_t);
11530Sstevel@tonic-gate 		(void) strcpy(dp->ind_driver_name, bindingname);
11540Sstevel@tonic-gate 	}
11550Sstevel@tonic-gate 	dp->ind_state = IN_UNKNOWN;
11560Sstevel@tonic-gate 	dp->ind_instance = -1;
11570Sstevel@tonic-gate 	return (dp);
11580Sstevel@tonic-gate }
11590Sstevel@tonic-gate 
11600Sstevel@tonic-gate static void
11610Sstevel@tonic-gate in_dealloc_node(in_node_t *np)
11620Sstevel@tonic-gate {
11630Sstevel@tonic-gate 	/*
11640Sstevel@tonic-gate 	 * The root node can never be de-allocated
11650Sstevel@tonic-gate 	 */
11660Sstevel@tonic-gate 	ASSERT(np->in_node_name && np->in_unit_addr);
11670Sstevel@tonic-gate 	ASSERT(e_ddi_inst_state.ins_busy);
11680Sstevel@tonic-gate 	kmem_free(np, sizeof (in_node_t) + strlen(np->in_node_name)
11690Sstevel@tonic-gate 	    + strlen(np->in_unit_addr) + 2);
11700Sstevel@tonic-gate }
11710Sstevel@tonic-gate 
11720Sstevel@tonic-gate static void
11730Sstevel@tonic-gate in_dealloc_drv(in_drv_t *dp)
11740Sstevel@tonic-gate {
11750Sstevel@tonic-gate 	ASSERT(dp->ind_driver_name);
11760Sstevel@tonic-gate 	ASSERT(e_ddi_inst_state.ins_busy);
11770Sstevel@tonic-gate 	kmem_free(dp, sizeof (in_drv_t) + strlen(dp->ind_driver_name)
11780Sstevel@tonic-gate 	    + 1);
11790Sstevel@tonic-gate }
11800Sstevel@tonic-gate 
11810Sstevel@tonic-gate /*
11820Sstevel@tonic-gate  * Handle the various possible versions of "no address"
11830Sstevel@tonic-gate  */
11840Sstevel@tonic-gate static int
11850Sstevel@tonic-gate in_eqstr(char *a, char *b)
11860Sstevel@tonic-gate {
11870Sstevel@tonic-gate 	if (a == b)	/* covers case where both are nulls */
11880Sstevel@tonic-gate 		return (1);
11890Sstevel@tonic-gate 	if (a == NULL && *b == 0)
11900Sstevel@tonic-gate 		return (1);
11910Sstevel@tonic-gate 	if (b == NULL && *a == 0)
11920Sstevel@tonic-gate 		return (1);
11930Sstevel@tonic-gate 	if (a == NULL || b == NULL)
11940Sstevel@tonic-gate 		return (0);
11950Sstevel@tonic-gate 	return (strcmp(a, b) == 0);
11960Sstevel@tonic-gate }
11970Sstevel@tonic-gate 
11980Sstevel@tonic-gate /*
11990Sstevel@tonic-gate  * Returns true if instance no. is already in use by named driver
12000Sstevel@tonic-gate  */
12010Sstevel@tonic-gate static int
12020Sstevel@tonic-gate in_inuse(int instance, char *name)
12030Sstevel@tonic-gate {
12040Sstevel@tonic-gate 	major_t major;
12050Sstevel@tonic-gate 	in_drv_t *dp;
12060Sstevel@tonic-gate 	struct devnames *dnp;
12070Sstevel@tonic-gate 
12080Sstevel@tonic-gate 	ASSERT(e_ddi_inst_state.ins_busy);
12090Sstevel@tonic-gate 	/*
12100Sstevel@tonic-gate 	 * For now, if we've never heard of this device we assume it is not
12110Sstevel@tonic-gate 	 * in use, since we can't tell
12120Sstevel@tonic-gate 	 * XXX could do the weaker search through the nomajor list checking
12130Sstevel@tonic-gate 	 * XXX for the same name
12140Sstevel@tonic-gate 	 */
1215*7009Scth 	if ((major = ddi_name_to_major(name)) == DDI_MAJOR_T_NONE)
12160Sstevel@tonic-gate 		return (0);
12170Sstevel@tonic-gate 	dnp = &devnamesp[major];
12180Sstevel@tonic-gate 
12190Sstevel@tonic-gate 	dp = dnp->dn_inlist;
12200Sstevel@tonic-gate 	while (dp) {
12210Sstevel@tonic-gate 		if (dp->ind_instance == instance)
12220Sstevel@tonic-gate 			return (1);
12230Sstevel@tonic-gate 		dp = dp->ind_next;
12240Sstevel@tonic-gate 	}
12250Sstevel@tonic-gate 	return (0);
12260Sstevel@tonic-gate }
12270Sstevel@tonic-gate 
12280Sstevel@tonic-gate static void
12290Sstevel@tonic-gate in_hashdrv(in_drv_t *dp)
12300Sstevel@tonic-gate {
12310Sstevel@tonic-gate 	struct devnames *dnp;
12320Sstevel@tonic-gate 	in_drv_t *mp, *pp;
12330Sstevel@tonic-gate 	major_t major;
12340Sstevel@tonic-gate 
12350Sstevel@tonic-gate 	/* hash to no major list */
1236*7009Scth 	major = ddi_name_to_major(dp->ind_driver_name);
1237*7009Scth 	if (major == DDI_MAJOR_T_NONE) {
12380Sstevel@tonic-gate 		dp->ind_next = e_ddi_inst_state.ins_no_major;
12390Sstevel@tonic-gate 		e_ddi_inst_state.ins_no_major = dp;
12400Sstevel@tonic-gate 		return;
12410Sstevel@tonic-gate 	}
12420Sstevel@tonic-gate 
12430Sstevel@tonic-gate 	/*
12440Sstevel@tonic-gate 	 * dnp->dn_inlist is sorted by instance number.
12450Sstevel@tonic-gate 	 * Adding a new instance entry may introduce holes,
12460Sstevel@tonic-gate 	 * set dn_instance to IN_SEARCHME so the next instance
12470Sstevel@tonic-gate 	 * assignment may fill in holes.
12480Sstevel@tonic-gate 	 */
12490Sstevel@tonic-gate 	dnp = &devnamesp[major];
12500Sstevel@tonic-gate 	pp = mp = dnp->dn_inlist;
12510Sstevel@tonic-gate 	if (mp == NULL || dp->ind_instance < mp->ind_instance) {
12520Sstevel@tonic-gate 		/* prepend as the first entry, turn on IN_SEARCHME */
12530Sstevel@tonic-gate 		dnp->dn_instance = IN_SEARCHME;
12540Sstevel@tonic-gate 		dp->ind_next = mp;
12550Sstevel@tonic-gate 		dnp->dn_inlist = dp;
12560Sstevel@tonic-gate 		return;
12570Sstevel@tonic-gate 	}
12580Sstevel@tonic-gate 
12590Sstevel@tonic-gate 	ASSERT(mp->ind_instance != dp->ind_instance);
12600Sstevel@tonic-gate 	while (mp->ind_instance < dp->ind_instance && mp->ind_next) {
12610Sstevel@tonic-gate 		pp = mp;
12620Sstevel@tonic-gate 		mp = mp->ind_next;
12630Sstevel@tonic-gate 		ASSERT(mp->ind_instance != dp->ind_instance);
12640Sstevel@tonic-gate 	}
12650Sstevel@tonic-gate 
12660Sstevel@tonic-gate 	if (mp->ind_instance < dp->ind_instance) { /* end of list */
12670Sstevel@tonic-gate 		dp->ind_next = NULL;
12680Sstevel@tonic-gate 		mp->ind_next = dp;
12690Sstevel@tonic-gate 	} else {
12700Sstevel@tonic-gate 		ASSERT(dnp->dn_instance == IN_SEARCHME);
12710Sstevel@tonic-gate 		dp->ind_next = pp->ind_next;
12720Sstevel@tonic-gate 		pp->ind_next = dp;
12730Sstevel@tonic-gate 	}
12740Sstevel@tonic-gate }
12750Sstevel@tonic-gate 
12760Sstevel@tonic-gate /*
12770Sstevel@tonic-gate  * Remove a driver entry from the list, given a previous pointer
12780Sstevel@tonic-gate  */
12790Sstevel@tonic-gate static void
12800Sstevel@tonic-gate in_removedrv(struct devnames *dnp, in_drv_t *mp)
12810Sstevel@tonic-gate {
12820Sstevel@tonic-gate 	in_drv_t *dp;
12830Sstevel@tonic-gate 	in_drv_t *prevp;
12840Sstevel@tonic-gate 
12850Sstevel@tonic-gate 	if (dnp->dn_inlist == mp) {	/* head of list */
12860Sstevel@tonic-gate 		dnp->dn_inlist = mp->ind_next;
12870Sstevel@tonic-gate 		dnp->dn_instance = IN_SEARCHME;
12880Sstevel@tonic-gate 		in_dq_drv(mp);
12890Sstevel@tonic-gate 		in_dealloc_drv(mp);
12900Sstevel@tonic-gate 		return;
12910Sstevel@tonic-gate 	}
12920Sstevel@tonic-gate 	prevp = dnp->dn_inlist;
12930Sstevel@tonic-gate 	for (dp = prevp->ind_next; dp; dp = dp->ind_next) {
12940Sstevel@tonic-gate 		if (dp == mp) {		/* found it */
12950Sstevel@tonic-gate 			break;
12960Sstevel@tonic-gate 		}
12970Sstevel@tonic-gate 		prevp = dp;
12980Sstevel@tonic-gate 	}
12990Sstevel@tonic-gate 
13000Sstevel@tonic-gate 	ASSERT(dp == mp);
13010Sstevel@tonic-gate 	dnp->dn_instance = IN_SEARCHME;
13020Sstevel@tonic-gate 	prevp->ind_next = mp->ind_next;
13030Sstevel@tonic-gate 	in_dq_drv(mp);
13040Sstevel@tonic-gate 	in_dealloc_drv(mp);
13050Sstevel@tonic-gate }
13060Sstevel@tonic-gate 
13070Sstevel@tonic-gate static void
13080Sstevel@tonic-gate in_dq_drv(in_drv_t *mp)
13090Sstevel@tonic-gate {
13100Sstevel@tonic-gate 	struct in_node *node = mp->ind_node;
13110Sstevel@tonic-gate 	in_drv_t *ptr, *prev;
13120Sstevel@tonic-gate 
13130Sstevel@tonic-gate 	if (mp == node->in_drivers) {
13140Sstevel@tonic-gate 		node->in_drivers = mp->ind_next_drv;
13150Sstevel@tonic-gate 		return;
13160Sstevel@tonic-gate 	}
13170Sstevel@tonic-gate 	prev = node->in_drivers;
13180Sstevel@tonic-gate 	for (ptr = prev->ind_next_drv; ptr != (struct in_drv *)NULL;
13190Sstevel@tonic-gate 	    ptr = ptr->ind_next_drv) {
13200Sstevel@tonic-gate 		if (ptr == mp) {
13210Sstevel@tonic-gate 			prev->ind_next_drv = ptr->ind_next_drv;
13220Sstevel@tonic-gate 			return;
13230Sstevel@tonic-gate 		}
13240Sstevel@tonic-gate 	}
13250Sstevel@tonic-gate 	panic("in_dq_drv: in_drv not found on node driver list");
13260Sstevel@tonic-gate }
13270Sstevel@tonic-gate 
13280Sstevel@tonic-gate 
13290Sstevel@tonic-gate in_drv_t *
13300Sstevel@tonic-gate in_drvwalk(in_node_t *np, char *binding_name)
13310Sstevel@tonic-gate {
13320Sstevel@tonic-gate 	char *name;
13330Sstevel@tonic-gate 	in_drv_t *dp = np->in_drivers;
13340Sstevel@tonic-gate 	while (dp) {
13350Sstevel@tonic-gate 		if ((name = i_binding_to_drv_name(dp->ind_driver_name))
13360Sstevel@tonic-gate 		    == NULL) {
13370Sstevel@tonic-gate 			name = dp->ind_driver_name;
13380Sstevel@tonic-gate 		}
13390Sstevel@tonic-gate 		if (strcmp(binding_name, name) == 0) {
13400Sstevel@tonic-gate 			break;
13410Sstevel@tonic-gate 		}
13420Sstevel@tonic-gate 		dp = dp->ind_next_drv;
13430Sstevel@tonic-gate 	}
13440Sstevel@tonic-gate 	return (dp);
13450Sstevel@tonic-gate }
13460Sstevel@tonic-gate 
13470Sstevel@tonic-gate 
13480Sstevel@tonic-gate 
13490Sstevel@tonic-gate static void
13500Sstevel@tonic-gate i_log_devfs_instance_mod(void)
13510Sstevel@tonic-gate {
13523250Scth 	sysevent_t	*ev;
13533250Scth 	sysevent_id_t	eid;
13543250Scth 	static int	sent_one = 0;
13550Sstevel@tonic-gate 
13560Sstevel@tonic-gate 	/*
13573250Scth 	 * Prevent unnecessary event generation.  Do not generate more than
13583250Scth 	 * one event during boot.
13590Sstevel@tonic-gate 	 */
13603250Scth 	if (sent_one && !i_ddi_io_initialized())
13610Sstevel@tonic-gate 		return;
13620Sstevel@tonic-gate 
13630Sstevel@tonic-gate 	ev = sysevent_alloc(EC_DEVFS, ESC_DEVFS_INSTANCE_MOD, EP_DDI,
13645648Ssetje 	    SE_NOSLEEP);
13650Sstevel@tonic-gate 	if (ev == NULL) {
13660Sstevel@tonic-gate 		return;
13670Sstevel@tonic-gate 	}
13680Sstevel@tonic-gate 	if (log_sysevent(ev, SE_NOSLEEP, &eid) != 0) {
13690Sstevel@tonic-gate 		cmn_err(CE_WARN, "i_log_devfs_instance_mod: failed to post "
13705648Ssetje 		    "event");
13713250Scth 	} else {
13723250Scth 		sent_one = 1;
13730Sstevel@tonic-gate 	}
13740Sstevel@tonic-gate 	sysevent_free(ev);
13750Sstevel@tonic-gate }
13760Sstevel@tonic-gate 
13770Sstevel@tonic-gate void
13780Sstevel@tonic-gate e_ddi_enter_instance()
13790Sstevel@tonic-gate {
13800Sstevel@tonic-gate 	mutex_enter(&e_ddi_inst_state.ins_serial);
13810Sstevel@tonic-gate 	if (e_ddi_inst_state.ins_thread == curthread)
13820Sstevel@tonic-gate 		e_ddi_inst_state.ins_busy++;
13830Sstevel@tonic-gate 	else {
13840Sstevel@tonic-gate 		while (e_ddi_inst_state.ins_busy)
13850Sstevel@tonic-gate 			cv_wait(&e_ddi_inst_state.ins_serial_cv,
13860Sstevel@tonic-gate 			    &e_ddi_inst_state.ins_serial);
13870Sstevel@tonic-gate 		e_ddi_inst_state.ins_thread = curthread;
13880Sstevel@tonic-gate 		e_ddi_inst_state.ins_busy = 1;
13890Sstevel@tonic-gate 	}
13900Sstevel@tonic-gate 	mutex_exit(&e_ddi_inst_state.ins_serial);
13910Sstevel@tonic-gate }
13920Sstevel@tonic-gate 
13930Sstevel@tonic-gate void
13940Sstevel@tonic-gate e_ddi_exit_instance()
13950Sstevel@tonic-gate {
13960Sstevel@tonic-gate 	mutex_enter(&e_ddi_inst_state.ins_serial);
13970Sstevel@tonic-gate 	e_ddi_inst_state.ins_busy--;
13980Sstevel@tonic-gate 	if (e_ddi_inst_state.ins_busy == 0) {
13990Sstevel@tonic-gate 		cv_broadcast(&e_ddi_inst_state.ins_serial_cv);
14000Sstevel@tonic-gate 		e_ddi_inst_state.ins_thread = NULL;
14010Sstevel@tonic-gate 	}
14020Sstevel@tonic-gate 	mutex_exit(&e_ddi_inst_state.ins_serial);
14030Sstevel@tonic-gate }
14040Sstevel@tonic-gate 
14050Sstevel@tonic-gate int
14060Sstevel@tonic-gate e_ddi_instance_is_clean()
14070Sstevel@tonic-gate {
14080Sstevel@tonic-gate 	return (e_ddi_inst_state.ins_dirty == 0);
14090Sstevel@tonic-gate }
14100Sstevel@tonic-gate 
14110Sstevel@tonic-gate void
14120Sstevel@tonic-gate e_ddi_instance_set_clean()
14130Sstevel@tonic-gate {
14140Sstevel@tonic-gate 	e_ddi_inst_state.ins_dirty = 0;
14150Sstevel@tonic-gate }
14160Sstevel@tonic-gate 
14170Sstevel@tonic-gate in_node_t *
14180Sstevel@tonic-gate e_ddi_instance_root()
14190Sstevel@tonic-gate {
14200Sstevel@tonic-gate 	return (e_ddi_inst_state.ins_root);
14210Sstevel@tonic-gate }
14220Sstevel@tonic-gate 
14230Sstevel@tonic-gate /*
14240Sstevel@tonic-gate  * Visit a node in the instance tree
14250Sstevel@tonic-gate  */
14260Sstevel@tonic-gate static int
14270Sstevel@tonic-gate in_walk_instances(in_node_t *np, char *path, char *this,
14280Sstevel@tonic-gate     int (*f)(const char *, in_node_t *, in_drv_t *, void *), void *arg)
14290Sstevel@tonic-gate {
14300Sstevel@tonic-gate 	in_drv_t *dp;
14310Sstevel@tonic-gate 	int rval = INST_WALK_CONTINUE;
14320Sstevel@tonic-gate 	char *next;
14330Sstevel@tonic-gate 
14340Sstevel@tonic-gate 	while (np != NULL) {
14350Sstevel@tonic-gate 
14360Sstevel@tonic-gate 		if (np->in_unit_addr[0] == 0)
14370Sstevel@tonic-gate 			(void) sprintf(this, "/%s", np->in_node_name);
14380Sstevel@tonic-gate 		else
14390Sstevel@tonic-gate 			(void) sprintf(this, "/%s@%s", np->in_node_name,
14400Sstevel@tonic-gate 			    np->in_unit_addr);
14410Sstevel@tonic-gate 		next = this + strlen(this);
14420Sstevel@tonic-gate 
14430Sstevel@tonic-gate 		for (dp = np->in_drivers; dp; dp = dp->ind_next_drv) {
14440Sstevel@tonic-gate 			if (dp->ind_state == IN_PERMANENT) {
14450Sstevel@tonic-gate 				rval = (*f)(path, np, dp, arg);
14460Sstevel@tonic-gate 				if (rval == INST_WALK_TERMINATE)
14470Sstevel@tonic-gate 					break;
14480Sstevel@tonic-gate 			}
14490Sstevel@tonic-gate 		}
14500Sstevel@tonic-gate 		if (np->in_child) {
14510Sstevel@tonic-gate 			rval = in_walk_instances(np->in_child,
14520Sstevel@tonic-gate 			    path, next, f, arg);
14530Sstevel@tonic-gate 			if (rval == INST_WALK_TERMINATE)
14540Sstevel@tonic-gate 				break;
14550Sstevel@tonic-gate 		}
14560Sstevel@tonic-gate 
14570Sstevel@tonic-gate 		np = np->in_sibling;
14580Sstevel@tonic-gate 	}
14590Sstevel@tonic-gate 
14600Sstevel@tonic-gate 	return (rval);
14610Sstevel@tonic-gate }
14620Sstevel@tonic-gate 
14630Sstevel@tonic-gate /*
14640Sstevel@tonic-gate  * A general interface for walking the instance tree,
14650Sstevel@tonic-gate  * calling a user-supplied callback for each node.
14660Sstevel@tonic-gate  */
14670Sstevel@tonic-gate int
14680Sstevel@tonic-gate e_ddi_walk_instances(int (*f)(const char *,
14690Sstevel@tonic-gate 	in_node_t *, in_drv_t *, void *), void *arg)
14700Sstevel@tonic-gate {
14710Sstevel@tonic-gate 	in_node_t *root;
14720Sstevel@tonic-gate 	int rval;
14730Sstevel@tonic-gate 	char *path;
14740Sstevel@tonic-gate 
14750Sstevel@tonic-gate 	path = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
14760Sstevel@tonic-gate 
14770Sstevel@tonic-gate 	e_ddi_enter_instance();
14780Sstevel@tonic-gate 	root = e_ddi_instance_root();
14790Sstevel@tonic-gate 	rval = in_walk_instances(root->in_child, path, path, f, arg);
14800Sstevel@tonic-gate 	e_ddi_exit_instance();
14810Sstevel@tonic-gate 
14820Sstevel@tonic-gate 	kmem_free(path, MAXPATHLEN);
14830Sstevel@tonic-gate 	return (rval);
14840Sstevel@tonic-gate }
1485