include: gcc 7's cpp has problems with the line continuations in .x files
[unleashed.git] / kernel / os / instance.c
blobd8d6b529dad73823a81ba548123b0789f031c91d
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
22 * Copyright (c) 1992, 2010, Oracle and/or its affiliates. All rights reserved.
26 * Instance number assignment code
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/sysmacros.h>
50 #include <sys/crc32.h>
53 static void in_preassign_instance(void);
54 static void i_log_devfs_instance_mod(void);
55 static int in_get_infile(char *);
56 static void in_removenode(struct devnames *dnp, in_node_t *mp, in_node_t *ap);
57 static in_node_t *in_alloc_node(char *name, char *addr);
58 static int in_eqstr(char *a, char *b);
59 static char *in_name_addr(char **cpp, char **addrp);
60 static in_node_t *in_devwalk(dev_info_t *dip, in_node_t **ap, char *addr);
61 static void in_dealloc_node(in_node_t *np);
62 static in_node_t *in_make_path(char *path);
63 static void in_enlist(in_node_t *ap, in_node_t *np);
64 static int in_inuse(int instance, char *name);
65 static void in_hashdrv(in_drv_t *dp);
66 static in_drv_t *in_drvwalk(in_node_t *np, char *binding_name);
67 static in_drv_t *in_alloc_drv(char *bindingname);
68 static void in_endrv(in_node_t *np, in_drv_t *dp);
69 static void in_dq_drv(in_drv_t *np);
70 static void in_removedrv(struct devnames *dnp, in_drv_t *mp);
71 static int in_pathin(char *cp, int instance, char *bname, struct bind **args);
72 static int in_next_instance_block(major_t, int);
73 static int in_next_instance(major_t);
75 #pragma weak plat_ioaliases_init
78 /* external functions */
79 extern char *i_binding_to_drv_name(char *bname);
80 extern void plat_ioaliases_init(void);
83 * This plus devnames defines the entire software state of the instance world.
85 typedef struct in_softstate {
86 in_node_t *ins_root; /* the root of our instance tree */
87 in_drv_t *ins_no_major; /* majorless drv entries */
89 * Used to serialize access to data structures
91 void *ins_thread;
92 kmutex_t ins_serial;
93 kcondvar_t ins_serial_cv;
94 int ins_busy;
95 boolean_t ins_dirty; /* instance info needs flush */
96 } in_softstate_t;
98 static in_softstate_t e_ddi_inst_state;
101 * State transition information:
102 * e_ddi_inst_state contains, among other things, the root of a tree of
103 * device nodes used to track instance number assignments.
104 * Each device node may contain multiple driver bindings, represented
105 * by a linked list of in_drv_t nodes, each with an instance assignment
106 * (except for root node). Each in_drv node can be in one of 3 states,
107 * indicated by ind_state:
109 * IN_UNKNOWN: Each node created in this state. The instance number of
110 * this node is not known. ind_instance is set to -1.
111 * IN_PROVISIONAL: When a node is assigned an instance number in
112 * e_ddi_assign_instance(), its state is set to IN_PROVISIONAL.
113 * Subsequently, the framework will always call either
114 * e_ddi_keep_instance() which makes the node IN_PERMANENT
115 * or e_ddi_free_instance(), which deletes the node.
116 * IN_PERMANENT:
117 * If e_ddi_keep_instance() is called on an IN_PROVISIONAL node,
118 * its state is set to IN_PERMANENT.
121 static char *instance_file = INSTANCE_FILE;
122 static char *instance_file_backup = INSTANCE_FILE INSTANCE_FILE_SUFFIX;
125 * Return values for in_get_infile().
127 #define PTI_FOUND 0
128 #define PTI_NOT_FOUND 1
129 #define PTI_REBUILD 2
131 int instance_searchme = 0; /* testing: use complex code path */
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.
138 #define PTI_MAGIC_STR "#path_to_inst_bootstrap_1"
139 #define PTI_MAGIC_STR_LEN (sizeof (PTI_MAGIC_STR) - 1)
141 void
142 e_ddi_instance_init(void)
144 char *file;
145 int rebuild = 1;
146 struct in_drv *dp;
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);
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.
158 e_ddi_enter_instance();
161 * Init the ioaliases if the platform supports it
163 if (&plat_ioaliases_init)
164 plat_ioaliases_init();
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.
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);
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;
184 * Something is wrong. First try the backup file.
185 * If not found, rebuild path_to_inst. Emit a
186 * message about the problem.
188 cmn_err(CE_WARN, "%s empty or not found", file);
190 file = instance_file_backup;
191 if (in_get_infile(file) != PTI_FOUND) {
192 cmn_err(CE_NOTE, "rebuilding device instance data");
193 break;
195 cmn_err(CE_NOTE, "using backup instance data in %s", file);
196 /*FALLTHROUGH*/
198 case PTI_FOUND:
200 * We've got a readable file
201 * parse the file into the instance tree
203 (void) read_binding_file(file, NULL, in_pathin);
204 rebuild = 0;
205 break;
207 case PTI_REBUILD:
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.
213 boothowto |= RB_RECONFIG;
214 cmn_err(CE_CONT,
215 "?Using default device instance data\n");
216 break;
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.
227 if (rebuild)
228 in_preassign_instance();
230 e_ddi_exit_instance();
233 static void
234 in_preassign_instance()
236 major_t m;
237 struct devnames *dnp;
238 dev_info_t *dip;
239 extern major_t devcnt;
241 for (m = 0; m < devcnt; m++) {
242 dnp = &devnamesp[m];
243 dip = dnp->dn_head;
244 while (dip) {
245 DEVI(dip)->devi_instance = dnp->dn_instance;
246 dnp->dn_instance++;
247 dip = ddi_get_next(dip);
251 * The preassign instance numbers are not fully
252 * accounted for until e_ddi_assign_instance().
253 * We can't fully account for them now because we
254 * don't currently have a unit-address. Because of
255 * this, we need to remember the preassign boundary
256 * to avoid ordering issues related to
257 * e_ddi_assign_instance of a preassigned value .vs.
258 * re-assignment of the same value for a dynamic
259 * SID node created by bus_config.
261 dnp->dn_pinstance = dnp->dn_instance;
262 dnp->dn_instance = IN_SEARCHME;
267 * Checks to see if the /etc/path_to_inst file exists and whether or not
268 * it has the magic string in it.
270 * Returns one of the following:
272 * PTI_FOUND - We have found the /etc/path_to_inst file
273 * PTI_REBUILD - We have found the /etc/path_to_inst file and the
274 * first line was PTI_MAGIC_STR.
275 * PTI_NOT_FOUND - We did not find the /etc/path_to_inst file
278 static int
279 in_get_infile(char *filename)
281 struct _buf *file;
282 int return_val;
283 char buf[PTI_MAGIC_STR_LEN];
286 * Try to open the file.
288 if ((file = kobj_open_file(filename)) == (struct _buf *)-1) {
289 return (PTI_NOT_FOUND);
291 return_val = PTI_FOUND;
294 * Read the first PTI_MAGIC_STR_LEN bytes from the file to see if
295 * it contains the magic string. If there aren't that many bytes
296 * in the file, then assume file is correct and no magic string
297 * and move on.
299 switch (kobj_read_file(file, buf, PTI_MAGIC_STR_LEN, 0)) {
301 case PTI_MAGIC_STR_LEN:
303 * If the first PTI_MAGIC_STR_LEN bytes are the magic string
304 * then return PTI_REBUILD.
306 if (strncmp(PTI_MAGIC_STR, buf, PTI_MAGIC_STR_LEN) == 0)
307 return_val = PTI_REBUILD;
308 break;
310 case 0:
312 * If the file is zero bytes in length, then consider the
313 * file to not be found
315 return_val = PTI_NOT_FOUND;
317 default: /* Do nothing we have a good file */
318 break;
321 kobj_close_file(file);
322 return (return_val);
326 is_pseudo_device(dev_info_t *dip)
328 dev_info_t *pdip;
330 for (pdip = ddi_get_parent(dip); pdip && pdip != ddi_root_node();
331 pdip = ddi_get_parent(pdip)) {
332 if (strcmp(ddi_get_name(pdip), DEVI_PSEUDO_NEXNAME) == 0)
333 return (1);
335 return (0);
339 static void
340 in_set_instance(dev_info_t *dip, in_drv_t *dp, major_t major)
342 /* use preassigned instance if available */
343 if (DEVI(dip)->devi_instance != -1)
344 dp->ind_instance = DEVI(dip)->devi_instance;
345 else
346 dp->ind_instance = in_next_instance(major);
350 * Return 1 if instance block was assigned for the path.
352 * For multi-port NIC cards, sequential instance assignment across all
353 * ports on a card is highly desirable since the ppa is typically the
354 * same as the instance number, and the ppa is used in the NIC's public
355 * /dev name. This sequential assignment typically occurs as a result
356 * of in_preassign_instance() after initial install, or by
357 * i_ndi_init_hw_children() for NIC ports that share a common parent.
359 * Some NIC cards however use multi-function bridge chips, and to
360 * support sequential instance assignment accross all ports, without
361 * disabling multi-threaded attach, we have a (currently) undocumented
362 * hack to allocate instance numbers in contiguous blocks based on
363 * driver.conf properties.
366 * /---------- ------------\
367 * pci@0 pci@0,1 MULTI-FUNCTION BRIDGE CHIP
368 * / \ / \
369 * FJSV,e4ta@4 FJSV,e4ta@4,1 FJSV,e4ta@6 FJSV,e4ta@6,1 NIC PORTS
370 * n n+2 n+2 n+3 INSTANCE
372 * For the above example, the following driver.conf properties would be
373 * used to guarantee sequential instance number assignment.
375 * ddi-instance-blocks ="ib-FJSVe4ca", "ib-FJSVe4ta", "ib-generic";
376 * ib-FJSVe4ca = "/pci@0/FJSV,e4ca@4", "/pci@0/FJSV,e4ca@4,1",
377 * "/pci@0,1/FJSV,e4ca@6", "/pci@0,1/FJSV,e4ca@6,1";
378 * ib-FJSVe4ta = "/pci@0/FJSV,e4ta@4", "/pci@0/FJSV,e4ta@4,1",
379 * "/pci@0,1/FJSV,e4ta@6", "/pci@0,1/FJSV,e4ta@6,1";
380 * ib-generic = "/pci@0/network@4", "/pci@0/network@4,1",
381 * "/pci@0,1/network@6", "/pci@0,1/network@6,1";
383 * The value of the 'ddi-instance-blocks' property references a series
384 * of card specific properties, like 'ib-FJSV-e4ta', who's value
385 * defines a single 'instance block'. The 'instance block' describes
386 * all the paths below a multi-function bridge, where each path is
387 * called an 'instance path'. The 'instance block' property value is a
388 * series of 'instance paths'. The number of 'instance paths' in an
389 * 'instance block' defines the size of the instance block, and the
390 * ordering of the 'instance paths' defines the instance number
391 * assignment order for paths going through the 'instance block'.
393 * In the instance assignment code below, if a (path, driver) that
394 * currently has no instance number has a path that goes through an
395 * 'instance block', then block instance number allocation occurs. The
396 * block allocation code will find a sequential set of unused instance
397 * numbers, and assign instance numbers for all the paths in the
398 * 'instance block'. Each path is assigned a persistent instance
399 * number, even paths that don't exist in the device tree or fail
400 * probe(9E).
402 static int
403 in_assign_instance_block(dev_info_t *dip)
405 char **ibn; /* instance block names */
406 uint_t nibn; /* number of instance block names */
407 uint_t ibni; /* ibn index */
408 char *driver;
409 major_t major;
410 char *path;
411 char *addr;
412 int plen;
413 char **ibp; /* instance block paths */
414 uint_t nibp; /* number of paths in instance block */
415 uint_t ibpi; /* ibp index */
416 int ibplen; /* length of instance block path */
417 char *ipath;
418 int instance_base;
419 int splice;
420 int i;
422 /* check for fresh install case (in miniroot) */
423 if (DEVI(dip)->devi_instance != -1)
424 return (0); /* already assigned */
427 * Check to see if we need to allocate a block of contiguous instance
428 * numbers by looking for the 'ddi-instance-blocks' property.
430 if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
431 "ddi-instance-blocks", &ibn, &nibn) != DDI_SUCCESS)
432 return (0); /* no instance block needed */
435 * Get information out about node we are processing.
437 * NOTE: Since the node is not yet at DS_INITIALIZED, ddi_pathname()
438 * will not return the unit-address of the final path component even
439 * though the node has an established devi_addr unit-address - so we
440 * need to add the unit-address by hand.
442 driver = (char *)ddi_driver_name(dip);
443 major = ddi_driver_major(dip);
444 path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
445 (void) ddi_pathname(dip, path);
446 if ((addr = ddi_get_name_addr(dip)) != NULL) {
447 (void) strcat(path, "@");
448 (void) strcat(path, addr);
450 plen = strlen(path);
452 /* loop through instance block names */
453 for (ibni = 0; ibni < nibn; ibni++) {
454 if (ibn[ibni] == NULL)
455 continue;
457 /* lookup instance block */
458 if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, dip,
459 DDI_PROP_DONTPASS, ibn[ibni],
460 &ibp, &nibp) != DDI_SUCCESS) {
461 cmn_err(CE_WARN,
462 "no devinition for instance block '%s' in %s.conf",
463 ibn[ibni], driver);
464 continue;
467 /* Does 'path' go through this instance block? */
468 for (ibpi = 0; ibpi < nibp; ibpi++) {
469 if (ibp[ibpi] == NULL)
470 continue;
471 ibplen = strlen(ibp[ibpi]);
472 if ((ibplen <= plen) &&
473 (strcmp(ibp[ibpi], path + plen - ibplen) == 0))
474 break;
477 if (ibpi >= nibp) {
478 ddi_prop_free(ibp);
479 continue; /* no try next instance block */
482 /* yes, allocate and assign instances for all paths in block */
485 * determine where we splice in instance paths and verify
486 * that none of the paths are too long.
488 splice = plen - ibplen;
489 for (i = 0; i < nibp; i++) {
490 if ((splice + strlen(ibp[i])+ 1) >= MAXPATHLEN) {
491 cmn_err(CE_WARN,
492 "path %d through instance block '%s' from "
493 "%s.conf too long", i, ibn[ibni], driver);
494 break;
497 if (i < nibp) {
498 ddi_prop_free(ibp);
499 continue; /* too long */
502 /* allocate the instance block - no more failures */
503 instance_base = in_next_instance_block(major, nibp);
505 ipath = kmem_alloc(MAXPATHLEN, KM_SLEEP);
506 for (ibpi = 0; ibpi < nibp; ibpi++) {
507 if (ibp[ibpi] == NULL)
508 continue;
509 (void) strcpy(ipath, path);
510 (void) strcpy(ipath + splice, ibp[ibpi]);
511 (void) in_pathin(ipath,
512 instance_base + ibpi, driver, NULL);
515 /* free allocations */
516 kmem_free(ipath, MAXPATHLEN);
517 ddi_prop_free(ibp);
518 kmem_free(path, MAXPATHLEN);
519 ddi_prop_free(ibn);
521 /* notify devfsadmd to sync of path_to_inst file */
522 mutex_enter(&e_ddi_inst_state.ins_serial);
523 i_log_devfs_instance_mod();
524 e_ddi_inst_state.ins_dirty = B_TRUE;
525 mutex_exit(&e_ddi_inst_state.ins_serial);
526 return (1);
529 /* our path did not go through any of of the instance blocks */
530 kmem_free(path, MAXPATHLEN);
531 ddi_prop_free(ibn);
532 return (0);
536 * Look up an instance number for a dev_info node, and assign one if it does
537 * not have one (the dev_info node has devi_name and devi_addr already set).
539 uint_t
540 e_ddi_assign_instance(dev_info_t *dip)
542 char *name;
543 in_node_t *ap, *np;
544 in_drv_t *dp;
545 major_t major;
546 uint_t ret;
547 char *bname;
550 * Allow implementation to override
552 if ((ret = impl_assign_instance(dip)) != (uint_t)-1)
553 return (ret);
556 * If this is a pseudo-device, use the instance number
557 * assigned by the pseudo nexus driver. The mutex is
558 * not needed since the instance tree is not used.
560 if (is_pseudo_device(dip)) {
561 return (ddi_get_instance(dip));
565 * Only one thread is allowed to change the state of the instance
566 * number assignments on the system at any given time.
568 e_ddi_enter_instance();
571 * Look for instance node, allocate one if not found
573 np = in_devwalk(dip, &ap, NULL);
574 if (np == NULL) {
575 if (in_assign_instance_block(dip)) {
576 np = in_devwalk(dip, &ap, NULL);
577 } else {
578 name = ddi_node_name(dip);
579 np = in_alloc_node(name, ddi_get_name_addr(dip));
580 ASSERT(np != NULL);
581 in_enlist(ap, np); /* insert into tree */
584 ASSERT(np == in_devwalk(dip, &ap, NULL));
587 * Link the devinfo node and in_node_t
589 if (DEVI(dip)->devi_in_node || np->in_devi) {
590 ddi_err(DER_MODE, dip, "devinfo and instance node (%p) "
591 "interlink fields are not NULL", (void *)np);
593 DEVI(dip)->devi_in_node = np;
594 np->in_devi = dip;
597 * Look for driver entry, allocate one if not found
599 bname = (char *)ddi_driver_name(dip);
600 dp = in_drvwalk(np, bname);
601 if (dp == NULL) {
603 if (ddi_aliases_present == B_TRUE) {
604 e_ddi_borrow_instance(dip, np);
607 if ((dp = in_drvwalk(np, bname)) == NULL) {
608 dp = in_alloc_drv(bname);
609 ASSERT(dp != NULL);
610 major = ddi_driver_major(dip);
611 ASSERT(major != DDI_MAJOR_T_NONE);
612 in_endrv(np, dp);
613 in_set_instance(dip, dp, major);
614 dp->ind_state = IN_PROVISIONAL;
615 in_hashdrv(dp);
616 } else {
617 dp->ind_state = IN_BORROWED;
621 ret = dp->ind_instance;
623 e_ddi_exit_instance();
624 return (ret);
627 static int
628 mkpathname(char *path, in_node_t *np, int len)
630 int len_needed;
632 if (np == e_ddi_inst_state.ins_root)
633 return (DDI_SUCCESS);
635 if (mkpathname(path, np->in_parent, len) == DDI_FAILURE)
636 return (DDI_FAILURE);
638 len_needed = strlen(path);
639 len_needed += strlen(np->in_node_name) + 1; /* for '/' */
640 if (np->in_unit_addr) {
641 len_needed += strlen(np->in_unit_addr) + 1; /* for '@' */
643 len_needed += 1; /* for '\0' */
646 * XX complain
648 if (len_needed > len)
649 return (DDI_FAILURE);
651 if (np->in_unit_addr[0] == '\0')
652 (void) sprintf(path+strlen(path), "/%s", np->in_node_name);
653 else
654 (void) sprintf(path+strlen(path), "/%s@%s", np->in_node_name,
655 np->in_unit_addr);
657 return (DDI_SUCCESS);
661 * produce the path to the given instance of a major number.
662 * path must hold MAXPATHLEN string
665 e_ddi_instance_majorinstance_to_path(major_t major, uint_t inst, char *path)
667 struct devnames *dnp;
668 in_drv_t *dp;
669 int ret;
671 e_ddi_enter_instance();
673 /* look for the instance threaded off major */
674 dnp = &devnamesp[major];
675 for (dp = dnp->dn_inlist; dp != NULL; dp = dp->ind_next)
676 if (dp->ind_instance == inst)
677 break;
679 /* produce path from the node that uses the instance */
680 if (dp) {
681 *path = 0;
682 ret = mkpathname(path, dp->ind_node, MAXPATHLEN);
683 } else
684 ret = DDI_FAILURE;
686 e_ddi_exit_instance();
687 return (ret);
691 * Allocate a sequential block of instance numbers for the specified driver,
692 * and return the base instance number of the block. The implementation
693 * depends on the list being sorted in ascending instance number sequence.
694 * When there are no 'holes' in the allocation sequence, dn_instance is the
695 * next available instance number. When dn_instance is IN_SEARCHME, hole(s)
696 * exists and a slower code path executes which tries to fill holes.
698 * The block returned can't be in the preassigned range.
700 static int
701 in_next_instance_block(major_t major, int block_size)
703 int prev;
704 struct devnames *dnp;
705 in_drv_t *dp;
706 int base;
707 int hole;
709 dnp = &devnamesp[major];
710 ASSERT(major != DDI_MAJOR_T_NONE);
711 ASSERT(e_ddi_inst_state.ins_busy);
712 ASSERT(block_size);
714 /* check to see if we can do a quick allocation */
715 if (!instance_searchme && (dnp->dn_instance != IN_SEARCHME)) {
716 base = dnp->dn_instance;
717 dnp->dn_instance += block_size;
718 return (base);
722 * Use more complex code path, start by skipping preassign entries.
724 for (dp = dnp->dn_inlist; dp; dp = dp->ind_next)
725 if (dp->ind_instance >= dnp->dn_pinstance)
726 break; /* beyond preassign */
728 /* No non-preassign entries, allocate block at preassign base. */
729 if (dp == NULL) {
730 base = dnp->dn_pinstance;
731 if (base == 0)
732 dnp->dn_instance = block_size;
733 return (base);
736 /* See if we fit in hole at beginning (after preassigns) */
737 prev = dp->ind_instance;
738 if ((prev - dnp->dn_pinstance) >= block_size)
739 return (dnp->dn_pinstance); /* we fit in beginning hole */
741 /* search the list for a large enough hole */
742 for (dp = dp->ind_next, hole = 0; dp; dp = dp->ind_next) {
743 if (dp->ind_instance != (prev + 1))
744 hole++; /* we have a hole */
745 if (dp->ind_instance >= (prev + block_size + 1))
746 break; /* we fit in hole */
747 prev = dp->ind_instance;
751 * If hole is zero then all holes are patched and we can resume
752 * quick allocations, but don't resume quick allocation if there is
753 * a preassign.
755 if ((hole == 0) && (dnp->dn_pinstance == 0))
756 dnp->dn_instance = prev + 1 + block_size;
758 return (prev + 1);
761 /* assign instance block of size 1 */
762 static int
763 in_next_instance(major_t major)
765 return (in_next_instance_block(major, 1));
769 * This call causes us to *forget* the instance number we've generated
770 * for a given device if it was not permanent.
772 void
773 e_ddi_free_instance(dev_info_t *dip, char *addr)
775 char *name;
776 in_node_t *np;
777 in_node_t *ap; /* ancestor node */
778 major_t major;
779 struct devnames *dnp;
780 in_drv_t *dp; /* in_drv entry */
783 * Allow implementation override
785 if (impl_free_instance(dip) == DDI_SUCCESS)
786 return;
789 * If this is a pseudo-device, no instance number
790 * was assigned.
792 if (is_pseudo_device(dip)) {
793 return;
796 name = (char *)ddi_driver_name(dip);
797 major = ddi_driver_major(dip);
798 ASSERT(major != DDI_MAJOR_T_NONE);
799 dnp = &devnamesp[major];
801 * Only one thread is allowed to change the state of the instance
802 * number assignments on the system at any given time.
804 e_ddi_enter_instance();
805 np = in_devwalk(dip, &ap, addr);
806 ASSERT(np);
809 * Break the interlink between dip and np
811 if (DEVI(dip)->devi_in_node != np || np->in_devi != dip) {
812 ddi_err(DER_MODE, dip, "devinfo node linked to "
813 "wrong instance node: %p", (void *)np);
815 DEVI(dip)->devi_in_node = NULL;
816 np->in_devi = NULL;
818 dp = in_drvwalk(np, name);
819 ASSERT(dp);
820 if (dp->ind_state == IN_PROVISIONAL) {
821 in_removedrv(dnp, dp);
822 } else if (dp->ind_state == IN_BORROWED) {
823 dp->ind_state = IN_PERMANENT;
824 e_ddi_return_instance(dip, addr, np);
826 if (np->in_drivers == NULL) {
827 in_removenode(dnp, np, ap);
829 e_ddi_exit_instance();
833 * This makes our memory of an instance assignment permanent
835 void
836 e_ddi_keep_instance(dev_info_t *dip)
838 in_node_t *np, *ap;
839 in_drv_t *dp;
841 /* Don't make nulldriver instance assignments permanent */
842 if (ddi_driver_major(dip) == nulldriver_major)
843 return;
846 * Allow implementation override
848 if (impl_keep_instance(dip) == DDI_SUCCESS)
849 return;
852 * Nothing to do for pseudo devices.
854 if (is_pseudo_device(dip))
855 return;
858 * Only one thread is allowed to change the state of the instance
859 * number assignments on the system at any given time.
861 e_ddi_enter_instance();
862 np = in_devwalk(dip, &ap, NULL);
863 ASSERT(np);
864 dp = in_drvwalk(np, (char *)ddi_driver_name(dip));
865 ASSERT(dp);
867 mutex_enter(&e_ddi_inst_state.ins_serial);
868 if (dp->ind_state == IN_PROVISIONAL || dp->ind_state == IN_BORROWED) {
869 dp->ind_state = IN_PERMANENT;
870 i_log_devfs_instance_mod();
871 e_ddi_inst_state.ins_dirty = B_TRUE;
873 mutex_exit(&e_ddi_inst_state.ins_serial);
874 e_ddi_exit_instance();
878 * A new major has been added to the system. Run through the orphan list
879 * and try to attach each one to a driver's list.
881 void
882 e_ddi_unorphan_instance_nos()
884 in_drv_t *dp, *ndp;
887 * disconnect the orphan list, and call in_hashdrv for each item
888 * on it
892 * Only one thread is allowed to change the state of the instance
893 * number assignments on the system at any given time.
895 e_ddi_enter_instance();
896 if (e_ddi_inst_state.ins_no_major == NULL) {
897 e_ddi_exit_instance();
898 return;
901 * Hash instance list to devnames structure of major.
902 * Note that if there is not a valid major number for the
903 * node, in_hashdrv will put it back on the no_major list.
905 dp = e_ddi_inst_state.ins_no_major;
906 e_ddi_inst_state.ins_no_major = NULL;
907 while (dp) {
908 ndp = dp->ind_next;
909 ASSERT(dp->ind_state != IN_UNKNOWN);
910 dp->ind_next = NULL;
911 in_hashdrv(dp);
912 dp = ndp;
914 e_ddi_exit_instance();
917 static void
918 in_removenode(struct devnames *dnp, in_node_t *mp, in_node_t *ap)
920 in_node_t *np;
922 ASSERT(e_ddi_inst_state.ins_busy);
925 * Assertion: parents are always instantiated by the framework
926 * before their children, destroyed after them
928 ASSERT(mp->in_child == NULL);
930 * Assertion: drv entries are always removed before their owning nodes
932 ASSERT(mp->in_drivers == NULL);
934 * Take the node out of the tree
936 if (ap->in_child == mp) {
937 ap->in_child = mp->in_sibling;
938 in_dealloc_node(mp);
939 return;
940 } else {
941 for (np = ap->in_child; np; np = np->in_sibling) {
942 if (np->in_sibling == mp) {
943 np->in_sibling = mp->in_sibling;
944 in_dealloc_node(mp);
945 return;
949 panic("in_removenode dnp %p mp %p", (void *)dnp, (void *)mp);
953 * Recursive ascent
955 * This now only does half the job. It finds the node, then the caller
956 * has to search the node for the binding name
958 static in_node_t *
959 in_devwalk(dev_info_t *dip, in_node_t **ap, char *addr)
961 in_node_t *np;
962 char *name;
964 ASSERT(dip);
965 ASSERT(e_ddi_inst_state.ins_busy);
966 if (dip == ddi_root_node()) {
967 *ap = NULL;
968 return (e_ddi_inst_state.ins_root);
971 * call up to find parent, then look through the list of kids
972 * for a match
974 np = in_devwalk(ddi_get_parent(dip), ap, NULL);
975 if (np == NULL)
976 return (np);
977 *ap = np;
978 np = np->in_child;
979 name = ddi_node_name(dip);
980 if (addr == NULL)
981 addr = ddi_get_name_addr(dip);
983 while (np) {
984 if (in_eqstr(np->in_node_name, name) &&
985 in_eqstr(np->in_unit_addr, addr)) {
986 return (np);
988 np = np->in_sibling;
991 return (np);
995 * Create a node specified by cp and assign it the given instance no.
997 static int
998 in_pathin(char *cp, int instance, char *bname, struct bind **args)
1000 in_node_t *np;
1001 in_drv_t *dp;
1002 char *name;
1004 ASSERT(e_ddi_inst_state.ins_busy);
1005 ASSERT(args == NULL);
1008 * Give a warning to the console.
1009 * return value ignored
1011 if (cp[0] != '/' || instance == -1 || bname == NULL) {
1012 cmn_err(CE_WARN,
1013 "invalid instance file entry %s %d",
1014 cp, instance);
1015 return (0);
1018 if ((name = i_binding_to_drv_name(bname)) != NULL)
1019 bname = name;
1021 np = in_make_path(cp);
1022 ASSERT(np);
1024 dp = in_drvwalk(np, bname);
1025 if (dp != NULL) {
1026 cmn_err(CE_WARN,
1027 "multiple instance number assignments for "
1028 "'%s' (driver %s), %d used",
1029 cp, bname, dp->ind_instance);
1030 return (0);
1033 if (in_inuse(instance, bname)) {
1034 cmn_err(CE_WARN,
1035 "instance already in use: %s %d", cp, instance);
1036 return (0);
1039 dp = in_alloc_drv(bname);
1040 in_endrv(np, dp);
1041 dp->ind_instance = instance;
1042 dp->ind_state = IN_PERMANENT;
1043 in_hashdrv(dp);
1045 return (0);
1049 * Create (or find) the node named by path by recursively descending from the
1050 * root's first child (we ignore the root, which is never named)
1052 static in_node_t *
1053 in_make_path(char *path)
1055 in_node_t *ap; /* ancestor pointer */
1056 in_node_t *np; /* working node pointer */
1057 in_node_t *rp; /* return node pointer */
1058 char buf[MAXPATHLEN]; /* copy of string so we can change it */
1059 char *cp, *name, *addr;
1061 ASSERT(e_ddi_inst_state.ins_busy);
1063 if (path == NULL || path[0] != '/')
1064 return (NULL);
1066 (void) snprintf(buf, sizeof (buf), "%s", path);
1067 cp = buf + 1; /* skip over initial '/' in path */
1068 name = in_name_addr(&cp, &addr);
1070 ap = e_ddi_inst_state.ins_root;
1071 np = e_ddi_inst_state.ins_root->in_child;
1072 rp = np;
1073 while (name) {
1074 while (name && np) {
1075 if (in_eqstr(name, np->in_node_name) &&
1076 in_eqstr(addr, np->in_unit_addr)) {
1077 name = in_name_addr(&cp, &addr);
1078 if (name == NULL)
1079 return (np);
1080 ap = np;
1081 np = np->in_child;
1082 } else {
1083 np = np->in_sibling;
1086 np = in_alloc_node(name, addr);
1087 in_enlist(ap, np); /* insert into tree */
1088 rp = np; /* value to return if we quit */
1089 ap = np; /* new parent */
1090 np = NULL; /* can have no children */
1091 name = in_name_addr(&cp, &addr);
1094 return (rp);
1098 * Insert node np into the tree as one of ap's children.
1100 static void
1101 in_enlist(in_node_t *ap, in_node_t *np)
1103 in_node_t *mp;
1104 ASSERT(e_ddi_inst_state.ins_busy);
1106 * Make this node some other node's child or child's sibling
1108 ASSERT(ap && np);
1109 if (ap->in_child == NULL) {
1110 ap->in_child = np;
1111 } else {
1112 for (mp = ap->in_child; mp; mp = mp->in_sibling)
1113 if (mp->in_sibling == NULL) {
1114 mp->in_sibling = np;
1115 break;
1118 np->in_parent = ap;
1122 * Insert drv entry dp onto a node's driver list
1124 static void
1125 in_endrv(in_node_t *np, in_drv_t *dp)
1127 in_drv_t *mp;
1128 ASSERT(e_ddi_inst_state.ins_busy);
1129 ASSERT(np && dp);
1130 mp = np->in_drivers;
1131 np->in_drivers = dp;
1132 dp->ind_next_drv = mp;
1133 dp->ind_node = np;
1137 * Parse the next name out of the path, null terminate it and update cp.
1138 * caller has copied string so we can mess with it.
1139 * Upon return *cpp points to the next section to be parsed, *addrp points
1140 * to the current address substring (or NULL if none) and we return the
1141 * current name substring (or NULL if none). name and address substrings
1142 * are null terminated in place.
1145 static char *
1146 in_name_addr(char **cpp, char **addrp)
1148 char *namep; /* return value holder */
1149 char *ap; /* pointer to '@' in string */
1150 char *sp; /* pointer to '/' in string */
1152 if (*cpp == NULL || **cpp == '\0') {
1153 *addrp = NULL;
1154 return (NULL);
1156 namep = *cpp;
1157 sp = strchr(*cpp, '/');
1158 if (sp != NULL) { /* more to follow */
1159 *sp = '\0';
1160 *cpp = sp + 1;
1161 } else { /* this is last component. */
1162 *cpp = NULL;
1164 ap = strchr(namep, '@');
1165 if (ap == NULL) {
1166 *addrp = NULL;
1167 } else {
1168 *ap = '\0'; /* terminate the name */
1169 *addrp = ap + 1;
1171 return (namep);
1175 * Allocate a node and storage for name and addr strings, and fill them in.
1177 static in_node_t *
1178 in_alloc_node(char *name, char *addr)
1180 in_node_t *np;
1181 char *cp;
1182 size_t namelen;
1184 ASSERT(e_ddi_inst_state.ins_busy);
1186 * Has name or will become root
1188 ASSERT(name || e_ddi_inst_state.ins_root == NULL);
1189 if (addr == NULL)
1190 addr = "";
1191 if (name == NULL)
1192 namelen = 0;
1193 else
1194 namelen = strlen(name) + 1;
1195 cp = kmem_zalloc(sizeof (in_node_t) + namelen + strlen(addr) + 1,
1196 KM_SLEEP);
1197 np = (in_node_t *)cp;
1198 if (name) {
1199 np->in_node_name = cp + sizeof (in_node_t);
1200 (void) strcpy(np->in_node_name, name);
1202 np->in_unit_addr = cp + sizeof (in_node_t) + namelen;
1203 (void) strcpy(np->in_unit_addr, addr);
1204 return (np);
1208 * Allocate a drv entry and storage for binding name string, and fill it in.
1210 static in_drv_t *
1211 in_alloc_drv(char *bindingname)
1213 in_drv_t *dp;
1214 char *cp;
1215 size_t namelen;
1217 ASSERT(e_ddi_inst_state.ins_busy);
1219 * Has name or will become root
1221 ASSERT(bindingname || e_ddi_inst_state.ins_root == NULL);
1222 if (bindingname == NULL)
1223 namelen = 0;
1224 else
1225 namelen = strlen(bindingname) + 1;
1226 cp = kmem_zalloc(sizeof (in_drv_t) + namelen, KM_SLEEP);
1227 dp = (in_drv_t *)cp;
1228 if (bindingname) {
1229 dp->ind_driver_name = cp + sizeof (in_drv_t);
1230 (void) strcpy(dp->ind_driver_name, bindingname);
1232 dp->ind_state = IN_UNKNOWN;
1233 dp->ind_instance = -1;
1234 return (dp);
1237 static void
1238 in_dealloc_node(in_node_t *np)
1241 * The root node can never be de-allocated
1243 ASSERT(np->in_node_name && np->in_unit_addr);
1244 ASSERT(e_ddi_inst_state.ins_busy);
1245 kmem_free(np, sizeof (in_node_t) + strlen(np->in_node_name)
1246 + strlen(np->in_unit_addr) + 2);
1249 static void
1250 in_dealloc_drv(in_drv_t *dp)
1252 ASSERT(dp->ind_driver_name);
1253 ASSERT(e_ddi_inst_state.ins_busy);
1254 kmem_free(dp, sizeof (in_drv_t) + strlen(dp->ind_driver_name)
1255 + 1);
1259 * Handle the various possible versions of "no address"
1261 static int
1262 in_eqstr(char *a, char *b)
1264 if (a == b) /* covers case where both are nulls */
1265 return (1);
1266 if (a == NULL && *b == 0)
1267 return (1);
1268 if (b == NULL && *a == 0)
1269 return (1);
1270 if (a == NULL || b == NULL)
1271 return (0);
1272 return (strcmp(a, b) == 0);
1276 * Returns true if instance no. is already in use by named driver
1278 static int
1279 in_inuse(int instance, char *name)
1281 major_t major;
1282 in_drv_t *dp;
1283 struct devnames *dnp;
1285 ASSERT(e_ddi_inst_state.ins_busy);
1287 * For now, if we've never heard of this device we assume it is not
1288 * in use, since we can't tell
1289 * XXX could do the weaker search through the nomajor list checking
1290 * XXX for the same name
1292 if ((major = ddi_name_to_major(name)) == DDI_MAJOR_T_NONE)
1293 return (0);
1294 dnp = &devnamesp[major];
1296 dp = dnp->dn_inlist;
1297 while (dp) {
1298 if (dp->ind_instance == instance)
1299 return (1);
1300 dp = dp->ind_next;
1302 return (0);
1305 static void
1306 in_hashdrv(in_drv_t *dp)
1308 struct devnames *dnp;
1309 in_drv_t *mp, *pp;
1310 major_t major;
1312 /* hash to no major list */
1313 major = ddi_name_to_major(dp->ind_driver_name);
1314 if (major == DDI_MAJOR_T_NONE) {
1315 dp->ind_next = e_ddi_inst_state.ins_no_major;
1316 e_ddi_inst_state.ins_no_major = dp;
1317 return;
1321 * dnp->dn_inlist is sorted by instance number.
1322 * Adding a new instance entry may introduce holes,
1323 * set dn_instance to IN_SEARCHME so the next instance
1324 * assignment may fill in holes.
1326 dnp = &devnamesp[major];
1327 pp = mp = dnp->dn_inlist;
1328 if (mp == NULL || dp->ind_instance < mp->ind_instance) {
1329 /* prepend as the first entry, turn on IN_SEARCHME */
1330 dnp->dn_instance = IN_SEARCHME;
1331 dp->ind_next = mp;
1332 dnp->dn_inlist = dp;
1333 return;
1336 ASSERT(mp->ind_instance != dp->ind_instance);
1337 while (mp->ind_instance < dp->ind_instance && mp->ind_next) {
1338 pp = mp;
1339 mp = mp->ind_next;
1340 ASSERT(mp->ind_instance != dp->ind_instance);
1343 if (mp->ind_instance < dp->ind_instance) { /* end of list */
1344 dp->ind_next = NULL;
1345 mp->ind_next = dp;
1346 } else {
1347 dp->ind_next = pp->ind_next;
1348 pp->ind_next = dp;
1353 * Remove a driver entry from the list, given a previous pointer
1355 static void
1356 in_removedrv(struct devnames *dnp, in_drv_t *mp)
1358 in_drv_t *dp;
1359 in_drv_t *prevp;
1361 if (dnp->dn_inlist == mp) { /* head of list */
1362 dnp->dn_inlist = mp->ind_next;
1363 dnp->dn_instance = IN_SEARCHME;
1364 in_dq_drv(mp);
1365 in_dealloc_drv(mp);
1366 return;
1368 prevp = dnp->dn_inlist;
1369 for (dp = prevp->ind_next; dp; dp = dp->ind_next) {
1370 if (dp == mp) { /* found it */
1371 break;
1373 prevp = dp;
1376 ASSERT(dp == mp);
1377 dnp->dn_instance = IN_SEARCHME;
1378 prevp->ind_next = mp->ind_next;
1379 in_dq_drv(mp);
1380 in_dealloc_drv(mp);
1383 static void
1384 in_dq_drv(in_drv_t *mp)
1386 struct in_node *node = mp->ind_node;
1387 in_drv_t *ptr, *prev;
1389 if (mp == node->in_drivers) {
1390 node->in_drivers = mp->ind_next_drv;
1391 return;
1393 prev = node->in_drivers;
1394 for (ptr = prev->ind_next_drv; ptr != NULL;
1395 ptr = ptr->ind_next_drv) {
1396 if (ptr == mp) {
1397 prev->ind_next_drv = ptr->ind_next_drv;
1398 return;
1400 prev = ptr;
1402 panic("in_dq_drv: in_drv not found on node driver list");
1406 in_drv_t *
1407 in_drvwalk(in_node_t *np, char *binding_name)
1409 char *name;
1410 in_drv_t *dp = np->in_drivers;
1411 while (dp) {
1412 if ((name = i_binding_to_drv_name(dp->ind_driver_name))
1413 == NULL) {
1414 name = dp->ind_driver_name;
1416 if (strcmp(binding_name, name) == 0) {
1417 break;
1419 dp = dp->ind_next_drv;
1421 return (dp);
1426 static void
1427 i_log_devfs_instance_mod(void)
1429 sysevent_t *ev;
1430 sysevent_id_t eid;
1431 static int sent_one = 0;
1434 * Prevent unnecessary event generation. Do not generate more than
1435 * one event during boot.
1437 if (sent_one && !i_ddi_io_initialized())
1438 return;
1440 ev = sysevent_alloc(EC_DEVFS, ESC_DEVFS_INSTANCE_MOD, EP_DDI,
1441 SE_NOSLEEP);
1442 if (ev == NULL) {
1443 return;
1445 if (log_sysevent(ev, SE_NOSLEEP, &eid) != 0) {
1446 cmn_err(CE_WARN, "i_log_devfs_instance_mod: failed to post "
1447 "event");
1448 } else {
1449 sent_one = 1;
1451 sysevent_free(ev);
1454 void
1455 e_ddi_enter_instance(void)
1457 mutex_enter(&e_ddi_inst_state.ins_serial);
1458 if (e_ddi_inst_state.ins_thread == curthread)
1459 e_ddi_inst_state.ins_busy++;
1460 else {
1461 while (e_ddi_inst_state.ins_busy)
1462 cv_wait(&e_ddi_inst_state.ins_serial_cv,
1463 &e_ddi_inst_state.ins_serial);
1464 e_ddi_inst_state.ins_thread = curthread;
1465 e_ddi_inst_state.ins_busy = 1;
1467 mutex_exit(&e_ddi_inst_state.ins_serial);
1470 void
1471 e_ddi_exit_instance(void)
1473 mutex_enter(&e_ddi_inst_state.ins_serial);
1474 e_ddi_inst_state.ins_busy--;
1475 if (e_ddi_inst_state.ins_busy == 0) {
1476 cv_broadcast(&e_ddi_inst_state.ins_serial_cv);
1477 e_ddi_inst_state.ins_thread = NULL;
1479 mutex_exit(&e_ddi_inst_state.ins_serial);
1483 e_ddi_instance_is_clean(void)
1485 return (e_ddi_inst_state.ins_dirty == B_FALSE);
1488 void
1489 e_ddi_instance_set_clean(void)
1491 e_ddi_inst_state.ins_dirty = B_FALSE;
1494 in_node_t *
1495 e_ddi_instance_root(void)
1497 return (e_ddi_inst_state.ins_root);
1501 * Visit a node in the instance tree
1503 static int
1504 in_walk_instances(in_node_t *np, char *path, char *this,
1505 int (*f)(const char *, in_node_t *, in_drv_t *, void *), void *arg)
1507 in_drv_t *dp;
1508 int rval = INST_WALK_CONTINUE;
1509 char *next;
1511 while (np != NULL) {
1513 if (np->in_unit_addr[0] == 0)
1514 (void) sprintf(this, "/%s", np->in_node_name);
1515 else
1516 (void) sprintf(this, "/%s@%s", np->in_node_name,
1517 np->in_unit_addr);
1518 next = this + strlen(this);
1520 for (dp = np->in_drivers; dp; dp = dp->ind_next_drv) {
1521 if (dp->ind_state == IN_PERMANENT) {
1522 rval = (*f)(path, np, dp, arg);
1523 if (rval == INST_WALK_TERMINATE)
1524 break;
1528 if (np->in_child) {
1529 rval = in_walk_instances(np->in_child,
1530 path, next, f, arg);
1531 if (rval == INST_WALK_TERMINATE)
1532 break;
1535 np = np->in_sibling;
1538 return (rval);
1542 * A general interface for walking the instance tree,
1543 * calling a user-supplied callback for each node.
1546 e_ddi_walk_instances(int (*f)(const char *,
1547 in_node_t *, in_drv_t *, void *), void *arg)
1549 in_node_t *root;
1550 int rval;
1551 char *path;
1553 path = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
1555 e_ddi_enter_instance();
1556 root = e_ddi_instance_root();
1557 rval = in_walk_instances(root->in_child, path, path, f, arg);
1559 e_ddi_exit_instance();
1561 kmem_free(path, MAXPATHLEN);
1562 return (rval);
1565 in_node_t *
1566 e_ddi_path_to_instance(char *path)
1568 in_node_t *np;
1570 np = in_make_path(path);
1571 if (np && np->in_drivers && np->in_drivers->ind_state == IN_PERMANENT) {
1572 return (np);
1574 return (NULL);
1577 void
1578 e_ddi_borrow_instance(dev_info_t *cdip, in_node_t *cnp)
1580 char *alias;
1581 in_node_t *anp;
1582 char *curr = kmem_alloc(MAXPATHLEN, KM_NOSLEEP);
1584 if (curr == NULL) {
1585 ddi_err(DER_PANIC, cdip, "curr alloc failed");
1586 /*NOTREACHED*/
1589 (void) ddi_pathname(cdip, curr);
1591 if (cnp->in_drivers) {
1592 /* there can be multiple drivers bound */
1593 ddi_err(DER_LOG, cdip, "%s has previous binding: %s", curr,
1594 cnp->in_drivers->ind_driver_name);
1597 alias = ddi_curr_redirect(curr);
1599 /* bail here if the alias matches any other current path or itself */
1600 if (alias && ((strcmp(curr, alias) == 0) ||
1601 (ddi_curr_redirect(alias) != 0))) {
1602 DDI_MP_DBG((CE_NOTE, "not borrowing current: %s alias: %s",
1603 curr, alias));
1604 goto out;
1607 if (alias && (anp = e_ddi_path_to_instance(alias)) != NULL) {
1609 * Since pcieb nodes can split and merge, it is dangerous
1610 * to borrow and instance for them. However since they do
1611 * not expose their instance numbers it is safe to never
1612 * borrow one.
1614 if (anp->in_drivers->ind_driver_name &&
1615 (strcmp(anp->in_drivers->ind_driver_name, "pcieb") == 0)) {
1616 DDI_MP_DBG((CE_NOTE, "not borrowing pcieb: "
1617 "%s alias: %s", curr, alias));
1618 goto out;
1620 DDI_MP_DBG((CE_NOTE, "borrowing current: %s alias: %s",
1621 curr, alias));
1622 cnp->in_drivers = anp->in_drivers;
1623 anp->in_drivers = NULL;
1625 out:
1626 kmem_free(curr, MAXPATHLEN);
1629 void
1630 e_ddi_return_instance(dev_info_t *cdip, char *addr, in_node_t *cnp)
1632 in_node_t *anp;
1633 char *alias;
1634 char *curr = kmem_alloc(MAXPATHLEN, KM_NOSLEEP);
1636 if (curr == NULL) {
1637 ddi_err(DER_PANIC, cdip, "alloc of curr failed");
1638 /*NOTREACHED*/
1641 (void) ddi_pathname(cdip, curr);
1642 if (addr) {
1643 (void) strlcat(curr, "@", MAXPATHLEN);
1644 (void) strlcat(curr, addr, MAXPATHLEN);
1647 if (cnp->in_drivers == NULL) {
1648 ddi_err(DER_PANIC, cdip, "cnp has no inst: %p", cnp);
1649 /*NOTREACHED*/
1652 alias = ddi_curr_redirect(curr);
1653 kmem_free(curr, MAXPATHLEN);
1655 if (alias && (anp = e_ddi_path_to_instance(alias)) != NULL) {
1656 ASSERT(anp->in_drivers == NULL);
1657 anp->in_drivers = cnp->in_drivers;
1658 cnp->in_drivers = NULL;