xref: /titanic_41/usr/src/uts/common/os/instance.c (revision 15359501f7d4b9abebd7b7bf6efd5982a8e7eb27)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 1992, 2010, Oracle and/or its affiliates. All rights reserved.
23  */
24 
25 /*
26  * Instance number assignment code
27  */
28 
29 #include <sys/types.h>
30 #include <sys/param.h>
31 #include <sys/errno.h>
32 #include <sys/systm.h>
33 #include <sys/kobj.h>
34 #include <sys/t_lock.h>
35 #include <sys/kmem.h>
36 #include <sys/cmn_err.h>
37 #include <sys/ddi.h>
38 #include <sys/sunddi.h>
39 #include <sys/autoconf.h>
40 #include <sys/systeminfo.h>
41 #include <sys/hwconf.h>
42 #include <sys/reboot.h>
43 #include <sys/ddi_impldefs.h>
44 #include <sys/instance.h>
45 #include <sys/debug.h>
46 #include <sys/sysevent.h>
47 #include <sys/modctl.h>
48 #include <sys/console.h>
49 #include <sys/cladm.h>
50 #include <sys/sysmacros.h>
51 #include <sys/crc32.h>
52 
53 
54 static void in_preassign_instance(void);
55 static void i_log_devfs_instance_mod(void);
56 static int in_get_infile(char *);
57 static void in_removenode(struct devnames *dnp, in_node_t *mp, in_node_t *ap);
58 static in_node_t *in_alloc_node(char *name, char *addr);
59 static int in_eqstr(char *a, char *b);
60 static char *in_name_addr(char **cpp, char **addrp);
61 static in_node_t *in_devwalk(dev_info_t *dip, in_node_t **ap, char *addr);
62 static void in_dealloc_node(in_node_t *np);
63 static in_node_t *in_make_path(char *path);
64 static void in_enlist(in_node_t *ap, in_node_t *np);
65 static int in_inuse(int instance, char *name);
66 static void in_hashdrv(in_drv_t *dp);
67 static in_drv_t *in_drvwalk(in_node_t *np, char *binding_name);
68 static in_drv_t *in_alloc_drv(char *bindingname);
69 static void in_endrv(in_node_t *np, in_drv_t *dp);
70 static void in_dq_drv(in_drv_t *np);
71 static void in_removedrv(struct devnames *dnp, in_drv_t *mp);
72 static int in_pathin(char *cp, int instance, char *bname, struct bind **args);
73 static int in_next_instance_block(major_t, int);
74 static int in_next_instance(major_t);
75 
76 #pragma weak plat_ioaliases_init
77 
78 
79 /* external functions */
80 extern char *i_binding_to_drv_name(char *bname);
81 extern void plat_ioaliases_init(void);
82 
83 /*
84  * This plus devnames defines the entire software state of the instance world.
85  */
86 typedef struct in_softstate {
87 	in_node_t	*ins_root;	/* the root of our instance tree */
88 	in_drv_t	*ins_no_major;	/* majorless drv entries */
89 	/*
90 	 * Used to serialize access to data structures
91 	 */
92 	void		*ins_thread;
93 	kmutex_t	ins_serial;
94 	kcondvar_t	ins_serial_cv;
95 	int		ins_busy;
96 	boolean_t	ins_dirty;	/* instance info needs flush */
97 } in_softstate_t;
98 
99 static in_softstate_t e_ddi_inst_state;
100 
101 /*
102  * State transition information:
103  * e_ddi_inst_state contains, among other things, the root of a tree of
104  * device nodes used to track instance number assignments.
105  * Each device node may contain multiple driver bindings, represented
106  * by a linked list of in_drv_t nodes, each with an instance assignment
107  * (except for root node). Each in_drv node can be in one of 3 states,
108  * indicated by ind_state:
109  *
110  * IN_UNKNOWN:	Each node created in this state.  The instance number of
111  *	this node is not known.  ind_instance is set to -1.
112  * IN_PROVISIONAL:  When a node is assigned an instance number in
113  *	e_ddi_assign_instance(), its state is set to IN_PROVISIONAL.
114  *	Subsequently, the framework will always call either
115  *	e_ddi_keep_instance() which makes the node IN_PERMANENT
116  *	or e_ddi_free_instance(), which deletes the node.
117  * IN_PERMANENT:
118  *	If e_ddi_keep_instance() is called on an IN_PROVISIONAL node,
119  *	its state is set to IN_PERMANENT.
120  */
121 
122 static char *instance_file = INSTANCE_FILE;
123 static char *instance_file_backup = INSTANCE_FILE INSTANCE_FILE_SUFFIX;
124 
125 /*
126  * Return values for in_get_infile().
127  */
128 #define	PTI_FOUND	0
129 #define	PTI_NOT_FOUND	1
130 #define	PTI_REBUILD	2
131 
132 
133 /*
134  * Path to instance file magic string used for first time boot after
135  * an install.  If this is the first string in the file we will
136  * automatically rebuild the file.
137  */
138 #define	PTI_MAGIC_STR		"#path_to_inst_bootstrap_1"
139 #define	PTI_MAGIC_STR_LEN	(sizeof (PTI_MAGIC_STR) - 1)
140 
141 void
142 e_ddi_instance_init(void)
143 {
144 	char *file;
145 	int rebuild = 1;
146 	struct in_drv *dp;
147 
148 	mutex_init(&e_ddi_inst_state.ins_serial, NULL, MUTEX_DEFAULT, NULL);
149 	cv_init(&e_ddi_inst_state.ins_serial_cv, NULL, CV_DEFAULT, NULL);
150 
151 	/*
152 	 * Only one thread is allowed to change the state of the instance
153 	 * number assignments on the system at any given time.
154 	 * Note that this is not really necessary, as we are single-threaded
155 	 * here, but it won't hurt, and it allows us to keep ASSERTS for
156 	 * our assumptions in the code.
157 	 */
158 	e_ddi_enter_instance();
159 
160 	/*
161 	 * Init the ioaliases if the platform supports it
162 	 */
163 	if (&plat_ioaliases_init)
164 		plat_ioaliases_init();
165 
166 	/*
167 	 * Create the root node, instance zallocs to 0.
168 	 * The name and address of this node never get examined, we always
169 	 * start searching with its first child.
170 	 */
171 	ASSERT(e_ddi_inst_state.ins_root == NULL);
172 	e_ddi_inst_state.ins_root = in_alloc_node(NULL, NULL);
173 	dp = in_alloc_drv("rootnex");
174 	in_endrv(e_ddi_inst_state.ins_root, dp);
175 
176 	file = instance_file;
177 	switch (in_get_infile(file)) {
178 	default:
179 	case PTI_NOT_FOUND:
180 		/* make sure path_to_inst is recreated */
181 		boothowto |= RB_RECONFIG;
182 
183 		/*
184 		 * Something is wrong. First try the backup file.
185 		 * If not found, rebuild path_to_inst. Emit a
186 		 * message about the problem.
187 		 */
188 		cmn_err(CE_WARN, "%s empty or not found", file);
189 
190 		file = instance_file_backup;
191 		if (in_get_infile(file) != PTI_FOUND) {
192 			cmn_err(CE_NOTE, "rebuilding device instance data");
193 			break;
194 		}
195 		cmn_err(CE_NOTE, "using backup instance data in %s", file);
196 		/*FALLTHROUGH*/
197 
198 	case PTI_FOUND:
199 		/*
200 		 * We've got a readable file
201 		 * parse the file into the instance tree
202 		 */
203 		(void) read_binding_file(file, NULL, in_pathin);
204 		rebuild = 0;
205 		break;
206 
207 	case PTI_REBUILD:
208 		/*
209 		 * path_to_inst has magic str requesting a create
210 		 * Convert boot to reconfig boot to ensure /dev is
211 		 * in sync with new path_to_inst.
212 		 */
213 		boothowto |= RB_RECONFIG;
214 		cmn_err(CE_CONT,
215 		    "?Using default device instance data\n");
216 		break;
217 	}
218 
219 	/*
220 	 * The OBP device tree has been copied to the kernel and
221 	 * bound to drivers at this point. We walk the per-driver
222 	 * list to preassign instances. Since the bus addr is
223 	 * unknown at this point, we cannot place the instance
224 	 * number in the instance tree. This will be done at
225 	 * a later time.
226 	 */
227 	if (rebuild)
228 		in_preassign_instance();
229 
230 	e_ddi_exit_instance();
231 }
232 
233 static void
234 in_preassign_instance()
235 {
236 	major_t m;
237 	extern major_t devcnt;
238 
239 	for (m = 0; m < devcnt; m++) {
240 		struct devnames *dnp = &devnamesp[m];
241 		dev_info_t *dip = dnp->dn_head;
242 		while (dip) {
243 			DEVI(dip)->devi_instance = dnp->dn_instance;
244 			dnp->dn_instance++;
245 			dip = ddi_get_next(dip);
246 		}
247 	}
248 }
249 
250 /*
251  * Checks to see if the /etc/path_to_inst file exists and whether or not
252  * it has the magic string in it.
253  *
254  * Returns one of the following:
255  *
256  *	PTI_FOUND	- We have found the /etc/path_to_inst file
257  *	PTI_REBUILD	- We have found the /etc/path_to_inst file and the
258  *			  first line was PTI_MAGIC_STR.
259  *	PTI_NOT_FOUND	- We did not find the /etc/path_to_inst file
260  *
261  */
262 static int
263 in_get_infile(char *filename)
264 {
265 	struct _buf *file;
266 	int return_val;
267 	char buf[PTI_MAGIC_STR_LEN];
268 
269 	/*
270 	 * Try to open the file.
271 	 */
272 	if ((file = kobj_open_file(filename)) == (struct _buf *)-1) {
273 		return (PTI_NOT_FOUND);
274 	}
275 	return_val = PTI_FOUND;
276 
277 	/*
278 	 * Read the first PTI_MAGIC_STR_LEN bytes from the file to see if
279 	 * it contains the magic string.  If there aren't that many bytes
280 	 * in the file, then assume file is correct and no magic string
281 	 * and move on.
282 	 */
283 	switch (kobj_read_file(file, buf, PTI_MAGIC_STR_LEN, 0)) {
284 
285 	case PTI_MAGIC_STR_LEN:
286 		/*
287 		 * If the first PTI_MAGIC_STR_LEN bytes are the magic string
288 		 * then return PTI_REBUILD.
289 		 */
290 		if (strncmp(PTI_MAGIC_STR, buf, PTI_MAGIC_STR_LEN) == 0)
291 			return_val = PTI_REBUILD;
292 		break;
293 
294 	case 0:
295 		/*
296 		 * If the file is zero bytes in length, then consider the
297 		 * file to not be found
298 		 */
299 		return_val = PTI_NOT_FOUND;
300 
301 	default: /* Do nothing we have a good file */
302 		break;
303 	}
304 
305 	kobj_close_file(file);
306 	return (return_val);
307 }
308 
309 int
310 is_pseudo_device(dev_info_t *dip)
311 {
312 	dev_info_t	*pdip;
313 
314 	for (pdip = ddi_get_parent(dip); pdip && pdip != ddi_root_node();
315 	    pdip = ddi_get_parent(pdip)) {
316 		if (strcmp(ddi_get_name(pdip), DEVI_PSEUDO_NEXNAME) == 0)
317 			return (1);
318 	}
319 	return (0);
320 }
321 
322 
323 static void
324 in_set_instance(dev_info_t *dip, in_drv_t *dp, major_t major)
325 {
326 	/* use preassigned instance if available */
327 	if (DEVI(dip)->devi_instance != -1)
328 		dp->ind_instance = DEVI(dip)->devi_instance;
329 	else
330 		dp->ind_instance = in_next_instance(major);
331 }
332 
333 /*
334  * Return 1 if instance block was assigned for the path.
335  *
336  * For multi-port NIC cards, sequential instance assignment across all
337  * ports on a card is highly desirable since the ppa is typically the
338  * same as the instance number, and the ppa is used in the NIC's public
339  * /dev name. This sequential assignment typically occurs as a result
340  * of in_preassign_instance() after initial install, or by
341  * i_ndi_init_hw_children() for NIC ports that share a common parent.
342  *
343  * Some NIC cards however use multi-function bridge chips, and to
344  * support sequential instance assignment accross all ports, without
345  * disabling multi-threaded attach, we have a (currently) undocumented
346  * hack to allocate instance numbers in contiguous blocks based on
347  * driver.conf properties.
348  *
349  *                       ^
350  *           /----------   ------------\
351  *        pci@0                      pci@0,1	MULTI-FUNCTION BRIDGE CHIP
352  *       /     \                    /       \
353  * FJSV,e4ta@4  FJSV,e4ta@4,1   FJSV,e4ta@6 FJSV,e4ta@6,1	NIC PORTS
354  *      n            n+2             n+2         n+3		INSTANCE
355  *
356  * For the above example, the following driver.conf properties would be
357  * used to guarantee sequential instance number assignment.
358  *
359  * ddi-instance-blocks ="ib-FJSVe4ca", "ib-FJSVe4ta", "ib-generic";
360  * ib-FJSVe4ca =	"/pci@0/FJSV,e4ca@4", "/pci@0/FJSV,e4ca@4,1",
361  *			"/pci@0,1/FJSV,e4ca@6", "/pci@0,1/FJSV,e4ca@6,1";
362  * ib-FJSVe4ta =	"/pci@0/FJSV,e4ta@4", "/pci@0/FJSV,e4ta@4,1",
363  *			"/pci@0,1/FJSV,e4ta@6", "/pci@0,1/FJSV,e4ta@6,1";
364  * ib-generic =		"/pci@0/network@4", "/pci@0/network@4,1",
365  *			"/pci@0,1/network@6", "/pci@0,1/network@6,1";
366  *
367  * The value of the 'ddi-instance-blocks' property references a series
368  * of card specific properties, like 'ib-FJSV-e4ta', who's value
369  * defines a single 'instance block'.  The 'instance block' describes
370  * all the paths below a multi-function bridge, where each path is
371  * called an 'instance path'.  The 'instance block' property value is a
372  * series of 'instance paths'.  The number of 'instance paths' in an
373  * 'instance block' defines the size of the instance block, and the
374  * ordering of the 'instance paths' defines the instance number
375  * assignment order for paths going through the 'instance block'.
376  *
377  * In the instance assignment code below, if a (path, driver) that
378  * currently has no instance number has a path that goes through an
379  * 'instance block', then block instance number allocation occurs.  The
380  * block allocation code will find a sequential set of unused instance
381  * numbers, and assign instance numbers for all the paths in the
382  * 'instance block'.  Each path is assigned a persistent instance
383  * number, even paths that don't exist in the device tree or fail
384  * probe(9E).
385  */
386 static int
387 in_assign_instance_block(dev_info_t *dip)
388 {
389 	char		**ibn;		/* instance block names */
390 	uint_t		nibn;		/* number of instance block names */
391 	uint_t		ibni;		/* ibn index */
392 	char		*driver;
393 	major_t		major;
394 	char		*path;
395 	char		*addr;
396 	int		plen;
397 	char		**ibp;		/* instance block paths */
398 	uint_t		nibp;		/* number of paths in instance block */
399 	uint_t		ibpi;		/* ibp index */
400 	int		ibplen;		/* length of instance block path */
401 	char		*ipath;
402 	int		instance_base;
403 	int		splice;
404 	int		i;
405 
406 	/* check for fresh install case (in miniroot) */
407 	if (DEVI(dip)->devi_instance != -1)
408 		return (0);			/* already assigned */
409 
410 	/*
411 	 * Check to see if we need to allocate a block of contiguous instance
412 	 * numbers by looking for the 'ddi-instance-blocks' property.
413 	 */
414 	if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
415 	    "ddi-instance-blocks", &ibn, &nibn) != DDI_SUCCESS)
416 		return (0);			/* no instance block needed */
417 
418 	/*
419 	 * Get information out about node we are processing.
420 	 *
421 	 * NOTE: Since the node is not yet at DS_INITIALIZED, ddi_pathname()
422 	 * will not return the unit-address of the final path component even
423 	 * though the node has an established devi_addr unit-address - so we
424 	 * need to add the unit-address by hand.
425 	 */
426 	driver = (char *)ddi_driver_name(dip);
427 	major = ddi_driver_major(dip);
428 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
429 	(void) ddi_pathname(dip, path);
430 	if ((addr =  ddi_get_name_addr(dip)) != NULL) {
431 		(void) strcat(path, "@");
432 		(void) strcat(path, addr);
433 	}
434 	plen = strlen(path);
435 
436 	/* loop through instance block names */
437 	for (ibni = 0; ibni < nibn; ibni++) {
438 		if (ibn[ibni] == NULL)
439 			continue;
440 
441 		/* lookup instance block */
442 		if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, dip,
443 		    DDI_PROP_DONTPASS, ibn[ibni],
444 		    &ibp, &nibp) != DDI_SUCCESS) {
445 			cmn_err(CE_WARN,
446 			    "no devinition for instance block '%s' in %s.conf",
447 			    ibn[ibni], driver);
448 			continue;
449 		}
450 
451 		/* Does 'path' go through this instance block? */
452 		for (ibpi = 0; ibpi < nibp; ibpi++) {
453 			if (ibp[ibpi] == NULL)
454 				continue;
455 			ibplen = strlen(ibp[ibpi]);
456 			if ((ibplen <= plen) &&
457 			    (strcmp(ibp[ibpi], path + plen - ibplen) == 0))
458 				break;
459 
460 		}
461 		if (ibpi >= nibp) {
462 			ddi_prop_free(ibp);
463 			continue;		/* no try next instance block */
464 		}
465 
466 		/* yes, allocate and assign instances for all paths in block */
467 
468 		/*
469 		 * determine where we splice in instance paths and verify
470 		 * that none of the paths are too long.
471 		 */
472 		splice = plen - ibplen;
473 		for (i = 0; i < nibp; i++) {
474 			if ((splice + strlen(ibp[i])+ 1) >= MAXPATHLEN) {
475 				cmn_err(CE_WARN,
476 				    "path %d through instance block '%s' from "
477 				    "%s.conf too long", i, ibn[ibni], driver);
478 				break;
479 			}
480 		}
481 		if (i < nibp) {
482 			ddi_prop_free(ibp);
483 			continue;		/* too long */
484 		}
485 
486 		/* allocate the instance block - no more failures */
487 		instance_base = in_next_instance_block(major, nibp);
488 
489 		ipath = kmem_alloc(MAXPATHLEN, KM_SLEEP);
490 		for (ibpi = 0; ibpi < nibp; ibpi++) {
491 			if (ibp[ibpi] == NULL)
492 				continue;
493 			(void) strcpy(ipath, path);
494 			(void) strcpy(ipath + splice, ibp[ibpi]);
495 			(void) in_pathin(ipath,
496 			    instance_base + ibpi, driver, NULL);
497 		}
498 
499 		/* free allocations */
500 		kmem_free(ipath, MAXPATHLEN);
501 		ddi_prop_free(ibp);
502 		kmem_free(path, MAXPATHLEN);
503 		ddi_prop_free(ibn);
504 
505 		/* notify devfsadmd to sync of path_to_inst file */
506 		mutex_enter(&e_ddi_inst_state.ins_serial);
507 		i_log_devfs_instance_mod();
508 		e_ddi_inst_state.ins_dirty = B_TRUE;
509 		mutex_exit(&e_ddi_inst_state.ins_serial);
510 		return (1);
511 	}
512 
513 	/* our path did not go through any of of the instance blocks */
514 	kmem_free(path, MAXPATHLEN);
515 	ddi_prop_free(ibn);
516 	return (0);
517 }
518 
519 /*
520  * Look up an instance number for a dev_info node, and assign one if it does
521  * not have one (the dev_info node has devi_name and devi_addr already set).
522  */
523 uint_t
524 e_ddi_assign_instance(dev_info_t *dip)
525 {
526 	char *name;
527 	in_node_t *ap, *np;
528 	in_drv_t *dp;
529 	major_t major;
530 	uint_t ret;
531 	char *bname;
532 
533 	/*
534 	 * Allow implementation to override
535 	 */
536 	if ((ret = impl_assign_instance(dip)) != (uint_t)-1)
537 		return (ret);
538 
539 	/*
540 	 * If this is a pseudo-device, use the instance number
541 	 * assigned by the pseudo nexus driver. The mutex is
542 	 * not needed since the instance tree is not used.
543 	 */
544 	if (is_pseudo_device(dip)) {
545 		return (ddi_get_instance(dip));
546 	}
547 
548 	/*
549 	 * Only one thread is allowed to change the state of the instance
550 	 * number assignments on the system at any given time.
551 	 */
552 	e_ddi_enter_instance();
553 
554 	/*
555 	 * Look for instance node, allocate one if not found
556 	 */
557 	np = in_devwalk(dip, &ap, NULL);
558 	if (np == NULL) {
559 		if (in_assign_instance_block(dip)) {
560 			np = in_devwalk(dip, &ap, NULL);
561 		} else {
562 			name = ddi_node_name(dip);
563 			np = in_alloc_node(name, ddi_get_name_addr(dip));
564 			ASSERT(np != NULL);
565 			in_enlist(ap, np);	/* insert into tree */
566 		}
567 	}
568 	ASSERT(np == in_devwalk(dip, &ap, NULL));
569 
570 	/*
571 	 * Link the devinfo node and in_node_t
572 	 */
573 	if (DEVI(dip)->devi_in_node || np->in_devi) {
574 		ddi_err(DER_MODE, dip, "devinfo and  instance node (%p) "
575 		    "interlink fields are not NULL", (void *)np);
576 	}
577 	DEVI(dip)->devi_in_node = np;
578 	np->in_devi = dip;
579 
580 	/*
581 	 * Look for driver entry, allocate one if not found
582 	 */
583 	bname = (char *)ddi_driver_name(dip);
584 	dp = in_drvwalk(np, bname);
585 	if (dp == NULL) {
586 
587 		if (ddi_aliases_present == B_TRUE) {
588 			e_ddi_borrow_instance(dip, np);
589 		}
590 
591 		if ((dp = in_drvwalk(np, bname)) == NULL) {
592 			dp = in_alloc_drv(bname);
593 			ASSERT(dp != NULL);
594 			major = ddi_driver_major(dip);
595 			ASSERT(major != DDI_MAJOR_T_NONE);
596 			in_endrv(np, dp);
597 			in_set_instance(dip, dp, major);
598 			dp->ind_state = IN_PROVISIONAL;
599 			in_hashdrv(dp);
600 		} else {
601 			dp->ind_state = IN_BORROWED;
602 		}
603 	}
604 
605 	ret = dp->ind_instance;
606 
607 	e_ddi_exit_instance();
608 	return (ret);
609 }
610 
611 static int
612 mkpathname(char *path, in_node_t *np, int len)
613 {
614 	int len_needed;
615 
616 	if (np == e_ddi_inst_state.ins_root)
617 		return (DDI_SUCCESS);
618 
619 	if (mkpathname(path, np->in_parent, len) == DDI_FAILURE)
620 		return (DDI_FAILURE);
621 
622 	len_needed = strlen(path);
623 	len_needed += strlen(np->in_node_name) + 1;	/* for '/' */
624 	if (np->in_unit_addr) {
625 		len_needed += strlen(np->in_unit_addr) + 1;  /* for '@' */
626 	}
627 	len_needed += 1; /* for '\0' */
628 
629 	/*
630 	 * XX complain
631 	 */
632 	if (len_needed > len)
633 		return (DDI_FAILURE);
634 
635 	if (np->in_unit_addr[0] == '\0')
636 		(void) sprintf(path+strlen(path), "/%s", np->in_node_name);
637 	else
638 		(void) sprintf(path+strlen(path), "/%s@%s", np->in_node_name,
639 		    np->in_unit_addr);
640 
641 	return (DDI_SUCCESS);
642 }
643 
644 /*
645  * produce the path to the given instance of a major number.
646  * path must hold MAXPATHLEN string
647  */
648 int
649 e_ddi_instance_majorinstance_to_path(major_t major, uint_t inst, char *path)
650 {
651 	struct devnames	*dnp;
652 	in_drv_t	*dp;
653 	int		ret;
654 
655 	e_ddi_enter_instance();
656 
657 	/* look for the instance threaded off major */
658 	dnp = &devnamesp[major];
659 	for (dp = dnp->dn_inlist; dp != NULL; dp = dp->ind_next)
660 		if (dp->ind_instance == inst)
661 			break;
662 
663 	/* produce path from the node that uses the instance */
664 	if (dp) {
665 		*path = 0;
666 		ret = mkpathname(path, dp->ind_node, MAXPATHLEN);
667 	} else
668 		ret = DDI_FAILURE;
669 
670 	e_ddi_exit_instance();
671 	return (ret);
672 }
673 
674 /*
675  * Allocate a sequential block of instance numbers for the specified driver,
676  * and return the base instance number of the block.  The implementation
677  * depends on the list being sorted in ascending instance number sequence.
678  * When there are no 'holes' in the allocation sequence, dn_instance is the
679  * next available instance number. When dn_instance is IN_SEARCHME, hole(s)
680  * exists and a slower code path executes which tries to fill holes.
681  */
682 static int
683 in_next_instance_block(major_t major, int block_size)
684 {
685 	unsigned int	prev;
686 	struct devnames	*dnp;
687 	in_drv_t	*dp;
688 	int		base;
689 	int		hole;
690 
691 	dnp = &devnamesp[major];
692 	ASSERT(major != DDI_MAJOR_T_NONE);
693 	ASSERT(e_ddi_inst_state.ins_busy);
694 	ASSERT(block_size);
695 
696 	/* check to see if we can do a quick allocation */
697 	if (dnp->dn_instance != IN_SEARCHME) {
698 		base = dnp->dn_instance;
699 		dnp->dn_instance += block_size;
700 		return (base);
701 	}
702 	dp = dnp->dn_inlist;
703 
704 	/* no existing entries, allocate block at 0 */
705 	if (dp == NULL) {
706 		dnp->dn_instance = block_size;
707 		return (0);
708 	}
709 
710 	prev = dp->ind_instance;
711 	if (prev >= block_size)
712 		return (0);		/* we fit in hole at beginning */
713 
714 	/* search the list for a large enough hole */
715 	for (dp = dp->ind_next, hole = 0; dp; dp = dp->ind_next) {
716 		if (dp->ind_instance != (prev + 1))
717 			hole++;			/* we have a hole */
718 		if (dp->ind_instance >= (prev + block_size + 1))
719 			break;			/* we fit in hole */
720 		prev = dp->ind_instance;
721 	}
722 
723 	/*
724 	 * If hole is zero then all holes are patched and we can resume
725 	 * quick allocations.
726 	 */
727 	if (hole == 0)
728 		dnp->dn_instance = prev + 1 + block_size;
729 
730 	return (prev + 1);
731 }
732 
733 /* assign instance block of size 1 */
734 static int
735 in_next_instance(major_t major)
736 {
737 	return (in_next_instance_block(major, 1));
738 }
739 
740 /*
741  * This call causes us to *forget* the instance number we've generated
742  * for a given device if it was not permanent.
743  */
744 void
745 e_ddi_free_instance(dev_info_t *dip, char *addr)
746 {
747 	char *name;
748 	in_node_t *np;
749 	in_node_t *ap;	/* ancestor node */
750 	major_t major;
751 	struct devnames *dnp;
752 	in_drv_t *dp;	/* in_drv entry */
753 
754 	/*
755 	 * Allow implementation override
756 	 */
757 	if (impl_free_instance(dip) == DDI_SUCCESS)
758 		return;
759 
760 	/*
761 	 * If this is a pseudo-device, no instance number
762 	 * was assigned.
763 	 */
764 	if (is_pseudo_device(dip)) {
765 		return;
766 	}
767 
768 	name = (char *)ddi_driver_name(dip);
769 	major = ddi_driver_major(dip);
770 	ASSERT(major != DDI_MAJOR_T_NONE);
771 	dnp = &devnamesp[major];
772 	/*
773 	 * Only one thread is allowed to change the state of the instance
774 	 * number assignments on the system at any given time.
775 	 */
776 	e_ddi_enter_instance();
777 	np = in_devwalk(dip, &ap, addr);
778 	ASSERT(np);
779 
780 	/*
781 	 * Break the interlink between dip and np
782 	 */
783 	if (DEVI(dip)->devi_in_node != np || np->in_devi != dip) {
784 		ddi_err(DER_MODE, dip, "devinfo node linked to "
785 		    "wrong instance node: %p", (void *)np);
786 	}
787 	DEVI(dip)->devi_in_node = NULL;
788 	np->in_devi = NULL;
789 
790 	dp = in_drvwalk(np, name);
791 	ASSERT(dp);
792 	if (dp->ind_state == IN_PROVISIONAL) {
793 		in_removedrv(dnp, dp);
794 	}
795 	if (dp->ind_state == IN_BORROWED) {
796 		dp->ind_state = IN_PERMANENT;
797 		e_ddi_return_instance(dip, addr, np);
798 	}
799 	if (np->in_drivers == NULL) {
800 		in_removenode(dnp, np, ap);
801 	}
802 	e_ddi_exit_instance();
803 }
804 
805 /*
806  * This makes our memory of an instance assignment permanent
807  */
808 void
809 e_ddi_keep_instance(dev_info_t *dip)
810 {
811 	in_node_t *np, *ap;
812 	in_drv_t *dp;
813 
814 	/* Don't make nulldriver instance assignments permanent */
815 	if (ddi_driver_major(dip) == nulldriver_major)
816 		return;
817 
818 	/*
819 	 * Allow implementation override
820 	 */
821 	if (impl_keep_instance(dip) == DDI_SUCCESS)
822 		return;
823 
824 	/*
825 	 * Nothing to do for pseudo devices.
826 	 */
827 	if (is_pseudo_device(dip))
828 		return;
829 
830 	/*
831 	 * Only one thread is allowed to change the state of the instance
832 	 * number assignments on the system at any given time.
833 	 */
834 	e_ddi_enter_instance();
835 	np = in_devwalk(dip, &ap, NULL);
836 	ASSERT(np);
837 	dp = in_drvwalk(np, (char *)ddi_driver_name(dip));
838 	ASSERT(dp);
839 
840 	mutex_enter(&e_ddi_inst_state.ins_serial);
841 	if (dp->ind_state == IN_PROVISIONAL || dp->ind_state == IN_BORROWED) {
842 		dp->ind_state = IN_PERMANENT;
843 		i_log_devfs_instance_mod();
844 		e_ddi_inst_state.ins_dirty = B_TRUE;
845 	}
846 	mutex_exit(&e_ddi_inst_state.ins_serial);
847 	e_ddi_exit_instance();
848 }
849 
850 /*
851  * A new major has been added to the system.  Run through the orphan list
852  * and try to attach each one to a driver's list.
853  */
854 void
855 e_ddi_unorphan_instance_nos()
856 {
857 	in_drv_t *dp, *ndp;
858 
859 	/*
860 	 * disconnect the orphan list, and call in_hashdrv for each item
861 	 * on it
862 	 */
863 
864 	/*
865 	 * Only one thread is allowed to change the state of the instance
866 	 * number assignments on the system at any given time.
867 	 */
868 	e_ddi_enter_instance();
869 	if (e_ddi_inst_state.ins_no_major == NULL) {
870 		e_ddi_exit_instance();
871 		return;
872 	}
873 	/*
874 	 * Hash instance list to devnames structure of major.
875 	 * Note that if there is not a valid major number for the
876 	 * node, in_hashdrv will put it back on the no_major list.
877 	 */
878 	dp = e_ddi_inst_state.ins_no_major;
879 	e_ddi_inst_state.ins_no_major = NULL;
880 	while (dp) {
881 		ndp = dp->ind_next;
882 		ASSERT(dp->ind_state != IN_UNKNOWN);
883 		dp->ind_next = NULL;
884 		in_hashdrv(dp);
885 		dp = ndp;
886 	}
887 	e_ddi_exit_instance();
888 }
889 
890 static void
891 in_removenode(struct devnames *dnp, in_node_t *mp, in_node_t *ap)
892 {
893 	in_node_t *np;
894 
895 	ASSERT(e_ddi_inst_state.ins_busy);
896 
897 	/*
898 	 * Assertion: parents are always instantiated by the framework
899 	 * before their children, destroyed after them
900 	 */
901 	ASSERT(mp->in_child == NULL);
902 	/*
903 	 * Assertion: drv entries are always removed before their owning nodes
904 	 */
905 	ASSERT(mp->in_drivers == NULL);
906 	/*
907 	 * Take the node out of the tree
908 	 */
909 	if (ap->in_child == mp) {
910 		ap->in_child = mp->in_sibling;
911 		in_dealloc_node(mp);
912 		return;
913 	} else {
914 		for (np = ap->in_child; np; np = np->in_sibling) {
915 			if (np->in_sibling == mp) {
916 				np->in_sibling = mp->in_sibling;
917 				in_dealloc_node(mp);
918 				return;
919 			}
920 		}
921 	}
922 	panic("in_removenode dnp %p mp %p", (void *)dnp, (void *)mp);
923 }
924 
925 /*
926  * Recursive ascent
927  *
928  * This now only does half the job.  It finds the node, then the caller
929  * has to search the node for the binding name
930  */
931 static in_node_t *
932 in_devwalk(dev_info_t *dip, in_node_t **ap, char *addr)
933 {
934 	in_node_t *np;
935 	char *name;
936 
937 	ASSERT(dip);
938 	ASSERT(e_ddi_inst_state.ins_busy);
939 	if (dip == ddi_root_node()) {
940 		*ap = NULL;
941 		return (e_ddi_inst_state.ins_root);
942 	}
943 	/*
944 	 * call up to find parent, then look through the list of kids
945 	 * for a match
946 	 */
947 	np = in_devwalk(ddi_get_parent(dip), ap, NULL);
948 	if (np == NULL)
949 		return (np);
950 	*ap = np;
951 	np = np->in_child;
952 	name = ddi_node_name(dip);
953 	if (addr == NULL)
954 		addr = ddi_get_name_addr(dip);
955 
956 	while (np) {
957 		if (in_eqstr(np->in_node_name, name) &&
958 		    in_eqstr(np->in_unit_addr, addr)) {
959 			return (np);
960 		}
961 		np = np->in_sibling;
962 	}
963 
964 	return (np);
965 }
966 
967 /*
968  * Create a node specified by cp and assign it the given instance no.
969  */
970 static int
971 in_pathin(char *cp, int instance, char *bname, struct bind **args)
972 {
973 	in_node_t *np;
974 	in_drv_t *dp;
975 	char *name;
976 
977 	ASSERT(e_ddi_inst_state.ins_busy);
978 	ASSERT(args == NULL);
979 
980 	/*
981 	 * Give a warning to the console.
982 	 * return value ignored
983 	 */
984 	if (cp[0] != '/' || instance == -1 || bname == NULL) {
985 		cmn_err(CE_WARN,
986 		    "invalid instance file entry %s %d",
987 		    cp, instance);
988 		return (0);
989 	}
990 
991 	if ((name  = i_binding_to_drv_name(bname)) != NULL)
992 		bname = name;
993 
994 	np = in_make_path(cp);
995 	ASSERT(np);
996 
997 	dp = in_drvwalk(np, bname);
998 	if (dp != NULL) {
999 		cmn_err(CE_WARN,
1000 		    "multiple instance number assignments for "
1001 		    "'%s' (driver %s), %d used",
1002 		    cp, bname, dp->ind_instance);
1003 		return (0);
1004 	}
1005 
1006 	if (in_inuse(instance, bname)) {
1007 		cmn_err(CE_WARN,
1008 		    "instance already in use: %s %d", cp, instance);
1009 		return (0);
1010 	}
1011 
1012 	dp = in_alloc_drv(bname);
1013 	in_endrv(np, dp);
1014 	dp->ind_instance = instance;
1015 	dp->ind_state = IN_PERMANENT;
1016 	in_hashdrv(dp);
1017 
1018 	return (0);
1019 }
1020 
1021 /*
1022  * Create (or find) the node named by path by recursively descending from the
1023  * root's first child (we ignore the root, which is never named)
1024  */
1025 static in_node_t *
1026 in_make_path(char *path)
1027 {
1028 	in_node_t *ap;		/* ancestor pointer */
1029 	in_node_t *np;		/* working node pointer */
1030 	in_node_t *rp;		/* return node pointer */
1031 	char buf[MAXPATHLEN];	/* copy of string so we can change it */
1032 	char *cp, *name, *addr;
1033 
1034 	ASSERT(e_ddi_inst_state.ins_busy);
1035 
1036 	if (path == NULL || path[0] != '/')
1037 		return (NULL);
1038 
1039 	(void) snprintf(buf, sizeof (buf), "%s", path);
1040 	cp = buf + 1;	/* skip over initial '/' in path */
1041 	name = in_name_addr(&cp, &addr);
1042 
1043 	/*
1044 	 * In S9 and earlier releases, the path_to_inst file
1045 	 * SunCluster was prepended with "/node@#". This was
1046 	 * removed in S10. We skip the prefix if the prefix
1047 	 * still exists in /etc/path_to_inst. It is needed for
1048 	 * various forms of Solaris upgrade to work properly
1049 	 * in the SunCluster environment.
1050 	 */
1051 	if ((cluster_bootflags & CLUSTER_CONFIGURED) &&
1052 	    (strcmp(name, "node") == 0))
1053 		name = in_name_addr(&cp, &addr);
1054 
1055 	ap = e_ddi_inst_state.ins_root;
1056 	np = e_ddi_inst_state.ins_root->in_child;
1057 	rp = np;
1058 	while (name) {
1059 		while (name && np) {
1060 			if (in_eqstr(name, np->in_node_name) &&
1061 			    in_eqstr(addr, np->in_unit_addr)) {
1062 				name = in_name_addr(&cp, &addr);
1063 				if (name == NULL)
1064 					return (np);
1065 				ap = np;
1066 				np = np->in_child;
1067 			} else {
1068 				np = np->in_sibling;
1069 			}
1070 		}
1071 		np = in_alloc_node(name, addr);
1072 		in_enlist(ap, np);	/* insert into tree */
1073 		rp = np;	/* value to return if we quit */
1074 		ap = np;	/* new parent */
1075 		np = NULL;	/* can have no children */
1076 		name = in_name_addr(&cp, &addr);
1077 	}
1078 
1079 	return (rp);
1080 }
1081 
1082 /*
1083  * Insert node np into the tree as one of ap's children.
1084  */
1085 static void
1086 in_enlist(in_node_t *ap, in_node_t *np)
1087 {
1088 	in_node_t *mp;
1089 	ASSERT(e_ddi_inst_state.ins_busy);
1090 	/*
1091 	 * Make this node some other node's child or child's sibling
1092 	 */
1093 	ASSERT(ap && np);
1094 	if (ap->in_child == NULL) {
1095 		ap->in_child = np;
1096 	} else {
1097 		for (mp = ap->in_child; mp; mp = mp->in_sibling)
1098 			if (mp->in_sibling == NULL) {
1099 				mp->in_sibling = np;
1100 				break;
1101 			}
1102 	}
1103 	np->in_parent = ap;
1104 }
1105 
1106 /*
1107  * Insert drv entry dp onto a node's driver list
1108  */
1109 static void
1110 in_endrv(in_node_t *np, in_drv_t *dp)
1111 {
1112 	in_drv_t *mp;
1113 	ASSERT(e_ddi_inst_state.ins_busy);
1114 	ASSERT(np && dp);
1115 	mp = np->in_drivers;
1116 	np->in_drivers = dp;
1117 	dp->ind_next_drv = mp;
1118 	dp->ind_node = np;
1119 }
1120 
1121 /*
1122  * Parse the next name out of the path, null terminate it and update cp.
1123  * caller has copied string so we can mess with it.
1124  * Upon return *cpp points to the next section to be parsed, *addrp points
1125  * to the current address substring (or NULL if none) and we return the
1126  * current name substring (or NULL if none).  name and address substrings
1127  * are null terminated in place.
1128  */
1129 
1130 static char *
1131 in_name_addr(char **cpp, char **addrp)
1132 {
1133 	char *namep;	/* return value holder */
1134 	char *ap;	/* pointer to '@' in string */
1135 	char *sp;	/* pointer to '/' in string */
1136 
1137 	if (*cpp == NULL || **cpp == '\0') {
1138 		*addrp = NULL;
1139 		return (NULL);
1140 	}
1141 	namep = *cpp;
1142 	sp = strchr(*cpp, '/');
1143 	if (sp != NULL) {	/* more to follow */
1144 		*sp = '\0';
1145 		*cpp = sp + 1;
1146 	} else {		/* this is last component. */
1147 		*cpp = NULL;
1148 	}
1149 	ap = strchr(namep, '@');
1150 	if (ap == NULL) {
1151 		*addrp = NULL;
1152 	} else {
1153 		*ap = '\0';		/* terminate the name */
1154 		*addrp = ap + 1;
1155 	}
1156 	return (namep);
1157 }
1158 
1159 /*
1160  * Allocate a node and storage for name and addr strings, and fill them in.
1161  */
1162 static in_node_t *
1163 in_alloc_node(char *name, char *addr)
1164 {
1165 	in_node_t *np;
1166 	char *cp;
1167 	size_t namelen;
1168 
1169 	ASSERT(e_ddi_inst_state.ins_busy);
1170 	/*
1171 	 * Has name or will become root
1172 	 */
1173 	ASSERT(name || e_ddi_inst_state.ins_root == NULL);
1174 	if (addr == NULL)
1175 		addr = "";
1176 	if (name == NULL)
1177 		namelen = 0;
1178 	else
1179 		namelen = strlen(name) + 1;
1180 	cp = kmem_zalloc(sizeof (in_node_t) + namelen + strlen(addr) + 1,
1181 	    KM_SLEEP);
1182 	np = (in_node_t *)cp;
1183 	if (name) {
1184 		np->in_node_name = cp + sizeof (in_node_t);
1185 		(void) strcpy(np->in_node_name, name);
1186 	}
1187 	np->in_unit_addr = cp + sizeof (in_node_t) + namelen;
1188 	(void) strcpy(np->in_unit_addr, addr);
1189 	return (np);
1190 }
1191 
1192 /*
1193  * Allocate a drv entry and storage for binding name string, and fill it in.
1194  */
1195 static in_drv_t *
1196 in_alloc_drv(char *bindingname)
1197 {
1198 	in_drv_t *dp;
1199 	char *cp;
1200 	size_t namelen;
1201 
1202 	ASSERT(e_ddi_inst_state.ins_busy);
1203 	/*
1204 	 * Has name or will become root
1205 	 */
1206 	ASSERT(bindingname || e_ddi_inst_state.ins_root == NULL);
1207 	if (bindingname == NULL)
1208 		namelen = 0;
1209 	else
1210 		namelen = strlen(bindingname) + 1;
1211 	cp = kmem_zalloc(sizeof (in_drv_t) + namelen, KM_SLEEP);
1212 	dp = (in_drv_t *)cp;
1213 	if (bindingname) {
1214 		dp->ind_driver_name = cp + sizeof (in_drv_t);
1215 		(void) strcpy(dp->ind_driver_name, bindingname);
1216 	}
1217 	dp->ind_state = IN_UNKNOWN;
1218 	dp->ind_instance = -1;
1219 	return (dp);
1220 }
1221 
1222 static void
1223 in_dealloc_node(in_node_t *np)
1224 {
1225 	/*
1226 	 * The root node can never be de-allocated
1227 	 */
1228 	ASSERT(np->in_node_name && np->in_unit_addr);
1229 	ASSERT(e_ddi_inst_state.ins_busy);
1230 	kmem_free(np, sizeof (in_node_t) + strlen(np->in_node_name)
1231 	    + strlen(np->in_unit_addr) + 2);
1232 }
1233 
1234 static void
1235 in_dealloc_drv(in_drv_t *dp)
1236 {
1237 	ASSERT(dp->ind_driver_name);
1238 	ASSERT(e_ddi_inst_state.ins_busy);
1239 	kmem_free(dp, sizeof (in_drv_t) + strlen(dp->ind_driver_name)
1240 	    + 1);
1241 }
1242 
1243 /*
1244  * Handle the various possible versions of "no address"
1245  */
1246 static int
1247 in_eqstr(char *a, char *b)
1248 {
1249 	if (a == b)	/* covers case where both are nulls */
1250 		return (1);
1251 	if (a == NULL && *b == 0)
1252 		return (1);
1253 	if (b == NULL && *a == 0)
1254 		return (1);
1255 	if (a == NULL || b == NULL)
1256 		return (0);
1257 	return (strcmp(a, b) == 0);
1258 }
1259 
1260 /*
1261  * Returns true if instance no. is already in use by named driver
1262  */
1263 static int
1264 in_inuse(int instance, char *name)
1265 {
1266 	major_t major;
1267 	in_drv_t *dp;
1268 	struct devnames *dnp;
1269 
1270 	ASSERT(e_ddi_inst_state.ins_busy);
1271 	/*
1272 	 * For now, if we've never heard of this device we assume it is not
1273 	 * in use, since we can't tell
1274 	 * XXX could do the weaker search through the nomajor list checking
1275 	 * XXX for the same name
1276 	 */
1277 	if ((major = ddi_name_to_major(name)) == DDI_MAJOR_T_NONE)
1278 		return (0);
1279 	dnp = &devnamesp[major];
1280 
1281 	dp = dnp->dn_inlist;
1282 	while (dp) {
1283 		if (dp->ind_instance == instance)
1284 			return (1);
1285 		dp = dp->ind_next;
1286 	}
1287 	return (0);
1288 }
1289 
1290 static void
1291 in_hashdrv(in_drv_t *dp)
1292 {
1293 	struct devnames *dnp;
1294 	in_drv_t *mp, *pp;
1295 	major_t major;
1296 
1297 	/* hash to no major list */
1298 	major = ddi_name_to_major(dp->ind_driver_name);
1299 	if (major == DDI_MAJOR_T_NONE) {
1300 		dp->ind_next = e_ddi_inst_state.ins_no_major;
1301 		e_ddi_inst_state.ins_no_major = dp;
1302 		return;
1303 	}
1304 
1305 	/*
1306 	 * dnp->dn_inlist is sorted by instance number.
1307 	 * Adding a new instance entry may introduce holes,
1308 	 * set dn_instance to IN_SEARCHME so the next instance
1309 	 * assignment may fill in holes.
1310 	 */
1311 	dnp = &devnamesp[major];
1312 	pp = mp = dnp->dn_inlist;
1313 	if (mp == NULL || dp->ind_instance < mp->ind_instance) {
1314 		/* prepend as the first entry, turn on IN_SEARCHME */
1315 		dnp->dn_instance = IN_SEARCHME;
1316 		dp->ind_next = mp;
1317 		dnp->dn_inlist = dp;
1318 		return;
1319 	}
1320 
1321 	ASSERT(mp->ind_instance != dp->ind_instance);
1322 	while (mp->ind_instance < dp->ind_instance && mp->ind_next) {
1323 		pp = mp;
1324 		mp = mp->ind_next;
1325 		ASSERT(mp->ind_instance != dp->ind_instance);
1326 	}
1327 
1328 	if (mp->ind_instance < dp->ind_instance) { /* end of list */
1329 		dp->ind_next = NULL;
1330 		mp->ind_next = dp;
1331 	} else {
1332 		ASSERT(dnp->dn_instance == IN_SEARCHME);
1333 		dp->ind_next = pp->ind_next;
1334 		pp->ind_next = dp;
1335 	}
1336 }
1337 
1338 /*
1339  * Remove a driver entry from the list, given a previous pointer
1340  */
1341 static void
1342 in_removedrv(struct devnames *dnp, in_drv_t *mp)
1343 {
1344 	in_drv_t *dp;
1345 	in_drv_t *prevp;
1346 
1347 	if (dnp->dn_inlist == mp) {	/* head of list */
1348 		dnp->dn_inlist = mp->ind_next;
1349 		dnp->dn_instance = IN_SEARCHME;
1350 		in_dq_drv(mp);
1351 		in_dealloc_drv(mp);
1352 		return;
1353 	}
1354 	prevp = dnp->dn_inlist;
1355 	for (dp = prevp->ind_next; dp; dp = dp->ind_next) {
1356 		if (dp == mp) {		/* found it */
1357 			break;
1358 		}
1359 		prevp = dp;
1360 	}
1361 
1362 	ASSERT(dp == mp);
1363 	dnp->dn_instance = IN_SEARCHME;
1364 	prevp->ind_next = mp->ind_next;
1365 	in_dq_drv(mp);
1366 	in_dealloc_drv(mp);
1367 }
1368 
1369 static void
1370 in_dq_drv(in_drv_t *mp)
1371 {
1372 	struct in_node *node = mp->ind_node;
1373 	in_drv_t *ptr, *prev;
1374 
1375 	if (mp == node->in_drivers) {
1376 		node->in_drivers = mp->ind_next_drv;
1377 		return;
1378 	}
1379 	prev = node->in_drivers;
1380 	for (ptr = prev->ind_next_drv; ptr != (struct in_drv *)NULL;
1381 	    ptr = ptr->ind_next_drv) {
1382 		if (ptr == mp) {
1383 			prev->ind_next_drv = ptr->ind_next_drv;
1384 			return;
1385 		}
1386 		prev = ptr;
1387 	}
1388 	panic("in_dq_drv: in_drv not found on node driver list");
1389 }
1390 
1391 
1392 in_drv_t *
1393 in_drvwalk(in_node_t *np, char *binding_name)
1394 {
1395 	char *name;
1396 	in_drv_t *dp = np->in_drivers;
1397 	while (dp) {
1398 		if ((name = i_binding_to_drv_name(dp->ind_driver_name))
1399 		    == NULL) {
1400 			name = dp->ind_driver_name;
1401 		}
1402 		if (strcmp(binding_name, name) == 0) {
1403 			break;
1404 		}
1405 		dp = dp->ind_next_drv;
1406 	}
1407 	return (dp);
1408 }
1409 
1410 
1411 
1412 static void
1413 i_log_devfs_instance_mod(void)
1414 {
1415 	sysevent_t	*ev;
1416 	sysevent_id_t	eid;
1417 	static int	sent_one = 0;
1418 
1419 	/*
1420 	 * Prevent unnecessary event generation.  Do not generate more than
1421 	 * one event during boot.
1422 	 */
1423 	if (sent_one && !i_ddi_io_initialized())
1424 		return;
1425 
1426 	ev = sysevent_alloc(EC_DEVFS, ESC_DEVFS_INSTANCE_MOD, EP_DDI,
1427 	    SE_NOSLEEP);
1428 	if (ev == NULL) {
1429 		return;
1430 	}
1431 	if (log_sysevent(ev, SE_NOSLEEP, &eid) != 0) {
1432 		cmn_err(CE_WARN, "i_log_devfs_instance_mod: failed to post "
1433 		    "event");
1434 	} else {
1435 		sent_one = 1;
1436 	}
1437 	sysevent_free(ev);
1438 }
1439 
1440 void
1441 e_ddi_enter_instance(void)
1442 {
1443 	mutex_enter(&e_ddi_inst_state.ins_serial);
1444 	if (e_ddi_inst_state.ins_thread == curthread)
1445 		e_ddi_inst_state.ins_busy++;
1446 	else {
1447 		while (e_ddi_inst_state.ins_busy)
1448 			cv_wait(&e_ddi_inst_state.ins_serial_cv,
1449 			    &e_ddi_inst_state.ins_serial);
1450 		e_ddi_inst_state.ins_thread = curthread;
1451 		e_ddi_inst_state.ins_busy = 1;
1452 	}
1453 	mutex_exit(&e_ddi_inst_state.ins_serial);
1454 }
1455 
1456 void
1457 e_ddi_exit_instance(void)
1458 {
1459 	mutex_enter(&e_ddi_inst_state.ins_serial);
1460 	e_ddi_inst_state.ins_busy--;
1461 	if (e_ddi_inst_state.ins_busy == 0) {
1462 		cv_broadcast(&e_ddi_inst_state.ins_serial_cv);
1463 		e_ddi_inst_state.ins_thread = NULL;
1464 	}
1465 	mutex_exit(&e_ddi_inst_state.ins_serial);
1466 }
1467 
1468 int
1469 e_ddi_instance_is_clean(void)
1470 {
1471 	return (e_ddi_inst_state.ins_dirty == B_FALSE);
1472 }
1473 
1474 void
1475 e_ddi_instance_set_clean(void)
1476 {
1477 	e_ddi_inst_state.ins_dirty = B_FALSE;
1478 }
1479 
1480 in_node_t *
1481 e_ddi_instance_root(void)
1482 {
1483 	return (e_ddi_inst_state.ins_root);
1484 }
1485 
1486 /*
1487  * Visit a node in the instance tree
1488  */
1489 static int
1490 in_walk_instances(in_node_t *np, char *path, char *this,
1491     int (*f)(const char *, in_node_t *, in_drv_t *, void *), void *arg)
1492 {
1493 	in_drv_t *dp;
1494 	int rval = INST_WALK_CONTINUE;
1495 	char *next;
1496 
1497 	while (np != NULL) {
1498 
1499 		if (np->in_unit_addr[0] == 0)
1500 			(void) sprintf(this, "/%s", np->in_node_name);
1501 		else
1502 			(void) sprintf(this, "/%s@%s", np->in_node_name,
1503 			    np->in_unit_addr);
1504 		next = this + strlen(this);
1505 
1506 		for (dp = np->in_drivers; dp; dp = dp->ind_next_drv) {
1507 			if (dp->ind_state == IN_PERMANENT) {
1508 				rval = (*f)(path, np, dp, arg);
1509 				if (rval == INST_WALK_TERMINATE)
1510 					break;
1511 			}
1512 		}
1513 
1514 		if (np->in_child) {
1515 			rval = in_walk_instances(np->in_child,
1516 			    path, next, f, arg);
1517 			if (rval == INST_WALK_TERMINATE)
1518 				break;
1519 		}
1520 
1521 		np = np->in_sibling;
1522 	}
1523 
1524 	return (rval);
1525 }
1526 
1527 /*
1528  * A general interface for walking the instance tree,
1529  * calling a user-supplied callback for each node.
1530  */
1531 int
1532 e_ddi_walk_instances(int (*f)(const char *,
1533 	in_node_t *, in_drv_t *, void *), void *arg)
1534 {
1535 	in_node_t *root;
1536 	int rval;
1537 	char *path;
1538 
1539 	path = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
1540 
1541 	e_ddi_enter_instance();
1542 	root = e_ddi_instance_root();
1543 	rval = in_walk_instances(root->in_child, path, path, f, arg);
1544 
1545 	e_ddi_exit_instance();
1546 
1547 	kmem_free(path, MAXPATHLEN);
1548 	return (rval);
1549 }
1550 
1551 in_node_t *
1552 e_ddi_path_to_instance(char *path)
1553 {
1554 	in_node_t *np;
1555 
1556 	np = in_make_path(path);
1557 	if (np && np->in_drivers && np->in_drivers->ind_state == IN_PERMANENT) {
1558 		return (np);
1559 	}
1560 	return (NULL);
1561 }
1562 
1563 void
1564 e_ddi_borrow_instance(dev_info_t *cdip, in_node_t *cnp)
1565 {
1566 	char		*alias;
1567 	in_node_t	*anp;
1568 	char		*curr = kmem_alloc(MAXPATHLEN, KM_NOSLEEP);
1569 
1570 	if (curr == NULL) {
1571 		ddi_err(DER_PANIC, cdip, "curr alloc failed");
1572 		/*NOTREACHED*/
1573 	}
1574 
1575 	(void) ddi_pathname(cdip, curr);
1576 
1577 	if (cnp->in_drivers) {
1578 		ddi_err(DER_PANIC, cdip, "cnp has instance: %p", cnp);
1579 		/*NOTREACHED*/
1580 	}
1581 
1582 	alias = ddi_curr_redirect(curr);
1583 	kmem_free(curr, MAXPATHLEN);
1584 
1585 	if (alias && (anp = e_ddi_path_to_instance(alias)) != NULL) {
1586 		cnp->in_drivers = anp->in_drivers;
1587 		anp->in_drivers = NULL;
1588 	}
1589 }
1590 
1591 void
1592 e_ddi_return_instance(dev_info_t *cdip, char *addr, in_node_t *cnp)
1593 {
1594 	in_node_t	*anp;
1595 	char 		*alias;
1596 	char		*curr = kmem_alloc(MAXPATHLEN, KM_NOSLEEP);
1597 
1598 	if (curr == NULL) {
1599 		ddi_err(DER_PANIC, cdip, "alloc of curr failed");
1600 		/*NOTREACHED*/
1601 	}
1602 
1603 	(void) ddi_pathname(cdip, curr);
1604 	if (addr) {
1605 		(void) strlcat(curr, "@", MAXPATHLEN);
1606 		(void) strlcat(curr, addr, MAXPATHLEN);
1607 
1608 	}
1609 	if (cnp->in_drivers == NULL) {
1610 		ddi_err(DER_PANIC, cdip, "cnp has no inst: %p", cnp);
1611 		/*NOTREACHED*/
1612 	}
1613 
1614 	alias = ddi_curr_redirect(curr);
1615 	kmem_free(curr, MAXPATHLEN);
1616 
1617 	if (alias && (anp = e_ddi_path_to_instance(alias)) != NULL) {
1618 		ASSERT(anp->in_drivers == NULL);
1619 		anp->in_drivers = cnp->in_drivers;
1620 		cnp->in_drivers = NULL;
1621 	}
1622 }
1623