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]
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>
34 #include <sys/t_lock.h>
36 #include <sys/cmn_err.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
93 kcondvar_t ins_serial_cv
;
95 boolean_t ins_dirty
; /* instance info needs flush */
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.
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().
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)
142 e_ddi_instance_init(void)
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
)) {
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");
195 cmn_err(CE_NOTE
, "using backup instance data in %s", file
);
200 * We've got a readable file
201 * parse the file into the instance tree
203 (void) read_binding_file(file
, NULL
, in_pathin
);
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
;
215 "?Using default device instance data\n");
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
228 in_preassign_instance();
230 e_ddi_exit_instance();
234 in_preassign_instance()
237 struct devnames
*dnp
;
239 extern major_t devcnt
;
241 for (m
= 0; m
< devcnt
; m
++) {
245 DEVI(dip
)->devi_instance
= 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
279 in_get_infile(char *filename
)
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
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
;
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 */
321 kobj_close_file(file
);
326 is_pseudo_device(dev_info_t
*dip
)
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)
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
;
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
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
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 */
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 */
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
);
452 /* loop through instance block names */
453 for (ibni
= 0; ibni
< nibn
; ibni
++) {
454 if (ibn
[ibni
] == NULL
)
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
) {
462 "no devinition for instance block '%s' in %s.conf",
467 /* Does 'path' go through this instance block? */
468 for (ibpi
= 0; ibpi
< nibp
; ibpi
++) {
469 if (ibp
[ibpi
] == NULL
)
471 ibplen
= strlen(ibp
[ibpi
]);
472 if ((ibplen
<= plen
) &&
473 (strcmp(ibp
[ibpi
], path
+ plen
- ibplen
) == 0))
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
) {
492 "path %d through instance block '%s' from "
493 "%s.conf too long", i
, ibn
[ibni
], driver
);
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
)
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
);
518 kmem_free(path
, MAXPATHLEN
);
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
);
529 /* our path did not go through any of of the instance blocks */
530 kmem_free(path
, MAXPATHLEN
);
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).
540 e_ddi_assign_instance(dev_info_t
*dip
)
550 * Allow implementation to override
552 if ((ret
= impl_assign_instance(dip
)) != (uint_t
)-1)
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
);
575 if (in_assign_instance_block(dip
)) {
576 np
= in_devwalk(dip
, &ap
, NULL
);
578 name
= ddi_node_name(dip
);
579 np
= in_alloc_node(name
, ddi_get_name_addr(dip
));
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
;
597 * Look for driver entry, allocate one if not found
599 bname
= (char *)ddi_driver_name(dip
);
600 dp
= in_drvwalk(np
, bname
);
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
);
610 major
= ddi_driver_major(dip
);
611 ASSERT(major
!= DDI_MAJOR_T_NONE
);
613 in_set_instance(dip
, dp
, major
);
614 dp
->ind_state
= IN_PROVISIONAL
;
617 dp
->ind_state
= IN_BORROWED
;
621 ret
= dp
->ind_instance
;
623 e_ddi_exit_instance();
628 mkpathname(char *path
, in_node_t
*np
, int len
)
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' */
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
);
654 (void) sprintf(path
+strlen(path
), "/%s@%s", np
->in_node_name
,
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
;
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
)
679 /* produce path from the node that uses the instance */
682 ret
= mkpathname(path
, dp
->ind_node
, MAXPATHLEN
);
686 e_ddi_exit_instance();
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.
701 in_next_instance_block(major_t major
, int block_size
)
704 struct devnames
*dnp
;
709 dnp
= &devnamesp
[major
];
710 ASSERT(major
!= DDI_MAJOR_T_NONE
);
711 ASSERT(e_ddi_inst_state
.ins_busy
);
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
;
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. */
730 base
= dnp
->dn_pinstance
;
732 dnp
->dn_instance
= block_size
;
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
755 if ((hole
== 0) && (dnp
->dn_pinstance
== 0))
756 dnp
->dn_instance
= prev
+ 1 + block_size
;
761 /* assign instance block of size 1 */
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.
773 e_ddi_free_instance(dev_info_t
*dip
, char *addr
)
777 in_node_t
*ap
; /* ancestor node */
779 struct devnames
*dnp
;
780 in_drv_t
*dp
; /* in_drv entry */
783 * Allow implementation override
785 if (impl_free_instance(dip
) == DDI_SUCCESS
)
789 * If this is a pseudo-device, no instance number
792 if (is_pseudo_device(dip
)) {
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
);
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
;
818 dp
= in_drvwalk(np
, name
);
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
836 e_ddi_keep_instance(dev_info_t
*dip
)
841 /* Don't make nulldriver instance assignments permanent */
842 if (ddi_driver_major(dip
) == nulldriver_major
)
846 * Allow implementation override
848 if (impl_keep_instance(dip
) == DDI_SUCCESS
)
852 * Nothing to do for pseudo devices.
854 if (is_pseudo_device(dip
))
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
);
864 dp
= in_drvwalk(np
, (char *)ddi_driver_name(dip
));
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.
882 e_ddi_unorphan_instance_nos()
887 * disconnect the orphan list, and call in_hashdrv for each item
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();
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
;
909 ASSERT(dp
->ind_state
!= IN_UNKNOWN
);
914 e_ddi_exit_instance();
918 in_removenode(struct devnames
*dnp
, in_node_t
*mp
, in_node_t
*ap
)
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
;
941 for (np
= ap
->in_child
; np
; np
= np
->in_sibling
) {
942 if (np
->in_sibling
== mp
) {
943 np
->in_sibling
= mp
->in_sibling
;
949 panic("in_removenode dnp %p mp %p", (void *)dnp
, (void *)mp
);
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
959 in_devwalk(dev_info_t
*dip
, in_node_t
**ap
, char *addr
)
965 ASSERT(e_ddi_inst_state
.ins_busy
);
966 if (dip
== ddi_root_node()) {
968 return (e_ddi_inst_state
.ins_root
);
971 * call up to find parent, then look through the list of kids
974 np
= in_devwalk(ddi_get_parent(dip
), ap
, NULL
);
979 name
= ddi_node_name(dip
);
981 addr
= ddi_get_name_addr(dip
);
984 if (in_eqstr(np
->in_node_name
, name
) &&
985 in_eqstr(np
->in_unit_addr
, addr
)) {
995 * Create a node specified by cp and assign it the given instance no.
998 in_pathin(char *cp
, int instance
, char *bname
, struct bind
**args
)
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
) {
1013 "invalid instance file entry %s %d",
1018 if ((name
= i_binding_to_drv_name(bname
)) != NULL
)
1021 np
= in_make_path(cp
);
1024 dp
= in_drvwalk(np
, bname
);
1027 "multiple instance number assignments for "
1028 "'%s' (driver %s), %d used",
1029 cp
, bname
, dp
->ind_instance
);
1033 if (in_inuse(instance
, bname
)) {
1035 "instance already in use: %s %d", cp
, instance
);
1039 dp
= in_alloc_drv(bname
);
1041 dp
->ind_instance
= instance
;
1042 dp
->ind_state
= IN_PERMANENT
;
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)
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] != '/')
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
;
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
);
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
);
1098 * Insert node np into the tree as one of ap's children.
1101 in_enlist(in_node_t
*ap
, in_node_t
*np
)
1104 ASSERT(e_ddi_inst_state
.ins_busy
);
1106 * Make this node some other node's child or child's sibling
1109 if (ap
->in_child
== NULL
) {
1112 for (mp
= ap
->in_child
; mp
; mp
= mp
->in_sibling
)
1113 if (mp
->in_sibling
== NULL
) {
1114 mp
->in_sibling
= np
;
1122 * Insert drv entry dp onto a node's driver list
1125 in_endrv(in_node_t
*np
, in_drv_t
*dp
)
1128 ASSERT(e_ddi_inst_state
.ins_busy
);
1130 mp
= np
->in_drivers
;
1131 np
->in_drivers
= dp
;
1132 dp
->ind_next_drv
= mp
;
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.
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') {
1157 sp
= strchr(*cpp
, '/');
1158 if (sp
!= NULL
) { /* more to follow */
1161 } else { /* this is last component. */
1164 ap
= strchr(namep
, '@');
1168 *ap
= '\0'; /* terminate the name */
1175 * Allocate a node and storage for name and addr strings, and fill them in.
1178 in_alloc_node(char *name
, char *addr
)
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
);
1194 namelen
= strlen(name
) + 1;
1195 cp
= kmem_zalloc(sizeof (in_node_t
) + namelen
+ strlen(addr
) + 1,
1197 np
= (in_node_t
*)cp
;
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
);
1208 * Allocate a drv entry and storage for binding name string, and fill it in.
1211 in_alloc_drv(char *bindingname
)
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
)
1225 namelen
= strlen(bindingname
) + 1;
1226 cp
= kmem_zalloc(sizeof (in_drv_t
) + namelen
, KM_SLEEP
);
1227 dp
= (in_drv_t
*)cp
;
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;
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);
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
)
1259 * Handle the various possible versions of "no address"
1262 in_eqstr(char *a
, char *b
)
1264 if (a
== b
) /* covers case where both are nulls */
1266 if (a
== NULL
&& *b
== 0)
1268 if (b
== NULL
&& *a
== 0)
1270 if (a
== NULL
|| b
== NULL
)
1272 return (strcmp(a
, b
) == 0);
1276 * Returns true if instance no. is already in use by named driver
1279 in_inuse(int instance
, char *name
)
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
)
1294 dnp
= &devnamesp
[major
];
1296 dp
= dnp
->dn_inlist
;
1298 if (dp
->ind_instance
== instance
)
1306 in_hashdrv(in_drv_t
*dp
)
1308 struct devnames
*dnp
;
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
;
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
;
1332 dnp
->dn_inlist
= dp
;
1336 ASSERT(mp
->ind_instance
!= dp
->ind_instance
);
1337 while (mp
->ind_instance
< dp
->ind_instance
&& 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
;
1347 dp
->ind_next
= pp
->ind_next
;
1353 * Remove a driver entry from the list, given a previous pointer
1356 in_removedrv(struct devnames
*dnp
, in_drv_t
*mp
)
1361 if (dnp
->dn_inlist
== mp
) { /* head of list */
1362 dnp
->dn_inlist
= mp
->ind_next
;
1363 dnp
->dn_instance
= IN_SEARCHME
;
1368 prevp
= dnp
->dn_inlist
;
1369 for (dp
= prevp
->ind_next
; dp
; dp
= dp
->ind_next
) {
1370 if (dp
== mp
) { /* found it */
1377 dnp
->dn_instance
= IN_SEARCHME
;
1378 prevp
->ind_next
= mp
->ind_next
;
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
;
1393 prev
= node
->in_drivers
;
1394 for (ptr
= prev
->ind_next_drv
; ptr
!= NULL
;
1395 ptr
= ptr
->ind_next_drv
) {
1397 prev
->ind_next_drv
= ptr
->ind_next_drv
;
1402 panic("in_dq_drv: in_drv not found on node driver list");
1407 in_drvwalk(in_node_t
*np
, char *binding_name
)
1410 in_drv_t
*dp
= np
->in_drivers
;
1412 if ((name
= i_binding_to_drv_name(dp
->ind_driver_name
))
1414 name
= dp
->ind_driver_name
;
1416 if (strcmp(binding_name
, name
) == 0) {
1419 dp
= dp
->ind_next_drv
;
1427 i_log_devfs_instance_mod(void)
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())
1440 ev
= sysevent_alloc(EC_DEVFS
, ESC_DEVFS_INSTANCE_MOD
, EP_DDI
,
1445 if (log_sysevent(ev
, SE_NOSLEEP
, &eid
) != 0) {
1446 cmn_err(CE_WARN
, "i_log_devfs_instance_mod: failed to post "
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
++;
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
);
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
);
1489 e_ddi_instance_set_clean(void)
1491 e_ddi_inst_state
.ins_dirty
= B_FALSE
;
1495 e_ddi_instance_root(void)
1497 return (e_ddi_inst_state
.ins_root
);
1501 * Visit a node in the instance tree
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
)
1508 int rval
= INST_WALK_CONTINUE
;
1511 while (np
!= NULL
) {
1513 if (np
->in_unit_addr
[0] == 0)
1514 (void) sprintf(this, "/%s", np
->in_node_name
);
1516 (void) sprintf(this, "/%s@%s", np
->in_node_name
,
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
)
1529 rval
= in_walk_instances(np
->in_child
,
1530 path
, next
, f
, arg
);
1531 if (rval
== INST_WALK_TERMINATE
)
1535 np
= np
->in_sibling
;
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
)
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
);
1566 e_ddi_path_to_instance(char *path
)
1570 np
= in_make_path(path
);
1571 if (np
&& np
->in_drivers
&& np
->in_drivers
->ind_state
== IN_PERMANENT
) {
1578 e_ddi_borrow_instance(dev_info_t
*cdip
, in_node_t
*cnp
)
1582 char *curr
= kmem_alloc(MAXPATHLEN
, KM_NOSLEEP
);
1585 ddi_err(DER_PANIC
, cdip
, "curr alloc failed");
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",
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
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
));
1620 DDI_MP_DBG((CE_NOTE
, "borrowing current: %s alias: %s",
1622 cnp
->in_drivers
= anp
->in_drivers
;
1623 anp
->in_drivers
= NULL
;
1626 kmem_free(curr
, MAXPATHLEN
);
1630 e_ddi_return_instance(dev_info_t
*cdip
, char *addr
, in_node_t
*cnp
)
1634 char *curr
= kmem_alloc(MAXPATHLEN
, KM_NOSLEEP
);
1637 ddi_err(DER_PANIC
, cdip
, "alloc of curr failed");
1641 (void) ddi_pathname(cdip
, curr
);
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
);
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
;