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 /* 2294c894bbSVikram Hegde * Copyright (c) 1992, 2010, Oracle and/or its affiliates. All rights reserved. 237c478bd9Sstevel@tonic-gate */ 247c478bd9Sstevel@tonic-gate 257c478bd9Sstevel@tonic-gate /* 267c478bd9Sstevel@tonic-gate * Instance number assignment code 277c478bd9Sstevel@tonic-gate */ 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate #include <sys/types.h> 307c478bd9Sstevel@tonic-gate #include <sys/param.h> 317c478bd9Sstevel@tonic-gate #include <sys/errno.h> 327c478bd9Sstevel@tonic-gate #include <sys/systm.h> 337c478bd9Sstevel@tonic-gate #include <sys/kobj.h> 347c478bd9Sstevel@tonic-gate #include <sys/t_lock.h> 357c478bd9Sstevel@tonic-gate #include <sys/kmem.h> 367c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h> 377c478bd9Sstevel@tonic-gate #include <sys/ddi.h> 387c478bd9Sstevel@tonic-gate #include <sys/sunddi.h> 397c478bd9Sstevel@tonic-gate #include <sys/autoconf.h> 407c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h> 417c478bd9Sstevel@tonic-gate #include <sys/hwconf.h> 427c478bd9Sstevel@tonic-gate #include <sys/reboot.h> 437c478bd9Sstevel@tonic-gate #include <sys/ddi_impldefs.h> 447c478bd9Sstevel@tonic-gate #include <sys/instance.h> 457c478bd9Sstevel@tonic-gate #include <sys/debug.h> 467c478bd9Sstevel@tonic-gate #include <sys/sysevent.h> 477c478bd9Sstevel@tonic-gate #include <sys/modctl.h> 487c478bd9Sstevel@tonic-gate #include <sys/console.h> 497c478bd9Sstevel@tonic-gate #include <sys/cladm.h> 5094c894bbSVikram Hegde #include <sys/sysmacros.h> 5194c894bbSVikram Hegde #include <sys/crc32.h> 5294c894bbSVikram Hegde 537c478bd9Sstevel@tonic-gate 547c478bd9Sstevel@tonic-gate static void in_preassign_instance(void); 557c478bd9Sstevel@tonic-gate static void i_log_devfs_instance_mod(void); 567c478bd9Sstevel@tonic-gate static int in_get_infile(char *); 577c478bd9Sstevel@tonic-gate static void in_removenode(struct devnames *dnp, in_node_t *mp, in_node_t *ap); 587c478bd9Sstevel@tonic-gate static in_node_t *in_alloc_node(char *name, char *addr); 597c478bd9Sstevel@tonic-gate static int in_eqstr(char *a, char *b); 607c478bd9Sstevel@tonic-gate static char *in_name_addr(char **cpp, char **addrp); 617c478bd9Sstevel@tonic-gate static in_node_t *in_devwalk(dev_info_t *dip, in_node_t **ap, char *addr); 627c478bd9Sstevel@tonic-gate static void in_dealloc_node(in_node_t *np); 637c478bd9Sstevel@tonic-gate static in_node_t *in_make_path(char *path); 647c478bd9Sstevel@tonic-gate static void in_enlist(in_node_t *ap, in_node_t *np); 657c478bd9Sstevel@tonic-gate static int in_inuse(int instance, char *name); 667c478bd9Sstevel@tonic-gate static void in_hashdrv(in_drv_t *dp); 677c478bd9Sstevel@tonic-gate static in_drv_t *in_drvwalk(in_node_t *np, char *binding_name); 687c478bd9Sstevel@tonic-gate static in_drv_t *in_alloc_drv(char *bindingname); 697c478bd9Sstevel@tonic-gate static void in_endrv(in_node_t *np, in_drv_t *dp); 707c478bd9Sstevel@tonic-gate static void in_dq_drv(in_drv_t *np); 717c478bd9Sstevel@tonic-gate static void in_removedrv(struct devnames *dnp, in_drv_t *mp); 727c478bd9Sstevel@tonic-gate static int in_pathin(char *cp, int instance, char *bname, struct bind **args); 7316bd7258Scth static int in_next_instance_block(major_t, int); 747c478bd9Sstevel@tonic-gate static int in_next_instance(major_t); 757c478bd9Sstevel@tonic-gate 7694c894bbSVikram Hegde #pragma weak plat_ioaliases_init 7794c894bbSVikram Hegde 7894c894bbSVikram Hegde 797c478bd9Sstevel@tonic-gate /* external functions */ 807c478bd9Sstevel@tonic-gate extern char *i_binding_to_drv_name(char *bname); 8194c894bbSVikram Hegde extern void plat_ioaliases_init(void); 827c478bd9Sstevel@tonic-gate 837c478bd9Sstevel@tonic-gate /* 847c478bd9Sstevel@tonic-gate * This plus devnames defines the entire software state of the instance world. 857c478bd9Sstevel@tonic-gate */ 867c478bd9Sstevel@tonic-gate typedef struct in_softstate { 877c478bd9Sstevel@tonic-gate in_node_t *ins_root; /* the root of our instance tree */ 887c478bd9Sstevel@tonic-gate in_drv_t *ins_no_major; /* majorless drv entries */ 897c478bd9Sstevel@tonic-gate /* 907c478bd9Sstevel@tonic-gate * Used to serialize access to data structures 917c478bd9Sstevel@tonic-gate */ 927c478bd9Sstevel@tonic-gate void *ins_thread; 937c478bd9Sstevel@tonic-gate kmutex_t ins_serial; 947c478bd9Sstevel@tonic-gate kcondvar_t ins_serial_cv; 957c478bd9Sstevel@tonic-gate int ins_busy; 9694c894bbSVikram Hegde boolean_t ins_dirty; /* instance info needs flush */ 977c478bd9Sstevel@tonic-gate } in_softstate_t; 987c478bd9Sstevel@tonic-gate 997c478bd9Sstevel@tonic-gate static in_softstate_t e_ddi_inst_state; 1007c478bd9Sstevel@tonic-gate 1017c478bd9Sstevel@tonic-gate /* 1027c478bd9Sstevel@tonic-gate * State transition information: 1037c478bd9Sstevel@tonic-gate * e_ddi_inst_state contains, among other things, the root of a tree of 1047c478bd9Sstevel@tonic-gate * device nodes used to track instance number assignments. 1057c478bd9Sstevel@tonic-gate * Each device node may contain multiple driver bindings, represented 1067c478bd9Sstevel@tonic-gate * by a linked list of in_drv_t nodes, each with an instance assignment 1077c478bd9Sstevel@tonic-gate * (except for root node). Each in_drv node can be in one of 3 states, 1087c478bd9Sstevel@tonic-gate * indicated by ind_state: 1097c478bd9Sstevel@tonic-gate * 1107c478bd9Sstevel@tonic-gate * IN_UNKNOWN: Each node created in this state. The instance number of 1117c478bd9Sstevel@tonic-gate * this node is not known. ind_instance is set to -1. 1127c478bd9Sstevel@tonic-gate * IN_PROVISIONAL: When a node is assigned an instance number in 1137c478bd9Sstevel@tonic-gate * e_ddi_assign_instance(), its state is set to IN_PROVISIONAL. 1147c478bd9Sstevel@tonic-gate * Subsequently, the framework will always call either 11594c894bbSVikram Hegde * e_ddi_keep_instance() which makes the node IN_PERMANENT 1167c478bd9Sstevel@tonic-gate * or e_ddi_free_instance(), which deletes the node. 1177c478bd9Sstevel@tonic-gate * IN_PERMANENT: 1187c478bd9Sstevel@tonic-gate * If e_ddi_keep_instance() is called on an IN_PROVISIONAL node, 1197c478bd9Sstevel@tonic-gate * its state is set to IN_PERMANENT. 1207c478bd9Sstevel@tonic-gate */ 1217c478bd9Sstevel@tonic-gate 1227c478bd9Sstevel@tonic-gate static char *instance_file = INSTANCE_FILE; 1237c478bd9Sstevel@tonic-gate static char *instance_file_backup = INSTANCE_FILE INSTANCE_FILE_SUFFIX; 1247c478bd9Sstevel@tonic-gate 1257c478bd9Sstevel@tonic-gate /* 1267c478bd9Sstevel@tonic-gate * Return values for in_get_infile(). 1277c478bd9Sstevel@tonic-gate */ 1287c478bd9Sstevel@tonic-gate #define PTI_FOUND 0 1297c478bd9Sstevel@tonic-gate #define PTI_NOT_FOUND 1 1307c478bd9Sstevel@tonic-gate #define PTI_REBUILD 2 1317c478bd9Sstevel@tonic-gate 132328d222bSChris Horne int instance_searchme = 0; /* testing: use complex code path */ 13394c894bbSVikram Hegde 1347c478bd9Sstevel@tonic-gate /* 1357c478bd9Sstevel@tonic-gate * Path to instance file magic string used for first time boot after 1367c478bd9Sstevel@tonic-gate * an install. If this is the first string in the file we will 1377c478bd9Sstevel@tonic-gate * automatically rebuild the file. 1387c478bd9Sstevel@tonic-gate */ 1397c478bd9Sstevel@tonic-gate #define PTI_MAGIC_STR "#path_to_inst_bootstrap_1" 1407c478bd9Sstevel@tonic-gate #define PTI_MAGIC_STR_LEN (sizeof (PTI_MAGIC_STR) - 1) 1417c478bd9Sstevel@tonic-gate 1427c478bd9Sstevel@tonic-gate void 1437c478bd9Sstevel@tonic-gate e_ddi_instance_init(void) 1447c478bd9Sstevel@tonic-gate { 1457c478bd9Sstevel@tonic-gate char *file; 1467c478bd9Sstevel@tonic-gate int rebuild = 1; 1477c478bd9Sstevel@tonic-gate struct in_drv *dp; 1487c478bd9Sstevel@tonic-gate 1497c478bd9Sstevel@tonic-gate mutex_init(&e_ddi_inst_state.ins_serial, NULL, MUTEX_DEFAULT, NULL); 1507c478bd9Sstevel@tonic-gate cv_init(&e_ddi_inst_state.ins_serial_cv, NULL, CV_DEFAULT, NULL); 1517c478bd9Sstevel@tonic-gate 1527c478bd9Sstevel@tonic-gate /* 1537c478bd9Sstevel@tonic-gate * Only one thread is allowed to change the state of the instance 1547c478bd9Sstevel@tonic-gate * number assignments on the system at any given time. 1557c478bd9Sstevel@tonic-gate * Note that this is not really necessary, as we are single-threaded 1567c478bd9Sstevel@tonic-gate * here, but it won't hurt, and it allows us to keep ASSERTS for 1577c478bd9Sstevel@tonic-gate * our assumptions in the code. 1587c478bd9Sstevel@tonic-gate */ 1597c478bd9Sstevel@tonic-gate e_ddi_enter_instance(); 1607c478bd9Sstevel@tonic-gate 1617c478bd9Sstevel@tonic-gate /* 16294c894bbSVikram Hegde * Init the ioaliases if the platform supports it 16394c894bbSVikram Hegde */ 16494c894bbSVikram Hegde if (&plat_ioaliases_init) 16594c894bbSVikram Hegde plat_ioaliases_init(); 16694c894bbSVikram Hegde 16794c894bbSVikram Hegde /* 1687c478bd9Sstevel@tonic-gate * Create the root node, instance zallocs to 0. 1697c478bd9Sstevel@tonic-gate * The name and address of this node never get examined, we always 1707c478bd9Sstevel@tonic-gate * start searching with its first child. 1717c478bd9Sstevel@tonic-gate */ 1727c478bd9Sstevel@tonic-gate ASSERT(e_ddi_inst_state.ins_root == NULL); 1737c478bd9Sstevel@tonic-gate e_ddi_inst_state.ins_root = in_alloc_node(NULL, NULL); 1747c478bd9Sstevel@tonic-gate dp = in_alloc_drv("rootnex"); 1757c478bd9Sstevel@tonic-gate in_endrv(e_ddi_inst_state.ins_root, dp); 1767c478bd9Sstevel@tonic-gate 1777c478bd9Sstevel@tonic-gate file = instance_file; 1787c478bd9Sstevel@tonic-gate switch (in_get_infile(file)) { 1797c478bd9Sstevel@tonic-gate default: 1807c478bd9Sstevel@tonic-gate case PTI_NOT_FOUND: 1817c478bd9Sstevel@tonic-gate /* make sure path_to_inst is recreated */ 1827c478bd9Sstevel@tonic-gate boothowto |= RB_RECONFIG; 1837c478bd9Sstevel@tonic-gate 1847c478bd9Sstevel@tonic-gate /* 1857c478bd9Sstevel@tonic-gate * Something is wrong. First try the backup file. 1867c478bd9Sstevel@tonic-gate * If not found, rebuild path_to_inst. Emit a 1877c478bd9Sstevel@tonic-gate * message about the problem. 1887c478bd9Sstevel@tonic-gate */ 1897c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "%s empty or not found", file); 1907c478bd9Sstevel@tonic-gate 1917c478bd9Sstevel@tonic-gate file = instance_file_backup; 1927c478bd9Sstevel@tonic-gate if (in_get_infile(file) != PTI_FOUND) { 1937c478bd9Sstevel@tonic-gate cmn_err(CE_NOTE, "rebuilding device instance data"); 1947c478bd9Sstevel@tonic-gate break; 1957c478bd9Sstevel@tonic-gate } 1967c478bd9Sstevel@tonic-gate cmn_err(CE_NOTE, "using backup instance data in %s", file); 1977c478bd9Sstevel@tonic-gate /*FALLTHROUGH*/ 1987c478bd9Sstevel@tonic-gate 1997c478bd9Sstevel@tonic-gate case PTI_FOUND: 2007c478bd9Sstevel@tonic-gate /* 2017c478bd9Sstevel@tonic-gate * We've got a readable file 2027c478bd9Sstevel@tonic-gate * parse the file into the instance tree 2037c478bd9Sstevel@tonic-gate */ 2047c478bd9Sstevel@tonic-gate (void) read_binding_file(file, NULL, in_pathin); 2057c478bd9Sstevel@tonic-gate rebuild = 0; 2067c478bd9Sstevel@tonic-gate break; 2077c478bd9Sstevel@tonic-gate 2087c478bd9Sstevel@tonic-gate case PTI_REBUILD: 20994c894bbSVikram Hegde /* 21094c894bbSVikram Hegde * path_to_inst has magic str requesting a create 21194c894bbSVikram Hegde * Convert boot to reconfig boot to ensure /dev is 21294c894bbSVikram Hegde * in sync with new path_to_inst. 21394c894bbSVikram Hegde */ 21494c894bbSVikram Hegde boothowto |= RB_RECONFIG; 2157c478bd9Sstevel@tonic-gate cmn_err(CE_CONT, 2167c478bd9Sstevel@tonic-gate "?Using default device instance data\n"); 2177c478bd9Sstevel@tonic-gate break; 2187c478bd9Sstevel@tonic-gate } 2197c478bd9Sstevel@tonic-gate 2207c478bd9Sstevel@tonic-gate /* 2217c478bd9Sstevel@tonic-gate * The OBP device tree has been copied to the kernel and 2227c478bd9Sstevel@tonic-gate * bound to drivers at this point. We walk the per-driver 2237c478bd9Sstevel@tonic-gate * list to preassign instances. Since the bus addr is 2247c478bd9Sstevel@tonic-gate * unknown at this point, we cannot place the instance 2257c478bd9Sstevel@tonic-gate * number in the instance tree. This will be done at 2267c478bd9Sstevel@tonic-gate * a later time. 2277c478bd9Sstevel@tonic-gate */ 2287c478bd9Sstevel@tonic-gate if (rebuild) 2297c478bd9Sstevel@tonic-gate in_preassign_instance(); 2307c478bd9Sstevel@tonic-gate 2317c478bd9Sstevel@tonic-gate e_ddi_exit_instance(); 2327c478bd9Sstevel@tonic-gate } 2337c478bd9Sstevel@tonic-gate 2347c478bd9Sstevel@tonic-gate static void 2357c478bd9Sstevel@tonic-gate in_preassign_instance() 2367c478bd9Sstevel@tonic-gate { 2377c478bd9Sstevel@tonic-gate major_t m; 238328d222bSChris Horne struct devnames *dnp; 239328d222bSChris Horne dev_info_t *dip; 2407c478bd9Sstevel@tonic-gate extern major_t devcnt; 2417c478bd9Sstevel@tonic-gate 2427c478bd9Sstevel@tonic-gate for (m = 0; m < devcnt; m++) { 243328d222bSChris Horne dnp = &devnamesp[m]; 244328d222bSChris Horne dip = dnp->dn_head; 2457c478bd9Sstevel@tonic-gate while (dip) { 2467c478bd9Sstevel@tonic-gate DEVI(dip)->devi_instance = dnp->dn_instance; 2477c478bd9Sstevel@tonic-gate dnp->dn_instance++; 2487c478bd9Sstevel@tonic-gate dip = ddi_get_next(dip); 2497c478bd9Sstevel@tonic-gate } 250328d222bSChris Horne 251328d222bSChris Horne /* 252328d222bSChris Horne * The preassign instance numbers are not fully 253328d222bSChris Horne * accounted for until e_ddi_assign_instance(). 254328d222bSChris Horne * We can't fully account for them now because we 255328d222bSChris Horne * don't currently have a unit-address. Because of 256328d222bSChris Horne * this, we need to remember the preassign boundary 257328d222bSChris Horne * to avoid ordering issues related to 258328d222bSChris Horne * e_ddi_assign_instance of a preassigned value .vs. 259328d222bSChris Horne * re-assignment of the same value for a dynamic 260328d222bSChris Horne * SID node created by bus_config. 261328d222bSChris Horne */ 262328d222bSChris Horne dnp->dn_pinstance = dnp->dn_instance; 263c3408929SChris Horne dnp->dn_instance = IN_SEARCHME; 2647c478bd9Sstevel@tonic-gate } 2657c478bd9Sstevel@tonic-gate } 2667c478bd9Sstevel@tonic-gate 2677c478bd9Sstevel@tonic-gate /* 2687c478bd9Sstevel@tonic-gate * Checks to see if the /etc/path_to_inst file exists and whether or not 2697c478bd9Sstevel@tonic-gate * it has the magic string in it. 2707c478bd9Sstevel@tonic-gate * 2717c478bd9Sstevel@tonic-gate * Returns one of the following: 2727c478bd9Sstevel@tonic-gate * 2737c478bd9Sstevel@tonic-gate * PTI_FOUND - We have found the /etc/path_to_inst file 2747c478bd9Sstevel@tonic-gate * PTI_REBUILD - We have found the /etc/path_to_inst file and the 2757c478bd9Sstevel@tonic-gate * first line was PTI_MAGIC_STR. 2767c478bd9Sstevel@tonic-gate * PTI_NOT_FOUND - We did not find the /etc/path_to_inst file 2777c478bd9Sstevel@tonic-gate * 2787c478bd9Sstevel@tonic-gate */ 2797c478bd9Sstevel@tonic-gate static int 2807c478bd9Sstevel@tonic-gate in_get_infile(char *filename) 2817c478bd9Sstevel@tonic-gate { 282986fd29aSsetje struct _buf *file; 2837c478bd9Sstevel@tonic-gate int return_val; 2847c478bd9Sstevel@tonic-gate char buf[PTI_MAGIC_STR_LEN]; 2857c478bd9Sstevel@tonic-gate 2867c478bd9Sstevel@tonic-gate /* 2877c478bd9Sstevel@tonic-gate * Try to open the file. 2887c478bd9Sstevel@tonic-gate */ 289986fd29aSsetje if ((file = kobj_open_file(filename)) == (struct _buf *)-1) { 2907c478bd9Sstevel@tonic-gate return (PTI_NOT_FOUND); 2917c478bd9Sstevel@tonic-gate } 2927c478bd9Sstevel@tonic-gate return_val = PTI_FOUND; 2937c478bd9Sstevel@tonic-gate 2947c478bd9Sstevel@tonic-gate /* 2957c478bd9Sstevel@tonic-gate * Read the first PTI_MAGIC_STR_LEN bytes from the file to see if 2967c478bd9Sstevel@tonic-gate * it contains the magic string. If there aren't that many bytes 2977c478bd9Sstevel@tonic-gate * in the file, then assume file is correct and no magic string 2987c478bd9Sstevel@tonic-gate * and move on. 2997c478bd9Sstevel@tonic-gate */ 300986fd29aSsetje switch (kobj_read_file(file, buf, PTI_MAGIC_STR_LEN, 0)) { 3017c478bd9Sstevel@tonic-gate 3027c478bd9Sstevel@tonic-gate case PTI_MAGIC_STR_LEN: 3037c478bd9Sstevel@tonic-gate /* 3047c478bd9Sstevel@tonic-gate * If the first PTI_MAGIC_STR_LEN bytes are the magic string 3057c478bd9Sstevel@tonic-gate * then return PTI_REBUILD. 3067c478bd9Sstevel@tonic-gate */ 3077c478bd9Sstevel@tonic-gate if (strncmp(PTI_MAGIC_STR, buf, PTI_MAGIC_STR_LEN) == 0) 3087c478bd9Sstevel@tonic-gate return_val = PTI_REBUILD; 3097c478bd9Sstevel@tonic-gate break; 3107c478bd9Sstevel@tonic-gate 3117c478bd9Sstevel@tonic-gate case 0: 3127c478bd9Sstevel@tonic-gate /* 3137c478bd9Sstevel@tonic-gate * If the file is zero bytes in length, then consider the 3147c478bd9Sstevel@tonic-gate * file to not be found 3157c478bd9Sstevel@tonic-gate */ 3167c478bd9Sstevel@tonic-gate return_val = PTI_NOT_FOUND; 3177c478bd9Sstevel@tonic-gate 3187c478bd9Sstevel@tonic-gate default: /* Do nothing we have a good file */ 3197c478bd9Sstevel@tonic-gate break; 3207c478bd9Sstevel@tonic-gate } 3217c478bd9Sstevel@tonic-gate 322986fd29aSsetje kobj_close_file(file); 3237c478bd9Sstevel@tonic-gate return (return_val); 3247c478bd9Sstevel@tonic-gate } 3257c478bd9Sstevel@tonic-gate 3267c478bd9Sstevel@tonic-gate int 3277c478bd9Sstevel@tonic-gate is_pseudo_device(dev_info_t *dip) 3287c478bd9Sstevel@tonic-gate { 3297c478bd9Sstevel@tonic-gate dev_info_t *pdip; 3307c478bd9Sstevel@tonic-gate 3317c478bd9Sstevel@tonic-gate for (pdip = ddi_get_parent(dip); pdip && pdip != ddi_root_node(); 3327c478bd9Sstevel@tonic-gate pdip = ddi_get_parent(pdip)) { 3337c478bd9Sstevel@tonic-gate if (strcmp(ddi_get_name(pdip), DEVI_PSEUDO_NEXNAME) == 0) 3347c478bd9Sstevel@tonic-gate return (1); 3357c478bd9Sstevel@tonic-gate } 3367c478bd9Sstevel@tonic-gate return (0); 3377c478bd9Sstevel@tonic-gate } 3387c478bd9Sstevel@tonic-gate 3397c478bd9Sstevel@tonic-gate 3407c478bd9Sstevel@tonic-gate static void 3417c478bd9Sstevel@tonic-gate in_set_instance(dev_info_t *dip, in_drv_t *dp, major_t major) 3427c478bd9Sstevel@tonic-gate { 3437c478bd9Sstevel@tonic-gate /* use preassigned instance if available */ 3447c478bd9Sstevel@tonic-gate if (DEVI(dip)->devi_instance != -1) 3457c478bd9Sstevel@tonic-gate dp->ind_instance = DEVI(dip)->devi_instance; 3467c478bd9Sstevel@tonic-gate else 3477c478bd9Sstevel@tonic-gate dp->ind_instance = in_next_instance(major); 3487c478bd9Sstevel@tonic-gate } 3497c478bd9Sstevel@tonic-gate 3507c478bd9Sstevel@tonic-gate /* 35116bd7258Scth * Return 1 if instance block was assigned for the path. 35216bd7258Scth * 35316bd7258Scth * For multi-port NIC cards, sequential instance assignment across all 35462a24de0SChris Horne * ports on a card is highly desirable since the ppa is typically the 35516bd7258Scth * same as the instance number, and the ppa is used in the NIC's public 35616bd7258Scth * /dev name. This sequential assignment typically occurs as a result 35716bd7258Scth * of in_preassign_instance() after initial install, or by 35816bd7258Scth * i_ndi_init_hw_children() for NIC ports that share a common parent. 35916bd7258Scth * 36016bd7258Scth * Some NIC cards however use multi-function bridge chips, and to 36116bd7258Scth * support sequential instance assignment accross all ports, without 36216bd7258Scth * disabling multi-threaded attach, we have a (currently) undocumented 36316bd7258Scth * hack to allocate instance numbers in contiguous blocks based on 36416bd7258Scth * driver.conf properties. 36516bd7258Scth * 36616bd7258Scth * ^ 36716bd7258Scth * /---------- ------------\ 36816bd7258Scth * pci@0 pci@0,1 MULTI-FUNCTION BRIDGE CHIP 36916bd7258Scth * / \ / \ 37016bd7258Scth * FJSV,e4ta@4 FJSV,e4ta@4,1 FJSV,e4ta@6 FJSV,e4ta@6,1 NIC PORTS 37116bd7258Scth * n n+2 n+2 n+3 INSTANCE 37216bd7258Scth * 37316bd7258Scth * For the above example, the following driver.conf properties would be 37416bd7258Scth * used to guarantee sequential instance number assignment. 37516bd7258Scth * 37616bd7258Scth * ddi-instance-blocks ="ib-FJSVe4ca", "ib-FJSVe4ta", "ib-generic"; 37716bd7258Scth * ib-FJSVe4ca = "/pci@0/FJSV,e4ca@4", "/pci@0/FJSV,e4ca@4,1", 37816bd7258Scth * "/pci@0,1/FJSV,e4ca@6", "/pci@0,1/FJSV,e4ca@6,1"; 37916bd7258Scth * ib-FJSVe4ta = "/pci@0/FJSV,e4ta@4", "/pci@0/FJSV,e4ta@4,1", 38016bd7258Scth * "/pci@0,1/FJSV,e4ta@6", "/pci@0,1/FJSV,e4ta@6,1"; 38116bd7258Scth * ib-generic = "/pci@0/network@4", "/pci@0/network@4,1", 38216bd7258Scth * "/pci@0,1/network@6", "/pci@0,1/network@6,1"; 38316bd7258Scth * 38416bd7258Scth * The value of the 'ddi-instance-blocks' property references a series 38516bd7258Scth * of card specific properties, like 'ib-FJSV-e4ta', who's value 38616bd7258Scth * defines a single 'instance block'. The 'instance block' describes 38716bd7258Scth * all the paths below a multi-function bridge, where each path is 38816bd7258Scth * called an 'instance path'. The 'instance block' property value is a 38916bd7258Scth * series of 'instance paths'. The number of 'instance paths' in an 39016bd7258Scth * 'instance block' defines the size of the instance block, and the 39116bd7258Scth * ordering of the 'instance paths' defines the instance number 39216bd7258Scth * assignment order for paths going through the 'instance block'. 39316bd7258Scth * 39416bd7258Scth * In the instance assignment code below, if a (path, driver) that 39516bd7258Scth * currently has no instance number has a path that goes through an 39616bd7258Scth * 'instance block', then block instance number allocation occurs. The 39716bd7258Scth * block allocation code will find a sequential set of unused instance 39816bd7258Scth * numbers, and assign instance numbers for all the paths in the 39916bd7258Scth * 'instance block'. Each path is assigned a persistent instance 40016bd7258Scth * number, even paths that don't exist in the device tree or fail 40116bd7258Scth * probe(9E). 40216bd7258Scth */ 40316bd7258Scth static int 40416bd7258Scth in_assign_instance_block(dev_info_t *dip) 40516bd7258Scth { 40616bd7258Scth char **ibn; /* instance block names */ 40716bd7258Scth uint_t nibn; /* number of instance block names */ 40816bd7258Scth uint_t ibni; /* ibn index */ 40916bd7258Scth char *driver; 41016bd7258Scth major_t major; 41116bd7258Scth char *path; 41216bd7258Scth char *addr; 41316bd7258Scth int plen; 41416bd7258Scth char **ibp; /* instance block paths */ 41516bd7258Scth uint_t nibp; /* number of paths in instance block */ 41616bd7258Scth uint_t ibpi; /* ibp index */ 41716bd7258Scth int ibplen; /* length of instance block path */ 41816bd7258Scth char *ipath; 41916bd7258Scth int instance_base; 42016bd7258Scth int splice; 42116bd7258Scth int i; 42216bd7258Scth 42316bd7258Scth /* check for fresh install case (in miniroot) */ 42416bd7258Scth if (DEVI(dip)->devi_instance != -1) 42516bd7258Scth return (0); /* already assigned */ 42616bd7258Scth 42716bd7258Scth /* 42816bd7258Scth * Check to see if we need to allocate a block of contiguous instance 42916bd7258Scth * numbers by looking for the 'ddi-instance-blocks' property. 43016bd7258Scth */ 43116bd7258Scth if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, 43216bd7258Scth "ddi-instance-blocks", &ibn, &nibn) != DDI_SUCCESS) 43316bd7258Scth return (0); /* no instance block needed */ 43416bd7258Scth 43516bd7258Scth /* 43616bd7258Scth * Get information out about node we are processing. 43716bd7258Scth * 43816bd7258Scth * NOTE: Since the node is not yet at DS_INITIALIZED, ddi_pathname() 43916bd7258Scth * will not return the unit-address of the final path component even 44016bd7258Scth * though the node has an established devi_addr unit-address - so we 44116bd7258Scth * need to add the unit-address by hand. 44216bd7258Scth */ 44316bd7258Scth driver = (char *)ddi_driver_name(dip); 44416bd7258Scth major = ddi_driver_major(dip); 44516bd7258Scth path = kmem_alloc(MAXPATHLEN, KM_SLEEP); 44616bd7258Scth (void) ddi_pathname(dip, path); 44716bd7258Scth if ((addr = ddi_get_name_addr(dip)) != NULL) { 44816bd7258Scth (void) strcat(path, "@"); 44916bd7258Scth (void) strcat(path, addr); 45016bd7258Scth } 45116bd7258Scth plen = strlen(path); 45216bd7258Scth 45316bd7258Scth /* loop through instance block names */ 45416bd7258Scth for (ibni = 0; ibni < nibn; ibni++) { 45516bd7258Scth if (ibn[ibni] == NULL) 45616bd7258Scth continue; 45716bd7258Scth 45816bd7258Scth /* lookup instance block */ 45916bd7258Scth if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, dip, 46016bd7258Scth DDI_PROP_DONTPASS, ibn[ibni], 46116bd7258Scth &ibp, &nibp) != DDI_SUCCESS) { 46216bd7258Scth cmn_err(CE_WARN, 46316bd7258Scth "no devinition for instance block '%s' in %s.conf", 46416bd7258Scth ibn[ibni], driver); 46516bd7258Scth continue; 46616bd7258Scth } 46716bd7258Scth 46816bd7258Scth /* Does 'path' go through this instance block? */ 46916bd7258Scth for (ibpi = 0; ibpi < nibp; ibpi++) { 47016bd7258Scth if (ibp[ibpi] == NULL) 47116bd7258Scth continue; 47216bd7258Scth ibplen = strlen(ibp[ibpi]); 47316bd7258Scth if ((ibplen <= plen) && 47416bd7258Scth (strcmp(ibp[ibpi], path + plen - ibplen) == 0)) 47516bd7258Scth break; 47616bd7258Scth 47716bd7258Scth } 47816bd7258Scth if (ibpi >= nibp) { 47916bd7258Scth ddi_prop_free(ibp); 48016bd7258Scth continue; /* no try next instance block */ 48116bd7258Scth } 48216bd7258Scth 48316bd7258Scth /* yes, allocate and assign instances for all paths in block */ 48416bd7258Scth 48516bd7258Scth /* 48616bd7258Scth * determine where we splice in instance paths and verify 48716bd7258Scth * that none of the paths are too long. 48816bd7258Scth */ 48916bd7258Scth splice = plen - ibplen; 49016bd7258Scth for (i = 0; i < nibp; i++) { 49116bd7258Scth if ((splice + strlen(ibp[i])+ 1) >= MAXPATHLEN) { 49216bd7258Scth cmn_err(CE_WARN, 49316bd7258Scth "path %d through instance block '%s' from " 49416bd7258Scth "%s.conf too long", i, ibn[ibni], driver); 49516bd7258Scth break; 49616bd7258Scth } 49716bd7258Scth } 49816bd7258Scth if (i < nibp) { 49916bd7258Scth ddi_prop_free(ibp); 50016bd7258Scth continue; /* too long */ 50116bd7258Scth } 50216bd7258Scth 50316bd7258Scth /* allocate the instance block - no more failures */ 50416bd7258Scth instance_base = in_next_instance_block(major, nibp); 50516bd7258Scth 50616bd7258Scth ipath = kmem_alloc(MAXPATHLEN, KM_SLEEP); 50716bd7258Scth for (ibpi = 0; ibpi < nibp; ibpi++) { 50816bd7258Scth if (ibp[ibpi] == NULL) 50916bd7258Scth continue; 51016bd7258Scth (void) strcpy(ipath, path); 51116bd7258Scth (void) strcpy(ipath + splice, ibp[ibpi]); 51216bd7258Scth (void) in_pathin(ipath, 51316bd7258Scth instance_base + ibpi, driver, NULL); 51416bd7258Scth } 51516bd7258Scth 51616bd7258Scth /* free allocations */ 51716bd7258Scth kmem_free(ipath, MAXPATHLEN); 51816bd7258Scth ddi_prop_free(ibp); 51916bd7258Scth kmem_free(path, MAXPATHLEN); 52016bd7258Scth ddi_prop_free(ibn); 52116bd7258Scth 52216bd7258Scth /* notify devfsadmd to sync of path_to_inst file */ 52316bd7258Scth mutex_enter(&e_ddi_inst_state.ins_serial); 52416bd7258Scth i_log_devfs_instance_mod(); 52594c894bbSVikram Hegde e_ddi_inst_state.ins_dirty = B_TRUE; 52616bd7258Scth mutex_exit(&e_ddi_inst_state.ins_serial); 52716bd7258Scth return (1); 52816bd7258Scth } 52916bd7258Scth 53016bd7258Scth /* our path did not go through any of of the instance blocks */ 53116bd7258Scth kmem_free(path, MAXPATHLEN); 53216bd7258Scth ddi_prop_free(ibn); 53316bd7258Scth return (0); 53416bd7258Scth } 53516bd7258Scth 53616bd7258Scth /* 5377c478bd9Sstevel@tonic-gate * Look up an instance number for a dev_info node, and assign one if it does 5387c478bd9Sstevel@tonic-gate * not have one (the dev_info node has devi_name and devi_addr already set). 5397c478bd9Sstevel@tonic-gate */ 5407c478bd9Sstevel@tonic-gate uint_t 5417c478bd9Sstevel@tonic-gate e_ddi_assign_instance(dev_info_t *dip) 5427c478bd9Sstevel@tonic-gate { 5437c478bd9Sstevel@tonic-gate char *name; 5447c478bd9Sstevel@tonic-gate in_node_t *ap, *np; 5457c478bd9Sstevel@tonic-gate in_drv_t *dp; 5467c478bd9Sstevel@tonic-gate major_t major; 5477c478bd9Sstevel@tonic-gate uint_t ret; 5487c478bd9Sstevel@tonic-gate char *bname; 5497c478bd9Sstevel@tonic-gate 5507c478bd9Sstevel@tonic-gate /* 5517c478bd9Sstevel@tonic-gate * Allow implementation to override 5527c478bd9Sstevel@tonic-gate */ 5537c478bd9Sstevel@tonic-gate if ((ret = impl_assign_instance(dip)) != (uint_t)-1) 5547c478bd9Sstevel@tonic-gate return (ret); 5557c478bd9Sstevel@tonic-gate 5567c478bd9Sstevel@tonic-gate /* 5577c478bd9Sstevel@tonic-gate * If this is a pseudo-device, use the instance number 5587c478bd9Sstevel@tonic-gate * assigned by the pseudo nexus driver. The mutex is 5597c478bd9Sstevel@tonic-gate * not needed since the instance tree is not used. 5607c478bd9Sstevel@tonic-gate */ 5617c478bd9Sstevel@tonic-gate if (is_pseudo_device(dip)) { 5627c478bd9Sstevel@tonic-gate return (ddi_get_instance(dip)); 5637c478bd9Sstevel@tonic-gate } 5647c478bd9Sstevel@tonic-gate 5657c478bd9Sstevel@tonic-gate /* 5667c478bd9Sstevel@tonic-gate * Only one thread is allowed to change the state of the instance 5677c478bd9Sstevel@tonic-gate * number assignments on the system at any given time. 5687c478bd9Sstevel@tonic-gate */ 5697c478bd9Sstevel@tonic-gate e_ddi_enter_instance(); 5707c478bd9Sstevel@tonic-gate 5717c478bd9Sstevel@tonic-gate /* 5727c478bd9Sstevel@tonic-gate * Look for instance node, allocate one if not found 5737c478bd9Sstevel@tonic-gate */ 5747c478bd9Sstevel@tonic-gate np = in_devwalk(dip, &ap, NULL); 5757c478bd9Sstevel@tonic-gate if (np == NULL) { 57616bd7258Scth if (in_assign_instance_block(dip)) { 57716bd7258Scth np = in_devwalk(dip, &ap, NULL); 57816bd7258Scth } else { 5797c478bd9Sstevel@tonic-gate name = ddi_node_name(dip); 5807c478bd9Sstevel@tonic-gate np = in_alloc_node(name, ddi_get_name_addr(dip)); 5817c478bd9Sstevel@tonic-gate ASSERT(np != NULL); 5827c478bd9Sstevel@tonic-gate in_enlist(ap, np); /* insert into tree */ 5837c478bd9Sstevel@tonic-gate } 58416bd7258Scth } 5857c478bd9Sstevel@tonic-gate ASSERT(np == in_devwalk(dip, &ap, NULL)); 5867c478bd9Sstevel@tonic-gate 5877c478bd9Sstevel@tonic-gate /* 58894c894bbSVikram Hegde * Link the devinfo node and in_node_t 58994c894bbSVikram Hegde */ 59094c894bbSVikram Hegde if (DEVI(dip)->devi_in_node || np->in_devi) { 59194c894bbSVikram Hegde ddi_err(DER_MODE, dip, "devinfo and instance node (%p) " 59294c894bbSVikram Hegde "interlink fields are not NULL", (void *)np); 59394c894bbSVikram Hegde } 59494c894bbSVikram Hegde DEVI(dip)->devi_in_node = np; 59594c894bbSVikram Hegde np->in_devi = dip; 59694c894bbSVikram Hegde 59794c894bbSVikram Hegde /* 5987c478bd9Sstevel@tonic-gate * Look for driver entry, allocate one if not found 5997c478bd9Sstevel@tonic-gate */ 6007c478bd9Sstevel@tonic-gate bname = (char *)ddi_driver_name(dip); 6017c478bd9Sstevel@tonic-gate dp = in_drvwalk(np, bname); 6027c478bd9Sstevel@tonic-gate if (dp == NULL) { 60394c894bbSVikram Hegde 60494c894bbSVikram Hegde if (ddi_aliases_present == B_TRUE) { 60594c894bbSVikram Hegde e_ddi_borrow_instance(dip, np); 60694c894bbSVikram Hegde } 60794c894bbSVikram Hegde 60894c894bbSVikram Hegde if ((dp = in_drvwalk(np, bname)) == NULL) { 6097c478bd9Sstevel@tonic-gate dp = in_alloc_drv(bname); 6107c478bd9Sstevel@tonic-gate ASSERT(dp != NULL); 6117c478bd9Sstevel@tonic-gate major = ddi_driver_major(dip); 612a204de77Scth ASSERT(major != DDI_MAJOR_T_NONE); 6137c478bd9Sstevel@tonic-gate in_endrv(np, dp); 6147c478bd9Sstevel@tonic-gate in_set_instance(dip, dp, major); 6157c478bd9Sstevel@tonic-gate dp->ind_state = IN_PROVISIONAL; 6167c478bd9Sstevel@tonic-gate in_hashdrv(dp); 61794c894bbSVikram Hegde } else { 61894c894bbSVikram Hegde dp->ind_state = IN_BORROWED; 61994c894bbSVikram Hegde } 6207c478bd9Sstevel@tonic-gate } 6217c478bd9Sstevel@tonic-gate 6227c478bd9Sstevel@tonic-gate ret = dp->ind_instance; 6237c478bd9Sstevel@tonic-gate 6247c478bd9Sstevel@tonic-gate e_ddi_exit_instance(); 6257c478bd9Sstevel@tonic-gate return (ret); 6267c478bd9Sstevel@tonic-gate } 6277c478bd9Sstevel@tonic-gate 6287c478bd9Sstevel@tonic-gate static int 6297c478bd9Sstevel@tonic-gate mkpathname(char *path, in_node_t *np, int len) 6307c478bd9Sstevel@tonic-gate { 6317c478bd9Sstevel@tonic-gate int len_needed; 6327c478bd9Sstevel@tonic-gate 6337c478bd9Sstevel@tonic-gate if (np == e_ddi_inst_state.ins_root) 6347c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 6357c478bd9Sstevel@tonic-gate 6367c478bd9Sstevel@tonic-gate if (mkpathname(path, np->in_parent, len) == DDI_FAILURE) 6377c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 6387c478bd9Sstevel@tonic-gate 6397c478bd9Sstevel@tonic-gate len_needed = strlen(path); 6407c478bd9Sstevel@tonic-gate len_needed += strlen(np->in_node_name) + 1; /* for '/' */ 6417c478bd9Sstevel@tonic-gate if (np->in_unit_addr) { 6427c478bd9Sstevel@tonic-gate len_needed += strlen(np->in_unit_addr) + 1; /* for '@' */ 6437c478bd9Sstevel@tonic-gate } 6447c478bd9Sstevel@tonic-gate len_needed += 1; /* for '\0' */ 6457c478bd9Sstevel@tonic-gate 6467c478bd9Sstevel@tonic-gate /* 6477c478bd9Sstevel@tonic-gate * XX complain 6487c478bd9Sstevel@tonic-gate */ 6497c478bd9Sstevel@tonic-gate if (len_needed > len) 6507c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 6517c478bd9Sstevel@tonic-gate 6527c478bd9Sstevel@tonic-gate if (np->in_unit_addr[0] == '\0') 6537c478bd9Sstevel@tonic-gate (void) sprintf(path+strlen(path), "/%s", np->in_node_name); 6547c478bd9Sstevel@tonic-gate else 6557c478bd9Sstevel@tonic-gate (void) sprintf(path+strlen(path), "/%s@%s", np->in_node_name, 6567c478bd9Sstevel@tonic-gate np->in_unit_addr); 6577c478bd9Sstevel@tonic-gate 6587c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 6597c478bd9Sstevel@tonic-gate } 6607c478bd9Sstevel@tonic-gate 6617c478bd9Sstevel@tonic-gate /* 6627c478bd9Sstevel@tonic-gate * produce the path to the given instance of a major number. 6637c478bd9Sstevel@tonic-gate * path must hold MAXPATHLEN string 6647c478bd9Sstevel@tonic-gate */ 6657c478bd9Sstevel@tonic-gate int 6667c478bd9Sstevel@tonic-gate e_ddi_instance_majorinstance_to_path(major_t major, uint_t inst, char *path) 6677c478bd9Sstevel@tonic-gate { 6687c478bd9Sstevel@tonic-gate struct devnames *dnp; 6697c478bd9Sstevel@tonic-gate in_drv_t *dp; 6707c478bd9Sstevel@tonic-gate int ret; 6717c478bd9Sstevel@tonic-gate 6727c478bd9Sstevel@tonic-gate e_ddi_enter_instance(); 6737c478bd9Sstevel@tonic-gate 6747c478bd9Sstevel@tonic-gate /* look for the instance threaded off major */ 6757c478bd9Sstevel@tonic-gate dnp = &devnamesp[major]; 6767c478bd9Sstevel@tonic-gate for (dp = dnp->dn_inlist; dp != NULL; dp = dp->ind_next) 6777c478bd9Sstevel@tonic-gate if (dp->ind_instance == inst) 6787c478bd9Sstevel@tonic-gate break; 6797c478bd9Sstevel@tonic-gate 6807c478bd9Sstevel@tonic-gate /* produce path from the node that uses the instance */ 6817c478bd9Sstevel@tonic-gate if (dp) { 6827c478bd9Sstevel@tonic-gate *path = 0; 6837c478bd9Sstevel@tonic-gate ret = mkpathname(path, dp->ind_node, MAXPATHLEN); 6847c478bd9Sstevel@tonic-gate } else 6857c478bd9Sstevel@tonic-gate ret = DDI_FAILURE; 6867c478bd9Sstevel@tonic-gate 6877c478bd9Sstevel@tonic-gate e_ddi_exit_instance(); 6887c478bd9Sstevel@tonic-gate return (ret); 6897c478bd9Sstevel@tonic-gate } 6907c478bd9Sstevel@tonic-gate 6917c478bd9Sstevel@tonic-gate /* 69216bd7258Scth * Allocate a sequential block of instance numbers for the specified driver, 69316bd7258Scth * and return the base instance number of the block. The implementation 69416bd7258Scth * depends on the list being sorted in ascending instance number sequence. 69516bd7258Scth * When there are no 'holes' in the allocation sequence, dn_instance is the 69616bd7258Scth * next available instance number. When dn_instance is IN_SEARCHME, hole(s) 69716bd7258Scth * exists and a slower code path executes which tries to fill holes. 698328d222bSChris Horne * 699328d222bSChris Horne * The block returned can't be in the preassigned range. 7007c478bd9Sstevel@tonic-gate */ 7017c478bd9Sstevel@tonic-gate static int 70216bd7258Scth in_next_instance_block(major_t major, int block_size) 7037c478bd9Sstevel@tonic-gate { 704328d222bSChris Horne int prev; 7057c478bd9Sstevel@tonic-gate struct devnames *dnp; 7067c478bd9Sstevel@tonic-gate in_drv_t *dp; 70716bd7258Scth int base; 70816bd7258Scth int hole; 7097c478bd9Sstevel@tonic-gate 7107c478bd9Sstevel@tonic-gate dnp = &devnamesp[major]; 711a204de77Scth ASSERT(major != DDI_MAJOR_T_NONE); 7127c478bd9Sstevel@tonic-gate ASSERT(e_ddi_inst_state.ins_busy); 71316bd7258Scth ASSERT(block_size); 71416bd7258Scth 71516bd7258Scth /* check to see if we can do a quick allocation */ 716328d222bSChris Horne if (!instance_searchme && (dnp->dn_instance != IN_SEARCHME)) { 71716bd7258Scth base = dnp->dn_instance; 71816bd7258Scth dnp->dn_instance += block_size; 71916bd7258Scth return (base); 72016bd7258Scth } 721328d222bSChris Horne 722c3408929SChris Horne /* 723c3408929SChris Horne * Use more complex code path, start by skipping preassign entries. 724c3408929SChris Horne */ 725c3408929SChris Horne for (dp = dnp->dn_inlist; dp; dp = dp->ind_next) 726c3408929SChris Horne if (dp->ind_instance >= dnp->dn_pinstance) 727c3408929SChris Horne break; /* beyond preassign */ 7287c478bd9Sstevel@tonic-gate 729c3408929SChris Horne /* No non-preassign entries, allocate block at preassign base. */ 7307c478bd9Sstevel@tonic-gate if (dp == NULL) { 731328d222bSChris Horne base = dnp->dn_pinstance; 732c3408929SChris Horne if (base == 0) 733c3408929SChris Horne dnp->dn_instance = block_size; 734328d222bSChris Horne return (base); 7357c478bd9Sstevel@tonic-gate } 7367c478bd9Sstevel@tonic-gate 737c3408929SChris Horne /* See if we fit in hole at beginning (after preassigns) */ 7387c478bd9Sstevel@tonic-gate prev = dp->ind_instance; 739328d222bSChris Horne if ((prev - dnp->dn_pinstance) >= block_size) 740328d222bSChris Horne return (dnp->dn_pinstance); /* we fit in beginning hole */ 7417c478bd9Sstevel@tonic-gate 74216bd7258Scth /* search the list for a large enough hole */ 74316bd7258Scth for (dp = dp->ind_next, hole = 0; dp; dp = dp->ind_next) { 74416bd7258Scth if (dp->ind_instance != (prev + 1)) 74516bd7258Scth hole++; /* we have a hole */ 74616bd7258Scth if (dp->ind_instance >= (prev + block_size + 1)) 74716bd7258Scth break; /* we fit in hole */ 74816bd7258Scth prev = dp->ind_instance; 74916bd7258Scth } 75016bd7258Scth 75116bd7258Scth /* 75216bd7258Scth * If hole is zero then all holes are patched and we can resume 753c3408929SChris Horne * quick allocations, but don't resume quick allocation if there is 754c3408929SChris Horne * a preassign. 75516bd7258Scth */ 756c3408929SChris Horne if ((hole == 0) && (dnp->dn_pinstance == 0)) 75716bd7258Scth dnp->dn_instance = prev + 1 + block_size; 75816bd7258Scth 75916bd7258Scth return (prev + 1); 76016bd7258Scth } 76116bd7258Scth 76216bd7258Scth /* assign instance block of size 1 */ 76316bd7258Scth static int 76416bd7258Scth in_next_instance(major_t major) 76516bd7258Scth { 76616bd7258Scth return (in_next_instance_block(major, 1)); 7677c478bd9Sstevel@tonic-gate } 7687c478bd9Sstevel@tonic-gate 7697c478bd9Sstevel@tonic-gate /* 7707c478bd9Sstevel@tonic-gate * This call causes us to *forget* the instance number we've generated 7717c478bd9Sstevel@tonic-gate * for a given device if it was not permanent. 7727c478bd9Sstevel@tonic-gate */ 7737c478bd9Sstevel@tonic-gate void 7747c478bd9Sstevel@tonic-gate e_ddi_free_instance(dev_info_t *dip, char *addr) 7757c478bd9Sstevel@tonic-gate { 7767c478bd9Sstevel@tonic-gate char *name; 7777c478bd9Sstevel@tonic-gate in_node_t *np; 7787c478bd9Sstevel@tonic-gate in_node_t *ap; /* ancestor node */ 7797c478bd9Sstevel@tonic-gate major_t major; 7807c478bd9Sstevel@tonic-gate struct devnames *dnp; 7817c478bd9Sstevel@tonic-gate in_drv_t *dp; /* in_drv entry */ 7827c478bd9Sstevel@tonic-gate 7837c478bd9Sstevel@tonic-gate /* 7847c478bd9Sstevel@tonic-gate * Allow implementation override 7857c478bd9Sstevel@tonic-gate */ 7867c478bd9Sstevel@tonic-gate if (impl_free_instance(dip) == DDI_SUCCESS) 7877c478bd9Sstevel@tonic-gate return; 7887c478bd9Sstevel@tonic-gate 7897c478bd9Sstevel@tonic-gate /* 7907c478bd9Sstevel@tonic-gate * If this is a pseudo-device, no instance number 7917c478bd9Sstevel@tonic-gate * was assigned. 7927c478bd9Sstevel@tonic-gate */ 7937c478bd9Sstevel@tonic-gate if (is_pseudo_device(dip)) { 7947c478bd9Sstevel@tonic-gate return; 7957c478bd9Sstevel@tonic-gate } 7967c478bd9Sstevel@tonic-gate 7977c478bd9Sstevel@tonic-gate name = (char *)ddi_driver_name(dip); 7987c478bd9Sstevel@tonic-gate major = ddi_driver_major(dip); 799a204de77Scth ASSERT(major != DDI_MAJOR_T_NONE); 8007c478bd9Sstevel@tonic-gate dnp = &devnamesp[major]; 8017c478bd9Sstevel@tonic-gate /* 8027c478bd9Sstevel@tonic-gate * Only one thread is allowed to change the state of the instance 8037c478bd9Sstevel@tonic-gate * number assignments on the system at any given time. 8047c478bd9Sstevel@tonic-gate */ 8057c478bd9Sstevel@tonic-gate e_ddi_enter_instance(); 8067c478bd9Sstevel@tonic-gate np = in_devwalk(dip, &ap, addr); 8077c478bd9Sstevel@tonic-gate ASSERT(np); 80894c894bbSVikram Hegde 80994c894bbSVikram Hegde /* 81094c894bbSVikram Hegde * Break the interlink between dip and np 81194c894bbSVikram Hegde */ 81294c894bbSVikram Hegde if (DEVI(dip)->devi_in_node != np || np->in_devi != dip) { 81394c894bbSVikram Hegde ddi_err(DER_MODE, dip, "devinfo node linked to " 81494c894bbSVikram Hegde "wrong instance node: %p", (void *)np); 81594c894bbSVikram Hegde } 81694c894bbSVikram Hegde DEVI(dip)->devi_in_node = NULL; 81794c894bbSVikram Hegde np->in_devi = NULL; 81894c894bbSVikram Hegde 8197c478bd9Sstevel@tonic-gate dp = in_drvwalk(np, name); 8207c478bd9Sstevel@tonic-gate ASSERT(dp); 8217c478bd9Sstevel@tonic-gate if (dp->ind_state == IN_PROVISIONAL) { 8227c478bd9Sstevel@tonic-gate in_removedrv(dnp, dp); 823f1ebe3a2SVikram Hegde } else if (dp->ind_state == IN_BORROWED) { 82494c894bbSVikram Hegde dp->ind_state = IN_PERMANENT; 82594c894bbSVikram Hegde e_ddi_return_instance(dip, addr, np); 82694c894bbSVikram Hegde } 8277c478bd9Sstevel@tonic-gate if (np->in_drivers == NULL) { 8287c478bd9Sstevel@tonic-gate in_removenode(dnp, np, ap); 8297c478bd9Sstevel@tonic-gate } 8307c478bd9Sstevel@tonic-gate e_ddi_exit_instance(); 8317c478bd9Sstevel@tonic-gate } 8327c478bd9Sstevel@tonic-gate 8337c478bd9Sstevel@tonic-gate /* 8347c478bd9Sstevel@tonic-gate * This makes our memory of an instance assignment permanent 8357c478bd9Sstevel@tonic-gate */ 8367c478bd9Sstevel@tonic-gate void 8377c478bd9Sstevel@tonic-gate e_ddi_keep_instance(dev_info_t *dip) 8387c478bd9Sstevel@tonic-gate { 8397c478bd9Sstevel@tonic-gate in_node_t *np, *ap; 8407c478bd9Sstevel@tonic-gate in_drv_t *dp; 8417c478bd9Sstevel@tonic-gate 84262a24de0SChris Horne /* Don't make nulldriver instance assignments permanent */ 84362a24de0SChris Horne if (ddi_driver_major(dip) == nulldriver_major) 84462a24de0SChris Horne return; 84562a24de0SChris Horne 8467c478bd9Sstevel@tonic-gate /* 8477c478bd9Sstevel@tonic-gate * Allow implementation override 8487c478bd9Sstevel@tonic-gate */ 8497c478bd9Sstevel@tonic-gate if (impl_keep_instance(dip) == DDI_SUCCESS) 8507c478bd9Sstevel@tonic-gate return; 8517c478bd9Sstevel@tonic-gate 8527c478bd9Sstevel@tonic-gate /* 8537c478bd9Sstevel@tonic-gate * Nothing to do for pseudo devices. 8547c478bd9Sstevel@tonic-gate */ 8557c478bd9Sstevel@tonic-gate if (is_pseudo_device(dip)) 8567c478bd9Sstevel@tonic-gate return; 8577c478bd9Sstevel@tonic-gate 8587c478bd9Sstevel@tonic-gate /* 8597c478bd9Sstevel@tonic-gate * Only one thread is allowed to change the state of the instance 8607c478bd9Sstevel@tonic-gate * number assignments on the system at any given time. 8617c478bd9Sstevel@tonic-gate */ 8627c478bd9Sstevel@tonic-gate e_ddi_enter_instance(); 8637c478bd9Sstevel@tonic-gate np = in_devwalk(dip, &ap, NULL); 8647c478bd9Sstevel@tonic-gate ASSERT(np); 8657c478bd9Sstevel@tonic-gate dp = in_drvwalk(np, (char *)ddi_driver_name(dip)); 8667c478bd9Sstevel@tonic-gate ASSERT(dp); 8677c478bd9Sstevel@tonic-gate 8687c478bd9Sstevel@tonic-gate mutex_enter(&e_ddi_inst_state.ins_serial); 86994c894bbSVikram Hegde if (dp->ind_state == IN_PROVISIONAL || dp->ind_state == IN_BORROWED) { 8707c478bd9Sstevel@tonic-gate dp->ind_state = IN_PERMANENT; 8717c478bd9Sstevel@tonic-gate i_log_devfs_instance_mod(); 87294c894bbSVikram Hegde e_ddi_inst_state.ins_dirty = B_TRUE; 8737c478bd9Sstevel@tonic-gate } 8747c478bd9Sstevel@tonic-gate mutex_exit(&e_ddi_inst_state.ins_serial); 8757c478bd9Sstevel@tonic-gate e_ddi_exit_instance(); 8767c478bd9Sstevel@tonic-gate } 8777c478bd9Sstevel@tonic-gate 8787c478bd9Sstevel@tonic-gate /* 8797c478bd9Sstevel@tonic-gate * A new major has been added to the system. Run through the orphan list 8807c478bd9Sstevel@tonic-gate * and try to attach each one to a driver's list. 8817c478bd9Sstevel@tonic-gate */ 8827c478bd9Sstevel@tonic-gate void 8837c478bd9Sstevel@tonic-gate e_ddi_unorphan_instance_nos() 8847c478bd9Sstevel@tonic-gate { 8857c478bd9Sstevel@tonic-gate in_drv_t *dp, *ndp; 8867c478bd9Sstevel@tonic-gate 8877c478bd9Sstevel@tonic-gate /* 8887c478bd9Sstevel@tonic-gate * disconnect the orphan list, and call in_hashdrv for each item 8897c478bd9Sstevel@tonic-gate * on it 8907c478bd9Sstevel@tonic-gate */ 8917c478bd9Sstevel@tonic-gate 8927c478bd9Sstevel@tonic-gate /* 8937c478bd9Sstevel@tonic-gate * Only one thread is allowed to change the state of the instance 8947c478bd9Sstevel@tonic-gate * number assignments on the system at any given time. 8957c478bd9Sstevel@tonic-gate */ 8967c478bd9Sstevel@tonic-gate e_ddi_enter_instance(); 8977c478bd9Sstevel@tonic-gate if (e_ddi_inst_state.ins_no_major == NULL) { 8987c478bd9Sstevel@tonic-gate e_ddi_exit_instance(); 8997c478bd9Sstevel@tonic-gate return; 9007c478bd9Sstevel@tonic-gate } 9017c478bd9Sstevel@tonic-gate /* 9027c478bd9Sstevel@tonic-gate * Hash instance list to devnames structure of major. 9037c478bd9Sstevel@tonic-gate * Note that if there is not a valid major number for the 9047c478bd9Sstevel@tonic-gate * node, in_hashdrv will put it back on the no_major list. 9057c478bd9Sstevel@tonic-gate */ 9067c478bd9Sstevel@tonic-gate dp = e_ddi_inst_state.ins_no_major; 9077c478bd9Sstevel@tonic-gate e_ddi_inst_state.ins_no_major = NULL; 9087c478bd9Sstevel@tonic-gate while (dp) { 9097c478bd9Sstevel@tonic-gate ndp = dp->ind_next; 9107c478bd9Sstevel@tonic-gate ASSERT(dp->ind_state != IN_UNKNOWN); 9117c478bd9Sstevel@tonic-gate dp->ind_next = NULL; 9127c478bd9Sstevel@tonic-gate in_hashdrv(dp); 9137c478bd9Sstevel@tonic-gate dp = ndp; 9147c478bd9Sstevel@tonic-gate } 9157c478bd9Sstevel@tonic-gate e_ddi_exit_instance(); 9167c478bd9Sstevel@tonic-gate } 9177c478bd9Sstevel@tonic-gate 9187c478bd9Sstevel@tonic-gate static void 9197c478bd9Sstevel@tonic-gate in_removenode(struct devnames *dnp, in_node_t *mp, in_node_t *ap) 9207c478bd9Sstevel@tonic-gate { 9217c478bd9Sstevel@tonic-gate in_node_t *np; 9227c478bd9Sstevel@tonic-gate 9237c478bd9Sstevel@tonic-gate ASSERT(e_ddi_inst_state.ins_busy); 92494c894bbSVikram Hegde 9257c478bd9Sstevel@tonic-gate /* 9267c478bd9Sstevel@tonic-gate * Assertion: parents are always instantiated by the framework 9277c478bd9Sstevel@tonic-gate * before their children, destroyed after them 9287c478bd9Sstevel@tonic-gate */ 9297c478bd9Sstevel@tonic-gate ASSERT(mp->in_child == NULL); 9307c478bd9Sstevel@tonic-gate /* 9317c478bd9Sstevel@tonic-gate * Assertion: drv entries are always removed before their owning nodes 9327c478bd9Sstevel@tonic-gate */ 9337c478bd9Sstevel@tonic-gate ASSERT(mp->in_drivers == NULL); 9347c478bd9Sstevel@tonic-gate /* 9357c478bd9Sstevel@tonic-gate * Take the node out of the tree 9367c478bd9Sstevel@tonic-gate */ 9377c478bd9Sstevel@tonic-gate if (ap->in_child == mp) { 9387c478bd9Sstevel@tonic-gate ap->in_child = mp->in_sibling; 9397c478bd9Sstevel@tonic-gate in_dealloc_node(mp); 9407c478bd9Sstevel@tonic-gate return; 9417c478bd9Sstevel@tonic-gate } else { 9427c478bd9Sstevel@tonic-gate for (np = ap->in_child; np; np = np->in_sibling) { 9437c478bd9Sstevel@tonic-gate if (np->in_sibling == mp) { 9447c478bd9Sstevel@tonic-gate np->in_sibling = mp->in_sibling; 9457c478bd9Sstevel@tonic-gate in_dealloc_node(mp); 9467c478bd9Sstevel@tonic-gate return; 9477c478bd9Sstevel@tonic-gate } 9487c478bd9Sstevel@tonic-gate } 9497c478bd9Sstevel@tonic-gate } 9507c478bd9Sstevel@tonic-gate panic("in_removenode dnp %p mp %p", (void *)dnp, (void *)mp); 9517c478bd9Sstevel@tonic-gate } 9527c478bd9Sstevel@tonic-gate 9537c478bd9Sstevel@tonic-gate /* 9547c478bd9Sstevel@tonic-gate * Recursive ascent 9557c478bd9Sstevel@tonic-gate * 9567c478bd9Sstevel@tonic-gate * This now only does half the job. It finds the node, then the caller 9577c478bd9Sstevel@tonic-gate * has to search the node for the binding name 9587c478bd9Sstevel@tonic-gate */ 9597c478bd9Sstevel@tonic-gate static in_node_t * 9607c478bd9Sstevel@tonic-gate in_devwalk(dev_info_t *dip, in_node_t **ap, char *addr) 9617c478bd9Sstevel@tonic-gate { 9627c478bd9Sstevel@tonic-gate in_node_t *np; 9637c478bd9Sstevel@tonic-gate char *name; 9647c478bd9Sstevel@tonic-gate 9657c478bd9Sstevel@tonic-gate ASSERT(dip); 9667c478bd9Sstevel@tonic-gate ASSERT(e_ddi_inst_state.ins_busy); 9677c478bd9Sstevel@tonic-gate if (dip == ddi_root_node()) { 9687c478bd9Sstevel@tonic-gate *ap = NULL; 9697c478bd9Sstevel@tonic-gate return (e_ddi_inst_state.ins_root); 9707c478bd9Sstevel@tonic-gate } 9717c478bd9Sstevel@tonic-gate /* 9727c478bd9Sstevel@tonic-gate * call up to find parent, then look through the list of kids 9737c478bd9Sstevel@tonic-gate * for a match 9747c478bd9Sstevel@tonic-gate */ 9757c478bd9Sstevel@tonic-gate np = in_devwalk(ddi_get_parent(dip), ap, NULL); 9767c478bd9Sstevel@tonic-gate if (np == NULL) 9777c478bd9Sstevel@tonic-gate return (np); 9787c478bd9Sstevel@tonic-gate *ap = np; 9797c478bd9Sstevel@tonic-gate np = np->in_child; 9807c478bd9Sstevel@tonic-gate name = ddi_node_name(dip); 9817c478bd9Sstevel@tonic-gate if (addr == NULL) 9827c478bd9Sstevel@tonic-gate addr = ddi_get_name_addr(dip); 9837c478bd9Sstevel@tonic-gate 9847c478bd9Sstevel@tonic-gate while (np) { 9857c478bd9Sstevel@tonic-gate if (in_eqstr(np->in_node_name, name) && 9867c478bd9Sstevel@tonic-gate in_eqstr(np->in_unit_addr, addr)) { 9877c478bd9Sstevel@tonic-gate return (np); 9887c478bd9Sstevel@tonic-gate } 9897c478bd9Sstevel@tonic-gate np = np->in_sibling; 9907c478bd9Sstevel@tonic-gate } 99194c894bbSVikram Hegde 9927c478bd9Sstevel@tonic-gate return (np); 9937c478bd9Sstevel@tonic-gate } 9947c478bd9Sstevel@tonic-gate 9957c478bd9Sstevel@tonic-gate /* 9967c478bd9Sstevel@tonic-gate * Create a node specified by cp and assign it the given instance no. 9977c478bd9Sstevel@tonic-gate */ 9987c478bd9Sstevel@tonic-gate static int 9997c478bd9Sstevel@tonic-gate in_pathin(char *cp, int instance, char *bname, struct bind **args) 10007c478bd9Sstevel@tonic-gate { 10017c478bd9Sstevel@tonic-gate in_node_t *np; 10027c478bd9Sstevel@tonic-gate in_drv_t *dp; 10037c478bd9Sstevel@tonic-gate char *name; 10047c478bd9Sstevel@tonic-gate 10057c478bd9Sstevel@tonic-gate ASSERT(e_ddi_inst_state.ins_busy); 10067c478bd9Sstevel@tonic-gate ASSERT(args == NULL); 10077c478bd9Sstevel@tonic-gate 10087c478bd9Sstevel@tonic-gate /* 10097c478bd9Sstevel@tonic-gate * Give a warning to the console. 10107c478bd9Sstevel@tonic-gate * return value ignored 10117c478bd9Sstevel@tonic-gate */ 10127c478bd9Sstevel@tonic-gate if (cp[0] != '/' || instance == -1 || bname == NULL) { 10137c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, 10147c478bd9Sstevel@tonic-gate "invalid instance file entry %s %d", 10157c478bd9Sstevel@tonic-gate cp, instance); 10167c478bd9Sstevel@tonic-gate return (0); 10177c478bd9Sstevel@tonic-gate } 10187c478bd9Sstevel@tonic-gate 10197c478bd9Sstevel@tonic-gate if ((name = i_binding_to_drv_name(bname)) != NULL) 10207c478bd9Sstevel@tonic-gate bname = name; 10217c478bd9Sstevel@tonic-gate 10227c478bd9Sstevel@tonic-gate np = in_make_path(cp); 10237c478bd9Sstevel@tonic-gate ASSERT(np); 102494c894bbSVikram Hegde 10257c478bd9Sstevel@tonic-gate dp = in_drvwalk(np, bname); 10267c478bd9Sstevel@tonic-gate if (dp != NULL) { 10277c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, 10287c478bd9Sstevel@tonic-gate "multiple instance number assignments for " 10297c478bd9Sstevel@tonic-gate "'%s' (driver %s), %d used", 10307c478bd9Sstevel@tonic-gate cp, bname, dp->ind_instance); 10317c478bd9Sstevel@tonic-gate return (0); 10327c478bd9Sstevel@tonic-gate } 103394c894bbSVikram Hegde 103494c894bbSVikram Hegde if (in_inuse(instance, bname)) { 103594c894bbSVikram Hegde cmn_err(CE_WARN, 103694c894bbSVikram Hegde "instance already in use: %s %d", cp, instance); 103794c894bbSVikram Hegde return (0); 103894c894bbSVikram Hegde } 103994c894bbSVikram Hegde 10407c478bd9Sstevel@tonic-gate dp = in_alloc_drv(bname); 10417c478bd9Sstevel@tonic-gate in_endrv(np, dp); 10427c478bd9Sstevel@tonic-gate dp->ind_instance = instance; 10437c478bd9Sstevel@tonic-gate dp->ind_state = IN_PERMANENT; 10447c478bd9Sstevel@tonic-gate in_hashdrv(dp); 10457c478bd9Sstevel@tonic-gate 10467c478bd9Sstevel@tonic-gate return (0); 10477c478bd9Sstevel@tonic-gate } 10487c478bd9Sstevel@tonic-gate 10497c478bd9Sstevel@tonic-gate /* 10507c478bd9Sstevel@tonic-gate * Create (or find) the node named by path by recursively descending from the 10517c478bd9Sstevel@tonic-gate * root's first child (we ignore the root, which is never named) 10527c478bd9Sstevel@tonic-gate */ 10537c478bd9Sstevel@tonic-gate static in_node_t * 10547c478bd9Sstevel@tonic-gate in_make_path(char *path) 10557c478bd9Sstevel@tonic-gate { 10567c478bd9Sstevel@tonic-gate in_node_t *ap; /* ancestor pointer */ 10577c478bd9Sstevel@tonic-gate in_node_t *np; /* working node pointer */ 10587c478bd9Sstevel@tonic-gate in_node_t *rp; /* return node pointer */ 10597c478bd9Sstevel@tonic-gate char buf[MAXPATHLEN]; /* copy of string so we can change it */ 10607c478bd9Sstevel@tonic-gate char *cp, *name, *addr; 10617c478bd9Sstevel@tonic-gate 10627c478bd9Sstevel@tonic-gate ASSERT(e_ddi_inst_state.ins_busy); 106394c894bbSVikram Hegde 10647c478bd9Sstevel@tonic-gate if (path == NULL || path[0] != '/') 10657c478bd9Sstevel@tonic-gate return (NULL); 106694c894bbSVikram Hegde 10677c478bd9Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), "%s", path); 10687c478bd9Sstevel@tonic-gate cp = buf + 1; /* skip over initial '/' in path */ 10697c478bd9Sstevel@tonic-gate name = in_name_addr(&cp, &addr); 10707c478bd9Sstevel@tonic-gate 10717c478bd9Sstevel@tonic-gate /* 10727c478bd9Sstevel@tonic-gate * In S9 and earlier releases, the path_to_inst file 10737c478bd9Sstevel@tonic-gate * SunCluster was prepended with "/node@#". This was 10747c478bd9Sstevel@tonic-gate * removed in S10. We skip the prefix if the prefix 10757c478bd9Sstevel@tonic-gate * still exists in /etc/path_to_inst. It is needed for 10767c478bd9Sstevel@tonic-gate * various forms of Solaris upgrade to work properly 10777c478bd9Sstevel@tonic-gate * in the SunCluster environment. 10787c478bd9Sstevel@tonic-gate */ 10797c478bd9Sstevel@tonic-gate if ((cluster_bootflags & CLUSTER_CONFIGURED) && 10807c478bd9Sstevel@tonic-gate (strcmp(name, "node") == 0)) 10817c478bd9Sstevel@tonic-gate name = in_name_addr(&cp, &addr); 10827c478bd9Sstevel@tonic-gate 10837c478bd9Sstevel@tonic-gate ap = e_ddi_inst_state.ins_root; 108494c894bbSVikram Hegde np = e_ddi_inst_state.ins_root->in_child; 108594c894bbSVikram Hegde rp = np; 10867c478bd9Sstevel@tonic-gate while (name) { 10877c478bd9Sstevel@tonic-gate while (name && np) { 10887c478bd9Sstevel@tonic-gate if (in_eqstr(name, np->in_node_name) && 10897c478bd9Sstevel@tonic-gate in_eqstr(addr, np->in_unit_addr)) { 10907c478bd9Sstevel@tonic-gate name = in_name_addr(&cp, &addr); 10917c478bd9Sstevel@tonic-gate if (name == NULL) 10927c478bd9Sstevel@tonic-gate return (np); 10937c478bd9Sstevel@tonic-gate ap = np; 10947c478bd9Sstevel@tonic-gate np = np->in_child; 10957c478bd9Sstevel@tonic-gate } else { 10967c478bd9Sstevel@tonic-gate np = np->in_sibling; 10977c478bd9Sstevel@tonic-gate } 10987c478bd9Sstevel@tonic-gate } 10997c478bd9Sstevel@tonic-gate np = in_alloc_node(name, addr); 11007c478bd9Sstevel@tonic-gate in_enlist(ap, np); /* insert into tree */ 11017c478bd9Sstevel@tonic-gate rp = np; /* value to return if we quit */ 11027c478bd9Sstevel@tonic-gate ap = np; /* new parent */ 11037c478bd9Sstevel@tonic-gate np = NULL; /* can have no children */ 11047c478bd9Sstevel@tonic-gate name = in_name_addr(&cp, &addr); 11057c478bd9Sstevel@tonic-gate } 110694c894bbSVikram Hegde 11077c478bd9Sstevel@tonic-gate return (rp); 11087c478bd9Sstevel@tonic-gate } 11097c478bd9Sstevel@tonic-gate 11107c478bd9Sstevel@tonic-gate /* 11117c478bd9Sstevel@tonic-gate * Insert node np into the tree as one of ap's children. 11127c478bd9Sstevel@tonic-gate */ 11137c478bd9Sstevel@tonic-gate static void 11147c478bd9Sstevel@tonic-gate in_enlist(in_node_t *ap, in_node_t *np) 11157c478bd9Sstevel@tonic-gate { 11167c478bd9Sstevel@tonic-gate in_node_t *mp; 11177c478bd9Sstevel@tonic-gate ASSERT(e_ddi_inst_state.ins_busy); 11187c478bd9Sstevel@tonic-gate /* 11197c478bd9Sstevel@tonic-gate * Make this node some other node's child or child's sibling 11207c478bd9Sstevel@tonic-gate */ 11217c478bd9Sstevel@tonic-gate ASSERT(ap && np); 11227c478bd9Sstevel@tonic-gate if (ap->in_child == NULL) { 11237c478bd9Sstevel@tonic-gate ap->in_child = np; 11247c478bd9Sstevel@tonic-gate } else { 11257c478bd9Sstevel@tonic-gate for (mp = ap->in_child; mp; mp = mp->in_sibling) 11267c478bd9Sstevel@tonic-gate if (mp->in_sibling == NULL) { 11277c478bd9Sstevel@tonic-gate mp->in_sibling = np; 11287c478bd9Sstevel@tonic-gate break; 11297c478bd9Sstevel@tonic-gate } 11307c478bd9Sstevel@tonic-gate } 11317c478bd9Sstevel@tonic-gate np->in_parent = ap; 11327c478bd9Sstevel@tonic-gate } 11337c478bd9Sstevel@tonic-gate 11347c478bd9Sstevel@tonic-gate /* 11357c478bd9Sstevel@tonic-gate * Insert drv entry dp onto a node's driver list 11367c478bd9Sstevel@tonic-gate */ 11377c478bd9Sstevel@tonic-gate static void 11387c478bd9Sstevel@tonic-gate in_endrv(in_node_t *np, in_drv_t *dp) 11397c478bd9Sstevel@tonic-gate { 11407c478bd9Sstevel@tonic-gate in_drv_t *mp; 11417c478bd9Sstevel@tonic-gate ASSERT(e_ddi_inst_state.ins_busy); 11427c478bd9Sstevel@tonic-gate ASSERT(np && dp); 11437c478bd9Sstevel@tonic-gate mp = np->in_drivers; 11447c478bd9Sstevel@tonic-gate np->in_drivers = dp; 11457c478bd9Sstevel@tonic-gate dp->ind_next_drv = mp; 11467c478bd9Sstevel@tonic-gate dp->ind_node = np; 11477c478bd9Sstevel@tonic-gate } 11487c478bd9Sstevel@tonic-gate 11497c478bd9Sstevel@tonic-gate /* 11507c478bd9Sstevel@tonic-gate * Parse the next name out of the path, null terminate it and update cp. 11517c478bd9Sstevel@tonic-gate * caller has copied string so we can mess with it. 11527c478bd9Sstevel@tonic-gate * Upon return *cpp points to the next section to be parsed, *addrp points 11537c478bd9Sstevel@tonic-gate * to the current address substring (or NULL if none) and we return the 11547c478bd9Sstevel@tonic-gate * current name substring (or NULL if none). name and address substrings 11557c478bd9Sstevel@tonic-gate * are null terminated in place. 11567c478bd9Sstevel@tonic-gate */ 11577c478bd9Sstevel@tonic-gate 11587c478bd9Sstevel@tonic-gate static char * 11597c478bd9Sstevel@tonic-gate in_name_addr(char **cpp, char **addrp) 11607c478bd9Sstevel@tonic-gate { 11617c478bd9Sstevel@tonic-gate char *namep; /* return value holder */ 11627c478bd9Sstevel@tonic-gate char *ap; /* pointer to '@' in string */ 11637c478bd9Sstevel@tonic-gate char *sp; /* pointer to '/' in string */ 11647c478bd9Sstevel@tonic-gate 11657c478bd9Sstevel@tonic-gate if (*cpp == NULL || **cpp == '\0') { 11667c478bd9Sstevel@tonic-gate *addrp = NULL; 11677c478bd9Sstevel@tonic-gate return (NULL); 11687c478bd9Sstevel@tonic-gate } 11697c478bd9Sstevel@tonic-gate namep = *cpp; 11707c478bd9Sstevel@tonic-gate sp = strchr(*cpp, '/'); 11717c478bd9Sstevel@tonic-gate if (sp != NULL) { /* more to follow */ 11727c478bd9Sstevel@tonic-gate *sp = '\0'; 11737c478bd9Sstevel@tonic-gate *cpp = sp + 1; 11747c478bd9Sstevel@tonic-gate } else { /* this is last component. */ 11757c478bd9Sstevel@tonic-gate *cpp = NULL; 11767c478bd9Sstevel@tonic-gate } 11777c478bd9Sstevel@tonic-gate ap = strchr(namep, '@'); 11787c478bd9Sstevel@tonic-gate if (ap == NULL) { 11797c478bd9Sstevel@tonic-gate *addrp = NULL; 11807c478bd9Sstevel@tonic-gate } else { 11817c478bd9Sstevel@tonic-gate *ap = '\0'; /* terminate the name */ 11827c478bd9Sstevel@tonic-gate *addrp = ap + 1; 11837c478bd9Sstevel@tonic-gate } 11847c478bd9Sstevel@tonic-gate return (namep); 11857c478bd9Sstevel@tonic-gate } 11867c478bd9Sstevel@tonic-gate 11877c478bd9Sstevel@tonic-gate /* 11887c478bd9Sstevel@tonic-gate * Allocate a node and storage for name and addr strings, and fill them in. 11897c478bd9Sstevel@tonic-gate */ 11907c478bd9Sstevel@tonic-gate static in_node_t * 11917c478bd9Sstevel@tonic-gate in_alloc_node(char *name, char *addr) 11927c478bd9Sstevel@tonic-gate { 11937c478bd9Sstevel@tonic-gate in_node_t *np; 11947c478bd9Sstevel@tonic-gate char *cp; 11957c478bd9Sstevel@tonic-gate size_t namelen; 11967c478bd9Sstevel@tonic-gate 11977c478bd9Sstevel@tonic-gate ASSERT(e_ddi_inst_state.ins_busy); 11987c478bd9Sstevel@tonic-gate /* 11997c478bd9Sstevel@tonic-gate * Has name or will become root 12007c478bd9Sstevel@tonic-gate */ 12017c478bd9Sstevel@tonic-gate ASSERT(name || e_ddi_inst_state.ins_root == NULL); 12027c478bd9Sstevel@tonic-gate if (addr == NULL) 12037c478bd9Sstevel@tonic-gate addr = ""; 12047c478bd9Sstevel@tonic-gate if (name == NULL) 12057c478bd9Sstevel@tonic-gate namelen = 0; 12067c478bd9Sstevel@tonic-gate else 12077c478bd9Sstevel@tonic-gate namelen = strlen(name) + 1; 12087c478bd9Sstevel@tonic-gate cp = kmem_zalloc(sizeof (in_node_t) + namelen + strlen(addr) + 1, 12097c478bd9Sstevel@tonic-gate KM_SLEEP); 12107c478bd9Sstevel@tonic-gate np = (in_node_t *)cp; 12117c478bd9Sstevel@tonic-gate if (name) { 12127c478bd9Sstevel@tonic-gate np->in_node_name = cp + sizeof (in_node_t); 12137c478bd9Sstevel@tonic-gate (void) strcpy(np->in_node_name, name); 12147c478bd9Sstevel@tonic-gate } 12157c478bd9Sstevel@tonic-gate np->in_unit_addr = cp + sizeof (in_node_t) + namelen; 12167c478bd9Sstevel@tonic-gate (void) strcpy(np->in_unit_addr, addr); 12177c478bd9Sstevel@tonic-gate return (np); 12187c478bd9Sstevel@tonic-gate } 12197c478bd9Sstevel@tonic-gate 12207c478bd9Sstevel@tonic-gate /* 12217c478bd9Sstevel@tonic-gate * Allocate a drv entry and storage for binding name string, and fill it in. 12227c478bd9Sstevel@tonic-gate */ 12237c478bd9Sstevel@tonic-gate static in_drv_t * 12247c478bd9Sstevel@tonic-gate in_alloc_drv(char *bindingname) 12257c478bd9Sstevel@tonic-gate { 12267c478bd9Sstevel@tonic-gate in_drv_t *dp; 12277c478bd9Sstevel@tonic-gate char *cp; 12287c478bd9Sstevel@tonic-gate size_t namelen; 12297c478bd9Sstevel@tonic-gate 12307c478bd9Sstevel@tonic-gate ASSERT(e_ddi_inst_state.ins_busy); 12317c478bd9Sstevel@tonic-gate /* 12327c478bd9Sstevel@tonic-gate * Has name or will become root 12337c478bd9Sstevel@tonic-gate */ 12347c478bd9Sstevel@tonic-gate ASSERT(bindingname || e_ddi_inst_state.ins_root == NULL); 12357c478bd9Sstevel@tonic-gate if (bindingname == NULL) 12367c478bd9Sstevel@tonic-gate namelen = 0; 12377c478bd9Sstevel@tonic-gate else 12387c478bd9Sstevel@tonic-gate namelen = strlen(bindingname) + 1; 12397c478bd9Sstevel@tonic-gate cp = kmem_zalloc(sizeof (in_drv_t) + namelen, KM_SLEEP); 12407c478bd9Sstevel@tonic-gate dp = (in_drv_t *)cp; 12417c478bd9Sstevel@tonic-gate if (bindingname) { 12427c478bd9Sstevel@tonic-gate dp->ind_driver_name = cp + sizeof (in_drv_t); 12437c478bd9Sstevel@tonic-gate (void) strcpy(dp->ind_driver_name, bindingname); 12447c478bd9Sstevel@tonic-gate } 12457c478bd9Sstevel@tonic-gate dp->ind_state = IN_UNKNOWN; 12467c478bd9Sstevel@tonic-gate dp->ind_instance = -1; 12477c478bd9Sstevel@tonic-gate return (dp); 12487c478bd9Sstevel@tonic-gate } 12497c478bd9Sstevel@tonic-gate 12507c478bd9Sstevel@tonic-gate static void 12517c478bd9Sstevel@tonic-gate in_dealloc_node(in_node_t *np) 12527c478bd9Sstevel@tonic-gate { 12537c478bd9Sstevel@tonic-gate /* 12547c478bd9Sstevel@tonic-gate * The root node can never be de-allocated 12557c478bd9Sstevel@tonic-gate */ 12567c478bd9Sstevel@tonic-gate ASSERT(np->in_node_name && np->in_unit_addr); 12577c478bd9Sstevel@tonic-gate ASSERT(e_ddi_inst_state.ins_busy); 12587c478bd9Sstevel@tonic-gate kmem_free(np, sizeof (in_node_t) + strlen(np->in_node_name) 12597c478bd9Sstevel@tonic-gate + strlen(np->in_unit_addr) + 2); 12607c478bd9Sstevel@tonic-gate } 12617c478bd9Sstevel@tonic-gate 12627c478bd9Sstevel@tonic-gate static void 12637c478bd9Sstevel@tonic-gate in_dealloc_drv(in_drv_t *dp) 12647c478bd9Sstevel@tonic-gate { 12657c478bd9Sstevel@tonic-gate ASSERT(dp->ind_driver_name); 12667c478bd9Sstevel@tonic-gate ASSERT(e_ddi_inst_state.ins_busy); 12677c478bd9Sstevel@tonic-gate kmem_free(dp, sizeof (in_drv_t) + strlen(dp->ind_driver_name) 12687c478bd9Sstevel@tonic-gate + 1); 12697c478bd9Sstevel@tonic-gate } 12707c478bd9Sstevel@tonic-gate 12717c478bd9Sstevel@tonic-gate /* 12727c478bd9Sstevel@tonic-gate * Handle the various possible versions of "no address" 12737c478bd9Sstevel@tonic-gate */ 12747c478bd9Sstevel@tonic-gate static int 12757c478bd9Sstevel@tonic-gate in_eqstr(char *a, char *b) 12767c478bd9Sstevel@tonic-gate { 12777c478bd9Sstevel@tonic-gate if (a == b) /* covers case where both are nulls */ 12787c478bd9Sstevel@tonic-gate return (1); 12797c478bd9Sstevel@tonic-gate if (a == NULL && *b == 0) 12807c478bd9Sstevel@tonic-gate return (1); 12817c478bd9Sstevel@tonic-gate if (b == NULL && *a == 0) 12827c478bd9Sstevel@tonic-gate return (1); 12837c478bd9Sstevel@tonic-gate if (a == NULL || b == NULL) 12847c478bd9Sstevel@tonic-gate return (0); 12857c478bd9Sstevel@tonic-gate return (strcmp(a, b) == 0); 12867c478bd9Sstevel@tonic-gate } 12877c478bd9Sstevel@tonic-gate 12887c478bd9Sstevel@tonic-gate /* 12897c478bd9Sstevel@tonic-gate * Returns true if instance no. is already in use by named driver 12907c478bd9Sstevel@tonic-gate */ 12917c478bd9Sstevel@tonic-gate static int 12927c478bd9Sstevel@tonic-gate in_inuse(int instance, char *name) 12937c478bd9Sstevel@tonic-gate { 12947c478bd9Sstevel@tonic-gate major_t major; 12957c478bd9Sstevel@tonic-gate in_drv_t *dp; 12967c478bd9Sstevel@tonic-gate struct devnames *dnp; 12977c478bd9Sstevel@tonic-gate 12987c478bd9Sstevel@tonic-gate ASSERT(e_ddi_inst_state.ins_busy); 12997c478bd9Sstevel@tonic-gate /* 13007c478bd9Sstevel@tonic-gate * For now, if we've never heard of this device we assume it is not 13017c478bd9Sstevel@tonic-gate * in use, since we can't tell 13027c478bd9Sstevel@tonic-gate * XXX could do the weaker search through the nomajor list checking 13037c478bd9Sstevel@tonic-gate * XXX for the same name 13047c478bd9Sstevel@tonic-gate */ 1305a204de77Scth if ((major = ddi_name_to_major(name)) == DDI_MAJOR_T_NONE) 13067c478bd9Sstevel@tonic-gate return (0); 13077c478bd9Sstevel@tonic-gate dnp = &devnamesp[major]; 13087c478bd9Sstevel@tonic-gate 13097c478bd9Sstevel@tonic-gate dp = dnp->dn_inlist; 13107c478bd9Sstevel@tonic-gate while (dp) { 13117c478bd9Sstevel@tonic-gate if (dp->ind_instance == instance) 13127c478bd9Sstevel@tonic-gate return (1); 13137c478bd9Sstevel@tonic-gate dp = dp->ind_next; 13147c478bd9Sstevel@tonic-gate } 13157c478bd9Sstevel@tonic-gate return (0); 13167c478bd9Sstevel@tonic-gate } 13177c478bd9Sstevel@tonic-gate 13187c478bd9Sstevel@tonic-gate static void 13197c478bd9Sstevel@tonic-gate in_hashdrv(in_drv_t *dp) 13207c478bd9Sstevel@tonic-gate { 13217c478bd9Sstevel@tonic-gate struct devnames *dnp; 13227c478bd9Sstevel@tonic-gate in_drv_t *mp, *pp; 13237c478bd9Sstevel@tonic-gate major_t major; 13247c478bd9Sstevel@tonic-gate 13257c478bd9Sstevel@tonic-gate /* hash to no major list */ 1326a204de77Scth major = ddi_name_to_major(dp->ind_driver_name); 1327a204de77Scth if (major == DDI_MAJOR_T_NONE) { 13287c478bd9Sstevel@tonic-gate dp->ind_next = e_ddi_inst_state.ins_no_major; 13297c478bd9Sstevel@tonic-gate e_ddi_inst_state.ins_no_major = dp; 13307c478bd9Sstevel@tonic-gate return; 13317c478bd9Sstevel@tonic-gate } 13327c478bd9Sstevel@tonic-gate 13337c478bd9Sstevel@tonic-gate /* 13347c478bd9Sstevel@tonic-gate * dnp->dn_inlist is sorted by instance number. 13357c478bd9Sstevel@tonic-gate * Adding a new instance entry may introduce holes, 13367c478bd9Sstevel@tonic-gate * set dn_instance to IN_SEARCHME so the next instance 13377c478bd9Sstevel@tonic-gate * assignment may fill in holes. 13387c478bd9Sstevel@tonic-gate */ 13397c478bd9Sstevel@tonic-gate dnp = &devnamesp[major]; 13407c478bd9Sstevel@tonic-gate pp = mp = dnp->dn_inlist; 13417c478bd9Sstevel@tonic-gate if (mp == NULL || dp->ind_instance < mp->ind_instance) { 13427c478bd9Sstevel@tonic-gate /* prepend as the first entry, turn on IN_SEARCHME */ 13437c478bd9Sstevel@tonic-gate dnp->dn_instance = IN_SEARCHME; 13447c478bd9Sstevel@tonic-gate dp->ind_next = mp; 13457c478bd9Sstevel@tonic-gate dnp->dn_inlist = dp; 13467c478bd9Sstevel@tonic-gate return; 13477c478bd9Sstevel@tonic-gate } 13487c478bd9Sstevel@tonic-gate 13497c478bd9Sstevel@tonic-gate ASSERT(mp->ind_instance != dp->ind_instance); 13507c478bd9Sstevel@tonic-gate while (mp->ind_instance < dp->ind_instance && mp->ind_next) { 13517c478bd9Sstevel@tonic-gate pp = mp; 13527c478bd9Sstevel@tonic-gate mp = mp->ind_next; 13537c478bd9Sstevel@tonic-gate ASSERT(mp->ind_instance != dp->ind_instance); 13547c478bd9Sstevel@tonic-gate } 13557c478bd9Sstevel@tonic-gate 13567c478bd9Sstevel@tonic-gate if (mp->ind_instance < dp->ind_instance) { /* end of list */ 13577c478bd9Sstevel@tonic-gate dp->ind_next = NULL; 13587c478bd9Sstevel@tonic-gate mp->ind_next = dp; 13597c478bd9Sstevel@tonic-gate } else { 13607c478bd9Sstevel@tonic-gate dp->ind_next = pp->ind_next; 13617c478bd9Sstevel@tonic-gate pp->ind_next = dp; 13627c478bd9Sstevel@tonic-gate } 13637c478bd9Sstevel@tonic-gate } 13647c478bd9Sstevel@tonic-gate 13657c478bd9Sstevel@tonic-gate /* 13667c478bd9Sstevel@tonic-gate * Remove a driver entry from the list, given a previous pointer 13677c478bd9Sstevel@tonic-gate */ 13687c478bd9Sstevel@tonic-gate static void 13697c478bd9Sstevel@tonic-gate in_removedrv(struct devnames *dnp, in_drv_t *mp) 13707c478bd9Sstevel@tonic-gate { 13717c478bd9Sstevel@tonic-gate in_drv_t *dp; 13727c478bd9Sstevel@tonic-gate in_drv_t *prevp; 13737c478bd9Sstevel@tonic-gate 13747c478bd9Sstevel@tonic-gate if (dnp->dn_inlist == mp) { /* head of list */ 13757c478bd9Sstevel@tonic-gate dnp->dn_inlist = mp->ind_next; 13767c478bd9Sstevel@tonic-gate dnp->dn_instance = IN_SEARCHME; 13777c478bd9Sstevel@tonic-gate in_dq_drv(mp); 13787c478bd9Sstevel@tonic-gate in_dealloc_drv(mp); 13797c478bd9Sstevel@tonic-gate return; 13807c478bd9Sstevel@tonic-gate } 13817c478bd9Sstevel@tonic-gate prevp = dnp->dn_inlist; 13827c478bd9Sstevel@tonic-gate for (dp = prevp->ind_next; dp; dp = dp->ind_next) { 13837c478bd9Sstevel@tonic-gate if (dp == mp) { /* found it */ 13847c478bd9Sstevel@tonic-gate break; 13857c478bd9Sstevel@tonic-gate } 13867c478bd9Sstevel@tonic-gate prevp = dp; 13877c478bd9Sstevel@tonic-gate } 13887c478bd9Sstevel@tonic-gate 13897c478bd9Sstevel@tonic-gate ASSERT(dp == mp); 13907c478bd9Sstevel@tonic-gate dnp->dn_instance = IN_SEARCHME; 13917c478bd9Sstevel@tonic-gate prevp->ind_next = mp->ind_next; 13927c478bd9Sstevel@tonic-gate in_dq_drv(mp); 13937c478bd9Sstevel@tonic-gate in_dealloc_drv(mp); 13947c478bd9Sstevel@tonic-gate } 13957c478bd9Sstevel@tonic-gate 13967c478bd9Sstevel@tonic-gate static void 13977c478bd9Sstevel@tonic-gate in_dq_drv(in_drv_t *mp) 13987c478bd9Sstevel@tonic-gate { 13997c478bd9Sstevel@tonic-gate struct in_node *node = mp->ind_node; 14007c478bd9Sstevel@tonic-gate in_drv_t *ptr, *prev; 14017c478bd9Sstevel@tonic-gate 14027c478bd9Sstevel@tonic-gate if (mp == node->in_drivers) { 14037c478bd9Sstevel@tonic-gate node->in_drivers = mp->ind_next_drv; 14047c478bd9Sstevel@tonic-gate return; 14057c478bd9Sstevel@tonic-gate } 14067c478bd9Sstevel@tonic-gate prev = node->in_drivers; 14077c478bd9Sstevel@tonic-gate for (ptr = prev->ind_next_drv; ptr != (struct in_drv *)NULL; 14087c478bd9Sstevel@tonic-gate ptr = ptr->ind_next_drv) { 14097c478bd9Sstevel@tonic-gate if (ptr == mp) { 14107c478bd9Sstevel@tonic-gate prev->ind_next_drv = ptr->ind_next_drv; 14117c478bd9Sstevel@tonic-gate return; 14127c478bd9Sstevel@tonic-gate } 1413ffc89d77Svikram prev = ptr; 14147c478bd9Sstevel@tonic-gate } 14157c478bd9Sstevel@tonic-gate panic("in_dq_drv: in_drv not found on node driver list"); 14167c478bd9Sstevel@tonic-gate } 14177c478bd9Sstevel@tonic-gate 14187c478bd9Sstevel@tonic-gate 14197c478bd9Sstevel@tonic-gate in_drv_t * 14207c478bd9Sstevel@tonic-gate in_drvwalk(in_node_t *np, char *binding_name) 14217c478bd9Sstevel@tonic-gate { 14227c478bd9Sstevel@tonic-gate char *name; 14237c478bd9Sstevel@tonic-gate in_drv_t *dp = np->in_drivers; 14247c478bd9Sstevel@tonic-gate while (dp) { 14257c478bd9Sstevel@tonic-gate if ((name = i_binding_to_drv_name(dp->ind_driver_name)) 14267c478bd9Sstevel@tonic-gate == NULL) { 14277c478bd9Sstevel@tonic-gate name = dp->ind_driver_name; 14287c478bd9Sstevel@tonic-gate } 14297c478bd9Sstevel@tonic-gate if (strcmp(binding_name, name) == 0) { 14307c478bd9Sstevel@tonic-gate break; 14317c478bd9Sstevel@tonic-gate } 14327c478bd9Sstevel@tonic-gate dp = dp->ind_next_drv; 14337c478bd9Sstevel@tonic-gate } 14347c478bd9Sstevel@tonic-gate return (dp); 14357c478bd9Sstevel@tonic-gate } 14367c478bd9Sstevel@tonic-gate 14377c478bd9Sstevel@tonic-gate 14387c478bd9Sstevel@tonic-gate 14397c478bd9Sstevel@tonic-gate static void 14407c478bd9Sstevel@tonic-gate i_log_devfs_instance_mod(void) 14417c478bd9Sstevel@tonic-gate { 14427c478bd9Sstevel@tonic-gate sysevent_t *ev; 14437c478bd9Sstevel@tonic-gate sysevent_id_t eid; 144416bd7258Scth static int sent_one = 0; 14457c478bd9Sstevel@tonic-gate 14467c478bd9Sstevel@tonic-gate /* 144716bd7258Scth * Prevent unnecessary event generation. Do not generate more than 144816bd7258Scth * one event during boot. 14497c478bd9Sstevel@tonic-gate */ 145016bd7258Scth if (sent_one && !i_ddi_io_initialized()) 14517c478bd9Sstevel@tonic-gate return; 14527c478bd9Sstevel@tonic-gate 14537c478bd9Sstevel@tonic-gate ev = sysevent_alloc(EC_DEVFS, ESC_DEVFS_INSTANCE_MOD, EP_DDI, 14547c478bd9Sstevel@tonic-gate SE_NOSLEEP); 14557c478bd9Sstevel@tonic-gate if (ev == NULL) { 14567c478bd9Sstevel@tonic-gate return; 14577c478bd9Sstevel@tonic-gate } 14587c478bd9Sstevel@tonic-gate if (log_sysevent(ev, SE_NOSLEEP, &eid) != 0) { 14597c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "i_log_devfs_instance_mod: failed to post " 14607c478bd9Sstevel@tonic-gate "event"); 146116bd7258Scth } else { 146216bd7258Scth sent_one = 1; 14637c478bd9Sstevel@tonic-gate } 14647c478bd9Sstevel@tonic-gate sysevent_free(ev); 14657c478bd9Sstevel@tonic-gate } 14667c478bd9Sstevel@tonic-gate 14677c478bd9Sstevel@tonic-gate void 146894c894bbSVikram Hegde e_ddi_enter_instance(void) 14697c478bd9Sstevel@tonic-gate { 14707c478bd9Sstevel@tonic-gate mutex_enter(&e_ddi_inst_state.ins_serial); 14717c478bd9Sstevel@tonic-gate if (e_ddi_inst_state.ins_thread == curthread) 14727c478bd9Sstevel@tonic-gate e_ddi_inst_state.ins_busy++; 14737c478bd9Sstevel@tonic-gate else { 14747c478bd9Sstevel@tonic-gate while (e_ddi_inst_state.ins_busy) 14757c478bd9Sstevel@tonic-gate cv_wait(&e_ddi_inst_state.ins_serial_cv, 14767c478bd9Sstevel@tonic-gate &e_ddi_inst_state.ins_serial); 14777c478bd9Sstevel@tonic-gate e_ddi_inst_state.ins_thread = curthread; 14787c478bd9Sstevel@tonic-gate e_ddi_inst_state.ins_busy = 1; 14797c478bd9Sstevel@tonic-gate } 14807c478bd9Sstevel@tonic-gate mutex_exit(&e_ddi_inst_state.ins_serial); 14817c478bd9Sstevel@tonic-gate } 14827c478bd9Sstevel@tonic-gate 14837c478bd9Sstevel@tonic-gate void 148494c894bbSVikram Hegde e_ddi_exit_instance(void) 14857c478bd9Sstevel@tonic-gate { 14867c478bd9Sstevel@tonic-gate mutex_enter(&e_ddi_inst_state.ins_serial); 14877c478bd9Sstevel@tonic-gate e_ddi_inst_state.ins_busy--; 14887c478bd9Sstevel@tonic-gate if (e_ddi_inst_state.ins_busy == 0) { 14897c478bd9Sstevel@tonic-gate cv_broadcast(&e_ddi_inst_state.ins_serial_cv); 14907c478bd9Sstevel@tonic-gate e_ddi_inst_state.ins_thread = NULL; 14917c478bd9Sstevel@tonic-gate } 14927c478bd9Sstevel@tonic-gate mutex_exit(&e_ddi_inst_state.ins_serial); 14937c478bd9Sstevel@tonic-gate } 14947c478bd9Sstevel@tonic-gate 14957c478bd9Sstevel@tonic-gate int 149694c894bbSVikram Hegde e_ddi_instance_is_clean(void) 14977c478bd9Sstevel@tonic-gate { 149894c894bbSVikram Hegde return (e_ddi_inst_state.ins_dirty == B_FALSE); 14997c478bd9Sstevel@tonic-gate } 15007c478bd9Sstevel@tonic-gate 15017c478bd9Sstevel@tonic-gate void 150294c894bbSVikram Hegde e_ddi_instance_set_clean(void) 15037c478bd9Sstevel@tonic-gate { 150494c894bbSVikram Hegde e_ddi_inst_state.ins_dirty = B_FALSE; 15057c478bd9Sstevel@tonic-gate } 15067c478bd9Sstevel@tonic-gate 15077c478bd9Sstevel@tonic-gate in_node_t * 150894c894bbSVikram Hegde e_ddi_instance_root(void) 15097c478bd9Sstevel@tonic-gate { 15107c478bd9Sstevel@tonic-gate return (e_ddi_inst_state.ins_root); 15117c478bd9Sstevel@tonic-gate } 15127c478bd9Sstevel@tonic-gate 15137c478bd9Sstevel@tonic-gate /* 15147c478bd9Sstevel@tonic-gate * Visit a node in the instance tree 15157c478bd9Sstevel@tonic-gate */ 15167c478bd9Sstevel@tonic-gate static int 15177c478bd9Sstevel@tonic-gate in_walk_instances(in_node_t *np, char *path, char *this, 15187c478bd9Sstevel@tonic-gate int (*f)(const char *, in_node_t *, in_drv_t *, void *), void *arg) 15197c478bd9Sstevel@tonic-gate { 15207c478bd9Sstevel@tonic-gate in_drv_t *dp; 15217c478bd9Sstevel@tonic-gate int rval = INST_WALK_CONTINUE; 15227c478bd9Sstevel@tonic-gate char *next; 15237c478bd9Sstevel@tonic-gate 15247c478bd9Sstevel@tonic-gate while (np != NULL) { 15257c478bd9Sstevel@tonic-gate 15267c478bd9Sstevel@tonic-gate if (np->in_unit_addr[0] == 0) 15277c478bd9Sstevel@tonic-gate (void) sprintf(this, "/%s", np->in_node_name); 15287c478bd9Sstevel@tonic-gate else 15297c478bd9Sstevel@tonic-gate (void) sprintf(this, "/%s@%s", np->in_node_name, 15307c478bd9Sstevel@tonic-gate np->in_unit_addr); 15317c478bd9Sstevel@tonic-gate next = this + strlen(this); 15327c478bd9Sstevel@tonic-gate 15337c478bd9Sstevel@tonic-gate for (dp = np->in_drivers; dp; dp = dp->ind_next_drv) { 15347c478bd9Sstevel@tonic-gate if (dp->ind_state == IN_PERMANENT) { 15357c478bd9Sstevel@tonic-gate rval = (*f)(path, np, dp, arg); 15367c478bd9Sstevel@tonic-gate if (rval == INST_WALK_TERMINATE) 15377c478bd9Sstevel@tonic-gate break; 15387c478bd9Sstevel@tonic-gate } 15397c478bd9Sstevel@tonic-gate } 154094c894bbSVikram Hegde 15417c478bd9Sstevel@tonic-gate if (np->in_child) { 15427c478bd9Sstevel@tonic-gate rval = in_walk_instances(np->in_child, 15437c478bd9Sstevel@tonic-gate path, next, f, arg); 15447c478bd9Sstevel@tonic-gate if (rval == INST_WALK_TERMINATE) 15457c478bd9Sstevel@tonic-gate break; 15467c478bd9Sstevel@tonic-gate } 15477c478bd9Sstevel@tonic-gate 15487c478bd9Sstevel@tonic-gate np = np->in_sibling; 15497c478bd9Sstevel@tonic-gate } 15507c478bd9Sstevel@tonic-gate 15517c478bd9Sstevel@tonic-gate return (rval); 15527c478bd9Sstevel@tonic-gate } 15537c478bd9Sstevel@tonic-gate 15547c478bd9Sstevel@tonic-gate /* 15557c478bd9Sstevel@tonic-gate * A general interface for walking the instance tree, 15567c478bd9Sstevel@tonic-gate * calling a user-supplied callback for each node. 15577c478bd9Sstevel@tonic-gate */ 15587c478bd9Sstevel@tonic-gate int 15597c478bd9Sstevel@tonic-gate e_ddi_walk_instances(int (*f)(const char *, 15607c478bd9Sstevel@tonic-gate in_node_t *, in_drv_t *, void *), void *arg) 15617c478bd9Sstevel@tonic-gate { 15627c478bd9Sstevel@tonic-gate in_node_t *root; 15637c478bd9Sstevel@tonic-gate int rval; 15647c478bd9Sstevel@tonic-gate char *path; 15657c478bd9Sstevel@tonic-gate 15667c478bd9Sstevel@tonic-gate path = kmem_zalloc(MAXPATHLEN, KM_SLEEP); 15677c478bd9Sstevel@tonic-gate 15687c478bd9Sstevel@tonic-gate e_ddi_enter_instance(); 15697c478bd9Sstevel@tonic-gate root = e_ddi_instance_root(); 15707c478bd9Sstevel@tonic-gate rval = in_walk_instances(root->in_child, path, path, f, arg); 157194c894bbSVikram Hegde 15727c478bd9Sstevel@tonic-gate e_ddi_exit_instance(); 15737c478bd9Sstevel@tonic-gate 15747c478bd9Sstevel@tonic-gate kmem_free(path, MAXPATHLEN); 15757c478bd9Sstevel@tonic-gate return (rval); 15767c478bd9Sstevel@tonic-gate } 157794c894bbSVikram Hegde 157894c894bbSVikram Hegde in_node_t * 157994c894bbSVikram Hegde e_ddi_path_to_instance(char *path) 158094c894bbSVikram Hegde { 158194c894bbSVikram Hegde in_node_t *np; 158294c894bbSVikram Hegde 158394c894bbSVikram Hegde np = in_make_path(path); 158494c894bbSVikram Hegde if (np && np->in_drivers && np->in_drivers->ind_state == IN_PERMANENT) { 158594c894bbSVikram Hegde return (np); 158694c894bbSVikram Hegde } 158794c894bbSVikram Hegde return (NULL); 158894c894bbSVikram Hegde } 158994c894bbSVikram Hegde 159094c894bbSVikram Hegde void 159194c894bbSVikram Hegde e_ddi_borrow_instance(dev_info_t *cdip, in_node_t *cnp) 159294c894bbSVikram Hegde { 159394c894bbSVikram Hegde char *alias; 159494c894bbSVikram Hegde in_node_t *anp; 159594c894bbSVikram Hegde char *curr = kmem_alloc(MAXPATHLEN, KM_NOSLEEP); 159694c894bbSVikram Hegde 159794c894bbSVikram Hegde if (curr == NULL) { 159894c894bbSVikram Hegde ddi_err(DER_PANIC, cdip, "curr alloc failed"); 159994c894bbSVikram Hegde /*NOTREACHED*/ 160094c894bbSVikram Hegde } 160194c894bbSVikram Hegde 160294c894bbSVikram Hegde (void) ddi_pathname(cdip, curr); 160394c894bbSVikram Hegde 160494c894bbSVikram Hegde if (cnp->in_drivers) { 16055633e4f8SJan Setje-Eilers /* there can be multiple drivers bound */ 16065633e4f8SJan Setje-Eilers ddi_err(DER_LOG, cdip, "%s has previous binding: %s", curr, 16075633e4f8SJan Setje-Eilers cnp->in_drivers->ind_driver_name); 160894c894bbSVikram Hegde } 160994c894bbSVikram Hegde 161094c894bbSVikram Hegde alias = ddi_curr_redirect(curr); 16115633e4f8SJan Setje-Eilers 16125633e4f8SJan Setje-Eilers /* bail here if the alias matches any other current path or itself */ 16135633e4f8SJan Setje-Eilers if (alias && ((strcmp(curr, alias) == 0) || 16145633e4f8SJan Setje-Eilers (ddi_curr_redirect(alias) != 0))) { 16155633e4f8SJan Setje-Eilers DDI_MP_DBG((CE_NOTE, "not borrowing current: %s alias: %s", 16165633e4f8SJan Setje-Eilers curr, alias)); 16175633e4f8SJan Setje-Eilers goto out; 16185633e4f8SJan Setje-Eilers } 161994c894bbSVikram Hegde 162094c894bbSVikram Hegde if (alias && (anp = e_ddi_path_to_instance(alias)) != NULL) { 1621*90646fe8SJan Setje-Eilers /* 1622*90646fe8SJan Setje-Eilers * Since pcieb nodes can split and merge, it is dangerous 1623*90646fe8SJan Setje-Eilers * to borrow and instance for them. However since they do 1624*90646fe8SJan Setje-Eilers * not expose their instance numbers it is safe to never 1625*90646fe8SJan Setje-Eilers * borrow one. 1626*90646fe8SJan Setje-Eilers */ 1627*90646fe8SJan Setje-Eilers if (anp->in_drivers->ind_driver_name && 1628*90646fe8SJan Setje-Eilers (strcmp(anp->in_drivers->ind_driver_name, "pcieb") == 0)) { 1629*90646fe8SJan Setje-Eilers DDI_MP_DBG((CE_NOTE, "not borrowing pcieb: " 1630*90646fe8SJan Setje-Eilers "%s alias: %s", curr, alias)); 1631*90646fe8SJan Setje-Eilers goto out; 1632*90646fe8SJan Setje-Eilers } 16335633e4f8SJan Setje-Eilers DDI_MP_DBG((CE_NOTE, "borrowing current: %s alias: %s", 16345633e4f8SJan Setje-Eilers curr, alias)); 163594c894bbSVikram Hegde cnp->in_drivers = anp->in_drivers; 163694c894bbSVikram Hegde anp->in_drivers = NULL; 163794c894bbSVikram Hegde } 16385633e4f8SJan Setje-Eilers out: 16395633e4f8SJan Setje-Eilers kmem_free(curr, MAXPATHLEN); 164094c894bbSVikram Hegde } 164194c894bbSVikram Hegde 164294c894bbSVikram Hegde void 164394c894bbSVikram Hegde e_ddi_return_instance(dev_info_t *cdip, char *addr, in_node_t *cnp) 164494c894bbSVikram Hegde { 164594c894bbSVikram Hegde in_node_t *anp; 164694c894bbSVikram Hegde char *alias; 164794c894bbSVikram Hegde char *curr = kmem_alloc(MAXPATHLEN, KM_NOSLEEP); 164894c894bbSVikram Hegde 164994c894bbSVikram Hegde if (curr == NULL) { 165094c894bbSVikram Hegde ddi_err(DER_PANIC, cdip, "alloc of curr failed"); 165194c894bbSVikram Hegde /*NOTREACHED*/ 165294c894bbSVikram Hegde } 165394c894bbSVikram Hegde 165494c894bbSVikram Hegde (void) ddi_pathname(cdip, curr); 165594c894bbSVikram Hegde if (addr) { 165694c894bbSVikram Hegde (void) strlcat(curr, "@", MAXPATHLEN); 165794c894bbSVikram Hegde (void) strlcat(curr, addr, MAXPATHLEN); 165894c894bbSVikram Hegde 165994c894bbSVikram Hegde } 166094c894bbSVikram Hegde if (cnp->in_drivers == NULL) { 166194c894bbSVikram Hegde ddi_err(DER_PANIC, cdip, "cnp has no inst: %p", cnp); 166294c894bbSVikram Hegde /*NOTREACHED*/ 166394c894bbSVikram Hegde } 166494c894bbSVikram Hegde 166594c894bbSVikram Hegde alias = ddi_curr_redirect(curr); 166694c894bbSVikram Hegde kmem_free(curr, MAXPATHLEN); 166794c894bbSVikram Hegde 166894c894bbSVikram Hegde if (alias && (anp = e_ddi_path_to_instance(alias)) != NULL) { 166994c894bbSVikram Hegde ASSERT(anp->in_drivers == NULL); 167094c894bbSVikram Hegde anp->in_drivers = cnp->in_drivers; 167194c894bbSVikram Hegde cnp->in_drivers = NULL; 167294c894bbSVikram Hegde } 167394c894bbSVikram Hegde } 1674