xref: /onnv-gate/usr/src/uts/common/os/instance.c (revision 13000:51b1767a74cb)
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 /*
2212116SVikram.Hegde@Sun.COM  * Copyright (c) 1992, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate  */
240Sstevel@tonic-gate 
250Sstevel@tonic-gate /*
260Sstevel@tonic-gate  * Instance number assignment code
270Sstevel@tonic-gate  */
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <sys/types.h>
300Sstevel@tonic-gate #include <sys/param.h>
310Sstevel@tonic-gate #include <sys/errno.h>
320Sstevel@tonic-gate #include <sys/systm.h>
330Sstevel@tonic-gate #include <sys/kobj.h>
340Sstevel@tonic-gate #include <sys/t_lock.h>
350Sstevel@tonic-gate #include <sys/kmem.h>
360Sstevel@tonic-gate #include <sys/cmn_err.h>
370Sstevel@tonic-gate #include <sys/ddi.h>
380Sstevel@tonic-gate #include <sys/sunddi.h>
390Sstevel@tonic-gate #include <sys/autoconf.h>
400Sstevel@tonic-gate #include <sys/systeminfo.h>
410Sstevel@tonic-gate #include <sys/hwconf.h>
420Sstevel@tonic-gate #include <sys/reboot.h>
430Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
440Sstevel@tonic-gate #include <sys/instance.h>
450Sstevel@tonic-gate #include <sys/debug.h>
460Sstevel@tonic-gate #include <sys/sysevent.h>
470Sstevel@tonic-gate #include <sys/modctl.h>
480Sstevel@tonic-gate #include <sys/console.h>
490Sstevel@tonic-gate #include <sys/cladm.h>
5012116SVikram.Hegde@Sun.COM #include <sys/sysmacros.h>
5112116SVikram.Hegde@Sun.COM #include <sys/crc32.h>
5212116SVikram.Hegde@Sun.COM 
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 
7612116SVikram.Hegde@Sun.COM #pragma weak plat_ioaliases_init
7712116SVikram.Hegde@Sun.COM 
7812116SVikram.Hegde@Sun.COM 
790Sstevel@tonic-gate /* external functions */
800Sstevel@tonic-gate extern char *i_binding_to_drv_name(char *bname);
8112116SVikram.Hegde@Sun.COM extern void plat_ioaliases_init(void);
820Sstevel@tonic-gate 
830Sstevel@tonic-gate /*
840Sstevel@tonic-gate  * This plus devnames defines the entire software state of the instance world.
850Sstevel@tonic-gate  */
860Sstevel@tonic-gate typedef struct in_softstate {
870Sstevel@tonic-gate 	in_node_t	*ins_root;	/* the root of our instance tree */
880Sstevel@tonic-gate 	in_drv_t	*ins_no_major;	/* majorless drv entries */
890Sstevel@tonic-gate 	/*
900Sstevel@tonic-gate 	 * Used to serialize access to data structures
910Sstevel@tonic-gate 	 */
920Sstevel@tonic-gate 	void		*ins_thread;
930Sstevel@tonic-gate 	kmutex_t	ins_serial;
940Sstevel@tonic-gate 	kcondvar_t	ins_serial_cv;
950Sstevel@tonic-gate 	int		ins_busy;
9612116SVikram.Hegde@Sun.COM 	boolean_t	ins_dirty;	/* instance info needs flush */
970Sstevel@tonic-gate } in_softstate_t;
980Sstevel@tonic-gate 
990Sstevel@tonic-gate static in_softstate_t e_ddi_inst_state;
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate /*
1020Sstevel@tonic-gate  * State transition information:
1030Sstevel@tonic-gate  * e_ddi_inst_state contains, among other things, the root of a tree of
1040Sstevel@tonic-gate  * device nodes used to track instance number assignments.
1050Sstevel@tonic-gate  * Each device node may contain multiple driver bindings, represented
1060Sstevel@tonic-gate  * by a linked list of in_drv_t nodes, each with an instance assignment
1070Sstevel@tonic-gate  * (except for root node). Each in_drv node can be in one of 3 states,
1080Sstevel@tonic-gate  * indicated by ind_state:
1090Sstevel@tonic-gate  *
1100Sstevel@tonic-gate  * IN_UNKNOWN:	Each node created in this state.  The instance number of
1110Sstevel@tonic-gate  *	this node is not known.  ind_instance is set to -1.
1120Sstevel@tonic-gate  * IN_PROVISIONAL:  When a node is assigned an instance number in
1130Sstevel@tonic-gate  *	e_ddi_assign_instance(), its state is set to IN_PROVISIONAL.
1140Sstevel@tonic-gate  *	Subsequently, the framework will always call either
11512116SVikram.Hegde@Sun.COM  *	e_ddi_keep_instance() which makes the node IN_PERMANENT
1160Sstevel@tonic-gate  *	or e_ddi_free_instance(), which deletes the node.
1170Sstevel@tonic-gate  * IN_PERMANENT:
1180Sstevel@tonic-gate  *	If e_ddi_keep_instance() is called on an IN_PROVISIONAL node,
1190Sstevel@tonic-gate  *	its state is set to IN_PERMANENT.
1200Sstevel@tonic-gate  */
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate static char *instance_file = INSTANCE_FILE;
1230Sstevel@tonic-gate static char *instance_file_backup = INSTANCE_FILE INSTANCE_FILE_SUFFIX;
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate /*
1260Sstevel@tonic-gate  * Return values for in_get_infile().
1270Sstevel@tonic-gate  */
1280Sstevel@tonic-gate #define	PTI_FOUND	0
1290Sstevel@tonic-gate #define	PTI_NOT_FOUND	1
1300Sstevel@tonic-gate #define	PTI_REBUILD	2
1310Sstevel@tonic-gate 
13212288SChris.Horne@Sun.COM int	instance_searchme = 0;	/* testing: use complex code path */
13312116SVikram.Hegde@Sun.COM 
1340Sstevel@tonic-gate /*
1350Sstevel@tonic-gate  * Path to instance file magic string used for first time boot after
1360Sstevel@tonic-gate  * an install.  If this is the first string in the file we will
1370Sstevel@tonic-gate  * automatically rebuild the file.
1380Sstevel@tonic-gate  */
1390Sstevel@tonic-gate #define	PTI_MAGIC_STR		"#path_to_inst_bootstrap_1"
1400Sstevel@tonic-gate #define	PTI_MAGIC_STR_LEN	(sizeof (PTI_MAGIC_STR) - 1)
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate void
e_ddi_instance_init(void)1430Sstevel@tonic-gate e_ddi_instance_init(void)
1440Sstevel@tonic-gate {
1450Sstevel@tonic-gate 	char *file;
1460Sstevel@tonic-gate 	int rebuild = 1;
1470Sstevel@tonic-gate 	struct in_drv *dp;
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate 	mutex_init(&e_ddi_inst_state.ins_serial, NULL, MUTEX_DEFAULT, NULL);
1500Sstevel@tonic-gate 	cv_init(&e_ddi_inst_state.ins_serial_cv, NULL, CV_DEFAULT, NULL);
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 	/*
1530Sstevel@tonic-gate 	 * Only one thread is allowed to change the state of the instance
1540Sstevel@tonic-gate 	 * number assignments on the system at any given time.
1550Sstevel@tonic-gate 	 * Note that this is not really necessary, as we are single-threaded
1560Sstevel@tonic-gate 	 * here, but it won't hurt, and it allows us to keep ASSERTS for
1570Sstevel@tonic-gate 	 * our assumptions in the code.
1580Sstevel@tonic-gate 	 */
1590Sstevel@tonic-gate 	e_ddi_enter_instance();
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 	/*
16212116SVikram.Hegde@Sun.COM 	 * Init the ioaliases if the platform supports it
16312116SVikram.Hegde@Sun.COM 	 */
16412116SVikram.Hegde@Sun.COM 	if (&plat_ioaliases_init)
16512116SVikram.Hegde@Sun.COM 		plat_ioaliases_init();
16612116SVikram.Hegde@Sun.COM 
16712116SVikram.Hegde@Sun.COM 	/*
1680Sstevel@tonic-gate 	 * Create the root node, instance zallocs to 0.
1690Sstevel@tonic-gate 	 * The name and address of this node never get examined, we always
1700Sstevel@tonic-gate 	 * start searching with its first child.
1710Sstevel@tonic-gate 	 */
1720Sstevel@tonic-gate 	ASSERT(e_ddi_inst_state.ins_root == NULL);
1730Sstevel@tonic-gate 	e_ddi_inst_state.ins_root = in_alloc_node(NULL, NULL);
1740Sstevel@tonic-gate 	dp = in_alloc_drv("rootnex");
1750Sstevel@tonic-gate 	in_endrv(e_ddi_inst_state.ins_root, dp);
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	file = instance_file;
1780Sstevel@tonic-gate 	switch (in_get_infile(file)) {
1790Sstevel@tonic-gate 	default:
1800Sstevel@tonic-gate 	case PTI_NOT_FOUND:
1810Sstevel@tonic-gate 		/* make sure path_to_inst is recreated */
1820Sstevel@tonic-gate 		boothowto |= RB_RECONFIG;
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate 		/*
1850Sstevel@tonic-gate 		 * Something is wrong. First try the backup file.
1860Sstevel@tonic-gate 		 * If not found, rebuild path_to_inst. Emit a
1870Sstevel@tonic-gate 		 * message about the problem.
1880Sstevel@tonic-gate 		 */
1890Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s empty or not found", file);
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 		file = instance_file_backup;
1920Sstevel@tonic-gate 		if (in_get_infile(file) != PTI_FOUND) {
1930Sstevel@tonic-gate 			cmn_err(CE_NOTE, "rebuilding device instance data");
1940Sstevel@tonic-gate 			break;
1950Sstevel@tonic-gate 		}
1960Sstevel@tonic-gate 		cmn_err(CE_NOTE, "using backup instance data in %s", file);
1970Sstevel@tonic-gate 		/*FALLTHROUGH*/
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate 	case PTI_FOUND:
2000Sstevel@tonic-gate 		/*
2010Sstevel@tonic-gate 		 * We've got a readable file
2020Sstevel@tonic-gate 		 * parse the file into the instance tree
2030Sstevel@tonic-gate 		 */
2040Sstevel@tonic-gate 		(void) read_binding_file(file, NULL, in_pathin);
2050Sstevel@tonic-gate 		rebuild = 0;
2060Sstevel@tonic-gate 		break;
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate 	case PTI_REBUILD:
20912116SVikram.Hegde@Sun.COM 		/*
21012116SVikram.Hegde@Sun.COM 		 * path_to_inst has magic str requesting a create
21112116SVikram.Hegde@Sun.COM 		 * Convert boot to reconfig boot to ensure /dev is
21212116SVikram.Hegde@Sun.COM 		 * in sync with new path_to_inst.
21312116SVikram.Hegde@Sun.COM 		 */
21412116SVikram.Hegde@Sun.COM 		boothowto |= RB_RECONFIG;
2150Sstevel@tonic-gate 		cmn_err(CE_CONT,
2165648Ssetje 		    "?Using default device instance data\n");
2170Sstevel@tonic-gate 		break;
2180Sstevel@tonic-gate 	}
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate 	/*
2210Sstevel@tonic-gate 	 * The OBP device tree has been copied to the kernel and
2220Sstevel@tonic-gate 	 * bound to drivers at this point. We walk the per-driver
2230Sstevel@tonic-gate 	 * list to preassign instances. Since the bus addr is
2240Sstevel@tonic-gate 	 * unknown at this point, we cannot place the instance
2250Sstevel@tonic-gate 	 * number in the instance tree. This will be done at
2260Sstevel@tonic-gate 	 * a later time.
2270Sstevel@tonic-gate 	 */
2280Sstevel@tonic-gate 	if (rebuild)
2290Sstevel@tonic-gate 		in_preassign_instance();
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate 	e_ddi_exit_instance();
2320Sstevel@tonic-gate }
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate static void
in_preassign_instance()2350Sstevel@tonic-gate in_preassign_instance()
2360Sstevel@tonic-gate {
23712288SChris.Horne@Sun.COM 	major_t		m;
23812288SChris.Horne@Sun.COM 	struct devnames	*dnp;
23912288SChris.Horne@Sun.COM 	dev_info_t	*dip;
24012288SChris.Horne@Sun.COM 	extern major_t	devcnt;
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 	for (m = 0; m < devcnt; m++) {
24312288SChris.Horne@Sun.COM 		dnp = &devnamesp[m];
24412288SChris.Horne@Sun.COM 		dip = dnp->dn_head;
2450Sstevel@tonic-gate 		while (dip) {
2460Sstevel@tonic-gate 			DEVI(dip)->devi_instance = dnp->dn_instance;
2470Sstevel@tonic-gate 			dnp->dn_instance++;
2480Sstevel@tonic-gate 			dip = ddi_get_next(dip);
2490Sstevel@tonic-gate 		}
25012288SChris.Horne@Sun.COM 
25112288SChris.Horne@Sun.COM 		/*
25212288SChris.Horne@Sun.COM 		 * The preassign instance numbers are not fully
25312288SChris.Horne@Sun.COM 		 * accounted for until e_ddi_assign_instance().
25412288SChris.Horne@Sun.COM 		 * We can't fully account for them now because we
25512288SChris.Horne@Sun.COM 		 * don't currently have a unit-address. Because of
25612288SChris.Horne@Sun.COM 		 * this, we need to remember the preassign boundary
25712288SChris.Horne@Sun.COM 		 * to avoid ordering issues related to
25812288SChris.Horne@Sun.COM 		 * e_ddi_assign_instance of a preassigned value .vs.
25912288SChris.Horne@Sun.COM 		 * re-assignment of the same value for a dynamic
26012288SChris.Horne@Sun.COM 		 * SID node created by bus_config.
26112288SChris.Horne@Sun.COM 		 */
26212288SChris.Horne@Sun.COM 		dnp->dn_pinstance = dnp->dn_instance;
26312442SChris.Horne@Sun.COM 		dnp->dn_instance = IN_SEARCHME;
2640Sstevel@tonic-gate 	}
2650Sstevel@tonic-gate }
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate /*
2680Sstevel@tonic-gate  * Checks to see if the /etc/path_to_inst file exists and whether or not
2690Sstevel@tonic-gate  * it has the magic string in it.
2700Sstevel@tonic-gate  *
2710Sstevel@tonic-gate  * Returns one of the following:
2720Sstevel@tonic-gate  *
2730Sstevel@tonic-gate  *	PTI_FOUND	- We have found the /etc/path_to_inst file
2740Sstevel@tonic-gate  *	PTI_REBUILD	- We have found the /etc/path_to_inst file and the
2750Sstevel@tonic-gate  *			  first line was PTI_MAGIC_STR.
2760Sstevel@tonic-gate  *	PTI_NOT_FOUND	- We did not find the /etc/path_to_inst file
2770Sstevel@tonic-gate  *
2780Sstevel@tonic-gate  */
2790Sstevel@tonic-gate static int
in_get_infile(char * filename)2800Sstevel@tonic-gate in_get_infile(char *filename)
2810Sstevel@tonic-gate {
2825648Ssetje 	struct _buf *file;
2830Sstevel@tonic-gate 	int return_val;
2840Sstevel@tonic-gate 	char buf[PTI_MAGIC_STR_LEN];
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate 	/*
2870Sstevel@tonic-gate 	 * Try to open the file.
2880Sstevel@tonic-gate 	 */
2895648Ssetje 	if ((file = kobj_open_file(filename)) == (struct _buf *)-1) {
2900Sstevel@tonic-gate 		return (PTI_NOT_FOUND);
2910Sstevel@tonic-gate 	}
2920Sstevel@tonic-gate 	return_val = PTI_FOUND;
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate 	/*
2950Sstevel@tonic-gate 	 * Read the first PTI_MAGIC_STR_LEN bytes from the file to see if
2960Sstevel@tonic-gate 	 * it contains the magic string.  If there aren't that many bytes
2970Sstevel@tonic-gate 	 * in the file, then assume file is correct and no magic string
2980Sstevel@tonic-gate 	 * and move on.
2990Sstevel@tonic-gate 	 */
3005648Ssetje 	switch (kobj_read_file(file, buf, PTI_MAGIC_STR_LEN, 0)) {
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate 	case PTI_MAGIC_STR_LEN:
3030Sstevel@tonic-gate 		/*
3040Sstevel@tonic-gate 		 * If the first PTI_MAGIC_STR_LEN bytes are the magic string
3050Sstevel@tonic-gate 		 * then return PTI_REBUILD.
3060Sstevel@tonic-gate 		 */
3070Sstevel@tonic-gate 		if (strncmp(PTI_MAGIC_STR, buf, PTI_MAGIC_STR_LEN) == 0)
3080Sstevel@tonic-gate 			return_val = PTI_REBUILD;
3090Sstevel@tonic-gate 		break;
3100Sstevel@tonic-gate 
3110Sstevel@tonic-gate 	case 0:
3120Sstevel@tonic-gate 		/*
3130Sstevel@tonic-gate 		 * If the file is zero bytes in length, then consider the
3140Sstevel@tonic-gate 		 * file to not be found
3150Sstevel@tonic-gate 		 */
3160Sstevel@tonic-gate 		return_val = PTI_NOT_FOUND;
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate 	default: /* Do nothing we have a good file */
3190Sstevel@tonic-gate 		break;
3200Sstevel@tonic-gate 	}
3210Sstevel@tonic-gate 
3225648Ssetje 	kobj_close_file(file);
3230Sstevel@tonic-gate 	return (return_val);
3240Sstevel@tonic-gate }
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate int
is_pseudo_device(dev_info_t * dip)3270Sstevel@tonic-gate is_pseudo_device(dev_info_t *dip)
3280Sstevel@tonic-gate {
3290Sstevel@tonic-gate 	dev_info_t	*pdip;
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate 	for (pdip = ddi_get_parent(dip); pdip && pdip != ddi_root_node();
3320Sstevel@tonic-gate 	    pdip = ddi_get_parent(pdip)) {
3330Sstevel@tonic-gate 		if (strcmp(ddi_get_name(pdip), DEVI_PSEUDO_NEXNAME) == 0)
3340Sstevel@tonic-gate 			return (1);
3350Sstevel@tonic-gate 	}
3360Sstevel@tonic-gate 	return (0);
3370Sstevel@tonic-gate }
3380Sstevel@tonic-gate 
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate static void
in_set_instance(dev_info_t * dip,in_drv_t * dp,major_t major)3410Sstevel@tonic-gate in_set_instance(dev_info_t *dip, in_drv_t *dp, major_t major)
3420Sstevel@tonic-gate {
3430Sstevel@tonic-gate 	/* use preassigned instance if available */
3440Sstevel@tonic-gate 	if (DEVI(dip)->devi_instance != -1)
3450Sstevel@tonic-gate 		dp->ind_instance = DEVI(dip)->devi_instance;
3460Sstevel@tonic-gate 	else
3470Sstevel@tonic-gate 		dp->ind_instance = in_next_instance(major);
3480Sstevel@tonic-gate }
3490Sstevel@tonic-gate 
3500Sstevel@tonic-gate /*
3513250Scth  * Return 1 if instance block was assigned for the path.
3523250Scth  *
3533250Scth  * For multi-port NIC cards, sequential instance assignment across all
3548011SChris.Horne@Sun.COM  * ports on a card is highly desirable since the ppa is typically the
3553250Scth  * same as the instance number, and the ppa is used in the NIC's public
3563250Scth  * /dev name. This sequential assignment typically occurs as a result
3573250Scth  * of in_preassign_instance() after initial install, or by
3583250Scth  * i_ndi_init_hw_children() for NIC ports that share a common parent.
3593250Scth  *
3603250Scth  * Some NIC cards however use multi-function bridge chips, and to
3613250Scth  * support sequential instance assignment accross all ports, without
3623250Scth  * disabling multi-threaded attach, we have a (currently) undocumented
3633250Scth  * hack to allocate instance numbers in contiguous blocks based on
3643250Scth  * driver.conf properties.
3653250Scth  *
3663250Scth  *                       ^
3673250Scth  *           /----------   ------------\
3683250Scth  *        pci@0                      pci@0,1	MULTI-FUNCTION BRIDGE CHIP
3693250Scth  *       /     \                    /       \
3703250Scth  * FJSV,e4ta@4  FJSV,e4ta@4,1   FJSV,e4ta@6 FJSV,e4ta@6,1	NIC PORTS
3713250Scth  *      n            n+2             n+2         n+3		INSTANCE
3723250Scth  *
3733250Scth  * For the above example, the following driver.conf properties would be
3743250Scth  * used to guarantee sequential instance number assignment.
3753250Scth  *
3763250Scth  * ddi-instance-blocks ="ib-FJSVe4ca", "ib-FJSVe4ta", "ib-generic";
3773250Scth  * ib-FJSVe4ca =	"/pci@0/FJSV,e4ca@4", "/pci@0/FJSV,e4ca@4,1",
3783250Scth  *			"/pci@0,1/FJSV,e4ca@6", "/pci@0,1/FJSV,e4ca@6,1";
3793250Scth  * ib-FJSVe4ta =	"/pci@0/FJSV,e4ta@4", "/pci@0/FJSV,e4ta@4,1",
3803250Scth  *			"/pci@0,1/FJSV,e4ta@6", "/pci@0,1/FJSV,e4ta@6,1";
3813250Scth  * ib-generic =		"/pci@0/network@4", "/pci@0/network@4,1",
3823250Scth  *			"/pci@0,1/network@6", "/pci@0,1/network@6,1";
3833250Scth  *
3843250Scth  * The value of the 'ddi-instance-blocks' property references a series
3853250Scth  * of card specific properties, like 'ib-FJSV-e4ta', who's value
3863250Scth  * defines a single 'instance block'.  The 'instance block' describes
3873250Scth  * all the paths below a multi-function bridge, where each path is
3883250Scth  * called an 'instance path'.  The 'instance block' property value is a
3893250Scth  * series of 'instance paths'.  The number of 'instance paths' in an
3903250Scth  * 'instance block' defines the size of the instance block, and the
3913250Scth  * ordering of the 'instance paths' defines the instance number
3923250Scth  * assignment order for paths going through the 'instance block'.
3933250Scth  *
3943250Scth  * In the instance assignment code below, if a (path, driver) that
3953250Scth  * currently has no instance number has a path that goes through an
3963250Scth  * 'instance block', then block instance number allocation occurs.  The
3973250Scth  * block allocation code will find a sequential set of unused instance
3983250Scth  * numbers, and assign instance numbers for all the paths in the
3993250Scth  * 'instance block'.  Each path is assigned a persistent instance
4003250Scth  * number, even paths that don't exist in the device tree or fail
4013250Scth  * probe(9E).
4023250Scth  */
4033250Scth static int
in_assign_instance_block(dev_info_t * dip)4043250Scth in_assign_instance_block(dev_info_t *dip)
4053250Scth {
4063250Scth 	char		**ibn;		/* instance block names */
4073250Scth 	uint_t		nibn;		/* number of instance block names */
4083250Scth 	uint_t		ibni;		/* ibn index */
4093250Scth 	char		*driver;
4103250Scth 	major_t		major;
4113250Scth 	char		*path;
4123250Scth 	char		*addr;
4133250Scth 	int		plen;
4143250Scth 	char		**ibp;		/* instance block paths */
4153250Scth 	uint_t		nibp;		/* number of paths in instance block */
4163250Scth 	uint_t		ibpi;		/* ibp index */
4173250Scth 	int		ibplen;		/* length of instance block path */
4183250Scth 	char		*ipath;
4193250Scth 	int		instance_base;
4203250Scth 	int		splice;
4213250Scth 	int		i;
4223250Scth 
4233250Scth 	/* check for fresh install case (in miniroot) */
4243250Scth 	if (DEVI(dip)->devi_instance != -1)
4253250Scth 		return (0);			/* already assigned */
4263250Scth 
4273250Scth 	/*
4283250Scth 	 * Check to see if we need to allocate a block of contiguous instance
4293250Scth 	 * numbers by looking for the 'ddi-instance-blocks' property.
4303250Scth 	 */
4313250Scth 	if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
4323250Scth 	    "ddi-instance-blocks", &ibn, &nibn) != DDI_SUCCESS)
4333250Scth 		return (0);			/* no instance block needed */
4343250Scth 
4353250Scth 	/*
4363250Scth 	 * Get information out about node we are processing.
4373250Scth 	 *
4383250Scth 	 * NOTE: Since the node is not yet at DS_INITIALIZED, ddi_pathname()
4393250Scth 	 * will not return the unit-address of the final path component even
4403250Scth 	 * though the node has an established devi_addr unit-address - so we
4413250Scth 	 * need to add the unit-address by hand.
4423250Scth 	 */
4433250Scth 	driver = (char *)ddi_driver_name(dip);
4443250Scth 	major = ddi_driver_major(dip);
4453250Scth 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
4463250Scth 	(void) ddi_pathname(dip, path);
4473250Scth 	if ((addr =  ddi_get_name_addr(dip)) != NULL) {
4483250Scth 		(void) strcat(path, "@");
4493250Scth 		(void) strcat(path, addr);
4503250Scth 	}
4513250Scth 	plen = strlen(path);
4523250Scth 
4533250Scth 	/* loop through instance block names */
4543250Scth 	for (ibni = 0; ibni < nibn; ibni++) {
4553250Scth 		if (ibn[ibni] == NULL)
4563250Scth 			continue;
4573250Scth 
4583250Scth 		/* lookup instance block */
4593250Scth 		if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, dip,
4603250Scth 		    DDI_PROP_DONTPASS, ibn[ibni],
4613250Scth 		    &ibp, &nibp) != DDI_SUCCESS) {
4623250Scth 			cmn_err(CE_WARN,
4633250Scth 			    "no devinition for instance block '%s' in %s.conf",
4643250Scth 			    ibn[ibni], driver);
4653250Scth 			continue;
4663250Scth 		}
4673250Scth 
4683250Scth 		/* Does 'path' go through this instance block? */
4693250Scth 		for (ibpi = 0; ibpi < nibp; ibpi++) {
4703250Scth 			if (ibp[ibpi] == NULL)
4713250Scth 				continue;
4723250Scth 			ibplen = strlen(ibp[ibpi]);
4733250Scth 			if ((ibplen <= plen) &&
4743250Scth 			    (strcmp(ibp[ibpi], path + plen - ibplen) == 0))
4753250Scth 				break;
4763250Scth 
4773250Scth 		}
4783250Scth 		if (ibpi >= nibp) {
4793250Scth 			ddi_prop_free(ibp);
4803250Scth 			continue;		/* no try next instance block */
4813250Scth 		}
4823250Scth 
4833250Scth 		/* yes, allocate and assign instances for all paths in block */
4843250Scth 
4853250Scth 		/*
4863250Scth 		 * determine where we splice in instance paths and verify
4873250Scth 		 * that none of the paths are too long.
4883250Scth 		 */
4893250Scth 		splice = plen - ibplen;
4903250Scth 		for (i = 0; i < nibp; i++) {
4913250Scth 			if ((splice + strlen(ibp[i])+ 1) >= MAXPATHLEN) {
4923250Scth 				cmn_err(CE_WARN,
4933250Scth 				    "path %d through instance block '%s' from "
4943250Scth 				    "%s.conf too long", i, ibn[ibni], driver);
4953250Scth 				break;
4963250Scth 			}
4973250Scth 		}
4983250Scth 		if (i < nibp) {
4993250Scth 			ddi_prop_free(ibp);
5003250Scth 			continue;		/* too long */
5013250Scth 		}
5023250Scth 
5033250Scth 		/* allocate the instance block - no more failures */
5043250Scth 		instance_base = in_next_instance_block(major, nibp);
5053250Scth 
5063250Scth 		ipath = kmem_alloc(MAXPATHLEN, KM_SLEEP);
5073250Scth 		for (ibpi = 0; ibpi < nibp; ibpi++) {
5083250Scth 			if (ibp[ibpi] == NULL)
5093250Scth 				continue;
5103250Scth 			(void) strcpy(ipath, path);
5113250Scth 			(void) strcpy(ipath + splice, ibp[ibpi]);
5123250Scth 			(void) in_pathin(ipath,
5133250Scth 			    instance_base + ibpi, driver, NULL);
5143250Scth 		}
5153250Scth 
5163250Scth 		/* free allocations */
5173250Scth 		kmem_free(ipath, MAXPATHLEN);
5183250Scth 		ddi_prop_free(ibp);
5193250Scth 		kmem_free(path, MAXPATHLEN);
5203250Scth 		ddi_prop_free(ibn);
5213250Scth 
5223250Scth 		/* notify devfsadmd to sync of path_to_inst file */
5233250Scth 		mutex_enter(&e_ddi_inst_state.ins_serial);
5243250Scth 		i_log_devfs_instance_mod();
52512116SVikram.Hegde@Sun.COM 		e_ddi_inst_state.ins_dirty = B_TRUE;
5263250Scth 		mutex_exit(&e_ddi_inst_state.ins_serial);
5273250Scth 		return (1);
5283250Scth 	}
5293250Scth 
5303250Scth 	/* our path did not go through any of of the instance blocks */
5313250Scth 	kmem_free(path, MAXPATHLEN);
5323250Scth 	ddi_prop_free(ibn);
5333250Scth 	return (0);
5343250Scth }
5353250Scth 
5363250Scth /*
5370Sstevel@tonic-gate  * Look up an instance number for a dev_info node, and assign one if it does
5380Sstevel@tonic-gate  * not have one (the dev_info node has devi_name and devi_addr already set).
5390Sstevel@tonic-gate  */
5400Sstevel@tonic-gate uint_t
e_ddi_assign_instance(dev_info_t * dip)5410Sstevel@tonic-gate e_ddi_assign_instance(dev_info_t *dip)
5420Sstevel@tonic-gate {
5430Sstevel@tonic-gate 	char *name;
5440Sstevel@tonic-gate 	in_node_t *ap, *np;
5450Sstevel@tonic-gate 	in_drv_t *dp;
5460Sstevel@tonic-gate 	major_t major;
5470Sstevel@tonic-gate 	uint_t ret;
5480Sstevel@tonic-gate 	char *bname;
5490Sstevel@tonic-gate 
5500Sstevel@tonic-gate 	/*
5510Sstevel@tonic-gate 	 * Allow implementation to override
5520Sstevel@tonic-gate 	 */
5530Sstevel@tonic-gate 	if ((ret = impl_assign_instance(dip)) != (uint_t)-1)
5540Sstevel@tonic-gate 		return (ret);
5550Sstevel@tonic-gate 
5560Sstevel@tonic-gate 	/*
5570Sstevel@tonic-gate 	 * If this is a pseudo-device, use the instance number
5580Sstevel@tonic-gate 	 * assigned by the pseudo nexus driver. The mutex is
5590Sstevel@tonic-gate 	 * not needed since the instance tree is not used.
5600Sstevel@tonic-gate 	 */
5610Sstevel@tonic-gate 	if (is_pseudo_device(dip)) {
5620Sstevel@tonic-gate 		return (ddi_get_instance(dip));
5630Sstevel@tonic-gate 	}
5640Sstevel@tonic-gate 
5650Sstevel@tonic-gate 	/*
5660Sstevel@tonic-gate 	 * Only one thread is allowed to change the state of the instance
5670Sstevel@tonic-gate 	 * number assignments on the system at any given time.
5680Sstevel@tonic-gate 	 */
5690Sstevel@tonic-gate 	e_ddi_enter_instance();
5700Sstevel@tonic-gate 
5710Sstevel@tonic-gate 	/*
5720Sstevel@tonic-gate 	 * Look for instance node, allocate one if not found
5730Sstevel@tonic-gate 	 */
5740Sstevel@tonic-gate 	np = in_devwalk(dip, &ap, NULL);
5750Sstevel@tonic-gate 	if (np == NULL) {
5763250Scth 		if (in_assign_instance_block(dip)) {
5773250Scth 			np = in_devwalk(dip, &ap, NULL);
5783250Scth 		} else {
5793250Scth 			name = ddi_node_name(dip);
5803250Scth 			np = in_alloc_node(name, ddi_get_name_addr(dip));
5813250Scth 			ASSERT(np != NULL);
5823250Scth 			in_enlist(ap, np);	/* insert into tree */
5833250Scth 		}
5840Sstevel@tonic-gate 	}
5850Sstevel@tonic-gate 	ASSERT(np == in_devwalk(dip, &ap, NULL));
5860Sstevel@tonic-gate 
5870Sstevel@tonic-gate 	/*
58812116SVikram.Hegde@Sun.COM 	 * Link the devinfo node and in_node_t
58912116SVikram.Hegde@Sun.COM 	 */
59012116SVikram.Hegde@Sun.COM 	if (DEVI(dip)->devi_in_node || np->in_devi) {
59112116SVikram.Hegde@Sun.COM 		ddi_err(DER_MODE, dip, "devinfo and  instance node (%p) "
59212116SVikram.Hegde@Sun.COM 		    "interlink fields are not NULL", (void *)np);
59312116SVikram.Hegde@Sun.COM 	}
59412116SVikram.Hegde@Sun.COM 	DEVI(dip)->devi_in_node = np;
59512116SVikram.Hegde@Sun.COM 	np->in_devi = dip;
59612116SVikram.Hegde@Sun.COM 
59712116SVikram.Hegde@Sun.COM 	/*
5980Sstevel@tonic-gate 	 * Look for driver entry, allocate one if not found
5990Sstevel@tonic-gate 	 */
6000Sstevel@tonic-gate 	bname = (char *)ddi_driver_name(dip);
6010Sstevel@tonic-gate 	dp = in_drvwalk(np, bname);
6020Sstevel@tonic-gate 	if (dp == NULL) {
60312116SVikram.Hegde@Sun.COM 
60412116SVikram.Hegde@Sun.COM 		if (ddi_aliases_present == B_TRUE) {
60512116SVikram.Hegde@Sun.COM 			e_ddi_borrow_instance(dip, np);
60612116SVikram.Hegde@Sun.COM 		}
60712116SVikram.Hegde@Sun.COM 
60812116SVikram.Hegde@Sun.COM 		if ((dp = in_drvwalk(np, bname)) == NULL) {
60912116SVikram.Hegde@Sun.COM 			dp = in_alloc_drv(bname);
61012116SVikram.Hegde@Sun.COM 			ASSERT(dp != NULL);
61112116SVikram.Hegde@Sun.COM 			major = ddi_driver_major(dip);
61212116SVikram.Hegde@Sun.COM 			ASSERT(major != DDI_MAJOR_T_NONE);
61312116SVikram.Hegde@Sun.COM 			in_endrv(np, dp);
61412116SVikram.Hegde@Sun.COM 			in_set_instance(dip, dp, major);
61512116SVikram.Hegde@Sun.COM 			dp->ind_state = IN_PROVISIONAL;
61612116SVikram.Hegde@Sun.COM 			in_hashdrv(dp);
61712116SVikram.Hegde@Sun.COM 		} else {
61812116SVikram.Hegde@Sun.COM 			dp->ind_state = IN_BORROWED;
61912116SVikram.Hegde@Sun.COM 		}
6200Sstevel@tonic-gate 	}
6210Sstevel@tonic-gate 
6220Sstevel@tonic-gate 	ret = dp->ind_instance;
6230Sstevel@tonic-gate 
6240Sstevel@tonic-gate 	e_ddi_exit_instance();
6250Sstevel@tonic-gate 	return (ret);
6260Sstevel@tonic-gate }
6270Sstevel@tonic-gate 
6280Sstevel@tonic-gate static int
mkpathname(char * path,in_node_t * np,int len)6290Sstevel@tonic-gate mkpathname(char *path, in_node_t *np, int len)
6300Sstevel@tonic-gate {
6310Sstevel@tonic-gate 	int len_needed;
6320Sstevel@tonic-gate 
6330Sstevel@tonic-gate 	if (np == e_ddi_inst_state.ins_root)
6340Sstevel@tonic-gate 		return (DDI_SUCCESS);
6350Sstevel@tonic-gate 
6360Sstevel@tonic-gate 	if (mkpathname(path, np->in_parent, len) == DDI_FAILURE)
6370Sstevel@tonic-gate 		return (DDI_FAILURE);
6380Sstevel@tonic-gate 
6390Sstevel@tonic-gate 	len_needed = strlen(path);
6400Sstevel@tonic-gate 	len_needed += strlen(np->in_node_name) + 1;	/* for '/' */
6410Sstevel@tonic-gate 	if (np->in_unit_addr) {
6420Sstevel@tonic-gate 		len_needed += strlen(np->in_unit_addr) + 1;  /* for '@' */
6430Sstevel@tonic-gate 	}
6440Sstevel@tonic-gate 	len_needed += 1; /* for '\0' */
6450Sstevel@tonic-gate 
6460Sstevel@tonic-gate 	/*
6470Sstevel@tonic-gate 	 * XX complain
6480Sstevel@tonic-gate 	 */
6490Sstevel@tonic-gate 	if (len_needed > len)
6500Sstevel@tonic-gate 		return (DDI_FAILURE);
6510Sstevel@tonic-gate 
6520Sstevel@tonic-gate 	if (np->in_unit_addr[0] == '\0')
6530Sstevel@tonic-gate 		(void) sprintf(path+strlen(path), "/%s", np->in_node_name);
6540Sstevel@tonic-gate 	else
6550Sstevel@tonic-gate 		(void) sprintf(path+strlen(path), "/%s@%s", np->in_node_name,
6560Sstevel@tonic-gate 		    np->in_unit_addr);
6570Sstevel@tonic-gate 
6580Sstevel@tonic-gate 	return (DDI_SUCCESS);
6590Sstevel@tonic-gate }
6600Sstevel@tonic-gate 
6610Sstevel@tonic-gate /*
6620Sstevel@tonic-gate  * produce the path to the given instance of a major number.
6630Sstevel@tonic-gate  * path must hold MAXPATHLEN string
6640Sstevel@tonic-gate  */
6650Sstevel@tonic-gate int
e_ddi_instance_majorinstance_to_path(major_t major,uint_t inst,char * path)6660Sstevel@tonic-gate e_ddi_instance_majorinstance_to_path(major_t major, uint_t inst, char *path)
6670Sstevel@tonic-gate {
6680Sstevel@tonic-gate 	struct devnames	*dnp;
6690Sstevel@tonic-gate 	in_drv_t	*dp;
6700Sstevel@tonic-gate 	int		ret;
6710Sstevel@tonic-gate 
6720Sstevel@tonic-gate 	e_ddi_enter_instance();
6730Sstevel@tonic-gate 
6740Sstevel@tonic-gate 	/* look for the instance threaded off major */
6750Sstevel@tonic-gate 	dnp = &devnamesp[major];
6760Sstevel@tonic-gate 	for (dp = dnp->dn_inlist; dp != NULL; dp = dp->ind_next)
6770Sstevel@tonic-gate 		if (dp->ind_instance == inst)
6780Sstevel@tonic-gate 			break;
6790Sstevel@tonic-gate 
6800Sstevel@tonic-gate 	/* produce path from the node that uses the instance */
6810Sstevel@tonic-gate 	if (dp) {
6820Sstevel@tonic-gate 		*path = 0;
6830Sstevel@tonic-gate 		ret = mkpathname(path, dp->ind_node, MAXPATHLEN);
6840Sstevel@tonic-gate 	} else
6850Sstevel@tonic-gate 		ret = DDI_FAILURE;
6860Sstevel@tonic-gate 
6870Sstevel@tonic-gate 	e_ddi_exit_instance();
6880Sstevel@tonic-gate 	return (ret);
6890Sstevel@tonic-gate }
6900Sstevel@tonic-gate 
6910Sstevel@tonic-gate /*
6923250Scth  * Allocate a sequential block of instance numbers for the specified driver,
6933250Scth  * and return the base instance number of the block.  The implementation
6943250Scth  * depends on the list being sorted in ascending instance number sequence.
6953250Scth  * When there are no 'holes' in the allocation sequence, dn_instance is the
6963250Scth  * next available instance number. When dn_instance is IN_SEARCHME, hole(s)
6973250Scth  * exists and a slower code path executes which tries to fill holes.
69812288SChris.Horne@Sun.COM  *
69912288SChris.Horne@Sun.COM  * The block returned can't be in the preassigned range.
7000Sstevel@tonic-gate  */
7010Sstevel@tonic-gate static int
in_next_instance_block(major_t major,int block_size)7023250Scth in_next_instance_block(major_t major, int block_size)
7030Sstevel@tonic-gate {
70412288SChris.Horne@Sun.COM 	int		prev;
7053250Scth 	struct devnames	*dnp;
7063250Scth 	in_drv_t	*dp;
7073250Scth 	int		base;
7083250Scth 	int		hole;
7090Sstevel@tonic-gate 
7100Sstevel@tonic-gate 	dnp = &devnamesp[major];
7117009Scth 	ASSERT(major != DDI_MAJOR_T_NONE);
7120Sstevel@tonic-gate 	ASSERT(e_ddi_inst_state.ins_busy);
7133250Scth 	ASSERT(block_size);
7143250Scth 
7153250Scth 	/* check to see if we can do a quick allocation */
71612288SChris.Horne@Sun.COM 	if (!instance_searchme && (dnp->dn_instance != IN_SEARCHME)) {
7173250Scth 		base = dnp->dn_instance;
7183250Scth 		dnp->dn_instance += block_size;
7193250Scth 		return (base);
7203250Scth 	}
72112288SChris.Horne@Sun.COM 
72212442SChris.Horne@Sun.COM 	/*
72312442SChris.Horne@Sun.COM 	 * Use more complex code path, start by skipping preassign entries.
72412442SChris.Horne@Sun.COM 	 */
72512442SChris.Horne@Sun.COM 	for (dp = dnp->dn_inlist; dp; dp = dp->ind_next)
72612442SChris.Horne@Sun.COM 		if (dp->ind_instance >= dnp->dn_pinstance)
72712442SChris.Horne@Sun.COM 			break;		/* beyond preassign */
7280Sstevel@tonic-gate 
72912442SChris.Horne@Sun.COM 	/* No non-preassign entries, allocate block at preassign base. */
7300Sstevel@tonic-gate 	if (dp == NULL) {
73112288SChris.Horne@Sun.COM 		base = dnp->dn_pinstance;
73212442SChris.Horne@Sun.COM 		if (base == 0)
73312442SChris.Horne@Sun.COM 			dnp->dn_instance = block_size;
73412288SChris.Horne@Sun.COM 		return (base);
7350Sstevel@tonic-gate 	}
7360Sstevel@tonic-gate 
73712442SChris.Horne@Sun.COM 	/* See if we fit in hole at beginning (after preassigns) */
7380Sstevel@tonic-gate 	prev = dp->ind_instance;
73912288SChris.Horne@Sun.COM 	if ((prev - dnp->dn_pinstance) >= block_size)
74012288SChris.Horne@Sun.COM 		return (dnp->dn_pinstance);	/* we fit in beginning hole */
7413250Scth 
7423250Scth 	/* search the list for a large enough hole */
7433250Scth 	for (dp = dp->ind_next, hole = 0; dp; dp = dp->ind_next) {
74412442SChris.Horne@Sun.COM 		if (dp->ind_instance != (prev + 1))
74512442SChris.Horne@Sun.COM 			hole++;			/* we have a hole */
74612442SChris.Horne@Sun.COM 		if (dp->ind_instance >= (prev + block_size + 1))
74712442SChris.Horne@Sun.COM 			break;			/* we fit in hole */
7483250Scth 		prev = dp->ind_instance;
7490Sstevel@tonic-gate 	}
7503250Scth 
7510Sstevel@tonic-gate 	/*
7523250Scth 	 * If hole is zero then all holes are patched and we can resume
75312442SChris.Horne@Sun.COM 	 * quick allocations, but don't resume quick allocation if there is
75412442SChris.Horne@Sun.COM 	 * a preassign.
7550Sstevel@tonic-gate 	 */
75612442SChris.Horne@Sun.COM 	if ((hole == 0) && (dnp->dn_pinstance == 0))
7573250Scth 		dnp->dn_instance = prev + 1 + block_size;
7583250Scth 
7593250Scth 	return (prev + 1);
7603250Scth }
7610Sstevel@tonic-gate 
7623250Scth /* assign instance block of size 1 */
7633250Scth static int
in_next_instance(major_t major)7643250Scth in_next_instance(major_t major)
7653250Scth {
7663250Scth 	return (in_next_instance_block(major, 1));
7670Sstevel@tonic-gate }
7680Sstevel@tonic-gate 
7690Sstevel@tonic-gate /*
7700Sstevel@tonic-gate  * This call causes us to *forget* the instance number we've generated
7710Sstevel@tonic-gate  * for a given device if it was not permanent.
7720Sstevel@tonic-gate  */
7730Sstevel@tonic-gate void
e_ddi_free_instance(dev_info_t * dip,char * addr)7740Sstevel@tonic-gate e_ddi_free_instance(dev_info_t *dip, char *addr)
7750Sstevel@tonic-gate {
7760Sstevel@tonic-gate 	char *name;
7770Sstevel@tonic-gate 	in_node_t *np;
7780Sstevel@tonic-gate 	in_node_t *ap;	/* ancestor node */
7790Sstevel@tonic-gate 	major_t major;
7800Sstevel@tonic-gate 	struct devnames *dnp;
7810Sstevel@tonic-gate 	in_drv_t *dp;	/* in_drv entry */
7820Sstevel@tonic-gate 
7830Sstevel@tonic-gate 	/*
7840Sstevel@tonic-gate 	 * Allow implementation override
7850Sstevel@tonic-gate 	 */
7860Sstevel@tonic-gate 	if (impl_free_instance(dip) == DDI_SUCCESS)
7870Sstevel@tonic-gate 		return;
7880Sstevel@tonic-gate 
7890Sstevel@tonic-gate 	/*
7900Sstevel@tonic-gate 	 * If this is a pseudo-device, no instance number
7910Sstevel@tonic-gate 	 * was assigned.
7920Sstevel@tonic-gate 	 */
7930Sstevel@tonic-gate 	if (is_pseudo_device(dip)) {
7940Sstevel@tonic-gate 		return;
7950Sstevel@tonic-gate 	}
7960Sstevel@tonic-gate 
7970Sstevel@tonic-gate 	name = (char *)ddi_driver_name(dip);
7980Sstevel@tonic-gate 	major = ddi_driver_major(dip);
7997009Scth 	ASSERT(major != DDI_MAJOR_T_NONE);
8000Sstevel@tonic-gate 	dnp = &devnamesp[major];
8010Sstevel@tonic-gate 	/*
8020Sstevel@tonic-gate 	 * Only one thread is allowed to change the state of the instance
8030Sstevel@tonic-gate 	 * number assignments on the system at any given time.
8040Sstevel@tonic-gate 	 */
8050Sstevel@tonic-gate 	e_ddi_enter_instance();
8060Sstevel@tonic-gate 	np = in_devwalk(dip, &ap, addr);
8070Sstevel@tonic-gate 	ASSERT(np);
80812116SVikram.Hegde@Sun.COM 
80912116SVikram.Hegde@Sun.COM 	/*
81012116SVikram.Hegde@Sun.COM 	 * Break the interlink between dip and np
81112116SVikram.Hegde@Sun.COM 	 */
81212116SVikram.Hegde@Sun.COM 	if (DEVI(dip)->devi_in_node != np || np->in_devi != dip) {
81312116SVikram.Hegde@Sun.COM 		ddi_err(DER_MODE, dip, "devinfo node linked to "
81412116SVikram.Hegde@Sun.COM 		    "wrong instance node: %p", (void *)np);
81512116SVikram.Hegde@Sun.COM 	}
81612116SVikram.Hegde@Sun.COM 	DEVI(dip)->devi_in_node = NULL;
81712116SVikram.Hegde@Sun.COM 	np->in_devi = NULL;
81812116SVikram.Hegde@Sun.COM 
8190Sstevel@tonic-gate 	dp = in_drvwalk(np, name);
8200Sstevel@tonic-gate 	ASSERT(dp);
8210Sstevel@tonic-gate 	if (dp->ind_state == IN_PROVISIONAL) {
8220Sstevel@tonic-gate 		in_removedrv(dnp, dp);
82312171SVikram.Hegde@Sun.COM 	} else if (dp->ind_state == IN_BORROWED) {
82412116SVikram.Hegde@Sun.COM 		dp->ind_state = IN_PERMANENT;
82512116SVikram.Hegde@Sun.COM 		e_ddi_return_instance(dip, addr, np);
82612116SVikram.Hegde@Sun.COM 	}
8270Sstevel@tonic-gate 	if (np->in_drivers == NULL) {
8280Sstevel@tonic-gate 		in_removenode(dnp, np, ap);
8290Sstevel@tonic-gate 	}
8300Sstevel@tonic-gate 	e_ddi_exit_instance();
8310Sstevel@tonic-gate }
8320Sstevel@tonic-gate 
8330Sstevel@tonic-gate /*
8340Sstevel@tonic-gate  * This makes our memory of an instance assignment permanent
8350Sstevel@tonic-gate  */
8360Sstevel@tonic-gate void
e_ddi_keep_instance(dev_info_t * dip)8370Sstevel@tonic-gate e_ddi_keep_instance(dev_info_t *dip)
8380Sstevel@tonic-gate {
8390Sstevel@tonic-gate 	in_node_t *np, *ap;
8400Sstevel@tonic-gate 	in_drv_t *dp;
8410Sstevel@tonic-gate 
8428011SChris.Horne@Sun.COM 	/* Don't make nulldriver instance assignments permanent */
8438011SChris.Horne@Sun.COM 	if (ddi_driver_major(dip) == nulldriver_major)
8448011SChris.Horne@Sun.COM 		return;
8458011SChris.Horne@Sun.COM 
8460Sstevel@tonic-gate 	/*
8470Sstevel@tonic-gate 	 * Allow implementation override
8480Sstevel@tonic-gate 	 */
8490Sstevel@tonic-gate 	if (impl_keep_instance(dip) == DDI_SUCCESS)
8500Sstevel@tonic-gate 		return;
8510Sstevel@tonic-gate 
8520Sstevel@tonic-gate 	/*
8530Sstevel@tonic-gate 	 * Nothing to do for pseudo devices.
8540Sstevel@tonic-gate 	 */
8550Sstevel@tonic-gate 	if (is_pseudo_device(dip))
8560Sstevel@tonic-gate 		return;
8570Sstevel@tonic-gate 
8580Sstevel@tonic-gate 	/*
8590Sstevel@tonic-gate 	 * Only one thread is allowed to change the state of the instance
8600Sstevel@tonic-gate 	 * number assignments on the system at any given time.
8610Sstevel@tonic-gate 	 */
8620Sstevel@tonic-gate 	e_ddi_enter_instance();
8630Sstevel@tonic-gate 	np = in_devwalk(dip, &ap, NULL);
8640Sstevel@tonic-gate 	ASSERT(np);
8650Sstevel@tonic-gate 	dp = in_drvwalk(np, (char *)ddi_driver_name(dip));
8660Sstevel@tonic-gate 	ASSERT(dp);
8670Sstevel@tonic-gate 
8680Sstevel@tonic-gate 	mutex_enter(&e_ddi_inst_state.ins_serial);
86912116SVikram.Hegde@Sun.COM 	if (dp->ind_state == IN_PROVISIONAL || dp->ind_state == IN_BORROWED) {
8700Sstevel@tonic-gate 		dp->ind_state = IN_PERMANENT;
8710Sstevel@tonic-gate 		i_log_devfs_instance_mod();
87212116SVikram.Hegde@Sun.COM 		e_ddi_inst_state.ins_dirty = B_TRUE;
8730Sstevel@tonic-gate 	}
8740Sstevel@tonic-gate 	mutex_exit(&e_ddi_inst_state.ins_serial);
8750Sstevel@tonic-gate 	e_ddi_exit_instance();
8760Sstevel@tonic-gate }
8770Sstevel@tonic-gate 
8780Sstevel@tonic-gate /*
8790Sstevel@tonic-gate  * A new major has been added to the system.  Run through the orphan list
8800Sstevel@tonic-gate  * and try to attach each one to a driver's list.
8810Sstevel@tonic-gate  */
8820Sstevel@tonic-gate void
e_ddi_unorphan_instance_nos()8830Sstevel@tonic-gate e_ddi_unorphan_instance_nos()
8840Sstevel@tonic-gate {
8850Sstevel@tonic-gate 	in_drv_t *dp, *ndp;
8860Sstevel@tonic-gate 
8870Sstevel@tonic-gate 	/*
8880Sstevel@tonic-gate 	 * disconnect the orphan list, and call in_hashdrv for each item
8890Sstevel@tonic-gate 	 * on it
8900Sstevel@tonic-gate 	 */
8910Sstevel@tonic-gate 
8920Sstevel@tonic-gate 	/*
8930Sstevel@tonic-gate 	 * Only one thread is allowed to change the state of the instance
8940Sstevel@tonic-gate 	 * number assignments on the system at any given time.
8950Sstevel@tonic-gate 	 */
8960Sstevel@tonic-gate 	e_ddi_enter_instance();
8970Sstevel@tonic-gate 	if (e_ddi_inst_state.ins_no_major == NULL) {
8980Sstevel@tonic-gate 		e_ddi_exit_instance();
8990Sstevel@tonic-gate 		return;
9000Sstevel@tonic-gate 	}
9010Sstevel@tonic-gate 	/*
9020Sstevel@tonic-gate 	 * Hash instance list to devnames structure of major.
9030Sstevel@tonic-gate 	 * Note that if there is not a valid major number for the
9040Sstevel@tonic-gate 	 * node, in_hashdrv will put it back on the no_major list.
9050Sstevel@tonic-gate 	 */
9060Sstevel@tonic-gate 	dp = e_ddi_inst_state.ins_no_major;
9070Sstevel@tonic-gate 	e_ddi_inst_state.ins_no_major = NULL;
9080Sstevel@tonic-gate 	while (dp) {
9090Sstevel@tonic-gate 		ndp = dp->ind_next;
9100Sstevel@tonic-gate 		ASSERT(dp->ind_state != IN_UNKNOWN);
9110Sstevel@tonic-gate 		dp->ind_next = NULL;
9120Sstevel@tonic-gate 		in_hashdrv(dp);
9130Sstevel@tonic-gate 		dp = ndp;
9140Sstevel@tonic-gate 	}
9150Sstevel@tonic-gate 	e_ddi_exit_instance();
9160Sstevel@tonic-gate }
9170Sstevel@tonic-gate 
9180Sstevel@tonic-gate static void
in_removenode(struct devnames * dnp,in_node_t * mp,in_node_t * ap)9190Sstevel@tonic-gate in_removenode(struct devnames *dnp, in_node_t *mp, in_node_t *ap)
9200Sstevel@tonic-gate {
9210Sstevel@tonic-gate 	in_node_t *np;
9220Sstevel@tonic-gate 
9230Sstevel@tonic-gate 	ASSERT(e_ddi_inst_state.ins_busy);
92412116SVikram.Hegde@Sun.COM 
9250Sstevel@tonic-gate 	/*
9260Sstevel@tonic-gate 	 * Assertion: parents are always instantiated by the framework
9270Sstevel@tonic-gate 	 * before their children, destroyed after them
9280Sstevel@tonic-gate 	 */
9290Sstevel@tonic-gate 	ASSERT(mp->in_child == NULL);
9300Sstevel@tonic-gate 	/*
9310Sstevel@tonic-gate 	 * Assertion: drv entries are always removed before their owning nodes
9320Sstevel@tonic-gate 	 */
9330Sstevel@tonic-gate 	ASSERT(mp->in_drivers == NULL);
9340Sstevel@tonic-gate 	/*
9350Sstevel@tonic-gate 	 * Take the node out of the tree
9360Sstevel@tonic-gate 	 */
9370Sstevel@tonic-gate 	if (ap->in_child == mp) {
9380Sstevel@tonic-gate 		ap->in_child = mp->in_sibling;
9390Sstevel@tonic-gate 		in_dealloc_node(mp);
9400Sstevel@tonic-gate 		return;
9410Sstevel@tonic-gate 	} else {
9420Sstevel@tonic-gate 		for (np = ap->in_child; np; np = np->in_sibling) {
9430Sstevel@tonic-gate 			if (np->in_sibling == mp) {
9440Sstevel@tonic-gate 				np->in_sibling = mp->in_sibling;
9450Sstevel@tonic-gate 				in_dealloc_node(mp);
9460Sstevel@tonic-gate 				return;
9470Sstevel@tonic-gate 			}
9480Sstevel@tonic-gate 		}
9490Sstevel@tonic-gate 	}
9500Sstevel@tonic-gate 	panic("in_removenode dnp %p mp %p", (void *)dnp, (void *)mp);
9510Sstevel@tonic-gate }
9520Sstevel@tonic-gate 
9530Sstevel@tonic-gate /*
9540Sstevel@tonic-gate  * Recursive ascent
9550Sstevel@tonic-gate  *
9560Sstevel@tonic-gate  * This now only does half the job.  It finds the node, then the caller
9570Sstevel@tonic-gate  * has to search the node for the binding name
9580Sstevel@tonic-gate  */
9590Sstevel@tonic-gate static in_node_t *
in_devwalk(dev_info_t * dip,in_node_t ** ap,char * addr)9600Sstevel@tonic-gate in_devwalk(dev_info_t *dip, in_node_t **ap, char *addr)
9610Sstevel@tonic-gate {
9620Sstevel@tonic-gate 	in_node_t *np;
9630Sstevel@tonic-gate 	char *name;
9640Sstevel@tonic-gate 
9650Sstevel@tonic-gate 	ASSERT(dip);
9660Sstevel@tonic-gate 	ASSERT(e_ddi_inst_state.ins_busy);
9670Sstevel@tonic-gate 	if (dip == ddi_root_node()) {
9680Sstevel@tonic-gate 		*ap = NULL;
9690Sstevel@tonic-gate 		return (e_ddi_inst_state.ins_root);
9700Sstevel@tonic-gate 	}
9710Sstevel@tonic-gate 	/*
9720Sstevel@tonic-gate 	 * call up to find parent, then look through the list of kids
9730Sstevel@tonic-gate 	 * for a match
9740Sstevel@tonic-gate 	 */
9750Sstevel@tonic-gate 	np = in_devwalk(ddi_get_parent(dip), ap, NULL);
9760Sstevel@tonic-gate 	if (np == NULL)
9770Sstevel@tonic-gate 		return (np);
9780Sstevel@tonic-gate 	*ap = np;
9790Sstevel@tonic-gate 	np = np->in_child;
9800Sstevel@tonic-gate 	name = ddi_node_name(dip);
9810Sstevel@tonic-gate 	if (addr == NULL)
9820Sstevel@tonic-gate 		addr = ddi_get_name_addr(dip);
9830Sstevel@tonic-gate 
9840Sstevel@tonic-gate 	while (np) {
9850Sstevel@tonic-gate 		if (in_eqstr(np->in_node_name, name) &&
9860Sstevel@tonic-gate 		    in_eqstr(np->in_unit_addr, addr)) {
9870Sstevel@tonic-gate 			return (np);
9880Sstevel@tonic-gate 		}
9890Sstevel@tonic-gate 		np = np->in_sibling;
9900Sstevel@tonic-gate 	}
99112116SVikram.Hegde@Sun.COM 
9920Sstevel@tonic-gate 	return (np);
9930Sstevel@tonic-gate }
9940Sstevel@tonic-gate 
9950Sstevel@tonic-gate /*
9960Sstevel@tonic-gate  * Create a node specified by cp and assign it the given instance no.
9970Sstevel@tonic-gate  */
9980Sstevel@tonic-gate static int
in_pathin(char * cp,int instance,char * bname,struct bind ** args)9990Sstevel@tonic-gate in_pathin(char *cp, int instance, char *bname, struct bind **args)
10000Sstevel@tonic-gate {
10010Sstevel@tonic-gate 	in_node_t *np;
10020Sstevel@tonic-gate 	in_drv_t *dp;
10030Sstevel@tonic-gate 	char *name;
10040Sstevel@tonic-gate 
10050Sstevel@tonic-gate 	ASSERT(e_ddi_inst_state.ins_busy);
10060Sstevel@tonic-gate 	ASSERT(args == NULL);
10070Sstevel@tonic-gate 
10080Sstevel@tonic-gate 	/*
10090Sstevel@tonic-gate 	 * Give a warning to the console.
10100Sstevel@tonic-gate 	 * return value ignored
10110Sstevel@tonic-gate 	 */
10120Sstevel@tonic-gate 	if (cp[0] != '/' || instance == -1 || bname == NULL) {
10130Sstevel@tonic-gate 		cmn_err(CE_WARN,
10140Sstevel@tonic-gate 		    "invalid instance file entry %s %d",
10150Sstevel@tonic-gate 		    cp, instance);
10160Sstevel@tonic-gate 		return (0);
10170Sstevel@tonic-gate 	}
10180Sstevel@tonic-gate 
10190Sstevel@tonic-gate 	if ((name  = i_binding_to_drv_name(bname)) != NULL)
10200Sstevel@tonic-gate 		bname = name;
10210Sstevel@tonic-gate 
10220Sstevel@tonic-gate 	np = in_make_path(cp);
10230Sstevel@tonic-gate 	ASSERT(np);
102412116SVikram.Hegde@Sun.COM 
10250Sstevel@tonic-gate 	dp = in_drvwalk(np, bname);
10260Sstevel@tonic-gate 	if (dp != NULL) {
10270Sstevel@tonic-gate 		cmn_err(CE_WARN,
10280Sstevel@tonic-gate 		    "multiple instance number assignments for "
10290Sstevel@tonic-gate 		    "'%s' (driver %s), %d used",
10300Sstevel@tonic-gate 		    cp, bname, dp->ind_instance);
10310Sstevel@tonic-gate 		return (0);
10320Sstevel@tonic-gate 	}
103312116SVikram.Hegde@Sun.COM 
103412116SVikram.Hegde@Sun.COM 	if (in_inuse(instance, bname)) {
103512116SVikram.Hegde@Sun.COM 		cmn_err(CE_WARN,
103612116SVikram.Hegde@Sun.COM 		    "instance already in use: %s %d", cp, instance);
103712116SVikram.Hegde@Sun.COM 		return (0);
103812116SVikram.Hegde@Sun.COM 	}
103912116SVikram.Hegde@Sun.COM 
10400Sstevel@tonic-gate 	dp = in_alloc_drv(bname);
10410Sstevel@tonic-gate 	in_endrv(np, dp);
10420Sstevel@tonic-gate 	dp->ind_instance = instance;
10430Sstevel@tonic-gate 	dp->ind_state = IN_PERMANENT;
10440Sstevel@tonic-gate 	in_hashdrv(dp);
10450Sstevel@tonic-gate 
10460Sstevel@tonic-gate 	return (0);
10470Sstevel@tonic-gate }
10480Sstevel@tonic-gate 
10490Sstevel@tonic-gate /*
10500Sstevel@tonic-gate  * Create (or find) the node named by path by recursively descending from the
10510Sstevel@tonic-gate  * root's first child (we ignore the root, which is never named)
10520Sstevel@tonic-gate  */
10530Sstevel@tonic-gate static in_node_t *
in_make_path(char * path)10540Sstevel@tonic-gate in_make_path(char *path)
10550Sstevel@tonic-gate {
10560Sstevel@tonic-gate 	in_node_t *ap;		/* ancestor pointer */
10570Sstevel@tonic-gate 	in_node_t *np;		/* working node pointer */
10580Sstevel@tonic-gate 	in_node_t *rp;		/* return node pointer */
10590Sstevel@tonic-gate 	char buf[MAXPATHLEN];	/* copy of string so we can change it */
10600Sstevel@tonic-gate 	char *cp, *name, *addr;
10610Sstevel@tonic-gate 
10620Sstevel@tonic-gate 	ASSERT(e_ddi_inst_state.ins_busy);
106312116SVikram.Hegde@Sun.COM 
10640Sstevel@tonic-gate 	if (path == NULL || path[0] != '/')
10650Sstevel@tonic-gate 		return (NULL);
106612116SVikram.Hegde@Sun.COM 
10670Sstevel@tonic-gate 	(void) snprintf(buf, sizeof (buf), "%s", path);
10680Sstevel@tonic-gate 	cp = buf + 1;	/* skip over initial '/' in path */
10690Sstevel@tonic-gate 	name = in_name_addr(&cp, &addr);
10700Sstevel@tonic-gate 
10710Sstevel@tonic-gate 	/*
10720Sstevel@tonic-gate 	 * In S9 and earlier releases, the path_to_inst file
10730Sstevel@tonic-gate 	 * SunCluster was prepended with "/node@#". This was
10740Sstevel@tonic-gate 	 * removed in S10. We skip the prefix if the prefix
10750Sstevel@tonic-gate 	 * still exists in /etc/path_to_inst. It is needed for
10760Sstevel@tonic-gate 	 * various forms of Solaris upgrade to work properly
10770Sstevel@tonic-gate 	 * in the SunCluster environment.
10780Sstevel@tonic-gate 	 */
10790Sstevel@tonic-gate 	if ((cluster_bootflags & CLUSTER_CONFIGURED) &&
10800Sstevel@tonic-gate 	    (strcmp(name, "node") == 0))
10810Sstevel@tonic-gate 		name = in_name_addr(&cp, &addr);
10820Sstevel@tonic-gate 
10830Sstevel@tonic-gate 	ap = e_ddi_inst_state.ins_root;
108412116SVikram.Hegde@Sun.COM 	np = e_ddi_inst_state.ins_root->in_child;
108512116SVikram.Hegde@Sun.COM 	rp = np;
10860Sstevel@tonic-gate 	while (name) {
10870Sstevel@tonic-gate 		while (name && np) {
10880Sstevel@tonic-gate 			if (in_eqstr(name, np->in_node_name) &&
10890Sstevel@tonic-gate 			    in_eqstr(addr, np->in_unit_addr)) {
10900Sstevel@tonic-gate 				name = in_name_addr(&cp, &addr);
10910Sstevel@tonic-gate 				if (name == NULL)
10920Sstevel@tonic-gate 					return (np);
10930Sstevel@tonic-gate 				ap = np;
10940Sstevel@tonic-gate 				np = np->in_child;
10950Sstevel@tonic-gate 			} else {
10960Sstevel@tonic-gate 				np = np->in_sibling;
10970Sstevel@tonic-gate 			}
10980Sstevel@tonic-gate 		}
10990Sstevel@tonic-gate 		np = in_alloc_node(name, addr);
11000Sstevel@tonic-gate 		in_enlist(ap, np);	/* insert into tree */
11010Sstevel@tonic-gate 		rp = np;	/* value to return if we quit */
11020Sstevel@tonic-gate 		ap = np;	/* new parent */
11030Sstevel@tonic-gate 		np = NULL;	/* can have no children */
11040Sstevel@tonic-gate 		name = in_name_addr(&cp, &addr);
11050Sstevel@tonic-gate 	}
110612116SVikram.Hegde@Sun.COM 
11070Sstevel@tonic-gate 	return (rp);
11080Sstevel@tonic-gate }
11090Sstevel@tonic-gate 
11100Sstevel@tonic-gate /*
11110Sstevel@tonic-gate  * Insert node np into the tree as one of ap's children.
11120Sstevel@tonic-gate  */
11130Sstevel@tonic-gate static void
in_enlist(in_node_t * ap,in_node_t * np)11140Sstevel@tonic-gate in_enlist(in_node_t *ap, in_node_t *np)
11150Sstevel@tonic-gate {
11160Sstevel@tonic-gate 	in_node_t *mp;
11170Sstevel@tonic-gate 	ASSERT(e_ddi_inst_state.ins_busy);
11180Sstevel@tonic-gate 	/*
11190Sstevel@tonic-gate 	 * Make this node some other node's child or child's sibling
11200Sstevel@tonic-gate 	 */
11210Sstevel@tonic-gate 	ASSERT(ap && np);
11220Sstevel@tonic-gate 	if (ap->in_child == NULL) {
11230Sstevel@tonic-gate 		ap->in_child = np;
11240Sstevel@tonic-gate 	} else {
11250Sstevel@tonic-gate 		for (mp = ap->in_child; mp; mp = mp->in_sibling)
11260Sstevel@tonic-gate 			if (mp->in_sibling == NULL) {
11270Sstevel@tonic-gate 				mp->in_sibling = np;
11280Sstevel@tonic-gate 				break;
11290Sstevel@tonic-gate 			}
11300Sstevel@tonic-gate 	}
11310Sstevel@tonic-gate 	np->in_parent = ap;
11320Sstevel@tonic-gate }
11330Sstevel@tonic-gate 
11340Sstevel@tonic-gate /*
11350Sstevel@tonic-gate  * Insert drv entry dp onto a node's driver list
11360Sstevel@tonic-gate  */
11370Sstevel@tonic-gate static void
in_endrv(in_node_t * np,in_drv_t * dp)11380Sstevel@tonic-gate in_endrv(in_node_t *np, in_drv_t *dp)
11390Sstevel@tonic-gate {
11400Sstevel@tonic-gate 	in_drv_t *mp;
11410Sstevel@tonic-gate 	ASSERT(e_ddi_inst_state.ins_busy);
11420Sstevel@tonic-gate 	ASSERT(np && dp);
11430Sstevel@tonic-gate 	mp = np->in_drivers;
11440Sstevel@tonic-gate 	np->in_drivers = dp;
11450Sstevel@tonic-gate 	dp->ind_next_drv = mp;
11460Sstevel@tonic-gate 	dp->ind_node = np;
11470Sstevel@tonic-gate }
11480Sstevel@tonic-gate 
11490Sstevel@tonic-gate /*
11500Sstevel@tonic-gate  * Parse the next name out of the path, null terminate it and update cp.
11510Sstevel@tonic-gate  * caller has copied string so we can mess with it.
11520Sstevel@tonic-gate  * Upon return *cpp points to the next section to be parsed, *addrp points
11530Sstevel@tonic-gate  * to the current address substring (or NULL if none) and we return the
11540Sstevel@tonic-gate  * current name substring (or NULL if none).  name and address substrings
11550Sstevel@tonic-gate  * are null terminated in place.
11560Sstevel@tonic-gate  */
11570Sstevel@tonic-gate 
11580Sstevel@tonic-gate static char *
in_name_addr(char ** cpp,char ** addrp)11590Sstevel@tonic-gate in_name_addr(char **cpp, char **addrp)
11600Sstevel@tonic-gate {
11610Sstevel@tonic-gate 	char *namep;	/* return value holder */
11620Sstevel@tonic-gate 	char *ap;	/* pointer to '@' in string */
11630Sstevel@tonic-gate 	char *sp;	/* pointer to '/' in string */
11640Sstevel@tonic-gate 
11650Sstevel@tonic-gate 	if (*cpp == NULL || **cpp == '\0') {
11660Sstevel@tonic-gate 		*addrp = NULL;
11670Sstevel@tonic-gate 		return (NULL);
11680Sstevel@tonic-gate 	}
11690Sstevel@tonic-gate 	namep = *cpp;
11700Sstevel@tonic-gate 	sp = strchr(*cpp, '/');
11710Sstevel@tonic-gate 	if (sp != NULL) {	/* more to follow */
11720Sstevel@tonic-gate 		*sp = '\0';
11730Sstevel@tonic-gate 		*cpp = sp + 1;
11740Sstevel@tonic-gate 	} else {		/* this is last component. */
11750Sstevel@tonic-gate 		*cpp = NULL;
11760Sstevel@tonic-gate 	}
11770Sstevel@tonic-gate 	ap = strchr(namep, '@');
11780Sstevel@tonic-gate 	if (ap == NULL) {
11790Sstevel@tonic-gate 		*addrp = NULL;
11800Sstevel@tonic-gate 	} else {
11810Sstevel@tonic-gate 		*ap = '\0';		/* terminate the name */
11820Sstevel@tonic-gate 		*addrp = ap + 1;
11830Sstevel@tonic-gate 	}
11840Sstevel@tonic-gate 	return (namep);
11850Sstevel@tonic-gate }
11860Sstevel@tonic-gate 
11870Sstevel@tonic-gate /*
11880Sstevel@tonic-gate  * Allocate a node and storage for name and addr strings, and fill them in.
11890Sstevel@tonic-gate  */
11900Sstevel@tonic-gate static in_node_t *
in_alloc_node(char * name,char * addr)11910Sstevel@tonic-gate in_alloc_node(char *name, char *addr)
11920Sstevel@tonic-gate {
11930Sstevel@tonic-gate 	in_node_t *np;
11940Sstevel@tonic-gate 	char *cp;
11950Sstevel@tonic-gate 	size_t namelen;
11960Sstevel@tonic-gate 
11970Sstevel@tonic-gate 	ASSERT(e_ddi_inst_state.ins_busy);
11980Sstevel@tonic-gate 	/*
11990Sstevel@tonic-gate 	 * Has name or will become root
12000Sstevel@tonic-gate 	 */
12010Sstevel@tonic-gate 	ASSERT(name || e_ddi_inst_state.ins_root == NULL);
12020Sstevel@tonic-gate 	if (addr == NULL)
12030Sstevel@tonic-gate 		addr = "";
12040Sstevel@tonic-gate 	if (name == NULL)
12050Sstevel@tonic-gate 		namelen = 0;
12060Sstevel@tonic-gate 	else
12070Sstevel@tonic-gate 		namelen = strlen(name) + 1;
12080Sstevel@tonic-gate 	cp = kmem_zalloc(sizeof (in_node_t) + namelen + strlen(addr) + 1,
12090Sstevel@tonic-gate 	    KM_SLEEP);
12100Sstevel@tonic-gate 	np = (in_node_t *)cp;
12110Sstevel@tonic-gate 	if (name) {
12120Sstevel@tonic-gate 		np->in_node_name = cp + sizeof (in_node_t);
12130Sstevel@tonic-gate 		(void) strcpy(np->in_node_name, name);
12140Sstevel@tonic-gate 	}
12150Sstevel@tonic-gate 	np->in_unit_addr = cp + sizeof (in_node_t) + namelen;
12160Sstevel@tonic-gate 	(void) strcpy(np->in_unit_addr, addr);
12170Sstevel@tonic-gate 	return (np);
12180Sstevel@tonic-gate }
12190Sstevel@tonic-gate 
12200Sstevel@tonic-gate /*
12210Sstevel@tonic-gate  * Allocate a drv entry and storage for binding name string, and fill it in.
12220Sstevel@tonic-gate  */
12230Sstevel@tonic-gate static in_drv_t *
in_alloc_drv(char * bindingname)12240Sstevel@tonic-gate in_alloc_drv(char *bindingname)
12250Sstevel@tonic-gate {
12260Sstevel@tonic-gate 	in_drv_t *dp;
12270Sstevel@tonic-gate 	char *cp;
12280Sstevel@tonic-gate 	size_t namelen;
12290Sstevel@tonic-gate 
12300Sstevel@tonic-gate 	ASSERT(e_ddi_inst_state.ins_busy);
12310Sstevel@tonic-gate 	/*
12320Sstevel@tonic-gate 	 * Has name or will become root
12330Sstevel@tonic-gate 	 */
12340Sstevel@tonic-gate 	ASSERT(bindingname || e_ddi_inst_state.ins_root == NULL);
12350Sstevel@tonic-gate 	if (bindingname == NULL)
12360Sstevel@tonic-gate 		namelen = 0;
12370Sstevel@tonic-gate 	else
12380Sstevel@tonic-gate 		namelen = strlen(bindingname) + 1;
12390Sstevel@tonic-gate 	cp = kmem_zalloc(sizeof (in_drv_t) + namelen, KM_SLEEP);
12400Sstevel@tonic-gate 	dp = (in_drv_t *)cp;
12410Sstevel@tonic-gate 	if (bindingname) {
12420Sstevel@tonic-gate 		dp->ind_driver_name = cp + sizeof (in_drv_t);
12430Sstevel@tonic-gate 		(void) strcpy(dp->ind_driver_name, bindingname);
12440Sstevel@tonic-gate 	}
12450Sstevel@tonic-gate 	dp->ind_state = IN_UNKNOWN;
12460Sstevel@tonic-gate 	dp->ind_instance = -1;
12470Sstevel@tonic-gate 	return (dp);
12480Sstevel@tonic-gate }
12490Sstevel@tonic-gate 
12500Sstevel@tonic-gate static void
in_dealloc_node(in_node_t * np)12510Sstevel@tonic-gate in_dealloc_node(in_node_t *np)
12520Sstevel@tonic-gate {
12530Sstevel@tonic-gate 	/*
12540Sstevel@tonic-gate 	 * The root node can never be de-allocated
12550Sstevel@tonic-gate 	 */
12560Sstevel@tonic-gate 	ASSERT(np->in_node_name && np->in_unit_addr);
12570Sstevel@tonic-gate 	ASSERT(e_ddi_inst_state.ins_busy);
12580Sstevel@tonic-gate 	kmem_free(np, sizeof (in_node_t) + strlen(np->in_node_name)
12590Sstevel@tonic-gate 	    + strlen(np->in_unit_addr) + 2);
12600Sstevel@tonic-gate }
12610Sstevel@tonic-gate 
12620Sstevel@tonic-gate static void
in_dealloc_drv(in_drv_t * dp)12630Sstevel@tonic-gate in_dealloc_drv(in_drv_t *dp)
12640Sstevel@tonic-gate {
12650Sstevel@tonic-gate 	ASSERT(dp->ind_driver_name);
12660Sstevel@tonic-gate 	ASSERT(e_ddi_inst_state.ins_busy);
12670Sstevel@tonic-gate 	kmem_free(dp, sizeof (in_drv_t) + strlen(dp->ind_driver_name)
12680Sstevel@tonic-gate 	    + 1);
12690Sstevel@tonic-gate }
12700Sstevel@tonic-gate 
12710Sstevel@tonic-gate /*
12720Sstevel@tonic-gate  * Handle the various possible versions of "no address"
12730Sstevel@tonic-gate  */
12740Sstevel@tonic-gate static int
in_eqstr(char * a,char * b)12750Sstevel@tonic-gate in_eqstr(char *a, char *b)
12760Sstevel@tonic-gate {
12770Sstevel@tonic-gate 	if (a == b)	/* covers case where both are nulls */
12780Sstevel@tonic-gate 		return (1);
12790Sstevel@tonic-gate 	if (a == NULL && *b == 0)
12800Sstevel@tonic-gate 		return (1);
12810Sstevel@tonic-gate 	if (b == NULL && *a == 0)
12820Sstevel@tonic-gate 		return (1);
12830Sstevel@tonic-gate 	if (a == NULL || b == NULL)
12840Sstevel@tonic-gate 		return (0);
12850Sstevel@tonic-gate 	return (strcmp(a, b) == 0);
12860Sstevel@tonic-gate }
12870Sstevel@tonic-gate 
12880Sstevel@tonic-gate /*
12890Sstevel@tonic-gate  * Returns true if instance no. is already in use by named driver
12900Sstevel@tonic-gate  */
12910Sstevel@tonic-gate static int
in_inuse(int instance,char * name)12920Sstevel@tonic-gate in_inuse(int instance, char *name)
12930Sstevel@tonic-gate {
12940Sstevel@tonic-gate 	major_t major;
12950Sstevel@tonic-gate 	in_drv_t *dp;
12960Sstevel@tonic-gate 	struct devnames *dnp;
12970Sstevel@tonic-gate 
12980Sstevel@tonic-gate 	ASSERT(e_ddi_inst_state.ins_busy);
12990Sstevel@tonic-gate 	/*
13000Sstevel@tonic-gate 	 * For now, if we've never heard of this device we assume it is not
13010Sstevel@tonic-gate 	 * in use, since we can't tell
13020Sstevel@tonic-gate 	 * XXX could do the weaker search through the nomajor list checking
13030Sstevel@tonic-gate 	 * XXX for the same name
13040Sstevel@tonic-gate 	 */
13057009Scth 	if ((major = ddi_name_to_major(name)) == DDI_MAJOR_T_NONE)
13060Sstevel@tonic-gate 		return (0);
13070Sstevel@tonic-gate 	dnp = &devnamesp[major];
13080Sstevel@tonic-gate 
13090Sstevel@tonic-gate 	dp = dnp->dn_inlist;
13100Sstevel@tonic-gate 	while (dp) {
13110Sstevel@tonic-gate 		if (dp->ind_instance == instance)
13120Sstevel@tonic-gate 			return (1);
13130Sstevel@tonic-gate 		dp = dp->ind_next;
13140Sstevel@tonic-gate 	}
13150Sstevel@tonic-gate 	return (0);
13160Sstevel@tonic-gate }
13170Sstevel@tonic-gate 
13180Sstevel@tonic-gate static void
in_hashdrv(in_drv_t * dp)13190Sstevel@tonic-gate in_hashdrv(in_drv_t *dp)
13200Sstevel@tonic-gate {
13210Sstevel@tonic-gate 	struct devnames *dnp;
13220Sstevel@tonic-gate 	in_drv_t *mp, *pp;
13230Sstevel@tonic-gate 	major_t major;
13240Sstevel@tonic-gate 
13250Sstevel@tonic-gate 	/* hash to no major list */
13267009Scth 	major = ddi_name_to_major(dp->ind_driver_name);
13277009Scth 	if (major == DDI_MAJOR_T_NONE) {
13280Sstevel@tonic-gate 		dp->ind_next = e_ddi_inst_state.ins_no_major;
13290Sstevel@tonic-gate 		e_ddi_inst_state.ins_no_major = dp;
13300Sstevel@tonic-gate 		return;
13310Sstevel@tonic-gate 	}
13320Sstevel@tonic-gate 
13330Sstevel@tonic-gate 	/*
13340Sstevel@tonic-gate 	 * dnp->dn_inlist is sorted by instance number.
13350Sstevel@tonic-gate 	 * Adding a new instance entry may introduce holes,
13360Sstevel@tonic-gate 	 * set dn_instance to IN_SEARCHME so the next instance
13370Sstevel@tonic-gate 	 * assignment may fill in holes.
13380Sstevel@tonic-gate 	 */
13390Sstevel@tonic-gate 	dnp = &devnamesp[major];
13400Sstevel@tonic-gate 	pp = mp = dnp->dn_inlist;
13410Sstevel@tonic-gate 	if (mp == NULL || dp->ind_instance < mp->ind_instance) {
13420Sstevel@tonic-gate 		/* prepend as the first entry, turn on IN_SEARCHME */
13430Sstevel@tonic-gate 		dnp->dn_instance = IN_SEARCHME;
13440Sstevel@tonic-gate 		dp->ind_next = mp;
13450Sstevel@tonic-gate 		dnp->dn_inlist = dp;
13460Sstevel@tonic-gate 		return;
13470Sstevel@tonic-gate 	}
13480Sstevel@tonic-gate 
13490Sstevel@tonic-gate 	ASSERT(mp->ind_instance != dp->ind_instance);
13500Sstevel@tonic-gate 	while (mp->ind_instance < dp->ind_instance && mp->ind_next) {
13510Sstevel@tonic-gate 		pp = mp;
13520Sstevel@tonic-gate 		mp = mp->ind_next;
13530Sstevel@tonic-gate 		ASSERT(mp->ind_instance != dp->ind_instance);
13540Sstevel@tonic-gate 	}
13550Sstevel@tonic-gate 
13560Sstevel@tonic-gate 	if (mp->ind_instance < dp->ind_instance) { /* end of list */
13570Sstevel@tonic-gate 		dp->ind_next = NULL;
13580Sstevel@tonic-gate 		mp->ind_next = dp;
13590Sstevel@tonic-gate 	} else {
13600Sstevel@tonic-gate 		dp->ind_next = pp->ind_next;
13610Sstevel@tonic-gate 		pp->ind_next = dp;
13620Sstevel@tonic-gate 	}
13630Sstevel@tonic-gate }
13640Sstevel@tonic-gate 
13650Sstevel@tonic-gate /*
13660Sstevel@tonic-gate  * Remove a driver entry from the list, given a previous pointer
13670Sstevel@tonic-gate  */
13680Sstevel@tonic-gate static void
in_removedrv(struct devnames * dnp,in_drv_t * mp)13690Sstevel@tonic-gate in_removedrv(struct devnames *dnp, in_drv_t *mp)
13700Sstevel@tonic-gate {
13710Sstevel@tonic-gate 	in_drv_t *dp;
13720Sstevel@tonic-gate 	in_drv_t *prevp;
13730Sstevel@tonic-gate 
13740Sstevel@tonic-gate 	if (dnp->dn_inlist == mp) {	/* head of list */
13750Sstevel@tonic-gate 		dnp->dn_inlist = mp->ind_next;
13760Sstevel@tonic-gate 		dnp->dn_instance = IN_SEARCHME;
13770Sstevel@tonic-gate 		in_dq_drv(mp);
13780Sstevel@tonic-gate 		in_dealloc_drv(mp);
13790Sstevel@tonic-gate 		return;
13800Sstevel@tonic-gate 	}
13810Sstevel@tonic-gate 	prevp = dnp->dn_inlist;
13820Sstevel@tonic-gate 	for (dp = prevp->ind_next; dp; dp = dp->ind_next) {
13830Sstevel@tonic-gate 		if (dp == mp) {		/* found it */
13840Sstevel@tonic-gate 			break;
13850Sstevel@tonic-gate 		}
13860Sstevel@tonic-gate 		prevp = dp;
13870Sstevel@tonic-gate 	}
13880Sstevel@tonic-gate 
13890Sstevel@tonic-gate 	ASSERT(dp == mp);
13900Sstevel@tonic-gate 	dnp->dn_instance = IN_SEARCHME;
13910Sstevel@tonic-gate 	prevp->ind_next = mp->ind_next;
13920Sstevel@tonic-gate 	in_dq_drv(mp);
13930Sstevel@tonic-gate 	in_dealloc_drv(mp);
13940Sstevel@tonic-gate }
13950Sstevel@tonic-gate 
13960Sstevel@tonic-gate static void
in_dq_drv(in_drv_t * mp)13970Sstevel@tonic-gate in_dq_drv(in_drv_t *mp)
13980Sstevel@tonic-gate {
13990Sstevel@tonic-gate 	struct in_node *node = mp->ind_node;
14000Sstevel@tonic-gate 	in_drv_t *ptr, *prev;
14010Sstevel@tonic-gate 
14020Sstevel@tonic-gate 	if (mp == node->in_drivers) {
14030Sstevel@tonic-gate 		node->in_drivers = mp->ind_next_drv;
14040Sstevel@tonic-gate 		return;
14050Sstevel@tonic-gate 	}
14060Sstevel@tonic-gate 	prev = node->in_drivers;
14070Sstevel@tonic-gate 	for (ptr = prev->ind_next_drv; ptr != (struct in_drv *)NULL;
14080Sstevel@tonic-gate 	    ptr = ptr->ind_next_drv) {
14090Sstevel@tonic-gate 		if (ptr == mp) {
14100Sstevel@tonic-gate 			prev->ind_next_drv = ptr->ind_next_drv;
14110Sstevel@tonic-gate 			return;
14120Sstevel@tonic-gate 		}
14137235Svikram 		prev = ptr;
14140Sstevel@tonic-gate 	}
14150Sstevel@tonic-gate 	panic("in_dq_drv: in_drv not found on node driver list");
14160Sstevel@tonic-gate }
14170Sstevel@tonic-gate 
14180Sstevel@tonic-gate 
14190Sstevel@tonic-gate in_drv_t *
in_drvwalk(in_node_t * np,char * binding_name)14200Sstevel@tonic-gate in_drvwalk(in_node_t *np, char *binding_name)
14210Sstevel@tonic-gate {
14220Sstevel@tonic-gate 	char *name;
14230Sstevel@tonic-gate 	in_drv_t *dp = np->in_drivers;
14240Sstevel@tonic-gate 	while (dp) {
14250Sstevel@tonic-gate 		if ((name = i_binding_to_drv_name(dp->ind_driver_name))
14260Sstevel@tonic-gate 		    == NULL) {
14270Sstevel@tonic-gate 			name = dp->ind_driver_name;
14280Sstevel@tonic-gate 		}
14290Sstevel@tonic-gate 		if (strcmp(binding_name, name) == 0) {
14300Sstevel@tonic-gate 			break;
14310Sstevel@tonic-gate 		}
14320Sstevel@tonic-gate 		dp = dp->ind_next_drv;
14330Sstevel@tonic-gate 	}
14340Sstevel@tonic-gate 	return (dp);
14350Sstevel@tonic-gate }
14360Sstevel@tonic-gate 
14370Sstevel@tonic-gate 
14380Sstevel@tonic-gate 
14390Sstevel@tonic-gate static void
i_log_devfs_instance_mod(void)14400Sstevel@tonic-gate i_log_devfs_instance_mod(void)
14410Sstevel@tonic-gate {
14423250Scth 	sysevent_t	*ev;
14433250Scth 	sysevent_id_t	eid;
14443250Scth 	static int	sent_one = 0;
14450Sstevel@tonic-gate 
14460Sstevel@tonic-gate 	/*
14473250Scth 	 * Prevent unnecessary event generation.  Do not generate more than
14483250Scth 	 * one event during boot.
14490Sstevel@tonic-gate 	 */
14503250Scth 	if (sent_one && !i_ddi_io_initialized())
14510Sstevel@tonic-gate 		return;
14520Sstevel@tonic-gate 
14530Sstevel@tonic-gate 	ev = sysevent_alloc(EC_DEVFS, ESC_DEVFS_INSTANCE_MOD, EP_DDI,
14545648Ssetje 	    SE_NOSLEEP);
14550Sstevel@tonic-gate 	if (ev == NULL) {
14560Sstevel@tonic-gate 		return;
14570Sstevel@tonic-gate 	}
14580Sstevel@tonic-gate 	if (log_sysevent(ev, SE_NOSLEEP, &eid) != 0) {
14590Sstevel@tonic-gate 		cmn_err(CE_WARN, "i_log_devfs_instance_mod: failed to post "
14605648Ssetje 		    "event");
14613250Scth 	} else {
14623250Scth 		sent_one = 1;
14630Sstevel@tonic-gate 	}
14640Sstevel@tonic-gate 	sysevent_free(ev);
14650Sstevel@tonic-gate }
14660Sstevel@tonic-gate 
14670Sstevel@tonic-gate void
e_ddi_enter_instance(void)146812116SVikram.Hegde@Sun.COM e_ddi_enter_instance(void)
14690Sstevel@tonic-gate {
14700Sstevel@tonic-gate 	mutex_enter(&e_ddi_inst_state.ins_serial);
14710Sstevel@tonic-gate 	if (e_ddi_inst_state.ins_thread == curthread)
14720Sstevel@tonic-gate 		e_ddi_inst_state.ins_busy++;
14730Sstevel@tonic-gate 	else {
14740Sstevel@tonic-gate 		while (e_ddi_inst_state.ins_busy)
14750Sstevel@tonic-gate 			cv_wait(&e_ddi_inst_state.ins_serial_cv,
14760Sstevel@tonic-gate 			    &e_ddi_inst_state.ins_serial);
14770Sstevel@tonic-gate 		e_ddi_inst_state.ins_thread = curthread;
14780Sstevel@tonic-gate 		e_ddi_inst_state.ins_busy = 1;
14790Sstevel@tonic-gate 	}
14800Sstevel@tonic-gate 	mutex_exit(&e_ddi_inst_state.ins_serial);
14810Sstevel@tonic-gate }
14820Sstevel@tonic-gate 
14830Sstevel@tonic-gate void
e_ddi_exit_instance(void)148412116SVikram.Hegde@Sun.COM e_ddi_exit_instance(void)
14850Sstevel@tonic-gate {
14860Sstevel@tonic-gate 	mutex_enter(&e_ddi_inst_state.ins_serial);
14870Sstevel@tonic-gate 	e_ddi_inst_state.ins_busy--;
14880Sstevel@tonic-gate 	if (e_ddi_inst_state.ins_busy == 0) {
14890Sstevel@tonic-gate 		cv_broadcast(&e_ddi_inst_state.ins_serial_cv);
14900Sstevel@tonic-gate 		e_ddi_inst_state.ins_thread = NULL;
14910Sstevel@tonic-gate 	}
14920Sstevel@tonic-gate 	mutex_exit(&e_ddi_inst_state.ins_serial);
14930Sstevel@tonic-gate }
14940Sstevel@tonic-gate 
14950Sstevel@tonic-gate int
e_ddi_instance_is_clean(void)149612116SVikram.Hegde@Sun.COM e_ddi_instance_is_clean(void)
14970Sstevel@tonic-gate {
149812116SVikram.Hegde@Sun.COM 	return (e_ddi_inst_state.ins_dirty == B_FALSE);
14990Sstevel@tonic-gate }
15000Sstevel@tonic-gate 
15010Sstevel@tonic-gate void
e_ddi_instance_set_clean(void)150212116SVikram.Hegde@Sun.COM e_ddi_instance_set_clean(void)
15030Sstevel@tonic-gate {
150412116SVikram.Hegde@Sun.COM 	e_ddi_inst_state.ins_dirty = B_FALSE;
15050Sstevel@tonic-gate }
15060Sstevel@tonic-gate 
15070Sstevel@tonic-gate in_node_t *
e_ddi_instance_root(void)150812116SVikram.Hegde@Sun.COM e_ddi_instance_root(void)
15090Sstevel@tonic-gate {
15100Sstevel@tonic-gate 	return (e_ddi_inst_state.ins_root);
15110Sstevel@tonic-gate }
15120Sstevel@tonic-gate 
15130Sstevel@tonic-gate /*
15140Sstevel@tonic-gate  * Visit a node in the instance tree
15150Sstevel@tonic-gate  */
15160Sstevel@tonic-gate static int
in_walk_instances(in_node_t * np,char * path,char * this,int (* f)(const char *,in_node_t *,in_drv_t *,void *),void * arg)15170Sstevel@tonic-gate in_walk_instances(in_node_t *np, char *path, char *this,
15180Sstevel@tonic-gate     int (*f)(const char *, in_node_t *, in_drv_t *, void *), void *arg)
15190Sstevel@tonic-gate {
15200Sstevel@tonic-gate 	in_drv_t *dp;
15210Sstevel@tonic-gate 	int rval = INST_WALK_CONTINUE;
15220Sstevel@tonic-gate 	char *next;
15230Sstevel@tonic-gate 
15240Sstevel@tonic-gate 	while (np != NULL) {
15250Sstevel@tonic-gate 
15260Sstevel@tonic-gate 		if (np->in_unit_addr[0] == 0)
15270Sstevel@tonic-gate 			(void) sprintf(this, "/%s", np->in_node_name);
15280Sstevel@tonic-gate 		else
15290Sstevel@tonic-gate 			(void) sprintf(this, "/%s@%s", np->in_node_name,
15300Sstevel@tonic-gate 			    np->in_unit_addr);
15310Sstevel@tonic-gate 		next = this + strlen(this);
15320Sstevel@tonic-gate 
15330Sstevel@tonic-gate 		for (dp = np->in_drivers; dp; dp = dp->ind_next_drv) {
15340Sstevel@tonic-gate 			if (dp->ind_state == IN_PERMANENT) {
15350Sstevel@tonic-gate 				rval = (*f)(path, np, dp, arg);
15360Sstevel@tonic-gate 				if (rval == INST_WALK_TERMINATE)
15370Sstevel@tonic-gate 					break;
15380Sstevel@tonic-gate 			}
15390Sstevel@tonic-gate 		}
154012116SVikram.Hegde@Sun.COM 
15410Sstevel@tonic-gate 		if (np->in_child) {
15420Sstevel@tonic-gate 			rval = in_walk_instances(np->in_child,
15430Sstevel@tonic-gate 			    path, next, f, arg);
15440Sstevel@tonic-gate 			if (rval == INST_WALK_TERMINATE)
15450Sstevel@tonic-gate 				break;
15460Sstevel@tonic-gate 		}
15470Sstevel@tonic-gate 
15480Sstevel@tonic-gate 		np = np->in_sibling;
15490Sstevel@tonic-gate 	}
15500Sstevel@tonic-gate 
15510Sstevel@tonic-gate 	return (rval);
15520Sstevel@tonic-gate }
15530Sstevel@tonic-gate 
15540Sstevel@tonic-gate /*
15550Sstevel@tonic-gate  * A general interface for walking the instance tree,
15560Sstevel@tonic-gate  * calling a user-supplied callback for each node.
15570Sstevel@tonic-gate  */
15580Sstevel@tonic-gate int
e_ddi_walk_instances(int (* f)(const char *,in_node_t *,in_drv_t *,void *),void * arg)15590Sstevel@tonic-gate e_ddi_walk_instances(int (*f)(const char *,
15600Sstevel@tonic-gate 	in_node_t *, in_drv_t *, void *), void *arg)
15610Sstevel@tonic-gate {
15620Sstevel@tonic-gate 	in_node_t *root;
15630Sstevel@tonic-gate 	int rval;
15640Sstevel@tonic-gate 	char *path;
15650Sstevel@tonic-gate 
15660Sstevel@tonic-gate 	path = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
15670Sstevel@tonic-gate 
15680Sstevel@tonic-gate 	e_ddi_enter_instance();
15690Sstevel@tonic-gate 	root = e_ddi_instance_root();
15700Sstevel@tonic-gate 	rval = in_walk_instances(root->in_child, path, path, f, arg);
157112116SVikram.Hegde@Sun.COM 
15720Sstevel@tonic-gate 	e_ddi_exit_instance();
15730Sstevel@tonic-gate 
15740Sstevel@tonic-gate 	kmem_free(path, MAXPATHLEN);
15750Sstevel@tonic-gate 	return (rval);
15760Sstevel@tonic-gate }
157712116SVikram.Hegde@Sun.COM 
157812116SVikram.Hegde@Sun.COM in_node_t *
e_ddi_path_to_instance(char * path)157912116SVikram.Hegde@Sun.COM e_ddi_path_to_instance(char *path)
158012116SVikram.Hegde@Sun.COM {
158112116SVikram.Hegde@Sun.COM 	in_node_t *np;
158212116SVikram.Hegde@Sun.COM 
158312116SVikram.Hegde@Sun.COM 	np = in_make_path(path);
158412116SVikram.Hegde@Sun.COM 	if (np && np->in_drivers && np->in_drivers->ind_state == IN_PERMANENT) {
158512116SVikram.Hegde@Sun.COM 		return (np);
158612116SVikram.Hegde@Sun.COM 	}
158712116SVikram.Hegde@Sun.COM 	return (NULL);
158812116SVikram.Hegde@Sun.COM }
158912116SVikram.Hegde@Sun.COM 
159012116SVikram.Hegde@Sun.COM void
e_ddi_borrow_instance(dev_info_t * cdip,in_node_t * cnp)159112116SVikram.Hegde@Sun.COM e_ddi_borrow_instance(dev_info_t *cdip, in_node_t *cnp)
159212116SVikram.Hegde@Sun.COM {
159312116SVikram.Hegde@Sun.COM 	char		*alias;
159412116SVikram.Hegde@Sun.COM 	in_node_t	*anp;
159512116SVikram.Hegde@Sun.COM 	char		*curr = kmem_alloc(MAXPATHLEN, KM_NOSLEEP);
159612116SVikram.Hegde@Sun.COM 
159712116SVikram.Hegde@Sun.COM 	if (curr == NULL) {
159812116SVikram.Hegde@Sun.COM 		ddi_err(DER_PANIC, cdip, "curr alloc failed");
159912116SVikram.Hegde@Sun.COM 		/*NOTREACHED*/
160012116SVikram.Hegde@Sun.COM 	}
160112116SVikram.Hegde@Sun.COM 
160212116SVikram.Hegde@Sun.COM 	(void) ddi_pathname(cdip, curr);
160312116SVikram.Hegde@Sun.COM 
160412116SVikram.Hegde@Sun.COM 	if (cnp->in_drivers) {
160512701SJan.Setje-Eilers@Sun.COM 		/* there can be multiple drivers bound */
160612701SJan.Setje-Eilers@Sun.COM 		ddi_err(DER_LOG, cdip, "%s has previous binding: %s", curr,
160712701SJan.Setje-Eilers@Sun.COM 		    cnp->in_drivers->ind_driver_name);
160812116SVikram.Hegde@Sun.COM 	}
160912116SVikram.Hegde@Sun.COM 
161012116SVikram.Hegde@Sun.COM 	alias = ddi_curr_redirect(curr);
161112701SJan.Setje-Eilers@Sun.COM 
161212701SJan.Setje-Eilers@Sun.COM 	/* bail here if the alias matches any other current path or itself */
161312701SJan.Setje-Eilers@Sun.COM 	if (alias && ((strcmp(curr, alias) == 0) ||
161412701SJan.Setje-Eilers@Sun.COM 	    (ddi_curr_redirect(alias) != 0))) {
161512701SJan.Setje-Eilers@Sun.COM 		DDI_MP_DBG((CE_NOTE, "not borrowing current: %s alias: %s",
161612701SJan.Setje-Eilers@Sun.COM 		    curr, alias));
161712701SJan.Setje-Eilers@Sun.COM 		goto out;
161812701SJan.Setje-Eilers@Sun.COM 	}
161912116SVikram.Hegde@Sun.COM 
162012116SVikram.Hegde@Sun.COM 	if (alias && (anp = e_ddi_path_to_instance(alias)) != NULL) {
1621*13000SJan.Setje-Eilers@Sun.COM 		/*
1622*13000SJan.Setje-Eilers@Sun.COM 		 * Since pcieb nodes can split and merge, it is dangerous
1623*13000SJan.Setje-Eilers@Sun.COM 		 * to borrow and instance for them. However since they do
1624*13000SJan.Setje-Eilers@Sun.COM 		 * not expose their instance numbers it is safe to never
1625*13000SJan.Setje-Eilers@Sun.COM 		 * borrow one.
1626*13000SJan.Setje-Eilers@Sun.COM 		 */
1627*13000SJan.Setje-Eilers@Sun.COM 		if (anp->in_drivers->ind_driver_name &&
1628*13000SJan.Setje-Eilers@Sun.COM 		    (strcmp(anp->in_drivers->ind_driver_name, "pcieb") == 0)) {
1629*13000SJan.Setje-Eilers@Sun.COM 			DDI_MP_DBG((CE_NOTE, "not borrowing pcieb: "
1630*13000SJan.Setje-Eilers@Sun.COM 			    "%s alias: %s", curr, alias));
1631*13000SJan.Setje-Eilers@Sun.COM 			goto out;
1632*13000SJan.Setje-Eilers@Sun.COM 		}
163312701SJan.Setje-Eilers@Sun.COM 		DDI_MP_DBG((CE_NOTE, "borrowing current: %s alias: %s",
163412701SJan.Setje-Eilers@Sun.COM 		    curr, alias));
163512116SVikram.Hegde@Sun.COM 		cnp->in_drivers = anp->in_drivers;
163612116SVikram.Hegde@Sun.COM 		anp->in_drivers = NULL;
163712116SVikram.Hegde@Sun.COM 	}
163812701SJan.Setje-Eilers@Sun.COM out:
163912701SJan.Setje-Eilers@Sun.COM 	kmem_free(curr, MAXPATHLEN);
164012116SVikram.Hegde@Sun.COM }
164112116SVikram.Hegde@Sun.COM 
164212116SVikram.Hegde@Sun.COM void
e_ddi_return_instance(dev_info_t * cdip,char * addr,in_node_t * cnp)164312116SVikram.Hegde@Sun.COM e_ddi_return_instance(dev_info_t *cdip, char *addr, in_node_t *cnp)
164412116SVikram.Hegde@Sun.COM {
164512116SVikram.Hegde@Sun.COM 	in_node_t	*anp;
164612116SVikram.Hegde@Sun.COM 	char 		*alias;
164712116SVikram.Hegde@Sun.COM 	char		*curr = kmem_alloc(MAXPATHLEN, KM_NOSLEEP);
164812116SVikram.Hegde@Sun.COM 
164912116SVikram.Hegde@Sun.COM 	if (curr == NULL) {
165012116SVikram.Hegde@Sun.COM 		ddi_err(DER_PANIC, cdip, "alloc of curr failed");
165112116SVikram.Hegde@Sun.COM 		/*NOTREACHED*/
165212116SVikram.Hegde@Sun.COM 	}
165312116SVikram.Hegde@Sun.COM 
165412116SVikram.Hegde@Sun.COM 	(void) ddi_pathname(cdip, curr);
165512116SVikram.Hegde@Sun.COM 	if (addr) {
165612116SVikram.Hegde@Sun.COM 		(void) strlcat(curr, "@", MAXPATHLEN);
165712116SVikram.Hegde@Sun.COM 		(void) strlcat(curr, addr, MAXPATHLEN);
165812116SVikram.Hegde@Sun.COM 
165912116SVikram.Hegde@Sun.COM 	}
166012116SVikram.Hegde@Sun.COM 	if (cnp->in_drivers == NULL) {
166112116SVikram.Hegde@Sun.COM 		ddi_err(DER_PANIC, cdip, "cnp has no inst: %p", cnp);
166212116SVikram.Hegde@Sun.COM 		/*NOTREACHED*/
166312116SVikram.Hegde@Sun.COM 	}
166412116SVikram.Hegde@Sun.COM 
166512116SVikram.Hegde@Sun.COM 	alias = ddi_curr_redirect(curr);
166612116SVikram.Hegde@Sun.COM 	kmem_free(curr, MAXPATHLEN);
166712116SVikram.Hegde@Sun.COM 
166812116SVikram.Hegde@Sun.COM 	if (alias && (anp = e_ddi_path_to_instance(alias)) != NULL) {
166912116SVikram.Hegde@Sun.COM 		ASSERT(anp->in_drivers == NULL);
167012116SVikram.Hegde@Sun.COM 		anp->in_drivers = cnp->in_drivers;
167112116SVikram.Hegde@Sun.COM 		cnp->in_drivers = NULL;
167212116SVikram.Hegde@Sun.COM 	}
167312116SVikram.Hegde@Sun.COM }
1674