17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 516bd7258Scth * Common Development and Distribution License (the "License"). 616bd7258Scth * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate /* 22a204de77Scth * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate /* 277c478bd9Sstevel@tonic-gate * Instance number assignment code 287c478bd9Sstevel@tonic-gate */ 297c478bd9Sstevel@tonic-gate 307c478bd9Sstevel@tonic-gate #include <sys/types.h> 317c478bd9Sstevel@tonic-gate #include <sys/param.h> 327c478bd9Sstevel@tonic-gate #include <sys/errno.h> 337c478bd9Sstevel@tonic-gate #include <sys/systm.h> 347c478bd9Sstevel@tonic-gate #include <sys/kobj.h> 357c478bd9Sstevel@tonic-gate #include <sys/t_lock.h> 367c478bd9Sstevel@tonic-gate #include <sys/kmem.h> 377c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h> 387c478bd9Sstevel@tonic-gate #include <sys/ddi.h> 397c478bd9Sstevel@tonic-gate #include <sys/sunddi.h> 407c478bd9Sstevel@tonic-gate #include <sys/autoconf.h> 417c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h> 427c478bd9Sstevel@tonic-gate #include <sys/hwconf.h> 437c478bd9Sstevel@tonic-gate #include <sys/reboot.h> 447c478bd9Sstevel@tonic-gate #include <sys/ddi_impldefs.h> 457c478bd9Sstevel@tonic-gate #include <sys/instance.h> 467c478bd9Sstevel@tonic-gate #include <sys/debug.h> 477c478bd9Sstevel@tonic-gate #include <sys/sysevent.h> 487c478bd9Sstevel@tonic-gate #include <sys/modctl.h> 497c478bd9Sstevel@tonic-gate #include <sys/console.h> 507c478bd9Sstevel@tonic-gate #include <sys/cladm.h> 517c478bd9Sstevel@tonic-gate 527c478bd9Sstevel@tonic-gate static void in_preassign_instance(void); 537c478bd9Sstevel@tonic-gate static void i_log_devfs_instance_mod(void); 547c478bd9Sstevel@tonic-gate static int in_get_infile(char *); 557c478bd9Sstevel@tonic-gate static void in_removenode(struct devnames *dnp, in_node_t *mp, in_node_t *ap); 567c478bd9Sstevel@tonic-gate static in_node_t *in_alloc_node(char *name, char *addr); 577c478bd9Sstevel@tonic-gate static int in_eqstr(char *a, char *b); 587c478bd9Sstevel@tonic-gate static char *in_name_addr(char **cpp, char **addrp); 597c478bd9Sstevel@tonic-gate static in_node_t *in_devwalk(dev_info_t *dip, in_node_t **ap, char *addr); 607c478bd9Sstevel@tonic-gate static void in_dealloc_node(in_node_t *np); 617c478bd9Sstevel@tonic-gate static in_node_t *in_make_path(char *path); 627c478bd9Sstevel@tonic-gate static void in_enlist(in_node_t *ap, in_node_t *np); 637c478bd9Sstevel@tonic-gate static int in_inuse(int instance, char *name); 647c478bd9Sstevel@tonic-gate static void in_hashdrv(in_drv_t *dp); 657c478bd9Sstevel@tonic-gate static in_drv_t *in_drvwalk(in_node_t *np, char *binding_name); 667c478bd9Sstevel@tonic-gate static in_drv_t *in_alloc_drv(char *bindingname); 677c478bd9Sstevel@tonic-gate static void in_endrv(in_node_t *np, in_drv_t *dp); 687c478bd9Sstevel@tonic-gate static void in_dq_drv(in_drv_t *np); 697c478bd9Sstevel@tonic-gate static void in_removedrv(struct devnames *dnp, in_drv_t *mp); 707c478bd9Sstevel@tonic-gate static int in_pathin(char *cp, int instance, char *bname, struct bind **args); 7116bd7258Scth static int in_next_instance_block(major_t, int); 727c478bd9Sstevel@tonic-gate static int in_next_instance(major_t); 737c478bd9Sstevel@tonic-gate 747c478bd9Sstevel@tonic-gate /* external functions */ 757c478bd9Sstevel@tonic-gate extern char *i_binding_to_drv_name(char *bname); 767c478bd9Sstevel@tonic-gate 777c478bd9Sstevel@tonic-gate /* 787c478bd9Sstevel@tonic-gate * This plus devnames defines the entire software state of the instance world. 797c478bd9Sstevel@tonic-gate */ 807c478bd9Sstevel@tonic-gate typedef struct in_softstate { 817c478bd9Sstevel@tonic-gate in_node_t *ins_root; /* the root of our instance tree */ 827c478bd9Sstevel@tonic-gate in_drv_t *ins_no_major; /* majorless drv entries */ 837c478bd9Sstevel@tonic-gate /* 847c478bd9Sstevel@tonic-gate * Used to serialize access to data structures 857c478bd9Sstevel@tonic-gate */ 867c478bd9Sstevel@tonic-gate void *ins_thread; 877c478bd9Sstevel@tonic-gate kmutex_t ins_serial; 887c478bd9Sstevel@tonic-gate kcondvar_t ins_serial_cv; 897c478bd9Sstevel@tonic-gate int ins_busy; 907c478bd9Sstevel@tonic-gate char ins_dirty; /* need flush */ 917c478bd9Sstevel@tonic-gate } in_softstate_t; 927c478bd9Sstevel@tonic-gate 937c478bd9Sstevel@tonic-gate static in_softstate_t e_ddi_inst_state; 947c478bd9Sstevel@tonic-gate 957c478bd9Sstevel@tonic-gate /* 967c478bd9Sstevel@tonic-gate * State transition information: 977c478bd9Sstevel@tonic-gate * e_ddi_inst_state contains, among other things, the root of a tree of 987c478bd9Sstevel@tonic-gate * device nodes used to track instance number assignments. 997c478bd9Sstevel@tonic-gate * Each device node may contain multiple driver bindings, represented 1007c478bd9Sstevel@tonic-gate * by a linked list of in_drv_t nodes, each with an instance assignment 1017c478bd9Sstevel@tonic-gate * (except for root node). Each in_drv node can be in one of 3 states, 1027c478bd9Sstevel@tonic-gate * indicated by ind_state: 1037c478bd9Sstevel@tonic-gate * 1047c478bd9Sstevel@tonic-gate * IN_UNKNOWN: Each node created in this state. The instance number of 1057c478bd9Sstevel@tonic-gate * this node is not known. ind_instance is set to -1. 1067c478bd9Sstevel@tonic-gate * IN_PROVISIONAL: When a node is assigned an instance number in 1077c478bd9Sstevel@tonic-gate * e_ddi_assign_instance(), its state is set to IN_PROVISIONAL. 1087c478bd9Sstevel@tonic-gate * Subsequently, the framework will always call either 1097c478bd9Sstevel@tonic-gate * e_ddi_keep_instance() which makes the node IN_PERMANENT, 1107c478bd9Sstevel@tonic-gate * or e_ddi_free_instance(), which deletes the node. 1117c478bd9Sstevel@tonic-gate * IN_PERMANENT: 1127c478bd9Sstevel@tonic-gate * If e_ddi_keep_instance() is called on an IN_PROVISIONAL node, 1137c478bd9Sstevel@tonic-gate * its state is set to IN_PERMANENT. 1147c478bd9Sstevel@tonic-gate */ 1157c478bd9Sstevel@tonic-gate 1167c478bd9Sstevel@tonic-gate static char *instance_file = INSTANCE_FILE; 1177c478bd9Sstevel@tonic-gate static char *instance_file_backup = INSTANCE_FILE INSTANCE_FILE_SUFFIX; 1187c478bd9Sstevel@tonic-gate 1197c478bd9Sstevel@tonic-gate /* 1207c478bd9Sstevel@tonic-gate * Return values for in_get_infile(). 1217c478bd9Sstevel@tonic-gate */ 1227c478bd9Sstevel@tonic-gate #define PTI_FOUND 0 1237c478bd9Sstevel@tonic-gate #define PTI_NOT_FOUND 1 1247c478bd9Sstevel@tonic-gate #define PTI_REBUILD 2 1257c478bd9Sstevel@tonic-gate 1267c478bd9Sstevel@tonic-gate /* 1277c478bd9Sstevel@tonic-gate * Path to instance file magic string used for first time boot after 1287c478bd9Sstevel@tonic-gate * an install. If this is the first string in the file we will 1297c478bd9Sstevel@tonic-gate * automatically rebuild the file. 1307c478bd9Sstevel@tonic-gate */ 1317c478bd9Sstevel@tonic-gate #define PTI_MAGIC_STR "#path_to_inst_bootstrap_1" 1327c478bd9Sstevel@tonic-gate #define PTI_MAGIC_STR_LEN (sizeof (PTI_MAGIC_STR) - 1) 1337c478bd9Sstevel@tonic-gate 1347c478bd9Sstevel@tonic-gate void 1357c478bd9Sstevel@tonic-gate e_ddi_instance_init(void) 1367c478bd9Sstevel@tonic-gate { 1377c478bd9Sstevel@tonic-gate char *file; 1387c478bd9Sstevel@tonic-gate int rebuild = 1; 1397c478bd9Sstevel@tonic-gate struct in_drv *dp; 1407c478bd9Sstevel@tonic-gate 1417c478bd9Sstevel@tonic-gate mutex_init(&e_ddi_inst_state.ins_serial, NULL, MUTEX_DEFAULT, NULL); 1427c478bd9Sstevel@tonic-gate cv_init(&e_ddi_inst_state.ins_serial_cv, NULL, CV_DEFAULT, NULL); 1437c478bd9Sstevel@tonic-gate 1447c478bd9Sstevel@tonic-gate /* 1457c478bd9Sstevel@tonic-gate * Only one thread is allowed to change the state of the instance 1467c478bd9Sstevel@tonic-gate * number assignments on the system at any given time. 1477c478bd9Sstevel@tonic-gate * Note that this is not really necessary, as we are single-threaded 1487c478bd9Sstevel@tonic-gate * here, but it won't hurt, and it allows us to keep ASSERTS for 1497c478bd9Sstevel@tonic-gate * our assumptions in the code. 1507c478bd9Sstevel@tonic-gate */ 1517c478bd9Sstevel@tonic-gate e_ddi_enter_instance(); 1527c478bd9Sstevel@tonic-gate 1537c478bd9Sstevel@tonic-gate /* 1547c478bd9Sstevel@tonic-gate * Create the root node, instance zallocs to 0. 1557c478bd9Sstevel@tonic-gate * The name and address of this node never get examined, we always 1567c478bd9Sstevel@tonic-gate * start searching with its first child. 1577c478bd9Sstevel@tonic-gate */ 1587c478bd9Sstevel@tonic-gate ASSERT(e_ddi_inst_state.ins_root == NULL); 1597c478bd9Sstevel@tonic-gate e_ddi_inst_state.ins_root = in_alloc_node(NULL, NULL); 1607c478bd9Sstevel@tonic-gate dp = in_alloc_drv("rootnex"); 1617c478bd9Sstevel@tonic-gate in_endrv(e_ddi_inst_state.ins_root, dp); 1627c478bd9Sstevel@tonic-gate 1637c478bd9Sstevel@tonic-gate file = instance_file; 1647c478bd9Sstevel@tonic-gate switch (in_get_infile(file)) { 1657c478bd9Sstevel@tonic-gate default: 1667c478bd9Sstevel@tonic-gate case PTI_NOT_FOUND: 1677c478bd9Sstevel@tonic-gate /* make sure path_to_inst is recreated */ 1687c478bd9Sstevel@tonic-gate boothowto |= RB_RECONFIG; 1697c478bd9Sstevel@tonic-gate 1707c478bd9Sstevel@tonic-gate /* 1717c478bd9Sstevel@tonic-gate * Something is wrong. First try the backup file. 1727c478bd9Sstevel@tonic-gate * If not found, rebuild path_to_inst. Emit a 1737c478bd9Sstevel@tonic-gate * message about the problem. 1747c478bd9Sstevel@tonic-gate */ 1757c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "%s empty or not found", file); 1767c478bd9Sstevel@tonic-gate 1777c478bd9Sstevel@tonic-gate file = instance_file_backup; 1787c478bd9Sstevel@tonic-gate if (in_get_infile(file) != PTI_FOUND) { 1797c478bd9Sstevel@tonic-gate cmn_err(CE_NOTE, "rebuilding device instance data"); 1807c478bd9Sstevel@tonic-gate break; 1817c478bd9Sstevel@tonic-gate } 1827c478bd9Sstevel@tonic-gate cmn_err(CE_NOTE, "using backup instance data in %s", file); 1837c478bd9Sstevel@tonic-gate /*FALLTHROUGH*/ 1847c478bd9Sstevel@tonic-gate 1857c478bd9Sstevel@tonic-gate case PTI_FOUND: 1867c478bd9Sstevel@tonic-gate /* 1877c478bd9Sstevel@tonic-gate * We've got a readable file 1887c478bd9Sstevel@tonic-gate * parse the file into the instance tree 1897c478bd9Sstevel@tonic-gate */ 1907c478bd9Sstevel@tonic-gate (void) read_binding_file(file, NULL, in_pathin); 1917c478bd9Sstevel@tonic-gate rebuild = 0; 1927c478bd9Sstevel@tonic-gate break; 1937c478bd9Sstevel@tonic-gate 1947c478bd9Sstevel@tonic-gate case PTI_REBUILD: 1957c478bd9Sstevel@tonic-gate cmn_err(CE_CONT, 1967c478bd9Sstevel@tonic-gate "?Using default device instance data\n"); 1977c478bd9Sstevel@tonic-gate break; 1987c478bd9Sstevel@tonic-gate } 1997c478bd9Sstevel@tonic-gate 2007c478bd9Sstevel@tonic-gate /* 2017c478bd9Sstevel@tonic-gate * The OBP device tree has been copied to the kernel and 2027c478bd9Sstevel@tonic-gate * bound to drivers at this point. We walk the per-driver 2037c478bd9Sstevel@tonic-gate * list to preassign instances. Since the bus addr is 2047c478bd9Sstevel@tonic-gate * unknown at this point, we cannot place the instance 2057c478bd9Sstevel@tonic-gate * number in the instance tree. This will be done at 2067c478bd9Sstevel@tonic-gate * a later time. 2077c478bd9Sstevel@tonic-gate */ 2087c478bd9Sstevel@tonic-gate if (rebuild) 2097c478bd9Sstevel@tonic-gate in_preassign_instance(); 2107c478bd9Sstevel@tonic-gate 2117c478bd9Sstevel@tonic-gate e_ddi_exit_instance(); 2127c478bd9Sstevel@tonic-gate } 2137c478bd9Sstevel@tonic-gate 2147c478bd9Sstevel@tonic-gate static void 2157c478bd9Sstevel@tonic-gate in_preassign_instance() 2167c478bd9Sstevel@tonic-gate { 2177c478bd9Sstevel@tonic-gate major_t m; 2187c478bd9Sstevel@tonic-gate extern major_t devcnt; 2197c478bd9Sstevel@tonic-gate 2207c478bd9Sstevel@tonic-gate for (m = 0; m < devcnt; m++) { 2217c478bd9Sstevel@tonic-gate struct devnames *dnp = &devnamesp[m]; 2227c478bd9Sstevel@tonic-gate dev_info_t *dip = dnp->dn_head; 2237c478bd9Sstevel@tonic-gate while (dip) { 2247c478bd9Sstevel@tonic-gate DEVI(dip)->devi_instance = dnp->dn_instance; 2257c478bd9Sstevel@tonic-gate dnp->dn_instance++; 2267c478bd9Sstevel@tonic-gate dip = ddi_get_next(dip); 2277c478bd9Sstevel@tonic-gate } 2287c478bd9Sstevel@tonic-gate } 2297c478bd9Sstevel@tonic-gate } 2307c478bd9Sstevel@tonic-gate 2317c478bd9Sstevel@tonic-gate /* 2327c478bd9Sstevel@tonic-gate * Checks to see if the /etc/path_to_inst file exists and whether or not 2337c478bd9Sstevel@tonic-gate * it has the magic string in it. 2347c478bd9Sstevel@tonic-gate * 2357c478bd9Sstevel@tonic-gate * Returns one of the following: 2367c478bd9Sstevel@tonic-gate * 2377c478bd9Sstevel@tonic-gate * PTI_FOUND - We have found the /etc/path_to_inst file 2387c478bd9Sstevel@tonic-gate * PTI_REBUILD - We have found the /etc/path_to_inst file and the 2397c478bd9Sstevel@tonic-gate * first line was PTI_MAGIC_STR. 2407c478bd9Sstevel@tonic-gate * PTI_NOT_FOUND - We did not find the /etc/path_to_inst file 2417c478bd9Sstevel@tonic-gate * 2427c478bd9Sstevel@tonic-gate */ 2437c478bd9Sstevel@tonic-gate static int 2447c478bd9Sstevel@tonic-gate in_get_infile(char *filename) 2457c478bd9Sstevel@tonic-gate { 246986fd29aSsetje struct _buf *file; 2477c478bd9Sstevel@tonic-gate int return_val; 2487c478bd9Sstevel@tonic-gate char buf[PTI_MAGIC_STR_LEN]; 2497c478bd9Sstevel@tonic-gate 2507c478bd9Sstevel@tonic-gate /* 2517c478bd9Sstevel@tonic-gate * Try to open the file. 2527c478bd9Sstevel@tonic-gate */ 253986fd29aSsetje if ((file = kobj_open_file(filename)) == (struct _buf *)-1) { 2547c478bd9Sstevel@tonic-gate return (PTI_NOT_FOUND); 2557c478bd9Sstevel@tonic-gate } 2567c478bd9Sstevel@tonic-gate return_val = PTI_FOUND; 2577c478bd9Sstevel@tonic-gate 2587c478bd9Sstevel@tonic-gate /* 2597c478bd9Sstevel@tonic-gate * Read the first PTI_MAGIC_STR_LEN bytes from the file to see if 2607c478bd9Sstevel@tonic-gate * it contains the magic string. If there aren't that many bytes 2617c478bd9Sstevel@tonic-gate * in the file, then assume file is correct and no magic string 2627c478bd9Sstevel@tonic-gate * and move on. 2637c478bd9Sstevel@tonic-gate */ 264986fd29aSsetje switch (kobj_read_file(file, buf, PTI_MAGIC_STR_LEN, 0)) { 2657c478bd9Sstevel@tonic-gate 2667c478bd9Sstevel@tonic-gate case PTI_MAGIC_STR_LEN: 2677c478bd9Sstevel@tonic-gate /* 2687c478bd9Sstevel@tonic-gate * If the first PTI_MAGIC_STR_LEN bytes are the magic string 2697c478bd9Sstevel@tonic-gate * then return PTI_REBUILD. 2707c478bd9Sstevel@tonic-gate */ 2717c478bd9Sstevel@tonic-gate if (strncmp(PTI_MAGIC_STR, buf, PTI_MAGIC_STR_LEN) == 0) 2727c478bd9Sstevel@tonic-gate return_val = PTI_REBUILD; 2737c478bd9Sstevel@tonic-gate break; 2747c478bd9Sstevel@tonic-gate 2757c478bd9Sstevel@tonic-gate case 0: 2767c478bd9Sstevel@tonic-gate /* 2777c478bd9Sstevel@tonic-gate * If the file is zero bytes in length, then consider the 2787c478bd9Sstevel@tonic-gate * file to not be found 2797c478bd9Sstevel@tonic-gate */ 2807c478bd9Sstevel@tonic-gate return_val = PTI_NOT_FOUND; 2817c478bd9Sstevel@tonic-gate 2827c478bd9Sstevel@tonic-gate default: /* Do nothing we have a good file */ 2837c478bd9Sstevel@tonic-gate break; 2847c478bd9Sstevel@tonic-gate } 2857c478bd9Sstevel@tonic-gate 286986fd29aSsetje kobj_close_file(file); 2877c478bd9Sstevel@tonic-gate return (return_val); 2887c478bd9Sstevel@tonic-gate } 2897c478bd9Sstevel@tonic-gate 2907c478bd9Sstevel@tonic-gate int 2917c478bd9Sstevel@tonic-gate is_pseudo_device(dev_info_t *dip) 2927c478bd9Sstevel@tonic-gate { 2937c478bd9Sstevel@tonic-gate dev_info_t *pdip; 2947c478bd9Sstevel@tonic-gate 2957c478bd9Sstevel@tonic-gate for (pdip = ddi_get_parent(dip); pdip && pdip != ddi_root_node(); 2967c478bd9Sstevel@tonic-gate pdip = ddi_get_parent(pdip)) { 2977c478bd9Sstevel@tonic-gate if (strcmp(ddi_get_name(pdip), DEVI_PSEUDO_NEXNAME) == 0) 2987c478bd9Sstevel@tonic-gate return (1); 2997c478bd9Sstevel@tonic-gate } 3007c478bd9Sstevel@tonic-gate return (0); 3017c478bd9Sstevel@tonic-gate } 3027c478bd9Sstevel@tonic-gate 3037c478bd9Sstevel@tonic-gate 3047c478bd9Sstevel@tonic-gate static void 3057c478bd9Sstevel@tonic-gate in_set_instance(dev_info_t *dip, in_drv_t *dp, major_t major) 3067c478bd9Sstevel@tonic-gate { 3077c478bd9Sstevel@tonic-gate /* use preassigned instance if available */ 3087c478bd9Sstevel@tonic-gate if (DEVI(dip)->devi_instance != -1) 3097c478bd9Sstevel@tonic-gate dp->ind_instance = DEVI(dip)->devi_instance; 3107c478bd9Sstevel@tonic-gate else 3117c478bd9Sstevel@tonic-gate dp->ind_instance = in_next_instance(major); 3127c478bd9Sstevel@tonic-gate } 3137c478bd9Sstevel@tonic-gate 3147c478bd9Sstevel@tonic-gate /* 31516bd7258Scth * Return 1 if instance block was assigned for the path. 31616bd7258Scth * 31716bd7258Scth * For multi-port NIC cards, sequential instance assignment across all 318*62a24de0SChris Horne * ports on a card is highly desirable since the ppa is typically the 31916bd7258Scth * same as the instance number, and the ppa is used in the NIC's public 32016bd7258Scth * /dev name. This sequential assignment typically occurs as a result 32116bd7258Scth * of in_preassign_instance() after initial install, or by 32216bd7258Scth * i_ndi_init_hw_children() for NIC ports that share a common parent. 32316bd7258Scth * 32416bd7258Scth * Some NIC cards however use multi-function bridge chips, and to 32516bd7258Scth * support sequential instance assignment accross all ports, without 32616bd7258Scth * disabling multi-threaded attach, we have a (currently) undocumented 32716bd7258Scth * hack to allocate instance numbers in contiguous blocks based on 32816bd7258Scth * driver.conf properties. 32916bd7258Scth * 33016bd7258Scth * ^ 33116bd7258Scth * /---------- ------------\ 33216bd7258Scth * pci@0 pci@0,1 MULTI-FUNCTION BRIDGE CHIP 33316bd7258Scth * / \ / \ 33416bd7258Scth * FJSV,e4ta@4 FJSV,e4ta@4,1 FJSV,e4ta@6 FJSV,e4ta@6,1 NIC PORTS 33516bd7258Scth * n n+2 n+2 n+3 INSTANCE 33616bd7258Scth * 33716bd7258Scth * For the above example, the following driver.conf properties would be 33816bd7258Scth * used to guarantee sequential instance number assignment. 33916bd7258Scth * 34016bd7258Scth * ddi-instance-blocks ="ib-FJSVe4ca", "ib-FJSVe4ta", "ib-generic"; 34116bd7258Scth * ib-FJSVe4ca = "/pci@0/FJSV,e4ca@4", "/pci@0/FJSV,e4ca@4,1", 34216bd7258Scth * "/pci@0,1/FJSV,e4ca@6", "/pci@0,1/FJSV,e4ca@6,1"; 34316bd7258Scth * ib-FJSVe4ta = "/pci@0/FJSV,e4ta@4", "/pci@0/FJSV,e4ta@4,1", 34416bd7258Scth * "/pci@0,1/FJSV,e4ta@6", "/pci@0,1/FJSV,e4ta@6,1"; 34516bd7258Scth * ib-generic = "/pci@0/network@4", "/pci@0/network@4,1", 34616bd7258Scth * "/pci@0,1/network@6", "/pci@0,1/network@6,1"; 34716bd7258Scth * 34816bd7258Scth * The value of the 'ddi-instance-blocks' property references a series 34916bd7258Scth * of card specific properties, like 'ib-FJSV-e4ta', who's value 35016bd7258Scth * defines a single 'instance block'. The 'instance block' describes 35116bd7258Scth * all the paths below a multi-function bridge, where each path is 35216bd7258Scth * called an 'instance path'. The 'instance block' property value is a 35316bd7258Scth * series of 'instance paths'. The number of 'instance paths' in an 35416bd7258Scth * 'instance block' defines the size of the instance block, and the 35516bd7258Scth * ordering of the 'instance paths' defines the instance number 35616bd7258Scth * assignment order for paths going through the 'instance block'. 35716bd7258Scth * 35816bd7258Scth * In the instance assignment code below, if a (path, driver) that 35916bd7258Scth * currently has no instance number has a path that goes through an 36016bd7258Scth * 'instance block', then block instance number allocation occurs. The 36116bd7258Scth * block allocation code will find a sequential set of unused instance 36216bd7258Scth * numbers, and assign instance numbers for all the paths in the 36316bd7258Scth * 'instance block'. Each path is assigned a persistent instance 36416bd7258Scth * number, even paths that don't exist in the device tree or fail 36516bd7258Scth * probe(9E). 36616bd7258Scth */ 36716bd7258Scth static int 36816bd7258Scth in_assign_instance_block(dev_info_t *dip) 36916bd7258Scth { 37016bd7258Scth char **ibn; /* instance block names */ 37116bd7258Scth uint_t nibn; /* number of instance block names */ 37216bd7258Scth uint_t ibni; /* ibn index */ 37316bd7258Scth char *driver; 37416bd7258Scth major_t major; 37516bd7258Scth char *path; 37616bd7258Scth char *addr; 37716bd7258Scth int plen; 37816bd7258Scth char **ibp; /* instance block paths */ 37916bd7258Scth uint_t nibp; /* number of paths in instance block */ 38016bd7258Scth uint_t ibpi; /* ibp index */ 38116bd7258Scth int ibplen; /* length of instance block path */ 38216bd7258Scth char *ipath; 38316bd7258Scth int instance_base; 38416bd7258Scth int splice; 38516bd7258Scth int i; 38616bd7258Scth 38716bd7258Scth /* check for fresh install case (in miniroot) */ 38816bd7258Scth if (DEVI(dip)->devi_instance != -1) 38916bd7258Scth return (0); /* already assigned */ 39016bd7258Scth 39116bd7258Scth /* 39216bd7258Scth * Check to see if we need to allocate a block of contiguous instance 39316bd7258Scth * numbers by looking for the 'ddi-instance-blocks' property. 39416bd7258Scth */ 39516bd7258Scth if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, 39616bd7258Scth "ddi-instance-blocks", &ibn, &nibn) != DDI_SUCCESS) 39716bd7258Scth return (0); /* no instance block needed */ 39816bd7258Scth 39916bd7258Scth /* 40016bd7258Scth * Get information out about node we are processing. 40116bd7258Scth * 40216bd7258Scth * NOTE: Since the node is not yet at DS_INITIALIZED, ddi_pathname() 40316bd7258Scth * will not return the unit-address of the final path component even 40416bd7258Scth * though the node has an established devi_addr unit-address - so we 40516bd7258Scth * need to add the unit-address by hand. 40616bd7258Scth */ 40716bd7258Scth driver = (char *)ddi_driver_name(dip); 40816bd7258Scth major = ddi_driver_major(dip); 40916bd7258Scth path = kmem_alloc(MAXPATHLEN, KM_SLEEP); 41016bd7258Scth (void) ddi_pathname(dip, path); 41116bd7258Scth if ((addr = ddi_get_name_addr(dip)) != NULL) { 41216bd7258Scth (void) strcat(path, "@"); 41316bd7258Scth (void) strcat(path, addr); 41416bd7258Scth } 41516bd7258Scth plen = strlen(path); 41616bd7258Scth 41716bd7258Scth /* loop through instance block names */ 41816bd7258Scth for (ibni = 0; ibni < nibn; ibni++) { 41916bd7258Scth if (ibn[ibni] == NULL) 42016bd7258Scth continue; 42116bd7258Scth 42216bd7258Scth /* lookup instance block */ 42316bd7258Scth if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, dip, 42416bd7258Scth DDI_PROP_DONTPASS, ibn[ibni], 42516bd7258Scth &ibp, &nibp) != DDI_SUCCESS) { 42616bd7258Scth cmn_err(CE_WARN, 42716bd7258Scth "no devinition for instance block '%s' in %s.conf", 42816bd7258Scth ibn[ibni], driver); 42916bd7258Scth continue; 43016bd7258Scth } 43116bd7258Scth 43216bd7258Scth /* Does 'path' go through this instance block? */ 43316bd7258Scth for (ibpi = 0; ibpi < nibp; ibpi++) { 43416bd7258Scth if (ibp[ibpi] == NULL) 43516bd7258Scth continue; 43616bd7258Scth ibplen = strlen(ibp[ibpi]); 43716bd7258Scth if ((ibplen <= plen) && 43816bd7258Scth (strcmp(ibp[ibpi], path + plen - ibplen) == 0)) 43916bd7258Scth break; 44016bd7258Scth 44116bd7258Scth } 44216bd7258Scth if (ibpi >= nibp) { 44316bd7258Scth ddi_prop_free(ibp); 44416bd7258Scth continue; /* no try next instance block */ 44516bd7258Scth } 44616bd7258Scth 44716bd7258Scth /* yes, allocate and assign instances for all paths in block */ 44816bd7258Scth 44916bd7258Scth /* 45016bd7258Scth * determine where we splice in instance paths and verify 45116bd7258Scth * that none of the paths are too long. 45216bd7258Scth */ 45316bd7258Scth splice = plen - ibplen; 45416bd7258Scth for (i = 0; i < nibp; i++) { 45516bd7258Scth if ((splice + strlen(ibp[i])+ 1) >= MAXPATHLEN) { 45616bd7258Scth cmn_err(CE_WARN, 45716bd7258Scth "path %d through instance block '%s' from " 45816bd7258Scth "%s.conf too long", i, ibn[ibni], driver); 45916bd7258Scth break; 46016bd7258Scth } 46116bd7258Scth } 46216bd7258Scth if (i < nibp) { 46316bd7258Scth ddi_prop_free(ibp); 46416bd7258Scth continue; /* too long */ 46516bd7258Scth } 46616bd7258Scth 46716bd7258Scth /* allocate the instance block - no more failures */ 46816bd7258Scth instance_base = in_next_instance_block(major, nibp); 46916bd7258Scth 47016bd7258Scth ipath = kmem_alloc(MAXPATHLEN, KM_SLEEP); 47116bd7258Scth for (ibpi = 0; ibpi < nibp; ibpi++) { 47216bd7258Scth if (ibp[ibpi] == NULL) 47316bd7258Scth continue; 47416bd7258Scth (void) strcpy(ipath, path); 47516bd7258Scth (void) strcpy(ipath + splice, ibp[ibpi]); 47616bd7258Scth (void) in_pathin(ipath, 47716bd7258Scth instance_base + ibpi, driver, NULL); 47816bd7258Scth } 47916bd7258Scth 48016bd7258Scth /* free allocations */ 48116bd7258Scth kmem_free(ipath, MAXPATHLEN); 48216bd7258Scth ddi_prop_free(ibp); 48316bd7258Scth kmem_free(path, MAXPATHLEN); 48416bd7258Scth ddi_prop_free(ibn); 48516bd7258Scth 48616bd7258Scth /* notify devfsadmd to sync of path_to_inst file */ 48716bd7258Scth mutex_enter(&e_ddi_inst_state.ins_serial); 48816bd7258Scth i_log_devfs_instance_mod(); 48916bd7258Scth e_ddi_inst_state.ins_dirty = 1; 49016bd7258Scth mutex_exit(&e_ddi_inst_state.ins_serial); 49116bd7258Scth return (1); 49216bd7258Scth } 49316bd7258Scth 49416bd7258Scth /* our path did not go through any of of the instance blocks */ 49516bd7258Scth kmem_free(path, MAXPATHLEN); 49616bd7258Scth ddi_prop_free(ibn); 49716bd7258Scth return (0); 49816bd7258Scth } 49916bd7258Scth 50016bd7258Scth /* 5017c478bd9Sstevel@tonic-gate * Look up an instance number for a dev_info node, and assign one if it does 5027c478bd9Sstevel@tonic-gate * not have one (the dev_info node has devi_name and devi_addr already set). 5037c478bd9Sstevel@tonic-gate */ 5047c478bd9Sstevel@tonic-gate uint_t 5057c478bd9Sstevel@tonic-gate e_ddi_assign_instance(dev_info_t *dip) 5067c478bd9Sstevel@tonic-gate { 5077c478bd9Sstevel@tonic-gate char *name; 5087c478bd9Sstevel@tonic-gate in_node_t *ap, *np; 5097c478bd9Sstevel@tonic-gate in_drv_t *dp; 5107c478bd9Sstevel@tonic-gate major_t major; 5117c478bd9Sstevel@tonic-gate uint_t ret; 5127c478bd9Sstevel@tonic-gate char *bname; 5137c478bd9Sstevel@tonic-gate 5147c478bd9Sstevel@tonic-gate /* 5157c478bd9Sstevel@tonic-gate * Allow implementation to override 5167c478bd9Sstevel@tonic-gate */ 5177c478bd9Sstevel@tonic-gate if ((ret = impl_assign_instance(dip)) != (uint_t)-1) 5187c478bd9Sstevel@tonic-gate return (ret); 5197c478bd9Sstevel@tonic-gate 5207c478bd9Sstevel@tonic-gate /* 5217c478bd9Sstevel@tonic-gate * If this is a pseudo-device, use the instance number 5227c478bd9Sstevel@tonic-gate * assigned by the pseudo nexus driver. The mutex is 5237c478bd9Sstevel@tonic-gate * not needed since the instance tree is not used. 5247c478bd9Sstevel@tonic-gate */ 5257c478bd9Sstevel@tonic-gate if (is_pseudo_device(dip)) { 5267c478bd9Sstevel@tonic-gate return (ddi_get_instance(dip)); 5277c478bd9Sstevel@tonic-gate } 5287c478bd9Sstevel@tonic-gate 5297c478bd9Sstevel@tonic-gate /* 5307c478bd9Sstevel@tonic-gate * Only one thread is allowed to change the state of the instance 5317c478bd9Sstevel@tonic-gate * number assignments on the system at any given time. 5327c478bd9Sstevel@tonic-gate */ 5337c478bd9Sstevel@tonic-gate e_ddi_enter_instance(); 5347c478bd9Sstevel@tonic-gate 5357c478bd9Sstevel@tonic-gate /* 5367c478bd9Sstevel@tonic-gate * Look for instance node, allocate one if not found 5377c478bd9Sstevel@tonic-gate */ 5387c478bd9Sstevel@tonic-gate np = in_devwalk(dip, &ap, NULL); 5397c478bd9Sstevel@tonic-gate if (np == NULL) { 54016bd7258Scth if (in_assign_instance_block(dip)) { 54116bd7258Scth np = in_devwalk(dip, &ap, NULL); 54216bd7258Scth } else { 5437c478bd9Sstevel@tonic-gate name = ddi_node_name(dip); 5447c478bd9Sstevel@tonic-gate np = in_alloc_node(name, ddi_get_name_addr(dip)); 5457c478bd9Sstevel@tonic-gate ASSERT(np != NULL); 5467c478bd9Sstevel@tonic-gate in_enlist(ap, np); /* insert into tree */ 5477c478bd9Sstevel@tonic-gate } 54816bd7258Scth } 5497c478bd9Sstevel@tonic-gate ASSERT(np == in_devwalk(dip, &ap, NULL)); 5507c478bd9Sstevel@tonic-gate 5517c478bd9Sstevel@tonic-gate /* 5527c478bd9Sstevel@tonic-gate * Look for driver entry, allocate one if not found 5537c478bd9Sstevel@tonic-gate */ 5547c478bd9Sstevel@tonic-gate bname = (char *)ddi_driver_name(dip); 5557c478bd9Sstevel@tonic-gate dp = in_drvwalk(np, bname); 5567c478bd9Sstevel@tonic-gate if (dp == NULL) { 5577c478bd9Sstevel@tonic-gate dp = in_alloc_drv(bname); 5587c478bd9Sstevel@tonic-gate ASSERT(dp != NULL); 5597c478bd9Sstevel@tonic-gate major = ddi_driver_major(dip); 560a204de77Scth ASSERT(major != DDI_MAJOR_T_NONE); 5617c478bd9Sstevel@tonic-gate in_endrv(np, dp); 5627c478bd9Sstevel@tonic-gate in_set_instance(dip, dp, major); 5637c478bd9Sstevel@tonic-gate dp->ind_state = IN_PROVISIONAL; 5647c478bd9Sstevel@tonic-gate in_hashdrv(dp); 5657c478bd9Sstevel@tonic-gate } 5667c478bd9Sstevel@tonic-gate 5677c478bd9Sstevel@tonic-gate ret = dp->ind_instance; 5687c478bd9Sstevel@tonic-gate 5697c478bd9Sstevel@tonic-gate e_ddi_exit_instance(); 5707c478bd9Sstevel@tonic-gate return (ret); 5717c478bd9Sstevel@tonic-gate } 5727c478bd9Sstevel@tonic-gate 5737c478bd9Sstevel@tonic-gate static int 5747c478bd9Sstevel@tonic-gate mkpathname(char *path, in_node_t *np, int len) 5757c478bd9Sstevel@tonic-gate { 5767c478bd9Sstevel@tonic-gate int len_needed; 5777c478bd9Sstevel@tonic-gate 5787c478bd9Sstevel@tonic-gate if (np == e_ddi_inst_state.ins_root) 5797c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 5807c478bd9Sstevel@tonic-gate 5817c478bd9Sstevel@tonic-gate if (mkpathname(path, np->in_parent, len) == DDI_FAILURE) 5827c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 5837c478bd9Sstevel@tonic-gate 5847c478bd9Sstevel@tonic-gate len_needed = strlen(path); 5857c478bd9Sstevel@tonic-gate len_needed += strlen(np->in_node_name) + 1; /* for '/' */ 5867c478bd9Sstevel@tonic-gate if (np->in_unit_addr) { 5877c478bd9Sstevel@tonic-gate len_needed += strlen(np->in_unit_addr) + 1; /* for '@' */ 5887c478bd9Sstevel@tonic-gate } 5897c478bd9Sstevel@tonic-gate len_needed += 1; /* for '\0' */ 5907c478bd9Sstevel@tonic-gate 5917c478bd9Sstevel@tonic-gate /* 5927c478bd9Sstevel@tonic-gate * XX complain 5937c478bd9Sstevel@tonic-gate */ 5947c478bd9Sstevel@tonic-gate if (len_needed > len) 5957c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 5967c478bd9Sstevel@tonic-gate 5977c478bd9Sstevel@tonic-gate if (np->in_unit_addr[0] == '\0') 5987c478bd9Sstevel@tonic-gate (void) sprintf(path+strlen(path), "/%s", np->in_node_name); 5997c478bd9Sstevel@tonic-gate else 6007c478bd9Sstevel@tonic-gate (void) sprintf(path+strlen(path), "/%s@%s", np->in_node_name, 6017c478bd9Sstevel@tonic-gate np->in_unit_addr); 6027c478bd9Sstevel@tonic-gate 6037c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 6047c478bd9Sstevel@tonic-gate } 6057c478bd9Sstevel@tonic-gate 6067c478bd9Sstevel@tonic-gate /* 6077c478bd9Sstevel@tonic-gate * produce the path to the given instance of a major number. 6087c478bd9Sstevel@tonic-gate * path must hold MAXPATHLEN string 6097c478bd9Sstevel@tonic-gate */ 6107c478bd9Sstevel@tonic-gate int 6117c478bd9Sstevel@tonic-gate e_ddi_instance_majorinstance_to_path(major_t major, uint_t inst, char *path) 6127c478bd9Sstevel@tonic-gate { 6137c478bd9Sstevel@tonic-gate struct devnames *dnp; 6147c478bd9Sstevel@tonic-gate in_drv_t *dp; 6157c478bd9Sstevel@tonic-gate int ret; 6167c478bd9Sstevel@tonic-gate 6177c478bd9Sstevel@tonic-gate e_ddi_enter_instance(); 6187c478bd9Sstevel@tonic-gate 6197c478bd9Sstevel@tonic-gate /* look for the instance threaded off major */ 6207c478bd9Sstevel@tonic-gate dnp = &devnamesp[major]; 6217c478bd9Sstevel@tonic-gate for (dp = dnp->dn_inlist; dp != NULL; dp = dp->ind_next) 6227c478bd9Sstevel@tonic-gate if (dp->ind_instance == inst) 6237c478bd9Sstevel@tonic-gate break; 6247c478bd9Sstevel@tonic-gate 6257c478bd9Sstevel@tonic-gate /* produce path from the node that uses the instance */ 6267c478bd9Sstevel@tonic-gate if (dp) { 6277c478bd9Sstevel@tonic-gate *path = 0; 6287c478bd9Sstevel@tonic-gate ret = mkpathname(path, dp->ind_node, MAXPATHLEN); 6297c478bd9Sstevel@tonic-gate } else 6307c478bd9Sstevel@tonic-gate ret = DDI_FAILURE; 6317c478bd9Sstevel@tonic-gate 6327c478bd9Sstevel@tonic-gate e_ddi_exit_instance(); 6337c478bd9Sstevel@tonic-gate return (ret); 6347c478bd9Sstevel@tonic-gate } 6357c478bd9Sstevel@tonic-gate 6367c478bd9Sstevel@tonic-gate /* 63716bd7258Scth * Allocate a sequential block of instance numbers for the specified driver, 63816bd7258Scth * and return the base instance number of the block. The implementation 63916bd7258Scth * depends on the list being sorted in ascending instance number sequence. 64016bd7258Scth * When there are no 'holes' in the allocation sequence, dn_instance is the 64116bd7258Scth * next available instance number. When dn_instance is IN_SEARCHME, hole(s) 64216bd7258Scth * exists and a slower code path executes which tries to fill holes. 6437c478bd9Sstevel@tonic-gate */ 6447c478bd9Sstevel@tonic-gate static int 64516bd7258Scth in_next_instance_block(major_t major, int block_size) 6467c478bd9Sstevel@tonic-gate { 6477c478bd9Sstevel@tonic-gate unsigned int prev; 6487c478bd9Sstevel@tonic-gate struct devnames *dnp; 6497c478bd9Sstevel@tonic-gate in_drv_t *dp; 65016bd7258Scth int base; 65116bd7258Scth int hole; 6527c478bd9Sstevel@tonic-gate 6537c478bd9Sstevel@tonic-gate dnp = &devnamesp[major]; 654a204de77Scth ASSERT(major != DDI_MAJOR_T_NONE); 6557c478bd9Sstevel@tonic-gate ASSERT(e_ddi_inst_state.ins_busy); 65616bd7258Scth ASSERT(block_size); 65716bd7258Scth 65816bd7258Scth /* check to see if we can do a quick allocation */ 65916bd7258Scth if (dnp->dn_instance != IN_SEARCHME) { 66016bd7258Scth base = dnp->dn_instance; 66116bd7258Scth dnp->dn_instance += block_size; 66216bd7258Scth return (base); 66316bd7258Scth } 6647c478bd9Sstevel@tonic-gate dp = dnp->dn_inlist; 6657c478bd9Sstevel@tonic-gate 66616bd7258Scth /* no existing entries, allocate block at 0 */ 6677c478bd9Sstevel@tonic-gate if (dp == NULL) { 66816bd7258Scth dnp->dn_instance = block_size; 6697c478bd9Sstevel@tonic-gate return (0); 6707c478bd9Sstevel@tonic-gate } 6717c478bd9Sstevel@tonic-gate 6727c478bd9Sstevel@tonic-gate prev = dp->ind_instance; 67316bd7258Scth if (prev >= block_size) 67416bd7258Scth return (0); /* we fit in hole at beginning */ 6757c478bd9Sstevel@tonic-gate 67616bd7258Scth /* search the list for a large enough hole */ 67716bd7258Scth for (dp = dp->ind_next, hole = 0; dp; dp = dp->ind_next) { 67816bd7258Scth if (dp->ind_instance != (prev + 1)) 67916bd7258Scth hole++; /* we have a hole */ 68016bd7258Scth if (dp->ind_instance >= (prev + block_size + 1)) 68116bd7258Scth break; /* we fit in hole */ 68216bd7258Scth prev = dp->ind_instance; 68316bd7258Scth } 68416bd7258Scth 68516bd7258Scth /* 68616bd7258Scth * If hole is zero then all holes are patched and we can resume 68716bd7258Scth * quick allocations. 68816bd7258Scth */ 68916bd7258Scth if (hole == 0) 69016bd7258Scth dnp->dn_instance = prev + 1 + block_size; 69116bd7258Scth 69216bd7258Scth return (prev + 1); 69316bd7258Scth } 69416bd7258Scth 69516bd7258Scth /* assign instance block of size 1 */ 69616bd7258Scth static int 69716bd7258Scth in_next_instance(major_t major) 69816bd7258Scth { 69916bd7258Scth return (in_next_instance_block(major, 1)); 7007c478bd9Sstevel@tonic-gate } 7017c478bd9Sstevel@tonic-gate 7027c478bd9Sstevel@tonic-gate /* 7037c478bd9Sstevel@tonic-gate * This call causes us to *forget* the instance number we've generated 7047c478bd9Sstevel@tonic-gate * for a given device if it was not permanent. 7057c478bd9Sstevel@tonic-gate */ 7067c478bd9Sstevel@tonic-gate void 7077c478bd9Sstevel@tonic-gate e_ddi_free_instance(dev_info_t *dip, char *addr) 7087c478bd9Sstevel@tonic-gate { 7097c478bd9Sstevel@tonic-gate char *name; 7107c478bd9Sstevel@tonic-gate in_node_t *np; 7117c478bd9Sstevel@tonic-gate in_node_t *ap; /* ancestor node */ 7127c478bd9Sstevel@tonic-gate major_t major; 7137c478bd9Sstevel@tonic-gate struct devnames *dnp; 7147c478bd9Sstevel@tonic-gate in_drv_t *dp; /* in_drv entry */ 7157c478bd9Sstevel@tonic-gate 7167c478bd9Sstevel@tonic-gate /* 7177c478bd9Sstevel@tonic-gate * Allow implementation override 7187c478bd9Sstevel@tonic-gate */ 7197c478bd9Sstevel@tonic-gate if (impl_free_instance(dip) == DDI_SUCCESS) 7207c478bd9Sstevel@tonic-gate return; 7217c478bd9Sstevel@tonic-gate 7227c478bd9Sstevel@tonic-gate /* 7237c478bd9Sstevel@tonic-gate * If this is a pseudo-device, no instance number 7247c478bd9Sstevel@tonic-gate * was assigned. 7257c478bd9Sstevel@tonic-gate */ 7267c478bd9Sstevel@tonic-gate if (is_pseudo_device(dip)) { 7277c478bd9Sstevel@tonic-gate return; 7287c478bd9Sstevel@tonic-gate } 7297c478bd9Sstevel@tonic-gate 7307c478bd9Sstevel@tonic-gate name = (char *)ddi_driver_name(dip); 7317c478bd9Sstevel@tonic-gate major = ddi_driver_major(dip); 732a204de77Scth ASSERT(major != DDI_MAJOR_T_NONE); 7337c478bd9Sstevel@tonic-gate dnp = &devnamesp[major]; 7347c478bd9Sstevel@tonic-gate /* 7357c478bd9Sstevel@tonic-gate * Only one thread is allowed to change the state of the instance 7367c478bd9Sstevel@tonic-gate * number assignments on the system at any given time. 7377c478bd9Sstevel@tonic-gate */ 7387c478bd9Sstevel@tonic-gate e_ddi_enter_instance(); 7397c478bd9Sstevel@tonic-gate np = in_devwalk(dip, &ap, addr); 7407c478bd9Sstevel@tonic-gate ASSERT(np); 7417c478bd9Sstevel@tonic-gate dp = in_drvwalk(np, name); 7427c478bd9Sstevel@tonic-gate ASSERT(dp); 7437c478bd9Sstevel@tonic-gate if (dp->ind_state == IN_PROVISIONAL) { 7447c478bd9Sstevel@tonic-gate in_removedrv(dnp, dp); 7457c478bd9Sstevel@tonic-gate } 7467c478bd9Sstevel@tonic-gate if (np->in_drivers == NULL) { 7477c478bd9Sstevel@tonic-gate in_removenode(dnp, np, ap); 7487c478bd9Sstevel@tonic-gate } 7497c478bd9Sstevel@tonic-gate e_ddi_exit_instance(); 7507c478bd9Sstevel@tonic-gate } 7517c478bd9Sstevel@tonic-gate 7527c478bd9Sstevel@tonic-gate /* 7537c478bd9Sstevel@tonic-gate * This makes our memory of an instance assignment permanent 7547c478bd9Sstevel@tonic-gate */ 7557c478bd9Sstevel@tonic-gate void 7567c478bd9Sstevel@tonic-gate e_ddi_keep_instance(dev_info_t *dip) 7577c478bd9Sstevel@tonic-gate { 7587c478bd9Sstevel@tonic-gate in_node_t *np, *ap; 7597c478bd9Sstevel@tonic-gate in_drv_t *dp; 7607c478bd9Sstevel@tonic-gate 761*62a24de0SChris Horne /* Don't make nulldriver instance assignments permanent */ 762*62a24de0SChris Horne if (ddi_driver_major(dip) == nulldriver_major) 763*62a24de0SChris Horne return; 764*62a24de0SChris Horne 7657c478bd9Sstevel@tonic-gate /* 7667c478bd9Sstevel@tonic-gate * Allow implementation override 7677c478bd9Sstevel@tonic-gate */ 7687c478bd9Sstevel@tonic-gate if (impl_keep_instance(dip) == DDI_SUCCESS) 7697c478bd9Sstevel@tonic-gate return; 7707c478bd9Sstevel@tonic-gate 7717c478bd9Sstevel@tonic-gate /* 7727c478bd9Sstevel@tonic-gate * Nothing to do for pseudo devices. 7737c478bd9Sstevel@tonic-gate */ 7747c478bd9Sstevel@tonic-gate if (is_pseudo_device(dip)) 7757c478bd9Sstevel@tonic-gate return; 7767c478bd9Sstevel@tonic-gate 7777c478bd9Sstevel@tonic-gate /* 7787c478bd9Sstevel@tonic-gate * Only one thread is allowed to change the state of the instance 7797c478bd9Sstevel@tonic-gate * number assignments on the system at any given time. 7807c478bd9Sstevel@tonic-gate */ 7817c478bd9Sstevel@tonic-gate e_ddi_enter_instance(); 7827c478bd9Sstevel@tonic-gate np = in_devwalk(dip, &ap, NULL); 7837c478bd9Sstevel@tonic-gate ASSERT(np); 7847c478bd9Sstevel@tonic-gate dp = in_drvwalk(np, (char *)ddi_driver_name(dip)); 7857c478bd9Sstevel@tonic-gate ASSERT(dp); 7867c478bd9Sstevel@tonic-gate 7877c478bd9Sstevel@tonic-gate mutex_enter(&e_ddi_inst_state.ins_serial); 7887c478bd9Sstevel@tonic-gate if (dp->ind_state == IN_PROVISIONAL) { 7897c478bd9Sstevel@tonic-gate dp->ind_state = IN_PERMANENT; 7907c478bd9Sstevel@tonic-gate i_log_devfs_instance_mod(); 7917c478bd9Sstevel@tonic-gate e_ddi_inst_state.ins_dirty = 1; 7927c478bd9Sstevel@tonic-gate } 7937c478bd9Sstevel@tonic-gate mutex_exit(&e_ddi_inst_state.ins_serial); 7947c478bd9Sstevel@tonic-gate e_ddi_exit_instance(); 7957c478bd9Sstevel@tonic-gate } 7967c478bd9Sstevel@tonic-gate 7977c478bd9Sstevel@tonic-gate /* 7987c478bd9Sstevel@tonic-gate * A new major has been added to the system. Run through the orphan list 7997c478bd9Sstevel@tonic-gate * and try to attach each one to a driver's list. 8007c478bd9Sstevel@tonic-gate */ 8017c478bd9Sstevel@tonic-gate void 8027c478bd9Sstevel@tonic-gate e_ddi_unorphan_instance_nos() 8037c478bd9Sstevel@tonic-gate { 8047c478bd9Sstevel@tonic-gate in_drv_t *dp, *ndp; 8057c478bd9Sstevel@tonic-gate 8067c478bd9Sstevel@tonic-gate /* 8077c478bd9Sstevel@tonic-gate * disconnect the orphan list, and call in_hashdrv for each item 8087c478bd9Sstevel@tonic-gate * on it 8097c478bd9Sstevel@tonic-gate */ 8107c478bd9Sstevel@tonic-gate 8117c478bd9Sstevel@tonic-gate /* 8127c478bd9Sstevel@tonic-gate * Only one thread is allowed to change the state of the instance 8137c478bd9Sstevel@tonic-gate * number assignments on the system at any given time. 8147c478bd9Sstevel@tonic-gate */ 8157c478bd9Sstevel@tonic-gate e_ddi_enter_instance(); 8167c478bd9Sstevel@tonic-gate if (e_ddi_inst_state.ins_no_major == NULL) { 8177c478bd9Sstevel@tonic-gate e_ddi_exit_instance(); 8187c478bd9Sstevel@tonic-gate return; 8197c478bd9Sstevel@tonic-gate } 8207c478bd9Sstevel@tonic-gate /* 8217c478bd9Sstevel@tonic-gate * Hash instance list to devnames structure of major. 8227c478bd9Sstevel@tonic-gate * Note that if there is not a valid major number for the 8237c478bd9Sstevel@tonic-gate * node, in_hashdrv will put it back on the no_major list. 8247c478bd9Sstevel@tonic-gate */ 8257c478bd9Sstevel@tonic-gate dp = e_ddi_inst_state.ins_no_major; 8267c478bd9Sstevel@tonic-gate e_ddi_inst_state.ins_no_major = NULL; 8277c478bd9Sstevel@tonic-gate while (dp) { 8287c478bd9Sstevel@tonic-gate ndp = dp->ind_next; 8297c478bd9Sstevel@tonic-gate ASSERT(dp->ind_state != IN_UNKNOWN); 8307c478bd9Sstevel@tonic-gate dp->ind_next = NULL; 8317c478bd9Sstevel@tonic-gate in_hashdrv(dp); 8327c478bd9Sstevel@tonic-gate dp = ndp; 8337c478bd9Sstevel@tonic-gate } 8347c478bd9Sstevel@tonic-gate e_ddi_exit_instance(); 8357c478bd9Sstevel@tonic-gate } 8367c478bd9Sstevel@tonic-gate 8377c478bd9Sstevel@tonic-gate static void 8387c478bd9Sstevel@tonic-gate in_removenode(struct devnames *dnp, in_node_t *mp, in_node_t *ap) 8397c478bd9Sstevel@tonic-gate { 8407c478bd9Sstevel@tonic-gate in_node_t *np; 8417c478bd9Sstevel@tonic-gate 8427c478bd9Sstevel@tonic-gate ASSERT(e_ddi_inst_state.ins_busy); 8437c478bd9Sstevel@tonic-gate /* 8447c478bd9Sstevel@tonic-gate * Assertion: parents are always instantiated by the framework 8457c478bd9Sstevel@tonic-gate * before their children, destroyed after them 8467c478bd9Sstevel@tonic-gate */ 8477c478bd9Sstevel@tonic-gate ASSERT(mp->in_child == NULL); 8487c478bd9Sstevel@tonic-gate /* 8497c478bd9Sstevel@tonic-gate * Assertion: drv entries are always removed before their owning nodes 8507c478bd9Sstevel@tonic-gate */ 8517c478bd9Sstevel@tonic-gate ASSERT(mp->in_drivers == NULL); 8527c478bd9Sstevel@tonic-gate /* 8537c478bd9Sstevel@tonic-gate * Take the node out of the tree 8547c478bd9Sstevel@tonic-gate */ 8557c478bd9Sstevel@tonic-gate if (ap->in_child == mp) { 8567c478bd9Sstevel@tonic-gate ap->in_child = mp->in_sibling; 8577c478bd9Sstevel@tonic-gate in_dealloc_node(mp); 8587c478bd9Sstevel@tonic-gate return; 8597c478bd9Sstevel@tonic-gate } else { 8607c478bd9Sstevel@tonic-gate for (np = ap->in_child; np; np = np->in_sibling) { 8617c478bd9Sstevel@tonic-gate if (np->in_sibling == mp) { 8627c478bd9Sstevel@tonic-gate np->in_sibling = mp->in_sibling; 8637c478bd9Sstevel@tonic-gate in_dealloc_node(mp); 8647c478bd9Sstevel@tonic-gate return; 8657c478bd9Sstevel@tonic-gate } 8667c478bd9Sstevel@tonic-gate } 8677c478bd9Sstevel@tonic-gate } 8687c478bd9Sstevel@tonic-gate panic("in_removenode dnp %p mp %p", (void *)dnp, (void *)mp); 8697c478bd9Sstevel@tonic-gate } 8707c478bd9Sstevel@tonic-gate 8717c478bd9Sstevel@tonic-gate /* 8727c478bd9Sstevel@tonic-gate * Recursive ascent 8737c478bd9Sstevel@tonic-gate * 8747c478bd9Sstevel@tonic-gate * This now only does half the job. It finds the node, then the caller 8757c478bd9Sstevel@tonic-gate * has to search the node for the binding name 8767c478bd9Sstevel@tonic-gate */ 8777c478bd9Sstevel@tonic-gate static in_node_t * 8787c478bd9Sstevel@tonic-gate in_devwalk(dev_info_t *dip, in_node_t **ap, char *addr) 8797c478bd9Sstevel@tonic-gate { 8807c478bd9Sstevel@tonic-gate in_node_t *np; 8817c478bd9Sstevel@tonic-gate char *name; 8827c478bd9Sstevel@tonic-gate 8837c478bd9Sstevel@tonic-gate ASSERT(dip); 8847c478bd9Sstevel@tonic-gate ASSERT(e_ddi_inst_state.ins_busy); 8857c478bd9Sstevel@tonic-gate if (dip == ddi_root_node()) { 8867c478bd9Sstevel@tonic-gate *ap = NULL; 8877c478bd9Sstevel@tonic-gate return (e_ddi_inst_state.ins_root); 8887c478bd9Sstevel@tonic-gate } 8897c478bd9Sstevel@tonic-gate /* 8907c478bd9Sstevel@tonic-gate * call up to find parent, then look through the list of kids 8917c478bd9Sstevel@tonic-gate * for a match 8927c478bd9Sstevel@tonic-gate */ 8937c478bd9Sstevel@tonic-gate np = in_devwalk(ddi_get_parent(dip), ap, NULL); 8947c478bd9Sstevel@tonic-gate if (np == NULL) 8957c478bd9Sstevel@tonic-gate return (np); 8967c478bd9Sstevel@tonic-gate *ap = np; 8977c478bd9Sstevel@tonic-gate np = np->in_child; 8987c478bd9Sstevel@tonic-gate name = ddi_node_name(dip); 8997c478bd9Sstevel@tonic-gate if (addr == NULL) 9007c478bd9Sstevel@tonic-gate addr = ddi_get_name_addr(dip); 9017c478bd9Sstevel@tonic-gate 9027c478bd9Sstevel@tonic-gate while (np) { 9037c478bd9Sstevel@tonic-gate if (in_eqstr(np->in_node_name, name) && 9047c478bd9Sstevel@tonic-gate in_eqstr(np->in_unit_addr, addr)) { 9057c478bd9Sstevel@tonic-gate return (np); 9067c478bd9Sstevel@tonic-gate } 9077c478bd9Sstevel@tonic-gate np = np->in_sibling; 9087c478bd9Sstevel@tonic-gate } 9097c478bd9Sstevel@tonic-gate return (np); 9107c478bd9Sstevel@tonic-gate } 9117c478bd9Sstevel@tonic-gate 9127c478bd9Sstevel@tonic-gate /* 9137c478bd9Sstevel@tonic-gate * Create a node specified by cp and assign it the given instance no. 9147c478bd9Sstevel@tonic-gate */ 9157c478bd9Sstevel@tonic-gate static int 9167c478bd9Sstevel@tonic-gate in_pathin(char *cp, int instance, char *bname, struct bind **args) 9177c478bd9Sstevel@tonic-gate { 9187c478bd9Sstevel@tonic-gate in_node_t *np; 9197c478bd9Sstevel@tonic-gate in_drv_t *dp; 9207c478bd9Sstevel@tonic-gate char *name; 9217c478bd9Sstevel@tonic-gate 9227c478bd9Sstevel@tonic-gate ASSERT(e_ddi_inst_state.ins_busy); 9237c478bd9Sstevel@tonic-gate ASSERT(args == NULL); 9247c478bd9Sstevel@tonic-gate 9257c478bd9Sstevel@tonic-gate /* 9267c478bd9Sstevel@tonic-gate * Give a warning to the console. 9277c478bd9Sstevel@tonic-gate * return value ignored 9287c478bd9Sstevel@tonic-gate */ 9297c478bd9Sstevel@tonic-gate if (cp[0] != '/' || instance == -1 || bname == NULL) { 9307c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, 9317c478bd9Sstevel@tonic-gate "invalid instance file entry %s %d", 9327c478bd9Sstevel@tonic-gate cp, instance); 9337c478bd9Sstevel@tonic-gate 9347c478bd9Sstevel@tonic-gate return (0); 9357c478bd9Sstevel@tonic-gate } 9367c478bd9Sstevel@tonic-gate 9377c478bd9Sstevel@tonic-gate if ((name = i_binding_to_drv_name(bname)) != NULL) 9387c478bd9Sstevel@tonic-gate bname = name; 9397c478bd9Sstevel@tonic-gate 9407c478bd9Sstevel@tonic-gate np = in_make_path(cp); 9417c478bd9Sstevel@tonic-gate ASSERT(np); 9427c478bd9Sstevel@tonic-gate if (in_inuse(instance, bname)) { 9437c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, 9447c478bd9Sstevel@tonic-gate "instance already in use: %s %d", cp, instance); 9457c478bd9Sstevel@tonic-gate return (0); 9467c478bd9Sstevel@tonic-gate } 9477c478bd9Sstevel@tonic-gate dp = in_drvwalk(np, bname); 9487c478bd9Sstevel@tonic-gate if (dp != NULL) { 9497c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, 9507c478bd9Sstevel@tonic-gate "multiple instance number assignments for " 9517c478bd9Sstevel@tonic-gate "'%s' (driver %s), %d used", 9527c478bd9Sstevel@tonic-gate cp, bname, dp->ind_instance); 9537c478bd9Sstevel@tonic-gate return (0); 9547c478bd9Sstevel@tonic-gate } 9557c478bd9Sstevel@tonic-gate dp = in_alloc_drv(bname); 9567c478bd9Sstevel@tonic-gate in_endrv(np, dp); 9577c478bd9Sstevel@tonic-gate dp->ind_instance = instance; 9587c478bd9Sstevel@tonic-gate dp->ind_state = IN_PERMANENT; 9597c478bd9Sstevel@tonic-gate in_hashdrv(dp); 9607c478bd9Sstevel@tonic-gate 9617c478bd9Sstevel@tonic-gate return (0); 9627c478bd9Sstevel@tonic-gate } 9637c478bd9Sstevel@tonic-gate 9647c478bd9Sstevel@tonic-gate /* 9657c478bd9Sstevel@tonic-gate * Create (or find) the node named by path by recursively descending from the 9667c478bd9Sstevel@tonic-gate * root's first child (we ignore the root, which is never named) 9677c478bd9Sstevel@tonic-gate */ 9687c478bd9Sstevel@tonic-gate static in_node_t * 9697c478bd9Sstevel@tonic-gate in_make_path(char *path) 9707c478bd9Sstevel@tonic-gate { 9717c478bd9Sstevel@tonic-gate in_node_t *ap; /* ancestor pointer */ 9727c478bd9Sstevel@tonic-gate in_node_t *np; /* working node pointer */ 9737c478bd9Sstevel@tonic-gate in_node_t *rp; /* return node pointer */ 9747c478bd9Sstevel@tonic-gate char buf[MAXPATHLEN]; /* copy of string so we can change it */ 9757c478bd9Sstevel@tonic-gate char *cp, *name, *addr; 9767c478bd9Sstevel@tonic-gate 9777c478bd9Sstevel@tonic-gate ASSERT(e_ddi_inst_state.ins_busy); 9787c478bd9Sstevel@tonic-gate if (path == NULL || path[0] != '/') 9797c478bd9Sstevel@tonic-gate return (NULL); 9807c478bd9Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), "%s", path); 9817c478bd9Sstevel@tonic-gate cp = buf + 1; /* skip over initial '/' in path */ 9827c478bd9Sstevel@tonic-gate name = in_name_addr(&cp, &addr); 9837c478bd9Sstevel@tonic-gate 9847c478bd9Sstevel@tonic-gate /* 9857c478bd9Sstevel@tonic-gate * In S9 and earlier releases, the path_to_inst file 9867c478bd9Sstevel@tonic-gate * SunCluster was prepended with "/node@#". This was 9877c478bd9Sstevel@tonic-gate * removed in S10. We skip the prefix if the prefix 9887c478bd9Sstevel@tonic-gate * still exists in /etc/path_to_inst. It is needed for 9897c478bd9Sstevel@tonic-gate * various forms of Solaris upgrade to work properly 9907c478bd9Sstevel@tonic-gate * in the SunCluster environment. 9917c478bd9Sstevel@tonic-gate */ 9927c478bd9Sstevel@tonic-gate if ((cluster_bootflags & CLUSTER_CONFIGURED) && 9937c478bd9Sstevel@tonic-gate (strcmp(name, "node") == 0)) 9947c478bd9Sstevel@tonic-gate name = in_name_addr(&cp, &addr); 9957c478bd9Sstevel@tonic-gate 9967c478bd9Sstevel@tonic-gate ap = e_ddi_inst_state.ins_root; 9977c478bd9Sstevel@tonic-gate rp = np = e_ddi_inst_state.ins_root->in_child; 9987c478bd9Sstevel@tonic-gate while (name) { 9997c478bd9Sstevel@tonic-gate while (name && np) { 10007c478bd9Sstevel@tonic-gate if (in_eqstr(name, np->in_node_name) && 10017c478bd9Sstevel@tonic-gate in_eqstr(addr, np->in_unit_addr)) { 10027c478bd9Sstevel@tonic-gate name = in_name_addr(&cp, &addr); 10037c478bd9Sstevel@tonic-gate if (name == NULL) 10047c478bd9Sstevel@tonic-gate return (np); 10057c478bd9Sstevel@tonic-gate ap = np; 10067c478bd9Sstevel@tonic-gate np = np->in_child; 10077c478bd9Sstevel@tonic-gate continue; 10087c478bd9Sstevel@tonic-gate } else { 10097c478bd9Sstevel@tonic-gate np = np->in_sibling; 10107c478bd9Sstevel@tonic-gate } 10117c478bd9Sstevel@tonic-gate } 10127c478bd9Sstevel@tonic-gate np = in_alloc_node(name, addr); 10137c478bd9Sstevel@tonic-gate in_enlist(ap, np); /* insert into tree */ 10147c478bd9Sstevel@tonic-gate rp = np; /* value to return if we quit */ 10157c478bd9Sstevel@tonic-gate ap = np; /* new parent */ 10167c478bd9Sstevel@tonic-gate np = NULL; /* can have no children */ 10177c478bd9Sstevel@tonic-gate name = in_name_addr(&cp, &addr); 10187c478bd9Sstevel@tonic-gate } 10197c478bd9Sstevel@tonic-gate return (rp); 10207c478bd9Sstevel@tonic-gate } 10217c478bd9Sstevel@tonic-gate 10227c478bd9Sstevel@tonic-gate /* 10237c478bd9Sstevel@tonic-gate * Insert node np into the tree as one of ap's children. 10247c478bd9Sstevel@tonic-gate */ 10257c478bd9Sstevel@tonic-gate static void 10267c478bd9Sstevel@tonic-gate in_enlist(in_node_t *ap, in_node_t *np) 10277c478bd9Sstevel@tonic-gate { 10287c478bd9Sstevel@tonic-gate in_node_t *mp; 10297c478bd9Sstevel@tonic-gate ASSERT(e_ddi_inst_state.ins_busy); 10307c478bd9Sstevel@tonic-gate /* 10317c478bd9Sstevel@tonic-gate * Make this node some other node's child or child's sibling 10327c478bd9Sstevel@tonic-gate */ 10337c478bd9Sstevel@tonic-gate ASSERT(ap && np); 10347c478bd9Sstevel@tonic-gate if (ap->in_child == NULL) { 10357c478bd9Sstevel@tonic-gate ap->in_child = np; 10367c478bd9Sstevel@tonic-gate } else { 10377c478bd9Sstevel@tonic-gate for (mp = ap->in_child; mp; mp = mp->in_sibling) 10387c478bd9Sstevel@tonic-gate if (mp->in_sibling == NULL) { 10397c478bd9Sstevel@tonic-gate mp->in_sibling = np; 10407c478bd9Sstevel@tonic-gate break; 10417c478bd9Sstevel@tonic-gate } 10427c478bd9Sstevel@tonic-gate } 10437c478bd9Sstevel@tonic-gate np->in_parent = ap; 10447c478bd9Sstevel@tonic-gate } 10457c478bd9Sstevel@tonic-gate 10467c478bd9Sstevel@tonic-gate /* 10477c478bd9Sstevel@tonic-gate * Insert drv entry dp onto a node's driver list 10487c478bd9Sstevel@tonic-gate */ 10497c478bd9Sstevel@tonic-gate static void 10507c478bd9Sstevel@tonic-gate in_endrv(in_node_t *np, in_drv_t *dp) 10517c478bd9Sstevel@tonic-gate { 10527c478bd9Sstevel@tonic-gate in_drv_t *mp; 10537c478bd9Sstevel@tonic-gate ASSERT(e_ddi_inst_state.ins_busy); 10547c478bd9Sstevel@tonic-gate ASSERT(np && dp); 10557c478bd9Sstevel@tonic-gate mp = np->in_drivers; 10567c478bd9Sstevel@tonic-gate np->in_drivers = dp; 10577c478bd9Sstevel@tonic-gate dp->ind_next_drv = mp; 10587c478bd9Sstevel@tonic-gate dp->ind_node = np; 10597c478bd9Sstevel@tonic-gate } 10607c478bd9Sstevel@tonic-gate 10617c478bd9Sstevel@tonic-gate /* 10627c478bd9Sstevel@tonic-gate * Parse the next name out of the path, null terminate it and update cp. 10637c478bd9Sstevel@tonic-gate * caller has copied string so we can mess with it. 10647c478bd9Sstevel@tonic-gate * Upon return *cpp points to the next section to be parsed, *addrp points 10657c478bd9Sstevel@tonic-gate * to the current address substring (or NULL if none) and we return the 10667c478bd9Sstevel@tonic-gate * current name substring (or NULL if none). name and address substrings 10677c478bd9Sstevel@tonic-gate * are null terminated in place. 10687c478bd9Sstevel@tonic-gate */ 10697c478bd9Sstevel@tonic-gate 10707c478bd9Sstevel@tonic-gate static char * 10717c478bd9Sstevel@tonic-gate in_name_addr(char **cpp, char **addrp) 10727c478bd9Sstevel@tonic-gate { 10737c478bd9Sstevel@tonic-gate char *namep; /* return value holder */ 10747c478bd9Sstevel@tonic-gate char *ap; /* pointer to '@' in string */ 10757c478bd9Sstevel@tonic-gate char *sp; /* pointer to '/' in string */ 10767c478bd9Sstevel@tonic-gate 10777c478bd9Sstevel@tonic-gate if (*cpp == NULL || **cpp == '\0') { 10787c478bd9Sstevel@tonic-gate *addrp = NULL; 10797c478bd9Sstevel@tonic-gate return (NULL); 10807c478bd9Sstevel@tonic-gate } 10817c478bd9Sstevel@tonic-gate namep = *cpp; 10827c478bd9Sstevel@tonic-gate sp = strchr(*cpp, '/'); 10837c478bd9Sstevel@tonic-gate if (sp != NULL) { /* more to follow */ 10847c478bd9Sstevel@tonic-gate *sp = '\0'; 10857c478bd9Sstevel@tonic-gate *cpp = sp + 1; 10867c478bd9Sstevel@tonic-gate } else { /* this is last component. */ 10877c478bd9Sstevel@tonic-gate *cpp = NULL; 10887c478bd9Sstevel@tonic-gate } 10897c478bd9Sstevel@tonic-gate ap = strchr(namep, '@'); 10907c478bd9Sstevel@tonic-gate if (ap == NULL) { 10917c478bd9Sstevel@tonic-gate *addrp = NULL; 10927c478bd9Sstevel@tonic-gate } else { 10937c478bd9Sstevel@tonic-gate *ap = '\0'; /* terminate the name */ 10947c478bd9Sstevel@tonic-gate *addrp = ap + 1; 10957c478bd9Sstevel@tonic-gate } 10967c478bd9Sstevel@tonic-gate return (namep); 10977c478bd9Sstevel@tonic-gate } 10987c478bd9Sstevel@tonic-gate 10997c478bd9Sstevel@tonic-gate /* 11007c478bd9Sstevel@tonic-gate * Allocate a node and storage for name and addr strings, and fill them in. 11017c478bd9Sstevel@tonic-gate */ 11027c478bd9Sstevel@tonic-gate static in_node_t * 11037c478bd9Sstevel@tonic-gate in_alloc_node(char *name, char *addr) 11047c478bd9Sstevel@tonic-gate { 11057c478bd9Sstevel@tonic-gate in_node_t *np; 11067c478bd9Sstevel@tonic-gate char *cp; 11077c478bd9Sstevel@tonic-gate size_t namelen; 11087c478bd9Sstevel@tonic-gate 11097c478bd9Sstevel@tonic-gate ASSERT(e_ddi_inst_state.ins_busy); 11107c478bd9Sstevel@tonic-gate /* 11117c478bd9Sstevel@tonic-gate * Has name or will become root 11127c478bd9Sstevel@tonic-gate */ 11137c478bd9Sstevel@tonic-gate ASSERT(name || e_ddi_inst_state.ins_root == NULL); 11147c478bd9Sstevel@tonic-gate if (addr == NULL) 11157c478bd9Sstevel@tonic-gate addr = ""; 11167c478bd9Sstevel@tonic-gate if (name == NULL) 11177c478bd9Sstevel@tonic-gate namelen = 0; 11187c478bd9Sstevel@tonic-gate else 11197c478bd9Sstevel@tonic-gate namelen = strlen(name) + 1; 11207c478bd9Sstevel@tonic-gate cp = kmem_zalloc(sizeof (in_node_t) + namelen + strlen(addr) + 1, 11217c478bd9Sstevel@tonic-gate KM_SLEEP); 11227c478bd9Sstevel@tonic-gate np = (in_node_t *)cp; 11237c478bd9Sstevel@tonic-gate if (name) { 11247c478bd9Sstevel@tonic-gate np->in_node_name = cp + sizeof (in_node_t); 11257c478bd9Sstevel@tonic-gate (void) strcpy(np->in_node_name, name); 11267c478bd9Sstevel@tonic-gate } 11277c478bd9Sstevel@tonic-gate np->in_unit_addr = cp + sizeof (in_node_t) + namelen; 11287c478bd9Sstevel@tonic-gate (void) strcpy(np->in_unit_addr, addr); 11297c478bd9Sstevel@tonic-gate return (np); 11307c478bd9Sstevel@tonic-gate } 11317c478bd9Sstevel@tonic-gate 11327c478bd9Sstevel@tonic-gate /* 11337c478bd9Sstevel@tonic-gate * Allocate a drv entry and storage for binding name string, and fill it in. 11347c478bd9Sstevel@tonic-gate */ 11357c478bd9Sstevel@tonic-gate static in_drv_t * 11367c478bd9Sstevel@tonic-gate in_alloc_drv(char *bindingname) 11377c478bd9Sstevel@tonic-gate { 11387c478bd9Sstevel@tonic-gate in_drv_t *dp; 11397c478bd9Sstevel@tonic-gate char *cp; 11407c478bd9Sstevel@tonic-gate size_t namelen; 11417c478bd9Sstevel@tonic-gate 11427c478bd9Sstevel@tonic-gate ASSERT(e_ddi_inst_state.ins_busy); 11437c478bd9Sstevel@tonic-gate /* 11447c478bd9Sstevel@tonic-gate * Has name or will become root 11457c478bd9Sstevel@tonic-gate */ 11467c478bd9Sstevel@tonic-gate ASSERT(bindingname || e_ddi_inst_state.ins_root == NULL); 11477c478bd9Sstevel@tonic-gate if (bindingname == NULL) 11487c478bd9Sstevel@tonic-gate namelen = 0; 11497c478bd9Sstevel@tonic-gate else 11507c478bd9Sstevel@tonic-gate namelen = strlen(bindingname) + 1; 11517c478bd9Sstevel@tonic-gate cp = kmem_zalloc(sizeof (in_drv_t) + namelen, KM_SLEEP); 11527c478bd9Sstevel@tonic-gate dp = (in_drv_t *)cp; 11537c478bd9Sstevel@tonic-gate if (bindingname) { 11547c478bd9Sstevel@tonic-gate dp->ind_driver_name = cp + sizeof (in_drv_t); 11557c478bd9Sstevel@tonic-gate (void) strcpy(dp->ind_driver_name, bindingname); 11567c478bd9Sstevel@tonic-gate } 11577c478bd9Sstevel@tonic-gate dp->ind_state = IN_UNKNOWN; 11587c478bd9Sstevel@tonic-gate dp->ind_instance = -1; 11597c478bd9Sstevel@tonic-gate return (dp); 11607c478bd9Sstevel@tonic-gate } 11617c478bd9Sstevel@tonic-gate 11627c478bd9Sstevel@tonic-gate static void 11637c478bd9Sstevel@tonic-gate in_dealloc_node(in_node_t *np) 11647c478bd9Sstevel@tonic-gate { 11657c478bd9Sstevel@tonic-gate /* 11667c478bd9Sstevel@tonic-gate * The root node can never be de-allocated 11677c478bd9Sstevel@tonic-gate */ 11687c478bd9Sstevel@tonic-gate ASSERT(np->in_node_name && np->in_unit_addr); 11697c478bd9Sstevel@tonic-gate ASSERT(e_ddi_inst_state.ins_busy); 11707c478bd9Sstevel@tonic-gate kmem_free(np, sizeof (in_node_t) + strlen(np->in_node_name) 11717c478bd9Sstevel@tonic-gate + strlen(np->in_unit_addr) + 2); 11727c478bd9Sstevel@tonic-gate } 11737c478bd9Sstevel@tonic-gate 11747c478bd9Sstevel@tonic-gate static void 11757c478bd9Sstevel@tonic-gate in_dealloc_drv(in_drv_t *dp) 11767c478bd9Sstevel@tonic-gate { 11777c478bd9Sstevel@tonic-gate ASSERT(dp->ind_driver_name); 11787c478bd9Sstevel@tonic-gate ASSERT(e_ddi_inst_state.ins_busy); 11797c478bd9Sstevel@tonic-gate kmem_free(dp, sizeof (in_drv_t) + strlen(dp->ind_driver_name) 11807c478bd9Sstevel@tonic-gate + 1); 11817c478bd9Sstevel@tonic-gate } 11827c478bd9Sstevel@tonic-gate 11837c478bd9Sstevel@tonic-gate /* 11847c478bd9Sstevel@tonic-gate * Handle the various possible versions of "no address" 11857c478bd9Sstevel@tonic-gate */ 11867c478bd9Sstevel@tonic-gate static int 11877c478bd9Sstevel@tonic-gate in_eqstr(char *a, char *b) 11887c478bd9Sstevel@tonic-gate { 11897c478bd9Sstevel@tonic-gate if (a == b) /* covers case where both are nulls */ 11907c478bd9Sstevel@tonic-gate return (1); 11917c478bd9Sstevel@tonic-gate if (a == NULL && *b == 0) 11927c478bd9Sstevel@tonic-gate return (1); 11937c478bd9Sstevel@tonic-gate if (b == NULL && *a == 0) 11947c478bd9Sstevel@tonic-gate return (1); 11957c478bd9Sstevel@tonic-gate if (a == NULL || b == NULL) 11967c478bd9Sstevel@tonic-gate return (0); 11977c478bd9Sstevel@tonic-gate return (strcmp(a, b) == 0); 11987c478bd9Sstevel@tonic-gate } 11997c478bd9Sstevel@tonic-gate 12007c478bd9Sstevel@tonic-gate /* 12017c478bd9Sstevel@tonic-gate * Returns true if instance no. is already in use by named driver 12027c478bd9Sstevel@tonic-gate */ 12037c478bd9Sstevel@tonic-gate static int 12047c478bd9Sstevel@tonic-gate in_inuse(int instance, char *name) 12057c478bd9Sstevel@tonic-gate { 12067c478bd9Sstevel@tonic-gate major_t major; 12077c478bd9Sstevel@tonic-gate in_drv_t *dp; 12087c478bd9Sstevel@tonic-gate struct devnames *dnp; 12097c478bd9Sstevel@tonic-gate 12107c478bd9Sstevel@tonic-gate ASSERT(e_ddi_inst_state.ins_busy); 12117c478bd9Sstevel@tonic-gate /* 12127c478bd9Sstevel@tonic-gate * For now, if we've never heard of this device we assume it is not 12137c478bd9Sstevel@tonic-gate * in use, since we can't tell 12147c478bd9Sstevel@tonic-gate * XXX could do the weaker search through the nomajor list checking 12157c478bd9Sstevel@tonic-gate * XXX for the same name 12167c478bd9Sstevel@tonic-gate */ 1217a204de77Scth if ((major = ddi_name_to_major(name)) == DDI_MAJOR_T_NONE) 12187c478bd9Sstevel@tonic-gate return (0); 12197c478bd9Sstevel@tonic-gate dnp = &devnamesp[major]; 12207c478bd9Sstevel@tonic-gate 12217c478bd9Sstevel@tonic-gate dp = dnp->dn_inlist; 12227c478bd9Sstevel@tonic-gate while (dp) { 12237c478bd9Sstevel@tonic-gate if (dp->ind_instance == instance) 12247c478bd9Sstevel@tonic-gate return (1); 12257c478bd9Sstevel@tonic-gate dp = dp->ind_next; 12267c478bd9Sstevel@tonic-gate } 12277c478bd9Sstevel@tonic-gate return (0); 12287c478bd9Sstevel@tonic-gate } 12297c478bd9Sstevel@tonic-gate 12307c478bd9Sstevel@tonic-gate static void 12317c478bd9Sstevel@tonic-gate in_hashdrv(in_drv_t *dp) 12327c478bd9Sstevel@tonic-gate { 12337c478bd9Sstevel@tonic-gate struct devnames *dnp; 12347c478bd9Sstevel@tonic-gate in_drv_t *mp, *pp; 12357c478bd9Sstevel@tonic-gate major_t major; 12367c478bd9Sstevel@tonic-gate 12377c478bd9Sstevel@tonic-gate /* hash to no major list */ 1238a204de77Scth major = ddi_name_to_major(dp->ind_driver_name); 1239a204de77Scth if (major == DDI_MAJOR_T_NONE) { 12407c478bd9Sstevel@tonic-gate dp->ind_next = e_ddi_inst_state.ins_no_major; 12417c478bd9Sstevel@tonic-gate e_ddi_inst_state.ins_no_major = dp; 12427c478bd9Sstevel@tonic-gate return; 12437c478bd9Sstevel@tonic-gate } 12447c478bd9Sstevel@tonic-gate 12457c478bd9Sstevel@tonic-gate /* 12467c478bd9Sstevel@tonic-gate * dnp->dn_inlist is sorted by instance number. 12477c478bd9Sstevel@tonic-gate * Adding a new instance entry may introduce holes, 12487c478bd9Sstevel@tonic-gate * set dn_instance to IN_SEARCHME so the next instance 12497c478bd9Sstevel@tonic-gate * assignment may fill in holes. 12507c478bd9Sstevel@tonic-gate */ 12517c478bd9Sstevel@tonic-gate dnp = &devnamesp[major]; 12527c478bd9Sstevel@tonic-gate pp = mp = dnp->dn_inlist; 12537c478bd9Sstevel@tonic-gate if (mp == NULL || dp->ind_instance < mp->ind_instance) { 12547c478bd9Sstevel@tonic-gate /* prepend as the first entry, turn on IN_SEARCHME */ 12557c478bd9Sstevel@tonic-gate dnp->dn_instance = IN_SEARCHME; 12567c478bd9Sstevel@tonic-gate dp->ind_next = mp; 12577c478bd9Sstevel@tonic-gate dnp->dn_inlist = dp; 12587c478bd9Sstevel@tonic-gate return; 12597c478bd9Sstevel@tonic-gate } 12607c478bd9Sstevel@tonic-gate 12617c478bd9Sstevel@tonic-gate ASSERT(mp->ind_instance != dp->ind_instance); 12627c478bd9Sstevel@tonic-gate while (mp->ind_instance < dp->ind_instance && mp->ind_next) { 12637c478bd9Sstevel@tonic-gate pp = mp; 12647c478bd9Sstevel@tonic-gate mp = mp->ind_next; 12657c478bd9Sstevel@tonic-gate ASSERT(mp->ind_instance != dp->ind_instance); 12667c478bd9Sstevel@tonic-gate } 12677c478bd9Sstevel@tonic-gate 12687c478bd9Sstevel@tonic-gate if (mp->ind_instance < dp->ind_instance) { /* end of list */ 12697c478bd9Sstevel@tonic-gate dp->ind_next = NULL; 12707c478bd9Sstevel@tonic-gate mp->ind_next = dp; 12717c478bd9Sstevel@tonic-gate } else { 12727c478bd9Sstevel@tonic-gate ASSERT(dnp->dn_instance == IN_SEARCHME); 12737c478bd9Sstevel@tonic-gate dp->ind_next = pp->ind_next; 12747c478bd9Sstevel@tonic-gate pp->ind_next = dp; 12757c478bd9Sstevel@tonic-gate } 12767c478bd9Sstevel@tonic-gate } 12777c478bd9Sstevel@tonic-gate 12787c478bd9Sstevel@tonic-gate /* 12797c478bd9Sstevel@tonic-gate * Remove a driver entry from the list, given a previous pointer 12807c478bd9Sstevel@tonic-gate */ 12817c478bd9Sstevel@tonic-gate static void 12827c478bd9Sstevel@tonic-gate in_removedrv(struct devnames *dnp, in_drv_t *mp) 12837c478bd9Sstevel@tonic-gate { 12847c478bd9Sstevel@tonic-gate in_drv_t *dp; 12857c478bd9Sstevel@tonic-gate in_drv_t *prevp; 12867c478bd9Sstevel@tonic-gate 12877c478bd9Sstevel@tonic-gate if (dnp->dn_inlist == mp) { /* head of list */ 12887c478bd9Sstevel@tonic-gate dnp->dn_inlist = mp->ind_next; 12897c478bd9Sstevel@tonic-gate dnp->dn_instance = IN_SEARCHME; 12907c478bd9Sstevel@tonic-gate in_dq_drv(mp); 12917c478bd9Sstevel@tonic-gate in_dealloc_drv(mp); 12927c478bd9Sstevel@tonic-gate return; 12937c478bd9Sstevel@tonic-gate } 12947c478bd9Sstevel@tonic-gate prevp = dnp->dn_inlist; 12957c478bd9Sstevel@tonic-gate for (dp = prevp->ind_next; dp; dp = dp->ind_next) { 12967c478bd9Sstevel@tonic-gate if (dp == mp) { /* found it */ 12977c478bd9Sstevel@tonic-gate break; 12987c478bd9Sstevel@tonic-gate } 12997c478bd9Sstevel@tonic-gate prevp = dp; 13007c478bd9Sstevel@tonic-gate } 13017c478bd9Sstevel@tonic-gate 13027c478bd9Sstevel@tonic-gate ASSERT(dp == mp); 13037c478bd9Sstevel@tonic-gate dnp->dn_instance = IN_SEARCHME; 13047c478bd9Sstevel@tonic-gate prevp->ind_next = mp->ind_next; 13057c478bd9Sstevel@tonic-gate in_dq_drv(mp); 13067c478bd9Sstevel@tonic-gate in_dealloc_drv(mp); 13077c478bd9Sstevel@tonic-gate } 13087c478bd9Sstevel@tonic-gate 13097c478bd9Sstevel@tonic-gate static void 13107c478bd9Sstevel@tonic-gate in_dq_drv(in_drv_t *mp) 13117c478bd9Sstevel@tonic-gate { 13127c478bd9Sstevel@tonic-gate struct in_node *node = mp->ind_node; 13137c478bd9Sstevel@tonic-gate in_drv_t *ptr, *prev; 13147c478bd9Sstevel@tonic-gate 13157c478bd9Sstevel@tonic-gate if (mp == node->in_drivers) { 13167c478bd9Sstevel@tonic-gate node->in_drivers = mp->ind_next_drv; 13177c478bd9Sstevel@tonic-gate return; 13187c478bd9Sstevel@tonic-gate } 13197c478bd9Sstevel@tonic-gate prev = node->in_drivers; 13207c478bd9Sstevel@tonic-gate for (ptr = prev->ind_next_drv; ptr != (struct in_drv *)NULL; 13217c478bd9Sstevel@tonic-gate ptr = ptr->ind_next_drv) { 13227c478bd9Sstevel@tonic-gate if (ptr == mp) { 13237c478bd9Sstevel@tonic-gate prev->ind_next_drv = ptr->ind_next_drv; 13247c478bd9Sstevel@tonic-gate return; 13257c478bd9Sstevel@tonic-gate } 1326ffc89d77Svikram prev = ptr; 13277c478bd9Sstevel@tonic-gate } 13287c478bd9Sstevel@tonic-gate panic("in_dq_drv: in_drv not found on node driver list"); 13297c478bd9Sstevel@tonic-gate } 13307c478bd9Sstevel@tonic-gate 13317c478bd9Sstevel@tonic-gate 13327c478bd9Sstevel@tonic-gate in_drv_t * 13337c478bd9Sstevel@tonic-gate in_drvwalk(in_node_t *np, char *binding_name) 13347c478bd9Sstevel@tonic-gate { 13357c478bd9Sstevel@tonic-gate char *name; 13367c478bd9Sstevel@tonic-gate in_drv_t *dp = np->in_drivers; 13377c478bd9Sstevel@tonic-gate while (dp) { 13387c478bd9Sstevel@tonic-gate if ((name = i_binding_to_drv_name(dp->ind_driver_name)) 13397c478bd9Sstevel@tonic-gate == NULL) { 13407c478bd9Sstevel@tonic-gate name = dp->ind_driver_name; 13417c478bd9Sstevel@tonic-gate } 13427c478bd9Sstevel@tonic-gate if (strcmp(binding_name, name) == 0) { 13437c478bd9Sstevel@tonic-gate break; 13447c478bd9Sstevel@tonic-gate } 13457c478bd9Sstevel@tonic-gate dp = dp->ind_next_drv; 13467c478bd9Sstevel@tonic-gate } 13477c478bd9Sstevel@tonic-gate return (dp); 13487c478bd9Sstevel@tonic-gate } 13497c478bd9Sstevel@tonic-gate 13507c478bd9Sstevel@tonic-gate 13517c478bd9Sstevel@tonic-gate 13527c478bd9Sstevel@tonic-gate static void 13537c478bd9Sstevel@tonic-gate i_log_devfs_instance_mod(void) 13547c478bd9Sstevel@tonic-gate { 13557c478bd9Sstevel@tonic-gate sysevent_t *ev; 13567c478bd9Sstevel@tonic-gate sysevent_id_t eid; 135716bd7258Scth static int sent_one = 0; 13587c478bd9Sstevel@tonic-gate 13597c478bd9Sstevel@tonic-gate /* 136016bd7258Scth * Prevent unnecessary event generation. Do not generate more than 136116bd7258Scth * one event during boot. 13627c478bd9Sstevel@tonic-gate */ 136316bd7258Scth if (sent_one && !i_ddi_io_initialized()) 13647c478bd9Sstevel@tonic-gate return; 13657c478bd9Sstevel@tonic-gate 13667c478bd9Sstevel@tonic-gate ev = sysevent_alloc(EC_DEVFS, ESC_DEVFS_INSTANCE_MOD, EP_DDI, 13677c478bd9Sstevel@tonic-gate SE_NOSLEEP); 13687c478bd9Sstevel@tonic-gate if (ev == NULL) { 13697c478bd9Sstevel@tonic-gate return; 13707c478bd9Sstevel@tonic-gate } 13717c478bd9Sstevel@tonic-gate if (log_sysevent(ev, SE_NOSLEEP, &eid) != 0) { 13727c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "i_log_devfs_instance_mod: failed to post " 13737c478bd9Sstevel@tonic-gate "event"); 137416bd7258Scth } else { 137516bd7258Scth sent_one = 1; 13767c478bd9Sstevel@tonic-gate } 13777c478bd9Sstevel@tonic-gate sysevent_free(ev); 13787c478bd9Sstevel@tonic-gate } 13797c478bd9Sstevel@tonic-gate 13807c478bd9Sstevel@tonic-gate void 13817c478bd9Sstevel@tonic-gate e_ddi_enter_instance() 13827c478bd9Sstevel@tonic-gate { 13837c478bd9Sstevel@tonic-gate mutex_enter(&e_ddi_inst_state.ins_serial); 13847c478bd9Sstevel@tonic-gate if (e_ddi_inst_state.ins_thread == curthread) 13857c478bd9Sstevel@tonic-gate e_ddi_inst_state.ins_busy++; 13867c478bd9Sstevel@tonic-gate else { 13877c478bd9Sstevel@tonic-gate while (e_ddi_inst_state.ins_busy) 13887c478bd9Sstevel@tonic-gate cv_wait(&e_ddi_inst_state.ins_serial_cv, 13897c478bd9Sstevel@tonic-gate &e_ddi_inst_state.ins_serial); 13907c478bd9Sstevel@tonic-gate e_ddi_inst_state.ins_thread = curthread; 13917c478bd9Sstevel@tonic-gate e_ddi_inst_state.ins_busy = 1; 13927c478bd9Sstevel@tonic-gate } 13937c478bd9Sstevel@tonic-gate mutex_exit(&e_ddi_inst_state.ins_serial); 13947c478bd9Sstevel@tonic-gate } 13957c478bd9Sstevel@tonic-gate 13967c478bd9Sstevel@tonic-gate void 13977c478bd9Sstevel@tonic-gate e_ddi_exit_instance() 13987c478bd9Sstevel@tonic-gate { 13997c478bd9Sstevel@tonic-gate mutex_enter(&e_ddi_inst_state.ins_serial); 14007c478bd9Sstevel@tonic-gate e_ddi_inst_state.ins_busy--; 14017c478bd9Sstevel@tonic-gate if (e_ddi_inst_state.ins_busy == 0) { 14027c478bd9Sstevel@tonic-gate cv_broadcast(&e_ddi_inst_state.ins_serial_cv); 14037c478bd9Sstevel@tonic-gate e_ddi_inst_state.ins_thread = NULL; 14047c478bd9Sstevel@tonic-gate } 14057c478bd9Sstevel@tonic-gate mutex_exit(&e_ddi_inst_state.ins_serial); 14067c478bd9Sstevel@tonic-gate } 14077c478bd9Sstevel@tonic-gate 14087c478bd9Sstevel@tonic-gate int 14097c478bd9Sstevel@tonic-gate e_ddi_instance_is_clean() 14107c478bd9Sstevel@tonic-gate { 14117c478bd9Sstevel@tonic-gate return (e_ddi_inst_state.ins_dirty == 0); 14127c478bd9Sstevel@tonic-gate } 14137c478bd9Sstevel@tonic-gate 14147c478bd9Sstevel@tonic-gate void 14157c478bd9Sstevel@tonic-gate e_ddi_instance_set_clean() 14167c478bd9Sstevel@tonic-gate { 14177c478bd9Sstevel@tonic-gate e_ddi_inst_state.ins_dirty = 0; 14187c478bd9Sstevel@tonic-gate } 14197c478bd9Sstevel@tonic-gate 14207c478bd9Sstevel@tonic-gate in_node_t * 14217c478bd9Sstevel@tonic-gate e_ddi_instance_root() 14227c478bd9Sstevel@tonic-gate { 14237c478bd9Sstevel@tonic-gate return (e_ddi_inst_state.ins_root); 14247c478bd9Sstevel@tonic-gate } 14257c478bd9Sstevel@tonic-gate 14267c478bd9Sstevel@tonic-gate /* 14277c478bd9Sstevel@tonic-gate * Visit a node in the instance tree 14287c478bd9Sstevel@tonic-gate */ 14297c478bd9Sstevel@tonic-gate static int 14307c478bd9Sstevel@tonic-gate in_walk_instances(in_node_t *np, char *path, char *this, 14317c478bd9Sstevel@tonic-gate int (*f)(const char *, in_node_t *, in_drv_t *, void *), void *arg) 14327c478bd9Sstevel@tonic-gate { 14337c478bd9Sstevel@tonic-gate in_drv_t *dp; 14347c478bd9Sstevel@tonic-gate int rval = INST_WALK_CONTINUE; 14357c478bd9Sstevel@tonic-gate char *next; 14367c478bd9Sstevel@tonic-gate 14377c478bd9Sstevel@tonic-gate while (np != NULL) { 14387c478bd9Sstevel@tonic-gate 14397c478bd9Sstevel@tonic-gate if (np->in_unit_addr[0] == 0) 14407c478bd9Sstevel@tonic-gate (void) sprintf(this, "/%s", np->in_node_name); 14417c478bd9Sstevel@tonic-gate else 14427c478bd9Sstevel@tonic-gate (void) sprintf(this, "/%s@%s", np->in_node_name, 14437c478bd9Sstevel@tonic-gate np->in_unit_addr); 14447c478bd9Sstevel@tonic-gate next = this + strlen(this); 14457c478bd9Sstevel@tonic-gate 14467c478bd9Sstevel@tonic-gate for (dp = np->in_drivers; dp; dp = dp->ind_next_drv) { 14477c478bd9Sstevel@tonic-gate if (dp->ind_state == IN_PERMANENT) { 14487c478bd9Sstevel@tonic-gate rval = (*f)(path, np, dp, arg); 14497c478bd9Sstevel@tonic-gate if (rval == INST_WALK_TERMINATE) 14507c478bd9Sstevel@tonic-gate break; 14517c478bd9Sstevel@tonic-gate } 14527c478bd9Sstevel@tonic-gate } 14537c478bd9Sstevel@tonic-gate if (np->in_child) { 14547c478bd9Sstevel@tonic-gate rval = in_walk_instances(np->in_child, 14557c478bd9Sstevel@tonic-gate path, next, f, arg); 14567c478bd9Sstevel@tonic-gate if (rval == INST_WALK_TERMINATE) 14577c478bd9Sstevel@tonic-gate break; 14587c478bd9Sstevel@tonic-gate } 14597c478bd9Sstevel@tonic-gate 14607c478bd9Sstevel@tonic-gate np = np->in_sibling; 14617c478bd9Sstevel@tonic-gate } 14627c478bd9Sstevel@tonic-gate 14637c478bd9Sstevel@tonic-gate return (rval); 14647c478bd9Sstevel@tonic-gate } 14657c478bd9Sstevel@tonic-gate 14667c478bd9Sstevel@tonic-gate /* 14677c478bd9Sstevel@tonic-gate * A general interface for walking the instance tree, 14687c478bd9Sstevel@tonic-gate * calling a user-supplied callback for each node. 14697c478bd9Sstevel@tonic-gate */ 14707c478bd9Sstevel@tonic-gate int 14717c478bd9Sstevel@tonic-gate e_ddi_walk_instances(int (*f)(const char *, 14727c478bd9Sstevel@tonic-gate in_node_t *, in_drv_t *, void *), void *arg) 14737c478bd9Sstevel@tonic-gate { 14747c478bd9Sstevel@tonic-gate in_node_t *root; 14757c478bd9Sstevel@tonic-gate int rval; 14767c478bd9Sstevel@tonic-gate char *path; 14777c478bd9Sstevel@tonic-gate 14787c478bd9Sstevel@tonic-gate path = kmem_zalloc(MAXPATHLEN, KM_SLEEP); 14797c478bd9Sstevel@tonic-gate 14807c478bd9Sstevel@tonic-gate e_ddi_enter_instance(); 14817c478bd9Sstevel@tonic-gate root = e_ddi_instance_root(); 14827c478bd9Sstevel@tonic-gate rval = in_walk_instances(root->in_child, path, path, f, arg); 14837c478bd9Sstevel@tonic-gate e_ddi_exit_instance(); 14847c478bd9Sstevel@tonic-gate 14857c478bd9Sstevel@tonic-gate kmem_free(path, MAXPATHLEN); 14867c478bd9Sstevel@tonic-gate return (rval); 14877c478bd9Sstevel@tonic-gate } 1488