10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 53250Scth * Common Development and Distribution License (the "License"). 63250Scth * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 210Sstevel@tonic-gate /* 22*12116SVikram.Hegde@Sun.COM * Copyright (c) 1992, 2010, Oracle and/or its affiliates. All rights reserved. 230Sstevel@tonic-gate */ 240Sstevel@tonic-gate 250Sstevel@tonic-gate /* 260Sstevel@tonic-gate * Instance number assignment code 270Sstevel@tonic-gate */ 280Sstevel@tonic-gate 290Sstevel@tonic-gate #include <sys/types.h> 300Sstevel@tonic-gate #include <sys/param.h> 310Sstevel@tonic-gate #include <sys/errno.h> 320Sstevel@tonic-gate #include <sys/systm.h> 330Sstevel@tonic-gate #include <sys/kobj.h> 340Sstevel@tonic-gate #include <sys/t_lock.h> 350Sstevel@tonic-gate #include <sys/kmem.h> 360Sstevel@tonic-gate #include <sys/cmn_err.h> 370Sstevel@tonic-gate #include <sys/ddi.h> 380Sstevel@tonic-gate #include <sys/sunddi.h> 390Sstevel@tonic-gate #include <sys/autoconf.h> 400Sstevel@tonic-gate #include <sys/systeminfo.h> 410Sstevel@tonic-gate #include <sys/hwconf.h> 420Sstevel@tonic-gate #include <sys/reboot.h> 430Sstevel@tonic-gate #include <sys/ddi_impldefs.h> 440Sstevel@tonic-gate #include <sys/instance.h> 450Sstevel@tonic-gate #include <sys/debug.h> 460Sstevel@tonic-gate #include <sys/sysevent.h> 470Sstevel@tonic-gate #include <sys/modctl.h> 480Sstevel@tonic-gate #include <sys/console.h> 490Sstevel@tonic-gate #include <sys/cladm.h> 50*12116SVikram.Hegde@Sun.COM #include <sys/sysmacros.h> 51*12116SVikram.Hegde@Sun.COM #include <sys/crc32.h> 52*12116SVikram.Hegde@Sun.COM 530Sstevel@tonic-gate 540Sstevel@tonic-gate static void in_preassign_instance(void); 550Sstevel@tonic-gate static void i_log_devfs_instance_mod(void); 560Sstevel@tonic-gate static int in_get_infile(char *); 570Sstevel@tonic-gate static void in_removenode(struct devnames *dnp, in_node_t *mp, in_node_t *ap); 580Sstevel@tonic-gate static in_node_t *in_alloc_node(char *name, char *addr); 590Sstevel@tonic-gate static int in_eqstr(char *a, char *b); 600Sstevel@tonic-gate static char *in_name_addr(char **cpp, char **addrp); 610Sstevel@tonic-gate static in_node_t *in_devwalk(dev_info_t *dip, in_node_t **ap, char *addr); 620Sstevel@tonic-gate static void in_dealloc_node(in_node_t *np); 630Sstevel@tonic-gate static in_node_t *in_make_path(char *path); 640Sstevel@tonic-gate static void in_enlist(in_node_t *ap, in_node_t *np); 650Sstevel@tonic-gate static int in_inuse(int instance, char *name); 660Sstevel@tonic-gate static void in_hashdrv(in_drv_t *dp); 670Sstevel@tonic-gate static in_drv_t *in_drvwalk(in_node_t *np, char *binding_name); 680Sstevel@tonic-gate static in_drv_t *in_alloc_drv(char *bindingname); 690Sstevel@tonic-gate static void in_endrv(in_node_t *np, in_drv_t *dp); 700Sstevel@tonic-gate static void in_dq_drv(in_drv_t *np); 710Sstevel@tonic-gate static void in_removedrv(struct devnames *dnp, in_drv_t *mp); 720Sstevel@tonic-gate static int in_pathin(char *cp, int instance, char *bname, struct bind **args); 733250Scth static int in_next_instance_block(major_t, int); 740Sstevel@tonic-gate static int in_next_instance(major_t); 750Sstevel@tonic-gate 76*12116SVikram.Hegde@Sun.COM #pragma weak plat_ioaliases_init 77*12116SVikram.Hegde@Sun.COM 78*12116SVikram.Hegde@Sun.COM 790Sstevel@tonic-gate /* external functions */ 800Sstevel@tonic-gate extern char *i_binding_to_drv_name(char *bname); 81*12116SVikram.Hegde@Sun.COM extern void plat_ioaliases_init(void); 820Sstevel@tonic-gate 830Sstevel@tonic-gate /* 840Sstevel@tonic-gate * This plus devnames defines the entire software state of the instance world. 850Sstevel@tonic-gate */ 860Sstevel@tonic-gate typedef struct in_softstate { 870Sstevel@tonic-gate in_node_t *ins_root; /* the root of our instance tree */ 880Sstevel@tonic-gate in_drv_t *ins_no_major; /* majorless drv entries */ 890Sstevel@tonic-gate /* 900Sstevel@tonic-gate * Used to serialize access to data structures 910Sstevel@tonic-gate */ 920Sstevel@tonic-gate void *ins_thread; 930Sstevel@tonic-gate kmutex_t ins_serial; 940Sstevel@tonic-gate kcondvar_t ins_serial_cv; 950Sstevel@tonic-gate int ins_busy; 96*12116SVikram.Hegde@Sun.COM boolean_t ins_dirty; /* instance info needs flush */ 970Sstevel@tonic-gate } in_softstate_t; 980Sstevel@tonic-gate 990Sstevel@tonic-gate static in_softstate_t e_ddi_inst_state; 1000Sstevel@tonic-gate 1010Sstevel@tonic-gate /* 1020Sstevel@tonic-gate * State transition information: 1030Sstevel@tonic-gate * e_ddi_inst_state contains, among other things, the root of a tree of 1040Sstevel@tonic-gate * device nodes used to track instance number assignments. 1050Sstevel@tonic-gate * Each device node may contain multiple driver bindings, represented 1060Sstevel@tonic-gate * by a linked list of in_drv_t nodes, each with an instance assignment 1070Sstevel@tonic-gate * (except for root node). Each in_drv node can be in one of 3 states, 1080Sstevel@tonic-gate * indicated by ind_state: 1090Sstevel@tonic-gate * 1100Sstevel@tonic-gate * IN_UNKNOWN: Each node created in this state. The instance number of 1110Sstevel@tonic-gate * this node is not known. ind_instance is set to -1. 1120Sstevel@tonic-gate * IN_PROVISIONAL: When a node is assigned an instance number in 1130Sstevel@tonic-gate * e_ddi_assign_instance(), its state is set to IN_PROVISIONAL. 1140Sstevel@tonic-gate * Subsequently, the framework will always call either 115*12116SVikram.Hegde@Sun.COM * e_ddi_keep_instance() which makes the node IN_PERMANENT 1160Sstevel@tonic-gate * or e_ddi_free_instance(), which deletes the node. 1170Sstevel@tonic-gate * IN_PERMANENT: 1180Sstevel@tonic-gate * If e_ddi_keep_instance() is called on an IN_PROVISIONAL node, 1190Sstevel@tonic-gate * its state is set to IN_PERMANENT. 1200Sstevel@tonic-gate */ 1210Sstevel@tonic-gate 1220Sstevel@tonic-gate static char *instance_file = INSTANCE_FILE; 1230Sstevel@tonic-gate static char *instance_file_backup = INSTANCE_FILE INSTANCE_FILE_SUFFIX; 1240Sstevel@tonic-gate 1250Sstevel@tonic-gate /* 1260Sstevel@tonic-gate * Return values for in_get_infile(). 1270Sstevel@tonic-gate */ 1280Sstevel@tonic-gate #define PTI_FOUND 0 1290Sstevel@tonic-gate #define PTI_NOT_FOUND 1 1300Sstevel@tonic-gate #define PTI_REBUILD 2 1310Sstevel@tonic-gate 132*12116SVikram.Hegde@Sun.COM 1330Sstevel@tonic-gate /* 1340Sstevel@tonic-gate * Path to instance file magic string used for first time boot after 1350Sstevel@tonic-gate * an install. If this is the first string in the file we will 1360Sstevel@tonic-gate * automatically rebuild the file. 1370Sstevel@tonic-gate */ 1380Sstevel@tonic-gate #define PTI_MAGIC_STR "#path_to_inst_bootstrap_1" 1390Sstevel@tonic-gate #define PTI_MAGIC_STR_LEN (sizeof (PTI_MAGIC_STR) - 1) 1400Sstevel@tonic-gate 1410Sstevel@tonic-gate void 1420Sstevel@tonic-gate e_ddi_instance_init(void) 1430Sstevel@tonic-gate { 1440Sstevel@tonic-gate char *file; 1450Sstevel@tonic-gate int rebuild = 1; 1460Sstevel@tonic-gate struct in_drv *dp; 1470Sstevel@tonic-gate 1480Sstevel@tonic-gate mutex_init(&e_ddi_inst_state.ins_serial, NULL, MUTEX_DEFAULT, NULL); 1490Sstevel@tonic-gate cv_init(&e_ddi_inst_state.ins_serial_cv, NULL, CV_DEFAULT, NULL); 1500Sstevel@tonic-gate 1510Sstevel@tonic-gate /* 1520Sstevel@tonic-gate * Only one thread is allowed to change the state of the instance 1530Sstevel@tonic-gate * number assignments on the system at any given time. 1540Sstevel@tonic-gate * Note that this is not really necessary, as we are single-threaded 1550Sstevel@tonic-gate * here, but it won't hurt, and it allows us to keep ASSERTS for 1560Sstevel@tonic-gate * our assumptions in the code. 1570Sstevel@tonic-gate */ 1580Sstevel@tonic-gate e_ddi_enter_instance(); 1590Sstevel@tonic-gate 1600Sstevel@tonic-gate /* 161*12116SVikram.Hegde@Sun.COM * Init the ioaliases if the platform supports it 162*12116SVikram.Hegde@Sun.COM */ 163*12116SVikram.Hegde@Sun.COM if (&plat_ioaliases_init) 164*12116SVikram.Hegde@Sun.COM plat_ioaliases_init(); 165*12116SVikram.Hegde@Sun.COM 166*12116SVikram.Hegde@Sun.COM /* 1670Sstevel@tonic-gate * Create the root node, instance zallocs to 0. 1680Sstevel@tonic-gate * The name and address of this node never get examined, we always 1690Sstevel@tonic-gate * start searching with its first child. 1700Sstevel@tonic-gate */ 1710Sstevel@tonic-gate ASSERT(e_ddi_inst_state.ins_root == NULL); 1720Sstevel@tonic-gate e_ddi_inst_state.ins_root = in_alloc_node(NULL, NULL); 1730Sstevel@tonic-gate dp = in_alloc_drv("rootnex"); 1740Sstevel@tonic-gate in_endrv(e_ddi_inst_state.ins_root, dp); 1750Sstevel@tonic-gate 1760Sstevel@tonic-gate file = instance_file; 1770Sstevel@tonic-gate switch (in_get_infile(file)) { 1780Sstevel@tonic-gate default: 1790Sstevel@tonic-gate case PTI_NOT_FOUND: 1800Sstevel@tonic-gate /* make sure path_to_inst is recreated */ 1810Sstevel@tonic-gate boothowto |= RB_RECONFIG; 1820Sstevel@tonic-gate 1830Sstevel@tonic-gate /* 1840Sstevel@tonic-gate * Something is wrong. First try the backup file. 1850Sstevel@tonic-gate * If not found, rebuild path_to_inst. Emit a 1860Sstevel@tonic-gate * message about the problem. 1870Sstevel@tonic-gate */ 1880Sstevel@tonic-gate cmn_err(CE_WARN, "%s empty or not found", file); 1890Sstevel@tonic-gate 1900Sstevel@tonic-gate file = instance_file_backup; 1910Sstevel@tonic-gate if (in_get_infile(file) != PTI_FOUND) { 1920Sstevel@tonic-gate cmn_err(CE_NOTE, "rebuilding device instance data"); 1930Sstevel@tonic-gate break; 1940Sstevel@tonic-gate } 1950Sstevel@tonic-gate cmn_err(CE_NOTE, "using backup instance data in %s", file); 1960Sstevel@tonic-gate /*FALLTHROUGH*/ 1970Sstevel@tonic-gate 1980Sstevel@tonic-gate case PTI_FOUND: 1990Sstevel@tonic-gate /* 2000Sstevel@tonic-gate * We've got a readable file 2010Sstevel@tonic-gate * parse the file into the instance tree 2020Sstevel@tonic-gate */ 2030Sstevel@tonic-gate (void) read_binding_file(file, NULL, in_pathin); 2040Sstevel@tonic-gate rebuild = 0; 2050Sstevel@tonic-gate break; 2060Sstevel@tonic-gate 2070Sstevel@tonic-gate case PTI_REBUILD: 208*12116SVikram.Hegde@Sun.COM /* 209*12116SVikram.Hegde@Sun.COM * path_to_inst has magic str requesting a create 210*12116SVikram.Hegde@Sun.COM * Convert boot to reconfig boot to ensure /dev is 211*12116SVikram.Hegde@Sun.COM * in sync with new path_to_inst. 212*12116SVikram.Hegde@Sun.COM */ 213*12116SVikram.Hegde@Sun.COM boothowto |= RB_RECONFIG; 2140Sstevel@tonic-gate cmn_err(CE_CONT, 2155648Ssetje "?Using default device instance data\n"); 2160Sstevel@tonic-gate break; 2170Sstevel@tonic-gate } 2180Sstevel@tonic-gate 2190Sstevel@tonic-gate /* 2200Sstevel@tonic-gate * The OBP device tree has been copied to the kernel and 2210Sstevel@tonic-gate * bound to drivers at this point. We walk the per-driver 2220Sstevel@tonic-gate * list to preassign instances. Since the bus addr is 2230Sstevel@tonic-gate * unknown at this point, we cannot place the instance 2240Sstevel@tonic-gate * number in the instance tree. This will be done at 2250Sstevel@tonic-gate * a later time. 2260Sstevel@tonic-gate */ 2270Sstevel@tonic-gate if (rebuild) 2280Sstevel@tonic-gate in_preassign_instance(); 2290Sstevel@tonic-gate 2300Sstevel@tonic-gate e_ddi_exit_instance(); 2310Sstevel@tonic-gate } 2320Sstevel@tonic-gate 2330Sstevel@tonic-gate static void 2340Sstevel@tonic-gate in_preassign_instance() 2350Sstevel@tonic-gate { 2360Sstevel@tonic-gate major_t m; 2370Sstevel@tonic-gate extern major_t devcnt; 2380Sstevel@tonic-gate 2390Sstevel@tonic-gate for (m = 0; m < devcnt; m++) { 2400Sstevel@tonic-gate struct devnames *dnp = &devnamesp[m]; 2410Sstevel@tonic-gate dev_info_t *dip = dnp->dn_head; 2420Sstevel@tonic-gate while (dip) { 2430Sstevel@tonic-gate DEVI(dip)->devi_instance = dnp->dn_instance; 2440Sstevel@tonic-gate dnp->dn_instance++; 2450Sstevel@tonic-gate dip = ddi_get_next(dip); 2460Sstevel@tonic-gate } 2470Sstevel@tonic-gate } 2480Sstevel@tonic-gate } 2490Sstevel@tonic-gate 2500Sstevel@tonic-gate /* 2510Sstevel@tonic-gate * Checks to see if the /etc/path_to_inst file exists and whether or not 2520Sstevel@tonic-gate * it has the magic string in it. 2530Sstevel@tonic-gate * 2540Sstevel@tonic-gate * Returns one of the following: 2550Sstevel@tonic-gate * 2560Sstevel@tonic-gate * PTI_FOUND - We have found the /etc/path_to_inst file 2570Sstevel@tonic-gate * PTI_REBUILD - We have found the /etc/path_to_inst file and the 2580Sstevel@tonic-gate * first line was PTI_MAGIC_STR. 2590Sstevel@tonic-gate * PTI_NOT_FOUND - We did not find the /etc/path_to_inst file 2600Sstevel@tonic-gate * 2610Sstevel@tonic-gate */ 2620Sstevel@tonic-gate static int 2630Sstevel@tonic-gate in_get_infile(char *filename) 2640Sstevel@tonic-gate { 2655648Ssetje struct _buf *file; 2660Sstevel@tonic-gate int return_val; 2670Sstevel@tonic-gate char buf[PTI_MAGIC_STR_LEN]; 2680Sstevel@tonic-gate 2690Sstevel@tonic-gate /* 2700Sstevel@tonic-gate * Try to open the file. 2710Sstevel@tonic-gate */ 2725648Ssetje if ((file = kobj_open_file(filename)) == (struct _buf *)-1) { 2730Sstevel@tonic-gate return (PTI_NOT_FOUND); 2740Sstevel@tonic-gate } 2750Sstevel@tonic-gate return_val = PTI_FOUND; 2760Sstevel@tonic-gate 2770Sstevel@tonic-gate /* 2780Sstevel@tonic-gate * Read the first PTI_MAGIC_STR_LEN bytes from the file to see if 2790Sstevel@tonic-gate * it contains the magic string. If there aren't that many bytes 2800Sstevel@tonic-gate * in the file, then assume file is correct and no magic string 2810Sstevel@tonic-gate * and move on. 2820Sstevel@tonic-gate */ 2835648Ssetje switch (kobj_read_file(file, buf, PTI_MAGIC_STR_LEN, 0)) { 2840Sstevel@tonic-gate 2850Sstevel@tonic-gate case PTI_MAGIC_STR_LEN: 2860Sstevel@tonic-gate /* 2870Sstevel@tonic-gate * If the first PTI_MAGIC_STR_LEN bytes are the magic string 2880Sstevel@tonic-gate * then return PTI_REBUILD. 2890Sstevel@tonic-gate */ 2900Sstevel@tonic-gate if (strncmp(PTI_MAGIC_STR, buf, PTI_MAGIC_STR_LEN) == 0) 2910Sstevel@tonic-gate return_val = PTI_REBUILD; 2920Sstevel@tonic-gate break; 2930Sstevel@tonic-gate 2940Sstevel@tonic-gate case 0: 2950Sstevel@tonic-gate /* 2960Sstevel@tonic-gate * If the file is zero bytes in length, then consider the 2970Sstevel@tonic-gate * file to not be found 2980Sstevel@tonic-gate */ 2990Sstevel@tonic-gate return_val = PTI_NOT_FOUND; 3000Sstevel@tonic-gate 3010Sstevel@tonic-gate default: /* Do nothing we have a good file */ 3020Sstevel@tonic-gate break; 3030Sstevel@tonic-gate } 3040Sstevel@tonic-gate 3055648Ssetje kobj_close_file(file); 3060Sstevel@tonic-gate return (return_val); 3070Sstevel@tonic-gate } 3080Sstevel@tonic-gate 3090Sstevel@tonic-gate int 3100Sstevel@tonic-gate is_pseudo_device(dev_info_t *dip) 3110Sstevel@tonic-gate { 3120Sstevel@tonic-gate dev_info_t *pdip; 3130Sstevel@tonic-gate 3140Sstevel@tonic-gate for (pdip = ddi_get_parent(dip); pdip && pdip != ddi_root_node(); 3150Sstevel@tonic-gate pdip = ddi_get_parent(pdip)) { 3160Sstevel@tonic-gate if (strcmp(ddi_get_name(pdip), DEVI_PSEUDO_NEXNAME) == 0) 3170Sstevel@tonic-gate return (1); 3180Sstevel@tonic-gate } 3190Sstevel@tonic-gate return (0); 3200Sstevel@tonic-gate } 3210Sstevel@tonic-gate 3220Sstevel@tonic-gate 3230Sstevel@tonic-gate static void 3240Sstevel@tonic-gate in_set_instance(dev_info_t *dip, in_drv_t *dp, major_t major) 3250Sstevel@tonic-gate { 3260Sstevel@tonic-gate /* use preassigned instance if available */ 3270Sstevel@tonic-gate if (DEVI(dip)->devi_instance != -1) 3280Sstevel@tonic-gate dp->ind_instance = DEVI(dip)->devi_instance; 3290Sstevel@tonic-gate else 3300Sstevel@tonic-gate dp->ind_instance = in_next_instance(major); 3310Sstevel@tonic-gate } 3320Sstevel@tonic-gate 3330Sstevel@tonic-gate /* 3343250Scth * Return 1 if instance block was assigned for the path. 3353250Scth * 3363250Scth * For multi-port NIC cards, sequential instance assignment across all 3378011SChris.Horne@Sun.COM * ports on a card is highly desirable since the ppa is typically the 3383250Scth * same as the instance number, and the ppa is used in the NIC's public 3393250Scth * /dev name. This sequential assignment typically occurs as a result 3403250Scth * of in_preassign_instance() after initial install, or by 3413250Scth * i_ndi_init_hw_children() for NIC ports that share a common parent. 3423250Scth * 3433250Scth * Some NIC cards however use multi-function bridge chips, and to 3443250Scth * support sequential instance assignment accross all ports, without 3453250Scth * disabling multi-threaded attach, we have a (currently) undocumented 3463250Scth * hack to allocate instance numbers in contiguous blocks based on 3473250Scth * driver.conf properties. 3483250Scth * 3493250Scth * ^ 3503250Scth * /---------- ------------\ 3513250Scth * pci@0 pci@0,1 MULTI-FUNCTION BRIDGE CHIP 3523250Scth * / \ / \ 3533250Scth * FJSV,e4ta@4 FJSV,e4ta@4,1 FJSV,e4ta@6 FJSV,e4ta@6,1 NIC PORTS 3543250Scth * n n+2 n+2 n+3 INSTANCE 3553250Scth * 3563250Scth * For the above example, the following driver.conf properties would be 3573250Scth * used to guarantee sequential instance number assignment. 3583250Scth * 3593250Scth * ddi-instance-blocks ="ib-FJSVe4ca", "ib-FJSVe4ta", "ib-generic"; 3603250Scth * ib-FJSVe4ca = "/pci@0/FJSV,e4ca@4", "/pci@0/FJSV,e4ca@4,1", 3613250Scth * "/pci@0,1/FJSV,e4ca@6", "/pci@0,1/FJSV,e4ca@6,1"; 3623250Scth * ib-FJSVe4ta = "/pci@0/FJSV,e4ta@4", "/pci@0/FJSV,e4ta@4,1", 3633250Scth * "/pci@0,1/FJSV,e4ta@6", "/pci@0,1/FJSV,e4ta@6,1"; 3643250Scth * ib-generic = "/pci@0/network@4", "/pci@0/network@4,1", 3653250Scth * "/pci@0,1/network@6", "/pci@0,1/network@6,1"; 3663250Scth * 3673250Scth * The value of the 'ddi-instance-blocks' property references a series 3683250Scth * of card specific properties, like 'ib-FJSV-e4ta', who's value 3693250Scth * defines a single 'instance block'. The 'instance block' describes 3703250Scth * all the paths below a multi-function bridge, where each path is 3713250Scth * called an 'instance path'. The 'instance block' property value is a 3723250Scth * series of 'instance paths'. The number of 'instance paths' in an 3733250Scth * 'instance block' defines the size of the instance block, and the 3743250Scth * ordering of the 'instance paths' defines the instance number 3753250Scth * assignment order for paths going through the 'instance block'. 3763250Scth * 3773250Scth * In the instance assignment code below, if a (path, driver) that 3783250Scth * currently has no instance number has a path that goes through an 3793250Scth * 'instance block', then block instance number allocation occurs. The 3803250Scth * block allocation code will find a sequential set of unused instance 3813250Scth * numbers, and assign instance numbers for all the paths in the 3823250Scth * 'instance block'. Each path is assigned a persistent instance 3833250Scth * number, even paths that don't exist in the device tree or fail 3843250Scth * probe(9E). 3853250Scth */ 3863250Scth static int 3873250Scth in_assign_instance_block(dev_info_t *dip) 3883250Scth { 3893250Scth char **ibn; /* instance block names */ 3903250Scth uint_t nibn; /* number of instance block names */ 3913250Scth uint_t ibni; /* ibn index */ 3923250Scth char *driver; 3933250Scth major_t major; 3943250Scth char *path; 3953250Scth char *addr; 3963250Scth int plen; 3973250Scth char **ibp; /* instance block paths */ 3983250Scth uint_t nibp; /* number of paths in instance block */ 3993250Scth uint_t ibpi; /* ibp index */ 4003250Scth int ibplen; /* length of instance block path */ 4013250Scth char *ipath; 4023250Scth int instance_base; 4033250Scth int splice; 4043250Scth int i; 4053250Scth 4063250Scth /* check for fresh install case (in miniroot) */ 4073250Scth if (DEVI(dip)->devi_instance != -1) 4083250Scth return (0); /* already assigned */ 4093250Scth 4103250Scth /* 4113250Scth * Check to see if we need to allocate a block of contiguous instance 4123250Scth * numbers by looking for the 'ddi-instance-blocks' property. 4133250Scth */ 4143250Scth if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, 4153250Scth "ddi-instance-blocks", &ibn, &nibn) != DDI_SUCCESS) 4163250Scth return (0); /* no instance block needed */ 4173250Scth 4183250Scth /* 4193250Scth * Get information out about node we are processing. 4203250Scth * 4213250Scth * NOTE: Since the node is not yet at DS_INITIALIZED, ddi_pathname() 4223250Scth * will not return the unit-address of the final path component even 4233250Scth * though the node has an established devi_addr unit-address - so we 4243250Scth * need to add the unit-address by hand. 4253250Scth */ 4263250Scth driver = (char *)ddi_driver_name(dip); 4273250Scth major = ddi_driver_major(dip); 4283250Scth path = kmem_alloc(MAXPATHLEN, KM_SLEEP); 4293250Scth (void) ddi_pathname(dip, path); 4303250Scth if ((addr = ddi_get_name_addr(dip)) != NULL) { 4313250Scth (void) strcat(path, "@"); 4323250Scth (void) strcat(path, addr); 4333250Scth } 4343250Scth plen = strlen(path); 4353250Scth 4363250Scth /* loop through instance block names */ 4373250Scth for (ibni = 0; ibni < nibn; ibni++) { 4383250Scth if (ibn[ibni] == NULL) 4393250Scth continue; 4403250Scth 4413250Scth /* lookup instance block */ 4423250Scth if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, dip, 4433250Scth DDI_PROP_DONTPASS, ibn[ibni], 4443250Scth &ibp, &nibp) != DDI_SUCCESS) { 4453250Scth cmn_err(CE_WARN, 4463250Scth "no devinition for instance block '%s' in %s.conf", 4473250Scth ibn[ibni], driver); 4483250Scth continue; 4493250Scth } 4503250Scth 4513250Scth /* Does 'path' go through this instance block? */ 4523250Scth for (ibpi = 0; ibpi < nibp; ibpi++) { 4533250Scth if (ibp[ibpi] == NULL) 4543250Scth continue; 4553250Scth ibplen = strlen(ibp[ibpi]); 4563250Scth if ((ibplen <= plen) && 4573250Scth (strcmp(ibp[ibpi], path + plen - ibplen) == 0)) 4583250Scth break; 4593250Scth 4603250Scth } 4613250Scth if (ibpi >= nibp) { 4623250Scth ddi_prop_free(ibp); 4633250Scth continue; /* no try next instance block */ 4643250Scth } 4653250Scth 4663250Scth /* yes, allocate and assign instances for all paths in block */ 4673250Scth 4683250Scth /* 4693250Scth * determine where we splice in instance paths and verify 4703250Scth * that none of the paths are too long. 4713250Scth */ 4723250Scth splice = plen - ibplen; 4733250Scth for (i = 0; i < nibp; i++) { 4743250Scth if ((splice + strlen(ibp[i])+ 1) >= MAXPATHLEN) { 4753250Scth cmn_err(CE_WARN, 4763250Scth "path %d through instance block '%s' from " 4773250Scth "%s.conf too long", i, ibn[ibni], driver); 4783250Scth break; 4793250Scth } 4803250Scth } 4813250Scth if (i < nibp) { 4823250Scth ddi_prop_free(ibp); 4833250Scth continue; /* too long */ 4843250Scth } 4853250Scth 4863250Scth /* allocate the instance block - no more failures */ 4873250Scth instance_base = in_next_instance_block(major, nibp); 4883250Scth 4893250Scth ipath = kmem_alloc(MAXPATHLEN, KM_SLEEP); 4903250Scth for (ibpi = 0; ibpi < nibp; ibpi++) { 4913250Scth if (ibp[ibpi] == NULL) 4923250Scth continue; 4933250Scth (void) strcpy(ipath, path); 4943250Scth (void) strcpy(ipath + splice, ibp[ibpi]); 4953250Scth (void) in_pathin(ipath, 4963250Scth instance_base + ibpi, driver, NULL); 4973250Scth } 4983250Scth 4993250Scth /* free allocations */ 5003250Scth kmem_free(ipath, MAXPATHLEN); 5013250Scth ddi_prop_free(ibp); 5023250Scth kmem_free(path, MAXPATHLEN); 5033250Scth ddi_prop_free(ibn); 5043250Scth 5053250Scth /* notify devfsadmd to sync of path_to_inst file */ 5063250Scth mutex_enter(&e_ddi_inst_state.ins_serial); 5073250Scth i_log_devfs_instance_mod(); 508*12116SVikram.Hegde@Sun.COM e_ddi_inst_state.ins_dirty = B_TRUE; 5093250Scth mutex_exit(&e_ddi_inst_state.ins_serial); 5103250Scth return (1); 5113250Scth } 5123250Scth 5133250Scth /* our path did not go through any of of the instance blocks */ 5143250Scth kmem_free(path, MAXPATHLEN); 5153250Scth ddi_prop_free(ibn); 5163250Scth return (0); 5173250Scth } 5183250Scth 5193250Scth /* 5200Sstevel@tonic-gate * Look up an instance number for a dev_info node, and assign one if it does 5210Sstevel@tonic-gate * not have one (the dev_info node has devi_name and devi_addr already set). 5220Sstevel@tonic-gate */ 5230Sstevel@tonic-gate uint_t 5240Sstevel@tonic-gate e_ddi_assign_instance(dev_info_t *dip) 5250Sstevel@tonic-gate { 5260Sstevel@tonic-gate char *name; 5270Sstevel@tonic-gate in_node_t *ap, *np; 5280Sstevel@tonic-gate in_drv_t *dp; 5290Sstevel@tonic-gate major_t major; 5300Sstevel@tonic-gate uint_t ret; 5310Sstevel@tonic-gate char *bname; 5320Sstevel@tonic-gate 5330Sstevel@tonic-gate /* 5340Sstevel@tonic-gate * Allow implementation to override 5350Sstevel@tonic-gate */ 5360Sstevel@tonic-gate if ((ret = impl_assign_instance(dip)) != (uint_t)-1) 5370Sstevel@tonic-gate return (ret); 5380Sstevel@tonic-gate 5390Sstevel@tonic-gate /* 5400Sstevel@tonic-gate * If this is a pseudo-device, use the instance number 5410Sstevel@tonic-gate * assigned by the pseudo nexus driver. The mutex is 5420Sstevel@tonic-gate * not needed since the instance tree is not used. 5430Sstevel@tonic-gate */ 5440Sstevel@tonic-gate if (is_pseudo_device(dip)) { 5450Sstevel@tonic-gate return (ddi_get_instance(dip)); 5460Sstevel@tonic-gate } 5470Sstevel@tonic-gate 5480Sstevel@tonic-gate /* 5490Sstevel@tonic-gate * Only one thread is allowed to change the state of the instance 5500Sstevel@tonic-gate * number assignments on the system at any given time. 5510Sstevel@tonic-gate */ 5520Sstevel@tonic-gate e_ddi_enter_instance(); 5530Sstevel@tonic-gate 5540Sstevel@tonic-gate /* 5550Sstevel@tonic-gate * Look for instance node, allocate one if not found 5560Sstevel@tonic-gate */ 5570Sstevel@tonic-gate np = in_devwalk(dip, &ap, NULL); 5580Sstevel@tonic-gate if (np == NULL) { 5593250Scth if (in_assign_instance_block(dip)) { 5603250Scth np = in_devwalk(dip, &ap, NULL); 5613250Scth } else { 5623250Scth name = ddi_node_name(dip); 5633250Scth np = in_alloc_node(name, ddi_get_name_addr(dip)); 5643250Scth ASSERT(np != NULL); 5653250Scth in_enlist(ap, np); /* insert into tree */ 5663250Scth } 5670Sstevel@tonic-gate } 5680Sstevel@tonic-gate ASSERT(np == in_devwalk(dip, &ap, NULL)); 5690Sstevel@tonic-gate 5700Sstevel@tonic-gate /* 571*12116SVikram.Hegde@Sun.COM * Link the devinfo node and in_node_t 572*12116SVikram.Hegde@Sun.COM */ 573*12116SVikram.Hegde@Sun.COM if (DEVI(dip)->devi_in_node || np->in_devi) { 574*12116SVikram.Hegde@Sun.COM ddi_err(DER_MODE, dip, "devinfo and instance node (%p) " 575*12116SVikram.Hegde@Sun.COM "interlink fields are not NULL", (void *)np); 576*12116SVikram.Hegde@Sun.COM } 577*12116SVikram.Hegde@Sun.COM DEVI(dip)->devi_in_node = np; 578*12116SVikram.Hegde@Sun.COM np->in_devi = dip; 579*12116SVikram.Hegde@Sun.COM 580*12116SVikram.Hegde@Sun.COM /* 5810Sstevel@tonic-gate * Look for driver entry, allocate one if not found 5820Sstevel@tonic-gate */ 5830Sstevel@tonic-gate bname = (char *)ddi_driver_name(dip); 5840Sstevel@tonic-gate dp = in_drvwalk(np, bname); 5850Sstevel@tonic-gate if (dp == NULL) { 586*12116SVikram.Hegde@Sun.COM 587*12116SVikram.Hegde@Sun.COM if (ddi_aliases_present == B_TRUE) { 588*12116SVikram.Hegde@Sun.COM e_ddi_borrow_instance(dip, np); 589*12116SVikram.Hegde@Sun.COM } 590*12116SVikram.Hegde@Sun.COM 591*12116SVikram.Hegde@Sun.COM if ((dp = in_drvwalk(np, bname)) == NULL) { 592*12116SVikram.Hegde@Sun.COM dp = in_alloc_drv(bname); 593*12116SVikram.Hegde@Sun.COM ASSERT(dp != NULL); 594*12116SVikram.Hegde@Sun.COM major = ddi_driver_major(dip); 595*12116SVikram.Hegde@Sun.COM ASSERT(major != DDI_MAJOR_T_NONE); 596*12116SVikram.Hegde@Sun.COM in_endrv(np, dp); 597*12116SVikram.Hegde@Sun.COM in_set_instance(dip, dp, major); 598*12116SVikram.Hegde@Sun.COM dp->ind_state = IN_PROVISIONAL; 599*12116SVikram.Hegde@Sun.COM in_hashdrv(dp); 600*12116SVikram.Hegde@Sun.COM } else { 601*12116SVikram.Hegde@Sun.COM dp->ind_state = IN_BORROWED; 602*12116SVikram.Hegde@Sun.COM } 6030Sstevel@tonic-gate } 6040Sstevel@tonic-gate 6050Sstevel@tonic-gate ret = dp->ind_instance; 6060Sstevel@tonic-gate 6070Sstevel@tonic-gate e_ddi_exit_instance(); 6080Sstevel@tonic-gate return (ret); 6090Sstevel@tonic-gate } 6100Sstevel@tonic-gate 6110Sstevel@tonic-gate static int 6120Sstevel@tonic-gate mkpathname(char *path, in_node_t *np, int len) 6130Sstevel@tonic-gate { 6140Sstevel@tonic-gate int len_needed; 6150Sstevel@tonic-gate 6160Sstevel@tonic-gate if (np == e_ddi_inst_state.ins_root) 6170Sstevel@tonic-gate return (DDI_SUCCESS); 6180Sstevel@tonic-gate 6190Sstevel@tonic-gate if (mkpathname(path, np->in_parent, len) == DDI_FAILURE) 6200Sstevel@tonic-gate return (DDI_FAILURE); 6210Sstevel@tonic-gate 6220Sstevel@tonic-gate len_needed = strlen(path); 6230Sstevel@tonic-gate len_needed += strlen(np->in_node_name) + 1; /* for '/' */ 6240Sstevel@tonic-gate if (np->in_unit_addr) { 6250Sstevel@tonic-gate len_needed += strlen(np->in_unit_addr) + 1; /* for '@' */ 6260Sstevel@tonic-gate } 6270Sstevel@tonic-gate len_needed += 1; /* for '\0' */ 6280Sstevel@tonic-gate 6290Sstevel@tonic-gate /* 6300Sstevel@tonic-gate * XX complain 6310Sstevel@tonic-gate */ 6320Sstevel@tonic-gate if (len_needed > len) 6330Sstevel@tonic-gate return (DDI_FAILURE); 6340Sstevel@tonic-gate 6350Sstevel@tonic-gate if (np->in_unit_addr[0] == '\0') 6360Sstevel@tonic-gate (void) sprintf(path+strlen(path), "/%s", np->in_node_name); 6370Sstevel@tonic-gate else 6380Sstevel@tonic-gate (void) sprintf(path+strlen(path), "/%s@%s", np->in_node_name, 6390Sstevel@tonic-gate np->in_unit_addr); 6400Sstevel@tonic-gate 6410Sstevel@tonic-gate return (DDI_SUCCESS); 6420Sstevel@tonic-gate } 6430Sstevel@tonic-gate 6440Sstevel@tonic-gate /* 6450Sstevel@tonic-gate * produce the path to the given instance of a major number. 6460Sstevel@tonic-gate * path must hold MAXPATHLEN string 6470Sstevel@tonic-gate */ 6480Sstevel@tonic-gate int 6490Sstevel@tonic-gate e_ddi_instance_majorinstance_to_path(major_t major, uint_t inst, char *path) 6500Sstevel@tonic-gate { 6510Sstevel@tonic-gate struct devnames *dnp; 6520Sstevel@tonic-gate in_drv_t *dp; 6530Sstevel@tonic-gate int ret; 6540Sstevel@tonic-gate 6550Sstevel@tonic-gate e_ddi_enter_instance(); 6560Sstevel@tonic-gate 6570Sstevel@tonic-gate /* look for the instance threaded off major */ 6580Sstevel@tonic-gate dnp = &devnamesp[major]; 6590Sstevel@tonic-gate for (dp = dnp->dn_inlist; dp != NULL; dp = dp->ind_next) 6600Sstevel@tonic-gate if (dp->ind_instance == inst) 6610Sstevel@tonic-gate break; 6620Sstevel@tonic-gate 6630Sstevel@tonic-gate /* produce path from the node that uses the instance */ 6640Sstevel@tonic-gate if (dp) { 6650Sstevel@tonic-gate *path = 0; 6660Sstevel@tonic-gate ret = mkpathname(path, dp->ind_node, MAXPATHLEN); 6670Sstevel@tonic-gate } else 6680Sstevel@tonic-gate ret = DDI_FAILURE; 6690Sstevel@tonic-gate 6700Sstevel@tonic-gate e_ddi_exit_instance(); 6710Sstevel@tonic-gate return (ret); 6720Sstevel@tonic-gate } 6730Sstevel@tonic-gate 6740Sstevel@tonic-gate /* 6753250Scth * Allocate a sequential block of instance numbers for the specified driver, 6763250Scth * and return the base instance number of the block. The implementation 6773250Scth * depends on the list being sorted in ascending instance number sequence. 6783250Scth * When there are no 'holes' in the allocation sequence, dn_instance is the 6793250Scth * next available instance number. When dn_instance is IN_SEARCHME, hole(s) 6803250Scth * exists and a slower code path executes which tries to fill holes. 6810Sstevel@tonic-gate */ 6820Sstevel@tonic-gate static int 6833250Scth in_next_instance_block(major_t major, int block_size) 6840Sstevel@tonic-gate { 6853250Scth unsigned int prev; 6863250Scth struct devnames *dnp; 6873250Scth in_drv_t *dp; 6883250Scth int base; 6893250Scth int hole; 6900Sstevel@tonic-gate 6910Sstevel@tonic-gate dnp = &devnamesp[major]; 6927009Scth ASSERT(major != DDI_MAJOR_T_NONE); 6930Sstevel@tonic-gate ASSERT(e_ddi_inst_state.ins_busy); 6943250Scth ASSERT(block_size); 6953250Scth 6963250Scth /* check to see if we can do a quick allocation */ 6973250Scth if (dnp->dn_instance != IN_SEARCHME) { 6983250Scth base = dnp->dn_instance; 6993250Scth dnp->dn_instance += block_size; 7003250Scth return (base); 7013250Scth } 7020Sstevel@tonic-gate dp = dnp->dn_inlist; 7030Sstevel@tonic-gate 7043250Scth /* no existing entries, allocate block at 0 */ 7050Sstevel@tonic-gate if (dp == NULL) { 7063250Scth dnp->dn_instance = block_size; 7070Sstevel@tonic-gate return (0); 7080Sstevel@tonic-gate } 7090Sstevel@tonic-gate 7100Sstevel@tonic-gate prev = dp->ind_instance; 7113250Scth if (prev >= block_size) 7123250Scth return (0); /* we fit in hole at beginning */ 7133250Scth 7143250Scth /* search the list for a large enough hole */ 7153250Scth for (dp = dp->ind_next, hole = 0; dp; dp = dp->ind_next) { 7163250Scth if (dp->ind_instance != (prev + 1)) 7173250Scth hole++; /* we have a hole */ 7183250Scth if (dp->ind_instance >= (prev + block_size + 1)) 7193250Scth break; /* we fit in hole */ 7203250Scth prev = dp->ind_instance; 7210Sstevel@tonic-gate } 7223250Scth 7230Sstevel@tonic-gate /* 7243250Scth * If hole is zero then all holes are patched and we can resume 7253250Scth * quick allocations. 7260Sstevel@tonic-gate */ 7273250Scth if (hole == 0) 7283250Scth dnp->dn_instance = prev + 1 + block_size; 7293250Scth 7303250Scth return (prev + 1); 7313250Scth } 7320Sstevel@tonic-gate 7333250Scth /* assign instance block of size 1 */ 7343250Scth static int 7353250Scth in_next_instance(major_t major) 7363250Scth { 7373250Scth return (in_next_instance_block(major, 1)); 7380Sstevel@tonic-gate } 7390Sstevel@tonic-gate 7400Sstevel@tonic-gate /* 7410Sstevel@tonic-gate * This call causes us to *forget* the instance number we've generated 7420Sstevel@tonic-gate * for a given device if it was not permanent. 7430Sstevel@tonic-gate */ 7440Sstevel@tonic-gate void 7450Sstevel@tonic-gate e_ddi_free_instance(dev_info_t *dip, char *addr) 7460Sstevel@tonic-gate { 7470Sstevel@tonic-gate char *name; 7480Sstevel@tonic-gate in_node_t *np; 7490Sstevel@tonic-gate in_node_t *ap; /* ancestor node */ 7500Sstevel@tonic-gate major_t major; 7510Sstevel@tonic-gate struct devnames *dnp; 7520Sstevel@tonic-gate in_drv_t *dp; /* in_drv entry */ 7530Sstevel@tonic-gate 7540Sstevel@tonic-gate /* 7550Sstevel@tonic-gate * Allow implementation override 7560Sstevel@tonic-gate */ 7570Sstevel@tonic-gate if (impl_free_instance(dip) == DDI_SUCCESS) 7580Sstevel@tonic-gate return; 7590Sstevel@tonic-gate 7600Sstevel@tonic-gate /* 7610Sstevel@tonic-gate * If this is a pseudo-device, no instance number 7620Sstevel@tonic-gate * was assigned. 7630Sstevel@tonic-gate */ 7640Sstevel@tonic-gate if (is_pseudo_device(dip)) { 7650Sstevel@tonic-gate return; 7660Sstevel@tonic-gate } 7670Sstevel@tonic-gate 7680Sstevel@tonic-gate name = (char *)ddi_driver_name(dip); 7690Sstevel@tonic-gate major = ddi_driver_major(dip); 7707009Scth ASSERT(major != DDI_MAJOR_T_NONE); 7710Sstevel@tonic-gate dnp = &devnamesp[major]; 7720Sstevel@tonic-gate /* 7730Sstevel@tonic-gate * Only one thread is allowed to change the state of the instance 7740Sstevel@tonic-gate * number assignments on the system at any given time. 7750Sstevel@tonic-gate */ 7760Sstevel@tonic-gate e_ddi_enter_instance(); 7770Sstevel@tonic-gate np = in_devwalk(dip, &ap, addr); 7780Sstevel@tonic-gate ASSERT(np); 779*12116SVikram.Hegde@Sun.COM 780*12116SVikram.Hegde@Sun.COM /* 781*12116SVikram.Hegde@Sun.COM * Break the interlink between dip and np 782*12116SVikram.Hegde@Sun.COM */ 783*12116SVikram.Hegde@Sun.COM if (DEVI(dip)->devi_in_node != np || np->in_devi != dip) { 784*12116SVikram.Hegde@Sun.COM ddi_err(DER_MODE, dip, "devinfo node linked to " 785*12116SVikram.Hegde@Sun.COM "wrong instance node: %p", (void *)np); 786*12116SVikram.Hegde@Sun.COM } 787*12116SVikram.Hegde@Sun.COM DEVI(dip)->devi_in_node = NULL; 788*12116SVikram.Hegde@Sun.COM np->in_devi = NULL; 789*12116SVikram.Hegde@Sun.COM 7900Sstevel@tonic-gate dp = in_drvwalk(np, name); 7910Sstevel@tonic-gate ASSERT(dp); 7920Sstevel@tonic-gate if (dp->ind_state == IN_PROVISIONAL) { 7930Sstevel@tonic-gate in_removedrv(dnp, dp); 7940Sstevel@tonic-gate } 795*12116SVikram.Hegde@Sun.COM if (dp->ind_state == IN_BORROWED) { 796*12116SVikram.Hegde@Sun.COM dp->ind_state = IN_PERMANENT; 797*12116SVikram.Hegde@Sun.COM e_ddi_return_instance(dip, addr, np); 798*12116SVikram.Hegde@Sun.COM } 7990Sstevel@tonic-gate if (np->in_drivers == NULL) { 8000Sstevel@tonic-gate in_removenode(dnp, np, ap); 8010Sstevel@tonic-gate } 8020Sstevel@tonic-gate e_ddi_exit_instance(); 8030Sstevel@tonic-gate } 8040Sstevel@tonic-gate 8050Sstevel@tonic-gate /* 8060Sstevel@tonic-gate * This makes our memory of an instance assignment permanent 8070Sstevel@tonic-gate */ 8080Sstevel@tonic-gate void 8090Sstevel@tonic-gate e_ddi_keep_instance(dev_info_t *dip) 8100Sstevel@tonic-gate { 8110Sstevel@tonic-gate in_node_t *np, *ap; 8120Sstevel@tonic-gate in_drv_t *dp; 8130Sstevel@tonic-gate 8148011SChris.Horne@Sun.COM /* Don't make nulldriver instance assignments permanent */ 8158011SChris.Horne@Sun.COM if (ddi_driver_major(dip) == nulldriver_major) 8168011SChris.Horne@Sun.COM return; 8178011SChris.Horne@Sun.COM 8180Sstevel@tonic-gate /* 8190Sstevel@tonic-gate * Allow implementation override 8200Sstevel@tonic-gate */ 8210Sstevel@tonic-gate if (impl_keep_instance(dip) == DDI_SUCCESS) 8220Sstevel@tonic-gate return; 8230Sstevel@tonic-gate 8240Sstevel@tonic-gate /* 8250Sstevel@tonic-gate * Nothing to do for pseudo devices. 8260Sstevel@tonic-gate */ 8270Sstevel@tonic-gate if (is_pseudo_device(dip)) 8280Sstevel@tonic-gate return; 8290Sstevel@tonic-gate 8300Sstevel@tonic-gate /* 8310Sstevel@tonic-gate * Only one thread is allowed to change the state of the instance 8320Sstevel@tonic-gate * number assignments on the system at any given time. 8330Sstevel@tonic-gate */ 8340Sstevel@tonic-gate e_ddi_enter_instance(); 8350Sstevel@tonic-gate np = in_devwalk(dip, &ap, NULL); 8360Sstevel@tonic-gate ASSERT(np); 8370Sstevel@tonic-gate dp = in_drvwalk(np, (char *)ddi_driver_name(dip)); 8380Sstevel@tonic-gate ASSERT(dp); 8390Sstevel@tonic-gate 8400Sstevel@tonic-gate mutex_enter(&e_ddi_inst_state.ins_serial); 841*12116SVikram.Hegde@Sun.COM if (dp->ind_state == IN_PROVISIONAL || dp->ind_state == IN_BORROWED) { 8420Sstevel@tonic-gate dp->ind_state = IN_PERMANENT; 8430Sstevel@tonic-gate i_log_devfs_instance_mod(); 844*12116SVikram.Hegde@Sun.COM e_ddi_inst_state.ins_dirty = B_TRUE; 8450Sstevel@tonic-gate } 8460Sstevel@tonic-gate mutex_exit(&e_ddi_inst_state.ins_serial); 8470Sstevel@tonic-gate e_ddi_exit_instance(); 8480Sstevel@tonic-gate } 8490Sstevel@tonic-gate 8500Sstevel@tonic-gate /* 8510Sstevel@tonic-gate * A new major has been added to the system. Run through the orphan list 8520Sstevel@tonic-gate * and try to attach each one to a driver's list. 8530Sstevel@tonic-gate */ 8540Sstevel@tonic-gate void 8550Sstevel@tonic-gate e_ddi_unorphan_instance_nos() 8560Sstevel@tonic-gate { 8570Sstevel@tonic-gate in_drv_t *dp, *ndp; 8580Sstevel@tonic-gate 8590Sstevel@tonic-gate /* 8600Sstevel@tonic-gate * disconnect the orphan list, and call in_hashdrv for each item 8610Sstevel@tonic-gate * on it 8620Sstevel@tonic-gate */ 8630Sstevel@tonic-gate 8640Sstevel@tonic-gate /* 8650Sstevel@tonic-gate * Only one thread is allowed to change the state of the instance 8660Sstevel@tonic-gate * number assignments on the system at any given time. 8670Sstevel@tonic-gate */ 8680Sstevel@tonic-gate e_ddi_enter_instance(); 8690Sstevel@tonic-gate if (e_ddi_inst_state.ins_no_major == NULL) { 8700Sstevel@tonic-gate e_ddi_exit_instance(); 8710Sstevel@tonic-gate return; 8720Sstevel@tonic-gate } 8730Sstevel@tonic-gate /* 8740Sstevel@tonic-gate * Hash instance list to devnames structure of major. 8750Sstevel@tonic-gate * Note that if there is not a valid major number for the 8760Sstevel@tonic-gate * node, in_hashdrv will put it back on the no_major list. 8770Sstevel@tonic-gate */ 8780Sstevel@tonic-gate dp = e_ddi_inst_state.ins_no_major; 8790Sstevel@tonic-gate e_ddi_inst_state.ins_no_major = NULL; 8800Sstevel@tonic-gate while (dp) { 8810Sstevel@tonic-gate ndp = dp->ind_next; 8820Sstevel@tonic-gate ASSERT(dp->ind_state != IN_UNKNOWN); 8830Sstevel@tonic-gate dp->ind_next = NULL; 8840Sstevel@tonic-gate in_hashdrv(dp); 8850Sstevel@tonic-gate dp = ndp; 8860Sstevel@tonic-gate } 8870Sstevel@tonic-gate e_ddi_exit_instance(); 8880Sstevel@tonic-gate } 8890Sstevel@tonic-gate 8900Sstevel@tonic-gate static void 8910Sstevel@tonic-gate in_removenode(struct devnames *dnp, in_node_t *mp, in_node_t *ap) 8920Sstevel@tonic-gate { 8930Sstevel@tonic-gate in_node_t *np; 8940Sstevel@tonic-gate 8950Sstevel@tonic-gate ASSERT(e_ddi_inst_state.ins_busy); 896*12116SVikram.Hegde@Sun.COM 8970Sstevel@tonic-gate /* 8980Sstevel@tonic-gate * Assertion: parents are always instantiated by the framework 8990Sstevel@tonic-gate * before their children, destroyed after them 9000Sstevel@tonic-gate */ 9010Sstevel@tonic-gate ASSERT(mp->in_child == NULL); 9020Sstevel@tonic-gate /* 9030Sstevel@tonic-gate * Assertion: drv entries are always removed before their owning nodes 9040Sstevel@tonic-gate */ 9050Sstevel@tonic-gate ASSERT(mp->in_drivers == NULL); 9060Sstevel@tonic-gate /* 9070Sstevel@tonic-gate * Take the node out of the tree 9080Sstevel@tonic-gate */ 9090Sstevel@tonic-gate if (ap->in_child == mp) { 9100Sstevel@tonic-gate ap->in_child = mp->in_sibling; 9110Sstevel@tonic-gate in_dealloc_node(mp); 9120Sstevel@tonic-gate return; 9130Sstevel@tonic-gate } else { 9140Sstevel@tonic-gate for (np = ap->in_child; np; np = np->in_sibling) { 9150Sstevel@tonic-gate if (np->in_sibling == mp) { 9160Sstevel@tonic-gate np->in_sibling = mp->in_sibling; 9170Sstevel@tonic-gate in_dealloc_node(mp); 9180Sstevel@tonic-gate return; 9190Sstevel@tonic-gate } 9200Sstevel@tonic-gate } 9210Sstevel@tonic-gate } 9220Sstevel@tonic-gate panic("in_removenode dnp %p mp %p", (void *)dnp, (void *)mp); 9230Sstevel@tonic-gate } 9240Sstevel@tonic-gate 9250Sstevel@tonic-gate /* 9260Sstevel@tonic-gate * Recursive ascent 9270Sstevel@tonic-gate * 9280Sstevel@tonic-gate * This now only does half the job. It finds the node, then the caller 9290Sstevel@tonic-gate * has to search the node for the binding name 9300Sstevel@tonic-gate */ 9310Sstevel@tonic-gate static in_node_t * 9320Sstevel@tonic-gate in_devwalk(dev_info_t *dip, in_node_t **ap, char *addr) 9330Sstevel@tonic-gate { 9340Sstevel@tonic-gate in_node_t *np; 9350Sstevel@tonic-gate char *name; 9360Sstevel@tonic-gate 9370Sstevel@tonic-gate ASSERT(dip); 9380Sstevel@tonic-gate ASSERT(e_ddi_inst_state.ins_busy); 9390Sstevel@tonic-gate if (dip == ddi_root_node()) { 9400Sstevel@tonic-gate *ap = NULL; 9410Sstevel@tonic-gate return (e_ddi_inst_state.ins_root); 9420Sstevel@tonic-gate } 9430Sstevel@tonic-gate /* 9440Sstevel@tonic-gate * call up to find parent, then look through the list of kids 9450Sstevel@tonic-gate * for a match 9460Sstevel@tonic-gate */ 9470Sstevel@tonic-gate np = in_devwalk(ddi_get_parent(dip), ap, NULL); 9480Sstevel@tonic-gate if (np == NULL) 9490Sstevel@tonic-gate return (np); 9500Sstevel@tonic-gate *ap = np; 9510Sstevel@tonic-gate np = np->in_child; 9520Sstevel@tonic-gate name = ddi_node_name(dip); 9530Sstevel@tonic-gate if (addr == NULL) 9540Sstevel@tonic-gate addr = ddi_get_name_addr(dip); 9550Sstevel@tonic-gate 9560Sstevel@tonic-gate while (np) { 9570Sstevel@tonic-gate if (in_eqstr(np->in_node_name, name) && 9580Sstevel@tonic-gate in_eqstr(np->in_unit_addr, addr)) { 9590Sstevel@tonic-gate return (np); 9600Sstevel@tonic-gate } 9610Sstevel@tonic-gate np = np->in_sibling; 9620Sstevel@tonic-gate } 963*12116SVikram.Hegde@Sun.COM 9640Sstevel@tonic-gate return (np); 9650Sstevel@tonic-gate } 9660Sstevel@tonic-gate 9670Sstevel@tonic-gate /* 9680Sstevel@tonic-gate * Create a node specified by cp and assign it the given instance no. 9690Sstevel@tonic-gate */ 9700Sstevel@tonic-gate static int 9710Sstevel@tonic-gate in_pathin(char *cp, int instance, char *bname, struct bind **args) 9720Sstevel@tonic-gate { 9730Sstevel@tonic-gate in_node_t *np; 9740Sstevel@tonic-gate in_drv_t *dp; 9750Sstevel@tonic-gate char *name; 9760Sstevel@tonic-gate 9770Sstevel@tonic-gate ASSERT(e_ddi_inst_state.ins_busy); 9780Sstevel@tonic-gate ASSERT(args == NULL); 9790Sstevel@tonic-gate 9800Sstevel@tonic-gate /* 9810Sstevel@tonic-gate * Give a warning to the console. 9820Sstevel@tonic-gate * return value ignored 9830Sstevel@tonic-gate */ 9840Sstevel@tonic-gate if (cp[0] != '/' || instance == -1 || bname == NULL) { 9850Sstevel@tonic-gate cmn_err(CE_WARN, 9860Sstevel@tonic-gate "invalid instance file entry %s %d", 9870Sstevel@tonic-gate cp, instance); 9880Sstevel@tonic-gate return (0); 9890Sstevel@tonic-gate } 9900Sstevel@tonic-gate 9910Sstevel@tonic-gate if ((name = i_binding_to_drv_name(bname)) != NULL) 9920Sstevel@tonic-gate bname = name; 9930Sstevel@tonic-gate 9940Sstevel@tonic-gate np = in_make_path(cp); 9950Sstevel@tonic-gate ASSERT(np); 996*12116SVikram.Hegde@Sun.COM 9970Sstevel@tonic-gate dp = in_drvwalk(np, bname); 9980Sstevel@tonic-gate if (dp != NULL) { 9990Sstevel@tonic-gate cmn_err(CE_WARN, 10000Sstevel@tonic-gate "multiple instance number assignments for " 10010Sstevel@tonic-gate "'%s' (driver %s), %d used", 10020Sstevel@tonic-gate cp, bname, dp->ind_instance); 10030Sstevel@tonic-gate return (0); 10040Sstevel@tonic-gate } 1005*12116SVikram.Hegde@Sun.COM 1006*12116SVikram.Hegde@Sun.COM if (in_inuse(instance, bname)) { 1007*12116SVikram.Hegde@Sun.COM cmn_err(CE_WARN, 1008*12116SVikram.Hegde@Sun.COM "instance already in use: %s %d", cp, instance); 1009*12116SVikram.Hegde@Sun.COM return (0); 1010*12116SVikram.Hegde@Sun.COM } 1011*12116SVikram.Hegde@Sun.COM 10120Sstevel@tonic-gate dp = in_alloc_drv(bname); 10130Sstevel@tonic-gate in_endrv(np, dp); 10140Sstevel@tonic-gate dp->ind_instance = instance; 10150Sstevel@tonic-gate dp->ind_state = IN_PERMANENT; 10160Sstevel@tonic-gate in_hashdrv(dp); 10170Sstevel@tonic-gate 10180Sstevel@tonic-gate return (0); 10190Sstevel@tonic-gate } 10200Sstevel@tonic-gate 10210Sstevel@tonic-gate /* 10220Sstevel@tonic-gate * Create (or find) the node named by path by recursively descending from the 10230Sstevel@tonic-gate * root's first child (we ignore the root, which is never named) 10240Sstevel@tonic-gate */ 10250Sstevel@tonic-gate static in_node_t * 10260Sstevel@tonic-gate in_make_path(char *path) 10270Sstevel@tonic-gate { 10280Sstevel@tonic-gate in_node_t *ap; /* ancestor pointer */ 10290Sstevel@tonic-gate in_node_t *np; /* working node pointer */ 10300Sstevel@tonic-gate in_node_t *rp; /* return node pointer */ 10310Sstevel@tonic-gate char buf[MAXPATHLEN]; /* copy of string so we can change it */ 10320Sstevel@tonic-gate char *cp, *name, *addr; 10330Sstevel@tonic-gate 10340Sstevel@tonic-gate ASSERT(e_ddi_inst_state.ins_busy); 1035*12116SVikram.Hegde@Sun.COM 10360Sstevel@tonic-gate if (path == NULL || path[0] != '/') 10370Sstevel@tonic-gate return (NULL); 1038*12116SVikram.Hegde@Sun.COM 10390Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), "%s", path); 10400Sstevel@tonic-gate cp = buf + 1; /* skip over initial '/' in path */ 10410Sstevel@tonic-gate name = in_name_addr(&cp, &addr); 10420Sstevel@tonic-gate 10430Sstevel@tonic-gate /* 10440Sstevel@tonic-gate * In S9 and earlier releases, the path_to_inst file 10450Sstevel@tonic-gate * SunCluster was prepended with "/node@#". This was 10460Sstevel@tonic-gate * removed in S10. We skip the prefix if the prefix 10470Sstevel@tonic-gate * still exists in /etc/path_to_inst. It is needed for 10480Sstevel@tonic-gate * various forms of Solaris upgrade to work properly 10490Sstevel@tonic-gate * in the SunCluster environment. 10500Sstevel@tonic-gate */ 10510Sstevel@tonic-gate if ((cluster_bootflags & CLUSTER_CONFIGURED) && 10520Sstevel@tonic-gate (strcmp(name, "node") == 0)) 10530Sstevel@tonic-gate name = in_name_addr(&cp, &addr); 10540Sstevel@tonic-gate 10550Sstevel@tonic-gate ap = e_ddi_inst_state.ins_root; 1056*12116SVikram.Hegde@Sun.COM np = e_ddi_inst_state.ins_root->in_child; 1057*12116SVikram.Hegde@Sun.COM rp = np; 10580Sstevel@tonic-gate while (name) { 10590Sstevel@tonic-gate while (name && np) { 10600Sstevel@tonic-gate if (in_eqstr(name, np->in_node_name) && 10610Sstevel@tonic-gate in_eqstr(addr, np->in_unit_addr)) { 10620Sstevel@tonic-gate name = in_name_addr(&cp, &addr); 10630Sstevel@tonic-gate if (name == NULL) 10640Sstevel@tonic-gate return (np); 10650Sstevel@tonic-gate ap = np; 10660Sstevel@tonic-gate np = np->in_child; 10670Sstevel@tonic-gate } else { 10680Sstevel@tonic-gate np = np->in_sibling; 10690Sstevel@tonic-gate } 10700Sstevel@tonic-gate } 10710Sstevel@tonic-gate np = in_alloc_node(name, addr); 10720Sstevel@tonic-gate in_enlist(ap, np); /* insert into tree */ 10730Sstevel@tonic-gate rp = np; /* value to return if we quit */ 10740Sstevel@tonic-gate ap = np; /* new parent */ 10750Sstevel@tonic-gate np = NULL; /* can have no children */ 10760Sstevel@tonic-gate name = in_name_addr(&cp, &addr); 10770Sstevel@tonic-gate } 1078*12116SVikram.Hegde@Sun.COM 10790Sstevel@tonic-gate return (rp); 10800Sstevel@tonic-gate } 10810Sstevel@tonic-gate 10820Sstevel@tonic-gate /* 10830Sstevel@tonic-gate * Insert node np into the tree as one of ap's children. 10840Sstevel@tonic-gate */ 10850Sstevel@tonic-gate static void 10860Sstevel@tonic-gate in_enlist(in_node_t *ap, in_node_t *np) 10870Sstevel@tonic-gate { 10880Sstevel@tonic-gate in_node_t *mp; 10890Sstevel@tonic-gate ASSERT(e_ddi_inst_state.ins_busy); 10900Sstevel@tonic-gate /* 10910Sstevel@tonic-gate * Make this node some other node's child or child's sibling 10920Sstevel@tonic-gate */ 10930Sstevel@tonic-gate ASSERT(ap && np); 10940Sstevel@tonic-gate if (ap->in_child == NULL) { 10950Sstevel@tonic-gate ap->in_child = np; 10960Sstevel@tonic-gate } else { 10970Sstevel@tonic-gate for (mp = ap->in_child; mp; mp = mp->in_sibling) 10980Sstevel@tonic-gate if (mp->in_sibling == NULL) { 10990Sstevel@tonic-gate mp->in_sibling = np; 11000Sstevel@tonic-gate break; 11010Sstevel@tonic-gate } 11020Sstevel@tonic-gate } 11030Sstevel@tonic-gate np->in_parent = ap; 11040Sstevel@tonic-gate } 11050Sstevel@tonic-gate 11060Sstevel@tonic-gate /* 11070Sstevel@tonic-gate * Insert drv entry dp onto a node's driver list 11080Sstevel@tonic-gate */ 11090Sstevel@tonic-gate static void 11100Sstevel@tonic-gate in_endrv(in_node_t *np, in_drv_t *dp) 11110Sstevel@tonic-gate { 11120Sstevel@tonic-gate in_drv_t *mp; 11130Sstevel@tonic-gate ASSERT(e_ddi_inst_state.ins_busy); 11140Sstevel@tonic-gate ASSERT(np && dp); 11150Sstevel@tonic-gate mp = np->in_drivers; 11160Sstevel@tonic-gate np->in_drivers = dp; 11170Sstevel@tonic-gate dp->ind_next_drv = mp; 11180Sstevel@tonic-gate dp->ind_node = np; 11190Sstevel@tonic-gate } 11200Sstevel@tonic-gate 11210Sstevel@tonic-gate /* 11220Sstevel@tonic-gate * Parse the next name out of the path, null terminate it and update cp. 11230Sstevel@tonic-gate * caller has copied string so we can mess with it. 11240Sstevel@tonic-gate * Upon return *cpp points to the next section to be parsed, *addrp points 11250Sstevel@tonic-gate * to the current address substring (or NULL if none) and we return the 11260Sstevel@tonic-gate * current name substring (or NULL if none). name and address substrings 11270Sstevel@tonic-gate * are null terminated in place. 11280Sstevel@tonic-gate */ 11290Sstevel@tonic-gate 11300Sstevel@tonic-gate static char * 11310Sstevel@tonic-gate in_name_addr(char **cpp, char **addrp) 11320Sstevel@tonic-gate { 11330Sstevel@tonic-gate char *namep; /* return value holder */ 11340Sstevel@tonic-gate char *ap; /* pointer to '@' in string */ 11350Sstevel@tonic-gate char *sp; /* pointer to '/' in string */ 11360Sstevel@tonic-gate 11370Sstevel@tonic-gate if (*cpp == NULL || **cpp == '\0') { 11380Sstevel@tonic-gate *addrp = NULL; 11390Sstevel@tonic-gate return (NULL); 11400Sstevel@tonic-gate } 11410Sstevel@tonic-gate namep = *cpp; 11420Sstevel@tonic-gate sp = strchr(*cpp, '/'); 11430Sstevel@tonic-gate if (sp != NULL) { /* more to follow */ 11440Sstevel@tonic-gate *sp = '\0'; 11450Sstevel@tonic-gate *cpp = sp + 1; 11460Sstevel@tonic-gate } else { /* this is last component. */ 11470Sstevel@tonic-gate *cpp = NULL; 11480Sstevel@tonic-gate } 11490Sstevel@tonic-gate ap = strchr(namep, '@'); 11500Sstevel@tonic-gate if (ap == NULL) { 11510Sstevel@tonic-gate *addrp = NULL; 11520Sstevel@tonic-gate } else { 11530Sstevel@tonic-gate *ap = '\0'; /* terminate the name */ 11540Sstevel@tonic-gate *addrp = ap + 1; 11550Sstevel@tonic-gate } 11560Sstevel@tonic-gate return (namep); 11570Sstevel@tonic-gate } 11580Sstevel@tonic-gate 11590Sstevel@tonic-gate /* 11600Sstevel@tonic-gate * Allocate a node and storage for name and addr strings, and fill them in. 11610Sstevel@tonic-gate */ 11620Sstevel@tonic-gate static in_node_t * 11630Sstevel@tonic-gate in_alloc_node(char *name, char *addr) 11640Sstevel@tonic-gate { 11650Sstevel@tonic-gate in_node_t *np; 11660Sstevel@tonic-gate char *cp; 11670Sstevel@tonic-gate size_t namelen; 11680Sstevel@tonic-gate 11690Sstevel@tonic-gate ASSERT(e_ddi_inst_state.ins_busy); 11700Sstevel@tonic-gate /* 11710Sstevel@tonic-gate * Has name or will become root 11720Sstevel@tonic-gate */ 11730Sstevel@tonic-gate ASSERT(name || e_ddi_inst_state.ins_root == NULL); 11740Sstevel@tonic-gate if (addr == NULL) 11750Sstevel@tonic-gate addr = ""; 11760Sstevel@tonic-gate if (name == NULL) 11770Sstevel@tonic-gate namelen = 0; 11780Sstevel@tonic-gate else 11790Sstevel@tonic-gate namelen = strlen(name) + 1; 11800Sstevel@tonic-gate cp = kmem_zalloc(sizeof (in_node_t) + namelen + strlen(addr) + 1, 11810Sstevel@tonic-gate KM_SLEEP); 11820Sstevel@tonic-gate np = (in_node_t *)cp; 11830Sstevel@tonic-gate if (name) { 11840Sstevel@tonic-gate np->in_node_name = cp + sizeof (in_node_t); 11850Sstevel@tonic-gate (void) strcpy(np->in_node_name, name); 11860Sstevel@tonic-gate } 11870Sstevel@tonic-gate np->in_unit_addr = cp + sizeof (in_node_t) + namelen; 11880Sstevel@tonic-gate (void) strcpy(np->in_unit_addr, addr); 11890Sstevel@tonic-gate return (np); 11900Sstevel@tonic-gate } 11910Sstevel@tonic-gate 11920Sstevel@tonic-gate /* 11930Sstevel@tonic-gate * Allocate a drv entry and storage for binding name string, and fill it in. 11940Sstevel@tonic-gate */ 11950Sstevel@tonic-gate static in_drv_t * 11960Sstevel@tonic-gate in_alloc_drv(char *bindingname) 11970Sstevel@tonic-gate { 11980Sstevel@tonic-gate in_drv_t *dp; 11990Sstevel@tonic-gate char *cp; 12000Sstevel@tonic-gate size_t namelen; 12010Sstevel@tonic-gate 12020Sstevel@tonic-gate ASSERT(e_ddi_inst_state.ins_busy); 12030Sstevel@tonic-gate /* 12040Sstevel@tonic-gate * Has name or will become root 12050Sstevel@tonic-gate */ 12060Sstevel@tonic-gate ASSERT(bindingname || e_ddi_inst_state.ins_root == NULL); 12070Sstevel@tonic-gate if (bindingname == NULL) 12080Sstevel@tonic-gate namelen = 0; 12090Sstevel@tonic-gate else 12100Sstevel@tonic-gate namelen = strlen(bindingname) + 1; 12110Sstevel@tonic-gate cp = kmem_zalloc(sizeof (in_drv_t) + namelen, KM_SLEEP); 12120Sstevel@tonic-gate dp = (in_drv_t *)cp; 12130Sstevel@tonic-gate if (bindingname) { 12140Sstevel@tonic-gate dp->ind_driver_name = cp + sizeof (in_drv_t); 12150Sstevel@tonic-gate (void) strcpy(dp->ind_driver_name, bindingname); 12160Sstevel@tonic-gate } 12170Sstevel@tonic-gate dp->ind_state = IN_UNKNOWN; 12180Sstevel@tonic-gate dp->ind_instance = -1; 12190Sstevel@tonic-gate return (dp); 12200Sstevel@tonic-gate } 12210Sstevel@tonic-gate 12220Sstevel@tonic-gate static void 12230Sstevel@tonic-gate in_dealloc_node(in_node_t *np) 12240Sstevel@tonic-gate { 12250Sstevel@tonic-gate /* 12260Sstevel@tonic-gate * The root node can never be de-allocated 12270Sstevel@tonic-gate */ 12280Sstevel@tonic-gate ASSERT(np->in_node_name && np->in_unit_addr); 12290Sstevel@tonic-gate ASSERT(e_ddi_inst_state.ins_busy); 12300Sstevel@tonic-gate kmem_free(np, sizeof (in_node_t) + strlen(np->in_node_name) 12310Sstevel@tonic-gate + strlen(np->in_unit_addr) + 2); 12320Sstevel@tonic-gate } 12330Sstevel@tonic-gate 12340Sstevel@tonic-gate static void 12350Sstevel@tonic-gate in_dealloc_drv(in_drv_t *dp) 12360Sstevel@tonic-gate { 12370Sstevel@tonic-gate ASSERT(dp->ind_driver_name); 12380Sstevel@tonic-gate ASSERT(e_ddi_inst_state.ins_busy); 12390Sstevel@tonic-gate kmem_free(dp, sizeof (in_drv_t) + strlen(dp->ind_driver_name) 12400Sstevel@tonic-gate + 1); 12410Sstevel@tonic-gate } 12420Sstevel@tonic-gate 12430Sstevel@tonic-gate /* 12440Sstevel@tonic-gate * Handle the various possible versions of "no address" 12450Sstevel@tonic-gate */ 12460Sstevel@tonic-gate static int 12470Sstevel@tonic-gate in_eqstr(char *a, char *b) 12480Sstevel@tonic-gate { 12490Sstevel@tonic-gate if (a == b) /* covers case where both are nulls */ 12500Sstevel@tonic-gate return (1); 12510Sstevel@tonic-gate if (a == NULL && *b == 0) 12520Sstevel@tonic-gate return (1); 12530Sstevel@tonic-gate if (b == NULL && *a == 0) 12540Sstevel@tonic-gate return (1); 12550Sstevel@tonic-gate if (a == NULL || b == NULL) 12560Sstevel@tonic-gate return (0); 12570Sstevel@tonic-gate return (strcmp(a, b) == 0); 12580Sstevel@tonic-gate } 12590Sstevel@tonic-gate 12600Sstevel@tonic-gate /* 12610Sstevel@tonic-gate * Returns true if instance no. is already in use by named driver 12620Sstevel@tonic-gate */ 12630Sstevel@tonic-gate static int 12640Sstevel@tonic-gate in_inuse(int instance, char *name) 12650Sstevel@tonic-gate { 12660Sstevel@tonic-gate major_t major; 12670Sstevel@tonic-gate in_drv_t *dp; 12680Sstevel@tonic-gate struct devnames *dnp; 12690Sstevel@tonic-gate 12700Sstevel@tonic-gate ASSERT(e_ddi_inst_state.ins_busy); 12710Sstevel@tonic-gate /* 12720Sstevel@tonic-gate * For now, if we've never heard of this device we assume it is not 12730Sstevel@tonic-gate * in use, since we can't tell 12740Sstevel@tonic-gate * XXX could do the weaker search through the nomajor list checking 12750Sstevel@tonic-gate * XXX for the same name 12760Sstevel@tonic-gate */ 12777009Scth if ((major = ddi_name_to_major(name)) == DDI_MAJOR_T_NONE) 12780Sstevel@tonic-gate return (0); 12790Sstevel@tonic-gate dnp = &devnamesp[major]; 12800Sstevel@tonic-gate 12810Sstevel@tonic-gate dp = dnp->dn_inlist; 12820Sstevel@tonic-gate while (dp) { 12830Sstevel@tonic-gate if (dp->ind_instance == instance) 12840Sstevel@tonic-gate return (1); 12850Sstevel@tonic-gate dp = dp->ind_next; 12860Sstevel@tonic-gate } 12870Sstevel@tonic-gate return (0); 12880Sstevel@tonic-gate } 12890Sstevel@tonic-gate 12900Sstevel@tonic-gate static void 12910Sstevel@tonic-gate in_hashdrv(in_drv_t *dp) 12920Sstevel@tonic-gate { 12930Sstevel@tonic-gate struct devnames *dnp; 12940Sstevel@tonic-gate in_drv_t *mp, *pp; 12950Sstevel@tonic-gate major_t major; 12960Sstevel@tonic-gate 12970Sstevel@tonic-gate /* hash to no major list */ 12987009Scth major = ddi_name_to_major(dp->ind_driver_name); 12997009Scth if (major == DDI_MAJOR_T_NONE) { 13000Sstevel@tonic-gate dp->ind_next = e_ddi_inst_state.ins_no_major; 13010Sstevel@tonic-gate e_ddi_inst_state.ins_no_major = dp; 13020Sstevel@tonic-gate return; 13030Sstevel@tonic-gate } 13040Sstevel@tonic-gate 13050Sstevel@tonic-gate /* 13060Sstevel@tonic-gate * dnp->dn_inlist is sorted by instance number. 13070Sstevel@tonic-gate * Adding a new instance entry may introduce holes, 13080Sstevel@tonic-gate * set dn_instance to IN_SEARCHME so the next instance 13090Sstevel@tonic-gate * assignment may fill in holes. 13100Sstevel@tonic-gate */ 13110Sstevel@tonic-gate dnp = &devnamesp[major]; 13120Sstevel@tonic-gate pp = mp = dnp->dn_inlist; 13130Sstevel@tonic-gate if (mp == NULL || dp->ind_instance < mp->ind_instance) { 13140Sstevel@tonic-gate /* prepend as the first entry, turn on IN_SEARCHME */ 13150Sstevel@tonic-gate dnp->dn_instance = IN_SEARCHME; 13160Sstevel@tonic-gate dp->ind_next = mp; 13170Sstevel@tonic-gate dnp->dn_inlist = dp; 13180Sstevel@tonic-gate return; 13190Sstevel@tonic-gate } 13200Sstevel@tonic-gate 13210Sstevel@tonic-gate ASSERT(mp->ind_instance != dp->ind_instance); 13220Sstevel@tonic-gate while (mp->ind_instance < dp->ind_instance && mp->ind_next) { 13230Sstevel@tonic-gate pp = mp; 13240Sstevel@tonic-gate mp = mp->ind_next; 13250Sstevel@tonic-gate ASSERT(mp->ind_instance != dp->ind_instance); 13260Sstevel@tonic-gate } 13270Sstevel@tonic-gate 13280Sstevel@tonic-gate if (mp->ind_instance < dp->ind_instance) { /* end of list */ 13290Sstevel@tonic-gate dp->ind_next = NULL; 13300Sstevel@tonic-gate mp->ind_next = dp; 13310Sstevel@tonic-gate } else { 13320Sstevel@tonic-gate ASSERT(dnp->dn_instance == IN_SEARCHME); 13330Sstevel@tonic-gate dp->ind_next = pp->ind_next; 13340Sstevel@tonic-gate pp->ind_next = dp; 13350Sstevel@tonic-gate } 13360Sstevel@tonic-gate } 13370Sstevel@tonic-gate 13380Sstevel@tonic-gate /* 13390Sstevel@tonic-gate * Remove a driver entry from the list, given a previous pointer 13400Sstevel@tonic-gate */ 13410Sstevel@tonic-gate static void 13420Sstevel@tonic-gate in_removedrv(struct devnames *dnp, in_drv_t *mp) 13430Sstevel@tonic-gate { 13440Sstevel@tonic-gate in_drv_t *dp; 13450Sstevel@tonic-gate in_drv_t *prevp; 13460Sstevel@tonic-gate 13470Sstevel@tonic-gate if (dnp->dn_inlist == mp) { /* head of list */ 13480Sstevel@tonic-gate dnp->dn_inlist = mp->ind_next; 13490Sstevel@tonic-gate dnp->dn_instance = IN_SEARCHME; 13500Sstevel@tonic-gate in_dq_drv(mp); 13510Sstevel@tonic-gate in_dealloc_drv(mp); 13520Sstevel@tonic-gate return; 13530Sstevel@tonic-gate } 13540Sstevel@tonic-gate prevp = dnp->dn_inlist; 13550Sstevel@tonic-gate for (dp = prevp->ind_next; dp; dp = dp->ind_next) { 13560Sstevel@tonic-gate if (dp == mp) { /* found it */ 13570Sstevel@tonic-gate break; 13580Sstevel@tonic-gate } 13590Sstevel@tonic-gate prevp = dp; 13600Sstevel@tonic-gate } 13610Sstevel@tonic-gate 13620Sstevel@tonic-gate ASSERT(dp == mp); 13630Sstevel@tonic-gate dnp->dn_instance = IN_SEARCHME; 13640Sstevel@tonic-gate prevp->ind_next = mp->ind_next; 13650Sstevel@tonic-gate in_dq_drv(mp); 13660Sstevel@tonic-gate in_dealloc_drv(mp); 13670Sstevel@tonic-gate } 13680Sstevel@tonic-gate 13690Sstevel@tonic-gate static void 13700Sstevel@tonic-gate in_dq_drv(in_drv_t *mp) 13710Sstevel@tonic-gate { 13720Sstevel@tonic-gate struct in_node *node = mp->ind_node; 13730Sstevel@tonic-gate in_drv_t *ptr, *prev; 13740Sstevel@tonic-gate 13750Sstevel@tonic-gate if (mp == node->in_drivers) { 13760Sstevel@tonic-gate node->in_drivers = mp->ind_next_drv; 13770Sstevel@tonic-gate return; 13780Sstevel@tonic-gate } 13790Sstevel@tonic-gate prev = node->in_drivers; 13800Sstevel@tonic-gate for (ptr = prev->ind_next_drv; ptr != (struct in_drv *)NULL; 13810Sstevel@tonic-gate ptr = ptr->ind_next_drv) { 13820Sstevel@tonic-gate if (ptr == mp) { 13830Sstevel@tonic-gate prev->ind_next_drv = ptr->ind_next_drv; 13840Sstevel@tonic-gate return; 13850Sstevel@tonic-gate } 13867235Svikram prev = ptr; 13870Sstevel@tonic-gate } 13880Sstevel@tonic-gate panic("in_dq_drv: in_drv not found on node driver list"); 13890Sstevel@tonic-gate } 13900Sstevel@tonic-gate 13910Sstevel@tonic-gate 13920Sstevel@tonic-gate in_drv_t * 13930Sstevel@tonic-gate in_drvwalk(in_node_t *np, char *binding_name) 13940Sstevel@tonic-gate { 13950Sstevel@tonic-gate char *name; 13960Sstevel@tonic-gate in_drv_t *dp = np->in_drivers; 13970Sstevel@tonic-gate while (dp) { 13980Sstevel@tonic-gate if ((name = i_binding_to_drv_name(dp->ind_driver_name)) 13990Sstevel@tonic-gate == NULL) { 14000Sstevel@tonic-gate name = dp->ind_driver_name; 14010Sstevel@tonic-gate } 14020Sstevel@tonic-gate if (strcmp(binding_name, name) == 0) { 14030Sstevel@tonic-gate break; 14040Sstevel@tonic-gate } 14050Sstevel@tonic-gate dp = dp->ind_next_drv; 14060Sstevel@tonic-gate } 14070Sstevel@tonic-gate return (dp); 14080Sstevel@tonic-gate } 14090Sstevel@tonic-gate 14100Sstevel@tonic-gate 14110Sstevel@tonic-gate 14120Sstevel@tonic-gate static void 14130Sstevel@tonic-gate i_log_devfs_instance_mod(void) 14140Sstevel@tonic-gate { 14153250Scth sysevent_t *ev; 14163250Scth sysevent_id_t eid; 14173250Scth static int sent_one = 0; 14180Sstevel@tonic-gate 14190Sstevel@tonic-gate /* 14203250Scth * Prevent unnecessary event generation. Do not generate more than 14213250Scth * one event during boot. 14220Sstevel@tonic-gate */ 14233250Scth if (sent_one && !i_ddi_io_initialized()) 14240Sstevel@tonic-gate return; 14250Sstevel@tonic-gate 14260Sstevel@tonic-gate ev = sysevent_alloc(EC_DEVFS, ESC_DEVFS_INSTANCE_MOD, EP_DDI, 14275648Ssetje SE_NOSLEEP); 14280Sstevel@tonic-gate if (ev == NULL) { 14290Sstevel@tonic-gate return; 14300Sstevel@tonic-gate } 14310Sstevel@tonic-gate if (log_sysevent(ev, SE_NOSLEEP, &eid) != 0) { 14320Sstevel@tonic-gate cmn_err(CE_WARN, "i_log_devfs_instance_mod: failed to post " 14335648Ssetje "event"); 14343250Scth } else { 14353250Scth sent_one = 1; 14360Sstevel@tonic-gate } 14370Sstevel@tonic-gate sysevent_free(ev); 14380Sstevel@tonic-gate } 14390Sstevel@tonic-gate 14400Sstevel@tonic-gate void 1441*12116SVikram.Hegde@Sun.COM e_ddi_enter_instance(void) 14420Sstevel@tonic-gate { 14430Sstevel@tonic-gate mutex_enter(&e_ddi_inst_state.ins_serial); 14440Sstevel@tonic-gate if (e_ddi_inst_state.ins_thread == curthread) 14450Sstevel@tonic-gate e_ddi_inst_state.ins_busy++; 14460Sstevel@tonic-gate else { 14470Sstevel@tonic-gate while (e_ddi_inst_state.ins_busy) 14480Sstevel@tonic-gate cv_wait(&e_ddi_inst_state.ins_serial_cv, 14490Sstevel@tonic-gate &e_ddi_inst_state.ins_serial); 14500Sstevel@tonic-gate e_ddi_inst_state.ins_thread = curthread; 14510Sstevel@tonic-gate e_ddi_inst_state.ins_busy = 1; 14520Sstevel@tonic-gate } 14530Sstevel@tonic-gate mutex_exit(&e_ddi_inst_state.ins_serial); 14540Sstevel@tonic-gate } 14550Sstevel@tonic-gate 14560Sstevel@tonic-gate void 1457*12116SVikram.Hegde@Sun.COM e_ddi_exit_instance(void) 14580Sstevel@tonic-gate { 14590Sstevel@tonic-gate mutex_enter(&e_ddi_inst_state.ins_serial); 14600Sstevel@tonic-gate e_ddi_inst_state.ins_busy--; 14610Sstevel@tonic-gate if (e_ddi_inst_state.ins_busy == 0) { 14620Sstevel@tonic-gate cv_broadcast(&e_ddi_inst_state.ins_serial_cv); 14630Sstevel@tonic-gate e_ddi_inst_state.ins_thread = NULL; 14640Sstevel@tonic-gate } 14650Sstevel@tonic-gate mutex_exit(&e_ddi_inst_state.ins_serial); 14660Sstevel@tonic-gate } 14670Sstevel@tonic-gate 14680Sstevel@tonic-gate int 1469*12116SVikram.Hegde@Sun.COM e_ddi_instance_is_clean(void) 14700Sstevel@tonic-gate { 1471*12116SVikram.Hegde@Sun.COM return (e_ddi_inst_state.ins_dirty == B_FALSE); 14720Sstevel@tonic-gate } 14730Sstevel@tonic-gate 14740Sstevel@tonic-gate void 1475*12116SVikram.Hegde@Sun.COM e_ddi_instance_set_clean(void) 14760Sstevel@tonic-gate { 1477*12116SVikram.Hegde@Sun.COM e_ddi_inst_state.ins_dirty = B_FALSE; 14780Sstevel@tonic-gate } 14790Sstevel@tonic-gate 14800Sstevel@tonic-gate in_node_t * 1481*12116SVikram.Hegde@Sun.COM e_ddi_instance_root(void) 14820Sstevel@tonic-gate { 14830Sstevel@tonic-gate return (e_ddi_inst_state.ins_root); 14840Sstevel@tonic-gate } 14850Sstevel@tonic-gate 14860Sstevel@tonic-gate /* 14870Sstevel@tonic-gate * Visit a node in the instance tree 14880Sstevel@tonic-gate */ 14890Sstevel@tonic-gate static int 14900Sstevel@tonic-gate in_walk_instances(in_node_t *np, char *path, char *this, 14910Sstevel@tonic-gate int (*f)(const char *, in_node_t *, in_drv_t *, void *), void *arg) 14920Sstevel@tonic-gate { 14930Sstevel@tonic-gate in_drv_t *dp; 14940Sstevel@tonic-gate int rval = INST_WALK_CONTINUE; 14950Sstevel@tonic-gate char *next; 14960Sstevel@tonic-gate 14970Sstevel@tonic-gate while (np != NULL) { 14980Sstevel@tonic-gate 14990Sstevel@tonic-gate if (np->in_unit_addr[0] == 0) 15000Sstevel@tonic-gate (void) sprintf(this, "/%s", np->in_node_name); 15010Sstevel@tonic-gate else 15020Sstevel@tonic-gate (void) sprintf(this, "/%s@%s", np->in_node_name, 15030Sstevel@tonic-gate np->in_unit_addr); 15040Sstevel@tonic-gate next = this + strlen(this); 15050Sstevel@tonic-gate 15060Sstevel@tonic-gate for (dp = np->in_drivers; dp; dp = dp->ind_next_drv) { 15070Sstevel@tonic-gate if (dp->ind_state == IN_PERMANENT) { 15080Sstevel@tonic-gate rval = (*f)(path, np, dp, arg); 15090Sstevel@tonic-gate if (rval == INST_WALK_TERMINATE) 15100Sstevel@tonic-gate break; 15110Sstevel@tonic-gate } 15120Sstevel@tonic-gate } 1513*12116SVikram.Hegde@Sun.COM 15140Sstevel@tonic-gate if (np->in_child) { 15150Sstevel@tonic-gate rval = in_walk_instances(np->in_child, 15160Sstevel@tonic-gate path, next, f, arg); 15170Sstevel@tonic-gate if (rval == INST_WALK_TERMINATE) 15180Sstevel@tonic-gate break; 15190Sstevel@tonic-gate } 15200Sstevel@tonic-gate 15210Sstevel@tonic-gate np = np->in_sibling; 15220Sstevel@tonic-gate } 15230Sstevel@tonic-gate 15240Sstevel@tonic-gate return (rval); 15250Sstevel@tonic-gate } 15260Sstevel@tonic-gate 15270Sstevel@tonic-gate /* 15280Sstevel@tonic-gate * A general interface for walking the instance tree, 15290Sstevel@tonic-gate * calling a user-supplied callback for each node. 15300Sstevel@tonic-gate */ 15310Sstevel@tonic-gate int 15320Sstevel@tonic-gate e_ddi_walk_instances(int (*f)(const char *, 15330Sstevel@tonic-gate in_node_t *, in_drv_t *, void *), void *arg) 15340Sstevel@tonic-gate { 15350Sstevel@tonic-gate in_node_t *root; 15360Sstevel@tonic-gate int rval; 15370Sstevel@tonic-gate char *path; 15380Sstevel@tonic-gate 15390Sstevel@tonic-gate path = kmem_zalloc(MAXPATHLEN, KM_SLEEP); 15400Sstevel@tonic-gate 15410Sstevel@tonic-gate e_ddi_enter_instance(); 15420Sstevel@tonic-gate root = e_ddi_instance_root(); 15430Sstevel@tonic-gate rval = in_walk_instances(root->in_child, path, path, f, arg); 1544*12116SVikram.Hegde@Sun.COM 15450Sstevel@tonic-gate e_ddi_exit_instance(); 15460Sstevel@tonic-gate 15470Sstevel@tonic-gate kmem_free(path, MAXPATHLEN); 15480Sstevel@tonic-gate return (rval); 15490Sstevel@tonic-gate } 1550*12116SVikram.Hegde@Sun.COM 1551*12116SVikram.Hegde@Sun.COM in_node_t * 1552*12116SVikram.Hegde@Sun.COM e_ddi_path_to_instance(char *path) 1553*12116SVikram.Hegde@Sun.COM { 1554*12116SVikram.Hegde@Sun.COM in_node_t *np; 1555*12116SVikram.Hegde@Sun.COM 1556*12116SVikram.Hegde@Sun.COM np = in_make_path(path); 1557*12116SVikram.Hegde@Sun.COM if (np && np->in_drivers && np->in_drivers->ind_state == IN_PERMANENT) { 1558*12116SVikram.Hegde@Sun.COM return (np); 1559*12116SVikram.Hegde@Sun.COM } 1560*12116SVikram.Hegde@Sun.COM return (NULL); 1561*12116SVikram.Hegde@Sun.COM } 1562*12116SVikram.Hegde@Sun.COM 1563*12116SVikram.Hegde@Sun.COM void 1564*12116SVikram.Hegde@Sun.COM e_ddi_borrow_instance(dev_info_t *cdip, in_node_t *cnp) 1565*12116SVikram.Hegde@Sun.COM { 1566*12116SVikram.Hegde@Sun.COM char *alias; 1567*12116SVikram.Hegde@Sun.COM in_node_t *anp; 1568*12116SVikram.Hegde@Sun.COM char *curr = kmem_alloc(MAXPATHLEN, KM_NOSLEEP); 1569*12116SVikram.Hegde@Sun.COM 1570*12116SVikram.Hegde@Sun.COM if (curr == NULL) { 1571*12116SVikram.Hegde@Sun.COM ddi_err(DER_PANIC, cdip, "curr alloc failed"); 1572*12116SVikram.Hegde@Sun.COM /*NOTREACHED*/ 1573*12116SVikram.Hegde@Sun.COM } 1574*12116SVikram.Hegde@Sun.COM 1575*12116SVikram.Hegde@Sun.COM (void) ddi_pathname(cdip, curr); 1576*12116SVikram.Hegde@Sun.COM 1577*12116SVikram.Hegde@Sun.COM if (cnp->in_drivers) { 1578*12116SVikram.Hegde@Sun.COM ddi_err(DER_PANIC, cdip, "cnp has instance: %p", cnp); 1579*12116SVikram.Hegde@Sun.COM /*NOTREACHED*/ 1580*12116SVikram.Hegde@Sun.COM } 1581*12116SVikram.Hegde@Sun.COM 1582*12116SVikram.Hegde@Sun.COM alias = ddi_curr_redirect(curr); 1583*12116SVikram.Hegde@Sun.COM kmem_free(curr, MAXPATHLEN); 1584*12116SVikram.Hegde@Sun.COM 1585*12116SVikram.Hegde@Sun.COM if (alias && (anp = e_ddi_path_to_instance(alias)) != NULL) { 1586*12116SVikram.Hegde@Sun.COM cnp->in_drivers = anp->in_drivers; 1587*12116SVikram.Hegde@Sun.COM anp->in_drivers = NULL; 1588*12116SVikram.Hegde@Sun.COM } 1589*12116SVikram.Hegde@Sun.COM } 1590*12116SVikram.Hegde@Sun.COM 1591*12116SVikram.Hegde@Sun.COM void 1592*12116SVikram.Hegde@Sun.COM e_ddi_return_instance(dev_info_t *cdip, char *addr, in_node_t *cnp) 1593*12116SVikram.Hegde@Sun.COM { 1594*12116SVikram.Hegde@Sun.COM in_node_t *anp; 1595*12116SVikram.Hegde@Sun.COM char *alias; 1596*12116SVikram.Hegde@Sun.COM char *curr = kmem_alloc(MAXPATHLEN, KM_NOSLEEP); 1597*12116SVikram.Hegde@Sun.COM 1598*12116SVikram.Hegde@Sun.COM if (curr == NULL) { 1599*12116SVikram.Hegde@Sun.COM ddi_err(DER_PANIC, cdip, "alloc of curr failed"); 1600*12116SVikram.Hegde@Sun.COM /*NOTREACHED*/ 1601*12116SVikram.Hegde@Sun.COM } 1602*12116SVikram.Hegde@Sun.COM 1603*12116SVikram.Hegde@Sun.COM (void) ddi_pathname(cdip, curr); 1604*12116SVikram.Hegde@Sun.COM if (addr) { 1605*12116SVikram.Hegde@Sun.COM (void) strlcat(curr, "@", MAXPATHLEN); 1606*12116SVikram.Hegde@Sun.COM (void) strlcat(curr, addr, MAXPATHLEN); 1607*12116SVikram.Hegde@Sun.COM 1608*12116SVikram.Hegde@Sun.COM } 1609*12116SVikram.Hegde@Sun.COM if (cnp->in_drivers == NULL) { 1610*12116SVikram.Hegde@Sun.COM ddi_err(DER_PANIC, cdip, "cnp has no inst: %p", cnp); 1611*12116SVikram.Hegde@Sun.COM /*NOTREACHED*/ 1612*12116SVikram.Hegde@Sun.COM } 1613*12116SVikram.Hegde@Sun.COM 1614*12116SVikram.Hegde@Sun.COM alias = ddi_curr_redirect(curr); 1615*12116SVikram.Hegde@Sun.COM kmem_free(curr, MAXPATHLEN); 1616*12116SVikram.Hegde@Sun.COM 1617*12116SVikram.Hegde@Sun.COM if (alias && (anp = e_ddi_path_to_instance(alias)) != NULL) { 1618*12116SVikram.Hegde@Sun.COM ASSERT(anp->in_drivers == NULL); 1619*12116SVikram.Hegde@Sun.COM anp->in_drivers = cnp->in_drivers; 1620*12116SVikram.Hegde@Sun.COM cnp->in_drivers = NULL; 1621*12116SVikram.Hegde@Sun.COM } 1622*12116SVikram.Hegde@Sun.COM } 1623