1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* 23*7c478bd9Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 25*7c478bd9Sstevel@tonic-gate */ 26*7c478bd9Sstevel@tonic-gate 27*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*7c478bd9Sstevel@tonic-gate 29*7c478bd9Sstevel@tonic-gate #include <sys/note.h> 30*7c478bd9Sstevel@tonic-gate #include <sys/t_lock.h> 31*7c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h> 32*7c478bd9Sstevel@tonic-gate #include <sys/instance.h> 33*7c478bd9Sstevel@tonic-gate #include <sys/conf.h> 34*7c478bd9Sstevel@tonic-gate #include <sys/stat.h> 35*7c478bd9Sstevel@tonic-gate #include <sys/ddi.h> 36*7c478bd9Sstevel@tonic-gate #include <sys/hwconf.h> 37*7c478bd9Sstevel@tonic-gate #include <sys/sunddi.h> 38*7c478bd9Sstevel@tonic-gate #include <sys/sunndi.h> 39*7c478bd9Sstevel@tonic-gate #include <sys/ddi_impldefs.h> 40*7c478bd9Sstevel@tonic-gate #include <sys/ndi_impldefs.h> 41*7c478bd9Sstevel@tonic-gate #include <sys/modctl.h> 42*7c478bd9Sstevel@tonic-gate #include <sys/dacf.h> 43*7c478bd9Sstevel@tonic-gate #include <sys/promif.h> 44*7c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h> 45*7c478bd9Sstevel@tonic-gate #include <sys/pathname.h> 46*7c478bd9Sstevel@tonic-gate #include <sys/taskq.h> 47*7c478bd9Sstevel@tonic-gate #include <sys/sysevent.h> 48*7c478bd9Sstevel@tonic-gate #include <sys/sunmdi.h> 49*7c478bd9Sstevel@tonic-gate #include <sys/stream.h> 50*7c478bd9Sstevel@tonic-gate #include <sys/strsubr.h> 51*7c478bd9Sstevel@tonic-gate #include <sys/fs/snode.h> 52*7c478bd9Sstevel@tonic-gate #include <sys/fs/dv_node.h> 53*7c478bd9Sstevel@tonic-gate 54*7c478bd9Sstevel@tonic-gate #ifdef DEBUG 55*7c478bd9Sstevel@tonic-gate int ddidebug = DDI_AUDIT; 56*7c478bd9Sstevel@tonic-gate #else 57*7c478bd9Sstevel@tonic-gate int ddidebug = 0; 58*7c478bd9Sstevel@tonic-gate #endif 59*7c478bd9Sstevel@tonic-gate 60*7c478bd9Sstevel@tonic-gate #define MT_CONFIG_OP 0 61*7c478bd9Sstevel@tonic-gate #define MT_UNCONFIG_OP 1 62*7c478bd9Sstevel@tonic-gate 63*7c478bd9Sstevel@tonic-gate /* Multi-threaded configuration */ 64*7c478bd9Sstevel@tonic-gate struct mt_config_handle { 65*7c478bd9Sstevel@tonic-gate kmutex_t mtc_lock; 66*7c478bd9Sstevel@tonic-gate kcondvar_t mtc_cv; 67*7c478bd9Sstevel@tonic-gate int mtc_thr_count; 68*7c478bd9Sstevel@tonic-gate dev_info_t *mtc_pdip; /* parent dip for mt_config_children */ 69*7c478bd9Sstevel@tonic-gate dev_info_t **mtc_fdip; /* "a" dip where unconfigure failed */ 70*7c478bd9Sstevel@tonic-gate major_t mtc_parmajor; /* parent major for mt_config_driver */ 71*7c478bd9Sstevel@tonic-gate major_t mtc_major; 72*7c478bd9Sstevel@tonic-gate int mtc_flags; 73*7c478bd9Sstevel@tonic-gate int mtc_op; /* config or unconfig */ 74*7c478bd9Sstevel@tonic-gate int mtc_error; /* operation error */ 75*7c478bd9Sstevel@tonic-gate struct brevq_node **mtc_brevqp; /* outstanding branch events queue */ 76*7c478bd9Sstevel@tonic-gate #ifdef DEBUG 77*7c478bd9Sstevel@tonic-gate int total_time; 78*7c478bd9Sstevel@tonic-gate timestruc_t start_time; 79*7c478bd9Sstevel@tonic-gate #endif /* DEBUG */ 80*7c478bd9Sstevel@tonic-gate }; 81*7c478bd9Sstevel@tonic-gate 82*7c478bd9Sstevel@tonic-gate struct devi_nodeid { 83*7c478bd9Sstevel@tonic-gate dnode_t nodeid; 84*7c478bd9Sstevel@tonic-gate dev_info_t *dip; 85*7c478bd9Sstevel@tonic-gate struct devi_nodeid *next; 86*7c478bd9Sstevel@tonic-gate }; 87*7c478bd9Sstevel@tonic-gate 88*7c478bd9Sstevel@tonic-gate struct devi_nodeid_list { 89*7c478bd9Sstevel@tonic-gate kmutex_t dno_lock; /* Protects other fields */ 90*7c478bd9Sstevel@tonic-gate struct devi_nodeid *dno_head; /* list of devi nodeid elements */ 91*7c478bd9Sstevel@tonic-gate struct devi_nodeid *dno_free; /* Free list */ 92*7c478bd9Sstevel@tonic-gate uint_t dno_list_length; /* number of dips in list */ 93*7c478bd9Sstevel@tonic-gate }; 94*7c478bd9Sstevel@tonic-gate 95*7c478bd9Sstevel@tonic-gate /* used to keep track of branch remove events to be generated */ 96*7c478bd9Sstevel@tonic-gate struct brevq_node { 97*7c478bd9Sstevel@tonic-gate char *deviname; 98*7c478bd9Sstevel@tonic-gate struct brevq_node *sibling; 99*7c478bd9Sstevel@tonic-gate struct brevq_node *child; 100*7c478bd9Sstevel@tonic-gate }; 101*7c478bd9Sstevel@tonic-gate 102*7c478bd9Sstevel@tonic-gate static struct devi_nodeid_list devi_nodeid_list; 103*7c478bd9Sstevel@tonic-gate static struct devi_nodeid_list *devimap = &devi_nodeid_list; 104*7c478bd9Sstevel@tonic-gate 105*7c478bd9Sstevel@tonic-gate /* 106*7c478bd9Sstevel@tonic-gate * Well known nodes which are attached first at boot time. 107*7c478bd9Sstevel@tonic-gate */ 108*7c478bd9Sstevel@tonic-gate dev_info_t *top_devinfo; /* root of device tree */ 109*7c478bd9Sstevel@tonic-gate dev_info_t *options_dip; 110*7c478bd9Sstevel@tonic-gate dev_info_t *pseudo_dip; 111*7c478bd9Sstevel@tonic-gate dev_info_t *clone_dip; 112*7c478bd9Sstevel@tonic-gate dev_info_t *scsi_vhci_dip; /* MPXIO dip */ 113*7c478bd9Sstevel@tonic-gate major_t clone_major; 114*7c478bd9Sstevel@tonic-gate 115*7c478bd9Sstevel@tonic-gate /* block all future dev_info state changes */ 116*7c478bd9Sstevel@tonic-gate static hrtime_t volatile devinfo_freeze = 0; 117*7c478bd9Sstevel@tonic-gate 118*7c478bd9Sstevel@tonic-gate /* number of dev_info attaches/detaches currently in progress */ 119*7c478bd9Sstevel@tonic-gate static ulong_t devinfo_attach_detach = 0; 120*7c478bd9Sstevel@tonic-gate 121*7c478bd9Sstevel@tonic-gate extern kmutex_t global_vhci_lock; 122*7c478bd9Sstevel@tonic-gate 123*7c478bd9Sstevel@tonic-gate /* 124*7c478bd9Sstevel@tonic-gate * The devinfo snapshot cache and related variables. 125*7c478bd9Sstevel@tonic-gate * The only field in the di_cache structure that needs initialization 126*7c478bd9Sstevel@tonic-gate * is the mutex (cache_lock). However, since this is an adaptive mutex 127*7c478bd9Sstevel@tonic-gate * (MUTEX_DEFAULT) - it is automatically initialized by being allocated 128*7c478bd9Sstevel@tonic-gate * in zeroed memory (static storage class). Therefore no explicit 129*7c478bd9Sstevel@tonic-gate * initialization of the di_cache structure is needed. 130*7c478bd9Sstevel@tonic-gate */ 131*7c478bd9Sstevel@tonic-gate struct di_cache di_cache = {1}; 132*7c478bd9Sstevel@tonic-gate int di_cache_debug = 0; 133*7c478bd9Sstevel@tonic-gate 134*7c478bd9Sstevel@tonic-gate /* For ddvis, which needs pseudo children under PCI */ 135*7c478bd9Sstevel@tonic-gate int pci_allow_pseudo_children = 0; 136*7c478bd9Sstevel@tonic-gate 137*7c478bd9Sstevel@tonic-gate /* 138*7c478bd9Sstevel@tonic-gate * The following switch is for service people, in case a 139*7c478bd9Sstevel@tonic-gate * 3rd party driver depends on identify(9e) being called. 140*7c478bd9Sstevel@tonic-gate */ 141*7c478bd9Sstevel@tonic-gate int identify_9e = 0; 142*7c478bd9Sstevel@tonic-gate 143*7c478bd9Sstevel@tonic-gate int mtc_off; /* turn off mt config */ 144*7c478bd9Sstevel@tonic-gate 145*7c478bd9Sstevel@tonic-gate static kmem_cache_t *ddi_node_cache; /* devinfo node cache */ 146*7c478bd9Sstevel@tonic-gate static devinfo_log_header_t *devinfo_audit_log; /* devinfo log */ 147*7c478bd9Sstevel@tonic-gate static int devinfo_log_size; /* size in pages */ 148*7c478bd9Sstevel@tonic-gate 149*7c478bd9Sstevel@tonic-gate static int lookup_compatible(dev_info_t *, uint_t); 150*7c478bd9Sstevel@tonic-gate static char *encode_composite_string(char **, uint_t, size_t *, uint_t); 151*7c478bd9Sstevel@tonic-gate static void link_to_driver_list(dev_info_t *); 152*7c478bd9Sstevel@tonic-gate static void unlink_from_driver_list(dev_info_t *); 153*7c478bd9Sstevel@tonic-gate static void add_to_dn_list(struct devnames *, dev_info_t *); 154*7c478bd9Sstevel@tonic-gate static void remove_from_dn_list(struct devnames *, dev_info_t *); 155*7c478bd9Sstevel@tonic-gate static dev_info_t *find_child_by_callback(dev_info_t *, char *, char *, 156*7c478bd9Sstevel@tonic-gate int (*)(dev_info_t *, char *, int)); 157*7c478bd9Sstevel@tonic-gate static dev_info_t *find_duplicate_child(); 158*7c478bd9Sstevel@tonic-gate static void add_global_props(dev_info_t *); 159*7c478bd9Sstevel@tonic-gate static void remove_global_props(dev_info_t *); 160*7c478bd9Sstevel@tonic-gate static int uninit_node(dev_info_t *); 161*7c478bd9Sstevel@tonic-gate static void da_log_init(void); 162*7c478bd9Sstevel@tonic-gate static void da_log_enter(dev_info_t *); 163*7c478bd9Sstevel@tonic-gate static int walk_devs(dev_info_t *, int (*f)(dev_info_t *, void *), void *, int); 164*7c478bd9Sstevel@tonic-gate static int reset_nexus_flags(dev_info_t *, void *); 165*7c478bd9Sstevel@tonic-gate static void ddi_optimize_dtree(dev_info_t *); 166*7c478bd9Sstevel@tonic-gate static int is_leaf_node(dev_info_t *); 167*7c478bd9Sstevel@tonic-gate static struct mt_config_handle *mt_config_init(dev_info_t *, dev_info_t **, 168*7c478bd9Sstevel@tonic-gate int, major_t, int, struct brevq_node **); 169*7c478bd9Sstevel@tonic-gate static void mt_config_children(struct mt_config_handle *); 170*7c478bd9Sstevel@tonic-gate static void mt_config_driver(struct mt_config_handle *); 171*7c478bd9Sstevel@tonic-gate static int mt_config_fini(struct mt_config_handle *); 172*7c478bd9Sstevel@tonic-gate static int devi_unconfig_common(dev_info_t *, dev_info_t **, int, major_t, 173*7c478bd9Sstevel@tonic-gate struct brevq_node **); 174*7c478bd9Sstevel@tonic-gate static int 175*7c478bd9Sstevel@tonic-gate ndi_devi_config_obp_args(dev_info_t *parent, char *devnm, 176*7c478bd9Sstevel@tonic-gate dev_info_t **childp, int flags); 177*7c478bd9Sstevel@tonic-gate 178*7c478bd9Sstevel@tonic-gate /* 179*7c478bd9Sstevel@tonic-gate * dev_info cache and node management 180*7c478bd9Sstevel@tonic-gate */ 181*7c478bd9Sstevel@tonic-gate 182*7c478bd9Sstevel@tonic-gate /* initialize dev_info node cache */ 183*7c478bd9Sstevel@tonic-gate void 184*7c478bd9Sstevel@tonic-gate i_ddi_node_cache_init() 185*7c478bd9Sstevel@tonic-gate { 186*7c478bd9Sstevel@tonic-gate ASSERT(ddi_node_cache == NULL); 187*7c478bd9Sstevel@tonic-gate ddi_node_cache = kmem_cache_create("dev_info_node_cache", 188*7c478bd9Sstevel@tonic-gate sizeof (struct dev_info), 0, NULL, NULL, NULL, NULL, NULL, 0); 189*7c478bd9Sstevel@tonic-gate 190*7c478bd9Sstevel@tonic-gate if (ddidebug & DDI_AUDIT) 191*7c478bd9Sstevel@tonic-gate da_log_init(); 192*7c478bd9Sstevel@tonic-gate } 193*7c478bd9Sstevel@tonic-gate 194*7c478bd9Sstevel@tonic-gate /* 195*7c478bd9Sstevel@tonic-gate * Allocating a dev_info node, callable from interrupt context with KM_NOSLEEP 196*7c478bd9Sstevel@tonic-gate * The allocated node has a reference count of 0. 197*7c478bd9Sstevel@tonic-gate */ 198*7c478bd9Sstevel@tonic-gate dev_info_t * 199*7c478bd9Sstevel@tonic-gate i_ddi_alloc_node(dev_info_t *pdip, char *node_name, dnode_t nodeid, 200*7c478bd9Sstevel@tonic-gate int instance, ddi_prop_t *sys_prop, int flag) 201*7c478bd9Sstevel@tonic-gate { 202*7c478bd9Sstevel@tonic-gate struct dev_info *devi; 203*7c478bd9Sstevel@tonic-gate struct devi_nodeid *elem; 204*7c478bd9Sstevel@tonic-gate static char failed[] = "i_ddi_alloc_node: out of memory"; 205*7c478bd9Sstevel@tonic-gate 206*7c478bd9Sstevel@tonic-gate ASSERT(node_name != NULL); 207*7c478bd9Sstevel@tonic-gate 208*7c478bd9Sstevel@tonic-gate if ((devi = kmem_cache_alloc(ddi_node_cache, flag)) == NULL) { 209*7c478bd9Sstevel@tonic-gate cmn_err(CE_NOTE, failed); 210*7c478bd9Sstevel@tonic-gate return (NULL); 211*7c478bd9Sstevel@tonic-gate } 212*7c478bd9Sstevel@tonic-gate 213*7c478bd9Sstevel@tonic-gate bzero(devi, sizeof (struct dev_info)); 214*7c478bd9Sstevel@tonic-gate 215*7c478bd9Sstevel@tonic-gate if (devinfo_audit_log) { 216*7c478bd9Sstevel@tonic-gate devi->devi_audit = kmem_zalloc(sizeof (devinfo_audit_t), flag); 217*7c478bd9Sstevel@tonic-gate if (devi->devi_audit == NULL) 218*7c478bd9Sstevel@tonic-gate goto fail; 219*7c478bd9Sstevel@tonic-gate } 220*7c478bd9Sstevel@tonic-gate 221*7c478bd9Sstevel@tonic-gate if ((devi->devi_node_name = i_ddi_strdup(node_name, flag)) == NULL) 222*7c478bd9Sstevel@tonic-gate goto fail; 223*7c478bd9Sstevel@tonic-gate /* default binding name is node name */ 224*7c478bd9Sstevel@tonic-gate devi->devi_binding_name = devi->devi_node_name; 225*7c478bd9Sstevel@tonic-gate devi->devi_major = (major_t)-1; /* unbound by default */ 226*7c478bd9Sstevel@tonic-gate 227*7c478bd9Sstevel@tonic-gate /* 228*7c478bd9Sstevel@tonic-gate * Make a copy of system property 229*7c478bd9Sstevel@tonic-gate */ 230*7c478bd9Sstevel@tonic-gate if (sys_prop && 231*7c478bd9Sstevel@tonic-gate (devi->devi_sys_prop_ptr = i_ddi_prop_list_dup(sys_prop, flag)) 232*7c478bd9Sstevel@tonic-gate == NULL) 233*7c478bd9Sstevel@tonic-gate goto fail; 234*7c478bd9Sstevel@tonic-gate 235*7c478bd9Sstevel@tonic-gate /* 236*7c478bd9Sstevel@tonic-gate * Assign devi_nodeid, devi_node_class, devi_node_attributes 237*7c478bd9Sstevel@tonic-gate * according to the following algorithm: 238*7c478bd9Sstevel@tonic-gate * 239*7c478bd9Sstevel@tonic-gate * nodeid arg node class node attributes 240*7c478bd9Sstevel@tonic-gate * 241*7c478bd9Sstevel@tonic-gate * DEVI_PSEUDO_NODEID DDI_NC_PSEUDO A 242*7c478bd9Sstevel@tonic-gate * DEVI_SID_NODEID DDI_NC_PSEUDO A,P 243*7c478bd9Sstevel@tonic-gate * other DDI_NC_PROM P 244*7c478bd9Sstevel@tonic-gate * 245*7c478bd9Sstevel@tonic-gate * Where A = DDI_AUTO_ASSIGNED_NODEID (auto-assign a nodeid) 246*7c478bd9Sstevel@tonic-gate * and P = DDI_PERSISTENT 247*7c478bd9Sstevel@tonic-gate * 248*7c478bd9Sstevel@tonic-gate * auto-assigned nodeids are also auto-freed. 249*7c478bd9Sstevel@tonic-gate */ 250*7c478bd9Sstevel@tonic-gate switch (nodeid) { 251*7c478bd9Sstevel@tonic-gate case DEVI_SID_NODEID: 252*7c478bd9Sstevel@tonic-gate devi->devi_node_attributes = DDI_PERSISTENT; 253*7c478bd9Sstevel@tonic-gate if ((elem = kmem_zalloc(sizeof (*elem), flag)) == NULL) 254*7c478bd9Sstevel@tonic-gate goto fail; 255*7c478bd9Sstevel@tonic-gate /*FALLTHROUGH*/ 256*7c478bd9Sstevel@tonic-gate case DEVI_PSEUDO_NODEID: 257*7c478bd9Sstevel@tonic-gate devi->devi_node_attributes |= DDI_AUTO_ASSIGNED_NODEID; 258*7c478bd9Sstevel@tonic-gate devi->devi_node_class = DDI_NC_PSEUDO; 259*7c478bd9Sstevel@tonic-gate if (impl_ddi_alloc_nodeid(&devi->devi_nodeid)) { 260*7c478bd9Sstevel@tonic-gate panic("i_ddi_alloc_node: out of nodeids"); 261*7c478bd9Sstevel@tonic-gate /*NOTREACHED*/ 262*7c478bd9Sstevel@tonic-gate } 263*7c478bd9Sstevel@tonic-gate break; 264*7c478bd9Sstevel@tonic-gate default: 265*7c478bd9Sstevel@tonic-gate if ((elem = kmem_zalloc(sizeof (*elem), flag)) == NULL) 266*7c478bd9Sstevel@tonic-gate goto fail; 267*7c478bd9Sstevel@tonic-gate /* 268*7c478bd9Sstevel@tonic-gate * the nodetype is 'prom', try to 'take' the nodeid now. 269*7c478bd9Sstevel@tonic-gate * This requires memory allocation, so check for failure. 270*7c478bd9Sstevel@tonic-gate */ 271*7c478bd9Sstevel@tonic-gate if (impl_ddi_take_nodeid(nodeid, flag) != 0) { 272*7c478bd9Sstevel@tonic-gate kmem_free(elem, sizeof (*elem)); 273*7c478bd9Sstevel@tonic-gate goto fail; 274*7c478bd9Sstevel@tonic-gate } 275*7c478bd9Sstevel@tonic-gate 276*7c478bd9Sstevel@tonic-gate devi->devi_nodeid = nodeid; 277*7c478bd9Sstevel@tonic-gate devi->devi_node_class = DDI_NC_PROM; 278*7c478bd9Sstevel@tonic-gate devi->devi_node_attributes = DDI_PERSISTENT; 279*7c478bd9Sstevel@tonic-gate 280*7c478bd9Sstevel@tonic-gate } 281*7c478bd9Sstevel@tonic-gate 282*7c478bd9Sstevel@tonic-gate if (ndi_dev_is_persistent_node((dev_info_t *)devi)) { 283*7c478bd9Sstevel@tonic-gate mutex_enter(&devimap->dno_lock); 284*7c478bd9Sstevel@tonic-gate elem->next = devimap->dno_free; 285*7c478bd9Sstevel@tonic-gate devimap->dno_free = elem; 286*7c478bd9Sstevel@tonic-gate mutex_exit(&devimap->dno_lock); 287*7c478bd9Sstevel@tonic-gate } 288*7c478bd9Sstevel@tonic-gate 289*7c478bd9Sstevel@tonic-gate /* 290*7c478bd9Sstevel@tonic-gate * Instance is normally initialized to -1. In a few special 291*7c478bd9Sstevel@tonic-gate * cases, the caller may specify an instance (e.g. CPU nodes). 292*7c478bd9Sstevel@tonic-gate */ 293*7c478bd9Sstevel@tonic-gate devi->devi_instance = instance; 294*7c478bd9Sstevel@tonic-gate 295*7c478bd9Sstevel@tonic-gate /* 296*7c478bd9Sstevel@tonic-gate * set parent and bus_ctl parent 297*7c478bd9Sstevel@tonic-gate */ 298*7c478bd9Sstevel@tonic-gate devi->devi_parent = DEVI(pdip); 299*7c478bd9Sstevel@tonic-gate devi->devi_bus_ctl = DEVI(pdip); 300*7c478bd9Sstevel@tonic-gate 301*7c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_CONT, 302*7c478bd9Sstevel@tonic-gate "i_ddi_alloc_node: name=%s id=%d\n", node_name, devi->devi_nodeid)); 303*7c478bd9Sstevel@tonic-gate 304*7c478bd9Sstevel@tonic-gate cv_init(&(devi->devi_cv), NULL, CV_DEFAULT, NULL); 305*7c478bd9Sstevel@tonic-gate mutex_init(&(devi->devi_lock), NULL, MUTEX_DEFAULT, NULL); 306*7c478bd9Sstevel@tonic-gate mutex_init(&(devi->devi_pm_lock), NULL, MUTEX_DEFAULT, NULL); 307*7c478bd9Sstevel@tonic-gate mutex_init(&(devi->devi_pm_busy_lock), NULL, MUTEX_DEFAULT, NULL); 308*7c478bd9Sstevel@tonic-gate 309*7c478bd9Sstevel@tonic-gate i_ddi_set_node_state((dev_info_t *)devi, DS_PROTO); 310*7c478bd9Sstevel@tonic-gate da_log_enter((dev_info_t *)devi); 311*7c478bd9Sstevel@tonic-gate return ((dev_info_t *)devi); 312*7c478bd9Sstevel@tonic-gate 313*7c478bd9Sstevel@tonic-gate fail: 314*7c478bd9Sstevel@tonic-gate if (devi->devi_sys_prop_ptr) 315*7c478bd9Sstevel@tonic-gate i_ddi_prop_list_delete(devi->devi_sys_prop_ptr); 316*7c478bd9Sstevel@tonic-gate if (devi->devi_node_name) 317*7c478bd9Sstevel@tonic-gate kmem_free(devi->devi_node_name, strlen(node_name) + 1); 318*7c478bd9Sstevel@tonic-gate if (devi->devi_audit) 319*7c478bd9Sstevel@tonic-gate kmem_free(devi->devi_audit, sizeof (devinfo_audit_t)); 320*7c478bd9Sstevel@tonic-gate kmem_cache_free(ddi_node_cache, devi); 321*7c478bd9Sstevel@tonic-gate cmn_err(CE_NOTE, failed); 322*7c478bd9Sstevel@tonic-gate return (NULL); 323*7c478bd9Sstevel@tonic-gate } 324*7c478bd9Sstevel@tonic-gate 325*7c478bd9Sstevel@tonic-gate /* 326*7c478bd9Sstevel@tonic-gate * free a dev_info structure. 327*7c478bd9Sstevel@tonic-gate * NB. Not callable from interrupt since impl_ddi_free_nodeid may block. 328*7c478bd9Sstevel@tonic-gate */ 329*7c478bd9Sstevel@tonic-gate void 330*7c478bd9Sstevel@tonic-gate i_ddi_free_node(dev_info_t *dip) 331*7c478bd9Sstevel@tonic-gate { 332*7c478bd9Sstevel@tonic-gate struct dev_info *devi = DEVI(dip); 333*7c478bd9Sstevel@tonic-gate struct devi_nodeid *elem; 334*7c478bd9Sstevel@tonic-gate 335*7c478bd9Sstevel@tonic-gate ASSERT(devi->devi_ref == 0); 336*7c478bd9Sstevel@tonic-gate ASSERT(devi->devi_addr == NULL); 337*7c478bd9Sstevel@tonic-gate ASSERT(devi->devi_node_state == DS_PROTO); 338*7c478bd9Sstevel@tonic-gate ASSERT(devi->devi_child == NULL); 339*7c478bd9Sstevel@tonic-gate 340*7c478bd9Sstevel@tonic-gate if (devi->devi_intr_p) 341*7c478bd9Sstevel@tonic-gate i_ddi_intr_devi_fini((dev_info_t *)devi); 342*7c478bd9Sstevel@tonic-gate 343*7c478bd9Sstevel@tonic-gate /* free devi_addr */ 344*7c478bd9Sstevel@tonic-gate ddi_set_name_addr(dip, NULL); 345*7c478bd9Sstevel@tonic-gate 346*7c478bd9Sstevel@tonic-gate if (i_ndi_dev_is_auto_assigned_node(dip)) 347*7c478bd9Sstevel@tonic-gate impl_ddi_free_nodeid(DEVI(dip)->devi_nodeid); 348*7c478bd9Sstevel@tonic-gate 349*7c478bd9Sstevel@tonic-gate if (ndi_dev_is_persistent_node(dip)) { 350*7c478bd9Sstevel@tonic-gate mutex_enter(&devimap->dno_lock); 351*7c478bd9Sstevel@tonic-gate ASSERT(devimap->dno_free); 352*7c478bd9Sstevel@tonic-gate elem = devimap->dno_free; 353*7c478bd9Sstevel@tonic-gate devimap->dno_free = elem->next; 354*7c478bd9Sstevel@tonic-gate mutex_exit(&devimap->dno_lock); 355*7c478bd9Sstevel@tonic-gate kmem_free(elem, sizeof (*elem)); 356*7c478bd9Sstevel@tonic-gate } 357*7c478bd9Sstevel@tonic-gate 358*7c478bd9Sstevel@tonic-gate if (DEVI(dip)->devi_compat_names) 359*7c478bd9Sstevel@tonic-gate kmem_free(DEVI(dip)->devi_compat_names, 360*7c478bd9Sstevel@tonic-gate DEVI(dip)->devi_compat_length); 361*7c478bd9Sstevel@tonic-gate 362*7c478bd9Sstevel@tonic-gate ddi_prop_remove_all(dip); /* remove driver properties */ 363*7c478bd9Sstevel@tonic-gate if (devi->devi_sys_prop_ptr) 364*7c478bd9Sstevel@tonic-gate i_ddi_prop_list_delete(devi->devi_sys_prop_ptr); 365*7c478bd9Sstevel@tonic-gate if (devi->devi_hw_prop_ptr) 366*7c478bd9Sstevel@tonic-gate i_ddi_prop_list_delete(devi->devi_hw_prop_ptr); 367*7c478bd9Sstevel@tonic-gate 368*7c478bd9Sstevel@tonic-gate i_ddi_set_node_state(dip, DS_INVAL); 369*7c478bd9Sstevel@tonic-gate da_log_enter(dip); 370*7c478bd9Sstevel@tonic-gate if (devi->devi_audit) { 371*7c478bd9Sstevel@tonic-gate kmem_free(devi->devi_audit, sizeof (devinfo_audit_t)); 372*7c478bd9Sstevel@tonic-gate } 373*7c478bd9Sstevel@tonic-gate kmem_free(devi->devi_node_name, strlen(devi->devi_node_name) + 1); 374*7c478bd9Sstevel@tonic-gate if (devi->devi_device_class) 375*7c478bd9Sstevel@tonic-gate kmem_free(devi->devi_device_class, 376*7c478bd9Sstevel@tonic-gate strlen(devi->devi_device_class) + 1); 377*7c478bd9Sstevel@tonic-gate cv_destroy(&(devi->devi_cv)); 378*7c478bd9Sstevel@tonic-gate mutex_destroy(&(devi->devi_lock)); 379*7c478bd9Sstevel@tonic-gate mutex_destroy(&(devi->devi_pm_lock)); 380*7c478bd9Sstevel@tonic-gate mutex_destroy(&(devi->devi_pm_busy_lock)); 381*7c478bd9Sstevel@tonic-gate 382*7c478bd9Sstevel@tonic-gate kmem_cache_free(ddi_node_cache, devi); 383*7c478bd9Sstevel@tonic-gate } 384*7c478bd9Sstevel@tonic-gate 385*7c478bd9Sstevel@tonic-gate 386*7c478bd9Sstevel@tonic-gate /* 387*7c478bd9Sstevel@tonic-gate * Node state transitions 388*7c478bd9Sstevel@tonic-gate */ 389*7c478bd9Sstevel@tonic-gate 390*7c478bd9Sstevel@tonic-gate /* 391*7c478bd9Sstevel@tonic-gate * Change the node name 392*7c478bd9Sstevel@tonic-gate */ 393*7c478bd9Sstevel@tonic-gate int 394*7c478bd9Sstevel@tonic-gate ndi_devi_set_nodename(dev_info_t *dip, char *name, int flags) 395*7c478bd9Sstevel@tonic-gate { 396*7c478bd9Sstevel@tonic-gate _NOTE(ARGUNUSED(flags)) 397*7c478bd9Sstevel@tonic-gate char *nname, *oname; 398*7c478bd9Sstevel@tonic-gate 399*7c478bd9Sstevel@tonic-gate ASSERT(dip && name); 400*7c478bd9Sstevel@tonic-gate 401*7c478bd9Sstevel@tonic-gate oname = DEVI(dip)->devi_node_name; 402*7c478bd9Sstevel@tonic-gate if (strcmp(oname, name) == 0) 403*7c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 404*7c478bd9Sstevel@tonic-gate 405*7c478bd9Sstevel@tonic-gate /* 406*7c478bd9Sstevel@tonic-gate * pcicfg_fix_ethernet requires a name change after node 407*7c478bd9Sstevel@tonic-gate * is linked into the tree. When pcicfg is fixed, we 408*7c478bd9Sstevel@tonic-gate * should only allow name change in DS_PROTO state. 409*7c478bd9Sstevel@tonic-gate */ 410*7c478bd9Sstevel@tonic-gate if (i_ddi_node_state(dip) >= DS_BOUND) { 411*7c478bd9Sstevel@tonic-gate /* 412*7c478bd9Sstevel@tonic-gate * Don't allow name change once node is bound 413*7c478bd9Sstevel@tonic-gate */ 414*7c478bd9Sstevel@tonic-gate cmn_err(CE_NOTE, 415*7c478bd9Sstevel@tonic-gate "ndi_devi_set_nodename: node already bound dip = %p," 416*7c478bd9Sstevel@tonic-gate " %s -> %s", (void *)dip, ddi_node_name(dip), name); 417*7c478bd9Sstevel@tonic-gate return (NDI_FAILURE); 418*7c478bd9Sstevel@tonic-gate } 419*7c478bd9Sstevel@tonic-gate 420*7c478bd9Sstevel@tonic-gate nname = i_ddi_strdup(name, KM_SLEEP); 421*7c478bd9Sstevel@tonic-gate DEVI(dip)->devi_node_name = nname; 422*7c478bd9Sstevel@tonic-gate i_ddi_set_binding_name(dip, nname); 423*7c478bd9Sstevel@tonic-gate kmem_free(oname, strlen(oname) + 1); 424*7c478bd9Sstevel@tonic-gate 425*7c478bd9Sstevel@tonic-gate da_log_enter(dip); 426*7c478bd9Sstevel@tonic-gate return (NDI_SUCCESS); 427*7c478bd9Sstevel@tonic-gate } 428*7c478bd9Sstevel@tonic-gate 429*7c478bd9Sstevel@tonic-gate void 430*7c478bd9Sstevel@tonic-gate i_ddi_add_devimap(dev_info_t *dip) 431*7c478bd9Sstevel@tonic-gate { 432*7c478bd9Sstevel@tonic-gate struct devi_nodeid *elem; 433*7c478bd9Sstevel@tonic-gate 434*7c478bd9Sstevel@tonic-gate ASSERT(dip); 435*7c478bd9Sstevel@tonic-gate 436*7c478bd9Sstevel@tonic-gate if (!ndi_dev_is_persistent_node(dip)) 437*7c478bd9Sstevel@tonic-gate return; 438*7c478bd9Sstevel@tonic-gate 439*7c478bd9Sstevel@tonic-gate ASSERT(ddi_get_parent(dip) == NULL || (DEVI_VHCI_NODE(dip)) || 440*7c478bd9Sstevel@tonic-gate DEVI_BUSY_OWNED(ddi_get_parent(dip))); 441*7c478bd9Sstevel@tonic-gate 442*7c478bd9Sstevel@tonic-gate mutex_enter(&devimap->dno_lock); 443*7c478bd9Sstevel@tonic-gate 444*7c478bd9Sstevel@tonic-gate ASSERT(devimap->dno_free); 445*7c478bd9Sstevel@tonic-gate 446*7c478bd9Sstevel@tonic-gate elem = devimap->dno_free; 447*7c478bd9Sstevel@tonic-gate devimap->dno_free = elem->next; 448*7c478bd9Sstevel@tonic-gate 449*7c478bd9Sstevel@tonic-gate elem->nodeid = ddi_get_nodeid(dip); 450*7c478bd9Sstevel@tonic-gate elem->dip = dip; 451*7c478bd9Sstevel@tonic-gate elem->next = devimap->dno_head; 452*7c478bd9Sstevel@tonic-gate devimap->dno_head = elem; 453*7c478bd9Sstevel@tonic-gate 454*7c478bd9Sstevel@tonic-gate devimap->dno_list_length++; 455*7c478bd9Sstevel@tonic-gate 456*7c478bd9Sstevel@tonic-gate mutex_exit(&devimap->dno_lock); 457*7c478bd9Sstevel@tonic-gate } 458*7c478bd9Sstevel@tonic-gate 459*7c478bd9Sstevel@tonic-gate static int 460*7c478bd9Sstevel@tonic-gate i_ddi_remove_devimap(dev_info_t *dip) 461*7c478bd9Sstevel@tonic-gate { 462*7c478bd9Sstevel@tonic-gate struct devi_nodeid *prev, *elem; 463*7c478bd9Sstevel@tonic-gate static const char *fcn = "i_ddi_remove_devimap"; 464*7c478bd9Sstevel@tonic-gate 465*7c478bd9Sstevel@tonic-gate ASSERT(dip); 466*7c478bd9Sstevel@tonic-gate 467*7c478bd9Sstevel@tonic-gate if (!ndi_dev_is_persistent_node(dip)) 468*7c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 469*7c478bd9Sstevel@tonic-gate 470*7c478bd9Sstevel@tonic-gate mutex_enter(&devimap->dno_lock); 471*7c478bd9Sstevel@tonic-gate 472*7c478bd9Sstevel@tonic-gate /* 473*7c478bd9Sstevel@tonic-gate * The following check is done with dno_lock held 474*7c478bd9Sstevel@tonic-gate * to prevent race between dip removal and 475*7c478bd9Sstevel@tonic-gate * e_ddi_prom_node_to_dip() 476*7c478bd9Sstevel@tonic-gate */ 477*7c478bd9Sstevel@tonic-gate if (e_ddi_devi_holdcnt(dip)) { 478*7c478bd9Sstevel@tonic-gate mutex_exit(&devimap->dno_lock); 479*7c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 480*7c478bd9Sstevel@tonic-gate } 481*7c478bd9Sstevel@tonic-gate 482*7c478bd9Sstevel@tonic-gate ASSERT(devimap->dno_head); 483*7c478bd9Sstevel@tonic-gate ASSERT(devimap->dno_list_length > 0); 484*7c478bd9Sstevel@tonic-gate 485*7c478bd9Sstevel@tonic-gate prev = NULL; 486*7c478bd9Sstevel@tonic-gate for (elem = devimap->dno_head; elem; elem = elem->next) { 487*7c478bd9Sstevel@tonic-gate if (elem->dip == dip) { 488*7c478bd9Sstevel@tonic-gate ASSERT(elem->nodeid == ddi_get_nodeid(dip)); 489*7c478bd9Sstevel@tonic-gate break; 490*7c478bd9Sstevel@tonic-gate } 491*7c478bd9Sstevel@tonic-gate prev = elem; 492*7c478bd9Sstevel@tonic-gate } 493*7c478bd9Sstevel@tonic-gate 494*7c478bd9Sstevel@tonic-gate if (elem && prev) 495*7c478bd9Sstevel@tonic-gate prev->next = elem->next; 496*7c478bd9Sstevel@tonic-gate else if (elem) 497*7c478bd9Sstevel@tonic-gate devimap->dno_head = elem->next; 498*7c478bd9Sstevel@tonic-gate else 499*7c478bd9Sstevel@tonic-gate panic("%s: devinfo node(%p) not found", 500*7c478bd9Sstevel@tonic-gate fcn, (void *)dip); 501*7c478bd9Sstevel@tonic-gate 502*7c478bd9Sstevel@tonic-gate devimap->dno_list_length--; 503*7c478bd9Sstevel@tonic-gate 504*7c478bd9Sstevel@tonic-gate elem->nodeid = 0; 505*7c478bd9Sstevel@tonic-gate elem->dip = NULL; 506*7c478bd9Sstevel@tonic-gate 507*7c478bd9Sstevel@tonic-gate elem->next = devimap->dno_free; 508*7c478bd9Sstevel@tonic-gate devimap->dno_free = elem; 509*7c478bd9Sstevel@tonic-gate 510*7c478bd9Sstevel@tonic-gate mutex_exit(&devimap->dno_lock); 511*7c478bd9Sstevel@tonic-gate 512*7c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 513*7c478bd9Sstevel@tonic-gate } 514*7c478bd9Sstevel@tonic-gate 515*7c478bd9Sstevel@tonic-gate /* 516*7c478bd9Sstevel@tonic-gate * Link this node into the devinfo tree and add to orphan list 517*7c478bd9Sstevel@tonic-gate * Not callable from interrupt context 518*7c478bd9Sstevel@tonic-gate */ 519*7c478bd9Sstevel@tonic-gate static void 520*7c478bd9Sstevel@tonic-gate link_node(dev_info_t *dip) 521*7c478bd9Sstevel@tonic-gate { 522*7c478bd9Sstevel@tonic-gate struct dev_info *devi = DEVI(dip); 523*7c478bd9Sstevel@tonic-gate struct dev_info *parent = devi->devi_parent; 524*7c478bd9Sstevel@tonic-gate dev_info_t **dipp; 525*7c478bd9Sstevel@tonic-gate 526*7c478bd9Sstevel@tonic-gate ASSERT(parent); /* never called for root node */ 527*7c478bd9Sstevel@tonic-gate 528*7c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_CONT, "link_node: parent = %s child = %s\n", 529*7c478bd9Sstevel@tonic-gate parent->devi_node_name, devi->devi_node_name)); 530*7c478bd9Sstevel@tonic-gate 531*7c478bd9Sstevel@tonic-gate /* 532*7c478bd9Sstevel@tonic-gate * Hold the global_vhci_lock before linking any direct 533*7c478bd9Sstevel@tonic-gate * children of rootnex driver. This special lock protects 534*7c478bd9Sstevel@tonic-gate * linking and unlinking for rootnext direct children. 535*7c478bd9Sstevel@tonic-gate */ 536*7c478bd9Sstevel@tonic-gate if ((dev_info_t *)parent == ddi_root_node()) 537*7c478bd9Sstevel@tonic-gate mutex_enter(&global_vhci_lock); 538*7c478bd9Sstevel@tonic-gate 539*7c478bd9Sstevel@tonic-gate /* 540*7c478bd9Sstevel@tonic-gate * attach the node to end of the list unless the node is already there 541*7c478bd9Sstevel@tonic-gate */ 542*7c478bd9Sstevel@tonic-gate dipp = (dev_info_t **)(&DEVI(parent)->devi_child); 543*7c478bd9Sstevel@tonic-gate while (*dipp && (*dipp != dip)) { 544*7c478bd9Sstevel@tonic-gate dipp = (dev_info_t **)(&DEVI(*dipp)->devi_sibling); 545*7c478bd9Sstevel@tonic-gate } 546*7c478bd9Sstevel@tonic-gate ASSERT(*dipp == NULL); /* node is not linked */ 547*7c478bd9Sstevel@tonic-gate 548*7c478bd9Sstevel@tonic-gate /* 549*7c478bd9Sstevel@tonic-gate * Now that we are in the tree, update the devi-nodeid map. 550*7c478bd9Sstevel@tonic-gate */ 551*7c478bd9Sstevel@tonic-gate i_ddi_add_devimap(dip); 552*7c478bd9Sstevel@tonic-gate 553*7c478bd9Sstevel@tonic-gate /* 554*7c478bd9Sstevel@tonic-gate * This is a temporary workaround for Bug 4618861. 555*7c478bd9Sstevel@tonic-gate * We keep the scsi_vhci nexus node on the left side of the devinfo 556*7c478bd9Sstevel@tonic-gate * tree (under the root nexus driver), so that virtual nodes under 557*7c478bd9Sstevel@tonic-gate * scsi_vhci will be SUSPENDed first and RESUMEd last. This ensures 558*7c478bd9Sstevel@tonic-gate * that the pHCI nodes are active during times when their clients 559*7c478bd9Sstevel@tonic-gate * may be depending on them. This workaround embodies the knowledge 560*7c478bd9Sstevel@tonic-gate * that system PM and CPR both traverse the tree left-to-right during 561*7c478bd9Sstevel@tonic-gate * SUSPEND and right-to-left during RESUME. 562*7c478bd9Sstevel@tonic-gate */ 563*7c478bd9Sstevel@tonic-gate if (strcmp(devi->devi_name, "scsi_vhci") == 0) { 564*7c478bd9Sstevel@tonic-gate /* Add scsi_vhci to beginning of list */ 565*7c478bd9Sstevel@tonic-gate ASSERT((dev_info_t *)parent == top_devinfo); 566*7c478bd9Sstevel@tonic-gate /* scsi_vhci under rootnex */ 567*7c478bd9Sstevel@tonic-gate devi->devi_sibling = parent->devi_child; 568*7c478bd9Sstevel@tonic-gate parent->devi_child = devi; 569*7c478bd9Sstevel@tonic-gate } else { 570*7c478bd9Sstevel@tonic-gate /* Add to end of list */ 571*7c478bd9Sstevel@tonic-gate *dipp = dip; 572*7c478bd9Sstevel@tonic-gate DEVI(dip)->devi_sibling = NULL; 573*7c478bd9Sstevel@tonic-gate } 574*7c478bd9Sstevel@tonic-gate 575*7c478bd9Sstevel@tonic-gate /* 576*7c478bd9Sstevel@tonic-gate * Release the global_vhci_lock before linking any direct 577*7c478bd9Sstevel@tonic-gate * children of rootnex driver. 578*7c478bd9Sstevel@tonic-gate */ 579*7c478bd9Sstevel@tonic-gate if ((dev_info_t *)parent == ddi_root_node()) 580*7c478bd9Sstevel@tonic-gate mutex_exit(&global_vhci_lock); 581*7c478bd9Sstevel@tonic-gate 582*7c478bd9Sstevel@tonic-gate /* persistent nodes go on orphan list */ 583*7c478bd9Sstevel@tonic-gate if (ndi_dev_is_persistent_node(dip)) 584*7c478bd9Sstevel@tonic-gate add_to_dn_list(&orphanlist, dip); 585*7c478bd9Sstevel@tonic-gate } 586*7c478bd9Sstevel@tonic-gate 587*7c478bd9Sstevel@tonic-gate /* 588*7c478bd9Sstevel@tonic-gate * Unlink this node from the devinfo tree 589*7c478bd9Sstevel@tonic-gate */ 590*7c478bd9Sstevel@tonic-gate static int 591*7c478bd9Sstevel@tonic-gate unlink_node(dev_info_t *dip) 592*7c478bd9Sstevel@tonic-gate { 593*7c478bd9Sstevel@tonic-gate struct dev_info *devi = DEVI(dip); 594*7c478bd9Sstevel@tonic-gate struct dev_info *parent = devi->devi_parent; 595*7c478bd9Sstevel@tonic-gate dev_info_t **dipp; 596*7c478bd9Sstevel@tonic-gate 597*7c478bd9Sstevel@tonic-gate ASSERT(parent != NULL); 598*7c478bd9Sstevel@tonic-gate ASSERT(devi->devi_node_state == DS_LINKED); 599*7c478bd9Sstevel@tonic-gate 600*7c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_CONT, "unlink_node: name = %s\n", 601*7c478bd9Sstevel@tonic-gate ddi_node_name(dip))); 602*7c478bd9Sstevel@tonic-gate 603*7c478bd9Sstevel@tonic-gate /* check references */ 604*7c478bd9Sstevel@tonic-gate if (devi->devi_ref || i_ddi_remove_devimap(dip) != DDI_SUCCESS) 605*7c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 606*7c478bd9Sstevel@tonic-gate 607*7c478bd9Sstevel@tonic-gate /* 608*7c478bd9Sstevel@tonic-gate * Hold the global_vhci_lock before linking any direct 609*7c478bd9Sstevel@tonic-gate * children of rootnex driver. 610*7c478bd9Sstevel@tonic-gate */ 611*7c478bd9Sstevel@tonic-gate if ((dev_info_t *)parent == ddi_root_node()) 612*7c478bd9Sstevel@tonic-gate mutex_enter(&global_vhci_lock); 613*7c478bd9Sstevel@tonic-gate 614*7c478bd9Sstevel@tonic-gate dipp = (dev_info_t **)(&DEVI(parent)->devi_child); 615*7c478bd9Sstevel@tonic-gate while (*dipp && (*dipp != dip)) { 616*7c478bd9Sstevel@tonic-gate dipp = (dev_info_t **)(&DEVI(*dipp)->devi_sibling); 617*7c478bd9Sstevel@tonic-gate } 618*7c478bd9Sstevel@tonic-gate if (*dipp) { 619*7c478bd9Sstevel@tonic-gate *dipp = (dev_info_t *)(devi->devi_sibling); 620*7c478bd9Sstevel@tonic-gate devi->devi_sibling = NULL; 621*7c478bd9Sstevel@tonic-gate } else { 622*7c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_NOTE, "unlink_node: %s not linked", 623*7c478bd9Sstevel@tonic-gate devi->devi_node_name)); 624*7c478bd9Sstevel@tonic-gate } 625*7c478bd9Sstevel@tonic-gate 626*7c478bd9Sstevel@tonic-gate /* 627*7c478bd9Sstevel@tonic-gate * Release the global_vhci_lock before linking any direct 628*7c478bd9Sstevel@tonic-gate * children of rootnex driver. 629*7c478bd9Sstevel@tonic-gate */ 630*7c478bd9Sstevel@tonic-gate if ((dev_info_t *)parent == ddi_root_node()) 631*7c478bd9Sstevel@tonic-gate mutex_exit(&global_vhci_lock); 632*7c478bd9Sstevel@tonic-gate 633*7c478bd9Sstevel@tonic-gate /* Remove node from orphan list */ 634*7c478bd9Sstevel@tonic-gate if (ndi_dev_is_persistent_node(dip)) { 635*7c478bd9Sstevel@tonic-gate remove_from_dn_list(&orphanlist, dip); 636*7c478bd9Sstevel@tonic-gate } 637*7c478bd9Sstevel@tonic-gate 638*7c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 639*7c478bd9Sstevel@tonic-gate } 640*7c478bd9Sstevel@tonic-gate 641*7c478bd9Sstevel@tonic-gate /* 642*7c478bd9Sstevel@tonic-gate * Bind this devinfo node to a driver. If compat is NON-NULL, try that first. 643*7c478bd9Sstevel@tonic-gate * Else, use the node-name. 644*7c478bd9Sstevel@tonic-gate * 645*7c478bd9Sstevel@tonic-gate * NOTE: IEEE1275 specifies that nodename should be tried before compatible. 646*7c478bd9Sstevel@tonic-gate * Solaris implementation binds nodename after compatible. 647*7c478bd9Sstevel@tonic-gate * 648*7c478bd9Sstevel@tonic-gate * If we find a binding, 649*7c478bd9Sstevel@tonic-gate * - set the binding name to the the string, 650*7c478bd9Sstevel@tonic-gate * - set major number to driver major 651*7c478bd9Sstevel@tonic-gate * 652*7c478bd9Sstevel@tonic-gate * If we don't find a binding, 653*7c478bd9Sstevel@tonic-gate * - return failure 654*7c478bd9Sstevel@tonic-gate */ 655*7c478bd9Sstevel@tonic-gate static int 656*7c478bd9Sstevel@tonic-gate bind_node(dev_info_t *dip) 657*7c478bd9Sstevel@tonic-gate { 658*7c478bd9Sstevel@tonic-gate char *p = NULL; 659*7c478bd9Sstevel@tonic-gate major_t major = (major_t)(major_t)-1; 660*7c478bd9Sstevel@tonic-gate struct dev_info *devi = DEVI(dip); 661*7c478bd9Sstevel@tonic-gate dev_info_t *parent = ddi_get_parent(dip); 662*7c478bd9Sstevel@tonic-gate 663*7c478bd9Sstevel@tonic-gate ASSERT(devi->devi_node_state == DS_LINKED); 664*7c478bd9Sstevel@tonic-gate 665*7c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_CONT, "bind_node: 0x%p(name = %s)\n", 666*7c478bd9Sstevel@tonic-gate (void *)dip, ddi_node_name(dip))); 667*7c478bd9Sstevel@tonic-gate 668*7c478bd9Sstevel@tonic-gate mutex_enter(&DEVI(dip)->devi_lock); 669*7c478bd9Sstevel@tonic-gate if (DEVI(dip)->devi_flags & DEVI_NO_BIND) { 670*7c478bd9Sstevel@tonic-gate mutex_exit(&DEVI(dip)->devi_lock); 671*7c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 672*7c478bd9Sstevel@tonic-gate } 673*7c478bd9Sstevel@tonic-gate mutex_exit(&DEVI(dip)->devi_lock); 674*7c478bd9Sstevel@tonic-gate 675*7c478bd9Sstevel@tonic-gate /* find the driver with most specific binding using compatible */ 676*7c478bd9Sstevel@tonic-gate major = ddi_compatible_driver_major(dip, &p); 677*7c478bd9Sstevel@tonic-gate if (major == (major_t)-1) 678*7c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 679*7c478bd9Sstevel@tonic-gate 680*7c478bd9Sstevel@tonic-gate devi->devi_major = major; 681*7c478bd9Sstevel@tonic-gate if (p != NULL) { 682*7c478bd9Sstevel@tonic-gate i_ddi_set_binding_name(dip, p); 683*7c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_CONT, "bind_node: %s bound to %s\n", 684*7c478bd9Sstevel@tonic-gate devi->devi_node_name, p)); 685*7c478bd9Sstevel@tonic-gate } 686*7c478bd9Sstevel@tonic-gate 687*7c478bd9Sstevel@tonic-gate /* Link node to per-driver list */ 688*7c478bd9Sstevel@tonic-gate link_to_driver_list(dip); 689*7c478bd9Sstevel@tonic-gate 690*7c478bd9Sstevel@tonic-gate /* 691*7c478bd9Sstevel@tonic-gate * reset parent flag so that nexus will merge .conf props 692*7c478bd9Sstevel@tonic-gate */ 693*7c478bd9Sstevel@tonic-gate if (ndi_dev_is_persistent_node(dip)) { 694*7c478bd9Sstevel@tonic-gate mutex_enter(&DEVI(parent)->devi_lock); 695*7c478bd9Sstevel@tonic-gate DEVI(parent)->devi_flags &= 696*7c478bd9Sstevel@tonic-gate ~(DEVI_ATTACHED_CHILDREN|DEVI_MADE_CHILDREN); 697*7c478bd9Sstevel@tonic-gate mutex_exit(&DEVI(parent)->devi_lock); 698*7c478bd9Sstevel@tonic-gate } 699*7c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 700*7c478bd9Sstevel@tonic-gate } 701*7c478bd9Sstevel@tonic-gate 702*7c478bd9Sstevel@tonic-gate /* 703*7c478bd9Sstevel@tonic-gate * Unbind this devinfo node 704*7c478bd9Sstevel@tonic-gate * Called before the node is destroyed or driver is removed from system 705*7c478bd9Sstevel@tonic-gate */ 706*7c478bd9Sstevel@tonic-gate static int 707*7c478bd9Sstevel@tonic-gate unbind_node(dev_info_t *dip) 708*7c478bd9Sstevel@tonic-gate { 709*7c478bd9Sstevel@tonic-gate ASSERT(DEVI(dip)->devi_node_state == DS_BOUND); 710*7c478bd9Sstevel@tonic-gate ASSERT(DEVI(dip)->devi_major != (major_t)-1); 711*7c478bd9Sstevel@tonic-gate 712*7c478bd9Sstevel@tonic-gate /* check references */ 713*7c478bd9Sstevel@tonic-gate if (DEVI(dip)->devi_ref) 714*7c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 715*7c478bd9Sstevel@tonic-gate 716*7c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_CONT, "unbind_node: 0x%p(name = %s)\n", 717*7c478bd9Sstevel@tonic-gate (void *)dip, ddi_node_name(dip))); 718*7c478bd9Sstevel@tonic-gate 719*7c478bd9Sstevel@tonic-gate unlink_from_driver_list(dip); 720*7c478bd9Sstevel@tonic-gate DEVI(dip)->devi_major = (major_t)-1; 721*7c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 722*7c478bd9Sstevel@tonic-gate } 723*7c478bd9Sstevel@tonic-gate 724*7c478bd9Sstevel@tonic-gate /* 725*7c478bd9Sstevel@tonic-gate * Initialize a node: calls the parent nexus' bus_ctl ops to do the operation. 726*7c478bd9Sstevel@tonic-gate * Must hold parent and per-driver list while calling this function. 727*7c478bd9Sstevel@tonic-gate * A successful init_node() returns with an active ndi_hold_devi() hold on 728*7c478bd9Sstevel@tonic-gate * the parent. 729*7c478bd9Sstevel@tonic-gate */ 730*7c478bd9Sstevel@tonic-gate static int 731*7c478bd9Sstevel@tonic-gate init_node(dev_info_t *dip) 732*7c478bd9Sstevel@tonic-gate { 733*7c478bd9Sstevel@tonic-gate int error; 734*7c478bd9Sstevel@tonic-gate dev_info_t *pdip = ddi_get_parent(dip); 735*7c478bd9Sstevel@tonic-gate int (*f)(dev_info_t *, dev_info_t *, ddi_ctl_enum_t, void *, void *); 736*7c478bd9Sstevel@tonic-gate char *path; 737*7c478bd9Sstevel@tonic-gate 738*7c478bd9Sstevel@tonic-gate ASSERT(i_ddi_node_state(dip) == DS_BOUND); 739*7c478bd9Sstevel@tonic-gate 740*7c478bd9Sstevel@tonic-gate /* should be DS_READY except for pcmcia ... */ 741*7c478bd9Sstevel@tonic-gate ASSERT(i_ddi_node_state(pdip) >= DS_PROBED); 742*7c478bd9Sstevel@tonic-gate 743*7c478bd9Sstevel@tonic-gate path = kmem_alloc(MAXPATHLEN, KM_SLEEP); 744*7c478bd9Sstevel@tonic-gate (void) ddi_pathname(dip, path); 745*7c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_CONT, "init_node: entry: path %s 0x%p\n", 746*7c478bd9Sstevel@tonic-gate path, (void *)dip)); 747*7c478bd9Sstevel@tonic-gate 748*7c478bd9Sstevel@tonic-gate /* 749*7c478bd9Sstevel@tonic-gate * The parent must have a bus_ctl operation. 750*7c478bd9Sstevel@tonic-gate */ 751*7c478bd9Sstevel@tonic-gate if ((DEVI(pdip)->devi_ops->devo_bus_ops == NULL) || 752*7c478bd9Sstevel@tonic-gate (f = DEVI(pdip)->devi_ops->devo_bus_ops->bus_ctl) == NULL) { 753*7c478bd9Sstevel@tonic-gate error = DDI_FAILURE; 754*7c478bd9Sstevel@tonic-gate goto out; 755*7c478bd9Sstevel@tonic-gate } 756*7c478bd9Sstevel@tonic-gate 757*7c478bd9Sstevel@tonic-gate add_global_props(dip); 758*7c478bd9Sstevel@tonic-gate 759*7c478bd9Sstevel@tonic-gate /* 760*7c478bd9Sstevel@tonic-gate * Invoke the parent's bus_ctl operation with the DDI_CTLOPS_INITCHILD 761*7c478bd9Sstevel@tonic-gate * command to transform the child to canonical form 1. If there 762*7c478bd9Sstevel@tonic-gate * is an error, ddi_remove_child should be called, to clean up. 763*7c478bd9Sstevel@tonic-gate */ 764*7c478bd9Sstevel@tonic-gate error = (*f)(pdip, pdip, DDI_CTLOPS_INITCHILD, dip, NULL); 765*7c478bd9Sstevel@tonic-gate if (error != DDI_SUCCESS) { 766*7c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_CONT, "init_node: %s 0x%p failed\n", 767*7c478bd9Sstevel@tonic-gate path, (void *)dip)); 768*7c478bd9Sstevel@tonic-gate remove_global_props(dip); 769*7c478bd9Sstevel@tonic-gate /* in case nexus driver didn't clear this field */ 770*7c478bd9Sstevel@tonic-gate ddi_set_name_addr(dip, NULL); 771*7c478bd9Sstevel@tonic-gate error = DDI_FAILURE; 772*7c478bd9Sstevel@tonic-gate goto out; 773*7c478bd9Sstevel@tonic-gate } 774*7c478bd9Sstevel@tonic-gate 775*7c478bd9Sstevel@tonic-gate ndi_hold_devi(pdip); 776*7c478bd9Sstevel@tonic-gate 777*7c478bd9Sstevel@tonic-gate /* check for duplicate nodes */ 778*7c478bd9Sstevel@tonic-gate if (find_duplicate_child(pdip, dip) != NULL) { 779*7c478bd9Sstevel@tonic-gate /* recompute path after initchild for @addr information */ 780*7c478bd9Sstevel@tonic-gate (void) ddi_pathname(dip, path); 781*7c478bd9Sstevel@tonic-gate 782*7c478bd9Sstevel@tonic-gate /* 783*7c478bd9Sstevel@tonic-gate * uninit_node() the duplicate - a successful uninit_node() 784*7c478bd9Sstevel@tonic-gate * does a ndi_rele_devi 785*7c478bd9Sstevel@tonic-gate */ 786*7c478bd9Sstevel@tonic-gate if ((error = uninit_node(dip)) != DDI_SUCCESS) { 787*7c478bd9Sstevel@tonic-gate ndi_rele_devi(pdip); 788*7c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "init_node: uninit of duplicate " 789*7c478bd9Sstevel@tonic-gate "node %s failed", path); 790*7c478bd9Sstevel@tonic-gate } 791*7c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_CONT, "init_node: duplicate uninit " 792*7c478bd9Sstevel@tonic-gate "%s 0x%p%s\n", path, (void *)dip, 793*7c478bd9Sstevel@tonic-gate (error == DDI_SUCCESS) ? "" : " failed")); 794*7c478bd9Sstevel@tonic-gate error = DDI_FAILURE; 795*7c478bd9Sstevel@tonic-gate goto out; 796*7c478bd9Sstevel@tonic-gate } 797*7c478bd9Sstevel@tonic-gate 798*7c478bd9Sstevel@tonic-gate /* 799*7c478bd9Sstevel@tonic-gate * Apply multi-parent/deep-nexus optimization to the new node 800*7c478bd9Sstevel@tonic-gate */ 801*7c478bd9Sstevel@tonic-gate DEVI(dip)->devi_instance = e_ddi_assign_instance(dip); 802*7c478bd9Sstevel@tonic-gate ddi_optimize_dtree(dip); 803*7c478bd9Sstevel@tonic-gate error = DDI_SUCCESS; 804*7c478bd9Sstevel@tonic-gate 805*7c478bd9Sstevel@tonic-gate out: kmem_free(path, MAXPATHLEN); 806*7c478bd9Sstevel@tonic-gate return (error); 807*7c478bd9Sstevel@tonic-gate } 808*7c478bd9Sstevel@tonic-gate 809*7c478bd9Sstevel@tonic-gate /* 810*7c478bd9Sstevel@tonic-gate * Uninitialize node 811*7c478bd9Sstevel@tonic-gate * The per-driver list must be held busy during the call. 812*7c478bd9Sstevel@tonic-gate * A successful uninit_node() releases the init_node() hold on 813*7c478bd9Sstevel@tonic-gate * the parent by calling ndi_rele_devi(). 814*7c478bd9Sstevel@tonic-gate */ 815*7c478bd9Sstevel@tonic-gate static int 816*7c478bd9Sstevel@tonic-gate uninit_node(dev_info_t *dip) 817*7c478bd9Sstevel@tonic-gate { 818*7c478bd9Sstevel@tonic-gate int node_state_entry; 819*7c478bd9Sstevel@tonic-gate dev_info_t *pdip; 820*7c478bd9Sstevel@tonic-gate struct dev_ops *ops; 821*7c478bd9Sstevel@tonic-gate int (*f)(); 822*7c478bd9Sstevel@tonic-gate int error; 823*7c478bd9Sstevel@tonic-gate char *addr; 824*7c478bd9Sstevel@tonic-gate 825*7c478bd9Sstevel@tonic-gate /* 826*7c478bd9Sstevel@tonic-gate * Don't check for references here or else a ref-counted 827*7c478bd9Sstevel@tonic-gate * dip cannot be downgraded by the framework. 828*7c478bd9Sstevel@tonic-gate */ 829*7c478bd9Sstevel@tonic-gate node_state_entry = i_ddi_node_state(dip); 830*7c478bd9Sstevel@tonic-gate ASSERT((node_state_entry == DS_BOUND) || 831*7c478bd9Sstevel@tonic-gate (node_state_entry == DS_INITIALIZED)); 832*7c478bd9Sstevel@tonic-gate pdip = ddi_get_parent(dip); 833*7c478bd9Sstevel@tonic-gate ASSERT(pdip); 834*7c478bd9Sstevel@tonic-gate 835*7c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_CONT, "uninit_node: 0x%p(%s%d)\n", 836*7c478bd9Sstevel@tonic-gate (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip))); 837*7c478bd9Sstevel@tonic-gate 838*7c478bd9Sstevel@tonic-gate if (((ops = ddi_get_driver(pdip)) == NULL) || 839*7c478bd9Sstevel@tonic-gate (ops->devo_bus_ops == NULL) || 840*7c478bd9Sstevel@tonic-gate ((f = ops->devo_bus_ops->bus_ctl) == NULL)) { 841*7c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 842*7c478bd9Sstevel@tonic-gate } 843*7c478bd9Sstevel@tonic-gate 844*7c478bd9Sstevel@tonic-gate /* 845*7c478bd9Sstevel@tonic-gate * save the @addr prior to DDI_CTLOPS_UNINITCHILD for use in 846*7c478bd9Sstevel@tonic-gate * freeing the instance if it succeeds. 847*7c478bd9Sstevel@tonic-gate */ 848*7c478bd9Sstevel@tonic-gate if (node_state_entry == DS_INITIALIZED) { 849*7c478bd9Sstevel@tonic-gate addr = ddi_get_name_addr(dip); 850*7c478bd9Sstevel@tonic-gate if (addr) 851*7c478bd9Sstevel@tonic-gate addr = i_ddi_strdup(addr, KM_SLEEP); 852*7c478bd9Sstevel@tonic-gate } else { 853*7c478bd9Sstevel@tonic-gate addr = NULL; 854*7c478bd9Sstevel@tonic-gate } 855*7c478bd9Sstevel@tonic-gate 856*7c478bd9Sstevel@tonic-gate error = (*f)(pdip, pdip, DDI_CTLOPS_UNINITCHILD, dip, (void *)NULL); 857*7c478bd9Sstevel@tonic-gate if (error == DDI_SUCCESS) { 858*7c478bd9Sstevel@tonic-gate /* if uninitchild forgot to set devi_addr to NULL do it now */ 859*7c478bd9Sstevel@tonic-gate ddi_set_name_addr(dip, NULL); 860*7c478bd9Sstevel@tonic-gate 861*7c478bd9Sstevel@tonic-gate /* 862*7c478bd9Sstevel@tonic-gate * Free instance number. This is a no-op if instance has 863*7c478bd9Sstevel@tonic-gate * been kept by probe_node(). Avoid free when we are called 864*7c478bd9Sstevel@tonic-gate * from init_node (DS_BOUND) because the instance has not yet 865*7c478bd9Sstevel@tonic-gate * been assigned. 866*7c478bd9Sstevel@tonic-gate */ 867*7c478bd9Sstevel@tonic-gate if (node_state_entry == DS_INITIALIZED) { 868*7c478bd9Sstevel@tonic-gate e_ddi_free_instance(dip, addr); 869*7c478bd9Sstevel@tonic-gate DEVI(dip)->devi_instance = -1; 870*7c478bd9Sstevel@tonic-gate } 871*7c478bd9Sstevel@tonic-gate 872*7c478bd9Sstevel@tonic-gate /* release the init_node hold */ 873*7c478bd9Sstevel@tonic-gate ndi_rele_devi(pdip); 874*7c478bd9Sstevel@tonic-gate 875*7c478bd9Sstevel@tonic-gate remove_global_props(dip); 876*7c478bd9Sstevel@tonic-gate e_ddi_prop_remove_all(dip); 877*7c478bd9Sstevel@tonic-gate } else { 878*7c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_CONT, "uninit_node failed: 0x%p(%s%d)\n", 879*7c478bd9Sstevel@tonic-gate (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip))); 880*7c478bd9Sstevel@tonic-gate } 881*7c478bd9Sstevel@tonic-gate 882*7c478bd9Sstevel@tonic-gate if (addr) 883*7c478bd9Sstevel@tonic-gate kmem_free(addr, strlen(addr) + 1); 884*7c478bd9Sstevel@tonic-gate return (error); 885*7c478bd9Sstevel@tonic-gate } 886*7c478bd9Sstevel@tonic-gate 887*7c478bd9Sstevel@tonic-gate /* 888*7c478bd9Sstevel@tonic-gate * Invoke driver's probe entry point to probe for existence of hardware. 889*7c478bd9Sstevel@tonic-gate * Keep instance permanent for successful probe and leaf nodes. 890*7c478bd9Sstevel@tonic-gate * 891*7c478bd9Sstevel@tonic-gate * Per-driver list must be held busy while calling this function. 892*7c478bd9Sstevel@tonic-gate */ 893*7c478bd9Sstevel@tonic-gate static int 894*7c478bd9Sstevel@tonic-gate probe_node(dev_info_t *dip) 895*7c478bd9Sstevel@tonic-gate { 896*7c478bd9Sstevel@tonic-gate int rv; 897*7c478bd9Sstevel@tonic-gate 898*7c478bd9Sstevel@tonic-gate ASSERT(i_ddi_node_state(dip) == DS_INITIALIZED); 899*7c478bd9Sstevel@tonic-gate 900*7c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_CONT, "probe_node: 0x%p(%s%d)\n", 901*7c478bd9Sstevel@tonic-gate (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip))); 902*7c478bd9Sstevel@tonic-gate 903*7c478bd9Sstevel@tonic-gate /* temporarily hold the driver while we probe */ 904*7c478bd9Sstevel@tonic-gate DEVI(dip)->devi_ops = ndi_hold_driver(dip); 905*7c478bd9Sstevel@tonic-gate if (DEVI(dip)->devi_ops == NULL) { 906*7c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_CONT, 907*7c478bd9Sstevel@tonic-gate "probe_node: 0x%p(%s%d) cannot load driver\n", 908*7c478bd9Sstevel@tonic-gate (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip))); 909*7c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 910*7c478bd9Sstevel@tonic-gate } 911*7c478bd9Sstevel@tonic-gate 912*7c478bd9Sstevel@tonic-gate if (identify_9e != 0) 913*7c478bd9Sstevel@tonic-gate (void) devi_identify(dip); 914*7c478bd9Sstevel@tonic-gate 915*7c478bd9Sstevel@tonic-gate rv = devi_probe(dip); 916*7c478bd9Sstevel@tonic-gate 917*7c478bd9Sstevel@tonic-gate /* release the driver now that probe is complete */ 918*7c478bd9Sstevel@tonic-gate ndi_rele_driver(dip); 919*7c478bd9Sstevel@tonic-gate DEVI(dip)->devi_ops = NULL; 920*7c478bd9Sstevel@tonic-gate 921*7c478bd9Sstevel@tonic-gate switch (rv) { 922*7c478bd9Sstevel@tonic-gate case DDI_PROBE_SUCCESS: /* found */ 923*7c478bd9Sstevel@tonic-gate case DDI_PROBE_DONTCARE: /* ddi_dev_is_sid */ 924*7c478bd9Sstevel@tonic-gate e_ddi_keep_instance(dip); /* persist instance */ 925*7c478bd9Sstevel@tonic-gate rv = DDI_SUCCESS; 926*7c478bd9Sstevel@tonic-gate break; 927*7c478bd9Sstevel@tonic-gate 928*7c478bd9Sstevel@tonic-gate case DDI_PROBE_PARTIAL: /* maybe later */ 929*7c478bd9Sstevel@tonic-gate case DDI_PROBE_FAILURE: /* not found */ 930*7c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_CONT, 931*7c478bd9Sstevel@tonic-gate "probe_node: 0x%p(%s%d) no hardware found%s\n", 932*7c478bd9Sstevel@tonic-gate (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip), 933*7c478bd9Sstevel@tonic-gate (rv == DDI_PROBE_PARTIAL) ? " yet" : "")); 934*7c478bd9Sstevel@tonic-gate rv = DDI_FAILURE; 935*7c478bd9Sstevel@tonic-gate break; 936*7c478bd9Sstevel@tonic-gate 937*7c478bd9Sstevel@tonic-gate default: 938*7c478bd9Sstevel@tonic-gate #ifdef DEBUG 939*7c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "probe_node: %s%d: illegal probe(9E) value", 940*7c478bd9Sstevel@tonic-gate ddi_driver_name(dip), ddi_get_instance(dip)); 941*7c478bd9Sstevel@tonic-gate #endif /* DEBUG */ 942*7c478bd9Sstevel@tonic-gate rv = DDI_FAILURE; 943*7c478bd9Sstevel@tonic-gate break; 944*7c478bd9Sstevel@tonic-gate } 945*7c478bd9Sstevel@tonic-gate return (rv); 946*7c478bd9Sstevel@tonic-gate } 947*7c478bd9Sstevel@tonic-gate 948*7c478bd9Sstevel@tonic-gate /* 949*7c478bd9Sstevel@tonic-gate * Unprobe a node. Simply reset the node state. 950*7c478bd9Sstevel@tonic-gate * Per-driver list must be held busy while calling this function. 951*7c478bd9Sstevel@tonic-gate */ 952*7c478bd9Sstevel@tonic-gate static int 953*7c478bd9Sstevel@tonic-gate unprobe_node(dev_info_t *dip) 954*7c478bd9Sstevel@tonic-gate { 955*7c478bd9Sstevel@tonic-gate ASSERT(i_ddi_node_state(dip) == DS_PROBED); 956*7c478bd9Sstevel@tonic-gate 957*7c478bd9Sstevel@tonic-gate /* 958*7c478bd9Sstevel@tonic-gate * Don't check for references here or else a ref-counted 959*7c478bd9Sstevel@tonic-gate * dip cannot be downgraded by the framework. 960*7c478bd9Sstevel@tonic-gate */ 961*7c478bd9Sstevel@tonic-gate 962*7c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_CONT, "unprobe_node: 0x%p(name = %s)\n", 963*7c478bd9Sstevel@tonic-gate (void *)dip, ddi_node_name(dip))); 964*7c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 965*7c478bd9Sstevel@tonic-gate } 966*7c478bd9Sstevel@tonic-gate 967*7c478bd9Sstevel@tonic-gate /* 968*7c478bd9Sstevel@tonic-gate * Attach devinfo node. 969*7c478bd9Sstevel@tonic-gate * Per-driver list must be held busy. 970*7c478bd9Sstevel@tonic-gate */ 971*7c478bd9Sstevel@tonic-gate static int 972*7c478bd9Sstevel@tonic-gate attach_node(dev_info_t *dip) 973*7c478bd9Sstevel@tonic-gate { 974*7c478bd9Sstevel@tonic-gate int rv; 975*7c478bd9Sstevel@tonic-gate 976*7c478bd9Sstevel@tonic-gate ASSERT(i_ddi_node_state(dip) == DS_PROBED); 977*7c478bd9Sstevel@tonic-gate 978*7c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_CONT, "attach_node: 0x%p(%s%d)\n", 979*7c478bd9Sstevel@tonic-gate (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip))); 980*7c478bd9Sstevel@tonic-gate 981*7c478bd9Sstevel@tonic-gate /* 982*7c478bd9Sstevel@tonic-gate * Tell mpxio framework that a node is about to online. 983*7c478bd9Sstevel@tonic-gate */ 984*7c478bd9Sstevel@tonic-gate if ((rv = mdi_devi_online(dip, 0)) != NDI_SUCCESS) { 985*7c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 986*7c478bd9Sstevel@tonic-gate } 987*7c478bd9Sstevel@tonic-gate 988*7c478bd9Sstevel@tonic-gate /* no recursive attachment */ 989*7c478bd9Sstevel@tonic-gate ASSERT(DEVI(dip)->devi_ops == NULL); 990*7c478bd9Sstevel@tonic-gate 991*7c478bd9Sstevel@tonic-gate /* 992*7c478bd9Sstevel@tonic-gate * Hold driver the node is bound to. 993*7c478bd9Sstevel@tonic-gate */ 994*7c478bd9Sstevel@tonic-gate DEVI(dip)->devi_ops = ndi_hold_driver(dip); 995*7c478bd9Sstevel@tonic-gate if (DEVI(dip)->devi_ops == NULL) { 996*7c478bd9Sstevel@tonic-gate /* 997*7c478bd9Sstevel@tonic-gate * We were able to load driver for probing, so we should 998*7c478bd9Sstevel@tonic-gate * not get here unless something really bad happened. 999*7c478bd9Sstevel@tonic-gate */ 1000*7c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "attach_node: no driver for major %d", 1001*7c478bd9Sstevel@tonic-gate DEVI(dip)->devi_major); 1002*7c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 1003*7c478bd9Sstevel@tonic-gate } 1004*7c478bd9Sstevel@tonic-gate 1005*7c478bd9Sstevel@tonic-gate if (NEXUS_DRV(DEVI(dip)->devi_ops)) 1006*7c478bd9Sstevel@tonic-gate DEVI(dip)->devi_taskq = ddi_taskq_create(dip, 1007*7c478bd9Sstevel@tonic-gate "nexus_enum_tq", 1, 1008*7c478bd9Sstevel@tonic-gate TASKQ_DEFAULTPRI, 0); 1009*7c478bd9Sstevel@tonic-gate 1010*7c478bd9Sstevel@tonic-gate DEVI_SET_ATTACHING(dip); 1011*7c478bd9Sstevel@tonic-gate DEVI_SET_NEED_RESET(dip); 1012*7c478bd9Sstevel@tonic-gate rv = devi_attach(dip, DDI_ATTACH); 1013*7c478bd9Sstevel@tonic-gate if (rv != DDI_SUCCESS) 1014*7c478bd9Sstevel@tonic-gate DEVI_CLR_NEED_RESET(dip); 1015*7c478bd9Sstevel@tonic-gate DEVI_CLR_ATTACHING(dip); 1016*7c478bd9Sstevel@tonic-gate 1017*7c478bd9Sstevel@tonic-gate if (rv != DDI_SUCCESS) { 1018*7c478bd9Sstevel@tonic-gate if (DEVI(dip)->devi_flags & DEVI_REGISTERED_DEVID) { 1019*7c478bd9Sstevel@tonic-gate e_devid_cache_unregister(dip); 1020*7c478bd9Sstevel@tonic-gate DEVI(dip)->devi_flags &= ~DEVI_REGISTERED_DEVID; 1021*7c478bd9Sstevel@tonic-gate } 1022*7c478bd9Sstevel@tonic-gate /* 1023*7c478bd9Sstevel@tonic-gate * Cleanup dacf reservations 1024*7c478bd9Sstevel@tonic-gate */ 1025*7c478bd9Sstevel@tonic-gate mutex_enter(&dacf_lock); 1026*7c478bd9Sstevel@tonic-gate dacf_clr_rsrvs(dip, DACF_OPID_POSTATTACH); 1027*7c478bd9Sstevel@tonic-gate dacf_clr_rsrvs(dip, DACF_OPID_PREDETACH); 1028*7c478bd9Sstevel@tonic-gate mutex_exit(&dacf_lock); 1029*7c478bd9Sstevel@tonic-gate if (DEVI(dip)->devi_taskq) 1030*7c478bd9Sstevel@tonic-gate ddi_taskq_destroy(DEVI(dip)->devi_taskq); 1031*7c478bd9Sstevel@tonic-gate ddi_remove_minor_node(dip, NULL); 1032*7c478bd9Sstevel@tonic-gate 1033*7c478bd9Sstevel@tonic-gate /* release the driver if attach failed */ 1034*7c478bd9Sstevel@tonic-gate ndi_rele_driver(dip); 1035*7c478bd9Sstevel@tonic-gate DEVI(dip)->devi_ops = NULL; 1036*7c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_CONT, "attach_node: 0x%p(%s%d) failed\n", 1037*7c478bd9Sstevel@tonic-gate (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip))); 1038*7c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 1039*7c478bd9Sstevel@tonic-gate } 1040*7c478bd9Sstevel@tonic-gate 1041*7c478bd9Sstevel@tonic-gate /* successful attach, return with driver held */ 1042*7c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 1043*7c478bd9Sstevel@tonic-gate } 1044*7c478bd9Sstevel@tonic-gate 1045*7c478bd9Sstevel@tonic-gate /* 1046*7c478bd9Sstevel@tonic-gate * Detach devinfo node. 1047*7c478bd9Sstevel@tonic-gate * Per-driver list must be held busy. 1048*7c478bd9Sstevel@tonic-gate */ 1049*7c478bd9Sstevel@tonic-gate static int 1050*7c478bd9Sstevel@tonic-gate detach_node(dev_info_t *dip, uint_t flag) 1051*7c478bd9Sstevel@tonic-gate { 1052*7c478bd9Sstevel@tonic-gate int rv; 1053*7c478bd9Sstevel@tonic-gate ASSERT(i_ddi_node_state(dip) == DS_ATTACHED); 1054*7c478bd9Sstevel@tonic-gate 1055*7c478bd9Sstevel@tonic-gate /* check references */ 1056*7c478bd9Sstevel@tonic-gate if (DEVI(dip)->devi_ref) 1057*7c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 1058*7c478bd9Sstevel@tonic-gate 1059*7c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_CONT, "detach_node: 0x%p(%s%d)\n", 1060*7c478bd9Sstevel@tonic-gate (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip))); 1061*7c478bd9Sstevel@tonic-gate 1062*7c478bd9Sstevel@tonic-gate /* Offline the device node with the mpxio framework. */ 1063*7c478bd9Sstevel@tonic-gate if (mdi_devi_offline(dip, flag) != NDI_SUCCESS) { 1064*7c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 1065*7c478bd9Sstevel@tonic-gate } 1066*7c478bd9Sstevel@tonic-gate 1067*7c478bd9Sstevel@tonic-gate /* drain the taskq */ 1068*7c478bd9Sstevel@tonic-gate if (DEVI(dip)->devi_taskq) 1069*7c478bd9Sstevel@tonic-gate ddi_taskq_wait(DEVI(dip)->devi_taskq); 1070*7c478bd9Sstevel@tonic-gate 1071*7c478bd9Sstevel@tonic-gate rv = devi_detach(dip, DDI_DETACH); 1072*7c478bd9Sstevel@tonic-gate if (rv == DDI_SUCCESS) 1073*7c478bd9Sstevel@tonic-gate DEVI_CLR_NEED_RESET(dip); 1074*7c478bd9Sstevel@tonic-gate 1075*7c478bd9Sstevel@tonic-gate if (rv != DDI_SUCCESS) { 1076*7c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_CONT, 1077*7c478bd9Sstevel@tonic-gate "detach_node: 0x%p(%s%d) failed\n", 1078*7c478bd9Sstevel@tonic-gate (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip))); 1079*7c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 1080*7c478bd9Sstevel@tonic-gate } 1081*7c478bd9Sstevel@tonic-gate 1082*7c478bd9Sstevel@tonic-gate /* destroy the taskq */ 1083*7c478bd9Sstevel@tonic-gate if (DEVI(dip)->devi_taskq) { 1084*7c478bd9Sstevel@tonic-gate ddi_taskq_destroy(DEVI(dip)->devi_taskq); 1085*7c478bd9Sstevel@tonic-gate DEVI(dip)->devi_taskq = NULL; 1086*7c478bd9Sstevel@tonic-gate } 1087*7c478bd9Sstevel@tonic-gate 1088*7c478bd9Sstevel@tonic-gate /* Cleanup dacf reservations */ 1089*7c478bd9Sstevel@tonic-gate mutex_enter(&dacf_lock); 1090*7c478bd9Sstevel@tonic-gate dacf_clr_rsrvs(dip, DACF_OPID_POSTATTACH); 1091*7c478bd9Sstevel@tonic-gate dacf_clr_rsrvs(dip, DACF_OPID_PREDETACH); 1092*7c478bd9Sstevel@tonic-gate mutex_exit(&dacf_lock); 1093*7c478bd9Sstevel@tonic-gate 1094*7c478bd9Sstevel@tonic-gate /* Remove properties and minor nodes in case driver forgots */ 1095*7c478bd9Sstevel@tonic-gate ddi_remove_minor_node(dip, NULL); 1096*7c478bd9Sstevel@tonic-gate ddi_prop_remove_all(dip); 1097*7c478bd9Sstevel@tonic-gate 1098*7c478bd9Sstevel@tonic-gate /* a detached node can't have attached or .conf children */ 1099*7c478bd9Sstevel@tonic-gate mutex_enter(&DEVI(dip)->devi_lock); 1100*7c478bd9Sstevel@tonic-gate DEVI(dip)->devi_flags &= ~(DEVI_MADE_CHILDREN|DEVI_ATTACHED_CHILDREN); 1101*7c478bd9Sstevel@tonic-gate if (DEVI(dip)->devi_flags & DEVI_REGISTERED_DEVID) { 1102*7c478bd9Sstevel@tonic-gate e_devid_cache_unregister(dip); 1103*7c478bd9Sstevel@tonic-gate DEVI(dip)->devi_flags &= ~DEVI_REGISTERED_DEVID; 1104*7c478bd9Sstevel@tonic-gate } 1105*7c478bd9Sstevel@tonic-gate mutex_exit(&DEVI(dip)->devi_lock); 1106*7c478bd9Sstevel@tonic-gate 1107*7c478bd9Sstevel@tonic-gate /* successful detach, release the driver */ 1108*7c478bd9Sstevel@tonic-gate ndi_rele_driver(dip); 1109*7c478bd9Sstevel@tonic-gate DEVI(dip)->devi_ops = NULL; 1110*7c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 1111*7c478bd9Sstevel@tonic-gate } 1112*7c478bd9Sstevel@tonic-gate 1113*7c478bd9Sstevel@tonic-gate /* 1114*7c478bd9Sstevel@tonic-gate * Run dacf post_attach routines 1115*7c478bd9Sstevel@tonic-gate */ 1116*7c478bd9Sstevel@tonic-gate static int 1117*7c478bd9Sstevel@tonic-gate postattach_node(dev_info_t *dip) 1118*7c478bd9Sstevel@tonic-gate { 1119*7c478bd9Sstevel@tonic-gate int rval; 1120*7c478bd9Sstevel@tonic-gate 1121*7c478bd9Sstevel@tonic-gate /* 1122*7c478bd9Sstevel@tonic-gate * For hotplug busses like USB, it's possible that devices 1123*7c478bd9Sstevel@tonic-gate * are removed but dip is still around. We don't want to 1124*7c478bd9Sstevel@tonic-gate * run dacf routines as part of detach failure recovery. 1125*7c478bd9Sstevel@tonic-gate * 1126*7c478bd9Sstevel@tonic-gate * Pretend success until we figure out how to prevent 1127*7c478bd9Sstevel@tonic-gate * access to such devinfo nodes. 1128*7c478bd9Sstevel@tonic-gate */ 1129*7c478bd9Sstevel@tonic-gate if (DEVI_IS_DEVICE_REMOVED(dip)) 1130*7c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 1131*7c478bd9Sstevel@tonic-gate 1132*7c478bd9Sstevel@tonic-gate /* 1133*7c478bd9Sstevel@tonic-gate * if dacf_postattach failed, report it to the framework 1134*7c478bd9Sstevel@tonic-gate * so that it can be retried later at the open time. 1135*7c478bd9Sstevel@tonic-gate */ 1136*7c478bd9Sstevel@tonic-gate mutex_enter(&dacf_lock); 1137*7c478bd9Sstevel@tonic-gate rval = dacfc_postattach(dip); 1138*7c478bd9Sstevel@tonic-gate mutex_exit(&dacf_lock); 1139*7c478bd9Sstevel@tonic-gate 1140*7c478bd9Sstevel@tonic-gate /* 1141*7c478bd9Sstevel@tonic-gate * Plumbing during postattach may fail because of the 1142*7c478bd9Sstevel@tonic-gate * underlying device is not ready. This will fail ndi_devi_config() 1143*7c478bd9Sstevel@tonic-gate * in dv_filldir() and a warning message is issued. The message 1144*7c478bd9Sstevel@tonic-gate * from here will explain what happened 1145*7c478bd9Sstevel@tonic-gate */ 1146*7c478bd9Sstevel@tonic-gate if (rval != DACF_SUCCESS) { 1147*7c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "Postattach failed for %s%d\n", 1148*7c478bd9Sstevel@tonic-gate ddi_driver_name(dip), ddi_get_instance(dip)); 1149*7c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 1150*7c478bd9Sstevel@tonic-gate } 1151*7c478bd9Sstevel@tonic-gate 1152*7c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 1153*7c478bd9Sstevel@tonic-gate } 1154*7c478bd9Sstevel@tonic-gate 1155*7c478bd9Sstevel@tonic-gate /* 1156*7c478bd9Sstevel@tonic-gate * Run dacf pre-detach routines 1157*7c478bd9Sstevel@tonic-gate */ 1158*7c478bd9Sstevel@tonic-gate static int 1159*7c478bd9Sstevel@tonic-gate predetach_node(dev_info_t *dip, uint_t flag) 1160*7c478bd9Sstevel@tonic-gate { 1161*7c478bd9Sstevel@tonic-gate int ret; 1162*7c478bd9Sstevel@tonic-gate 1163*7c478bd9Sstevel@tonic-gate /* 1164*7c478bd9Sstevel@tonic-gate * Don't auto-detach if DDI_FORCEATTACH or DDI_NO_AUTODETACH 1165*7c478bd9Sstevel@tonic-gate * properties are set. 1166*7c478bd9Sstevel@tonic-gate */ 1167*7c478bd9Sstevel@tonic-gate if (flag & NDI_AUTODETACH) { 1168*7c478bd9Sstevel@tonic-gate struct devnames *dnp; 1169*7c478bd9Sstevel@tonic-gate int pflag = DDI_PROP_NOTPROM | DDI_PROP_DONTPASS; 1170*7c478bd9Sstevel@tonic-gate 1171*7c478bd9Sstevel@tonic-gate if ((ddi_prop_get_int(DDI_DEV_T_ANY, dip, 1172*7c478bd9Sstevel@tonic-gate pflag, DDI_FORCEATTACH, 0) == 1) || 1173*7c478bd9Sstevel@tonic-gate (ddi_prop_get_int(DDI_DEV_T_ANY, dip, 1174*7c478bd9Sstevel@tonic-gate pflag, DDI_NO_AUTODETACH, 0) == 1)) 1175*7c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 1176*7c478bd9Sstevel@tonic-gate 1177*7c478bd9Sstevel@tonic-gate /* check for driver global version of DDI_NO_AUTODETACH */ 1178*7c478bd9Sstevel@tonic-gate dnp = &devnamesp[DEVI(dip)->devi_major]; 1179*7c478bd9Sstevel@tonic-gate LOCK_DEV_OPS(&dnp->dn_lock); 1180*7c478bd9Sstevel@tonic-gate if (dnp->dn_flags & DN_NO_AUTODETACH) { 1181*7c478bd9Sstevel@tonic-gate UNLOCK_DEV_OPS(&dnp->dn_lock); 1182*7c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 1183*7c478bd9Sstevel@tonic-gate } 1184*7c478bd9Sstevel@tonic-gate UNLOCK_DEV_OPS(&dnp->dn_lock); 1185*7c478bd9Sstevel@tonic-gate } 1186*7c478bd9Sstevel@tonic-gate 1187*7c478bd9Sstevel@tonic-gate mutex_enter(&dacf_lock); 1188*7c478bd9Sstevel@tonic-gate ret = dacfc_predetach(dip); 1189*7c478bd9Sstevel@tonic-gate mutex_exit(&dacf_lock); 1190*7c478bd9Sstevel@tonic-gate 1191*7c478bd9Sstevel@tonic-gate return (ret); 1192*7c478bd9Sstevel@tonic-gate } 1193*7c478bd9Sstevel@tonic-gate 1194*7c478bd9Sstevel@tonic-gate /* 1195*7c478bd9Sstevel@tonic-gate * Wrapper for making multiple state transitions 1196*7c478bd9Sstevel@tonic-gate */ 1197*7c478bd9Sstevel@tonic-gate 1198*7c478bd9Sstevel@tonic-gate /* 1199*7c478bd9Sstevel@tonic-gate * i_ndi_config_node: upgrade dev_info node into a specified state. 1200*7c478bd9Sstevel@tonic-gate * It is a bit tricky because the locking protocol changes before and 1201*7c478bd9Sstevel@tonic-gate * after a node is bound to a driver. All locks are held external to 1202*7c478bd9Sstevel@tonic-gate * this function. 1203*7c478bd9Sstevel@tonic-gate */ 1204*7c478bd9Sstevel@tonic-gate int 1205*7c478bd9Sstevel@tonic-gate i_ndi_config_node(dev_info_t *dip, ddi_node_state_t state, uint_t flag) 1206*7c478bd9Sstevel@tonic-gate { 1207*7c478bd9Sstevel@tonic-gate _NOTE(ARGUNUSED(flag)) 1208*7c478bd9Sstevel@tonic-gate int rv = DDI_SUCCESS; 1209*7c478bd9Sstevel@tonic-gate 1210*7c478bd9Sstevel@tonic-gate ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip))); 1211*7c478bd9Sstevel@tonic-gate 1212*7c478bd9Sstevel@tonic-gate while ((i_ddi_node_state(dip) < state) && (rv == DDI_SUCCESS)) { 1213*7c478bd9Sstevel@tonic-gate 1214*7c478bd9Sstevel@tonic-gate /* don't allow any more changes to the device tree */ 1215*7c478bd9Sstevel@tonic-gate if (devinfo_freeze) { 1216*7c478bd9Sstevel@tonic-gate rv = DDI_FAILURE; 1217*7c478bd9Sstevel@tonic-gate break; 1218*7c478bd9Sstevel@tonic-gate } 1219*7c478bd9Sstevel@tonic-gate 1220*7c478bd9Sstevel@tonic-gate switch (i_ddi_node_state(dip)) { 1221*7c478bd9Sstevel@tonic-gate case DS_PROTO: 1222*7c478bd9Sstevel@tonic-gate /* 1223*7c478bd9Sstevel@tonic-gate * only caller can reference this node, no external 1224*7c478bd9Sstevel@tonic-gate * locking needed. 1225*7c478bd9Sstevel@tonic-gate */ 1226*7c478bd9Sstevel@tonic-gate link_node(dip); 1227*7c478bd9Sstevel@tonic-gate i_ddi_set_node_state(dip, DS_LINKED); 1228*7c478bd9Sstevel@tonic-gate break; 1229*7c478bd9Sstevel@tonic-gate case DS_LINKED: 1230*7c478bd9Sstevel@tonic-gate /* 1231*7c478bd9Sstevel@tonic-gate * Three code path may attempt to bind a node: 1232*7c478bd9Sstevel@tonic-gate * - boot code 1233*7c478bd9Sstevel@tonic-gate * - add_drv 1234*7c478bd9Sstevel@tonic-gate * - hotplug thread 1235*7c478bd9Sstevel@tonic-gate * Boot code is single threaded, add_drv synchronize 1236*7c478bd9Sstevel@tonic-gate * on a userland lock, and hotplug synchronize on 1237*7c478bd9Sstevel@tonic-gate * hotplug_lk. There could be a race between add_drv 1238*7c478bd9Sstevel@tonic-gate * and hotplug thread. We'll live with this until the 1239*7c478bd9Sstevel@tonic-gate * conversion to top-down loading. 1240*7c478bd9Sstevel@tonic-gate */ 1241*7c478bd9Sstevel@tonic-gate if ((rv = bind_node(dip)) == DDI_SUCCESS) 1242*7c478bd9Sstevel@tonic-gate i_ddi_set_node_state(dip, DS_BOUND); 1243*7c478bd9Sstevel@tonic-gate break; 1244*7c478bd9Sstevel@tonic-gate case DS_BOUND: 1245*7c478bd9Sstevel@tonic-gate /* 1246*7c478bd9Sstevel@tonic-gate * The following transitions synchronizes on the 1247*7c478bd9Sstevel@tonic-gate * per-driver busy changing flag, since we already 1248*7c478bd9Sstevel@tonic-gate * have a driver. 1249*7c478bd9Sstevel@tonic-gate */ 1250*7c478bd9Sstevel@tonic-gate if ((rv = init_node(dip)) == DDI_SUCCESS) 1251*7c478bd9Sstevel@tonic-gate i_ddi_set_node_state(dip, DS_INITIALIZED); 1252*7c478bd9Sstevel@tonic-gate break; 1253*7c478bd9Sstevel@tonic-gate case DS_INITIALIZED: 1254*7c478bd9Sstevel@tonic-gate if ((rv = probe_node(dip)) == DDI_SUCCESS) 1255*7c478bd9Sstevel@tonic-gate i_ddi_set_node_state(dip, DS_PROBED); 1256*7c478bd9Sstevel@tonic-gate break; 1257*7c478bd9Sstevel@tonic-gate case DS_PROBED: 1258*7c478bd9Sstevel@tonic-gate atomic_add_long(&devinfo_attach_detach, 1); 1259*7c478bd9Sstevel@tonic-gate if ((rv = attach_node(dip)) == DDI_SUCCESS) 1260*7c478bd9Sstevel@tonic-gate i_ddi_set_node_state(dip, DS_ATTACHED); 1261*7c478bd9Sstevel@tonic-gate atomic_add_long(&devinfo_attach_detach, -1); 1262*7c478bd9Sstevel@tonic-gate break; 1263*7c478bd9Sstevel@tonic-gate case DS_ATTACHED: 1264*7c478bd9Sstevel@tonic-gate if ((rv = postattach_node(dip)) == DDI_SUCCESS) 1265*7c478bd9Sstevel@tonic-gate i_ddi_set_node_state(dip, DS_READY); 1266*7c478bd9Sstevel@tonic-gate break; 1267*7c478bd9Sstevel@tonic-gate case DS_READY: 1268*7c478bd9Sstevel@tonic-gate break; 1269*7c478bd9Sstevel@tonic-gate default: 1270*7c478bd9Sstevel@tonic-gate /* should never reach here */ 1271*7c478bd9Sstevel@tonic-gate ASSERT("unknown devinfo state"); 1272*7c478bd9Sstevel@tonic-gate } 1273*7c478bd9Sstevel@tonic-gate } 1274*7c478bd9Sstevel@tonic-gate 1275*7c478bd9Sstevel@tonic-gate if (ddidebug & DDI_AUDIT) 1276*7c478bd9Sstevel@tonic-gate da_log_enter(dip); 1277*7c478bd9Sstevel@tonic-gate return (rv); 1278*7c478bd9Sstevel@tonic-gate } 1279*7c478bd9Sstevel@tonic-gate 1280*7c478bd9Sstevel@tonic-gate /* 1281*7c478bd9Sstevel@tonic-gate * i_ndi_unconfig_node: downgrade dev_info node into a specified state. 1282*7c478bd9Sstevel@tonic-gate */ 1283*7c478bd9Sstevel@tonic-gate int 1284*7c478bd9Sstevel@tonic-gate i_ndi_unconfig_node(dev_info_t *dip, ddi_node_state_t state, uint_t flag) 1285*7c478bd9Sstevel@tonic-gate { 1286*7c478bd9Sstevel@tonic-gate int rv = DDI_SUCCESS; 1287*7c478bd9Sstevel@tonic-gate 1288*7c478bd9Sstevel@tonic-gate ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip))); 1289*7c478bd9Sstevel@tonic-gate 1290*7c478bd9Sstevel@tonic-gate while ((i_ddi_node_state(dip) > state) && (rv == DDI_SUCCESS)) { 1291*7c478bd9Sstevel@tonic-gate 1292*7c478bd9Sstevel@tonic-gate /* don't allow any more changes to the device tree */ 1293*7c478bd9Sstevel@tonic-gate if (devinfo_freeze) { 1294*7c478bd9Sstevel@tonic-gate rv = DDI_FAILURE; 1295*7c478bd9Sstevel@tonic-gate break; 1296*7c478bd9Sstevel@tonic-gate } 1297*7c478bd9Sstevel@tonic-gate 1298*7c478bd9Sstevel@tonic-gate switch (i_ddi_node_state(dip)) { 1299*7c478bd9Sstevel@tonic-gate case DS_PROTO: 1300*7c478bd9Sstevel@tonic-gate break; 1301*7c478bd9Sstevel@tonic-gate case DS_LINKED: 1302*7c478bd9Sstevel@tonic-gate /* 1303*7c478bd9Sstevel@tonic-gate * Persistent nodes are only removed by hotplug code 1304*7c478bd9Sstevel@tonic-gate * .conf nodes synchronizes on per-driver list. 1305*7c478bd9Sstevel@tonic-gate */ 1306*7c478bd9Sstevel@tonic-gate if ((rv = unlink_node(dip)) == DDI_SUCCESS) 1307*7c478bd9Sstevel@tonic-gate i_ddi_set_node_state(dip, DS_PROTO); 1308*7c478bd9Sstevel@tonic-gate break; 1309*7c478bd9Sstevel@tonic-gate case DS_BOUND: 1310*7c478bd9Sstevel@tonic-gate /* 1311*7c478bd9Sstevel@tonic-gate * The following transitions synchronizes on the 1312*7c478bd9Sstevel@tonic-gate * per-driver busy changing flag, since we already 1313*7c478bd9Sstevel@tonic-gate * have a driver. 1314*7c478bd9Sstevel@tonic-gate */ 1315*7c478bd9Sstevel@tonic-gate if ((rv = unbind_node(dip)) == DDI_SUCCESS) 1316*7c478bd9Sstevel@tonic-gate i_ddi_set_node_state(dip, DS_LINKED); 1317*7c478bd9Sstevel@tonic-gate break; 1318*7c478bd9Sstevel@tonic-gate case DS_INITIALIZED: 1319*7c478bd9Sstevel@tonic-gate if ((rv = uninit_node(dip)) == DDI_SUCCESS) 1320*7c478bd9Sstevel@tonic-gate i_ddi_set_node_state(dip, DS_BOUND); 1321*7c478bd9Sstevel@tonic-gate break; 1322*7c478bd9Sstevel@tonic-gate case DS_PROBED: 1323*7c478bd9Sstevel@tonic-gate if ((rv = unprobe_node(dip)) == DDI_SUCCESS) 1324*7c478bd9Sstevel@tonic-gate i_ddi_set_node_state(dip, DS_INITIALIZED); 1325*7c478bd9Sstevel@tonic-gate break; 1326*7c478bd9Sstevel@tonic-gate case DS_ATTACHED: 1327*7c478bd9Sstevel@tonic-gate atomic_add_long(&devinfo_attach_detach, 1); 1328*7c478bd9Sstevel@tonic-gate DEVI_SET_DETACHING(dip); 1329*7c478bd9Sstevel@tonic-gate membar_enter(); /* ensure visibility for hold_devi */ 1330*7c478bd9Sstevel@tonic-gate 1331*7c478bd9Sstevel@tonic-gate if ((rv = detach_node(dip, flag)) == DDI_SUCCESS) 1332*7c478bd9Sstevel@tonic-gate i_ddi_set_node_state(dip, DS_PROBED); 1333*7c478bd9Sstevel@tonic-gate DEVI_CLR_DETACHING(dip); 1334*7c478bd9Sstevel@tonic-gate atomic_add_long(&devinfo_attach_detach, -1); 1335*7c478bd9Sstevel@tonic-gate break; 1336*7c478bd9Sstevel@tonic-gate case DS_READY: 1337*7c478bd9Sstevel@tonic-gate if ((rv = predetach_node(dip, flag)) == DDI_SUCCESS) 1338*7c478bd9Sstevel@tonic-gate i_ddi_set_node_state(dip, DS_ATTACHED); 1339*7c478bd9Sstevel@tonic-gate break; 1340*7c478bd9Sstevel@tonic-gate default: 1341*7c478bd9Sstevel@tonic-gate ASSERT("unknown devinfo state"); 1342*7c478bd9Sstevel@tonic-gate } 1343*7c478bd9Sstevel@tonic-gate } 1344*7c478bd9Sstevel@tonic-gate da_log_enter(dip); 1345*7c478bd9Sstevel@tonic-gate return (rv); 1346*7c478bd9Sstevel@tonic-gate } 1347*7c478bd9Sstevel@tonic-gate 1348*7c478bd9Sstevel@tonic-gate /* 1349*7c478bd9Sstevel@tonic-gate * ddi_initchild: transform node to DS_INITIALIZED state 1350*7c478bd9Sstevel@tonic-gate */ 1351*7c478bd9Sstevel@tonic-gate int 1352*7c478bd9Sstevel@tonic-gate ddi_initchild(dev_info_t *parent, dev_info_t *proto) 1353*7c478bd9Sstevel@tonic-gate { 1354*7c478bd9Sstevel@tonic-gate int ret, circ; 1355*7c478bd9Sstevel@tonic-gate 1356*7c478bd9Sstevel@tonic-gate ndi_devi_enter(parent, &circ); 1357*7c478bd9Sstevel@tonic-gate ret = i_ndi_config_node(proto, DS_INITIALIZED, 0); 1358*7c478bd9Sstevel@tonic-gate ndi_devi_exit(parent, circ); 1359*7c478bd9Sstevel@tonic-gate 1360*7c478bd9Sstevel@tonic-gate return (ret); 1361*7c478bd9Sstevel@tonic-gate } 1362*7c478bd9Sstevel@tonic-gate 1363*7c478bd9Sstevel@tonic-gate /* 1364*7c478bd9Sstevel@tonic-gate * ddi_uninitchild: transform node down to DS_BOUND state 1365*7c478bd9Sstevel@tonic-gate */ 1366*7c478bd9Sstevel@tonic-gate int 1367*7c478bd9Sstevel@tonic-gate ddi_uninitchild(dev_info_t *dip) 1368*7c478bd9Sstevel@tonic-gate { 1369*7c478bd9Sstevel@tonic-gate int ret, circ; 1370*7c478bd9Sstevel@tonic-gate dev_info_t *parent = ddi_get_parent(dip); 1371*7c478bd9Sstevel@tonic-gate ASSERT(parent); 1372*7c478bd9Sstevel@tonic-gate 1373*7c478bd9Sstevel@tonic-gate ndi_devi_enter(parent, &circ); 1374*7c478bd9Sstevel@tonic-gate ret = i_ndi_unconfig_node(dip, DS_BOUND, 0); 1375*7c478bd9Sstevel@tonic-gate ndi_devi_exit(parent, circ); 1376*7c478bd9Sstevel@tonic-gate 1377*7c478bd9Sstevel@tonic-gate return (ret); 1378*7c478bd9Sstevel@tonic-gate } 1379*7c478bd9Sstevel@tonic-gate 1380*7c478bd9Sstevel@tonic-gate /* 1381*7c478bd9Sstevel@tonic-gate * i_ddi_attachchild: transform node to DS_READY state 1382*7c478bd9Sstevel@tonic-gate */ 1383*7c478bd9Sstevel@tonic-gate static int 1384*7c478bd9Sstevel@tonic-gate i_ddi_attachchild(dev_info_t *dip) 1385*7c478bd9Sstevel@tonic-gate { 1386*7c478bd9Sstevel@tonic-gate int ret, circ; 1387*7c478bd9Sstevel@tonic-gate dev_info_t *parent = ddi_get_parent(dip); 1388*7c478bd9Sstevel@tonic-gate ASSERT(parent); 1389*7c478bd9Sstevel@tonic-gate 1390*7c478bd9Sstevel@tonic-gate if ((i_ddi_node_state(dip) < DS_BOUND) || DEVI_IS_DEVICE_OFFLINE(dip)) 1391*7c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 1392*7c478bd9Sstevel@tonic-gate 1393*7c478bd9Sstevel@tonic-gate ndi_devi_enter(parent, &circ); 1394*7c478bd9Sstevel@tonic-gate ret = i_ndi_config_node(dip, DS_READY, 0); 1395*7c478bd9Sstevel@tonic-gate if (ret == NDI_SUCCESS) { 1396*7c478bd9Sstevel@tonic-gate ret = DDI_SUCCESS; 1397*7c478bd9Sstevel@tonic-gate } else { 1398*7c478bd9Sstevel@tonic-gate /* 1399*7c478bd9Sstevel@tonic-gate * Take it down to DS_INITIALIZED so pm_pre_probe is run 1400*7c478bd9Sstevel@tonic-gate * on the next attach 1401*7c478bd9Sstevel@tonic-gate */ 1402*7c478bd9Sstevel@tonic-gate (void) i_ndi_unconfig_node(dip, DS_INITIALIZED, 0); 1403*7c478bd9Sstevel@tonic-gate ret = DDI_FAILURE; 1404*7c478bd9Sstevel@tonic-gate } 1405*7c478bd9Sstevel@tonic-gate ndi_devi_exit(parent, circ); 1406*7c478bd9Sstevel@tonic-gate 1407*7c478bd9Sstevel@tonic-gate return (ret); 1408*7c478bd9Sstevel@tonic-gate } 1409*7c478bd9Sstevel@tonic-gate 1410*7c478bd9Sstevel@tonic-gate /* 1411*7c478bd9Sstevel@tonic-gate * i_ddi_detachchild: transform node down to DS_PROBED state 1412*7c478bd9Sstevel@tonic-gate * If it fails, put it back to DS_READY state. 1413*7c478bd9Sstevel@tonic-gate * NOTE: A node that fails detach may be at DS_ATTACHED instead 1414*7c478bd9Sstevel@tonic-gate * of DS_READY for a small amount of time. 1415*7c478bd9Sstevel@tonic-gate */ 1416*7c478bd9Sstevel@tonic-gate static int 1417*7c478bd9Sstevel@tonic-gate i_ddi_detachchild(dev_info_t *dip, uint_t flags) 1418*7c478bd9Sstevel@tonic-gate { 1419*7c478bd9Sstevel@tonic-gate int ret, circ; 1420*7c478bd9Sstevel@tonic-gate dev_info_t *parent = ddi_get_parent(dip); 1421*7c478bd9Sstevel@tonic-gate ASSERT(parent); 1422*7c478bd9Sstevel@tonic-gate 1423*7c478bd9Sstevel@tonic-gate ndi_devi_enter(parent, &circ); 1424*7c478bd9Sstevel@tonic-gate ret = i_ndi_unconfig_node(dip, DS_PROBED, flags); 1425*7c478bd9Sstevel@tonic-gate if (ret != DDI_SUCCESS) 1426*7c478bd9Sstevel@tonic-gate (void) i_ndi_config_node(dip, DS_READY, 0); 1427*7c478bd9Sstevel@tonic-gate else 1428*7c478bd9Sstevel@tonic-gate /* allow pm_pre_probe to reestablish pm state */ 1429*7c478bd9Sstevel@tonic-gate (void) i_ndi_unconfig_node(dip, DS_INITIALIZED, 0); 1430*7c478bd9Sstevel@tonic-gate ndi_devi_exit(parent, circ); 1431*7c478bd9Sstevel@tonic-gate 1432*7c478bd9Sstevel@tonic-gate return (ret); 1433*7c478bd9Sstevel@tonic-gate } 1434*7c478bd9Sstevel@tonic-gate 1435*7c478bd9Sstevel@tonic-gate /* 1436*7c478bd9Sstevel@tonic-gate * Add a child and bind to driver 1437*7c478bd9Sstevel@tonic-gate */ 1438*7c478bd9Sstevel@tonic-gate dev_info_t * 1439*7c478bd9Sstevel@tonic-gate ddi_add_child(dev_info_t *pdip, char *name, uint_t nodeid, uint_t unit) 1440*7c478bd9Sstevel@tonic-gate { 1441*7c478bd9Sstevel@tonic-gate int circ; 1442*7c478bd9Sstevel@tonic-gate dev_info_t *dip; 1443*7c478bd9Sstevel@tonic-gate 1444*7c478bd9Sstevel@tonic-gate /* allocate a new node */ 1445*7c478bd9Sstevel@tonic-gate dip = i_ddi_alloc_node(pdip, name, nodeid, (int)unit, NULL, KM_SLEEP); 1446*7c478bd9Sstevel@tonic-gate 1447*7c478bd9Sstevel@tonic-gate ndi_devi_enter(pdip, &circ); 1448*7c478bd9Sstevel@tonic-gate (void) i_ndi_config_node(dip, DS_BOUND, 0); 1449*7c478bd9Sstevel@tonic-gate ndi_devi_exit(pdip, circ); 1450*7c478bd9Sstevel@tonic-gate return (dip); 1451*7c478bd9Sstevel@tonic-gate } 1452*7c478bd9Sstevel@tonic-gate 1453*7c478bd9Sstevel@tonic-gate /* 1454*7c478bd9Sstevel@tonic-gate * ddi_remove_child: remove the dip. The parent must be attached and held 1455*7c478bd9Sstevel@tonic-gate */ 1456*7c478bd9Sstevel@tonic-gate int 1457*7c478bd9Sstevel@tonic-gate ddi_remove_child(dev_info_t *dip, int dummy) 1458*7c478bd9Sstevel@tonic-gate { 1459*7c478bd9Sstevel@tonic-gate _NOTE(ARGUNUSED(dummy)) 1460*7c478bd9Sstevel@tonic-gate int circ, ret; 1461*7c478bd9Sstevel@tonic-gate dev_info_t *parent = ddi_get_parent(dip); 1462*7c478bd9Sstevel@tonic-gate ASSERT(parent); 1463*7c478bd9Sstevel@tonic-gate 1464*7c478bd9Sstevel@tonic-gate ndi_devi_enter(parent, &circ); 1465*7c478bd9Sstevel@tonic-gate 1466*7c478bd9Sstevel@tonic-gate /* 1467*7c478bd9Sstevel@tonic-gate * If we still have children, for example SID nodes marked 1468*7c478bd9Sstevel@tonic-gate * as persistent but not attached, attempt to remove them. 1469*7c478bd9Sstevel@tonic-gate */ 1470*7c478bd9Sstevel@tonic-gate if (DEVI(dip)->devi_child) { 1471*7c478bd9Sstevel@tonic-gate ret = ndi_devi_unconfig(dip, NDI_DEVI_REMOVE); 1472*7c478bd9Sstevel@tonic-gate if (ret != NDI_SUCCESS) { 1473*7c478bd9Sstevel@tonic-gate ndi_devi_exit(parent, circ); 1474*7c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 1475*7c478bd9Sstevel@tonic-gate } 1476*7c478bd9Sstevel@tonic-gate ASSERT(DEVI(dip)->devi_child == NULL); 1477*7c478bd9Sstevel@tonic-gate } 1478*7c478bd9Sstevel@tonic-gate 1479*7c478bd9Sstevel@tonic-gate ret = i_ndi_unconfig_node(dip, DS_PROTO, 0); 1480*7c478bd9Sstevel@tonic-gate ndi_devi_exit(parent, circ); 1481*7c478bd9Sstevel@tonic-gate 1482*7c478bd9Sstevel@tonic-gate if (ret != DDI_SUCCESS) 1483*7c478bd9Sstevel@tonic-gate return (ret); 1484*7c478bd9Sstevel@tonic-gate 1485*7c478bd9Sstevel@tonic-gate ASSERT(i_ddi_node_state(dip) == DS_PROTO); 1486*7c478bd9Sstevel@tonic-gate i_ddi_free_node(dip); 1487*7c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 1488*7c478bd9Sstevel@tonic-gate } 1489*7c478bd9Sstevel@tonic-gate 1490*7c478bd9Sstevel@tonic-gate /* 1491*7c478bd9Sstevel@tonic-gate * NDI wrappers for ref counting, node allocation, and transitions 1492*7c478bd9Sstevel@tonic-gate */ 1493*7c478bd9Sstevel@tonic-gate 1494*7c478bd9Sstevel@tonic-gate /* 1495*7c478bd9Sstevel@tonic-gate * Hold/release the devinfo node itself. 1496*7c478bd9Sstevel@tonic-gate * Caller is assumed to prevent the devi from detaching during this call 1497*7c478bd9Sstevel@tonic-gate */ 1498*7c478bd9Sstevel@tonic-gate void 1499*7c478bd9Sstevel@tonic-gate ndi_hold_devi(dev_info_t *dip) 1500*7c478bd9Sstevel@tonic-gate { 1501*7c478bd9Sstevel@tonic-gate mutex_enter(&DEVI(dip)->devi_lock); 1502*7c478bd9Sstevel@tonic-gate ASSERT(DEVI(dip)->devi_ref >= 0); 1503*7c478bd9Sstevel@tonic-gate DEVI(dip)->devi_ref++; 1504*7c478bd9Sstevel@tonic-gate membar_enter(); /* make sure stores are flushed */ 1505*7c478bd9Sstevel@tonic-gate mutex_exit(&DEVI(dip)->devi_lock); 1506*7c478bd9Sstevel@tonic-gate } 1507*7c478bd9Sstevel@tonic-gate 1508*7c478bd9Sstevel@tonic-gate void 1509*7c478bd9Sstevel@tonic-gate ndi_rele_devi(dev_info_t *dip) 1510*7c478bd9Sstevel@tonic-gate { 1511*7c478bd9Sstevel@tonic-gate ASSERT(DEVI(dip)->devi_ref > 0); 1512*7c478bd9Sstevel@tonic-gate 1513*7c478bd9Sstevel@tonic-gate mutex_enter(&DEVI(dip)->devi_lock); 1514*7c478bd9Sstevel@tonic-gate DEVI(dip)->devi_ref--; 1515*7c478bd9Sstevel@tonic-gate membar_enter(); /* make sure stores are flushed */ 1516*7c478bd9Sstevel@tonic-gate mutex_exit(&DEVI(dip)->devi_lock); 1517*7c478bd9Sstevel@tonic-gate } 1518*7c478bd9Sstevel@tonic-gate 1519*7c478bd9Sstevel@tonic-gate int 1520*7c478bd9Sstevel@tonic-gate e_ddi_devi_holdcnt(dev_info_t *dip) 1521*7c478bd9Sstevel@tonic-gate { 1522*7c478bd9Sstevel@tonic-gate return (DEVI(dip)->devi_ref); 1523*7c478bd9Sstevel@tonic-gate } 1524*7c478bd9Sstevel@tonic-gate 1525*7c478bd9Sstevel@tonic-gate /* 1526*7c478bd9Sstevel@tonic-gate * Hold/release the driver the devinfo node is bound to. 1527*7c478bd9Sstevel@tonic-gate */ 1528*7c478bd9Sstevel@tonic-gate struct dev_ops * 1529*7c478bd9Sstevel@tonic-gate ndi_hold_driver(dev_info_t *dip) 1530*7c478bd9Sstevel@tonic-gate { 1531*7c478bd9Sstevel@tonic-gate if (i_ddi_node_state(dip) < DS_BOUND) 1532*7c478bd9Sstevel@tonic-gate return (NULL); 1533*7c478bd9Sstevel@tonic-gate 1534*7c478bd9Sstevel@tonic-gate ASSERT(DEVI(dip)->devi_major != -1); 1535*7c478bd9Sstevel@tonic-gate return (mod_hold_dev_by_major(DEVI(dip)->devi_major)); 1536*7c478bd9Sstevel@tonic-gate } 1537*7c478bd9Sstevel@tonic-gate 1538*7c478bd9Sstevel@tonic-gate void 1539*7c478bd9Sstevel@tonic-gate ndi_rele_driver(dev_info_t *dip) 1540*7c478bd9Sstevel@tonic-gate { 1541*7c478bd9Sstevel@tonic-gate ASSERT(i_ddi_node_state(dip) >= DS_BOUND); 1542*7c478bd9Sstevel@tonic-gate mod_rele_dev_by_major(DEVI(dip)->devi_major); 1543*7c478bd9Sstevel@tonic-gate } 1544*7c478bd9Sstevel@tonic-gate 1545*7c478bd9Sstevel@tonic-gate /* 1546*7c478bd9Sstevel@tonic-gate * Single thread entry into devinfo node for modifying its children. 1547*7c478bd9Sstevel@tonic-gate * To verify in ASSERTS use DEVI_BUSY_OWNED macro. 1548*7c478bd9Sstevel@tonic-gate */ 1549*7c478bd9Sstevel@tonic-gate void 1550*7c478bd9Sstevel@tonic-gate ndi_devi_enter(dev_info_t *dip, int *circular) 1551*7c478bd9Sstevel@tonic-gate { 1552*7c478bd9Sstevel@tonic-gate struct dev_info *devi = DEVI(dip); 1553*7c478bd9Sstevel@tonic-gate ASSERT(dip != NULL); 1554*7c478bd9Sstevel@tonic-gate 1555*7c478bd9Sstevel@tonic-gate mutex_enter(&devi->devi_lock); 1556*7c478bd9Sstevel@tonic-gate if (devi->devi_busy_thread == curthread) { 1557*7c478bd9Sstevel@tonic-gate devi->devi_circular++; 1558*7c478bd9Sstevel@tonic-gate } else { 1559*7c478bd9Sstevel@tonic-gate while (DEVI_BUSY_CHANGING(devi) && !panicstr) 1560*7c478bd9Sstevel@tonic-gate cv_wait(&(devi->devi_cv), &(devi->devi_lock)); 1561*7c478bd9Sstevel@tonic-gate if (panicstr) { 1562*7c478bd9Sstevel@tonic-gate mutex_exit(&devi->devi_lock); 1563*7c478bd9Sstevel@tonic-gate return; 1564*7c478bd9Sstevel@tonic-gate } 1565*7c478bd9Sstevel@tonic-gate devi->devi_flags |= DEVI_BUSY; 1566*7c478bd9Sstevel@tonic-gate devi->devi_busy_thread = curthread; 1567*7c478bd9Sstevel@tonic-gate } 1568*7c478bd9Sstevel@tonic-gate *circular = devi->devi_circular; 1569*7c478bd9Sstevel@tonic-gate mutex_exit(&devi->devi_lock); 1570*7c478bd9Sstevel@tonic-gate } 1571*7c478bd9Sstevel@tonic-gate 1572*7c478bd9Sstevel@tonic-gate /* 1573*7c478bd9Sstevel@tonic-gate * Release ndi_devi_enter or successful ndi_devi_tryenter. 1574*7c478bd9Sstevel@tonic-gate */ 1575*7c478bd9Sstevel@tonic-gate void 1576*7c478bd9Sstevel@tonic-gate ndi_devi_exit(dev_info_t *dip, int circular) 1577*7c478bd9Sstevel@tonic-gate { 1578*7c478bd9Sstevel@tonic-gate struct dev_info *devi = DEVI(dip); 1579*7c478bd9Sstevel@tonic-gate ASSERT(dip != NULL); 1580*7c478bd9Sstevel@tonic-gate 1581*7c478bd9Sstevel@tonic-gate if (panicstr) 1582*7c478bd9Sstevel@tonic-gate return; 1583*7c478bd9Sstevel@tonic-gate 1584*7c478bd9Sstevel@tonic-gate mutex_enter(&(devi->devi_lock)); 1585*7c478bd9Sstevel@tonic-gate if (circular != 0) { 1586*7c478bd9Sstevel@tonic-gate devi->devi_circular--; 1587*7c478bd9Sstevel@tonic-gate } else { 1588*7c478bd9Sstevel@tonic-gate devi->devi_flags &= ~DEVI_BUSY; 1589*7c478bd9Sstevel@tonic-gate ASSERT(devi->devi_busy_thread == curthread); 1590*7c478bd9Sstevel@tonic-gate devi->devi_busy_thread = NULL; 1591*7c478bd9Sstevel@tonic-gate cv_broadcast(&(devi->devi_cv)); 1592*7c478bd9Sstevel@tonic-gate } 1593*7c478bd9Sstevel@tonic-gate mutex_exit(&(devi->devi_lock)); 1594*7c478bd9Sstevel@tonic-gate } 1595*7c478bd9Sstevel@tonic-gate 1596*7c478bd9Sstevel@tonic-gate /* 1597*7c478bd9Sstevel@tonic-gate * Attempt to single thread entry into devinfo node for modifying its children. 1598*7c478bd9Sstevel@tonic-gate */ 1599*7c478bd9Sstevel@tonic-gate int 1600*7c478bd9Sstevel@tonic-gate ndi_devi_tryenter(dev_info_t *dip, int *circular) 1601*7c478bd9Sstevel@tonic-gate { 1602*7c478bd9Sstevel@tonic-gate int rval = 1; /* assume we enter */ 1603*7c478bd9Sstevel@tonic-gate struct dev_info *devi = DEVI(dip); 1604*7c478bd9Sstevel@tonic-gate ASSERT(dip != NULL); 1605*7c478bd9Sstevel@tonic-gate 1606*7c478bd9Sstevel@tonic-gate mutex_enter(&devi->devi_lock); 1607*7c478bd9Sstevel@tonic-gate if (devi->devi_busy_thread == (void *)curthread) { 1608*7c478bd9Sstevel@tonic-gate devi->devi_circular++; 1609*7c478bd9Sstevel@tonic-gate } else { 1610*7c478bd9Sstevel@tonic-gate if (!DEVI_BUSY_CHANGING(devi)) { 1611*7c478bd9Sstevel@tonic-gate devi->devi_flags |= DEVI_BUSY; 1612*7c478bd9Sstevel@tonic-gate devi->devi_busy_thread = (void *)curthread; 1613*7c478bd9Sstevel@tonic-gate } else { 1614*7c478bd9Sstevel@tonic-gate rval = 0; /* devi is busy */ 1615*7c478bd9Sstevel@tonic-gate } 1616*7c478bd9Sstevel@tonic-gate } 1617*7c478bd9Sstevel@tonic-gate *circular = devi->devi_circular; 1618*7c478bd9Sstevel@tonic-gate mutex_exit(&devi->devi_lock); 1619*7c478bd9Sstevel@tonic-gate return (rval); 1620*7c478bd9Sstevel@tonic-gate } 1621*7c478bd9Sstevel@tonic-gate 1622*7c478bd9Sstevel@tonic-gate /* 1623*7c478bd9Sstevel@tonic-gate * Allocate and initialize a new dev_info structure. 1624*7c478bd9Sstevel@tonic-gate * 1625*7c478bd9Sstevel@tonic-gate * This routine may be called at interrupt time by a nexus in 1626*7c478bd9Sstevel@tonic-gate * response to a hotplug event, therefore memory allocations are 1627*7c478bd9Sstevel@tonic-gate * not allowed to sleep. 1628*7c478bd9Sstevel@tonic-gate */ 1629*7c478bd9Sstevel@tonic-gate int 1630*7c478bd9Sstevel@tonic-gate ndi_devi_alloc(dev_info_t *parent, char *node_name, dnode_t nodeid, 1631*7c478bd9Sstevel@tonic-gate dev_info_t **ret_dip) 1632*7c478bd9Sstevel@tonic-gate { 1633*7c478bd9Sstevel@tonic-gate ASSERT(node_name != NULL); 1634*7c478bd9Sstevel@tonic-gate ASSERT(ret_dip != NULL); 1635*7c478bd9Sstevel@tonic-gate 1636*7c478bd9Sstevel@tonic-gate *ret_dip = i_ddi_alloc_node(parent, node_name, nodeid, -1, NULL, 1637*7c478bd9Sstevel@tonic-gate KM_NOSLEEP); 1638*7c478bd9Sstevel@tonic-gate if (*ret_dip == NULL) { 1639*7c478bd9Sstevel@tonic-gate return (NDI_NOMEM); 1640*7c478bd9Sstevel@tonic-gate } 1641*7c478bd9Sstevel@tonic-gate 1642*7c478bd9Sstevel@tonic-gate return (NDI_SUCCESS); 1643*7c478bd9Sstevel@tonic-gate } 1644*7c478bd9Sstevel@tonic-gate 1645*7c478bd9Sstevel@tonic-gate /* 1646*7c478bd9Sstevel@tonic-gate * Allocate and initialize a new dev_info structure 1647*7c478bd9Sstevel@tonic-gate * This routine may sleep and should not be called at interrupt time 1648*7c478bd9Sstevel@tonic-gate */ 1649*7c478bd9Sstevel@tonic-gate void 1650*7c478bd9Sstevel@tonic-gate ndi_devi_alloc_sleep(dev_info_t *parent, char *node_name, dnode_t nodeid, 1651*7c478bd9Sstevel@tonic-gate dev_info_t **ret_dip) 1652*7c478bd9Sstevel@tonic-gate { 1653*7c478bd9Sstevel@tonic-gate ASSERT(node_name != NULL); 1654*7c478bd9Sstevel@tonic-gate ASSERT(ret_dip != NULL); 1655*7c478bd9Sstevel@tonic-gate 1656*7c478bd9Sstevel@tonic-gate *ret_dip = i_ddi_alloc_node(parent, node_name, nodeid, -1, NULL, 1657*7c478bd9Sstevel@tonic-gate KM_SLEEP); 1658*7c478bd9Sstevel@tonic-gate ASSERT(*ret_dip); 1659*7c478bd9Sstevel@tonic-gate } 1660*7c478bd9Sstevel@tonic-gate 1661*7c478bd9Sstevel@tonic-gate /* 1662*7c478bd9Sstevel@tonic-gate * Remove an initialized (but not yet attached) dev_info 1663*7c478bd9Sstevel@tonic-gate * node from it's parent. 1664*7c478bd9Sstevel@tonic-gate */ 1665*7c478bd9Sstevel@tonic-gate int 1666*7c478bd9Sstevel@tonic-gate ndi_devi_free(dev_info_t *dip) 1667*7c478bd9Sstevel@tonic-gate { 1668*7c478bd9Sstevel@tonic-gate ASSERT(dip != NULL); 1669*7c478bd9Sstevel@tonic-gate 1670*7c478bd9Sstevel@tonic-gate if (i_ddi_node_state(dip) >= DS_INITIALIZED) 1671*7c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 1672*7c478bd9Sstevel@tonic-gate 1673*7c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_CONT, "ndi_devi_free: %s%d (%p)\n", 1674*7c478bd9Sstevel@tonic-gate ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip)); 1675*7c478bd9Sstevel@tonic-gate 1676*7c478bd9Sstevel@tonic-gate (void) ddi_remove_child(dip, 0); 1677*7c478bd9Sstevel@tonic-gate 1678*7c478bd9Sstevel@tonic-gate return (NDI_SUCCESS); 1679*7c478bd9Sstevel@tonic-gate } 1680*7c478bd9Sstevel@tonic-gate 1681*7c478bd9Sstevel@tonic-gate /* 1682*7c478bd9Sstevel@tonic-gate * ndi_devi_bind_driver() binds a driver to a given device. If it fails 1683*7c478bd9Sstevel@tonic-gate * to bind the driver, it returns an appropriate error back. Some drivers 1684*7c478bd9Sstevel@tonic-gate * may want to know if the actually failed to bind. 1685*7c478bd9Sstevel@tonic-gate */ 1686*7c478bd9Sstevel@tonic-gate int 1687*7c478bd9Sstevel@tonic-gate ndi_devi_bind_driver(dev_info_t *dip, uint_t flags) 1688*7c478bd9Sstevel@tonic-gate { 1689*7c478bd9Sstevel@tonic-gate int ret = NDI_FAILURE; 1690*7c478bd9Sstevel@tonic-gate int circ; 1691*7c478bd9Sstevel@tonic-gate dev_info_t *pdip = ddi_get_parent(dip); 1692*7c478bd9Sstevel@tonic-gate ASSERT(pdip); 1693*7c478bd9Sstevel@tonic-gate 1694*7c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_CONT, 1695*7c478bd9Sstevel@tonic-gate "ndi_devi_bind_driver: %s%d (%p) flags: %x\n", 1696*7c478bd9Sstevel@tonic-gate ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags)); 1697*7c478bd9Sstevel@tonic-gate 1698*7c478bd9Sstevel@tonic-gate ndi_devi_enter(pdip, &circ); 1699*7c478bd9Sstevel@tonic-gate if (i_ndi_config_node(dip, DS_BOUND, flags) == DDI_SUCCESS) 1700*7c478bd9Sstevel@tonic-gate ret = NDI_SUCCESS; 1701*7c478bd9Sstevel@tonic-gate ndi_devi_exit(pdip, circ); 1702*7c478bd9Sstevel@tonic-gate 1703*7c478bd9Sstevel@tonic-gate return (ret); 1704*7c478bd9Sstevel@tonic-gate } 1705*7c478bd9Sstevel@tonic-gate 1706*7c478bd9Sstevel@tonic-gate /* 1707*7c478bd9Sstevel@tonic-gate * ndi_devi_unbind_driver: unbind the dip 1708*7c478bd9Sstevel@tonic-gate */ 1709*7c478bd9Sstevel@tonic-gate static int 1710*7c478bd9Sstevel@tonic-gate ndi_devi_unbind_driver(dev_info_t *dip) 1711*7c478bd9Sstevel@tonic-gate { 1712*7c478bd9Sstevel@tonic-gate ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip))); 1713*7c478bd9Sstevel@tonic-gate 1714*7c478bd9Sstevel@tonic-gate return (i_ndi_unconfig_node(dip, DS_LINKED, 0)); 1715*7c478bd9Sstevel@tonic-gate } 1716*7c478bd9Sstevel@tonic-gate 1717*7c478bd9Sstevel@tonic-gate /* 1718*7c478bd9Sstevel@tonic-gate * Misc. help routines called by framework only 1719*7c478bd9Sstevel@tonic-gate */ 1720*7c478bd9Sstevel@tonic-gate 1721*7c478bd9Sstevel@tonic-gate /* 1722*7c478bd9Sstevel@tonic-gate * Get the state of node 1723*7c478bd9Sstevel@tonic-gate */ 1724*7c478bd9Sstevel@tonic-gate ddi_node_state_t 1725*7c478bd9Sstevel@tonic-gate i_ddi_node_state(dev_info_t *dip) 1726*7c478bd9Sstevel@tonic-gate { 1727*7c478bd9Sstevel@tonic-gate return (DEVI(dip)->devi_node_state); 1728*7c478bd9Sstevel@tonic-gate } 1729*7c478bd9Sstevel@tonic-gate 1730*7c478bd9Sstevel@tonic-gate /* 1731*7c478bd9Sstevel@tonic-gate * Set the state of node 1732*7c478bd9Sstevel@tonic-gate */ 1733*7c478bd9Sstevel@tonic-gate void 1734*7c478bd9Sstevel@tonic-gate i_ddi_set_node_state(dev_info_t *dip, ddi_node_state_t state) 1735*7c478bd9Sstevel@tonic-gate { 1736*7c478bd9Sstevel@tonic-gate DEVI(dip)->devi_node_state = state; 1737*7c478bd9Sstevel@tonic-gate membar_enter(); /* make sure stores are flushed */ 1738*7c478bd9Sstevel@tonic-gate } 1739*7c478bd9Sstevel@tonic-gate 1740*7c478bd9Sstevel@tonic-gate /* 1741*7c478bd9Sstevel@tonic-gate * Common function for finding a node in a sibling list given name and addr. 1742*7c478bd9Sstevel@tonic-gate * 1743*7c478bd9Sstevel@tonic-gate * By default, name is matched with devi_node_name. The following 1744*7c478bd9Sstevel@tonic-gate * alternative match strategies are supported: 1745*7c478bd9Sstevel@tonic-gate * 1746*7c478bd9Sstevel@tonic-gate * FIND_NAME_BY_DRIVER: A match on driver name bound to node is conducted. 1747*7c478bd9Sstevel@tonic-gate * This support is used for support of OBP generic names and 1748*7c478bd9Sstevel@tonic-gate * for the conversion from driver names to generic names. When 1749*7c478bd9Sstevel@tonic-gate * more consistency in the generic name environment is achieved 1750*7c478bd9Sstevel@tonic-gate * (and not needed for upgrade) this support can be removed. 1751*7c478bd9Sstevel@tonic-gate * 1752*7c478bd9Sstevel@tonic-gate * If a child is not named (dev_addr == NULL), there are three 1753*7c478bd9Sstevel@tonic-gate * possible actions: 1754*7c478bd9Sstevel@tonic-gate * 1755*7c478bd9Sstevel@tonic-gate * (1) skip it 1756*7c478bd9Sstevel@tonic-gate * (2) FIND_ADDR_BY_INIT: bring child to DS_INITIALIZED state 1757*7c478bd9Sstevel@tonic-gate * (3) FIND_ADDR_BY_CALLBACK: use a caller-supplied callback function 1758*7c478bd9Sstevel@tonic-gate */ 1759*7c478bd9Sstevel@tonic-gate #define FIND_NAME_BY_DRIVER 0x01 1760*7c478bd9Sstevel@tonic-gate #define FIND_ADDR_BY_INIT 0x10 1761*7c478bd9Sstevel@tonic-gate #define FIND_ADDR_BY_CALLBACK 0x20 1762*7c478bd9Sstevel@tonic-gate 1763*7c478bd9Sstevel@tonic-gate static dev_info_t * 1764*7c478bd9Sstevel@tonic-gate find_sibling(dev_info_t *head, char *cname, char *caddr, uint_t flag, 1765*7c478bd9Sstevel@tonic-gate int (*callback)(dev_info_t *, char *, int)) 1766*7c478bd9Sstevel@tonic-gate { 1767*7c478bd9Sstevel@tonic-gate dev_info_t *dip; 1768*7c478bd9Sstevel@tonic-gate char *addr, *buf; 1769*7c478bd9Sstevel@tonic-gate major_t major; 1770*7c478bd9Sstevel@tonic-gate 1771*7c478bd9Sstevel@tonic-gate /* only one way to name a node */ 1772*7c478bd9Sstevel@tonic-gate ASSERT(((flag & FIND_ADDR_BY_INIT) == 0) || 1773*7c478bd9Sstevel@tonic-gate ((flag & FIND_ADDR_BY_CALLBACK) == 0)); 1774*7c478bd9Sstevel@tonic-gate 1775*7c478bd9Sstevel@tonic-gate if (flag & FIND_NAME_BY_DRIVER) { 1776*7c478bd9Sstevel@tonic-gate major = ddi_name_to_major(cname); 1777*7c478bd9Sstevel@tonic-gate if (major == (major_t)-1) 1778*7c478bd9Sstevel@tonic-gate return (NULL); 1779*7c478bd9Sstevel@tonic-gate } 1780*7c478bd9Sstevel@tonic-gate 1781*7c478bd9Sstevel@tonic-gate /* preallocate buffer of naming node by callback */ 1782*7c478bd9Sstevel@tonic-gate if (flag & FIND_ADDR_BY_CALLBACK) 1783*7c478bd9Sstevel@tonic-gate buf = kmem_alloc(MAXNAMELEN, KM_SLEEP); 1784*7c478bd9Sstevel@tonic-gate 1785*7c478bd9Sstevel@tonic-gate /* 1786*7c478bd9Sstevel@tonic-gate * Walk the child list to find a match 1787*7c478bd9Sstevel@tonic-gate */ 1788*7c478bd9Sstevel@tonic-gate 1789*7c478bd9Sstevel@tonic-gate for (dip = head; dip; dip = ddi_get_next_sibling(dip)) { 1790*7c478bd9Sstevel@tonic-gate if (flag & FIND_NAME_BY_DRIVER) { 1791*7c478bd9Sstevel@tonic-gate /* match driver major */ 1792*7c478bd9Sstevel@tonic-gate if (DEVI(dip)->devi_major != major) 1793*7c478bd9Sstevel@tonic-gate continue; 1794*7c478bd9Sstevel@tonic-gate } else { 1795*7c478bd9Sstevel@tonic-gate /* match node name */ 1796*7c478bd9Sstevel@tonic-gate if (strcmp(cname, DEVI(dip)->devi_node_name) != 0) 1797*7c478bd9Sstevel@tonic-gate continue; 1798*7c478bd9Sstevel@tonic-gate } 1799*7c478bd9Sstevel@tonic-gate 1800*7c478bd9Sstevel@tonic-gate if ((addr = DEVI(dip)->devi_addr) == NULL) { 1801*7c478bd9Sstevel@tonic-gate /* name the child based on the flag */ 1802*7c478bd9Sstevel@tonic-gate if (flag & FIND_ADDR_BY_INIT) { 1803*7c478bd9Sstevel@tonic-gate if (ddi_initchild(ddi_get_parent(dip), dip) 1804*7c478bd9Sstevel@tonic-gate != DDI_SUCCESS) 1805*7c478bd9Sstevel@tonic-gate continue; 1806*7c478bd9Sstevel@tonic-gate addr = DEVI(dip)->devi_addr; 1807*7c478bd9Sstevel@tonic-gate } else if (flag & FIND_ADDR_BY_CALLBACK) { 1808*7c478bd9Sstevel@tonic-gate if ((callback == NULL) || (callback( 1809*7c478bd9Sstevel@tonic-gate dip, buf, MAXNAMELEN) != DDI_SUCCESS)) 1810*7c478bd9Sstevel@tonic-gate continue; 1811*7c478bd9Sstevel@tonic-gate addr = buf; 1812*7c478bd9Sstevel@tonic-gate } else { 1813*7c478bd9Sstevel@tonic-gate continue; /* skip */ 1814*7c478bd9Sstevel@tonic-gate } 1815*7c478bd9Sstevel@tonic-gate } 1816*7c478bd9Sstevel@tonic-gate 1817*7c478bd9Sstevel@tonic-gate /* match addr */ 1818*7c478bd9Sstevel@tonic-gate ASSERT(addr != NULL); 1819*7c478bd9Sstevel@tonic-gate if (strcmp(caddr, addr) == 0) 1820*7c478bd9Sstevel@tonic-gate break; /* node found */ 1821*7c478bd9Sstevel@tonic-gate 1822*7c478bd9Sstevel@tonic-gate } 1823*7c478bd9Sstevel@tonic-gate if (flag & FIND_ADDR_BY_CALLBACK) 1824*7c478bd9Sstevel@tonic-gate kmem_free(buf, MAXNAMELEN); 1825*7c478bd9Sstevel@tonic-gate return (dip); 1826*7c478bd9Sstevel@tonic-gate } 1827*7c478bd9Sstevel@tonic-gate 1828*7c478bd9Sstevel@tonic-gate /* 1829*7c478bd9Sstevel@tonic-gate * Find child of pdip with name: cname@caddr 1830*7c478bd9Sstevel@tonic-gate * Called by init_node() to look for duplicate nodes 1831*7c478bd9Sstevel@tonic-gate */ 1832*7c478bd9Sstevel@tonic-gate static dev_info_t * 1833*7c478bd9Sstevel@tonic-gate find_duplicate_child(dev_info_t *pdip, dev_info_t *dip) 1834*7c478bd9Sstevel@tonic-gate { 1835*7c478bd9Sstevel@tonic-gate dev_info_t *dup; 1836*7c478bd9Sstevel@tonic-gate char *cname = DEVI(dip)->devi_node_name; 1837*7c478bd9Sstevel@tonic-gate char *caddr = DEVI(dip)->devi_addr; 1838*7c478bd9Sstevel@tonic-gate 1839*7c478bd9Sstevel@tonic-gate /* search nodes before dip */ 1840*7c478bd9Sstevel@tonic-gate dup = find_sibling(ddi_get_child(pdip), cname, caddr, 0, NULL); 1841*7c478bd9Sstevel@tonic-gate if (dup != dip) 1842*7c478bd9Sstevel@tonic-gate return (dup); 1843*7c478bd9Sstevel@tonic-gate 1844*7c478bd9Sstevel@tonic-gate /* 1845*7c478bd9Sstevel@tonic-gate * search nodes after dip; normally this is not needed, 1846*7c478bd9Sstevel@tonic-gate */ 1847*7c478bd9Sstevel@tonic-gate return (find_sibling(ddi_get_next_sibling(dip), cname, caddr, 1848*7c478bd9Sstevel@tonic-gate 0, NULL)); 1849*7c478bd9Sstevel@tonic-gate } 1850*7c478bd9Sstevel@tonic-gate 1851*7c478bd9Sstevel@tonic-gate /* 1852*7c478bd9Sstevel@tonic-gate * Find a child of a given name and address, using a callback to name 1853*7c478bd9Sstevel@tonic-gate * unnamed children. cname is the binding name. 1854*7c478bd9Sstevel@tonic-gate */ 1855*7c478bd9Sstevel@tonic-gate static dev_info_t * 1856*7c478bd9Sstevel@tonic-gate find_child_by_callback(dev_info_t *pdip, char *cname, char *caddr, 1857*7c478bd9Sstevel@tonic-gate int (*name_node)(dev_info_t *, char *, int)) 1858*7c478bd9Sstevel@tonic-gate { 1859*7c478bd9Sstevel@tonic-gate return (find_sibling(ddi_get_child(pdip), cname, caddr, 1860*7c478bd9Sstevel@tonic-gate FIND_NAME_BY_DRIVER|FIND_ADDR_BY_CALLBACK, name_node)); 1861*7c478bd9Sstevel@tonic-gate } 1862*7c478bd9Sstevel@tonic-gate 1863*7c478bd9Sstevel@tonic-gate /* 1864*7c478bd9Sstevel@tonic-gate * Find a child of a given name and address, invoking initchild to name 1865*7c478bd9Sstevel@tonic-gate * unnamed children. cname is the node name. 1866*7c478bd9Sstevel@tonic-gate */ 1867*7c478bd9Sstevel@tonic-gate static dev_info_t * 1868*7c478bd9Sstevel@tonic-gate find_child_by_name(dev_info_t *pdip, char *cname, char *caddr) 1869*7c478bd9Sstevel@tonic-gate { 1870*7c478bd9Sstevel@tonic-gate dev_info_t *dip; 1871*7c478bd9Sstevel@tonic-gate 1872*7c478bd9Sstevel@tonic-gate /* attempt search without changing state of preceeding siblings */ 1873*7c478bd9Sstevel@tonic-gate dip = find_sibling(ddi_get_child(pdip), cname, caddr, 0, NULL); 1874*7c478bd9Sstevel@tonic-gate if (dip) 1875*7c478bd9Sstevel@tonic-gate return (dip); 1876*7c478bd9Sstevel@tonic-gate 1877*7c478bd9Sstevel@tonic-gate return (find_sibling(ddi_get_child(pdip), cname, caddr, 1878*7c478bd9Sstevel@tonic-gate FIND_ADDR_BY_INIT, NULL)); 1879*7c478bd9Sstevel@tonic-gate } 1880*7c478bd9Sstevel@tonic-gate 1881*7c478bd9Sstevel@tonic-gate /* 1882*7c478bd9Sstevel@tonic-gate * Find a child of a given name and address, invoking initchild to name 1883*7c478bd9Sstevel@tonic-gate * unnamed children. cname is the node name. 1884*7c478bd9Sstevel@tonic-gate */ 1885*7c478bd9Sstevel@tonic-gate static dev_info_t * 1886*7c478bd9Sstevel@tonic-gate find_child_by_driver(dev_info_t *pdip, char *cname, char *caddr) 1887*7c478bd9Sstevel@tonic-gate { 1888*7c478bd9Sstevel@tonic-gate dev_info_t *dip; 1889*7c478bd9Sstevel@tonic-gate 1890*7c478bd9Sstevel@tonic-gate /* attempt search without changing state of preceeding siblings */ 1891*7c478bd9Sstevel@tonic-gate dip = find_sibling(ddi_get_child(pdip), cname, caddr, 1892*7c478bd9Sstevel@tonic-gate FIND_NAME_BY_DRIVER, NULL); 1893*7c478bd9Sstevel@tonic-gate if (dip) 1894*7c478bd9Sstevel@tonic-gate return (dip); 1895*7c478bd9Sstevel@tonic-gate 1896*7c478bd9Sstevel@tonic-gate return (find_sibling(ddi_get_child(pdip), cname, caddr, 1897*7c478bd9Sstevel@tonic-gate FIND_NAME_BY_DRIVER|FIND_ADDR_BY_INIT, NULL)); 1898*7c478bd9Sstevel@tonic-gate } 1899*7c478bd9Sstevel@tonic-gate 1900*7c478bd9Sstevel@tonic-gate /* 1901*7c478bd9Sstevel@tonic-gate * Deleting a property list. Take care, since some property structures 1902*7c478bd9Sstevel@tonic-gate * may not be fully built. 1903*7c478bd9Sstevel@tonic-gate */ 1904*7c478bd9Sstevel@tonic-gate void 1905*7c478bd9Sstevel@tonic-gate i_ddi_prop_list_delete(ddi_prop_t *prop) 1906*7c478bd9Sstevel@tonic-gate { 1907*7c478bd9Sstevel@tonic-gate while (prop) { 1908*7c478bd9Sstevel@tonic-gate ddi_prop_t *next = prop->prop_next; 1909*7c478bd9Sstevel@tonic-gate if (prop->prop_name) 1910*7c478bd9Sstevel@tonic-gate kmem_free(prop->prop_name, strlen(prop->prop_name) + 1); 1911*7c478bd9Sstevel@tonic-gate if ((prop->prop_len != 0) && prop->prop_val) 1912*7c478bd9Sstevel@tonic-gate kmem_free(prop->prop_val, prop->prop_len); 1913*7c478bd9Sstevel@tonic-gate kmem_free(prop, sizeof (struct ddi_prop)); 1914*7c478bd9Sstevel@tonic-gate prop = next; 1915*7c478bd9Sstevel@tonic-gate } 1916*7c478bd9Sstevel@tonic-gate } 1917*7c478bd9Sstevel@tonic-gate 1918*7c478bd9Sstevel@tonic-gate /* 1919*7c478bd9Sstevel@tonic-gate * Duplicate property list 1920*7c478bd9Sstevel@tonic-gate */ 1921*7c478bd9Sstevel@tonic-gate ddi_prop_t * 1922*7c478bd9Sstevel@tonic-gate i_ddi_prop_list_dup(ddi_prop_t *prop, uint_t flag) 1923*7c478bd9Sstevel@tonic-gate { 1924*7c478bd9Sstevel@tonic-gate ddi_prop_t *result, *prev, *copy; 1925*7c478bd9Sstevel@tonic-gate 1926*7c478bd9Sstevel@tonic-gate if (prop == NULL) 1927*7c478bd9Sstevel@tonic-gate return (NULL); 1928*7c478bd9Sstevel@tonic-gate 1929*7c478bd9Sstevel@tonic-gate result = prev = NULL; 1930*7c478bd9Sstevel@tonic-gate for (; prop != NULL; prop = prop->prop_next) { 1931*7c478bd9Sstevel@tonic-gate ASSERT(prop->prop_name != NULL); 1932*7c478bd9Sstevel@tonic-gate copy = kmem_zalloc(sizeof (struct ddi_prop), flag); 1933*7c478bd9Sstevel@tonic-gate if (copy == NULL) 1934*7c478bd9Sstevel@tonic-gate goto fail; 1935*7c478bd9Sstevel@tonic-gate 1936*7c478bd9Sstevel@tonic-gate copy->prop_dev = prop->prop_dev; 1937*7c478bd9Sstevel@tonic-gate copy->prop_flags = prop->prop_flags; 1938*7c478bd9Sstevel@tonic-gate copy->prop_name = i_ddi_strdup(prop->prop_name, flag); 1939*7c478bd9Sstevel@tonic-gate if (copy->prop_name == NULL) 1940*7c478bd9Sstevel@tonic-gate goto fail; 1941*7c478bd9Sstevel@tonic-gate 1942*7c478bd9Sstevel@tonic-gate if ((copy->prop_len = prop->prop_len) != 0) { 1943*7c478bd9Sstevel@tonic-gate copy->prop_val = kmem_zalloc(prop->prop_len, flag); 1944*7c478bd9Sstevel@tonic-gate if (copy->prop_val == NULL) 1945*7c478bd9Sstevel@tonic-gate goto fail; 1946*7c478bd9Sstevel@tonic-gate 1947*7c478bd9Sstevel@tonic-gate bcopy(prop->prop_val, copy->prop_val, prop->prop_len); 1948*7c478bd9Sstevel@tonic-gate } 1949*7c478bd9Sstevel@tonic-gate 1950*7c478bd9Sstevel@tonic-gate if (prev == NULL) 1951*7c478bd9Sstevel@tonic-gate result = prev = copy; 1952*7c478bd9Sstevel@tonic-gate else 1953*7c478bd9Sstevel@tonic-gate prev->prop_next = copy; 1954*7c478bd9Sstevel@tonic-gate prev = copy; 1955*7c478bd9Sstevel@tonic-gate } 1956*7c478bd9Sstevel@tonic-gate return (result); 1957*7c478bd9Sstevel@tonic-gate 1958*7c478bd9Sstevel@tonic-gate fail: 1959*7c478bd9Sstevel@tonic-gate i_ddi_prop_list_delete(result); 1960*7c478bd9Sstevel@tonic-gate return (NULL); 1961*7c478bd9Sstevel@tonic-gate } 1962*7c478bd9Sstevel@tonic-gate 1963*7c478bd9Sstevel@tonic-gate /* 1964*7c478bd9Sstevel@tonic-gate * Create a reference property list, currently used only for 1965*7c478bd9Sstevel@tonic-gate * driver global properties. Created with ref count of 1. 1966*7c478bd9Sstevel@tonic-gate */ 1967*7c478bd9Sstevel@tonic-gate ddi_prop_list_t * 1968*7c478bd9Sstevel@tonic-gate i_ddi_prop_list_create(ddi_prop_t *props) 1969*7c478bd9Sstevel@tonic-gate { 1970*7c478bd9Sstevel@tonic-gate ddi_prop_list_t *list = kmem_alloc(sizeof (*list), KM_SLEEP); 1971*7c478bd9Sstevel@tonic-gate list->prop_list = props; 1972*7c478bd9Sstevel@tonic-gate list->prop_ref = 1; 1973*7c478bd9Sstevel@tonic-gate return (list); 1974*7c478bd9Sstevel@tonic-gate } 1975*7c478bd9Sstevel@tonic-gate 1976*7c478bd9Sstevel@tonic-gate /* 1977*7c478bd9Sstevel@tonic-gate * Increment/decrement reference count. The reference is 1978*7c478bd9Sstevel@tonic-gate * protected by dn_lock. The only interfaces modifying 1979*7c478bd9Sstevel@tonic-gate * dn_global_prop_ptr is in impl_make[free]_parlist(). 1980*7c478bd9Sstevel@tonic-gate */ 1981*7c478bd9Sstevel@tonic-gate void 1982*7c478bd9Sstevel@tonic-gate i_ddi_prop_list_hold(ddi_prop_list_t *prop_list, struct devnames *dnp) 1983*7c478bd9Sstevel@tonic-gate { 1984*7c478bd9Sstevel@tonic-gate ASSERT(prop_list->prop_ref >= 0); 1985*7c478bd9Sstevel@tonic-gate ASSERT(mutex_owned(&dnp->dn_lock)); 1986*7c478bd9Sstevel@tonic-gate prop_list->prop_ref++; 1987*7c478bd9Sstevel@tonic-gate } 1988*7c478bd9Sstevel@tonic-gate 1989*7c478bd9Sstevel@tonic-gate void 1990*7c478bd9Sstevel@tonic-gate i_ddi_prop_list_rele(ddi_prop_list_t *prop_list, struct devnames *dnp) 1991*7c478bd9Sstevel@tonic-gate { 1992*7c478bd9Sstevel@tonic-gate ASSERT(prop_list->prop_ref > 0); 1993*7c478bd9Sstevel@tonic-gate ASSERT(mutex_owned(&dnp->dn_lock)); 1994*7c478bd9Sstevel@tonic-gate prop_list->prop_ref--; 1995*7c478bd9Sstevel@tonic-gate 1996*7c478bd9Sstevel@tonic-gate if (prop_list->prop_ref == 0) { 1997*7c478bd9Sstevel@tonic-gate i_ddi_prop_list_delete(prop_list->prop_list); 1998*7c478bd9Sstevel@tonic-gate kmem_free(prop_list, sizeof (*prop_list)); 1999*7c478bd9Sstevel@tonic-gate } 2000*7c478bd9Sstevel@tonic-gate } 2001*7c478bd9Sstevel@tonic-gate 2002*7c478bd9Sstevel@tonic-gate /* 2003*7c478bd9Sstevel@tonic-gate * Free table of classes by drivers 2004*7c478bd9Sstevel@tonic-gate */ 2005*7c478bd9Sstevel@tonic-gate void 2006*7c478bd9Sstevel@tonic-gate i_ddi_free_exported_classes(char **classes, int n) 2007*7c478bd9Sstevel@tonic-gate { 2008*7c478bd9Sstevel@tonic-gate if ((n == 0) || (classes == NULL)) 2009*7c478bd9Sstevel@tonic-gate return; 2010*7c478bd9Sstevel@tonic-gate 2011*7c478bd9Sstevel@tonic-gate kmem_free(classes, n * sizeof (char *)); 2012*7c478bd9Sstevel@tonic-gate } 2013*7c478bd9Sstevel@tonic-gate 2014*7c478bd9Sstevel@tonic-gate /* 2015*7c478bd9Sstevel@tonic-gate * Get all classes exported by dip 2016*7c478bd9Sstevel@tonic-gate */ 2017*7c478bd9Sstevel@tonic-gate int 2018*7c478bd9Sstevel@tonic-gate i_ddi_get_exported_classes(dev_info_t *dip, char ***classes) 2019*7c478bd9Sstevel@tonic-gate { 2020*7c478bd9Sstevel@tonic-gate extern void lock_hw_class_list(); 2021*7c478bd9Sstevel@tonic-gate extern void unlock_hw_class_list(); 2022*7c478bd9Sstevel@tonic-gate extern int get_class(const char *, char **); 2023*7c478bd9Sstevel@tonic-gate 2024*7c478bd9Sstevel@tonic-gate static char *rootclass = "root"; 2025*7c478bd9Sstevel@tonic-gate int n = 0, nclass = 0; 2026*7c478bd9Sstevel@tonic-gate char **buf; 2027*7c478bd9Sstevel@tonic-gate 2028*7c478bd9Sstevel@tonic-gate ASSERT(i_ddi_node_state(dip) >= DS_BOUND); 2029*7c478bd9Sstevel@tonic-gate 2030*7c478bd9Sstevel@tonic-gate if (dip == ddi_root_node()) /* rootnode exports class "root" */ 2031*7c478bd9Sstevel@tonic-gate nclass = 1; 2032*7c478bd9Sstevel@tonic-gate lock_hw_class_list(); 2033*7c478bd9Sstevel@tonic-gate nclass += get_class(ddi_driver_name(dip), NULL); 2034*7c478bd9Sstevel@tonic-gate if (nclass == 0) { 2035*7c478bd9Sstevel@tonic-gate unlock_hw_class_list(); 2036*7c478bd9Sstevel@tonic-gate return (0); /* no class exported */ 2037*7c478bd9Sstevel@tonic-gate } 2038*7c478bd9Sstevel@tonic-gate 2039*7c478bd9Sstevel@tonic-gate *classes = buf = kmem_alloc(nclass * sizeof (char *), KM_SLEEP); 2040*7c478bd9Sstevel@tonic-gate if (dip == ddi_root_node()) { 2041*7c478bd9Sstevel@tonic-gate *buf++ = rootclass; 2042*7c478bd9Sstevel@tonic-gate n = 1; 2043*7c478bd9Sstevel@tonic-gate } 2044*7c478bd9Sstevel@tonic-gate n += get_class(ddi_driver_name(dip), buf); 2045*7c478bd9Sstevel@tonic-gate unlock_hw_class_list(); 2046*7c478bd9Sstevel@tonic-gate 2047*7c478bd9Sstevel@tonic-gate ASSERT(n == nclass); /* make sure buf wasn't overrun */ 2048*7c478bd9Sstevel@tonic-gate return (nclass); 2049*7c478bd9Sstevel@tonic-gate } 2050*7c478bd9Sstevel@tonic-gate 2051*7c478bd9Sstevel@tonic-gate /* 2052*7c478bd9Sstevel@tonic-gate * Helper functions, returns NULL if no memory. 2053*7c478bd9Sstevel@tonic-gate */ 2054*7c478bd9Sstevel@tonic-gate char * 2055*7c478bd9Sstevel@tonic-gate i_ddi_strdup(char *str, uint_t flag) 2056*7c478bd9Sstevel@tonic-gate { 2057*7c478bd9Sstevel@tonic-gate char *copy; 2058*7c478bd9Sstevel@tonic-gate 2059*7c478bd9Sstevel@tonic-gate if (str == NULL) 2060*7c478bd9Sstevel@tonic-gate return (NULL); 2061*7c478bd9Sstevel@tonic-gate 2062*7c478bd9Sstevel@tonic-gate copy = kmem_alloc(strlen(str) + 1, flag); 2063*7c478bd9Sstevel@tonic-gate if (copy == NULL) 2064*7c478bd9Sstevel@tonic-gate return (NULL); 2065*7c478bd9Sstevel@tonic-gate 2066*7c478bd9Sstevel@tonic-gate (void) strcpy(copy, str); 2067*7c478bd9Sstevel@tonic-gate return (copy); 2068*7c478bd9Sstevel@tonic-gate } 2069*7c478bd9Sstevel@tonic-gate 2070*7c478bd9Sstevel@tonic-gate /* 2071*7c478bd9Sstevel@tonic-gate * Load driver.conf file for major. Load all if major == -1. 2072*7c478bd9Sstevel@tonic-gate * 2073*7c478bd9Sstevel@tonic-gate * This is called 2074*7c478bd9Sstevel@tonic-gate * - early in boot after devnames array is initialized 2075*7c478bd9Sstevel@tonic-gate * - from vfs code when certain file systems are mounted 2076*7c478bd9Sstevel@tonic-gate * - from add_drv when a new driver is added 2077*7c478bd9Sstevel@tonic-gate */ 2078*7c478bd9Sstevel@tonic-gate int 2079*7c478bd9Sstevel@tonic-gate i_ddi_load_drvconf(major_t major) 2080*7c478bd9Sstevel@tonic-gate { 2081*7c478bd9Sstevel@tonic-gate extern int modrootloaded; 2082*7c478bd9Sstevel@tonic-gate 2083*7c478bd9Sstevel@tonic-gate major_t low, high, m; 2084*7c478bd9Sstevel@tonic-gate 2085*7c478bd9Sstevel@tonic-gate if (major == (major_t)-1) { 2086*7c478bd9Sstevel@tonic-gate low = 0; 2087*7c478bd9Sstevel@tonic-gate high = devcnt - 1; 2088*7c478bd9Sstevel@tonic-gate } else { 2089*7c478bd9Sstevel@tonic-gate if (major >= devcnt) 2090*7c478bd9Sstevel@tonic-gate return (EINVAL); 2091*7c478bd9Sstevel@tonic-gate low = high = major; 2092*7c478bd9Sstevel@tonic-gate } 2093*7c478bd9Sstevel@tonic-gate 2094*7c478bd9Sstevel@tonic-gate for (m = low; m <= high; m++) { 2095*7c478bd9Sstevel@tonic-gate struct devnames *dnp = &devnamesp[m]; 2096*7c478bd9Sstevel@tonic-gate LOCK_DEV_OPS(&dnp->dn_lock); 2097*7c478bd9Sstevel@tonic-gate dnp->dn_flags &= ~DN_DRIVER_HELD; 2098*7c478bd9Sstevel@tonic-gate (void) impl_make_parlist(m); 2099*7c478bd9Sstevel@tonic-gate UNLOCK_DEV_OPS(&dnp->dn_lock); 2100*7c478bd9Sstevel@tonic-gate } 2101*7c478bd9Sstevel@tonic-gate 2102*7c478bd9Sstevel@tonic-gate if (modrootloaded) { 2103*7c478bd9Sstevel@tonic-gate ddi_walk_devs(ddi_root_node(), reset_nexus_flags, 2104*7c478bd9Sstevel@tonic-gate (void *)(uintptr_t)major); 2105*7c478bd9Sstevel@tonic-gate } 2106*7c478bd9Sstevel@tonic-gate 2107*7c478bd9Sstevel@tonic-gate /* build dn_list from old entries in path_to_inst */ 2108*7c478bd9Sstevel@tonic-gate e_ddi_unorphan_instance_nos(); 2109*7c478bd9Sstevel@tonic-gate return (0); 2110*7c478bd9Sstevel@tonic-gate } 2111*7c478bd9Sstevel@tonic-gate 2112*7c478bd9Sstevel@tonic-gate /* 2113*7c478bd9Sstevel@tonic-gate * Unload a specific driver.conf. 2114*7c478bd9Sstevel@tonic-gate * Don't support unload all because it doesn't make any sense 2115*7c478bd9Sstevel@tonic-gate */ 2116*7c478bd9Sstevel@tonic-gate int 2117*7c478bd9Sstevel@tonic-gate i_ddi_unload_drvconf(major_t major) 2118*7c478bd9Sstevel@tonic-gate { 2119*7c478bd9Sstevel@tonic-gate int error; 2120*7c478bd9Sstevel@tonic-gate struct devnames *dnp; 2121*7c478bd9Sstevel@tonic-gate 2122*7c478bd9Sstevel@tonic-gate if (major >= devcnt) 2123*7c478bd9Sstevel@tonic-gate return (EINVAL); 2124*7c478bd9Sstevel@tonic-gate 2125*7c478bd9Sstevel@tonic-gate /* 2126*7c478bd9Sstevel@tonic-gate * Take the per-driver lock while unloading driver.conf 2127*7c478bd9Sstevel@tonic-gate */ 2128*7c478bd9Sstevel@tonic-gate dnp = &devnamesp[major]; 2129*7c478bd9Sstevel@tonic-gate LOCK_DEV_OPS(&dnp->dn_lock); 2130*7c478bd9Sstevel@tonic-gate error = impl_free_parlist(major); 2131*7c478bd9Sstevel@tonic-gate UNLOCK_DEV_OPS(&dnp->dn_lock); 2132*7c478bd9Sstevel@tonic-gate return (error); 2133*7c478bd9Sstevel@tonic-gate } 2134*7c478bd9Sstevel@tonic-gate 2135*7c478bd9Sstevel@tonic-gate /* 2136*7c478bd9Sstevel@tonic-gate * Merge a .conf node. This is called by nexus drivers to augment 2137*7c478bd9Sstevel@tonic-gate * hw node with properties specified in driver.conf file. This function 2138*7c478bd9Sstevel@tonic-gate * takes a callback routine to name nexus children. 2139*7c478bd9Sstevel@tonic-gate * The parent node must be held busy. 2140*7c478bd9Sstevel@tonic-gate * 2141*7c478bd9Sstevel@tonic-gate * It returns DDI_SUCCESS if the node is merged and DDI_FAILURE otherwise. 2142*7c478bd9Sstevel@tonic-gate */ 2143*7c478bd9Sstevel@tonic-gate int 2144*7c478bd9Sstevel@tonic-gate ndi_merge_node(dev_info_t *dip, int (*name_node)(dev_info_t *, char *, int)) 2145*7c478bd9Sstevel@tonic-gate { 2146*7c478bd9Sstevel@tonic-gate dev_info_t *hwdip; 2147*7c478bd9Sstevel@tonic-gate 2148*7c478bd9Sstevel@tonic-gate ASSERT(ndi_dev_is_persistent_node(dip) == 0); 2149*7c478bd9Sstevel@tonic-gate ASSERT(ddi_get_name_addr(dip) != NULL); 2150*7c478bd9Sstevel@tonic-gate 2151*7c478bd9Sstevel@tonic-gate hwdip = find_child_by_callback(ddi_get_parent(dip), 2152*7c478bd9Sstevel@tonic-gate ddi_binding_name(dip), ddi_get_name_addr(dip), name_node); 2153*7c478bd9Sstevel@tonic-gate 2154*7c478bd9Sstevel@tonic-gate /* 2155*7c478bd9Sstevel@tonic-gate * Look for the hardware node that is the target of the merge; 2156*7c478bd9Sstevel@tonic-gate * return failure if not found. 2157*7c478bd9Sstevel@tonic-gate */ 2158*7c478bd9Sstevel@tonic-gate if ((hwdip == NULL) || (hwdip == dip)) { 2159*7c478bd9Sstevel@tonic-gate char *buf = kmem_alloc(MAXNAMELEN, KM_SLEEP); 2160*7c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_WARN, "No HW node to merge conf node %s", 2161*7c478bd9Sstevel@tonic-gate ddi_deviname(dip, buf))); 2162*7c478bd9Sstevel@tonic-gate kmem_free(buf, MAXNAMELEN); 2163*7c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 2164*7c478bd9Sstevel@tonic-gate } 2165*7c478bd9Sstevel@tonic-gate 2166*7c478bd9Sstevel@tonic-gate /* 2167*7c478bd9Sstevel@tonic-gate * Make sure the hardware node is uninitialized and has no property. 2168*7c478bd9Sstevel@tonic-gate * This may not be the case if new .conf files are load after some 2169*7c478bd9Sstevel@tonic-gate * hardware nodes have already been initialized and attached. 2170*7c478bd9Sstevel@tonic-gate * 2171*7c478bd9Sstevel@tonic-gate * N.B. We return success here because the node was *intended* 2172*7c478bd9Sstevel@tonic-gate * to be a merge node because there is a hw node with the name. 2173*7c478bd9Sstevel@tonic-gate */ 2174*7c478bd9Sstevel@tonic-gate mutex_enter(&DEVI(hwdip)->devi_lock); 2175*7c478bd9Sstevel@tonic-gate if (ndi_dev_is_persistent_node(hwdip) == 0) { 2176*7c478bd9Sstevel@tonic-gate char *buf; 2177*7c478bd9Sstevel@tonic-gate mutex_exit(&DEVI(hwdip)->devi_lock); 2178*7c478bd9Sstevel@tonic-gate 2179*7c478bd9Sstevel@tonic-gate buf = kmem_alloc(MAXNAMELEN, KM_SLEEP); 2180*7c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_NOTE, "Duplicate .conf node %s", 2181*7c478bd9Sstevel@tonic-gate ddi_deviname(dip, buf))); 2182*7c478bd9Sstevel@tonic-gate kmem_free(buf, MAXNAMELEN); 2183*7c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 2184*7c478bd9Sstevel@tonic-gate } 2185*7c478bd9Sstevel@tonic-gate 2186*7c478bd9Sstevel@tonic-gate /* 2187*7c478bd9Sstevel@tonic-gate * If it is possible that the hardware has already been touched 2188*7c478bd9Sstevel@tonic-gate * then don't merge. 2189*7c478bd9Sstevel@tonic-gate */ 2190*7c478bd9Sstevel@tonic-gate if (i_ddi_node_state(hwdip) >= DS_INITIALIZED || 2191*7c478bd9Sstevel@tonic-gate (DEVI(hwdip)->devi_sys_prop_ptr != NULL) || 2192*7c478bd9Sstevel@tonic-gate (DEVI(hwdip)->devi_drv_prop_ptr != NULL)) { 2193*7c478bd9Sstevel@tonic-gate char *buf; 2194*7c478bd9Sstevel@tonic-gate mutex_exit(&DEVI(hwdip)->devi_lock); 2195*7c478bd9Sstevel@tonic-gate 2196*7c478bd9Sstevel@tonic-gate buf = kmem_alloc(MAXNAMELEN, KM_SLEEP); 2197*7c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_NOTE, 2198*7c478bd9Sstevel@tonic-gate "!Cannot merge .conf node %s with hw node %p " 2199*7c478bd9Sstevel@tonic-gate "-- not in proper state", 2200*7c478bd9Sstevel@tonic-gate ddi_deviname(dip, buf), (void *)hwdip)); 2201*7c478bd9Sstevel@tonic-gate kmem_free(buf, MAXNAMELEN); 2202*7c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 2203*7c478bd9Sstevel@tonic-gate } 2204*7c478bd9Sstevel@tonic-gate 2205*7c478bd9Sstevel@tonic-gate mutex_enter(&DEVI(dip)->devi_lock); 2206*7c478bd9Sstevel@tonic-gate DEVI(hwdip)->devi_sys_prop_ptr = DEVI(dip)->devi_sys_prop_ptr; 2207*7c478bd9Sstevel@tonic-gate DEVI(hwdip)->devi_drv_prop_ptr = DEVI(dip)->devi_drv_prop_ptr; 2208*7c478bd9Sstevel@tonic-gate DEVI(dip)->devi_sys_prop_ptr = NULL; 2209*7c478bd9Sstevel@tonic-gate DEVI(dip)->devi_drv_prop_ptr = NULL; 2210*7c478bd9Sstevel@tonic-gate mutex_exit(&DEVI(dip)->devi_lock); 2211*7c478bd9Sstevel@tonic-gate mutex_exit(&DEVI(hwdip)->devi_lock); 2212*7c478bd9Sstevel@tonic-gate 2213*7c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 2214*7c478bd9Sstevel@tonic-gate } 2215*7c478bd9Sstevel@tonic-gate 2216*7c478bd9Sstevel@tonic-gate /* 2217*7c478bd9Sstevel@tonic-gate * Merge a "wildcard" .conf node. This is called by nexus drivers to 2218*7c478bd9Sstevel@tonic-gate * augment a set of hw node with properties specified in driver.conf file. 2219*7c478bd9Sstevel@tonic-gate * The parent node must be held busy. 2220*7c478bd9Sstevel@tonic-gate * 2221*7c478bd9Sstevel@tonic-gate * There is no failure mode, since the nexus may or may not have child 2222*7c478bd9Sstevel@tonic-gate * node bound the driver specified by the wildcard node. 2223*7c478bd9Sstevel@tonic-gate */ 2224*7c478bd9Sstevel@tonic-gate void 2225*7c478bd9Sstevel@tonic-gate ndi_merge_wildcard_node(dev_info_t *dip) 2226*7c478bd9Sstevel@tonic-gate { 2227*7c478bd9Sstevel@tonic-gate dev_info_t *hwdip; 2228*7c478bd9Sstevel@tonic-gate dev_info_t *pdip = ddi_get_parent(dip); 2229*7c478bd9Sstevel@tonic-gate major_t major = ddi_driver_major(dip); 2230*7c478bd9Sstevel@tonic-gate 2231*7c478bd9Sstevel@tonic-gate /* never attempt to merge a hw node */ 2232*7c478bd9Sstevel@tonic-gate ASSERT(ndi_dev_is_persistent_node(dip) == 0); 2233*7c478bd9Sstevel@tonic-gate /* must be bound to a driver major number */ 2234*7c478bd9Sstevel@tonic-gate ASSERT(major != (major_t)-1); 2235*7c478bd9Sstevel@tonic-gate 2236*7c478bd9Sstevel@tonic-gate /* 2237*7c478bd9Sstevel@tonic-gate * Walk the child list to find all nodes bound to major 2238*7c478bd9Sstevel@tonic-gate * and copy properties. 2239*7c478bd9Sstevel@tonic-gate */ 2240*7c478bd9Sstevel@tonic-gate mutex_enter(&DEVI(dip)->devi_lock); 2241*7c478bd9Sstevel@tonic-gate for (hwdip = ddi_get_child(pdip); hwdip; 2242*7c478bd9Sstevel@tonic-gate hwdip = ddi_get_next_sibling(hwdip)) { 2243*7c478bd9Sstevel@tonic-gate /* 2244*7c478bd9Sstevel@tonic-gate * Skip nodes not bound to same driver 2245*7c478bd9Sstevel@tonic-gate */ 2246*7c478bd9Sstevel@tonic-gate if (ddi_driver_major(hwdip) != major) 2247*7c478bd9Sstevel@tonic-gate continue; 2248*7c478bd9Sstevel@tonic-gate 2249*7c478bd9Sstevel@tonic-gate /* 2250*7c478bd9Sstevel@tonic-gate * Skip .conf nodes 2251*7c478bd9Sstevel@tonic-gate */ 2252*7c478bd9Sstevel@tonic-gate if (ndi_dev_is_persistent_node(hwdip) == 0) 2253*7c478bd9Sstevel@tonic-gate continue; 2254*7c478bd9Sstevel@tonic-gate 2255*7c478bd9Sstevel@tonic-gate /* 2256*7c478bd9Sstevel@tonic-gate * Make sure the node is uninitialized and has no property. 2257*7c478bd9Sstevel@tonic-gate */ 2258*7c478bd9Sstevel@tonic-gate mutex_enter(&DEVI(hwdip)->devi_lock); 2259*7c478bd9Sstevel@tonic-gate if (i_ddi_node_state(hwdip) >= DS_INITIALIZED || 2260*7c478bd9Sstevel@tonic-gate (DEVI(hwdip)->devi_sys_prop_ptr != NULL) || 2261*7c478bd9Sstevel@tonic-gate (DEVI(hwdip)->devi_drv_prop_ptr != NULL)) { 2262*7c478bd9Sstevel@tonic-gate mutex_exit(&DEVI(hwdip)->devi_lock); 2263*7c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_NOTE, "HW node %p state not " 2264*7c478bd9Sstevel@tonic-gate "suitable for merging wildcard conf node %s", 2265*7c478bd9Sstevel@tonic-gate (void *)hwdip, ddi_node_name(dip))); 2266*7c478bd9Sstevel@tonic-gate continue; 2267*7c478bd9Sstevel@tonic-gate } 2268*7c478bd9Sstevel@tonic-gate 2269*7c478bd9Sstevel@tonic-gate DEVI(hwdip)->devi_sys_prop_ptr = 2270*7c478bd9Sstevel@tonic-gate i_ddi_prop_list_dup(DEVI(dip)->devi_sys_prop_ptr, KM_SLEEP); 2271*7c478bd9Sstevel@tonic-gate DEVI(hwdip)->devi_drv_prop_ptr = 2272*7c478bd9Sstevel@tonic-gate i_ddi_prop_list_dup(DEVI(dip)->devi_drv_prop_ptr, KM_SLEEP); 2273*7c478bd9Sstevel@tonic-gate mutex_exit(&DEVI(hwdip)->devi_lock); 2274*7c478bd9Sstevel@tonic-gate } 2275*7c478bd9Sstevel@tonic-gate mutex_exit(&DEVI(dip)->devi_lock); 2276*7c478bd9Sstevel@tonic-gate } 2277*7c478bd9Sstevel@tonic-gate 2278*7c478bd9Sstevel@tonic-gate /* 2279*7c478bd9Sstevel@tonic-gate * Return the major number based on the compatible property. This interface 2280*7c478bd9Sstevel@tonic-gate * may be used in situations where we are trying to detect if a better driver 2281*7c478bd9Sstevel@tonic-gate * now exists for a device, so it must use the 'compatible' property. If 2282*7c478bd9Sstevel@tonic-gate * a non-NULL formp is specified and the binding was based on compatible then 2283*7c478bd9Sstevel@tonic-gate * return the pointer to the form used in *formp. 2284*7c478bd9Sstevel@tonic-gate */ 2285*7c478bd9Sstevel@tonic-gate major_t 2286*7c478bd9Sstevel@tonic-gate ddi_compatible_driver_major(dev_info_t *dip, char **formp) 2287*7c478bd9Sstevel@tonic-gate { 2288*7c478bd9Sstevel@tonic-gate struct dev_info *devi = DEVI(dip); 2289*7c478bd9Sstevel@tonic-gate void *compat; 2290*7c478bd9Sstevel@tonic-gate size_t len; 2291*7c478bd9Sstevel@tonic-gate char *p = NULL; 2292*7c478bd9Sstevel@tonic-gate major_t major = (major_t)-1; 2293*7c478bd9Sstevel@tonic-gate 2294*7c478bd9Sstevel@tonic-gate if (formp) 2295*7c478bd9Sstevel@tonic-gate *formp = NULL; 2296*7c478bd9Sstevel@tonic-gate 2297*7c478bd9Sstevel@tonic-gate /* look up compatible property */ 2298*7c478bd9Sstevel@tonic-gate (void) lookup_compatible(dip, KM_SLEEP); 2299*7c478bd9Sstevel@tonic-gate compat = (void *)(devi->devi_compat_names); 2300*7c478bd9Sstevel@tonic-gate len = devi->devi_compat_length; 2301*7c478bd9Sstevel@tonic-gate 2302*7c478bd9Sstevel@tonic-gate /* find the highest precedence compatible form with a driver binding */ 2303*7c478bd9Sstevel@tonic-gate while ((p = prom_decode_composite_string(compat, len, p)) != NULL) { 2304*7c478bd9Sstevel@tonic-gate major = ddi_name_to_major(p); 2305*7c478bd9Sstevel@tonic-gate if ((major != (major_t)-1) && 2306*7c478bd9Sstevel@tonic-gate !(devnamesp[major].dn_flags & DN_DRIVER_REMOVED)) { 2307*7c478bd9Sstevel@tonic-gate if (formp) 2308*7c478bd9Sstevel@tonic-gate *formp = p; 2309*7c478bd9Sstevel@tonic-gate return (major); 2310*7c478bd9Sstevel@tonic-gate } 2311*7c478bd9Sstevel@tonic-gate } 2312*7c478bd9Sstevel@tonic-gate 2313*7c478bd9Sstevel@tonic-gate /* 2314*7c478bd9Sstevel@tonic-gate * none of the compatible forms have a driver binding, see if 2315*7c478bd9Sstevel@tonic-gate * the node name has a driver binding. 2316*7c478bd9Sstevel@tonic-gate */ 2317*7c478bd9Sstevel@tonic-gate major = ddi_name_to_major(ddi_node_name(dip)); 2318*7c478bd9Sstevel@tonic-gate if ((major != (major_t)-1) && 2319*7c478bd9Sstevel@tonic-gate !(devnamesp[major].dn_flags & DN_DRIVER_REMOVED)) 2320*7c478bd9Sstevel@tonic-gate return (major); 2321*7c478bd9Sstevel@tonic-gate 2322*7c478bd9Sstevel@tonic-gate /* no driver */ 2323*7c478bd9Sstevel@tonic-gate return ((major_t)-1); 2324*7c478bd9Sstevel@tonic-gate } 2325*7c478bd9Sstevel@tonic-gate 2326*7c478bd9Sstevel@tonic-gate /* 2327*7c478bd9Sstevel@tonic-gate * Static help functions 2328*7c478bd9Sstevel@tonic-gate */ 2329*7c478bd9Sstevel@tonic-gate 2330*7c478bd9Sstevel@tonic-gate /* 2331*7c478bd9Sstevel@tonic-gate * lookup the "compatible" property and cache it's contents in the 2332*7c478bd9Sstevel@tonic-gate * device node. 2333*7c478bd9Sstevel@tonic-gate */ 2334*7c478bd9Sstevel@tonic-gate static int 2335*7c478bd9Sstevel@tonic-gate lookup_compatible(dev_info_t *dip, uint_t flag) 2336*7c478bd9Sstevel@tonic-gate { 2337*7c478bd9Sstevel@tonic-gate int rv; 2338*7c478bd9Sstevel@tonic-gate int prop_flags; 2339*7c478bd9Sstevel@tonic-gate uint_t ncompatstrs; 2340*7c478bd9Sstevel@tonic-gate char **compatstrpp; 2341*7c478bd9Sstevel@tonic-gate char *di_compat_strp; 2342*7c478bd9Sstevel@tonic-gate size_t di_compat_strlen; 2343*7c478bd9Sstevel@tonic-gate 2344*7c478bd9Sstevel@tonic-gate if (DEVI(dip)->devi_compat_names) { 2345*7c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 2346*7c478bd9Sstevel@tonic-gate } 2347*7c478bd9Sstevel@tonic-gate 2348*7c478bd9Sstevel@tonic-gate prop_flags = DDI_PROP_TYPE_STRING | DDI_PROP_DONTPASS; 2349*7c478bd9Sstevel@tonic-gate 2350*7c478bd9Sstevel@tonic-gate if (flag & KM_NOSLEEP) { 2351*7c478bd9Sstevel@tonic-gate prop_flags |= DDI_PROP_DONTSLEEP; 2352*7c478bd9Sstevel@tonic-gate } 2353*7c478bd9Sstevel@tonic-gate 2354*7c478bd9Sstevel@tonic-gate if (ndi_dev_is_prom_node(dip) == 0) { 2355*7c478bd9Sstevel@tonic-gate prop_flags |= DDI_PROP_NOTPROM; 2356*7c478bd9Sstevel@tonic-gate } 2357*7c478bd9Sstevel@tonic-gate 2358*7c478bd9Sstevel@tonic-gate rv = ddi_prop_lookup_common(DDI_DEV_T_ANY, dip, prop_flags, 2359*7c478bd9Sstevel@tonic-gate "compatible", &compatstrpp, &ncompatstrs, 2360*7c478bd9Sstevel@tonic-gate ddi_prop_fm_decode_strings); 2361*7c478bd9Sstevel@tonic-gate 2362*7c478bd9Sstevel@tonic-gate if (rv == DDI_PROP_NOT_FOUND) { 2363*7c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 2364*7c478bd9Sstevel@tonic-gate } 2365*7c478bd9Sstevel@tonic-gate 2366*7c478bd9Sstevel@tonic-gate if (rv != DDI_PROP_SUCCESS) { 2367*7c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 2368*7c478bd9Sstevel@tonic-gate } 2369*7c478bd9Sstevel@tonic-gate 2370*7c478bd9Sstevel@tonic-gate /* 2371*7c478bd9Sstevel@tonic-gate * encode the compatible property data in the dev_info node 2372*7c478bd9Sstevel@tonic-gate */ 2373*7c478bd9Sstevel@tonic-gate rv = DDI_SUCCESS; 2374*7c478bd9Sstevel@tonic-gate if (ncompatstrs != 0) { 2375*7c478bd9Sstevel@tonic-gate di_compat_strp = encode_composite_string(compatstrpp, 2376*7c478bd9Sstevel@tonic-gate ncompatstrs, &di_compat_strlen, flag); 2377*7c478bd9Sstevel@tonic-gate if (di_compat_strp != NULL) { 2378*7c478bd9Sstevel@tonic-gate DEVI(dip)->devi_compat_names = di_compat_strp; 2379*7c478bd9Sstevel@tonic-gate DEVI(dip)->devi_compat_length = di_compat_strlen; 2380*7c478bd9Sstevel@tonic-gate } else { 2381*7c478bd9Sstevel@tonic-gate rv = DDI_FAILURE; 2382*7c478bd9Sstevel@tonic-gate } 2383*7c478bd9Sstevel@tonic-gate } 2384*7c478bd9Sstevel@tonic-gate ddi_prop_free(compatstrpp); 2385*7c478bd9Sstevel@tonic-gate return (rv); 2386*7c478bd9Sstevel@tonic-gate } 2387*7c478bd9Sstevel@tonic-gate 2388*7c478bd9Sstevel@tonic-gate /* 2389*7c478bd9Sstevel@tonic-gate * Create a composite string from a list of strings. 2390*7c478bd9Sstevel@tonic-gate * 2391*7c478bd9Sstevel@tonic-gate * A composite string consists of a single buffer containing one 2392*7c478bd9Sstevel@tonic-gate * or more NULL terminated strings. 2393*7c478bd9Sstevel@tonic-gate */ 2394*7c478bd9Sstevel@tonic-gate static char * 2395*7c478bd9Sstevel@tonic-gate encode_composite_string(char **strings, uint_t nstrings, size_t *retsz, 2396*7c478bd9Sstevel@tonic-gate uint_t flag) 2397*7c478bd9Sstevel@tonic-gate { 2398*7c478bd9Sstevel@tonic-gate uint_t index; 2399*7c478bd9Sstevel@tonic-gate char **strpp; 2400*7c478bd9Sstevel@tonic-gate uint_t slen; 2401*7c478bd9Sstevel@tonic-gate size_t cbuf_sz = 0; 2402*7c478bd9Sstevel@tonic-gate char *cbuf_p; 2403*7c478bd9Sstevel@tonic-gate char *cbuf_ip; 2404*7c478bd9Sstevel@tonic-gate 2405*7c478bd9Sstevel@tonic-gate if (strings == NULL || nstrings == 0 || retsz == NULL) { 2406*7c478bd9Sstevel@tonic-gate return (NULL); 2407*7c478bd9Sstevel@tonic-gate } 2408*7c478bd9Sstevel@tonic-gate 2409*7c478bd9Sstevel@tonic-gate for (index = 0, strpp = strings; index < nstrings; index++) 2410*7c478bd9Sstevel@tonic-gate cbuf_sz += strlen(*(strpp++)) + 1; 2411*7c478bd9Sstevel@tonic-gate 2412*7c478bd9Sstevel@tonic-gate if ((cbuf_p = kmem_alloc(cbuf_sz, flag)) == NULL) { 2413*7c478bd9Sstevel@tonic-gate cmn_err(CE_NOTE, 2414*7c478bd9Sstevel@tonic-gate "?failed to allocate device node compatstr"); 2415*7c478bd9Sstevel@tonic-gate return (NULL); 2416*7c478bd9Sstevel@tonic-gate } 2417*7c478bd9Sstevel@tonic-gate 2418*7c478bd9Sstevel@tonic-gate cbuf_ip = cbuf_p; 2419*7c478bd9Sstevel@tonic-gate for (index = 0, strpp = strings; index < nstrings; index++) { 2420*7c478bd9Sstevel@tonic-gate slen = strlen(*strpp); 2421*7c478bd9Sstevel@tonic-gate bcopy(*(strpp++), cbuf_ip, slen); 2422*7c478bd9Sstevel@tonic-gate cbuf_ip += slen; 2423*7c478bd9Sstevel@tonic-gate *(cbuf_ip++) = '\0'; 2424*7c478bd9Sstevel@tonic-gate } 2425*7c478bd9Sstevel@tonic-gate 2426*7c478bd9Sstevel@tonic-gate *retsz = cbuf_sz; 2427*7c478bd9Sstevel@tonic-gate return (cbuf_p); 2428*7c478bd9Sstevel@tonic-gate } 2429*7c478bd9Sstevel@tonic-gate 2430*7c478bd9Sstevel@tonic-gate static void 2431*7c478bd9Sstevel@tonic-gate link_to_driver_list(dev_info_t *dip) 2432*7c478bd9Sstevel@tonic-gate { 2433*7c478bd9Sstevel@tonic-gate major_t major = DEVI(dip)->devi_major; 2434*7c478bd9Sstevel@tonic-gate struct devnames *dnp; 2435*7c478bd9Sstevel@tonic-gate 2436*7c478bd9Sstevel@tonic-gate ASSERT(major != (major_t)-1); 2437*7c478bd9Sstevel@tonic-gate 2438*7c478bd9Sstevel@tonic-gate /* 2439*7c478bd9Sstevel@tonic-gate * Remove from orphan list 2440*7c478bd9Sstevel@tonic-gate */ 2441*7c478bd9Sstevel@tonic-gate if (ndi_dev_is_persistent_node(dip)) { 2442*7c478bd9Sstevel@tonic-gate dnp = &orphanlist; 2443*7c478bd9Sstevel@tonic-gate remove_from_dn_list(dnp, dip); 2444*7c478bd9Sstevel@tonic-gate } 2445*7c478bd9Sstevel@tonic-gate 2446*7c478bd9Sstevel@tonic-gate /* 2447*7c478bd9Sstevel@tonic-gate * Add to per driver list 2448*7c478bd9Sstevel@tonic-gate */ 2449*7c478bd9Sstevel@tonic-gate dnp = &devnamesp[major]; 2450*7c478bd9Sstevel@tonic-gate add_to_dn_list(dnp, dip); 2451*7c478bd9Sstevel@tonic-gate } 2452*7c478bd9Sstevel@tonic-gate 2453*7c478bd9Sstevel@tonic-gate static void 2454*7c478bd9Sstevel@tonic-gate unlink_from_driver_list(dev_info_t *dip) 2455*7c478bd9Sstevel@tonic-gate { 2456*7c478bd9Sstevel@tonic-gate major_t major = DEVI(dip)->devi_major; 2457*7c478bd9Sstevel@tonic-gate struct devnames *dnp; 2458*7c478bd9Sstevel@tonic-gate 2459*7c478bd9Sstevel@tonic-gate ASSERT(major != (major_t)-1); 2460*7c478bd9Sstevel@tonic-gate 2461*7c478bd9Sstevel@tonic-gate /* 2462*7c478bd9Sstevel@tonic-gate * Remove from per-driver list 2463*7c478bd9Sstevel@tonic-gate */ 2464*7c478bd9Sstevel@tonic-gate dnp = &devnamesp[major]; 2465*7c478bd9Sstevel@tonic-gate remove_from_dn_list(dnp, dip); 2466*7c478bd9Sstevel@tonic-gate 2467*7c478bd9Sstevel@tonic-gate /* 2468*7c478bd9Sstevel@tonic-gate * Add to orphan list 2469*7c478bd9Sstevel@tonic-gate */ 2470*7c478bd9Sstevel@tonic-gate if (ndi_dev_is_persistent_node(dip)) { 2471*7c478bd9Sstevel@tonic-gate dnp = &orphanlist; 2472*7c478bd9Sstevel@tonic-gate add_to_dn_list(dnp, dip); 2473*7c478bd9Sstevel@tonic-gate } 2474*7c478bd9Sstevel@tonic-gate } 2475*7c478bd9Sstevel@tonic-gate 2476*7c478bd9Sstevel@tonic-gate /* 2477*7c478bd9Sstevel@tonic-gate * scan the per-driver list looking for dev_info "dip" 2478*7c478bd9Sstevel@tonic-gate */ 2479*7c478bd9Sstevel@tonic-gate static dev_info_t * 2480*7c478bd9Sstevel@tonic-gate in_dn_list(struct devnames *dnp, dev_info_t *dip) 2481*7c478bd9Sstevel@tonic-gate { 2482*7c478bd9Sstevel@tonic-gate struct dev_info *idevi; 2483*7c478bd9Sstevel@tonic-gate 2484*7c478bd9Sstevel@tonic-gate if ((idevi = DEVI(dnp->dn_head)) == NULL) 2485*7c478bd9Sstevel@tonic-gate return (NULL); 2486*7c478bd9Sstevel@tonic-gate 2487*7c478bd9Sstevel@tonic-gate while (idevi) { 2488*7c478bd9Sstevel@tonic-gate if (idevi == DEVI(dip)) 2489*7c478bd9Sstevel@tonic-gate return (dip); 2490*7c478bd9Sstevel@tonic-gate idevi = idevi->devi_next; 2491*7c478bd9Sstevel@tonic-gate } 2492*7c478bd9Sstevel@tonic-gate return (NULL); 2493*7c478bd9Sstevel@tonic-gate } 2494*7c478bd9Sstevel@tonic-gate 2495*7c478bd9Sstevel@tonic-gate /* 2496*7c478bd9Sstevel@tonic-gate * insert devinfo node 'dip' into the per-driver instance list 2497*7c478bd9Sstevel@tonic-gate * headed by 'dnp' 2498*7c478bd9Sstevel@tonic-gate * 2499*7c478bd9Sstevel@tonic-gate * Nodes on the per-driver list are ordered: HW - SID - PSEUDO. The order is 2500*7c478bd9Sstevel@tonic-gate * required for merging of .conf file data to work properly. 2501*7c478bd9Sstevel@tonic-gate */ 2502*7c478bd9Sstevel@tonic-gate static void 2503*7c478bd9Sstevel@tonic-gate add_to_ordered_dn_list(struct devnames *dnp, dev_info_t *dip) 2504*7c478bd9Sstevel@tonic-gate { 2505*7c478bd9Sstevel@tonic-gate dev_info_t **dipp; 2506*7c478bd9Sstevel@tonic-gate 2507*7c478bd9Sstevel@tonic-gate ASSERT(mutex_owned(&(dnp->dn_lock))); 2508*7c478bd9Sstevel@tonic-gate 2509*7c478bd9Sstevel@tonic-gate dipp = &dnp->dn_head; 2510*7c478bd9Sstevel@tonic-gate if (ndi_dev_is_prom_node(dip)) { 2511*7c478bd9Sstevel@tonic-gate /* 2512*7c478bd9Sstevel@tonic-gate * Find the first non-prom node or end of list 2513*7c478bd9Sstevel@tonic-gate */ 2514*7c478bd9Sstevel@tonic-gate while (*dipp && (ndi_dev_is_prom_node(*dipp) != 0)) { 2515*7c478bd9Sstevel@tonic-gate dipp = (dev_info_t **)&DEVI(*dipp)->devi_next; 2516*7c478bd9Sstevel@tonic-gate } 2517*7c478bd9Sstevel@tonic-gate } else if (ndi_dev_is_persistent_node(dip)) { 2518*7c478bd9Sstevel@tonic-gate /* 2519*7c478bd9Sstevel@tonic-gate * Find the first non-persistent node 2520*7c478bd9Sstevel@tonic-gate */ 2521*7c478bd9Sstevel@tonic-gate while (*dipp && (ndi_dev_is_persistent_node(*dipp) != 0)) { 2522*7c478bd9Sstevel@tonic-gate dipp = (dev_info_t **)&DEVI(*dipp)->devi_next; 2523*7c478bd9Sstevel@tonic-gate } 2524*7c478bd9Sstevel@tonic-gate } else { 2525*7c478bd9Sstevel@tonic-gate /* 2526*7c478bd9Sstevel@tonic-gate * Find the end of the list 2527*7c478bd9Sstevel@tonic-gate */ 2528*7c478bd9Sstevel@tonic-gate while (*dipp) { 2529*7c478bd9Sstevel@tonic-gate dipp = (dev_info_t **)&DEVI(*dipp)->devi_next; 2530*7c478bd9Sstevel@tonic-gate } 2531*7c478bd9Sstevel@tonic-gate } 2532*7c478bd9Sstevel@tonic-gate 2533*7c478bd9Sstevel@tonic-gate DEVI(dip)->devi_next = DEVI(*dipp); 2534*7c478bd9Sstevel@tonic-gate *dipp = dip; 2535*7c478bd9Sstevel@tonic-gate } 2536*7c478bd9Sstevel@tonic-gate 2537*7c478bd9Sstevel@tonic-gate /* 2538*7c478bd9Sstevel@tonic-gate * add a list of device nodes to the device node list in the 2539*7c478bd9Sstevel@tonic-gate * devnames structure 2540*7c478bd9Sstevel@tonic-gate */ 2541*7c478bd9Sstevel@tonic-gate static void 2542*7c478bd9Sstevel@tonic-gate add_to_dn_list(struct devnames *dnp, dev_info_t *dip) 2543*7c478bd9Sstevel@tonic-gate { 2544*7c478bd9Sstevel@tonic-gate /* 2545*7c478bd9Sstevel@tonic-gate * Look to see if node already exists 2546*7c478bd9Sstevel@tonic-gate */ 2547*7c478bd9Sstevel@tonic-gate LOCK_DEV_OPS(&(dnp->dn_lock)); 2548*7c478bd9Sstevel@tonic-gate if (in_dn_list(dnp, dip)) { 2549*7c478bd9Sstevel@tonic-gate cmn_err(CE_NOTE, "add_to_dn_list: node %s already in list", 2550*7c478bd9Sstevel@tonic-gate DEVI(dip)->devi_node_name); 2551*7c478bd9Sstevel@tonic-gate } else { 2552*7c478bd9Sstevel@tonic-gate add_to_ordered_dn_list(dnp, dip); 2553*7c478bd9Sstevel@tonic-gate } 2554*7c478bd9Sstevel@tonic-gate UNLOCK_DEV_OPS(&(dnp->dn_lock)); 2555*7c478bd9Sstevel@tonic-gate } 2556*7c478bd9Sstevel@tonic-gate 2557*7c478bd9Sstevel@tonic-gate static void 2558*7c478bd9Sstevel@tonic-gate remove_from_dn_list(struct devnames *dnp, dev_info_t *dip) 2559*7c478bd9Sstevel@tonic-gate { 2560*7c478bd9Sstevel@tonic-gate dev_info_t **plist; 2561*7c478bd9Sstevel@tonic-gate 2562*7c478bd9Sstevel@tonic-gate LOCK_DEV_OPS(&(dnp->dn_lock)); 2563*7c478bd9Sstevel@tonic-gate 2564*7c478bd9Sstevel@tonic-gate plist = (dev_info_t **)&dnp->dn_head; 2565*7c478bd9Sstevel@tonic-gate while (*plist && (*plist != dip)) { 2566*7c478bd9Sstevel@tonic-gate plist = (dev_info_t **)&DEVI(*plist)->devi_next; 2567*7c478bd9Sstevel@tonic-gate } 2568*7c478bd9Sstevel@tonic-gate 2569*7c478bd9Sstevel@tonic-gate if (*plist != NULL) { 2570*7c478bd9Sstevel@tonic-gate ASSERT(*plist == dip); 2571*7c478bd9Sstevel@tonic-gate *plist = (dev_info_t *)(DEVI(dip)->devi_next); 2572*7c478bd9Sstevel@tonic-gate DEVI(dip)->devi_next = NULL; 2573*7c478bd9Sstevel@tonic-gate } else { 2574*7c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_NOTE, 2575*7c478bd9Sstevel@tonic-gate "remove_from_dn_list: node %s not found in list", 2576*7c478bd9Sstevel@tonic-gate DEVI(dip)->devi_node_name)); 2577*7c478bd9Sstevel@tonic-gate } 2578*7c478bd9Sstevel@tonic-gate 2579*7c478bd9Sstevel@tonic-gate UNLOCK_DEV_OPS(&(dnp->dn_lock)); 2580*7c478bd9Sstevel@tonic-gate } 2581*7c478bd9Sstevel@tonic-gate 2582*7c478bd9Sstevel@tonic-gate /* 2583*7c478bd9Sstevel@tonic-gate * Add and remove reference driver global property list 2584*7c478bd9Sstevel@tonic-gate */ 2585*7c478bd9Sstevel@tonic-gate static void 2586*7c478bd9Sstevel@tonic-gate add_global_props(dev_info_t *dip) 2587*7c478bd9Sstevel@tonic-gate { 2588*7c478bd9Sstevel@tonic-gate struct devnames *dnp; 2589*7c478bd9Sstevel@tonic-gate ddi_prop_list_t *plist; 2590*7c478bd9Sstevel@tonic-gate 2591*7c478bd9Sstevel@tonic-gate ASSERT(DEVI(dip)->devi_global_prop_list == NULL); 2592*7c478bd9Sstevel@tonic-gate ASSERT(DEVI(dip)->devi_major != (major_t)-1); 2593*7c478bd9Sstevel@tonic-gate 2594*7c478bd9Sstevel@tonic-gate dnp = &devnamesp[DEVI(dip)->devi_major]; 2595*7c478bd9Sstevel@tonic-gate LOCK_DEV_OPS(&dnp->dn_lock); 2596*7c478bd9Sstevel@tonic-gate plist = dnp->dn_global_prop_ptr; 2597*7c478bd9Sstevel@tonic-gate if (plist == NULL) { 2598*7c478bd9Sstevel@tonic-gate UNLOCK_DEV_OPS(&dnp->dn_lock); 2599*7c478bd9Sstevel@tonic-gate return; 2600*7c478bd9Sstevel@tonic-gate } 2601*7c478bd9Sstevel@tonic-gate i_ddi_prop_list_hold(plist, dnp); 2602*7c478bd9Sstevel@tonic-gate UNLOCK_DEV_OPS(&dnp->dn_lock); 2603*7c478bd9Sstevel@tonic-gate 2604*7c478bd9Sstevel@tonic-gate mutex_enter(&DEVI(dip)->devi_lock); 2605*7c478bd9Sstevel@tonic-gate DEVI(dip)->devi_global_prop_list = plist; 2606*7c478bd9Sstevel@tonic-gate mutex_exit(&DEVI(dip)->devi_lock); 2607*7c478bd9Sstevel@tonic-gate } 2608*7c478bd9Sstevel@tonic-gate 2609*7c478bd9Sstevel@tonic-gate static void 2610*7c478bd9Sstevel@tonic-gate remove_global_props(dev_info_t *dip) 2611*7c478bd9Sstevel@tonic-gate { 2612*7c478bd9Sstevel@tonic-gate ddi_prop_list_t *proplist; 2613*7c478bd9Sstevel@tonic-gate 2614*7c478bd9Sstevel@tonic-gate mutex_enter(&DEVI(dip)->devi_lock); 2615*7c478bd9Sstevel@tonic-gate proplist = DEVI(dip)->devi_global_prop_list; 2616*7c478bd9Sstevel@tonic-gate DEVI(dip)->devi_global_prop_list = NULL; 2617*7c478bd9Sstevel@tonic-gate mutex_exit(&DEVI(dip)->devi_lock); 2618*7c478bd9Sstevel@tonic-gate 2619*7c478bd9Sstevel@tonic-gate if (proplist) { 2620*7c478bd9Sstevel@tonic-gate major_t major; 2621*7c478bd9Sstevel@tonic-gate struct devnames *dnp; 2622*7c478bd9Sstevel@tonic-gate 2623*7c478bd9Sstevel@tonic-gate major = ddi_driver_major(dip); 2624*7c478bd9Sstevel@tonic-gate ASSERT(major != (major_t)-1); 2625*7c478bd9Sstevel@tonic-gate dnp = &devnamesp[major]; 2626*7c478bd9Sstevel@tonic-gate LOCK_DEV_OPS(&dnp->dn_lock); 2627*7c478bd9Sstevel@tonic-gate i_ddi_prop_list_rele(proplist, dnp); 2628*7c478bd9Sstevel@tonic-gate UNLOCK_DEV_OPS(&dnp->dn_lock); 2629*7c478bd9Sstevel@tonic-gate } 2630*7c478bd9Sstevel@tonic-gate } 2631*7c478bd9Sstevel@tonic-gate 2632*7c478bd9Sstevel@tonic-gate #ifdef DEBUG 2633*7c478bd9Sstevel@tonic-gate /* 2634*7c478bd9Sstevel@tonic-gate * Set this variable to '0' to disable the optimization, 2635*7c478bd9Sstevel@tonic-gate * and to 2 to print debug message. 2636*7c478bd9Sstevel@tonic-gate */ 2637*7c478bd9Sstevel@tonic-gate static int optimize_dtree = 1; 2638*7c478bd9Sstevel@tonic-gate 2639*7c478bd9Sstevel@tonic-gate static void 2640*7c478bd9Sstevel@tonic-gate debug_dtree(dev_info_t *devi, struct dev_info *adevi, char *service) 2641*7c478bd9Sstevel@tonic-gate { 2642*7c478bd9Sstevel@tonic-gate char *adeviname, *buf; 2643*7c478bd9Sstevel@tonic-gate 2644*7c478bd9Sstevel@tonic-gate /* 2645*7c478bd9Sstevel@tonic-gate * Don't print unless optimize dtree is set to 2+ 2646*7c478bd9Sstevel@tonic-gate */ 2647*7c478bd9Sstevel@tonic-gate if (optimize_dtree <= 1) 2648*7c478bd9Sstevel@tonic-gate return; 2649*7c478bd9Sstevel@tonic-gate 2650*7c478bd9Sstevel@tonic-gate buf = kmem_alloc(MAXNAMELEN, KM_SLEEP); 2651*7c478bd9Sstevel@tonic-gate adeviname = ddi_deviname((dev_info_t *)adevi, buf); 2652*7c478bd9Sstevel@tonic-gate if (*adeviname == '\0') 2653*7c478bd9Sstevel@tonic-gate adeviname = "root"; 2654*7c478bd9Sstevel@tonic-gate 2655*7c478bd9Sstevel@tonic-gate cmn_err(CE_CONT, "%s %s -> %s\n", 2656*7c478bd9Sstevel@tonic-gate ddi_deviname(devi, buf), service, adeviname); 2657*7c478bd9Sstevel@tonic-gate 2658*7c478bd9Sstevel@tonic-gate kmem_free(buf, MAXNAMELEN); 2659*7c478bd9Sstevel@tonic-gate } 2660*7c478bd9Sstevel@tonic-gate #else /* DEBUG */ 2661*7c478bd9Sstevel@tonic-gate #define debug_dtree(a1, a2, a3) /* nothing */ 2662*7c478bd9Sstevel@tonic-gate #endif /* DEBUG */ 2663*7c478bd9Sstevel@tonic-gate 2664*7c478bd9Sstevel@tonic-gate static void 2665*7c478bd9Sstevel@tonic-gate ddi_optimize_dtree(dev_info_t *devi) 2666*7c478bd9Sstevel@tonic-gate { 2667*7c478bd9Sstevel@tonic-gate struct dev_info *pdevi; 2668*7c478bd9Sstevel@tonic-gate struct bus_ops *b; 2669*7c478bd9Sstevel@tonic-gate 2670*7c478bd9Sstevel@tonic-gate pdevi = DEVI(devi)->devi_parent; 2671*7c478bd9Sstevel@tonic-gate ASSERT(pdevi); 2672*7c478bd9Sstevel@tonic-gate 2673*7c478bd9Sstevel@tonic-gate /* 2674*7c478bd9Sstevel@tonic-gate * Set the unoptimized values 2675*7c478bd9Sstevel@tonic-gate */ 2676*7c478bd9Sstevel@tonic-gate DEVI(devi)->devi_bus_map_fault = pdevi; 2677*7c478bd9Sstevel@tonic-gate DEVI(devi)->devi_bus_dma_map = pdevi; 2678*7c478bd9Sstevel@tonic-gate DEVI(devi)->devi_bus_dma_allochdl = pdevi; 2679*7c478bd9Sstevel@tonic-gate DEVI(devi)->devi_bus_dma_freehdl = pdevi; 2680*7c478bd9Sstevel@tonic-gate DEVI(devi)->devi_bus_dma_bindhdl = pdevi; 2681*7c478bd9Sstevel@tonic-gate DEVI(devi)->devi_bus_dma_bindfunc = 2682*7c478bd9Sstevel@tonic-gate pdevi->devi_ops->devo_bus_ops->bus_dma_bindhdl; 2683*7c478bd9Sstevel@tonic-gate DEVI(devi)->devi_bus_dma_unbindhdl = pdevi; 2684*7c478bd9Sstevel@tonic-gate DEVI(devi)->devi_bus_dma_unbindfunc = 2685*7c478bd9Sstevel@tonic-gate pdevi->devi_ops->devo_bus_ops->bus_dma_unbindhdl; 2686*7c478bd9Sstevel@tonic-gate DEVI(devi)->devi_bus_dma_flush = pdevi; 2687*7c478bd9Sstevel@tonic-gate DEVI(devi)->devi_bus_dma_win = pdevi; 2688*7c478bd9Sstevel@tonic-gate DEVI(devi)->devi_bus_dma_ctl = pdevi; 2689*7c478bd9Sstevel@tonic-gate DEVI(devi)->devi_bus_ctl = pdevi; 2690*7c478bd9Sstevel@tonic-gate 2691*7c478bd9Sstevel@tonic-gate #ifdef DEBUG 2692*7c478bd9Sstevel@tonic-gate if (optimize_dtree == 0) 2693*7c478bd9Sstevel@tonic-gate return; 2694*7c478bd9Sstevel@tonic-gate #endif /* DEBUG */ 2695*7c478bd9Sstevel@tonic-gate 2696*7c478bd9Sstevel@tonic-gate b = pdevi->devi_ops->devo_bus_ops; 2697*7c478bd9Sstevel@tonic-gate 2698*7c478bd9Sstevel@tonic-gate if (i_ddi_map_fault == b->bus_map_fault) { 2699*7c478bd9Sstevel@tonic-gate DEVI(devi)->devi_bus_map_fault = pdevi->devi_bus_map_fault; 2700*7c478bd9Sstevel@tonic-gate debug_dtree(devi, DEVI(devi)->devi_bus_map_fault, 2701*7c478bd9Sstevel@tonic-gate "bus_map_fault"); 2702*7c478bd9Sstevel@tonic-gate } 2703*7c478bd9Sstevel@tonic-gate 2704*7c478bd9Sstevel@tonic-gate if (ddi_dma_map == b->bus_dma_map) { 2705*7c478bd9Sstevel@tonic-gate DEVI(devi)->devi_bus_dma_map = pdevi->devi_bus_dma_map; 2706*7c478bd9Sstevel@tonic-gate debug_dtree(devi, DEVI(devi)->devi_bus_dma_map, "bus_dma_map"); 2707*7c478bd9Sstevel@tonic-gate } 2708*7c478bd9Sstevel@tonic-gate 2709*7c478bd9Sstevel@tonic-gate if (ddi_dma_allochdl == b->bus_dma_allochdl) { 2710*7c478bd9Sstevel@tonic-gate DEVI(devi)->devi_bus_dma_allochdl = 2711*7c478bd9Sstevel@tonic-gate pdevi->devi_bus_dma_allochdl; 2712*7c478bd9Sstevel@tonic-gate debug_dtree(devi, DEVI(devi)->devi_bus_dma_allochdl, 2713*7c478bd9Sstevel@tonic-gate "bus_dma_allochdl"); 2714*7c478bd9Sstevel@tonic-gate } 2715*7c478bd9Sstevel@tonic-gate 2716*7c478bd9Sstevel@tonic-gate if (ddi_dma_freehdl == b->bus_dma_freehdl) { 2717*7c478bd9Sstevel@tonic-gate DEVI(devi)->devi_bus_dma_freehdl = pdevi->devi_bus_dma_freehdl; 2718*7c478bd9Sstevel@tonic-gate debug_dtree(devi, DEVI(devi)->devi_bus_dma_freehdl, 2719*7c478bd9Sstevel@tonic-gate "bus_dma_freehdl"); 2720*7c478bd9Sstevel@tonic-gate } 2721*7c478bd9Sstevel@tonic-gate 2722*7c478bd9Sstevel@tonic-gate if (ddi_dma_bindhdl == b->bus_dma_bindhdl) { 2723*7c478bd9Sstevel@tonic-gate DEVI(devi)->devi_bus_dma_bindhdl = pdevi->devi_bus_dma_bindhdl; 2724*7c478bd9Sstevel@tonic-gate DEVI(devi)->devi_bus_dma_bindfunc = 2725*7c478bd9Sstevel@tonic-gate pdevi->devi_bus_dma_bindhdl->devi_ops-> 2726*7c478bd9Sstevel@tonic-gate devo_bus_ops->bus_dma_bindhdl; 2727*7c478bd9Sstevel@tonic-gate debug_dtree(devi, DEVI(devi)->devi_bus_dma_bindhdl, 2728*7c478bd9Sstevel@tonic-gate "bus_dma_bindhdl"); 2729*7c478bd9Sstevel@tonic-gate } 2730*7c478bd9Sstevel@tonic-gate 2731*7c478bd9Sstevel@tonic-gate if (ddi_dma_unbindhdl == b->bus_dma_unbindhdl) { 2732*7c478bd9Sstevel@tonic-gate DEVI(devi)->devi_bus_dma_unbindhdl = 2733*7c478bd9Sstevel@tonic-gate pdevi->devi_bus_dma_unbindhdl; 2734*7c478bd9Sstevel@tonic-gate DEVI(devi)->devi_bus_dma_unbindfunc = 2735*7c478bd9Sstevel@tonic-gate pdevi->devi_bus_dma_unbindhdl->devi_ops-> 2736*7c478bd9Sstevel@tonic-gate devo_bus_ops->bus_dma_unbindhdl; 2737*7c478bd9Sstevel@tonic-gate debug_dtree(devi, DEVI(devi)->devi_bus_dma_unbindhdl, 2738*7c478bd9Sstevel@tonic-gate "bus_dma_unbindhdl"); 2739*7c478bd9Sstevel@tonic-gate } 2740*7c478bd9Sstevel@tonic-gate 2741*7c478bd9Sstevel@tonic-gate if (ddi_dma_flush == b->bus_dma_flush) { 2742*7c478bd9Sstevel@tonic-gate DEVI(devi)->devi_bus_dma_flush = pdevi->devi_bus_dma_flush; 2743*7c478bd9Sstevel@tonic-gate debug_dtree(devi, DEVI(devi)->devi_bus_dma_flush, 2744*7c478bd9Sstevel@tonic-gate "bus_dma_flush"); 2745*7c478bd9Sstevel@tonic-gate } 2746*7c478bd9Sstevel@tonic-gate 2747*7c478bd9Sstevel@tonic-gate if (ddi_dma_win == b->bus_dma_win) { 2748*7c478bd9Sstevel@tonic-gate DEVI(devi)->devi_bus_dma_win = pdevi->devi_bus_dma_win; 2749*7c478bd9Sstevel@tonic-gate debug_dtree(devi, DEVI(devi)->devi_bus_dma_win, 2750*7c478bd9Sstevel@tonic-gate "bus_dma_win"); 2751*7c478bd9Sstevel@tonic-gate } 2752*7c478bd9Sstevel@tonic-gate 2753*7c478bd9Sstevel@tonic-gate if (ddi_dma_mctl == b->bus_dma_ctl) { 2754*7c478bd9Sstevel@tonic-gate DEVI(devi)->devi_bus_dma_ctl = pdevi->devi_bus_dma_ctl; 2755*7c478bd9Sstevel@tonic-gate debug_dtree(devi, DEVI(devi)->devi_bus_dma_ctl, "bus_dma_ctl"); 2756*7c478bd9Sstevel@tonic-gate } 2757*7c478bd9Sstevel@tonic-gate 2758*7c478bd9Sstevel@tonic-gate if (ddi_ctlops == b->bus_ctl) { 2759*7c478bd9Sstevel@tonic-gate DEVI(devi)->devi_bus_ctl = pdevi->devi_bus_ctl; 2760*7c478bd9Sstevel@tonic-gate debug_dtree(devi, DEVI(devi)->devi_bus_ctl, "bus_ctl"); 2761*7c478bd9Sstevel@tonic-gate } 2762*7c478bd9Sstevel@tonic-gate } 2763*7c478bd9Sstevel@tonic-gate 2764*7c478bd9Sstevel@tonic-gate #define MIN_DEVINFO_LOG_SIZE max_ncpus 2765*7c478bd9Sstevel@tonic-gate #define MAX_DEVINFO_LOG_SIZE max_ncpus * 10 2766*7c478bd9Sstevel@tonic-gate 2767*7c478bd9Sstevel@tonic-gate static void 2768*7c478bd9Sstevel@tonic-gate da_log_init() 2769*7c478bd9Sstevel@tonic-gate { 2770*7c478bd9Sstevel@tonic-gate devinfo_log_header_t *dh; 2771*7c478bd9Sstevel@tonic-gate int logsize = devinfo_log_size; 2772*7c478bd9Sstevel@tonic-gate 2773*7c478bd9Sstevel@tonic-gate if (logsize == 0) 2774*7c478bd9Sstevel@tonic-gate logsize = MIN_DEVINFO_LOG_SIZE; 2775*7c478bd9Sstevel@tonic-gate else if (logsize > MAX_DEVINFO_LOG_SIZE) 2776*7c478bd9Sstevel@tonic-gate logsize = MAX_DEVINFO_LOG_SIZE; 2777*7c478bd9Sstevel@tonic-gate 2778*7c478bd9Sstevel@tonic-gate dh = kmem_alloc(logsize * PAGESIZE, KM_SLEEP); 2779*7c478bd9Sstevel@tonic-gate mutex_init(&dh->dh_lock, NULL, MUTEX_DEFAULT, NULL); 2780*7c478bd9Sstevel@tonic-gate dh->dh_max = ((logsize * PAGESIZE) - sizeof (*dh)) / 2781*7c478bd9Sstevel@tonic-gate sizeof (devinfo_audit_t) + 1; 2782*7c478bd9Sstevel@tonic-gate dh->dh_curr = -1; 2783*7c478bd9Sstevel@tonic-gate dh->dh_hits = 0; 2784*7c478bd9Sstevel@tonic-gate 2785*7c478bd9Sstevel@tonic-gate devinfo_audit_log = dh; 2786*7c478bd9Sstevel@tonic-gate } 2787*7c478bd9Sstevel@tonic-gate 2788*7c478bd9Sstevel@tonic-gate /* 2789*7c478bd9Sstevel@tonic-gate * Log the stack trace in per-devinfo audit structure and also enter 2790*7c478bd9Sstevel@tonic-gate * it into a system wide log for recording the time history. 2791*7c478bd9Sstevel@tonic-gate */ 2792*7c478bd9Sstevel@tonic-gate static void 2793*7c478bd9Sstevel@tonic-gate da_log_enter(dev_info_t *dip) 2794*7c478bd9Sstevel@tonic-gate { 2795*7c478bd9Sstevel@tonic-gate devinfo_audit_t *da_log, *da = DEVI(dip)->devi_audit; 2796*7c478bd9Sstevel@tonic-gate devinfo_log_header_t *dh = devinfo_audit_log; 2797*7c478bd9Sstevel@tonic-gate 2798*7c478bd9Sstevel@tonic-gate if (devinfo_audit_log == NULL) 2799*7c478bd9Sstevel@tonic-gate return; 2800*7c478bd9Sstevel@tonic-gate 2801*7c478bd9Sstevel@tonic-gate ASSERT(da != NULL); 2802*7c478bd9Sstevel@tonic-gate 2803*7c478bd9Sstevel@tonic-gate da->da_devinfo = dip; 2804*7c478bd9Sstevel@tonic-gate da->da_timestamp = gethrtime(); 2805*7c478bd9Sstevel@tonic-gate da->da_thread = curthread; 2806*7c478bd9Sstevel@tonic-gate da->da_node_state = DEVI(dip)->devi_node_state; 2807*7c478bd9Sstevel@tonic-gate da->da_device_state = DEVI(dip)->devi_state; 2808*7c478bd9Sstevel@tonic-gate da->da_depth = getpcstack(da->da_stack, DDI_STACK_DEPTH); 2809*7c478bd9Sstevel@tonic-gate 2810*7c478bd9Sstevel@tonic-gate /* 2811*7c478bd9Sstevel@tonic-gate * Copy into common log and note the location for tracing history 2812*7c478bd9Sstevel@tonic-gate */ 2813*7c478bd9Sstevel@tonic-gate mutex_enter(&dh->dh_lock); 2814*7c478bd9Sstevel@tonic-gate dh->dh_hits++; 2815*7c478bd9Sstevel@tonic-gate dh->dh_curr++; 2816*7c478bd9Sstevel@tonic-gate if (dh->dh_curr >= dh->dh_max) 2817*7c478bd9Sstevel@tonic-gate dh->dh_curr -= dh->dh_max; 2818*7c478bd9Sstevel@tonic-gate da_log = &dh->dh_entry[dh->dh_curr]; 2819*7c478bd9Sstevel@tonic-gate mutex_exit(&dh->dh_lock); 2820*7c478bd9Sstevel@tonic-gate 2821*7c478bd9Sstevel@tonic-gate bcopy(da, da_log, sizeof (devinfo_audit_t)); 2822*7c478bd9Sstevel@tonic-gate da->da_lastlog = da_log; 2823*7c478bd9Sstevel@tonic-gate } 2824*7c478bd9Sstevel@tonic-gate 2825*7c478bd9Sstevel@tonic-gate static void 2826*7c478bd9Sstevel@tonic-gate attach_drivers() 2827*7c478bd9Sstevel@tonic-gate { 2828*7c478bd9Sstevel@tonic-gate int i; 2829*7c478bd9Sstevel@tonic-gate for (i = 0; i < devcnt; i++) { 2830*7c478bd9Sstevel@tonic-gate struct devnames *dnp = &devnamesp[i]; 2831*7c478bd9Sstevel@tonic-gate if ((dnp->dn_flags & DN_FORCE_ATTACH) && 2832*7c478bd9Sstevel@tonic-gate (ddi_hold_installed_driver((major_t)i) != NULL)) 2833*7c478bd9Sstevel@tonic-gate ddi_rele_driver((major_t)i); 2834*7c478bd9Sstevel@tonic-gate } 2835*7c478bd9Sstevel@tonic-gate } 2836*7c478bd9Sstevel@tonic-gate 2837*7c478bd9Sstevel@tonic-gate /* 2838*7c478bd9Sstevel@tonic-gate * Launch a thread to force attach drivers. This avoids penalty on boot time. 2839*7c478bd9Sstevel@tonic-gate */ 2840*7c478bd9Sstevel@tonic-gate void 2841*7c478bd9Sstevel@tonic-gate i_ddi_forceattach_drivers() 2842*7c478bd9Sstevel@tonic-gate { 2843*7c478bd9Sstevel@tonic-gate /* 2844*7c478bd9Sstevel@tonic-gate * On i386, the USB drivers need to load and take over from the 2845*7c478bd9Sstevel@tonic-gate * SMM BIOS drivers ASAP after consconfig(), so make sure they 2846*7c478bd9Sstevel@tonic-gate * get loaded right here rather than letting the thread do it. 2847*7c478bd9Sstevel@tonic-gate * 2848*7c478bd9Sstevel@tonic-gate * The order here is important. EHCI must be loaded first, as 2849*7c478bd9Sstevel@tonic-gate * we have observed many systems on which hangs occur if the 2850*7c478bd9Sstevel@tonic-gate * {U,O}HCI companion controllers take over control from the BIOS 2851*7c478bd9Sstevel@tonic-gate * before EHCI does. These hangs are also caused by BIOSes leaving 2852*7c478bd9Sstevel@tonic-gate * interrupt-on-port-change enabled in the ehci controller, so that 2853*7c478bd9Sstevel@tonic-gate * when uhci/ohci reset themselves, it induces a port change on 2854*7c478bd9Sstevel@tonic-gate * the ehci companion controller. Since there's no interrupt handler 2855*7c478bd9Sstevel@tonic-gate * installed at the time, the moment that interrupt is unmasked, an 2856*7c478bd9Sstevel@tonic-gate * interrupt storm will occur. All this is averted when ehci is 2857*7c478bd9Sstevel@tonic-gate * loaded first. And now you know..... the REST of the story. 2858*7c478bd9Sstevel@tonic-gate * 2859*7c478bd9Sstevel@tonic-gate * Regardless of platform, ehci needs to initialize first to avoid 2860*7c478bd9Sstevel@tonic-gate * unnecessary connects and disconnects on the companion controller 2861*7c478bd9Sstevel@tonic-gate * when ehci sets up the routing. 2862*7c478bd9Sstevel@tonic-gate */ 2863*7c478bd9Sstevel@tonic-gate (void) ddi_hold_installed_driver(ddi_name_to_major("ehci")); 2864*7c478bd9Sstevel@tonic-gate (void) ddi_hold_installed_driver(ddi_name_to_major("uhci")); 2865*7c478bd9Sstevel@tonic-gate (void) ddi_hold_installed_driver(ddi_name_to_major("ohci")); 2866*7c478bd9Sstevel@tonic-gate 2867*7c478bd9Sstevel@tonic-gate (void) thread_create(NULL, 0, (void (*)())attach_drivers, NULL, 0, &p0, 2868*7c478bd9Sstevel@tonic-gate TS_RUN, minclsyspri); 2869*7c478bd9Sstevel@tonic-gate } 2870*7c478bd9Sstevel@tonic-gate 2871*7c478bd9Sstevel@tonic-gate /* 2872*7c478bd9Sstevel@tonic-gate * This is a private DDI interface for optimizing boot performance. 2873*7c478bd9Sstevel@tonic-gate * I/O subsystem initialization is considered complete when devfsadm 2874*7c478bd9Sstevel@tonic-gate * is executed. 2875*7c478bd9Sstevel@tonic-gate * 2876*7c478bd9Sstevel@tonic-gate * NOTE: The start of syseventd in S60devfsadm happen to be convenient 2877*7c478bd9Sstevel@tonic-gate * indicator for the completion of I/O initialization during boot. 2878*7c478bd9Sstevel@tonic-gate * The implementation should be replaced by something more robust. 2879*7c478bd9Sstevel@tonic-gate */ 2880*7c478bd9Sstevel@tonic-gate int 2881*7c478bd9Sstevel@tonic-gate i_ddi_io_initialized() 2882*7c478bd9Sstevel@tonic-gate { 2883*7c478bd9Sstevel@tonic-gate extern int sysevent_daemon_init; 2884*7c478bd9Sstevel@tonic-gate return (sysevent_daemon_init); 2885*7c478bd9Sstevel@tonic-gate } 2886*7c478bd9Sstevel@tonic-gate 2887*7c478bd9Sstevel@tonic-gate 2888*7c478bd9Sstevel@tonic-gate /* 2889*7c478bd9Sstevel@tonic-gate * device tree walking 2890*7c478bd9Sstevel@tonic-gate */ 2891*7c478bd9Sstevel@tonic-gate 2892*7c478bd9Sstevel@tonic-gate struct walk_elem { 2893*7c478bd9Sstevel@tonic-gate struct walk_elem *next; 2894*7c478bd9Sstevel@tonic-gate dev_info_t *dip; 2895*7c478bd9Sstevel@tonic-gate }; 2896*7c478bd9Sstevel@tonic-gate 2897*7c478bd9Sstevel@tonic-gate static void 2898*7c478bd9Sstevel@tonic-gate free_list(struct walk_elem *list) 2899*7c478bd9Sstevel@tonic-gate { 2900*7c478bd9Sstevel@tonic-gate while (list) { 2901*7c478bd9Sstevel@tonic-gate struct walk_elem *next = list->next; 2902*7c478bd9Sstevel@tonic-gate kmem_free(list, sizeof (*list)); 2903*7c478bd9Sstevel@tonic-gate list = next; 2904*7c478bd9Sstevel@tonic-gate } 2905*7c478bd9Sstevel@tonic-gate } 2906*7c478bd9Sstevel@tonic-gate 2907*7c478bd9Sstevel@tonic-gate static void 2908*7c478bd9Sstevel@tonic-gate append_node(struct walk_elem **list, dev_info_t *dip) 2909*7c478bd9Sstevel@tonic-gate { 2910*7c478bd9Sstevel@tonic-gate struct walk_elem *tail; 2911*7c478bd9Sstevel@tonic-gate struct walk_elem *elem = kmem_alloc(sizeof (*elem), KM_SLEEP); 2912*7c478bd9Sstevel@tonic-gate 2913*7c478bd9Sstevel@tonic-gate elem->next = NULL; 2914*7c478bd9Sstevel@tonic-gate elem->dip = dip; 2915*7c478bd9Sstevel@tonic-gate 2916*7c478bd9Sstevel@tonic-gate if (*list == NULL) { 2917*7c478bd9Sstevel@tonic-gate *list = elem; 2918*7c478bd9Sstevel@tonic-gate return; 2919*7c478bd9Sstevel@tonic-gate } 2920*7c478bd9Sstevel@tonic-gate 2921*7c478bd9Sstevel@tonic-gate tail = *list; 2922*7c478bd9Sstevel@tonic-gate while (tail->next) 2923*7c478bd9Sstevel@tonic-gate tail = tail->next; 2924*7c478bd9Sstevel@tonic-gate 2925*7c478bd9Sstevel@tonic-gate tail->next = elem; 2926*7c478bd9Sstevel@tonic-gate } 2927*7c478bd9Sstevel@tonic-gate 2928*7c478bd9Sstevel@tonic-gate /* 2929*7c478bd9Sstevel@tonic-gate * The implementation of ddi_walk_devs(). 2930*7c478bd9Sstevel@tonic-gate */ 2931*7c478bd9Sstevel@tonic-gate static int 2932*7c478bd9Sstevel@tonic-gate walk_devs(dev_info_t *dip, int (*f)(dev_info_t *, void *), void *arg, 2933*7c478bd9Sstevel@tonic-gate int do_locking) 2934*7c478bd9Sstevel@tonic-gate { 2935*7c478bd9Sstevel@tonic-gate struct walk_elem *head = NULL; 2936*7c478bd9Sstevel@tonic-gate 2937*7c478bd9Sstevel@tonic-gate /* 2938*7c478bd9Sstevel@tonic-gate * Do it in two passes. First pass invoke callback on each 2939*7c478bd9Sstevel@tonic-gate * dip on the sibling list. Second pass invoke callback on 2940*7c478bd9Sstevel@tonic-gate * children of each dip. 2941*7c478bd9Sstevel@tonic-gate */ 2942*7c478bd9Sstevel@tonic-gate while (dip) { 2943*7c478bd9Sstevel@tonic-gate switch ((*f)(dip, arg)) { 2944*7c478bd9Sstevel@tonic-gate case DDI_WALK_TERMINATE: 2945*7c478bd9Sstevel@tonic-gate free_list(head); 2946*7c478bd9Sstevel@tonic-gate return (DDI_WALK_TERMINATE); 2947*7c478bd9Sstevel@tonic-gate 2948*7c478bd9Sstevel@tonic-gate case DDI_WALK_PRUNESIB: 2949*7c478bd9Sstevel@tonic-gate /* ignore sibling by setting dip to NULL */ 2950*7c478bd9Sstevel@tonic-gate append_node(&head, dip); 2951*7c478bd9Sstevel@tonic-gate dip = NULL; 2952*7c478bd9Sstevel@tonic-gate break; 2953*7c478bd9Sstevel@tonic-gate 2954*7c478bd9Sstevel@tonic-gate case DDI_WALK_PRUNECHILD: 2955*7c478bd9Sstevel@tonic-gate /* don't worry about children */ 2956*7c478bd9Sstevel@tonic-gate dip = ddi_get_next_sibling(dip); 2957*7c478bd9Sstevel@tonic-gate break; 2958*7c478bd9Sstevel@tonic-gate 2959*7c478bd9Sstevel@tonic-gate case DDI_WALK_CONTINUE: 2960*7c478bd9Sstevel@tonic-gate default: 2961*7c478bd9Sstevel@tonic-gate append_node(&head, dip); 2962*7c478bd9Sstevel@tonic-gate dip = ddi_get_next_sibling(dip); 2963*7c478bd9Sstevel@tonic-gate break; 2964*7c478bd9Sstevel@tonic-gate } 2965*7c478bd9Sstevel@tonic-gate 2966*7c478bd9Sstevel@tonic-gate } 2967*7c478bd9Sstevel@tonic-gate 2968*7c478bd9Sstevel@tonic-gate /* second pass */ 2969*7c478bd9Sstevel@tonic-gate while (head) { 2970*7c478bd9Sstevel@tonic-gate int circ; 2971*7c478bd9Sstevel@tonic-gate struct walk_elem *next = head->next; 2972*7c478bd9Sstevel@tonic-gate 2973*7c478bd9Sstevel@tonic-gate if (do_locking) 2974*7c478bd9Sstevel@tonic-gate ndi_devi_enter(head->dip, &circ); 2975*7c478bd9Sstevel@tonic-gate if (walk_devs(ddi_get_child(head->dip), f, arg, do_locking) == 2976*7c478bd9Sstevel@tonic-gate DDI_WALK_TERMINATE) { 2977*7c478bd9Sstevel@tonic-gate if (do_locking) 2978*7c478bd9Sstevel@tonic-gate ndi_devi_exit(head->dip, circ); 2979*7c478bd9Sstevel@tonic-gate free_list(head); 2980*7c478bd9Sstevel@tonic-gate return (DDI_WALK_TERMINATE); 2981*7c478bd9Sstevel@tonic-gate } 2982*7c478bd9Sstevel@tonic-gate if (do_locking) 2983*7c478bd9Sstevel@tonic-gate ndi_devi_exit(head->dip, circ); 2984*7c478bd9Sstevel@tonic-gate kmem_free(head, sizeof (*head)); 2985*7c478bd9Sstevel@tonic-gate head = next; 2986*7c478bd9Sstevel@tonic-gate } 2987*7c478bd9Sstevel@tonic-gate 2988*7c478bd9Sstevel@tonic-gate return (DDI_WALK_CONTINUE); 2989*7c478bd9Sstevel@tonic-gate } 2990*7c478bd9Sstevel@tonic-gate 2991*7c478bd9Sstevel@tonic-gate /* 2992*7c478bd9Sstevel@tonic-gate * This general-purpose routine traverses the tree of dev_info nodes, 2993*7c478bd9Sstevel@tonic-gate * starting from the given node, and calls the given function for each 2994*7c478bd9Sstevel@tonic-gate * node that it finds with the current node and the pointer arg (which 2995*7c478bd9Sstevel@tonic-gate * can point to a structure of information that the function 2996*7c478bd9Sstevel@tonic-gate * needs) as arguments. 2997*7c478bd9Sstevel@tonic-gate * 2998*7c478bd9Sstevel@tonic-gate * It does the walk a layer at a time, not depth-first. The given function 2999*7c478bd9Sstevel@tonic-gate * must return one of the following values: 3000*7c478bd9Sstevel@tonic-gate * DDI_WALK_CONTINUE 3001*7c478bd9Sstevel@tonic-gate * DDI_WALK_PRUNESIB 3002*7c478bd9Sstevel@tonic-gate * DDI_WALK_PRUNECHILD 3003*7c478bd9Sstevel@tonic-gate * DDI_WALK_TERMINATE 3004*7c478bd9Sstevel@tonic-gate * 3005*7c478bd9Sstevel@tonic-gate * N.B. Since we walk the sibling list, the caller must ensure that 3006*7c478bd9Sstevel@tonic-gate * the parent of dip is held against changes, unless the parent 3007*7c478bd9Sstevel@tonic-gate * is rootnode. ndi_devi_enter() on the parent is sufficient. 3008*7c478bd9Sstevel@tonic-gate * 3009*7c478bd9Sstevel@tonic-gate * To avoid deadlock situations, caller must not attempt to 3010*7c478bd9Sstevel@tonic-gate * configure/unconfigure/remove device node in (*f)(), nor should 3011*7c478bd9Sstevel@tonic-gate * it attempt to recurse on other nodes in the system. 3012*7c478bd9Sstevel@tonic-gate * 3013*7c478bd9Sstevel@tonic-gate * This is not callable from device autoconfiguration routines. 3014*7c478bd9Sstevel@tonic-gate * They include, but not limited to, _init(9e), _fini(9e), probe(9e), 3015*7c478bd9Sstevel@tonic-gate * attach(9e), and detach(9e). 3016*7c478bd9Sstevel@tonic-gate */ 3017*7c478bd9Sstevel@tonic-gate 3018*7c478bd9Sstevel@tonic-gate void 3019*7c478bd9Sstevel@tonic-gate ddi_walk_devs(dev_info_t *dip, int (*f)(dev_info_t *, void *), void *arg) 3020*7c478bd9Sstevel@tonic-gate { 3021*7c478bd9Sstevel@tonic-gate 3022*7c478bd9Sstevel@tonic-gate ASSERT(dip == NULL || ddi_get_parent(dip) == NULL || 3023*7c478bd9Sstevel@tonic-gate DEVI_BUSY_OWNED(ddi_get_parent(dip))); 3024*7c478bd9Sstevel@tonic-gate 3025*7c478bd9Sstevel@tonic-gate (void) walk_devs(dip, f, arg, 1); 3026*7c478bd9Sstevel@tonic-gate } 3027*7c478bd9Sstevel@tonic-gate 3028*7c478bd9Sstevel@tonic-gate /* 3029*7c478bd9Sstevel@tonic-gate * This is a general-purpose routine traverses the per-driver list 3030*7c478bd9Sstevel@tonic-gate * and calls the given function for each node. must return one of 3031*7c478bd9Sstevel@tonic-gate * the following values: 3032*7c478bd9Sstevel@tonic-gate * DDI_WALK_CONTINUE 3033*7c478bd9Sstevel@tonic-gate * DDI_WALK_TERMINATE 3034*7c478bd9Sstevel@tonic-gate * 3035*7c478bd9Sstevel@tonic-gate * N.B. The same restrictions from ddi_walk_devs() apply. 3036*7c478bd9Sstevel@tonic-gate */ 3037*7c478bd9Sstevel@tonic-gate 3038*7c478bd9Sstevel@tonic-gate void 3039*7c478bd9Sstevel@tonic-gate e_ddi_walk_driver(char *drv, int (*f)(dev_info_t *, void *), void *arg) 3040*7c478bd9Sstevel@tonic-gate { 3041*7c478bd9Sstevel@tonic-gate major_t major; 3042*7c478bd9Sstevel@tonic-gate struct devnames *dnp; 3043*7c478bd9Sstevel@tonic-gate dev_info_t *dip; 3044*7c478bd9Sstevel@tonic-gate 3045*7c478bd9Sstevel@tonic-gate major = ddi_name_to_major(drv); 3046*7c478bd9Sstevel@tonic-gate if (major == (major_t)-1) 3047*7c478bd9Sstevel@tonic-gate return; 3048*7c478bd9Sstevel@tonic-gate 3049*7c478bd9Sstevel@tonic-gate dnp = &devnamesp[major]; 3050*7c478bd9Sstevel@tonic-gate LOCK_DEV_OPS(&dnp->dn_lock); 3051*7c478bd9Sstevel@tonic-gate dip = dnp->dn_head; 3052*7c478bd9Sstevel@tonic-gate while (dip) { 3053*7c478bd9Sstevel@tonic-gate ndi_hold_devi(dip); 3054*7c478bd9Sstevel@tonic-gate UNLOCK_DEV_OPS(&dnp->dn_lock); 3055*7c478bd9Sstevel@tonic-gate if ((*f)(dip, arg) == DDI_WALK_TERMINATE) { 3056*7c478bd9Sstevel@tonic-gate ndi_rele_devi(dip); 3057*7c478bd9Sstevel@tonic-gate return; 3058*7c478bd9Sstevel@tonic-gate } 3059*7c478bd9Sstevel@tonic-gate LOCK_DEV_OPS(&dnp->dn_lock); 3060*7c478bd9Sstevel@tonic-gate ndi_rele_devi(dip); 3061*7c478bd9Sstevel@tonic-gate dip = ddi_get_next(dip); 3062*7c478bd9Sstevel@tonic-gate } 3063*7c478bd9Sstevel@tonic-gate UNLOCK_DEV_OPS(&dnp->dn_lock); 3064*7c478bd9Sstevel@tonic-gate } 3065*7c478bd9Sstevel@tonic-gate 3066*7c478bd9Sstevel@tonic-gate /* 3067*7c478bd9Sstevel@tonic-gate * argument to i_find_devi, a devinfo node search callback function. 3068*7c478bd9Sstevel@tonic-gate */ 3069*7c478bd9Sstevel@tonic-gate struct match_info { 3070*7c478bd9Sstevel@tonic-gate dev_info_t *dip; /* result */ 3071*7c478bd9Sstevel@tonic-gate char *nodename; /* if non-null, nodename must match */ 3072*7c478bd9Sstevel@tonic-gate int instance; /* if != -1, instance must match */ 3073*7c478bd9Sstevel@tonic-gate int attached; /* if != 0, state >= DS_ATTACHED */ 3074*7c478bd9Sstevel@tonic-gate }; 3075*7c478bd9Sstevel@tonic-gate 3076*7c478bd9Sstevel@tonic-gate static int 3077*7c478bd9Sstevel@tonic-gate i_find_devi(dev_info_t *dip, void *arg) 3078*7c478bd9Sstevel@tonic-gate { 3079*7c478bd9Sstevel@tonic-gate struct match_info *info = (struct match_info *)arg; 3080*7c478bd9Sstevel@tonic-gate 3081*7c478bd9Sstevel@tonic-gate if (((info->nodename == NULL) || 3082*7c478bd9Sstevel@tonic-gate (strcmp(ddi_node_name(dip), info->nodename) == 0)) && 3083*7c478bd9Sstevel@tonic-gate ((info->instance == -1) || 3084*7c478bd9Sstevel@tonic-gate (ddi_get_instance(dip) == info->instance)) && 3085*7c478bd9Sstevel@tonic-gate ((info->attached == 0) || 3086*7c478bd9Sstevel@tonic-gate (i_ddi_node_state(dip) >= DS_ATTACHED))) { 3087*7c478bd9Sstevel@tonic-gate info->dip = dip; 3088*7c478bd9Sstevel@tonic-gate ndi_hold_devi(dip); 3089*7c478bd9Sstevel@tonic-gate return (DDI_WALK_TERMINATE); 3090*7c478bd9Sstevel@tonic-gate } 3091*7c478bd9Sstevel@tonic-gate 3092*7c478bd9Sstevel@tonic-gate return (DDI_WALK_CONTINUE); 3093*7c478bd9Sstevel@tonic-gate } 3094*7c478bd9Sstevel@tonic-gate 3095*7c478bd9Sstevel@tonic-gate /* 3096*7c478bd9Sstevel@tonic-gate * Find dip with a known node name and instance and return with it held 3097*7c478bd9Sstevel@tonic-gate */ 3098*7c478bd9Sstevel@tonic-gate dev_info_t * 3099*7c478bd9Sstevel@tonic-gate ddi_find_devinfo(char *nodename, int instance, int attached) 3100*7c478bd9Sstevel@tonic-gate { 3101*7c478bd9Sstevel@tonic-gate struct match_info info; 3102*7c478bd9Sstevel@tonic-gate 3103*7c478bd9Sstevel@tonic-gate info.nodename = nodename; 3104*7c478bd9Sstevel@tonic-gate info.instance = instance; 3105*7c478bd9Sstevel@tonic-gate info.attached = attached; 3106*7c478bd9Sstevel@tonic-gate info.dip = NULL; 3107*7c478bd9Sstevel@tonic-gate 3108*7c478bd9Sstevel@tonic-gate ddi_walk_devs(ddi_root_node(), i_find_devi, &info); 3109*7c478bd9Sstevel@tonic-gate return (info.dip); 3110*7c478bd9Sstevel@tonic-gate } 3111*7c478bd9Sstevel@tonic-gate 3112*7c478bd9Sstevel@tonic-gate /* 3113*7c478bd9Sstevel@tonic-gate * Parse for name, addr, and minor names. Some args may be NULL. 3114*7c478bd9Sstevel@tonic-gate */ 3115*7c478bd9Sstevel@tonic-gate void 3116*7c478bd9Sstevel@tonic-gate i_ddi_parse_name(char *name, char **nodename, char **addrname, char **minorname) 3117*7c478bd9Sstevel@tonic-gate { 3118*7c478bd9Sstevel@tonic-gate char *cp; 3119*7c478bd9Sstevel@tonic-gate static char nulladdrname[] = ""; 3120*7c478bd9Sstevel@tonic-gate 3121*7c478bd9Sstevel@tonic-gate /* default values */ 3122*7c478bd9Sstevel@tonic-gate if (nodename) 3123*7c478bd9Sstevel@tonic-gate *nodename = name; 3124*7c478bd9Sstevel@tonic-gate if (addrname) 3125*7c478bd9Sstevel@tonic-gate *addrname = nulladdrname; 3126*7c478bd9Sstevel@tonic-gate if (minorname) 3127*7c478bd9Sstevel@tonic-gate *minorname = NULL; 3128*7c478bd9Sstevel@tonic-gate 3129*7c478bd9Sstevel@tonic-gate cp = name; 3130*7c478bd9Sstevel@tonic-gate while (*cp != '\0') { 3131*7c478bd9Sstevel@tonic-gate if (addrname && *cp == '@') { 3132*7c478bd9Sstevel@tonic-gate *addrname = cp + 1; 3133*7c478bd9Sstevel@tonic-gate *cp = '\0'; 3134*7c478bd9Sstevel@tonic-gate } else if (minorname && *cp == ':') { 3135*7c478bd9Sstevel@tonic-gate *minorname = cp + 1; 3136*7c478bd9Sstevel@tonic-gate *cp = '\0'; 3137*7c478bd9Sstevel@tonic-gate } 3138*7c478bd9Sstevel@tonic-gate ++cp; 3139*7c478bd9Sstevel@tonic-gate } 3140*7c478bd9Sstevel@tonic-gate } 3141*7c478bd9Sstevel@tonic-gate 3142*7c478bd9Sstevel@tonic-gate static char * 3143*7c478bd9Sstevel@tonic-gate child_path_to_driver(dev_info_t *parent, char *child_name, char *unit_address) 3144*7c478bd9Sstevel@tonic-gate { 3145*7c478bd9Sstevel@tonic-gate char *p, *drvname = NULL; 3146*7c478bd9Sstevel@tonic-gate major_t maj; 3147*7c478bd9Sstevel@tonic-gate 3148*7c478bd9Sstevel@tonic-gate /* 3149*7c478bd9Sstevel@tonic-gate * Construct the pathname and ask the implementation 3150*7c478bd9Sstevel@tonic-gate * if it can do a driver = f(pathname) for us, if not 3151*7c478bd9Sstevel@tonic-gate * we'll just default to using the node-name that 3152*7c478bd9Sstevel@tonic-gate * was given to us. We want to do this first to 3153*7c478bd9Sstevel@tonic-gate * allow the platform to use 'generic' names for 3154*7c478bd9Sstevel@tonic-gate * legacy device drivers. 3155*7c478bd9Sstevel@tonic-gate */ 3156*7c478bd9Sstevel@tonic-gate p = kmem_zalloc(MAXPATHLEN, KM_SLEEP); 3157*7c478bd9Sstevel@tonic-gate (void) ddi_pathname(parent, p); 3158*7c478bd9Sstevel@tonic-gate (void) strcat(p, "/"); 3159*7c478bd9Sstevel@tonic-gate (void) strcat(p, child_name); 3160*7c478bd9Sstevel@tonic-gate if (unit_address && *unit_address) { 3161*7c478bd9Sstevel@tonic-gate (void) strcat(p, "@"); 3162*7c478bd9Sstevel@tonic-gate (void) strcat(p, unit_address); 3163*7c478bd9Sstevel@tonic-gate } 3164*7c478bd9Sstevel@tonic-gate 3165*7c478bd9Sstevel@tonic-gate /* 3166*7c478bd9Sstevel@tonic-gate * Get the binding. If there is none, return the child_name 3167*7c478bd9Sstevel@tonic-gate * and let the caller deal with it. 3168*7c478bd9Sstevel@tonic-gate */ 3169*7c478bd9Sstevel@tonic-gate maj = path_to_major(p); 3170*7c478bd9Sstevel@tonic-gate 3171*7c478bd9Sstevel@tonic-gate kmem_free(p, MAXPATHLEN); 3172*7c478bd9Sstevel@tonic-gate 3173*7c478bd9Sstevel@tonic-gate if (maj != (major_t)-1) 3174*7c478bd9Sstevel@tonic-gate drvname = ddi_major_to_name(maj); 3175*7c478bd9Sstevel@tonic-gate if (drvname == NULL) 3176*7c478bd9Sstevel@tonic-gate drvname = child_name; 3177*7c478bd9Sstevel@tonic-gate 3178*7c478bd9Sstevel@tonic-gate return (drvname); 3179*7c478bd9Sstevel@tonic-gate } 3180*7c478bd9Sstevel@tonic-gate 3181*7c478bd9Sstevel@tonic-gate 3182*7c478bd9Sstevel@tonic-gate /* 3183*7c478bd9Sstevel@tonic-gate * Given the pathname of a device, fill in the dev_info_t value and/or the 3184*7c478bd9Sstevel@tonic-gate * dev_t value and/or the spectype, depending on which parameters are non-NULL. 3185*7c478bd9Sstevel@tonic-gate * If there is an error, this function returns -1. 3186*7c478bd9Sstevel@tonic-gate * 3187*7c478bd9Sstevel@tonic-gate * NOTE: If this function returns the dev_info_t structure, then it 3188*7c478bd9Sstevel@tonic-gate * does so with a hold on the devi. Caller should ensure that they get 3189*7c478bd9Sstevel@tonic-gate * decremented via ddi_release_devi() or ndi_rele_devi(); 3190*7c478bd9Sstevel@tonic-gate * 3191*7c478bd9Sstevel@tonic-gate * This function can be invoked in the boot case for a pathname without 3192*7c478bd9Sstevel@tonic-gate * device argument (:xxxx), traditionally treated as a minor name. 3193*7c478bd9Sstevel@tonic-gate * In this case, we do the following 3194*7c478bd9Sstevel@tonic-gate * (1) search the minor node of type DDM_DEFAULT. 3195*7c478bd9Sstevel@tonic-gate * (2) if no DDM_DEFAULT minor exists, then the first non-alias minor is chosen. 3196*7c478bd9Sstevel@tonic-gate * (3) if neither exists, a dev_t is faked with minor number = instance. 3197*7c478bd9Sstevel@tonic-gate * As of S9 FCS, no instance of #1 exists. #2 is used by several platforms 3198*7c478bd9Sstevel@tonic-gate * to default the boot partition to :a possibly by other OBP definitions. 3199*7c478bd9Sstevel@tonic-gate * #3 is used for booting off network interfaces, most SPARC network 3200*7c478bd9Sstevel@tonic-gate * drivers support Style-2 only, so only DDM_ALIAS minor exists. 3201*7c478bd9Sstevel@tonic-gate * 3202*7c478bd9Sstevel@tonic-gate * It is possible for OBP to present device args at the end of the path as 3203*7c478bd9Sstevel@tonic-gate * well as in the middle. For example, with IB the following strings are 3204*7c478bd9Sstevel@tonic-gate * valid boot paths. 3205*7c478bd9Sstevel@tonic-gate * a /pci@8,700000/ib@1,2:port=1,pkey=ff,dhcp,... 3206*7c478bd9Sstevel@tonic-gate * b /pci@8,700000/ib@1,1:port=1/ioc@xxxxxx,yyyyyyy:dhcp 3207*7c478bd9Sstevel@tonic-gate * Case (a), we first look for minor node "port=1,pkey...". 3208*7c478bd9Sstevel@tonic-gate * Failing that, we will pass "port=1,pkey..." to the bus_config 3209*7c478bd9Sstevel@tonic-gate * entry point of ib (HCA) driver. 3210*7c478bd9Sstevel@tonic-gate * Case (b), configure ib@1,1 as usual. Then invoke ib's bus_config 3211*7c478bd9Sstevel@tonic-gate * with argument "ioc@xxxxxxx,yyyyyyy:port=1". After configuring 3212*7c478bd9Sstevel@tonic-gate * the ioc, look for minor node dhcp. If not found, pass ":dhcp" 3213*7c478bd9Sstevel@tonic-gate * to ioc's bus_config entry point. 3214*7c478bd9Sstevel@tonic-gate */ 3215*7c478bd9Sstevel@tonic-gate int 3216*7c478bd9Sstevel@tonic-gate resolve_pathname(char *pathname, 3217*7c478bd9Sstevel@tonic-gate dev_info_t **dipp, dev_t *devtp, int *spectypep) 3218*7c478bd9Sstevel@tonic-gate { 3219*7c478bd9Sstevel@tonic-gate int error; 3220*7c478bd9Sstevel@tonic-gate dev_info_t *parent, *child; 3221*7c478bd9Sstevel@tonic-gate struct pathname pn; 3222*7c478bd9Sstevel@tonic-gate char *component, *config_name; 3223*7c478bd9Sstevel@tonic-gate char *minorname = NULL; 3224*7c478bd9Sstevel@tonic-gate char *prev_minor = NULL; 3225*7c478bd9Sstevel@tonic-gate dev_t devt = NODEV; 3226*7c478bd9Sstevel@tonic-gate int spectype; 3227*7c478bd9Sstevel@tonic-gate struct ddi_minor_data *dmn; 3228*7c478bd9Sstevel@tonic-gate 3229*7c478bd9Sstevel@tonic-gate if (*pathname != '/') 3230*7c478bd9Sstevel@tonic-gate return (EINVAL); 3231*7c478bd9Sstevel@tonic-gate parent = ddi_root_node(); /* Begin at the top of the tree */ 3232*7c478bd9Sstevel@tonic-gate 3233*7c478bd9Sstevel@tonic-gate if (error = pn_get(pathname, UIO_SYSSPACE, &pn)) 3234*7c478bd9Sstevel@tonic-gate return (error); 3235*7c478bd9Sstevel@tonic-gate pn_skipslash(&pn); 3236*7c478bd9Sstevel@tonic-gate 3237*7c478bd9Sstevel@tonic-gate ASSERT(i_ddi_node_state(parent) >= DS_ATTACHED); 3238*7c478bd9Sstevel@tonic-gate ndi_hold_devi(parent); 3239*7c478bd9Sstevel@tonic-gate 3240*7c478bd9Sstevel@tonic-gate component = kmem_alloc(MAXNAMELEN, KM_SLEEP); 3241*7c478bd9Sstevel@tonic-gate config_name = kmem_alloc(MAXNAMELEN, KM_SLEEP); 3242*7c478bd9Sstevel@tonic-gate 3243*7c478bd9Sstevel@tonic-gate while (pn_pathleft(&pn)) { 3244*7c478bd9Sstevel@tonic-gate /* remember prev minor (:xxx) in the middle of path */ 3245*7c478bd9Sstevel@tonic-gate if (minorname) 3246*7c478bd9Sstevel@tonic-gate prev_minor = i_ddi_strdup(minorname, KM_SLEEP); 3247*7c478bd9Sstevel@tonic-gate 3248*7c478bd9Sstevel@tonic-gate /* Get component and chop off minorname */ 3249*7c478bd9Sstevel@tonic-gate (void) pn_getcomponent(&pn, component); 3250*7c478bd9Sstevel@tonic-gate i_ddi_parse_name(component, NULL, NULL, &minorname); 3251*7c478bd9Sstevel@tonic-gate 3252*7c478bd9Sstevel@tonic-gate if (prev_minor == NULL) { 3253*7c478bd9Sstevel@tonic-gate (void) snprintf(config_name, MAXNAMELEN, "%s", 3254*7c478bd9Sstevel@tonic-gate component); 3255*7c478bd9Sstevel@tonic-gate } else { 3256*7c478bd9Sstevel@tonic-gate (void) snprintf(config_name, MAXNAMELEN, "%s:%s", 3257*7c478bd9Sstevel@tonic-gate component, prev_minor); 3258*7c478bd9Sstevel@tonic-gate kmem_free(prev_minor, strlen(prev_minor) + 1); 3259*7c478bd9Sstevel@tonic-gate prev_minor = NULL; 3260*7c478bd9Sstevel@tonic-gate } 3261*7c478bd9Sstevel@tonic-gate 3262*7c478bd9Sstevel@tonic-gate /* 3263*7c478bd9Sstevel@tonic-gate * Find and configure the child 3264*7c478bd9Sstevel@tonic-gate */ 3265*7c478bd9Sstevel@tonic-gate if (ndi_devi_config_one(parent, config_name, &child, 3266*7c478bd9Sstevel@tonic-gate NDI_PROMNAME | NDI_NO_EVENT) != NDI_SUCCESS) { 3267*7c478bd9Sstevel@tonic-gate ndi_rele_devi(parent); 3268*7c478bd9Sstevel@tonic-gate pn_free(&pn); 3269*7c478bd9Sstevel@tonic-gate kmem_free(component, MAXNAMELEN); 3270*7c478bd9Sstevel@tonic-gate kmem_free(config_name, MAXNAMELEN); 3271*7c478bd9Sstevel@tonic-gate return (-1); 3272*7c478bd9Sstevel@tonic-gate } 3273*7c478bd9Sstevel@tonic-gate 3274*7c478bd9Sstevel@tonic-gate ASSERT(i_ddi_node_state(child) >= DS_ATTACHED); 3275*7c478bd9Sstevel@tonic-gate ndi_rele_devi(parent); 3276*7c478bd9Sstevel@tonic-gate parent = child; 3277*7c478bd9Sstevel@tonic-gate pn_skipslash(&pn); 3278*7c478bd9Sstevel@tonic-gate } 3279*7c478bd9Sstevel@tonic-gate 3280*7c478bd9Sstevel@tonic-gate /* 3281*7c478bd9Sstevel@tonic-gate * First look for a minor node matching minorname. 3282*7c478bd9Sstevel@tonic-gate * Failing that, try to pass minorname to bus_config(). 3283*7c478bd9Sstevel@tonic-gate */ 3284*7c478bd9Sstevel@tonic-gate if (minorname && i_ddi_minorname_to_devtspectype(parent, 3285*7c478bd9Sstevel@tonic-gate minorname, &devt, &spectype) == DDI_FAILURE) { 3286*7c478bd9Sstevel@tonic-gate (void) snprintf(config_name, MAXNAMELEN, "%s", minorname); 3287*7c478bd9Sstevel@tonic-gate if (ndi_devi_config_obp_args(parent, 3288*7c478bd9Sstevel@tonic-gate config_name, &child, 0) != NDI_SUCCESS) { 3289*7c478bd9Sstevel@tonic-gate ndi_rele_devi(parent); 3290*7c478bd9Sstevel@tonic-gate pn_free(&pn); 3291*7c478bd9Sstevel@tonic-gate kmem_free(component, MAXNAMELEN); 3292*7c478bd9Sstevel@tonic-gate kmem_free(config_name, MAXNAMELEN); 3293*7c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_NOTE, 3294*7c478bd9Sstevel@tonic-gate "%s: minor node not found\n", pathname)); 3295*7c478bd9Sstevel@tonic-gate return (-1); 3296*7c478bd9Sstevel@tonic-gate } 3297*7c478bd9Sstevel@tonic-gate minorname = NULL; /* look for default minor */ 3298*7c478bd9Sstevel@tonic-gate ASSERT(i_ddi_node_state(child) >= DS_ATTACHED); 3299*7c478bd9Sstevel@tonic-gate ndi_rele_devi(parent); 3300*7c478bd9Sstevel@tonic-gate parent = child; 3301*7c478bd9Sstevel@tonic-gate } 3302*7c478bd9Sstevel@tonic-gate 3303*7c478bd9Sstevel@tonic-gate if (devtp || spectypep) { 3304*7c478bd9Sstevel@tonic-gate if (minorname == NULL) { 3305*7c478bd9Sstevel@tonic-gate /* search for a default entry */ 3306*7c478bd9Sstevel@tonic-gate mutex_enter(&(DEVI(parent)->devi_lock)); 3307*7c478bd9Sstevel@tonic-gate for (dmn = DEVI(parent)->devi_minor; dmn; 3308*7c478bd9Sstevel@tonic-gate dmn = dmn->next) { 3309*7c478bd9Sstevel@tonic-gate if (dmn->type == DDM_DEFAULT) { 3310*7c478bd9Sstevel@tonic-gate devt = dmn->ddm_dev; 3311*7c478bd9Sstevel@tonic-gate spectype = dmn->ddm_spec_type; 3312*7c478bd9Sstevel@tonic-gate break; 3313*7c478bd9Sstevel@tonic-gate } 3314*7c478bd9Sstevel@tonic-gate } 3315*7c478bd9Sstevel@tonic-gate 3316*7c478bd9Sstevel@tonic-gate if (devt == NODEV) { 3317*7c478bd9Sstevel@tonic-gate /* 3318*7c478bd9Sstevel@tonic-gate * No default minor node, try the first one; 3319*7c478bd9Sstevel@tonic-gate * else, assume 1-1 instance-minor mapping 3320*7c478bd9Sstevel@tonic-gate */ 3321*7c478bd9Sstevel@tonic-gate dmn = DEVI(parent)->devi_minor; 3322*7c478bd9Sstevel@tonic-gate if (dmn && ((dmn->type == DDM_MINOR) || 3323*7c478bd9Sstevel@tonic-gate (dmn->type == DDM_INTERNAL_PATH))) { 3324*7c478bd9Sstevel@tonic-gate devt = dmn->ddm_dev; 3325*7c478bd9Sstevel@tonic-gate spectype = dmn->ddm_spec_type; 3326*7c478bd9Sstevel@tonic-gate } else { 3327*7c478bd9Sstevel@tonic-gate devt = makedevice( 3328*7c478bd9Sstevel@tonic-gate DEVI(parent)->devi_major, 3329*7c478bd9Sstevel@tonic-gate ddi_get_instance(parent)); 3330*7c478bd9Sstevel@tonic-gate spectype = S_IFCHR; 3331*7c478bd9Sstevel@tonic-gate } 3332*7c478bd9Sstevel@tonic-gate } 3333*7c478bd9Sstevel@tonic-gate mutex_exit(&(DEVI(parent)->devi_lock)); 3334*7c478bd9Sstevel@tonic-gate } 3335*7c478bd9Sstevel@tonic-gate if (devtp) 3336*7c478bd9Sstevel@tonic-gate *devtp = devt; 3337*7c478bd9Sstevel@tonic-gate if (spectypep) 3338*7c478bd9Sstevel@tonic-gate *spectypep = spectype; 3339*7c478bd9Sstevel@tonic-gate } 3340*7c478bd9Sstevel@tonic-gate 3341*7c478bd9Sstevel@tonic-gate pn_free(&pn); 3342*7c478bd9Sstevel@tonic-gate kmem_free(component, MAXNAMELEN); 3343*7c478bd9Sstevel@tonic-gate kmem_free(config_name, MAXNAMELEN); 3344*7c478bd9Sstevel@tonic-gate 3345*7c478bd9Sstevel@tonic-gate /* 3346*7c478bd9Sstevel@tonic-gate * If there is no error, return the appropriate parameters 3347*7c478bd9Sstevel@tonic-gate */ 3348*7c478bd9Sstevel@tonic-gate if (dipp != NULL) 3349*7c478bd9Sstevel@tonic-gate *dipp = parent; 3350*7c478bd9Sstevel@tonic-gate else { 3351*7c478bd9Sstevel@tonic-gate /* 3352*7c478bd9Sstevel@tonic-gate * We should really keep the ref count to keep the node from 3353*7c478bd9Sstevel@tonic-gate * detaching but ddi_pathname_to_dev_t() specifies a NULL dipp, 3354*7c478bd9Sstevel@tonic-gate * so we have no way of passing back the held dip. Not holding 3355*7c478bd9Sstevel@tonic-gate * the dip allows detaches to occur - which can cause problems 3356*7c478bd9Sstevel@tonic-gate * for subsystems which call ddi_pathname_to_dev_t (console). 3357*7c478bd9Sstevel@tonic-gate * 3358*7c478bd9Sstevel@tonic-gate * Instead of holding the dip, we place a ddi-no-autodetach 3359*7c478bd9Sstevel@tonic-gate * property on the node to prevent auto detaching. 3360*7c478bd9Sstevel@tonic-gate * 3361*7c478bd9Sstevel@tonic-gate * The right fix is to remove ddi_pathname_to_dev_t and replace 3362*7c478bd9Sstevel@tonic-gate * it, and all references, with a call that specifies a dipp. 3363*7c478bd9Sstevel@tonic-gate * In addition, the callers of this new interfaces would then 3364*7c478bd9Sstevel@tonic-gate * need to call ndi_rele_devi when the reference is complete. 3365*7c478bd9Sstevel@tonic-gate */ 3366*7c478bd9Sstevel@tonic-gate (void) ddi_prop_update_int(DDI_DEV_T_NONE, parent, 3367*7c478bd9Sstevel@tonic-gate DDI_NO_AUTODETACH, 1); 3368*7c478bd9Sstevel@tonic-gate ndi_rele_devi(parent); 3369*7c478bd9Sstevel@tonic-gate } 3370*7c478bd9Sstevel@tonic-gate 3371*7c478bd9Sstevel@tonic-gate return (0); 3372*7c478bd9Sstevel@tonic-gate } 3373*7c478bd9Sstevel@tonic-gate 3374*7c478bd9Sstevel@tonic-gate /* 3375*7c478bd9Sstevel@tonic-gate * Given the pathname of a device, return the dev_t of the corresponding 3376*7c478bd9Sstevel@tonic-gate * device. Returns NODEV on failure. 3377*7c478bd9Sstevel@tonic-gate * 3378*7c478bd9Sstevel@tonic-gate * Note that this call sets the DDI_NO_AUTODETACH property on the devinfo node. 3379*7c478bd9Sstevel@tonic-gate */ 3380*7c478bd9Sstevel@tonic-gate dev_t 3381*7c478bd9Sstevel@tonic-gate ddi_pathname_to_dev_t(char *pathname) 3382*7c478bd9Sstevel@tonic-gate { 3383*7c478bd9Sstevel@tonic-gate dev_t devt; 3384*7c478bd9Sstevel@tonic-gate int error; 3385*7c478bd9Sstevel@tonic-gate 3386*7c478bd9Sstevel@tonic-gate error = resolve_pathname(pathname, NULL, &devt, NULL); 3387*7c478bd9Sstevel@tonic-gate 3388*7c478bd9Sstevel@tonic-gate return (error ? NODEV : devt); 3389*7c478bd9Sstevel@tonic-gate } 3390*7c478bd9Sstevel@tonic-gate 3391*7c478bd9Sstevel@tonic-gate /* 3392*7c478bd9Sstevel@tonic-gate * Translate a prom pathname to kernel devfs pathname. 3393*7c478bd9Sstevel@tonic-gate * Caller is assumed to allocate devfspath memory of 3394*7c478bd9Sstevel@tonic-gate * size at least MAXPATHLEN 3395*7c478bd9Sstevel@tonic-gate * 3396*7c478bd9Sstevel@tonic-gate * The prom pathname may not include minor name, but 3397*7c478bd9Sstevel@tonic-gate * devfs pathname has a minor name portion. 3398*7c478bd9Sstevel@tonic-gate */ 3399*7c478bd9Sstevel@tonic-gate int 3400*7c478bd9Sstevel@tonic-gate i_ddi_prompath_to_devfspath(char *prompath, char *devfspath) 3401*7c478bd9Sstevel@tonic-gate { 3402*7c478bd9Sstevel@tonic-gate dev_t devt = (dev_t)NODEV; 3403*7c478bd9Sstevel@tonic-gate dev_info_t *dip = NULL; 3404*7c478bd9Sstevel@tonic-gate char *minor_name = NULL; 3405*7c478bd9Sstevel@tonic-gate int spectype; 3406*7c478bd9Sstevel@tonic-gate int error; 3407*7c478bd9Sstevel@tonic-gate 3408*7c478bd9Sstevel@tonic-gate error = resolve_pathname(prompath, &dip, &devt, &spectype); 3409*7c478bd9Sstevel@tonic-gate if (error) 3410*7c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 3411*7c478bd9Sstevel@tonic-gate ASSERT(dip && devt != NODEV); 3412*7c478bd9Sstevel@tonic-gate 3413*7c478bd9Sstevel@tonic-gate /* 3414*7c478bd9Sstevel@tonic-gate * Get in-kernel devfs pathname 3415*7c478bd9Sstevel@tonic-gate */ 3416*7c478bd9Sstevel@tonic-gate (void) ddi_pathname(dip, devfspath); 3417*7c478bd9Sstevel@tonic-gate 3418*7c478bd9Sstevel@tonic-gate mutex_enter(&(DEVI(dip)->devi_lock)); 3419*7c478bd9Sstevel@tonic-gate minor_name = i_ddi_devtspectype_to_minorname(dip, devt, spectype); 3420*7c478bd9Sstevel@tonic-gate if (minor_name) { 3421*7c478bd9Sstevel@tonic-gate (void) strcat(devfspath, ":"); 3422*7c478bd9Sstevel@tonic-gate (void) strcat(devfspath, minor_name); 3423*7c478bd9Sstevel@tonic-gate } else { 3424*7c478bd9Sstevel@tonic-gate /* 3425*7c478bd9Sstevel@tonic-gate * If minor_name is NULL, we have an alias minor node. 3426*7c478bd9Sstevel@tonic-gate * So manufacture a path to the corresponding clone minor. 3427*7c478bd9Sstevel@tonic-gate */ 3428*7c478bd9Sstevel@tonic-gate (void) snprintf(devfspath, MAXPATHLEN, "%s:%s", 3429*7c478bd9Sstevel@tonic-gate CLONE_PATH, ddi_driver_name(dip)); 3430*7c478bd9Sstevel@tonic-gate } 3431*7c478bd9Sstevel@tonic-gate mutex_exit(&(DEVI(dip)->devi_lock)); 3432*7c478bd9Sstevel@tonic-gate 3433*7c478bd9Sstevel@tonic-gate /* release hold from resolve_pathname() */ 3434*7c478bd9Sstevel@tonic-gate ndi_rele_devi(dip); 3435*7c478bd9Sstevel@tonic-gate return (0); 3436*7c478bd9Sstevel@tonic-gate } 3437*7c478bd9Sstevel@tonic-gate 3438*7c478bd9Sstevel@tonic-gate /* 3439*7c478bd9Sstevel@tonic-gate * Reset all the pure leaf drivers on the system at halt time 3440*7c478bd9Sstevel@tonic-gate */ 3441*7c478bd9Sstevel@tonic-gate static int 3442*7c478bd9Sstevel@tonic-gate reset_leaf_device(dev_info_t *dip, void *arg) 3443*7c478bd9Sstevel@tonic-gate { 3444*7c478bd9Sstevel@tonic-gate _NOTE(ARGUNUSED(arg)) 3445*7c478bd9Sstevel@tonic-gate struct dev_ops *ops; 3446*7c478bd9Sstevel@tonic-gate 3447*7c478bd9Sstevel@tonic-gate /* if the device doesn't need to be reset then there's nothing to do */ 3448*7c478bd9Sstevel@tonic-gate if (!DEVI_NEED_RESET(dip)) 3449*7c478bd9Sstevel@tonic-gate return (DDI_WALK_CONTINUE); 3450*7c478bd9Sstevel@tonic-gate 3451*7c478bd9Sstevel@tonic-gate /* 3452*7c478bd9Sstevel@tonic-gate * if the device isn't a char/block device or doesn't have a 3453*7c478bd9Sstevel@tonic-gate * reset entry point then there's nothing to do. 3454*7c478bd9Sstevel@tonic-gate */ 3455*7c478bd9Sstevel@tonic-gate ops = ddi_get_driver(dip); 3456*7c478bd9Sstevel@tonic-gate if ((ops == NULL) || (ops->devo_cb_ops == NULL) || 3457*7c478bd9Sstevel@tonic-gate (ops->devo_reset == nodev) || (ops->devo_reset == nulldev) || 3458*7c478bd9Sstevel@tonic-gate (ops->devo_reset == NULL)) 3459*7c478bd9Sstevel@tonic-gate return (DDI_WALK_CONTINUE); 3460*7c478bd9Sstevel@tonic-gate 3461*7c478bd9Sstevel@tonic-gate if (DEVI_IS_ATTACHING(dip) || DEVI_IS_DETACHING(dip)) { 3462*7c478bd9Sstevel@tonic-gate static char path[MAXPATHLEN]; 3463*7c478bd9Sstevel@tonic-gate 3464*7c478bd9Sstevel@tonic-gate /* 3465*7c478bd9Sstevel@tonic-gate * bad news, this device has blocked in it's attach or 3466*7c478bd9Sstevel@tonic-gate * detach routine, which means it not safe to call it's 3467*7c478bd9Sstevel@tonic-gate * devo_reset() entry point. 3468*7c478bd9Sstevel@tonic-gate */ 3469*7c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "unable to reset device: %s", 3470*7c478bd9Sstevel@tonic-gate ddi_pathname(dip, path)); 3471*7c478bd9Sstevel@tonic-gate return (DDI_WALK_CONTINUE); 3472*7c478bd9Sstevel@tonic-gate } 3473*7c478bd9Sstevel@tonic-gate 3474*7c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_NOTE, "resetting %s%d\n", 3475*7c478bd9Sstevel@tonic-gate ddi_driver_name(dip), ddi_get_instance(dip))); 3476*7c478bd9Sstevel@tonic-gate 3477*7c478bd9Sstevel@tonic-gate (void) devi_reset(dip, DDI_RESET_FORCE); 3478*7c478bd9Sstevel@tonic-gate return (DDI_WALK_CONTINUE); 3479*7c478bd9Sstevel@tonic-gate } 3480*7c478bd9Sstevel@tonic-gate 3481*7c478bd9Sstevel@tonic-gate void 3482*7c478bd9Sstevel@tonic-gate reset_leaves(void) 3483*7c478bd9Sstevel@tonic-gate { 3484*7c478bd9Sstevel@tonic-gate /* 3485*7c478bd9Sstevel@tonic-gate * if we're reached here, the device tree better not be changing. 3486*7c478bd9Sstevel@tonic-gate * so either devinfo_freeze better be set or we better be panicing. 3487*7c478bd9Sstevel@tonic-gate */ 3488*7c478bd9Sstevel@tonic-gate ASSERT(devinfo_freeze || panicstr); 3489*7c478bd9Sstevel@tonic-gate 3490*7c478bd9Sstevel@tonic-gate (void) walk_devs(top_devinfo, reset_leaf_device, NULL, 0); 3491*7c478bd9Sstevel@tonic-gate } 3492*7c478bd9Sstevel@tonic-gate 3493*7c478bd9Sstevel@tonic-gate /* 3494*7c478bd9Sstevel@tonic-gate * devtree_freeze() must be called before reset_leaves() during a 3495*7c478bd9Sstevel@tonic-gate * normal system shutdown. It attempts to ensure that there are no 3496*7c478bd9Sstevel@tonic-gate * outstanding attach or detach operations in progress when reset_leaves() 3497*7c478bd9Sstevel@tonic-gate * is invoked. It must be called before the system becomes single-threaded 3498*7c478bd9Sstevel@tonic-gate * because device attach and detach are multi-threaded operations. (note 3499*7c478bd9Sstevel@tonic-gate * that during system shutdown the system doesn't actually become 3500*7c478bd9Sstevel@tonic-gate * single-thread since other threads still exist, but the shutdown thread 3501*7c478bd9Sstevel@tonic-gate * will disable preemption for itself, raise it's pil, and stop all the 3502*7c478bd9Sstevel@tonic-gate * other cpus in the system there by effectively making the system 3503*7c478bd9Sstevel@tonic-gate * single-threaded.) 3504*7c478bd9Sstevel@tonic-gate */ 3505*7c478bd9Sstevel@tonic-gate void 3506*7c478bd9Sstevel@tonic-gate devtree_freeze(void) 3507*7c478bd9Sstevel@tonic-gate { 3508*7c478bd9Sstevel@tonic-gate int delayed = 0; 3509*7c478bd9Sstevel@tonic-gate 3510*7c478bd9Sstevel@tonic-gate /* if we're panicing then the device tree isn't going to be changing */ 3511*7c478bd9Sstevel@tonic-gate if (panicstr) 3512*7c478bd9Sstevel@tonic-gate return; 3513*7c478bd9Sstevel@tonic-gate 3514*7c478bd9Sstevel@tonic-gate /* stop all dev_info state changes in the device tree */ 3515*7c478bd9Sstevel@tonic-gate devinfo_freeze = gethrtime(); 3516*7c478bd9Sstevel@tonic-gate 3517*7c478bd9Sstevel@tonic-gate /* 3518*7c478bd9Sstevel@tonic-gate * if we're not panicing and there are on-going attach or detach 3519*7c478bd9Sstevel@tonic-gate * operations, wait for up to 3 seconds for them to finish. This 3520*7c478bd9Sstevel@tonic-gate * is a randomly chosen interval but this should be ok because: 3521*7c478bd9Sstevel@tonic-gate * - 3 seconds is very small relative to the deadman timer. 3522*7c478bd9Sstevel@tonic-gate * - normal attach and detach operations should be very quick. 3523*7c478bd9Sstevel@tonic-gate * - attach and detach operations are fairly rare. 3524*7c478bd9Sstevel@tonic-gate */ 3525*7c478bd9Sstevel@tonic-gate while (!panicstr && atomic_add_long_nv(&devinfo_attach_detach, 0) && 3526*7c478bd9Sstevel@tonic-gate (delayed < 3)) { 3527*7c478bd9Sstevel@tonic-gate delayed += 1; 3528*7c478bd9Sstevel@tonic-gate 3529*7c478bd9Sstevel@tonic-gate /* do a sleeping wait for one second */ 3530*7c478bd9Sstevel@tonic-gate ASSERT(!servicing_interrupt()); 3531*7c478bd9Sstevel@tonic-gate delay(drv_usectohz(MICROSEC)); 3532*7c478bd9Sstevel@tonic-gate } 3533*7c478bd9Sstevel@tonic-gate } 3534*7c478bd9Sstevel@tonic-gate 3535*7c478bd9Sstevel@tonic-gate static int 3536*7c478bd9Sstevel@tonic-gate bind_dip(dev_info_t *dip, void *arg) 3537*7c478bd9Sstevel@tonic-gate { 3538*7c478bd9Sstevel@tonic-gate _NOTE(ARGUNUSED(arg)) 3539*7c478bd9Sstevel@tonic-gate if (i_ddi_node_state(dip) < DS_BOUND) 3540*7c478bd9Sstevel@tonic-gate (void) ndi_devi_bind_driver(dip, 0); 3541*7c478bd9Sstevel@tonic-gate 3542*7c478bd9Sstevel@tonic-gate return (DDI_WALK_CONTINUE); 3543*7c478bd9Sstevel@tonic-gate } 3544*7c478bd9Sstevel@tonic-gate 3545*7c478bd9Sstevel@tonic-gate void 3546*7c478bd9Sstevel@tonic-gate i_ddi_bind_devs(void) 3547*7c478bd9Sstevel@tonic-gate { 3548*7c478bd9Sstevel@tonic-gate ddi_walk_devs(top_devinfo, bind_dip, (void *)NULL); 3549*7c478bd9Sstevel@tonic-gate } 3550*7c478bd9Sstevel@tonic-gate 3551*7c478bd9Sstevel@tonic-gate static int 3552*7c478bd9Sstevel@tonic-gate unbind_children(dev_info_t *dip, void *arg) 3553*7c478bd9Sstevel@tonic-gate { 3554*7c478bd9Sstevel@tonic-gate int circ; 3555*7c478bd9Sstevel@tonic-gate dev_info_t *cdip; 3556*7c478bd9Sstevel@tonic-gate major_t major = (major_t)(uintptr_t)arg; 3557*7c478bd9Sstevel@tonic-gate 3558*7c478bd9Sstevel@tonic-gate ndi_devi_enter(dip, &circ); 3559*7c478bd9Sstevel@tonic-gate cdip = ddi_get_child(dip); 3560*7c478bd9Sstevel@tonic-gate /* 3561*7c478bd9Sstevel@tonic-gate * We are called either from rem_drv or update_drv. 3562*7c478bd9Sstevel@tonic-gate * In both cases, we unbind persistent nodes and destroy 3563*7c478bd9Sstevel@tonic-gate * .conf nodes. In the case of rem_drv, this will be the 3564*7c478bd9Sstevel@tonic-gate * final state. In the case of update_drv, i_ddi_bind_devs() 3565*7c478bd9Sstevel@tonic-gate * will be invoked later to reenumerate (new) driver.conf 3566*7c478bd9Sstevel@tonic-gate * rebind persistent nodes. 3567*7c478bd9Sstevel@tonic-gate */ 3568*7c478bd9Sstevel@tonic-gate while (cdip) { 3569*7c478bd9Sstevel@tonic-gate dev_info_t *next = ddi_get_next_sibling(cdip); 3570*7c478bd9Sstevel@tonic-gate if ((i_ddi_node_state(cdip) > DS_INITIALIZED) || 3571*7c478bd9Sstevel@tonic-gate (ddi_driver_major(cdip) != major)) { 3572*7c478bd9Sstevel@tonic-gate cdip = next; 3573*7c478bd9Sstevel@tonic-gate continue; 3574*7c478bd9Sstevel@tonic-gate } 3575*7c478bd9Sstevel@tonic-gate (void) ndi_devi_unbind_driver(cdip); 3576*7c478bd9Sstevel@tonic-gate if (ndi_dev_is_persistent_node(cdip) == 0) 3577*7c478bd9Sstevel@tonic-gate (void) ddi_remove_child(cdip, 0); 3578*7c478bd9Sstevel@tonic-gate cdip = next; 3579*7c478bd9Sstevel@tonic-gate } 3580*7c478bd9Sstevel@tonic-gate ndi_devi_exit(dip, circ); 3581*7c478bd9Sstevel@tonic-gate 3582*7c478bd9Sstevel@tonic-gate return (DDI_WALK_CONTINUE); 3583*7c478bd9Sstevel@tonic-gate } 3584*7c478bd9Sstevel@tonic-gate 3585*7c478bd9Sstevel@tonic-gate void 3586*7c478bd9Sstevel@tonic-gate i_ddi_unbind_devs(major_t major) 3587*7c478bd9Sstevel@tonic-gate { 3588*7c478bd9Sstevel@tonic-gate ddi_walk_devs(top_devinfo, unbind_children, (void *)(uintptr_t)major); 3589*7c478bd9Sstevel@tonic-gate } 3590*7c478bd9Sstevel@tonic-gate 3591*7c478bd9Sstevel@tonic-gate /* 3592*7c478bd9Sstevel@tonic-gate * I/O Hotplug control 3593*7c478bd9Sstevel@tonic-gate */ 3594*7c478bd9Sstevel@tonic-gate 3595*7c478bd9Sstevel@tonic-gate /* 3596*7c478bd9Sstevel@tonic-gate * create and attach a dev_info node from a .conf file spec 3597*7c478bd9Sstevel@tonic-gate */ 3598*7c478bd9Sstevel@tonic-gate static void 3599*7c478bd9Sstevel@tonic-gate init_spec_child(dev_info_t *pdip, struct hwc_spec *specp, uint_t flags) 3600*7c478bd9Sstevel@tonic-gate { 3601*7c478bd9Sstevel@tonic-gate _NOTE(ARGUNUSED(flags)) 3602*7c478bd9Sstevel@tonic-gate dev_info_t *dip; 3603*7c478bd9Sstevel@tonic-gate char *node_name; 3604*7c478bd9Sstevel@tonic-gate 3605*7c478bd9Sstevel@tonic-gate if (((node_name = specp->hwc_devi_name) == NULL) || 3606*7c478bd9Sstevel@tonic-gate (ddi_name_to_major(node_name) == (major_t)-1)) { 3607*7c478bd9Sstevel@tonic-gate char *tmp = node_name; 3608*7c478bd9Sstevel@tonic-gate if (tmp == NULL) 3609*7c478bd9Sstevel@tonic-gate tmp = "<none>"; 3610*7c478bd9Sstevel@tonic-gate cmn_err(CE_CONT, 3611*7c478bd9Sstevel@tonic-gate "init_spec_child: parent=%s, bad spec (%s)\n", 3612*7c478bd9Sstevel@tonic-gate ddi_node_name(pdip), tmp); 3613*7c478bd9Sstevel@tonic-gate return; 3614*7c478bd9Sstevel@tonic-gate } 3615*7c478bd9Sstevel@tonic-gate 3616*7c478bd9Sstevel@tonic-gate dip = i_ddi_alloc_node(pdip, node_name, (dnode_t)DEVI_PSEUDO_NODEID, 3617*7c478bd9Sstevel@tonic-gate -1, specp->hwc_devi_sys_prop_ptr, KM_SLEEP); 3618*7c478bd9Sstevel@tonic-gate 3619*7c478bd9Sstevel@tonic-gate if (dip == NULL) 3620*7c478bd9Sstevel@tonic-gate return; 3621*7c478bd9Sstevel@tonic-gate 3622*7c478bd9Sstevel@tonic-gate if (ddi_initchild(pdip, dip) != DDI_SUCCESS) 3623*7c478bd9Sstevel@tonic-gate (void) ddi_remove_child(dip, 0); 3624*7c478bd9Sstevel@tonic-gate } 3625*7c478bd9Sstevel@tonic-gate 3626*7c478bd9Sstevel@tonic-gate /* 3627*7c478bd9Sstevel@tonic-gate * Lookup hwc specs from hash tables and make children from the spec 3628*7c478bd9Sstevel@tonic-gate * Because some .conf children are "merge" nodes, we also initialize 3629*7c478bd9Sstevel@tonic-gate * .conf children to merge properties onto hardware nodes. 3630*7c478bd9Sstevel@tonic-gate * 3631*7c478bd9Sstevel@tonic-gate * The pdip must be held busy. 3632*7c478bd9Sstevel@tonic-gate */ 3633*7c478bd9Sstevel@tonic-gate int 3634*7c478bd9Sstevel@tonic-gate i_ndi_make_spec_children(dev_info_t *pdip, uint_t flags) 3635*7c478bd9Sstevel@tonic-gate { 3636*7c478bd9Sstevel@tonic-gate extern struct hwc_spec *hwc_get_child_spec(dev_info_t *, major_t); 3637*7c478bd9Sstevel@tonic-gate 3638*7c478bd9Sstevel@tonic-gate struct hwc_spec *list, *spec; 3639*7c478bd9Sstevel@tonic-gate 3640*7c478bd9Sstevel@tonic-gate if (DEVI(pdip)->devi_flags & DEVI_MADE_CHILDREN) 3641*7c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 3642*7c478bd9Sstevel@tonic-gate 3643*7c478bd9Sstevel@tonic-gate list = hwc_get_child_spec(pdip, (major_t)-1); 3644*7c478bd9Sstevel@tonic-gate for (spec = list; spec != NULL; spec = spec->hwc_next) { 3645*7c478bd9Sstevel@tonic-gate init_spec_child(pdip, spec, flags); 3646*7c478bd9Sstevel@tonic-gate } 3647*7c478bd9Sstevel@tonic-gate hwc_free_spec_list(list); 3648*7c478bd9Sstevel@tonic-gate 3649*7c478bd9Sstevel@tonic-gate mutex_enter(&DEVI(pdip)->devi_lock); 3650*7c478bd9Sstevel@tonic-gate DEVI(pdip)->devi_flags |= DEVI_MADE_CHILDREN; 3651*7c478bd9Sstevel@tonic-gate mutex_exit(&DEVI(pdip)->devi_lock); 3652*7c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 3653*7c478bd9Sstevel@tonic-gate } 3654*7c478bd9Sstevel@tonic-gate 3655*7c478bd9Sstevel@tonic-gate /* 3656*7c478bd9Sstevel@tonic-gate * Run initchild on all child nodes such that instance assignment 3657*7c478bd9Sstevel@tonic-gate * for multiport network cards are contiguous. 3658*7c478bd9Sstevel@tonic-gate * 3659*7c478bd9Sstevel@tonic-gate * The pdip must be held busy. 3660*7c478bd9Sstevel@tonic-gate */ 3661*7c478bd9Sstevel@tonic-gate static void 3662*7c478bd9Sstevel@tonic-gate i_ndi_init_hw_children(dev_info_t *pdip, uint_t flags) 3663*7c478bd9Sstevel@tonic-gate { 3664*7c478bd9Sstevel@tonic-gate dev_info_t *dip; 3665*7c478bd9Sstevel@tonic-gate 3666*7c478bd9Sstevel@tonic-gate ASSERT(DEVI(pdip)->devi_flags & DEVI_MADE_CHILDREN); 3667*7c478bd9Sstevel@tonic-gate 3668*7c478bd9Sstevel@tonic-gate /* contiguous instance assignment */ 3669*7c478bd9Sstevel@tonic-gate e_ddi_enter_instance(); 3670*7c478bd9Sstevel@tonic-gate dip = ddi_get_child(pdip); 3671*7c478bd9Sstevel@tonic-gate while (dip) { 3672*7c478bd9Sstevel@tonic-gate if (ndi_dev_is_persistent_node(dip)) 3673*7c478bd9Sstevel@tonic-gate (void) i_ndi_config_node(dip, DS_INITIALIZED, flags); 3674*7c478bd9Sstevel@tonic-gate dip = ddi_get_next_sibling(dip); 3675*7c478bd9Sstevel@tonic-gate } 3676*7c478bd9Sstevel@tonic-gate e_ddi_exit_instance(); 3677*7c478bd9Sstevel@tonic-gate } 3678*7c478bd9Sstevel@tonic-gate 3679*7c478bd9Sstevel@tonic-gate /* 3680*7c478bd9Sstevel@tonic-gate * report device status 3681*7c478bd9Sstevel@tonic-gate */ 3682*7c478bd9Sstevel@tonic-gate static void 3683*7c478bd9Sstevel@tonic-gate i_ndi_devi_report_status_change(dev_info_t *dip, char *path) 3684*7c478bd9Sstevel@tonic-gate { 3685*7c478bd9Sstevel@tonic-gate char *status; 3686*7c478bd9Sstevel@tonic-gate 3687*7c478bd9Sstevel@tonic-gate if (!DEVI_NEED_REPORT(dip) || 3688*7c478bd9Sstevel@tonic-gate (i_ddi_node_state(dip) < DS_INITIALIZED)) { 3689*7c478bd9Sstevel@tonic-gate return; 3690*7c478bd9Sstevel@tonic-gate } 3691*7c478bd9Sstevel@tonic-gate 3692*7c478bd9Sstevel@tonic-gate if (DEVI_IS_DEVICE_OFFLINE(dip)) { 3693*7c478bd9Sstevel@tonic-gate status = "offline"; 3694*7c478bd9Sstevel@tonic-gate } else if (DEVI_IS_DEVICE_DOWN(dip)) { 3695*7c478bd9Sstevel@tonic-gate status = "down"; 3696*7c478bd9Sstevel@tonic-gate } else if (DEVI_IS_BUS_QUIESCED(dip)) { 3697*7c478bd9Sstevel@tonic-gate status = "quiesced"; 3698*7c478bd9Sstevel@tonic-gate } else if (DEVI_IS_BUS_DOWN(dip)) { 3699*7c478bd9Sstevel@tonic-gate status = "down"; 3700*7c478bd9Sstevel@tonic-gate } else if (i_ddi_node_state(dip) == DS_READY) { 3701*7c478bd9Sstevel@tonic-gate status = "online"; 3702*7c478bd9Sstevel@tonic-gate } else { 3703*7c478bd9Sstevel@tonic-gate status = "unknown"; 3704*7c478bd9Sstevel@tonic-gate } 3705*7c478bd9Sstevel@tonic-gate 3706*7c478bd9Sstevel@tonic-gate if (path == NULL) { 3707*7c478bd9Sstevel@tonic-gate path = kmem_alloc(MAXPATHLEN, KM_SLEEP); 3708*7c478bd9Sstevel@tonic-gate cmn_err(CE_CONT, "?%s (%s%d) %s\n", 3709*7c478bd9Sstevel@tonic-gate ddi_pathname(dip, path), ddi_driver_name(dip), 3710*7c478bd9Sstevel@tonic-gate ddi_get_instance(dip), status); 3711*7c478bd9Sstevel@tonic-gate kmem_free(path, MAXPATHLEN); 3712*7c478bd9Sstevel@tonic-gate } else { 3713*7c478bd9Sstevel@tonic-gate cmn_err(CE_CONT, "?%s (%s%d) %s\n", 3714*7c478bd9Sstevel@tonic-gate path, ddi_driver_name(dip), 3715*7c478bd9Sstevel@tonic-gate ddi_get_instance(dip), status); 3716*7c478bd9Sstevel@tonic-gate } 3717*7c478bd9Sstevel@tonic-gate 3718*7c478bd9Sstevel@tonic-gate DEVI_REPORT_DONE(dip); 3719*7c478bd9Sstevel@tonic-gate } 3720*7c478bd9Sstevel@tonic-gate 3721*7c478bd9Sstevel@tonic-gate /* 3722*7c478bd9Sstevel@tonic-gate * log a notification that a dev_info node has been configured. 3723*7c478bd9Sstevel@tonic-gate */ 3724*7c478bd9Sstevel@tonic-gate static int 3725*7c478bd9Sstevel@tonic-gate i_log_devfs_add_devinfo(dev_info_t *dip, uint_t flags) 3726*7c478bd9Sstevel@tonic-gate { 3727*7c478bd9Sstevel@tonic-gate int se_err; 3728*7c478bd9Sstevel@tonic-gate char *pathname; 3729*7c478bd9Sstevel@tonic-gate sysevent_t *ev; 3730*7c478bd9Sstevel@tonic-gate sysevent_id_t eid; 3731*7c478bd9Sstevel@tonic-gate sysevent_value_t se_val; 3732*7c478bd9Sstevel@tonic-gate sysevent_attr_list_t *ev_attr_list = NULL; 3733*7c478bd9Sstevel@tonic-gate char *class_name; 3734*7c478bd9Sstevel@tonic-gate int no_transport = 0; 3735*7c478bd9Sstevel@tonic-gate 3736*7c478bd9Sstevel@tonic-gate ASSERT(dip); 3737*7c478bd9Sstevel@tonic-gate 3738*7c478bd9Sstevel@tonic-gate /* 3739*7c478bd9Sstevel@tonic-gate * Invalidate the devinfo snapshot cache 3740*7c478bd9Sstevel@tonic-gate */ 3741*7c478bd9Sstevel@tonic-gate i_ddi_di_cache_invalidate(KM_SLEEP); 3742*7c478bd9Sstevel@tonic-gate 3743*7c478bd9Sstevel@tonic-gate /* do not generate ESC_DEVFS_DEVI_ADD event during boot */ 3744*7c478bd9Sstevel@tonic-gate if (!i_ddi_io_initialized()) 3745*7c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 3746*7c478bd9Sstevel@tonic-gate 3747*7c478bd9Sstevel@tonic-gate ev = sysevent_alloc(EC_DEVFS, ESC_DEVFS_DEVI_ADD, EP_DDI, SE_SLEEP); 3748*7c478bd9Sstevel@tonic-gate 3749*7c478bd9Sstevel@tonic-gate pathname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 3750*7c478bd9Sstevel@tonic-gate 3751*7c478bd9Sstevel@tonic-gate (void) ddi_pathname(dip, pathname); 3752*7c478bd9Sstevel@tonic-gate ASSERT(strlen(pathname)); 3753*7c478bd9Sstevel@tonic-gate 3754*7c478bd9Sstevel@tonic-gate se_val.value_type = SE_DATA_TYPE_STRING; 3755*7c478bd9Sstevel@tonic-gate se_val.value.sv_string = pathname; 3756*7c478bd9Sstevel@tonic-gate if (sysevent_add_attr(&ev_attr_list, DEVFS_PATHNAME, 3757*7c478bd9Sstevel@tonic-gate &se_val, SE_SLEEP) != 0) { 3758*7c478bd9Sstevel@tonic-gate goto fail; 3759*7c478bd9Sstevel@tonic-gate } 3760*7c478bd9Sstevel@tonic-gate 3761*7c478bd9Sstevel@tonic-gate /* add the device class attribute */ 3762*7c478bd9Sstevel@tonic-gate if ((class_name = i_ddi_devi_class(dip)) != NULL) { 3763*7c478bd9Sstevel@tonic-gate se_val.value_type = SE_DATA_TYPE_STRING; 3764*7c478bd9Sstevel@tonic-gate se_val.value.sv_string = class_name; 3765*7c478bd9Sstevel@tonic-gate 3766*7c478bd9Sstevel@tonic-gate if (sysevent_add_attr(&ev_attr_list, 3767*7c478bd9Sstevel@tonic-gate DEVFS_DEVI_CLASS, &se_val, SE_SLEEP) != 0) { 3768*7c478bd9Sstevel@tonic-gate sysevent_free_attr(ev_attr_list); 3769*7c478bd9Sstevel@tonic-gate goto fail; 3770*7c478bd9Sstevel@tonic-gate } 3771*7c478bd9Sstevel@tonic-gate } 3772*7c478bd9Sstevel@tonic-gate 3773*7c478bd9Sstevel@tonic-gate /* 3774*7c478bd9Sstevel@tonic-gate * must log a branch event too unless NDI_BRANCH_EVENT_OP is set, 3775*7c478bd9Sstevel@tonic-gate * in which case the branch event will be logged by the caller 3776*7c478bd9Sstevel@tonic-gate * after the entire branch has been configured. 3777*7c478bd9Sstevel@tonic-gate */ 3778*7c478bd9Sstevel@tonic-gate if ((flags & NDI_BRANCH_EVENT_OP) == 0) { 3779*7c478bd9Sstevel@tonic-gate /* 3780*7c478bd9Sstevel@tonic-gate * Instead of logging a separate branch event just add 3781*7c478bd9Sstevel@tonic-gate * DEVFS_BRANCH_EVENT attribute. It indicates devfsadmd to 3782*7c478bd9Sstevel@tonic-gate * generate a EC_DEV_BRANCH event. 3783*7c478bd9Sstevel@tonic-gate */ 3784*7c478bd9Sstevel@tonic-gate se_val.value_type = SE_DATA_TYPE_INT32; 3785*7c478bd9Sstevel@tonic-gate se_val.value.sv_int32 = 1; 3786*7c478bd9Sstevel@tonic-gate if (sysevent_add_attr(&ev_attr_list, 3787*7c478bd9Sstevel@tonic-gate DEVFS_BRANCH_EVENT, &se_val, SE_SLEEP) != 0) { 3788*7c478bd9Sstevel@tonic-gate sysevent_free_attr(ev_attr_list); 3789*7c478bd9Sstevel@tonic-gate goto fail; 3790*7c478bd9Sstevel@tonic-gate } 3791*7c478bd9Sstevel@tonic-gate } 3792*7c478bd9Sstevel@tonic-gate 3793*7c478bd9Sstevel@tonic-gate if (sysevent_attach_attributes(ev, ev_attr_list) != 0) { 3794*7c478bd9Sstevel@tonic-gate sysevent_free_attr(ev_attr_list); 3795*7c478bd9Sstevel@tonic-gate goto fail; 3796*7c478bd9Sstevel@tonic-gate } 3797*7c478bd9Sstevel@tonic-gate 3798*7c478bd9Sstevel@tonic-gate if ((se_err = log_sysevent(ev, SE_SLEEP, &eid)) != 0) { 3799*7c478bd9Sstevel@tonic-gate if (se_err == SE_NO_TRANSPORT) 3800*7c478bd9Sstevel@tonic-gate no_transport = 1; 3801*7c478bd9Sstevel@tonic-gate goto fail; 3802*7c478bd9Sstevel@tonic-gate } 3803*7c478bd9Sstevel@tonic-gate 3804*7c478bd9Sstevel@tonic-gate sysevent_free(ev); 3805*7c478bd9Sstevel@tonic-gate kmem_free(pathname, MAXPATHLEN); 3806*7c478bd9Sstevel@tonic-gate 3807*7c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 3808*7c478bd9Sstevel@tonic-gate 3809*7c478bd9Sstevel@tonic-gate fail: 3810*7c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "failed to log ESC_DEVFS_DEVI_ADD event for %s%s", 3811*7c478bd9Sstevel@tonic-gate pathname, (no_transport) ? " (syseventd not responding)" : ""); 3812*7c478bd9Sstevel@tonic-gate 3813*7c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "/dev may not be current for driver %s. " 3814*7c478bd9Sstevel@tonic-gate "Run devfsadm -i %s", 3815*7c478bd9Sstevel@tonic-gate ddi_driver_name(dip), ddi_driver_name(dip)); 3816*7c478bd9Sstevel@tonic-gate 3817*7c478bd9Sstevel@tonic-gate sysevent_free(ev); 3818*7c478bd9Sstevel@tonic-gate kmem_free(pathname, MAXPATHLEN); 3819*7c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 3820*7c478bd9Sstevel@tonic-gate } 3821*7c478bd9Sstevel@tonic-gate 3822*7c478bd9Sstevel@tonic-gate /* 3823*7c478bd9Sstevel@tonic-gate * log a notification that a dev_info node has been unconfigured. 3824*7c478bd9Sstevel@tonic-gate */ 3825*7c478bd9Sstevel@tonic-gate static int 3826*7c478bd9Sstevel@tonic-gate i_log_devfs_remove_devinfo(char *pathname, char *class_name, char *driver_name, 3827*7c478bd9Sstevel@tonic-gate int instance, uint_t flags) 3828*7c478bd9Sstevel@tonic-gate { 3829*7c478bd9Sstevel@tonic-gate sysevent_t *ev; 3830*7c478bd9Sstevel@tonic-gate sysevent_id_t eid; 3831*7c478bd9Sstevel@tonic-gate sysevent_value_t se_val; 3832*7c478bd9Sstevel@tonic-gate sysevent_attr_list_t *ev_attr_list = NULL; 3833*7c478bd9Sstevel@tonic-gate int se_err; 3834*7c478bd9Sstevel@tonic-gate int no_transport = 0; 3835*7c478bd9Sstevel@tonic-gate 3836*7c478bd9Sstevel@tonic-gate i_ddi_di_cache_invalidate(KM_SLEEP); 3837*7c478bd9Sstevel@tonic-gate 3838*7c478bd9Sstevel@tonic-gate if (!i_ddi_io_initialized()) 3839*7c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 3840*7c478bd9Sstevel@tonic-gate 3841*7c478bd9Sstevel@tonic-gate ev = sysevent_alloc(EC_DEVFS, ESC_DEVFS_DEVI_REMOVE, EP_DDI, SE_SLEEP); 3842*7c478bd9Sstevel@tonic-gate 3843*7c478bd9Sstevel@tonic-gate se_val.value_type = SE_DATA_TYPE_STRING; 3844*7c478bd9Sstevel@tonic-gate se_val.value.sv_string = pathname; 3845*7c478bd9Sstevel@tonic-gate if (sysevent_add_attr(&ev_attr_list, DEVFS_PATHNAME, 3846*7c478bd9Sstevel@tonic-gate &se_val, SE_SLEEP) != 0) { 3847*7c478bd9Sstevel@tonic-gate goto fail; 3848*7c478bd9Sstevel@tonic-gate } 3849*7c478bd9Sstevel@tonic-gate 3850*7c478bd9Sstevel@tonic-gate if (class_name) { 3851*7c478bd9Sstevel@tonic-gate /* add the device class, driver name and instance attributes */ 3852*7c478bd9Sstevel@tonic-gate 3853*7c478bd9Sstevel@tonic-gate se_val.value_type = SE_DATA_TYPE_STRING; 3854*7c478bd9Sstevel@tonic-gate se_val.value.sv_string = class_name; 3855*7c478bd9Sstevel@tonic-gate if (sysevent_add_attr(&ev_attr_list, 3856*7c478bd9Sstevel@tonic-gate DEVFS_DEVI_CLASS, &se_val, SE_SLEEP) != 0) { 3857*7c478bd9Sstevel@tonic-gate sysevent_free_attr(ev_attr_list); 3858*7c478bd9Sstevel@tonic-gate goto fail; 3859*7c478bd9Sstevel@tonic-gate } 3860*7c478bd9Sstevel@tonic-gate 3861*7c478bd9Sstevel@tonic-gate se_val.value_type = SE_DATA_TYPE_STRING; 3862*7c478bd9Sstevel@tonic-gate se_val.value.sv_string = driver_name; 3863*7c478bd9Sstevel@tonic-gate if (sysevent_add_attr(&ev_attr_list, 3864*7c478bd9Sstevel@tonic-gate DEVFS_DRIVER_NAME, &se_val, SE_SLEEP) != 0) { 3865*7c478bd9Sstevel@tonic-gate sysevent_free_attr(ev_attr_list); 3866*7c478bd9Sstevel@tonic-gate goto fail; 3867*7c478bd9Sstevel@tonic-gate } 3868*7c478bd9Sstevel@tonic-gate 3869*7c478bd9Sstevel@tonic-gate se_val.value_type = SE_DATA_TYPE_INT32; 3870*7c478bd9Sstevel@tonic-gate se_val.value.sv_int32 = instance; 3871*7c478bd9Sstevel@tonic-gate if (sysevent_add_attr(&ev_attr_list, 3872*7c478bd9Sstevel@tonic-gate DEVFS_INSTANCE, &se_val, SE_SLEEP) != 0) { 3873*7c478bd9Sstevel@tonic-gate sysevent_free_attr(ev_attr_list); 3874*7c478bd9Sstevel@tonic-gate goto fail; 3875*7c478bd9Sstevel@tonic-gate } 3876*7c478bd9Sstevel@tonic-gate } 3877*7c478bd9Sstevel@tonic-gate 3878*7c478bd9Sstevel@tonic-gate /* 3879*7c478bd9Sstevel@tonic-gate * must log a branch event too unless NDI_BRANCH_EVENT_OP is set, 3880*7c478bd9Sstevel@tonic-gate * in which case the branch event will be logged by the caller 3881*7c478bd9Sstevel@tonic-gate * after the entire branch has been unconfigured. 3882*7c478bd9Sstevel@tonic-gate */ 3883*7c478bd9Sstevel@tonic-gate if ((flags & NDI_BRANCH_EVENT_OP) == 0) { 3884*7c478bd9Sstevel@tonic-gate /* 3885*7c478bd9Sstevel@tonic-gate * Instead of logging a separate branch event just add 3886*7c478bd9Sstevel@tonic-gate * DEVFS_BRANCH_EVENT attribute. It indicates devfsadmd to 3887*7c478bd9Sstevel@tonic-gate * generate a EC_DEV_BRANCH event. 3888*7c478bd9Sstevel@tonic-gate */ 3889*7c478bd9Sstevel@tonic-gate se_val.value_type = SE_DATA_TYPE_INT32; 3890*7c478bd9Sstevel@tonic-gate se_val.value.sv_int32 = 1; 3891*7c478bd9Sstevel@tonic-gate if (sysevent_add_attr(&ev_attr_list, 3892*7c478bd9Sstevel@tonic-gate DEVFS_BRANCH_EVENT, &se_val, SE_SLEEP) != 0) { 3893*7c478bd9Sstevel@tonic-gate sysevent_free_attr(ev_attr_list); 3894*7c478bd9Sstevel@tonic-gate goto fail; 3895*7c478bd9Sstevel@tonic-gate } 3896*7c478bd9Sstevel@tonic-gate } 3897*7c478bd9Sstevel@tonic-gate 3898*7c478bd9Sstevel@tonic-gate if (sysevent_attach_attributes(ev, ev_attr_list) != 0) { 3899*7c478bd9Sstevel@tonic-gate sysevent_free_attr(ev_attr_list); 3900*7c478bd9Sstevel@tonic-gate goto fail; 3901*7c478bd9Sstevel@tonic-gate } 3902*7c478bd9Sstevel@tonic-gate 3903*7c478bd9Sstevel@tonic-gate if ((se_err = log_sysevent(ev, SE_SLEEP, &eid)) != 0) { 3904*7c478bd9Sstevel@tonic-gate if (se_err == SE_NO_TRANSPORT) 3905*7c478bd9Sstevel@tonic-gate no_transport = 1; 3906*7c478bd9Sstevel@tonic-gate goto fail; 3907*7c478bd9Sstevel@tonic-gate } 3908*7c478bd9Sstevel@tonic-gate 3909*7c478bd9Sstevel@tonic-gate sysevent_free(ev); 3910*7c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 3911*7c478bd9Sstevel@tonic-gate 3912*7c478bd9Sstevel@tonic-gate fail: 3913*7c478bd9Sstevel@tonic-gate sysevent_free(ev); 3914*7c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "failed to log ESC_DEVFS_DEVI_REMOVE event for %s%s", 3915*7c478bd9Sstevel@tonic-gate pathname, (no_transport) ? " (syseventd not responding)" : ""); 3916*7c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 3917*7c478bd9Sstevel@tonic-gate } 3918*7c478bd9Sstevel@tonic-gate 3919*7c478bd9Sstevel@tonic-gate /* 3920*7c478bd9Sstevel@tonic-gate * log an event that a dev_info branch has been configured or unconfigured. 3921*7c478bd9Sstevel@tonic-gate */ 3922*7c478bd9Sstevel@tonic-gate static int 3923*7c478bd9Sstevel@tonic-gate i_log_devfs_branch(char *node_path, char *subclass) 3924*7c478bd9Sstevel@tonic-gate { 3925*7c478bd9Sstevel@tonic-gate int se_err; 3926*7c478bd9Sstevel@tonic-gate sysevent_t *ev; 3927*7c478bd9Sstevel@tonic-gate sysevent_id_t eid; 3928*7c478bd9Sstevel@tonic-gate sysevent_value_t se_val; 3929*7c478bd9Sstevel@tonic-gate sysevent_attr_list_t *ev_attr_list = NULL; 3930*7c478bd9Sstevel@tonic-gate int no_transport = 0; 3931*7c478bd9Sstevel@tonic-gate 3932*7c478bd9Sstevel@tonic-gate /* do not generate the event during boot */ 3933*7c478bd9Sstevel@tonic-gate if (!i_ddi_io_initialized()) 3934*7c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 3935*7c478bd9Sstevel@tonic-gate 3936*7c478bd9Sstevel@tonic-gate ev = sysevent_alloc(EC_DEVFS, subclass, EP_DDI, SE_SLEEP); 3937*7c478bd9Sstevel@tonic-gate 3938*7c478bd9Sstevel@tonic-gate se_val.value_type = SE_DATA_TYPE_STRING; 3939*7c478bd9Sstevel@tonic-gate se_val.value.sv_string = node_path; 3940*7c478bd9Sstevel@tonic-gate 3941*7c478bd9Sstevel@tonic-gate if (sysevent_add_attr(&ev_attr_list, DEVFS_PATHNAME, 3942*7c478bd9Sstevel@tonic-gate &se_val, SE_SLEEP) != 0) { 3943*7c478bd9Sstevel@tonic-gate goto fail; 3944*7c478bd9Sstevel@tonic-gate } 3945*7c478bd9Sstevel@tonic-gate 3946*7c478bd9Sstevel@tonic-gate if (sysevent_attach_attributes(ev, ev_attr_list) != 0) { 3947*7c478bd9Sstevel@tonic-gate sysevent_free_attr(ev_attr_list); 3948*7c478bd9Sstevel@tonic-gate goto fail; 3949*7c478bd9Sstevel@tonic-gate } 3950*7c478bd9Sstevel@tonic-gate 3951*7c478bd9Sstevel@tonic-gate if ((se_err = log_sysevent(ev, SE_SLEEP, &eid)) != 0) { 3952*7c478bd9Sstevel@tonic-gate if (se_err == SE_NO_TRANSPORT) 3953*7c478bd9Sstevel@tonic-gate no_transport = 1; 3954*7c478bd9Sstevel@tonic-gate goto fail; 3955*7c478bd9Sstevel@tonic-gate } 3956*7c478bd9Sstevel@tonic-gate 3957*7c478bd9Sstevel@tonic-gate sysevent_free(ev); 3958*7c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 3959*7c478bd9Sstevel@tonic-gate 3960*7c478bd9Sstevel@tonic-gate fail: 3961*7c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "failed to log %s branch event for %s%s", 3962*7c478bd9Sstevel@tonic-gate subclass, node_path, 3963*7c478bd9Sstevel@tonic-gate (no_transport) ? " (syseventd not responding)" : ""); 3964*7c478bd9Sstevel@tonic-gate 3965*7c478bd9Sstevel@tonic-gate sysevent_free(ev); 3966*7c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 3967*7c478bd9Sstevel@tonic-gate } 3968*7c478bd9Sstevel@tonic-gate 3969*7c478bd9Sstevel@tonic-gate /* 3970*7c478bd9Sstevel@tonic-gate * log an event that a dev_info tree branch has been configured. 3971*7c478bd9Sstevel@tonic-gate */ 3972*7c478bd9Sstevel@tonic-gate static int 3973*7c478bd9Sstevel@tonic-gate i_log_devfs_branch_add(dev_info_t *dip) 3974*7c478bd9Sstevel@tonic-gate { 3975*7c478bd9Sstevel@tonic-gate char *node_path; 3976*7c478bd9Sstevel@tonic-gate int rv; 3977*7c478bd9Sstevel@tonic-gate 3978*7c478bd9Sstevel@tonic-gate node_path = kmem_alloc(MAXPATHLEN, KM_SLEEP); 3979*7c478bd9Sstevel@tonic-gate (void) ddi_pathname(dip, node_path); 3980*7c478bd9Sstevel@tonic-gate rv = i_log_devfs_branch(node_path, ESC_DEVFS_BRANCH_ADD); 3981*7c478bd9Sstevel@tonic-gate kmem_free(node_path, MAXPATHLEN); 3982*7c478bd9Sstevel@tonic-gate 3983*7c478bd9Sstevel@tonic-gate return (rv); 3984*7c478bd9Sstevel@tonic-gate } 3985*7c478bd9Sstevel@tonic-gate 3986*7c478bd9Sstevel@tonic-gate /* 3987*7c478bd9Sstevel@tonic-gate * log an event that a dev_info tree branch has been unconfigured. 3988*7c478bd9Sstevel@tonic-gate */ 3989*7c478bd9Sstevel@tonic-gate static int 3990*7c478bd9Sstevel@tonic-gate i_log_devfs_branch_remove(char *node_path) 3991*7c478bd9Sstevel@tonic-gate { 3992*7c478bd9Sstevel@tonic-gate return (i_log_devfs_branch(node_path, ESC_DEVFS_BRANCH_REMOVE)); 3993*7c478bd9Sstevel@tonic-gate } 3994*7c478bd9Sstevel@tonic-gate 3995*7c478bd9Sstevel@tonic-gate /* 3996*7c478bd9Sstevel@tonic-gate * enqueue the dip's deviname on the branch event queue. 3997*7c478bd9Sstevel@tonic-gate */ 3998*7c478bd9Sstevel@tonic-gate static struct brevq_node * 3999*7c478bd9Sstevel@tonic-gate brevq_enqueue(struct brevq_node **brevqp, dev_info_t *dip, 4000*7c478bd9Sstevel@tonic-gate struct brevq_node *child) 4001*7c478bd9Sstevel@tonic-gate { 4002*7c478bd9Sstevel@tonic-gate struct brevq_node *brn; 4003*7c478bd9Sstevel@tonic-gate char *deviname; 4004*7c478bd9Sstevel@tonic-gate 4005*7c478bd9Sstevel@tonic-gate deviname = kmem_alloc(MAXNAMELEN, KM_SLEEP); 4006*7c478bd9Sstevel@tonic-gate (void) ddi_deviname(dip, deviname); 4007*7c478bd9Sstevel@tonic-gate 4008*7c478bd9Sstevel@tonic-gate brn = kmem_zalloc(sizeof (*brn), KM_SLEEP); 4009*7c478bd9Sstevel@tonic-gate brn->deviname = i_ddi_strdup(deviname, KM_SLEEP); 4010*7c478bd9Sstevel@tonic-gate kmem_free(deviname, MAXNAMELEN); 4011*7c478bd9Sstevel@tonic-gate brn->child = child; 4012*7c478bd9Sstevel@tonic-gate brn->sibling = *brevqp; 4013*7c478bd9Sstevel@tonic-gate *brevqp = brn; 4014*7c478bd9Sstevel@tonic-gate 4015*7c478bd9Sstevel@tonic-gate return (brn); 4016*7c478bd9Sstevel@tonic-gate } 4017*7c478bd9Sstevel@tonic-gate 4018*7c478bd9Sstevel@tonic-gate /* 4019*7c478bd9Sstevel@tonic-gate * free the memory allocated for the elements on the branch event queue. 4020*7c478bd9Sstevel@tonic-gate */ 4021*7c478bd9Sstevel@tonic-gate static void 4022*7c478bd9Sstevel@tonic-gate free_brevq(struct brevq_node *brevq) 4023*7c478bd9Sstevel@tonic-gate { 4024*7c478bd9Sstevel@tonic-gate struct brevq_node *brn, *next_brn; 4025*7c478bd9Sstevel@tonic-gate 4026*7c478bd9Sstevel@tonic-gate for (brn = brevq; brn != NULL; brn = next_brn) { 4027*7c478bd9Sstevel@tonic-gate next_brn = brn->sibling; 4028*7c478bd9Sstevel@tonic-gate ASSERT(brn->child == NULL); 4029*7c478bd9Sstevel@tonic-gate kmem_free(brn->deviname, strlen(brn->deviname) + 1); 4030*7c478bd9Sstevel@tonic-gate kmem_free(brn, sizeof (*brn)); 4031*7c478bd9Sstevel@tonic-gate } 4032*7c478bd9Sstevel@tonic-gate } 4033*7c478bd9Sstevel@tonic-gate 4034*7c478bd9Sstevel@tonic-gate /* 4035*7c478bd9Sstevel@tonic-gate * log the events queued up on the branch event queue and free the 4036*7c478bd9Sstevel@tonic-gate * associated memory. 4037*7c478bd9Sstevel@tonic-gate * 4038*7c478bd9Sstevel@tonic-gate * node_path must have been allocated with at least MAXPATHLEN bytes. 4039*7c478bd9Sstevel@tonic-gate */ 4040*7c478bd9Sstevel@tonic-gate static void 4041*7c478bd9Sstevel@tonic-gate log_and_free_brevq(char *node_path, struct brevq_node *brevq) 4042*7c478bd9Sstevel@tonic-gate { 4043*7c478bd9Sstevel@tonic-gate struct brevq_node *brn; 4044*7c478bd9Sstevel@tonic-gate char *p; 4045*7c478bd9Sstevel@tonic-gate 4046*7c478bd9Sstevel@tonic-gate p = node_path + strlen(node_path); 4047*7c478bd9Sstevel@tonic-gate for (brn = brevq; brn != NULL; brn = brn->sibling) { 4048*7c478bd9Sstevel@tonic-gate (void) strcpy(p, brn->deviname); 4049*7c478bd9Sstevel@tonic-gate (void) i_log_devfs_branch_remove(node_path); 4050*7c478bd9Sstevel@tonic-gate } 4051*7c478bd9Sstevel@tonic-gate *p = '\0'; 4052*7c478bd9Sstevel@tonic-gate 4053*7c478bd9Sstevel@tonic-gate free_brevq(brevq); 4054*7c478bd9Sstevel@tonic-gate } 4055*7c478bd9Sstevel@tonic-gate 4056*7c478bd9Sstevel@tonic-gate /* 4057*7c478bd9Sstevel@tonic-gate * log the events queued up on the branch event queue and free the 4058*7c478bd9Sstevel@tonic-gate * associated memory. Same as the previous function but operates on dip. 4059*7c478bd9Sstevel@tonic-gate */ 4060*7c478bd9Sstevel@tonic-gate static void 4061*7c478bd9Sstevel@tonic-gate log_and_free_brevq_dip(dev_info_t *dip, struct brevq_node *brevq) 4062*7c478bd9Sstevel@tonic-gate { 4063*7c478bd9Sstevel@tonic-gate char *path; 4064*7c478bd9Sstevel@tonic-gate 4065*7c478bd9Sstevel@tonic-gate path = kmem_alloc(MAXPATHLEN, KM_SLEEP); 4066*7c478bd9Sstevel@tonic-gate (void) ddi_pathname(dip, path); 4067*7c478bd9Sstevel@tonic-gate log_and_free_brevq(path, brevq); 4068*7c478bd9Sstevel@tonic-gate kmem_free(path, MAXPATHLEN); 4069*7c478bd9Sstevel@tonic-gate } 4070*7c478bd9Sstevel@tonic-gate 4071*7c478bd9Sstevel@tonic-gate /* 4072*7c478bd9Sstevel@tonic-gate * log the outstanding branch remove events for the grand children of the dip 4073*7c478bd9Sstevel@tonic-gate * and free the associated memory. 4074*7c478bd9Sstevel@tonic-gate */ 4075*7c478bd9Sstevel@tonic-gate static void 4076*7c478bd9Sstevel@tonic-gate log_and_free_br_events_on_grand_children(dev_info_t *dip, 4077*7c478bd9Sstevel@tonic-gate struct brevq_node *brevq) 4078*7c478bd9Sstevel@tonic-gate { 4079*7c478bd9Sstevel@tonic-gate struct brevq_node *brn; 4080*7c478bd9Sstevel@tonic-gate char *path; 4081*7c478bd9Sstevel@tonic-gate char *p; 4082*7c478bd9Sstevel@tonic-gate 4083*7c478bd9Sstevel@tonic-gate path = kmem_alloc(MAXPATHLEN, KM_SLEEP); 4084*7c478bd9Sstevel@tonic-gate (void) ddi_pathname(dip, path); 4085*7c478bd9Sstevel@tonic-gate p = path + strlen(path); 4086*7c478bd9Sstevel@tonic-gate for (brn = brevq; brn != NULL; brn = brn->sibling) { 4087*7c478bd9Sstevel@tonic-gate if (brn->child) { 4088*7c478bd9Sstevel@tonic-gate (void) strcpy(p, brn->deviname); 4089*7c478bd9Sstevel@tonic-gate /* now path contains the node path to the dip's child */ 4090*7c478bd9Sstevel@tonic-gate log_and_free_brevq(path, brn->child); 4091*7c478bd9Sstevel@tonic-gate brn->child = NULL; 4092*7c478bd9Sstevel@tonic-gate } 4093*7c478bd9Sstevel@tonic-gate } 4094*7c478bd9Sstevel@tonic-gate kmem_free(path, MAXPATHLEN); 4095*7c478bd9Sstevel@tonic-gate } 4096*7c478bd9Sstevel@tonic-gate 4097*7c478bd9Sstevel@tonic-gate /* 4098*7c478bd9Sstevel@tonic-gate * log and cleanup branch remove events for the grand children of the dip. 4099*7c478bd9Sstevel@tonic-gate */ 4100*7c478bd9Sstevel@tonic-gate static void 4101*7c478bd9Sstevel@tonic-gate cleanup_br_events_on_grand_children(dev_info_t *dip, struct brevq_node **brevqp) 4102*7c478bd9Sstevel@tonic-gate { 4103*7c478bd9Sstevel@tonic-gate dev_info_t *child; 4104*7c478bd9Sstevel@tonic-gate struct brevq_node *brevq, *brn, *prev_brn, *next_brn; 4105*7c478bd9Sstevel@tonic-gate char *path; 4106*7c478bd9Sstevel@tonic-gate int circ; 4107*7c478bd9Sstevel@tonic-gate 4108*7c478bd9Sstevel@tonic-gate path = kmem_alloc(MAXPATHLEN, KM_SLEEP); 4109*7c478bd9Sstevel@tonic-gate prev_brn = NULL; 4110*7c478bd9Sstevel@tonic-gate brevq = *brevqp; 4111*7c478bd9Sstevel@tonic-gate 4112*7c478bd9Sstevel@tonic-gate ndi_devi_enter(dip, &circ); 4113*7c478bd9Sstevel@tonic-gate for (brn = brevq; brn != NULL; brn = next_brn) { 4114*7c478bd9Sstevel@tonic-gate next_brn = brn->sibling; 4115*7c478bd9Sstevel@tonic-gate for (child = ddi_get_child(dip); child != NULL; 4116*7c478bd9Sstevel@tonic-gate child = ddi_get_next_sibling(child)) { 4117*7c478bd9Sstevel@tonic-gate if (i_ddi_node_state(child) >= DS_INITIALIZED) { 4118*7c478bd9Sstevel@tonic-gate (void) ddi_deviname(child, path); 4119*7c478bd9Sstevel@tonic-gate if (strcmp(path, brn->deviname) == 0) 4120*7c478bd9Sstevel@tonic-gate break; 4121*7c478bd9Sstevel@tonic-gate } 4122*7c478bd9Sstevel@tonic-gate } 4123*7c478bd9Sstevel@tonic-gate 4124*7c478bd9Sstevel@tonic-gate if (child != NULL && !(DEVI_EVREMOVE(child))) { 4125*7c478bd9Sstevel@tonic-gate /* 4126*7c478bd9Sstevel@tonic-gate * Event state is not REMOVE. So branch remove event 4127*7c478bd9Sstevel@tonic-gate * is not going be generated on brn->child. 4128*7c478bd9Sstevel@tonic-gate * If any branch remove events were queued up on 4129*7c478bd9Sstevel@tonic-gate * brn->child log them and remove the brn 4130*7c478bd9Sstevel@tonic-gate * from the queue. 4131*7c478bd9Sstevel@tonic-gate */ 4132*7c478bd9Sstevel@tonic-gate if (brn->child) { 4133*7c478bd9Sstevel@tonic-gate (void) ddi_pathname(dip, path); 4134*7c478bd9Sstevel@tonic-gate (void) strcat(path, brn->deviname); 4135*7c478bd9Sstevel@tonic-gate log_and_free_brevq(path, brn->child); 4136*7c478bd9Sstevel@tonic-gate } 4137*7c478bd9Sstevel@tonic-gate 4138*7c478bd9Sstevel@tonic-gate if (prev_brn) 4139*7c478bd9Sstevel@tonic-gate prev_brn->sibling = next_brn; 4140*7c478bd9Sstevel@tonic-gate else 4141*7c478bd9Sstevel@tonic-gate *brevqp = next_brn; 4142*7c478bd9Sstevel@tonic-gate 4143*7c478bd9Sstevel@tonic-gate kmem_free(brn->deviname, strlen(brn->deviname) + 1); 4144*7c478bd9Sstevel@tonic-gate kmem_free(brn, sizeof (*brn)); 4145*7c478bd9Sstevel@tonic-gate } else { 4146*7c478bd9Sstevel@tonic-gate /* 4147*7c478bd9Sstevel@tonic-gate * Free up the outstanding branch remove events 4148*7c478bd9Sstevel@tonic-gate * queued on brn->child since brn->child 4149*7c478bd9Sstevel@tonic-gate * itself is eligible for branch remove event. 4150*7c478bd9Sstevel@tonic-gate */ 4151*7c478bd9Sstevel@tonic-gate if (brn->child) { 4152*7c478bd9Sstevel@tonic-gate free_brevq(brn->child); 4153*7c478bd9Sstevel@tonic-gate brn->child = NULL; 4154*7c478bd9Sstevel@tonic-gate } 4155*7c478bd9Sstevel@tonic-gate prev_brn = brn; 4156*7c478bd9Sstevel@tonic-gate } 4157*7c478bd9Sstevel@tonic-gate } 4158*7c478bd9Sstevel@tonic-gate 4159*7c478bd9Sstevel@tonic-gate ndi_devi_exit(dip, circ); 4160*7c478bd9Sstevel@tonic-gate kmem_free(path, MAXPATHLEN); 4161*7c478bd9Sstevel@tonic-gate } 4162*7c478bd9Sstevel@tonic-gate 4163*7c478bd9Sstevel@tonic-gate static int 4164*7c478bd9Sstevel@tonic-gate need_remove_event(dev_info_t *dip, int flags) 4165*7c478bd9Sstevel@tonic-gate { 4166*7c478bd9Sstevel@tonic-gate if ((flags & (NDI_NO_EVENT | NDI_AUTODETACH)) == 0 && 4167*7c478bd9Sstevel@tonic-gate (flags & (NDI_DEVI_OFFLINE | NDI_UNCONFIG | NDI_DEVI_REMOVE)) && 4168*7c478bd9Sstevel@tonic-gate !(DEVI_EVREMOVE(dip))) 4169*7c478bd9Sstevel@tonic-gate return (1); 4170*7c478bd9Sstevel@tonic-gate else 4171*7c478bd9Sstevel@tonic-gate return (0); 4172*7c478bd9Sstevel@tonic-gate } 4173*7c478bd9Sstevel@tonic-gate 4174*7c478bd9Sstevel@tonic-gate /* 4175*7c478bd9Sstevel@tonic-gate * Unconfigure children/descendants of the dip. 4176*7c478bd9Sstevel@tonic-gate * 4177*7c478bd9Sstevel@tonic-gate * If the operation involves a branch event NDI_BRANCH_EVENT_OP is set 4178*7c478bd9Sstevel@tonic-gate * through out the unconfiguration. On successful return *brevqp is set to 4179*7c478bd9Sstevel@tonic-gate * a queue of dip's child devinames for which branch remove events need 4180*7c478bd9Sstevel@tonic-gate * to be generated. 4181*7c478bd9Sstevel@tonic-gate */ 4182*7c478bd9Sstevel@tonic-gate static int 4183*7c478bd9Sstevel@tonic-gate devi_unconfig_branch(dev_info_t *dip, dev_info_t **dipp, int flags, 4184*7c478bd9Sstevel@tonic-gate struct brevq_node **brevqp) 4185*7c478bd9Sstevel@tonic-gate { 4186*7c478bd9Sstevel@tonic-gate int rval; 4187*7c478bd9Sstevel@tonic-gate 4188*7c478bd9Sstevel@tonic-gate *brevqp = NULL; 4189*7c478bd9Sstevel@tonic-gate 4190*7c478bd9Sstevel@tonic-gate if ((!(flags & NDI_BRANCH_EVENT_OP)) && need_remove_event(dip, flags)) 4191*7c478bd9Sstevel@tonic-gate flags |= NDI_BRANCH_EVENT_OP; 4192*7c478bd9Sstevel@tonic-gate 4193*7c478bd9Sstevel@tonic-gate if (flags & NDI_BRANCH_EVENT_OP) { 4194*7c478bd9Sstevel@tonic-gate rval = devi_unconfig_common(dip, dipp, flags, (major_t)-1, 4195*7c478bd9Sstevel@tonic-gate brevqp); 4196*7c478bd9Sstevel@tonic-gate 4197*7c478bd9Sstevel@tonic-gate if (rval != NDI_SUCCESS && (*brevqp)) { 4198*7c478bd9Sstevel@tonic-gate log_and_free_brevq_dip(dip, *brevqp); 4199*7c478bd9Sstevel@tonic-gate *brevqp = NULL; 4200*7c478bd9Sstevel@tonic-gate } 4201*7c478bd9Sstevel@tonic-gate } else 4202*7c478bd9Sstevel@tonic-gate rval = devi_unconfig_common(dip, dipp, flags, (major_t)-1, 4203*7c478bd9Sstevel@tonic-gate NULL); 4204*7c478bd9Sstevel@tonic-gate 4205*7c478bd9Sstevel@tonic-gate return (rval); 4206*7c478bd9Sstevel@tonic-gate } 4207*7c478bd9Sstevel@tonic-gate 4208*7c478bd9Sstevel@tonic-gate /* 4209*7c478bd9Sstevel@tonic-gate * If the dip is already bound to a driver transition to DS_INITIALIZED 4210*7c478bd9Sstevel@tonic-gate * in order to generate an event in the case where the node was left in 4211*7c478bd9Sstevel@tonic-gate * DS_BOUND state since boot (never got attached) and the node is now 4212*7c478bd9Sstevel@tonic-gate * being offlined. 4213*7c478bd9Sstevel@tonic-gate */ 4214*7c478bd9Sstevel@tonic-gate static void 4215*7c478bd9Sstevel@tonic-gate init_bound_node_ev(dev_info_t *pdip, dev_info_t *dip, int flags) 4216*7c478bd9Sstevel@tonic-gate { 4217*7c478bd9Sstevel@tonic-gate if (need_remove_event(dip, flags) && 4218*7c478bd9Sstevel@tonic-gate i_ddi_node_state(dip) == DS_BOUND && 4219*7c478bd9Sstevel@tonic-gate i_ddi_node_state(pdip) >= DS_ATTACHED && 4220*7c478bd9Sstevel@tonic-gate !(DEVI_IS_DEVICE_OFFLINE(dip))) 4221*7c478bd9Sstevel@tonic-gate (void) ddi_initchild(pdip, dip); 4222*7c478bd9Sstevel@tonic-gate } 4223*7c478bd9Sstevel@tonic-gate 4224*7c478bd9Sstevel@tonic-gate /* 4225*7c478bd9Sstevel@tonic-gate * attach a node/branch with parent already held busy 4226*7c478bd9Sstevel@tonic-gate */ 4227*7c478bd9Sstevel@tonic-gate static int 4228*7c478bd9Sstevel@tonic-gate devi_attach_node(dev_info_t *dip, uint_t flags) 4229*7c478bd9Sstevel@tonic-gate { 4230*7c478bd9Sstevel@tonic-gate if (flags & NDI_DEVI_ONLINE) { 4231*7c478bd9Sstevel@tonic-gate DEVI_SET_DEVICE_ONLINE(dip); 4232*7c478bd9Sstevel@tonic-gate } 4233*7c478bd9Sstevel@tonic-gate 4234*7c478bd9Sstevel@tonic-gate if (DEVI_IS_DEVICE_OFFLINE(dip)) { 4235*7c478bd9Sstevel@tonic-gate return (NDI_FAILURE); 4236*7c478bd9Sstevel@tonic-gate } 4237*7c478bd9Sstevel@tonic-gate 4238*7c478bd9Sstevel@tonic-gate if (i_ddi_attachchild(dip) != DDI_SUCCESS) { 4239*7c478bd9Sstevel@tonic-gate DEVI_SET_EVUNINIT(dip); 4240*7c478bd9Sstevel@tonic-gate if (ndi_dev_is_persistent_node(dip)) 4241*7c478bd9Sstevel@tonic-gate (void) ddi_uninitchild(dip); 4242*7c478bd9Sstevel@tonic-gate else { 4243*7c478bd9Sstevel@tonic-gate /* 4244*7c478bd9Sstevel@tonic-gate * Delete .conf nodes and nodes that are not 4245*7c478bd9Sstevel@tonic-gate * well formed. 4246*7c478bd9Sstevel@tonic-gate */ 4247*7c478bd9Sstevel@tonic-gate (void) ddi_remove_child(dip, 0); 4248*7c478bd9Sstevel@tonic-gate } 4249*7c478bd9Sstevel@tonic-gate return (NDI_FAILURE); 4250*7c478bd9Sstevel@tonic-gate } 4251*7c478bd9Sstevel@tonic-gate 4252*7c478bd9Sstevel@tonic-gate i_ndi_devi_report_status_change(dip, NULL); 4253*7c478bd9Sstevel@tonic-gate 4254*7c478bd9Sstevel@tonic-gate /* 4255*7c478bd9Sstevel@tonic-gate * log an event, but not during devfs lookups in which case 4256*7c478bd9Sstevel@tonic-gate * NDI_NO_EVENT is set. 4257*7c478bd9Sstevel@tonic-gate */ 4258*7c478bd9Sstevel@tonic-gate if ((flags & NDI_NO_EVENT) == 0 && !(DEVI_EVADD(dip))) { 4259*7c478bd9Sstevel@tonic-gate (void) i_log_devfs_add_devinfo(dip, flags); 4260*7c478bd9Sstevel@tonic-gate DEVI_SET_EVADD(dip); 4261*7c478bd9Sstevel@tonic-gate } else if (!(flags & NDI_NO_EVENT_STATE_CHNG)) 4262*7c478bd9Sstevel@tonic-gate DEVI_SET_EVADD(dip); 4263*7c478bd9Sstevel@tonic-gate 4264*7c478bd9Sstevel@tonic-gate return (NDI_SUCCESS); 4265*7c478bd9Sstevel@tonic-gate } 4266*7c478bd9Sstevel@tonic-gate 4267*7c478bd9Sstevel@tonic-gate /* 4268*7c478bd9Sstevel@tonic-gate * Configure all children of a nexus, assuming all spec children have 4269*7c478bd9Sstevel@tonic-gate * been made. 4270*7c478bd9Sstevel@tonic-gate */ 4271*7c478bd9Sstevel@tonic-gate static int 4272*7c478bd9Sstevel@tonic-gate devi_attach_children(dev_info_t *pdip, uint_t flags, major_t major) 4273*7c478bd9Sstevel@tonic-gate { 4274*7c478bd9Sstevel@tonic-gate dev_info_t *dip; 4275*7c478bd9Sstevel@tonic-gate 4276*7c478bd9Sstevel@tonic-gate ASSERT(DEVI(pdip)->devi_flags & DEVI_MADE_CHILDREN); 4277*7c478bd9Sstevel@tonic-gate 4278*7c478bd9Sstevel@tonic-gate dip = ddi_get_child(pdip); 4279*7c478bd9Sstevel@tonic-gate while (dip) { 4280*7c478bd9Sstevel@tonic-gate /* 4281*7c478bd9Sstevel@tonic-gate * NOTE: devi_attach_node() may remove the dip 4282*7c478bd9Sstevel@tonic-gate */ 4283*7c478bd9Sstevel@tonic-gate dev_info_t *next = ddi_get_next_sibling(dip); 4284*7c478bd9Sstevel@tonic-gate 4285*7c478bd9Sstevel@tonic-gate /* 4286*7c478bd9Sstevel@tonic-gate * Configure all nexus nodes or leaf nodes with 4287*7c478bd9Sstevel@tonic-gate * matching driver major 4288*7c478bd9Sstevel@tonic-gate */ 4289*7c478bd9Sstevel@tonic-gate if ((major == (major_t)-1) || 4290*7c478bd9Sstevel@tonic-gate (major == ddi_driver_major(dip)) || 4291*7c478bd9Sstevel@tonic-gate ((flags & NDI_CONFIG) && (is_leaf_node(dip) == 0))) 4292*7c478bd9Sstevel@tonic-gate (void) devi_attach_node(dip, flags); 4293*7c478bd9Sstevel@tonic-gate dip = next; 4294*7c478bd9Sstevel@tonic-gate } 4295*7c478bd9Sstevel@tonic-gate 4296*7c478bd9Sstevel@tonic-gate return (NDI_SUCCESS); 4297*7c478bd9Sstevel@tonic-gate } 4298*7c478bd9Sstevel@tonic-gate 4299*7c478bd9Sstevel@tonic-gate /* internal function to config immediate children */ 4300*7c478bd9Sstevel@tonic-gate static int 4301*7c478bd9Sstevel@tonic-gate config_immediate_children(dev_info_t *pdip, uint_t flags, major_t major) 4302*7c478bd9Sstevel@tonic-gate { 4303*7c478bd9Sstevel@tonic-gate int circ; 4304*7c478bd9Sstevel@tonic-gate ASSERT(i_ddi_node_state(pdip) >= DS_ATTACHED); 4305*7c478bd9Sstevel@tonic-gate 4306*7c478bd9Sstevel@tonic-gate if (!NEXUS_DRV(ddi_get_driver(pdip))) 4307*7c478bd9Sstevel@tonic-gate return (NDI_SUCCESS); 4308*7c478bd9Sstevel@tonic-gate 4309*7c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_CONT, 4310*7c478bd9Sstevel@tonic-gate "config_immediate_children: %s%d (%p), flags=%x\n", 4311*7c478bd9Sstevel@tonic-gate ddi_driver_name(pdip), ddi_get_instance(pdip), 4312*7c478bd9Sstevel@tonic-gate (void *)pdip, flags)); 4313*7c478bd9Sstevel@tonic-gate 4314*7c478bd9Sstevel@tonic-gate ndi_devi_enter(pdip, &circ); 4315*7c478bd9Sstevel@tonic-gate 4316*7c478bd9Sstevel@tonic-gate if (flags & NDI_CONFIG_REPROBE) { 4317*7c478bd9Sstevel@tonic-gate mutex_enter(&DEVI(pdip)->devi_lock); 4318*7c478bd9Sstevel@tonic-gate DEVI(pdip)->devi_flags &= ~DEVI_MADE_CHILDREN; 4319*7c478bd9Sstevel@tonic-gate mutex_exit(&DEVI(pdip)->devi_lock); 4320*7c478bd9Sstevel@tonic-gate } 4321*7c478bd9Sstevel@tonic-gate (void) i_ndi_make_spec_children(pdip, flags); 4322*7c478bd9Sstevel@tonic-gate i_ndi_init_hw_children(pdip, flags); 4323*7c478bd9Sstevel@tonic-gate (void) devi_attach_children(pdip, flags, major); 4324*7c478bd9Sstevel@tonic-gate 4325*7c478bd9Sstevel@tonic-gate ndi_devi_exit(pdip, circ); 4326*7c478bd9Sstevel@tonic-gate 4327*7c478bd9Sstevel@tonic-gate return (NDI_SUCCESS); 4328*7c478bd9Sstevel@tonic-gate } 4329*7c478bd9Sstevel@tonic-gate 4330*7c478bd9Sstevel@tonic-gate /* internal function to config grand children */ 4331*7c478bd9Sstevel@tonic-gate static int 4332*7c478bd9Sstevel@tonic-gate config_grand_children(dev_info_t *pdip, uint_t flags, major_t major) 4333*7c478bd9Sstevel@tonic-gate { 4334*7c478bd9Sstevel@tonic-gate struct mt_config_handle *hdl; 4335*7c478bd9Sstevel@tonic-gate 4336*7c478bd9Sstevel@tonic-gate /* multi-threaded configuration of child nexus */ 4337*7c478bd9Sstevel@tonic-gate hdl = mt_config_init(pdip, NULL, flags, major, MT_CONFIG_OP, NULL); 4338*7c478bd9Sstevel@tonic-gate mt_config_children(hdl); 4339*7c478bd9Sstevel@tonic-gate 4340*7c478bd9Sstevel@tonic-gate return (mt_config_fini(hdl)); /* wait for threads to exit */ 4341*7c478bd9Sstevel@tonic-gate } 4342*7c478bd9Sstevel@tonic-gate 4343*7c478bd9Sstevel@tonic-gate /* 4344*7c478bd9Sstevel@tonic-gate * Common function for device tree configuration, 4345*7c478bd9Sstevel@tonic-gate * either BUS_CONFIG_ALL or BUS_CONFIG_DRIVER. 4346*7c478bd9Sstevel@tonic-gate * The NDI_CONFIG flag causes recursive configuration of 4347*7c478bd9Sstevel@tonic-gate * grandchildren, devfs usage should not recurse. 4348*7c478bd9Sstevel@tonic-gate */ 4349*7c478bd9Sstevel@tonic-gate static int 4350*7c478bd9Sstevel@tonic-gate devi_config_common(dev_info_t *dip, int flags, major_t major) 4351*7c478bd9Sstevel@tonic-gate { 4352*7c478bd9Sstevel@tonic-gate int error; 4353*7c478bd9Sstevel@tonic-gate int (*f)(); 4354*7c478bd9Sstevel@tonic-gate 4355*7c478bd9Sstevel@tonic-gate if (i_ddi_node_state(dip) < DS_READY) 4356*7c478bd9Sstevel@tonic-gate return (NDI_FAILURE); 4357*7c478bd9Sstevel@tonic-gate 4358*7c478bd9Sstevel@tonic-gate if (pm_pre_config(dip, NULL) != DDI_SUCCESS) 4359*7c478bd9Sstevel@tonic-gate return (NDI_FAILURE); 4360*7c478bd9Sstevel@tonic-gate 4361*7c478bd9Sstevel@tonic-gate if ((DEVI(dip)->devi_ops->devo_bus_ops == NULL) || 4362*7c478bd9Sstevel@tonic-gate (DEVI(dip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) || 4363*7c478bd9Sstevel@tonic-gate (f = DEVI(dip)->devi_ops->devo_bus_ops->bus_config) == NULL) { 4364*7c478bd9Sstevel@tonic-gate error = config_immediate_children(dip, flags, major); 4365*7c478bd9Sstevel@tonic-gate } else { 4366*7c478bd9Sstevel@tonic-gate /* call bus_config entry point */ 4367*7c478bd9Sstevel@tonic-gate ddi_bus_config_op_t bus_op = (major == (major_t)-1) ? 4368*7c478bd9Sstevel@tonic-gate BUS_CONFIG_ALL : BUS_CONFIG_DRIVER; 4369*7c478bd9Sstevel@tonic-gate error = (*f)(dip, 4370*7c478bd9Sstevel@tonic-gate flags, bus_op, (void *)(uintptr_t)major, NULL, 0); 4371*7c478bd9Sstevel@tonic-gate } 4372*7c478bd9Sstevel@tonic-gate 4373*7c478bd9Sstevel@tonic-gate if (error) { 4374*7c478bd9Sstevel@tonic-gate pm_post_config(dip, NULL); 4375*7c478bd9Sstevel@tonic-gate return (error); 4376*7c478bd9Sstevel@tonic-gate } 4377*7c478bd9Sstevel@tonic-gate 4378*7c478bd9Sstevel@tonic-gate /* 4379*7c478bd9Sstevel@tonic-gate * Some callers, notably SCSI, need to mark the devfs cache 4380*7c478bd9Sstevel@tonic-gate * to be rebuilt together with the config operation. 4381*7c478bd9Sstevel@tonic-gate */ 4382*7c478bd9Sstevel@tonic-gate if (flags & NDI_DEVFS_CLEAN) 4383*7c478bd9Sstevel@tonic-gate (void) devfs_clean(dip, NULL, 0); 4384*7c478bd9Sstevel@tonic-gate 4385*7c478bd9Sstevel@tonic-gate if (flags & NDI_CONFIG) 4386*7c478bd9Sstevel@tonic-gate (void) config_grand_children(dip, flags, major); 4387*7c478bd9Sstevel@tonic-gate 4388*7c478bd9Sstevel@tonic-gate pm_post_config(dip, NULL); 4389*7c478bd9Sstevel@tonic-gate 4390*7c478bd9Sstevel@tonic-gate return (NDI_SUCCESS); 4391*7c478bd9Sstevel@tonic-gate } 4392*7c478bd9Sstevel@tonic-gate 4393*7c478bd9Sstevel@tonic-gate /* 4394*7c478bd9Sstevel@tonic-gate * Framework entry point for BUS_CONFIG_ALL 4395*7c478bd9Sstevel@tonic-gate */ 4396*7c478bd9Sstevel@tonic-gate int 4397*7c478bd9Sstevel@tonic-gate ndi_devi_config(dev_info_t *dip, int flags) 4398*7c478bd9Sstevel@tonic-gate { 4399*7c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_CONT, 4400*7c478bd9Sstevel@tonic-gate "ndi_devi_config: par = %s%d (%p), flags = 0x%x\n", 4401*7c478bd9Sstevel@tonic-gate ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags)); 4402*7c478bd9Sstevel@tonic-gate 4403*7c478bd9Sstevel@tonic-gate return (devi_config_common(dip, flags, (major_t)-1)); 4404*7c478bd9Sstevel@tonic-gate } 4405*7c478bd9Sstevel@tonic-gate 4406*7c478bd9Sstevel@tonic-gate /* 4407*7c478bd9Sstevel@tonic-gate * Framework entry point for BUS_CONFIG_DRIVER, bound to major 4408*7c478bd9Sstevel@tonic-gate */ 4409*7c478bd9Sstevel@tonic-gate int 4410*7c478bd9Sstevel@tonic-gate ndi_devi_config_driver(dev_info_t *dip, int flags, major_t major) 4411*7c478bd9Sstevel@tonic-gate { 4412*7c478bd9Sstevel@tonic-gate /* don't abuse this function */ 4413*7c478bd9Sstevel@tonic-gate ASSERT(major != (major_t)-1); 4414*7c478bd9Sstevel@tonic-gate 4415*7c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_CONT, 4416*7c478bd9Sstevel@tonic-gate "ndi_devi_config_driver: par = %s%d (%p), flags = 0x%x\n", 4417*7c478bd9Sstevel@tonic-gate ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags)); 4418*7c478bd9Sstevel@tonic-gate 4419*7c478bd9Sstevel@tonic-gate return (devi_config_common(dip, flags, major)); 4420*7c478bd9Sstevel@tonic-gate } 4421*7c478bd9Sstevel@tonic-gate 4422*7c478bd9Sstevel@tonic-gate /* 4423*7c478bd9Sstevel@tonic-gate * called by nexus drivers to configure/unconfigure its children 4424*7c478bd9Sstevel@tonic-gate */ 4425*7c478bd9Sstevel@tonic-gate static int 4426*7c478bd9Sstevel@tonic-gate devi_config_one(dev_info_t *pdip, char *devnm, dev_info_t **dipp, 4427*7c478bd9Sstevel@tonic-gate uint_t flags, clock_t timeout) 4428*7c478bd9Sstevel@tonic-gate { 4429*7c478bd9Sstevel@tonic-gate int circ, probed, rv; 4430*7c478bd9Sstevel@tonic-gate dev_info_t *dip = NULL; 4431*7c478bd9Sstevel@tonic-gate char *name, *addr, *drivername = NULL; 4432*7c478bd9Sstevel@tonic-gate clock_t end_time; /* 60 sec */ 4433*7c478bd9Sstevel@tonic-gate 4434*7c478bd9Sstevel@tonic-gate if (!NEXUS_DRV(ddi_get_driver(pdip))) 4435*7c478bd9Sstevel@tonic-gate return (NDI_FAILURE); 4436*7c478bd9Sstevel@tonic-gate 4437*7c478bd9Sstevel@tonic-gate if (MDI_PHCI(pdip)) { 4438*7c478bd9Sstevel@tonic-gate /* Call mdi_ to configure the child */ 4439*7c478bd9Sstevel@tonic-gate rv = mdi_devi_config_one(pdip, devnm, dipp, flags, timeout); 4440*7c478bd9Sstevel@tonic-gate if (rv == MDI_SUCCESS) 4441*7c478bd9Sstevel@tonic-gate return (NDI_SUCCESS); 4442*7c478bd9Sstevel@tonic-gate 4443*7c478bd9Sstevel@tonic-gate /* 4444*7c478bd9Sstevel@tonic-gate * Normally, we should return failure here. 4445*7c478bd9Sstevel@tonic-gate * 4446*7c478bd9Sstevel@tonic-gate * Leadville implemented an unfortunate fallback mechanism. 4447*7c478bd9Sstevel@tonic-gate * If a target is non-standard and scsi_vhci doesn't know 4448*7c478bd9Sstevel@tonic-gate * how to do failover, then the node is enumerated under 4449*7c478bd9Sstevel@tonic-gate * phci. Leadville specifies NDI_MDI_FALLBACK flag to 4450*7c478bd9Sstevel@tonic-gate * maintain the old behavior. 4451*7c478bd9Sstevel@tonic-gate */ 4452*7c478bd9Sstevel@tonic-gate if ((flags & NDI_MDI_FALLBACK) == 0) 4453*7c478bd9Sstevel@tonic-gate return (NDI_FAILURE); 4454*7c478bd9Sstevel@tonic-gate } 4455*7c478bd9Sstevel@tonic-gate 4456*7c478bd9Sstevel@tonic-gate /* split name into "name@addr" parts */ 4457*7c478bd9Sstevel@tonic-gate i_ddi_parse_name(devnm, &name, &addr, NULL); 4458*7c478bd9Sstevel@tonic-gate 4459*7c478bd9Sstevel@tonic-gate if (flags & NDI_PROMNAME) { 4460*7c478bd9Sstevel@tonic-gate /* 4461*7c478bd9Sstevel@tonic-gate * We may have a genericname on a system that creates 4462*7c478bd9Sstevel@tonic-gate * drivername nodes (from .conf files). Find the drivername 4463*7c478bd9Sstevel@tonic-gate * by nodeid. If we can't find a node with devnm as the 4464*7c478bd9Sstevel@tonic-gate * node name then we search by drivername. This allows an 4465*7c478bd9Sstevel@tonic-gate * implementation to supply a genericly named boot path (disk) 4466*7c478bd9Sstevel@tonic-gate * and locate drivename nodes (sd). 4467*7c478bd9Sstevel@tonic-gate */ 4468*7c478bd9Sstevel@tonic-gate drivername = child_path_to_driver(pdip, name, addr); 4469*7c478bd9Sstevel@tonic-gate } 4470*7c478bd9Sstevel@tonic-gate 4471*7c478bd9Sstevel@tonic-gate if (timeout > 0) { 4472*7c478bd9Sstevel@tonic-gate end_time = ddi_get_lbolt() + timeout; 4473*7c478bd9Sstevel@tonic-gate } 4474*7c478bd9Sstevel@tonic-gate 4475*7c478bd9Sstevel@tonic-gate ndi_devi_enter(pdip, &circ); 4476*7c478bd9Sstevel@tonic-gate 4477*7c478bd9Sstevel@tonic-gate reprobe: 4478*7c478bd9Sstevel@tonic-gate probed = (DEVI(pdip)->devi_flags & DEVI_MADE_CHILDREN); 4479*7c478bd9Sstevel@tonic-gate (void) i_ndi_make_spec_children(pdip, flags); 4480*7c478bd9Sstevel@tonic-gate for (;;) { 4481*7c478bd9Sstevel@tonic-gate dip = find_child_by_name(pdip, name, addr); 4482*7c478bd9Sstevel@tonic-gate /* 4483*7c478bd9Sstevel@tonic-gate * Search for a node bound to the drivername driver with 4484*7c478bd9Sstevel@tonic-gate * the specified "@addr". 4485*7c478bd9Sstevel@tonic-gate */ 4486*7c478bd9Sstevel@tonic-gate if (dip == NULL && drivername) 4487*7c478bd9Sstevel@tonic-gate dip = find_child_by_driver(pdip, drivername, addr); 4488*7c478bd9Sstevel@tonic-gate 4489*7c478bd9Sstevel@tonic-gate if (dip || timeout <= 0 || ddi_get_lbolt() >= end_time) 4490*7c478bd9Sstevel@tonic-gate break; 4491*7c478bd9Sstevel@tonic-gate 4492*7c478bd9Sstevel@tonic-gate /* 4493*7c478bd9Sstevel@tonic-gate * Wait up to end_time for asynchronous enumeration 4494*7c478bd9Sstevel@tonic-gate */ 4495*7c478bd9Sstevel@tonic-gate ndi_devi_exit(pdip, circ); 4496*7c478bd9Sstevel@tonic-gate NDI_DEBUG(flags, (CE_CONT, 4497*7c478bd9Sstevel@tonic-gate "%s%d: waiting for child %s@%s, timeout %ld", 4498*7c478bd9Sstevel@tonic-gate ddi_driver_name(pdip), ddi_get_instance(pdip), 4499*7c478bd9Sstevel@tonic-gate name, addr, timeout)); 4500*7c478bd9Sstevel@tonic-gate 4501*7c478bd9Sstevel@tonic-gate mutex_enter(&DEVI(pdip)->devi_lock); 4502*7c478bd9Sstevel@tonic-gate (void) cv_timedwait(&DEVI(pdip)->devi_cv, 4503*7c478bd9Sstevel@tonic-gate &DEVI(pdip)->devi_lock, end_time); 4504*7c478bd9Sstevel@tonic-gate mutex_exit(&DEVI(pdip)->devi_lock); 4505*7c478bd9Sstevel@tonic-gate ndi_devi_enter(pdip, &circ); 4506*7c478bd9Sstevel@tonic-gate (void) i_ndi_make_spec_children(pdip, flags); 4507*7c478bd9Sstevel@tonic-gate } 4508*7c478bd9Sstevel@tonic-gate 4509*7c478bd9Sstevel@tonic-gate if ((dip == NULL) && probed && (flags & NDI_CONFIG_REPROBE) && 4510*7c478bd9Sstevel@tonic-gate i_ddi_io_initialized()) { 4511*7c478bd9Sstevel@tonic-gate /* 4512*7c478bd9Sstevel@tonic-gate * reenumerate .conf nodes and probe again 4513*7c478bd9Sstevel@tonic-gate */ 4514*7c478bd9Sstevel@tonic-gate mutex_enter(&DEVI(pdip)->devi_lock); 4515*7c478bd9Sstevel@tonic-gate DEVI(pdip)->devi_flags &= ~DEVI_MADE_CHILDREN; 4516*7c478bd9Sstevel@tonic-gate mutex_exit(&DEVI(pdip)->devi_lock); 4517*7c478bd9Sstevel@tonic-gate goto reprobe; 4518*7c478bd9Sstevel@tonic-gate } 4519*7c478bd9Sstevel@tonic-gate 4520*7c478bd9Sstevel@tonic-gate if (addr[0] != '\0') 4521*7c478bd9Sstevel@tonic-gate *(addr - 1) = '@'; 4522*7c478bd9Sstevel@tonic-gate 4523*7c478bd9Sstevel@tonic-gate if (dip == NULL || devi_attach_node(dip, flags) != NDI_SUCCESS) { 4524*7c478bd9Sstevel@tonic-gate ndi_devi_exit(pdip, circ); 4525*7c478bd9Sstevel@tonic-gate return (NDI_FAILURE); 4526*7c478bd9Sstevel@tonic-gate } 4527*7c478bd9Sstevel@tonic-gate 4528*7c478bd9Sstevel@tonic-gate *dipp = dip; 4529*7c478bd9Sstevel@tonic-gate ndi_hold_devi(dip); 4530*7c478bd9Sstevel@tonic-gate ndi_devi_exit(pdip, circ); 4531*7c478bd9Sstevel@tonic-gate return (NDI_SUCCESS); 4532*7c478bd9Sstevel@tonic-gate } 4533*7c478bd9Sstevel@tonic-gate 4534*7c478bd9Sstevel@tonic-gate /* 4535*7c478bd9Sstevel@tonic-gate * Enumerate and attach a child specified by name 'devnm'. 4536*7c478bd9Sstevel@tonic-gate * Called by devfs lookup and DR to perform a BUS_CONFIG_ONE. 4537*7c478bd9Sstevel@tonic-gate * Note: devfs does not make use of NDI_CONFIG to configure 4538*7c478bd9Sstevel@tonic-gate * an entire branch. 4539*7c478bd9Sstevel@tonic-gate */ 4540*7c478bd9Sstevel@tonic-gate int 4541*7c478bd9Sstevel@tonic-gate ndi_devi_config_one(dev_info_t *dip, char *devnm, dev_info_t **dipp, int flags) 4542*7c478bd9Sstevel@tonic-gate { 4543*7c478bd9Sstevel@tonic-gate int error; 4544*7c478bd9Sstevel@tonic-gate int (*f)(); 4545*7c478bd9Sstevel@tonic-gate int branch_event = 0; 4546*7c478bd9Sstevel@tonic-gate 4547*7c478bd9Sstevel@tonic-gate ASSERT(dipp); 4548*7c478bd9Sstevel@tonic-gate ASSERT(i_ddi_node_state(dip) >= DS_ATTACHED); 4549*7c478bd9Sstevel@tonic-gate 4550*7c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_CONT, 4551*7c478bd9Sstevel@tonic-gate "ndi_devi_config_one: par = %s%d (%p), child = %s\n", 4552*7c478bd9Sstevel@tonic-gate ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, devnm)); 4553*7c478bd9Sstevel@tonic-gate 4554*7c478bd9Sstevel@tonic-gate if (pm_pre_config(dip, devnm) != DDI_SUCCESS) 4555*7c478bd9Sstevel@tonic-gate return (NDI_FAILURE); 4556*7c478bd9Sstevel@tonic-gate 4557*7c478bd9Sstevel@tonic-gate if ((flags & (NDI_NO_EVENT | NDI_BRANCH_EVENT_OP)) == 0 && 4558*7c478bd9Sstevel@tonic-gate (flags & NDI_CONFIG)) { 4559*7c478bd9Sstevel@tonic-gate flags |= NDI_BRANCH_EVENT_OP; 4560*7c478bd9Sstevel@tonic-gate branch_event = 1; 4561*7c478bd9Sstevel@tonic-gate } 4562*7c478bd9Sstevel@tonic-gate 4563*7c478bd9Sstevel@tonic-gate if ((DEVI(dip)->devi_ops->devo_bus_ops == NULL) || 4564*7c478bd9Sstevel@tonic-gate (DEVI(dip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) || 4565*7c478bd9Sstevel@tonic-gate (f = DEVI(dip)->devi_ops->devo_bus_ops->bus_config) == NULL) { 4566*7c478bd9Sstevel@tonic-gate error = devi_config_one(dip, devnm, dipp, flags, 0); 4567*7c478bd9Sstevel@tonic-gate } else { 4568*7c478bd9Sstevel@tonic-gate /* call bus_config entry point */ 4569*7c478bd9Sstevel@tonic-gate error = (*f)(dip, flags, BUS_CONFIG_ONE, (void *)devnm, dipp); 4570*7c478bd9Sstevel@tonic-gate } 4571*7c478bd9Sstevel@tonic-gate 4572*7c478bd9Sstevel@tonic-gate if (error || (flags & NDI_CONFIG) == 0) { 4573*7c478bd9Sstevel@tonic-gate pm_post_config(dip, devnm); 4574*7c478bd9Sstevel@tonic-gate return (error); 4575*7c478bd9Sstevel@tonic-gate } 4576*7c478bd9Sstevel@tonic-gate 4577*7c478bd9Sstevel@tonic-gate /* 4578*7c478bd9Sstevel@tonic-gate * DR usage ((i.e. call with NDI_CONFIG) recursively configures 4579*7c478bd9Sstevel@tonic-gate * grandchildren, performing a BUS_CONFIG_ALL from the node attached 4580*7c478bd9Sstevel@tonic-gate * by the BUS_CONFIG_ONE. 4581*7c478bd9Sstevel@tonic-gate */ 4582*7c478bd9Sstevel@tonic-gate ASSERT(*dipp); 4583*7c478bd9Sstevel@tonic-gate 4584*7c478bd9Sstevel@tonic-gate error = devi_config_common(*dipp, flags, (major_t)-1); 4585*7c478bd9Sstevel@tonic-gate 4586*7c478bd9Sstevel@tonic-gate pm_post_config(dip, devnm); 4587*7c478bd9Sstevel@tonic-gate 4588*7c478bd9Sstevel@tonic-gate if (branch_event) 4589*7c478bd9Sstevel@tonic-gate (void) i_log_devfs_branch_add(*dipp); 4590*7c478bd9Sstevel@tonic-gate 4591*7c478bd9Sstevel@tonic-gate return (error); 4592*7c478bd9Sstevel@tonic-gate } 4593*7c478bd9Sstevel@tonic-gate 4594*7c478bd9Sstevel@tonic-gate 4595*7c478bd9Sstevel@tonic-gate /* 4596*7c478bd9Sstevel@tonic-gate * Enumerate and attach a child specified by name 'devnm'. 4597*7c478bd9Sstevel@tonic-gate * Called during configure the OBP options. This configures 4598*7c478bd9Sstevel@tonic-gate * only one node. 4599*7c478bd9Sstevel@tonic-gate */ 4600*7c478bd9Sstevel@tonic-gate static int 4601*7c478bd9Sstevel@tonic-gate ndi_devi_config_obp_args(dev_info_t *parent, char *devnm, 4602*7c478bd9Sstevel@tonic-gate dev_info_t **childp, int flags) 4603*7c478bd9Sstevel@tonic-gate { 4604*7c478bd9Sstevel@tonic-gate int error; 4605*7c478bd9Sstevel@tonic-gate int (*f)(); 4606*7c478bd9Sstevel@tonic-gate 4607*7c478bd9Sstevel@tonic-gate ASSERT(childp); 4608*7c478bd9Sstevel@tonic-gate ASSERT(i_ddi_node_state(parent) >= DS_ATTACHED); 4609*7c478bd9Sstevel@tonic-gate 4610*7c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_CONT, "ndi_devi_config_obp_args: " 4611*7c478bd9Sstevel@tonic-gate "par = %s%d (%p), child = %s\n", ddi_driver_name(parent), 4612*7c478bd9Sstevel@tonic-gate ddi_get_instance(parent), (void *)parent, devnm)); 4613*7c478bd9Sstevel@tonic-gate 4614*7c478bd9Sstevel@tonic-gate if ((DEVI(parent)->devi_ops->devo_bus_ops == NULL) || 4615*7c478bd9Sstevel@tonic-gate (DEVI(parent)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) || 4616*7c478bd9Sstevel@tonic-gate (f = DEVI(parent)->devi_ops->devo_bus_ops->bus_config) == NULL) { 4617*7c478bd9Sstevel@tonic-gate error = NDI_FAILURE; 4618*7c478bd9Sstevel@tonic-gate } else { 4619*7c478bd9Sstevel@tonic-gate /* call bus_config entry point */ 4620*7c478bd9Sstevel@tonic-gate error = (*f)(parent, flags, 4621*7c478bd9Sstevel@tonic-gate BUS_CONFIG_OBP_ARGS, (void *)devnm, childp); 4622*7c478bd9Sstevel@tonic-gate } 4623*7c478bd9Sstevel@tonic-gate return (error); 4624*7c478bd9Sstevel@tonic-gate } 4625*7c478bd9Sstevel@tonic-gate 4626*7c478bd9Sstevel@tonic-gate 4627*7c478bd9Sstevel@tonic-gate /* 4628*7c478bd9Sstevel@tonic-gate * detach a node with parent already held busy 4629*7c478bd9Sstevel@tonic-gate */ 4630*7c478bd9Sstevel@tonic-gate static int 4631*7c478bd9Sstevel@tonic-gate devi_detach_node(dev_info_t *dip, uint_t flags) 4632*7c478bd9Sstevel@tonic-gate { 4633*7c478bd9Sstevel@tonic-gate dev_info_t *pdip = ddi_get_parent(dip); 4634*7c478bd9Sstevel@tonic-gate int ret = NDI_SUCCESS; 4635*7c478bd9Sstevel@tonic-gate ddi_eventcookie_t cookie; 4636*7c478bd9Sstevel@tonic-gate 4637*7c478bd9Sstevel@tonic-gate if (flags & NDI_POST_EVENT) { 4638*7c478bd9Sstevel@tonic-gate if (pdip && i_ddi_node_state(pdip) >= DS_ATTACHED) { 4639*7c478bd9Sstevel@tonic-gate if (ddi_get_eventcookie(dip, DDI_DEVI_REMOVE_EVENT, 4640*7c478bd9Sstevel@tonic-gate &cookie) == NDI_SUCCESS) 4641*7c478bd9Sstevel@tonic-gate (void) ndi_post_event(dip, dip, cookie, NULL); 4642*7c478bd9Sstevel@tonic-gate } 4643*7c478bd9Sstevel@tonic-gate } 4644*7c478bd9Sstevel@tonic-gate 4645*7c478bd9Sstevel@tonic-gate if (i_ddi_detachchild(dip, flags) != DDI_SUCCESS) 4646*7c478bd9Sstevel@tonic-gate return (NDI_FAILURE); 4647*7c478bd9Sstevel@tonic-gate 4648*7c478bd9Sstevel@tonic-gate if (flags & NDI_AUTODETACH) 4649*7c478bd9Sstevel@tonic-gate return (NDI_SUCCESS); 4650*7c478bd9Sstevel@tonic-gate 4651*7c478bd9Sstevel@tonic-gate /* 4652*7c478bd9Sstevel@tonic-gate * For DR, even bound nodes may need to have offline 4653*7c478bd9Sstevel@tonic-gate * flag set. 4654*7c478bd9Sstevel@tonic-gate */ 4655*7c478bd9Sstevel@tonic-gate if (flags & NDI_DEVI_OFFLINE) { 4656*7c478bd9Sstevel@tonic-gate DEVI_SET_DEVICE_OFFLINE(dip); 4657*7c478bd9Sstevel@tonic-gate } 4658*7c478bd9Sstevel@tonic-gate 4659*7c478bd9Sstevel@tonic-gate if (i_ddi_node_state(dip) == DS_INITIALIZED) { 4660*7c478bd9Sstevel@tonic-gate char *path = kmem_alloc(MAXPATHLEN, KM_SLEEP); 4661*7c478bd9Sstevel@tonic-gate (void) ddi_pathname(dip, path); 4662*7c478bd9Sstevel@tonic-gate if (flags & NDI_DEVI_OFFLINE) 4663*7c478bd9Sstevel@tonic-gate i_ndi_devi_report_status_change(dip, path); 4664*7c478bd9Sstevel@tonic-gate 4665*7c478bd9Sstevel@tonic-gate if (need_remove_event(dip, flags)) { 4666*7c478bd9Sstevel@tonic-gate (void) i_log_devfs_remove_devinfo(path, 4667*7c478bd9Sstevel@tonic-gate i_ddi_devi_class(dip), 4668*7c478bd9Sstevel@tonic-gate (char *)ddi_driver_name(dip), 4669*7c478bd9Sstevel@tonic-gate ddi_get_instance(dip), 4670*7c478bd9Sstevel@tonic-gate flags); 4671*7c478bd9Sstevel@tonic-gate DEVI_SET_EVREMOVE(dip); 4672*7c478bd9Sstevel@tonic-gate } 4673*7c478bd9Sstevel@tonic-gate kmem_free(path, MAXPATHLEN); 4674*7c478bd9Sstevel@tonic-gate } 4675*7c478bd9Sstevel@tonic-gate 4676*7c478bd9Sstevel@tonic-gate if (flags & (NDI_UNCONFIG | NDI_DEVI_REMOVE)) { 4677*7c478bd9Sstevel@tonic-gate ret = ddi_uninitchild(dip); 4678*7c478bd9Sstevel@tonic-gate if (ret == NDI_SUCCESS) { 4679*7c478bd9Sstevel@tonic-gate /* 4680*7c478bd9Sstevel@tonic-gate * Remove uninitialized pseudo nodes because 4681*7c478bd9Sstevel@tonic-gate * system props are lost and the node cannot be 4682*7c478bd9Sstevel@tonic-gate * reattached. 4683*7c478bd9Sstevel@tonic-gate */ 4684*7c478bd9Sstevel@tonic-gate if (!ndi_dev_is_persistent_node(dip)) 4685*7c478bd9Sstevel@tonic-gate flags |= NDI_DEVI_REMOVE; 4686*7c478bd9Sstevel@tonic-gate 4687*7c478bd9Sstevel@tonic-gate if (flags & NDI_DEVI_REMOVE) 4688*7c478bd9Sstevel@tonic-gate ret = ddi_remove_child(dip, 0); 4689*7c478bd9Sstevel@tonic-gate } 4690*7c478bd9Sstevel@tonic-gate } 4691*7c478bd9Sstevel@tonic-gate 4692*7c478bd9Sstevel@tonic-gate return (ret); 4693*7c478bd9Sstevel@tonic-gate } 4694*7c478bd9Sstevel@tonic-gate 4695*7c478bd9Sstevel@tonic-gate /* 4696*7c478bd9Sstevel@tonic-gate * unconfigure immediate children of bus nexus device 4697*7c478bd9Sstevel@tonic-gate */ 4698*7c478bd9Sstevel@tonic-gate static int 4699*7c478bd9Sstevel@tonic-gate unconfig_immediate_children( 4700*7c478bd9Sstevel@tonic-gate dev_info_t *dip, 4701*7c478bd9Sstevel@tonic-gate dev_info_t **dipp, 4702*7c478bd9Sstevel@tonic-gate int flags, 4703*7c478bd9Sstevel@tonic-gate major_t major) 4704*7c478bd9Sstevel@tonic-gate { 4705*7c478bd9Sstevel@tonic-gate int rv = NDI_SUCCESS, circ; 4706*7c478bd9Sstevel@tonic-gate dev_info_t *child; 4707*7c478bd9Sstevel@tonic-gate 4708*7c478bd9Sstevel@tonic-gate ASSERT(dipp == NULL || *dipp == NULL); 4709*7c478bd9Sstevel@tonic-gate 4710*7c478bd9Sstevel@tonic-gate ndi_devi_enter(dip, &circ); 4711*7c478bd9Sstevel@tonic-gate child = ddi_get_child(dip); 4712*7c478bd9Sstevel@tonic-gate while (child) { 4713*7c478bd9Sstevel@tonic-gate dev_info_t *next = ddi_get_next_sibling(child); 4714*7c478bd9Sstevel@tonic-gate if ((major != (major_t)-1) && 4715*7c478bd9Sstevel@tonic-gate (major != ddi_driver_major(child))) { 4716*7c478bd9Sstevel@tonic-gate child = next; 4717*7c478bd9Sstevel@tonic-gate continue; 4718*7c478bd9Sstevel@tonic-gate } 4719*7c478bd9Sstevel@tonic-gate 4720*7c478bd9Sstevel@tonic-gate /* skip nexus nodes during autodetach */ 4721*7c478bd9Sstevel@tonic-gate if ((flags & NDI_AUTODETACH) && !is_leaf_node(child)) { 4722*7c478bd9Sstevel@tonic-gate child = next; 4723*7c478bd9Sstevel@tonic-gate continue; 4724*7c478bd9Sstevel@tonic-gate } 4725*7c478bd9Sstevel@tonic-gate 4726*7c478bd9Sstevel@tonic-gate if (devi_detach_node(child, flags) != NDI_SUCCESS) { 4727*7c478bd9Sstevel@tonic-gate if (dipp && *dipp == NULL) { 4728*7c478bd9Sstevel@tonic-gate ndi_hold_devi(child); 4729*7c478bd9Sstevel@tonic-gate *dipp = child; 4730*7c478bd9Sstevel@tonic-gate } 4731*7c478bd9Sstevel@tonic-gate rv = NDI_FAILURE; 4732*7c478bd9Sstevel@tonic-gate } 4733*7c478bd9Sstevel@tonic-gate 4734*7c478bd9Sstevel@tonic-gate /* 4735*7c478bd9Sstevel@tonic-gate * Continue upon failure--best effort algorithm 4736*7c478bd9Sstevel@tonic-gate */ 4737*7c478bd9Sstevel@tonic-gate child = next; 4738*7c478bd9Sstevel@tonic-gate } 4739*7c478bd9Sstevel@tonic-gate ndi_devi_exit(dip, circ); 4740*7c478bd9Sstevel@tonic-gate return (rv); 4741*7c478bd9Sstevel@tonic-gate } 4742*7c478bd9Sstevel@tonic-gate 4743*7c478bd9Sstevel@tonic-gate /* 4744*7c478bd9Sstevel@tonic-gate * unconfigure grand children of bus nexus device 4745*7c478bd9Sstevel@tonic-gate */ 4746*7c478bd9Sstevel@tonic-gate static int 4747*7c478bd9Sstevel@tonic-gate unconfig_grand_children( 4748*7c478bd9Sstevel@tonic-gate dev_info_t *dip, 4749*7c478bd9Sstevel@tonic-gate dev_info_t **dipp, 4750*7c478bd9Sstevel@tonic-gate int flags, 4751*7c478bd9Sstevel@tonic-gate major_t major, 4752*7c478bd9Sstevel@tonic-gate struct brevq_node **brevqp) 4753*7c478bd9Sstevel@tonic-gate { 4754*7c478bd9Sstevel@tonic-gate struct mt_config_handle *hdl; 4755*7c478bd9Sstevel@tonic-gate 4756*7c478bd9Sstevel@tonic-gate if (brevqp) 4757*7c478bd9Sstevel@tonic-gate *brevqp = NULL; 4758*7c478bd9Sstevel@tonic-gate 4759*7c478bd9Sstevel@tonic-gate /* multi-threaded configuration of child nexus */ 4760*7c478bd9Sstevel@tonic-gate hdl = mt_config_init(dip, dipp, flags, major, MT_UNCONFIG_OP, brevqp); 4761*7c478bd9Sstevel@tonic-gate mt_config_children(hdl); 4762*7c478bd9Sstevel@tonic-gate 4763*7c478bd9Sstevel@tonic-gate return (mt_config_fini(hdl)); /* wait for threads to exit */ 4764*7c478bd9Sstevel@tonic-gate } 4765*7c478bd9Sstevel@tonic-gate 4766*7c478bd9Sstevel@tonic-gate /* 4767*7c478bd9Sstevel@tonic-gate * Unconfigure children/descendants of the dip. 4768*7c478bd9Sstevel@tonic-gate * 4769*7c478bd9Sstevel@tonic-gate * If brevqp is not NULL, on return *brevqp is set to a queue of dip's 4770*7c478bd9Sstevel@tonic-gate * child devinames for which branch remove events need to be generated. 4771*7c478bd9Sstevel@tonic-gate */ 4772*7c478bd9Sstevel@tonic-gate static int 4773*7c478bd9Sstevel@tonic-gate devi_unconfig_common( 4774*7c478bd9Sstevel@tonic-gate dev_info_t *dip, 4775*7c478bd9Sstevel@tonic-gate dev_info_t **dipp, 4776*7c478bd9Sstevel@tonic-gate int flags, 4777*7c478bd9Sstevel@tonic-gate major_t major, 4778*7c478bd9Sstevel@tonic-gate struct brevq_node **brevqp) 4779*7c478bd9Sstevel@tonic-gate { 4780*7c478bd9Sstevel@tonic-gate int rv; 4781*7c478bd9Sstevel@tonic-gate int pm_cookie; 4782*7c478bd9Sstevel@tonic-gate int (*f)(); 4783*7c478bd9Sstevel@tonic-gate ddi_bus_config_op_t bus_op; 4784*7c478bd9Sstevel@tonic-gate 4785*7c478bd9Sstevel@tonic-gate if (dipp) 4786*7c478bd9Sstevel@tonic-gate *dipp = NULL; 4787*7c478bd9Sstevel@tonic-gate if (brevqp) 4788*7c478bd9Sstevel@tonic-gate *brevqp = NULL; 4789*7c478bd9Sstevel@tonic-gate 4790*7c478bd9Sstevel@tonic-gate /* 4791*7c478bd9Sstevel@tonic-gate * Power up the dip if it is powered off. If the flag bit 4792*7c478bd9Sstevel@tonic-gate * NDI_AUTODETACH is set and the dip is not at its full power, 4793*7c478bd9Sstevel@tonic-gate * skip the rest of the branch. 4794*7c478bd9Sstevel@tonic-gate */ 4795*7c478bd9Sstevel@tonic-gate if (pm_pre_unconfig(dip, flags, &pm_cookie, NULL) != DDI_SUCCESS) 4796*7c478bd9Sstevel@tonic-gate return ((flags & NDI_AUTODETACH) ? NDI_SUCCESS : 4797*7c478bd9Sstevel@tonic-gate NDI_FAILURE); 4798*7c478bd9Sstevel@tonic-gate 4799*7c478bd9Sstevel@tonic-gate /* 4800*7c478bd9Sstevel@tonic-gate * Some callers, notably SCSI, need to clear out the devfs 4801*7c478bd9Sstevel@tonic-gate * cache together with the unconfig to prevent stale entries. 4802*7c478bd9Sstevel@tonic-gate */ 4803*7c478bd9Sstevel@tonic-gate if (flags & NDI_DEVFS_CLEAN) 4804*7c478bd9Sstevel@tonic-gate (void) devfs_clean(dip, NULL, 0); 4805*7c478bd9Sstevel@tonic-gate 4806*7c478bd9Sstevel@tonic-gate rv = unconfig_grand_children(dip, dipp, flags, major, brevqp); 4807*7c478bd9Sstevel@tonic-gate 4808*7c478bd9Sstevel@tonic-gate if ((rv != NDI_SUCCESS) && ((flags & NDI_AUTODETACH) == 0)) { 4809*7c478bd9Sstevel@tonic-gate if (brevqp && *brevqp) { 4810*7c478bd9Sstevel@tonic-gate log_and_free_br_events_on_grand_children(dip, *brevqp); 4811*7c478bd9Sstevel@tonic-gate free_brevq(*brevqp); 4812*7c478bd9Sstevel@tonic-gate *brevqp = NULL; 4813*7c478bd9Sstevel@tonic-gate } 4814*7c478bd9Sstevel@tonic-gate pm_post_unconfig(dip, pm_cookie, NULL); 4815*7c478bd9Sstevel@tonic-gate return (rv); 4816*7c478bd9Sstevel@tonic-gate } 4817*7c478bd9Sstevel@tonic-gate 4818*7c478bd9Sstevel@tonic-gate if (dipp && *dipp) { 4819*7c478bd9Sstevel@tonic-gate ndi_rele_devi(*dipp); 4820*7c478bd9Sstevel@tonic-gate *dipp = NULL; 4821*7c478bd9Sstevel@tonic-gate } 4822*7c478bd9Sstevel@tonic-gate 4823*7c478bd9Sstevel@tonic-gate /* 4824*7c478bd9Sstevel@tonic-gate * It is possible to have a detached nexus with children 4825*7c478bd9Sstevel@tonic-gate * and grandchildren (for example: a branch consisting 4826*7c478bd9Sstevel@tonic-gate * entirely of bound nodes.) Since the nexus is detached 4827*7c478bd9Sstevel@tonic-gate * the bus_unconfig entry point cannot be used to remove 4828*7c478bd9Sstevel@tonic-gate * or unconfigure the descendants. 4829*7c478bd9Sstevel@tonic-gate */ 4830*7c478bd9Sstevel@tonic-gate if (i_ddi_node_state(dip) < DS_ATTACHED || 4831*7c478bd9Sstevel@tonic-gate (DEVI(dip)->devi_ops->devo_bus_ops == NULL) || 4832*7c478bd9Sstevel@tonic-gate (DEVI(dip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) || 4833*7c478bd9Sstevel@tonic-gate (f = DEVI(dip)->devi_ops->devo_bus_ops->bus_unconfig) == NULL) { 4834*7c478bd9Sstevel@tonic-gate rv = unconfig_immediate_children(dip, dipp, flags, major); 4835*7c478bd9Sstevel@tonic-gate } else { 4836*7c478bd9Sstevel@tonic-gate /* 4837*7c478bd9Sstevel@tonic-gate * call bus_unconfig entry point 4838*7c478bd9Sstevel@tonic-gate * It should reset nexus flags if unconfigure succeeds. 4839*7c478bd9Sstevel@tonic-gate */ 4840*7c478bd9Sstevel@tonic-gate bus_op = (major == (major_t)-1) ? 4841*7c478bd9Sstevel@tonic-gate BUS_UNCONFIG_ALL : BUS_UNCONFIG_DRIVER; 4842*7c478bd9Sstevel@tonic-gate rv = (*f)(dip, flags, bus_op, (void *)(uintptr_t)major); 4843*7c478bd9Sstevel@tonic-gate } 4844*7c478bd9Sstevel@tonic-gate 4845*7c478bd9Sstevel@tonic-gate pm_post_unconfig(dip, pm_cookie, NULL); 4846*7c478bd9Sstevel@tonic-gate 4847*7c478bd9Sstevel@tonic-gate if (brevqp && *brevqp) 4848*7c478bd9Sstevel@tonic-gate cleanup_br_events_on_grand_children(dip, brevqp); 4849*7c478bd9Sstevel@tonic-gate 4850*7c478bd9Sstevel@tonic-gate return (rv); 4851*7c478bd9Sstevel@tonic-gate } 4852*7c478bd9Sstevel@tonic-gate 4853*7c478bd9Sstevel@tonic-gate /* 4854*7c478bd9Sstevel@tonic-gate * called by devfs/framework to unconfigure children bound to major 4855*7c478bd9Sstevel@tonic-gate * If NDI_AUTODETACH is specified, this is invoked by either the 4856*7c478bd9Sstevel@tonic-gate * moduninstall daemon or the modunload -i 0 command. 4857*7c478bd9Sstevel@tonic-gate */ 4858*7c478bd9Sstevel@tonic-gate int 4859*7c478bd9Sstevel@tonic-gate ndi_devi_unconfig_driver(dev_info_t *dip, int flags, major_t major) 4860*7c478bd9Sstevel@tonic-gate { 4861*7c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_CONT, 4862*7c478bd9Sstevel@tonic-gate "ndi_devi_unconfig_driver: par = %s%d (%p), flags = 0x%x\n", 4863*7c478bd9Sstevel@tonic-gate ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags)); 4864*7c478bd9Sstevel@tonic-gate 4865*7c478bd9Sstevel@tonic-gate return (devi_unconfig_common(dip, NULL, flags, major, NULL)); 4866*7c478bd9Sstevel@tonic-gate } 4867*7c478bd9Sstevel@tonic-gate 4868*7c478bd9Sstevel@tonic-gate int 4869*7c478bd9Sstevel@tonic-gate ndi_devi_unconfig(dev_info_t *dip, int flags) 4870*7c478bd9Sstevel@tonic-gate { 4871*7c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_CONT, 4872*7c478bd9Sstevel@tonic-gate "ndi_devi_unconfig: par = %s%d (%p), flags = 0x%x\n", 4873*7c478bd9Sstevel@tonic-gate ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags)); 4874*7c478bd9Sstevel@tonic-gate 4875*7c478bd9Sstevel@tonic-gate return (devi_unconfig_common(dip, NULL, flags, (major_t)-1, NULL)); 4876*7c478bd9Sstevel@tonic-gate } 4877*7c478bd9Sstevel@tonic-gate 4878*7c478bd9Sstevel@tonic-gate int 4879*7c478bd9Sstevel@tonic-gate e_ddi_devi_unconfig(dev_info_t *dip, dev_info_t **dipp, int flags) 4880*7c478bd9Sstevel@tonic-gate { 4881*7c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_CONT, 4882*7c478bd9Sstevel@tonic-gate "e_ddi_devi_unconfig: par = %s%d (%p), flags = 0x%x\n", 4883*7c478bd9Sstevel@tonic-gate ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags)); 4884*7c478bd9Sstevel@tonic-gate 4885*7c478bd9Sstevel@tonic-gate return (devi_unconfig_common(dip, dipp, flags, (major_t)-1, NULL)); 4886*7c478bd9Sstevel@tonic-gate } 4887*7c478bd9Sstevel@tonic-gate 4888*7c478bd9Sstevel@tonic-gate /* 4889*7c478bd9Sstevel@tonic-gate * Unconfigure child by name 4890*7c478bd9Sstevel@tonic-gate */ 4891*7c478bd9Sstevel@tonic-gate static int 4892*7c478bd9Sstevel@tonic-gate devi_unconfig_one(dev_info_t *pdip, char *devnm, int flags) 4893*7c478bd9Sstevel@tonic-gate { 4894*7c478bd9Sstevel@tonic-gate int rv, circ; 4895*7c478bd9Sstevel@tonic-gate dev_info_t *child; 4896*7c478bd9Sstevel@tonic-gate 4897*7c478bd9Sstevel@tonic-gate ndi_devi_enter(pdip, &circ); 4898*7c478bd9Sstevel@tonic-gate child = ndi_devi_findchild(pdip, devnm); 4899*7c478bd9Sstevel@tonic-gate if (child == NULL) { 4900*7c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_CONT, 4901*7c478bd9Sstevel@tonic-gate "devi_unconfig_one: %s not found\n", devnm)); 4902*7c478bd9Sstevel@tonic-gate ndi_devi_exit(pdip, circ); 4903*7c478bd9Sstevel@tonic-gate return (NDI_SUCCESS); 4904*7c478bd9Sstevel@tonic-gate } 4905*7c478bd9Sstevel@tonic-gate rv = devi_detach_node(child, flags); 4906*7c478bd9Sstevel@tonic-gate ndi_devi_exit(pdip, circ); 4907*7c478bd9Sstevel@tonic-gate return (rv); 4908*7c478bd9Sstevel@tonic-gate } 4909*7c478bd9Sstevel@tonic-gate 4910*7c478bd9Sstevel@tonic-gate int 4911*7c478bd9Sstevel@tonic-gate ndi_devi_unconfig_one( 4912*7c478bd9Sstevel@tonic-gate dev_info_t *pdip, 4913*7c478bd9Sstevel@tonic-gate char *devnm, 4914*7c478bd9Sstevel@tonic-gate dev_info_t **dipp, 4915*7c478bd9Sstevel@tonic-gate int flags) 4916*7c478bd9Sstevel@tonic-gate { 4917*7c478bd9Sstevel@tonic-gate int (*f)(); 4918*7c478bd9Sstevel@tonic-gate int circ, rv; 4919*7c478bd9Sstevel@tonic-gate int pm_cookie; 4920*7c478bd9Sstevel@tonic-gate dev_info_t *child; 4921*7c478bd9Sstevel@tonic-gate struct brevq_node *brevq = NULL; 4922*7c478bd9Sstevel@tonic-gate 4923*7c478bd9Sstevel@tonic-gate ASSERT(i_ddi_node_state(pdip) >= DS_ATTACHED); 4924*7c478bd9Sstevel@tonic-gate 4925*7c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_CONT, 4926*7c478bd9Sstevel@tonic-gate "ndi_devi_unconfig_one: par = %s%d (%p), child = %s\n", 4927*7c478bd9Sstevel@tonic-gate ddi_driver_name(pdip), ddi_get_instance(pdip), 4928*7c478bd9Sstevel@tonic-gate (void *)pdip, devnm)); 4929*7c478bd9Sstevel@tonic-gate 4930*7c478bd9Sstevel@tonic-gate if (pm_pre_unconfig(pdip, flags, &pm_cookie, devnm) != DDI_SUCCESS) 4931*7c478bd9Sstevel@tonic-gate return (NDI_FAILURE); 4932*7c478bd9Sstevel@tonic-gate 4933*7c478bd9Sstevel@tonic-gate if (dipp) 4934*7c478bd9Sstevel@tonic-gate *dipp = NULL; 4935*7c478bd9Sstevel@tonic-gate 4936*7c478bd9Sstevel@tonic-gate ndi_devi_enter(pdip, &circ); 4937*7c478bd9Sstevel@tonic-gate child = ndi_devi_findchild(pdip, devnm); 4938*7c478bd9Sstevel@tonic-gate if (child == NULL) { 4939*7c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_CONT, "ndi_devi_unconfig_one: %s" 4940*7c478bd9Sstevel@tonic-gate " not found\n", devnm)); 4941*7c478bd9Sstevel@tonic-gate ndi_devi_exit(pdip, circ); 4942*7c478bd9Sstevel@tonic-gate pm_post_unconfig(pdip, pm_cookie, devnm); 4943*7c478bd9Sstevel@tonic-gate return (NDI_SUCCESS); 4944*7c478bd9Sstevel@tonic-gate } 4945*7c478bd9Sstevel@tonic-gate 4946*7c478bd9Sstevel@tonic-gate /* 4947*7c478bd9Sstevel@tonic-gate * Unconfigure children/descendants of named child 4948*7c478bd9Sstevel@tonic-gate */ 4949*7c478bd9Sstevel@tonic-gate rv = devi_unconfig_branch(child, dipp, flags | NDI_UNCONFIG, &brevq); 4950*7c478bd9Sstevel@tonic-gate if (rv != NDI_SUCCESS) 4951*7c478bd9Sstevel@tonic-gate goto out; 4952*7c478bd9Sstevel@tonic-gate 4953*7c478bd9Sstevel@tonic-gate init_bound_node_ev(pdip, child, flags); 4954*7c478bd9Sstevel@tonic-gate 4955*7c478bd9Sstevel@tonic-gate if ((DEVI(pdip)->devi_ops->devo_bus_ops == NULL) || 4956*7c478bd9Sstevel@tonic-gate (DEVI(pdip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) || 4957*7c478bd9Sstevel@tonic-gate (f = DEVI(pdip)->devi_ops->devo_bus_ops->bus_unconfig) == NULL) { 4958*7c478bd9Sstevel@tonic-gate rv = devi_detach_node(child, flags); 4959*7c478bd9Sstevel@tonic-gate } else { 4960*7c478bd9Sstevel@tonic-gate /* call bus_config entry point */ 4961*7c478bd9Sstevel@tonic-gate rv = (*f)(pdip, flags, BUS_UNCONFIG_ONE, (void *)devnm); 4962*7c478bd9Sstevel@tonic-gate } 4963*7c478bd9Sstevel@tonic-gate 4964*7c478bd9Sstevel@tonic-gate if (brevq) { 4965*7c478bd9Sstevel@tonic-gate if (rv != NDI_SUCCESS) 4966*7c478bd9Sstevel@tonic-gate log_and_free_brevq_dip(child, brevq); 4967*7c478bd9Sstevel@tonic-gate else 4968*7c478bd9Sstevel@tonic-gate free_brevq(brevq); 4969*7c478bd9Sstevel@tonic-gate } 4970*7c478bd9Sstevel@tonic-gate 4971*7c478bd9Sstevel@tonic-gate if (dipp && rv != NDI_SUCCESS) { 4972*7c478bd9Sstevel@tonic-gate ndi_hold_devi(child); 4973*7c478bd9Sstevel@tonic-gate ASSERT(*dipp == NULL); 4974*7c478bd9Sstevel@tonic-gate *dipp = child; 4975*7c478bd9Sstevel@tonic-gate } 4976*7c478bd9Sstevel@tonic-gate 4977*7c478bd9Sstevel@tonic-gate out: 4978*7c478bd9Sstevel@tonic-gate ndi_devi_exit(pdip, circ); 4979*7c478bd9Sstevel@tonic-gate pm_post_unconfig(pdip, pm_cookie, devnm); 4980*7c478bd9Sstevel@tonic-gate 4981*7c478bd9Sstevel@tonic-gate return (rv); 4982*7c478bd9Sstevel@tonic-gate } 4983*7c478bd9Sstevel@tonic-gate 4984*7c478bd9Sstevel@tonic-gate struct async_arg { 4985*7c478bd9Sstevel@tonic-gate dev_info_t *dip; 4986*7c478bd9Sstevel@tonic-gate uint_t flags; 4987*7c478bd9Sstevel@tonic-gate }; 4988*7c478bd9Sstevel@tonic-gate 4989*7c478bd9Sstevel@tonic-gate /* 4990*7c478bd9Sstevel@tonic-gate * Common async handler for: 4991*7c478bd9Sstevel@tonic-gate * ndi_devi_bind_driver_async 4992*7c478bd9Sstevel@tonic-gate * ndi_devi_online_async 4993*7c478bd9Sstevel@tonic-gate */ 4994*7c478bd9Sstevel@tonic-gate static int 4995*7c478bd9Sstevel@tonic-gate i_ndi_devi_async_common(dev_info_t *dip, uint_t flags, void (*func)()) 4996*7c478bd9Sstevel@tonic-gate { 4997*7c478bd9Sstevel@tonic-gate int tqflag; 4998*7c478bd9Sstevel@tonic-gate int kmflag; 4999*7c478bd9Sstevel@tonic-gate struct async_arg *arg; 5000*7c478bd9Sstevel@tonic-gate dev_info_t *pdip = ddi_get_parent(dip); 5001*7c478bd9Sstevel@tonic-gate 5002*7c478bd9Sstevel@tonic-gate ASSERT(pdip); 5003*7c478bd9Sstevel@tonic-gate ASSERT(DEVI(pdip)->devi_taskq); 5004*7c478bd9Sstevel@tonic-gate ASSERT(ndi_dev_is_persistent_node(dip)); 5005*7c478bd9Sstevel@tonic-gate 5006*7c478bd9Sstevel@tonic-gate if (flags & NDI_NOSLEEP) { 5007*7c478bd9Sstevel@tonic-gate kmflag = KM_NOSLEEP; 5008*7c478bd9Sstevel@tonic-gate tqflag = TQ_NOSLEEP; 5009*7c478bd9Sstevel@tonic-gate } else { 5010*7c478bd9Sstevel@tonic-gate kmflag = KM_SLEEP; 5011*7c478bd9Sstevel@tonic-gate tqflag = TQ_SLEEP; 5012*7c478bd9Sstevel@tonic-gate } 5013*7c478bd9Sstevel@tonic-gate 5014*7c478bd9Sstevel@tonic-gate arg = kmem_alloc(sizeof (*arg), kmflag); 5015*7c478bd9Sstevel@tonic-gate if (arg == NULL) 5016*7c478bd9Sstevel@tonic-gate goto fail; 5017*7c478bd9Sstevel@tonic-gate 5018*7c478bd9Sstevel@tonic-gate arg->flags = flags; 5019*7c478bd9Sstevel@tonic-gate arg->dip = dip; 5020*7c478bd9Sstevel@tonic-gate if (ddi_taskq_dispatch(DEVI(pdip)->devi_taskq, func, arg, tqflag) == 5021*7c478bd9Sstevel@tonic-gate DDI_SUCCESS) { 5022*7c478bd9Sstevel@tonic-gate return (NDI_SUCCESS); 5023*7c478bd9Sstevel@tonic-gate } 5024*7c478bd9Sstevel@tonic-gate 5025*7c478bd9Sstevel@tonic-gate fail: 5026*7c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_CONT, "%s%d: ddi_taskq_dispatch failed", 5027*7c478bd9Sstevel@tonic-gate ddi_driver_name(pdip), ddi_get_instance(pdip))); 5028*7c478bd9Sstevel@tonic-gate 5029*7c478bd9Sstevel@tonic-gate if (arg) 5030*7c478bd9Sstevel@tonic-gate kmem_free(arg, sizeof (*arg)); 5031*7c478bd9Sstevel@tonic-gate return (NDI_FAILURE); 5032*7c478bd9Sstevel@tonic-gate } 5033*7c478bd9Sstevel@tonic-gate 5034*7c478bd9Sstevel@tonic-gate static void 5035*7c478bd9Sstevel@tonic-gate i_ndi_devi_bind_driver_cb(struct async_arg *arg) 5036*7c478bd9Sstevel@tonic-gate { 5037*7c478bd9Sstevel@tonic-gate (void) ndi_devi_bind_driver(arg->dip, arg->flags); 5038*7c478bd9Sstevel@tonic-gate kmem_free(arg, sizeof (*arg)); 5039*7c478bd9Sstevel@tonic-gate } 5040*7c478bd9Sstevel@tonic-gate 5041*7c478bd9Sstevel@tonic-gate int 5042*7c478bd9Sstevel@tonic-gate ndi_devi_bind_driver_async(dev_info_t *dip, uint_t flags) 5043*7c478bd9Sstevel@tonic-gate { 5044*7c478bd9Sstevel@tonic-gate return (i_ndi_devi_async_common(dip, flags, 5045*7c478bd9Sstevel@tonic-gate (void (*)())i_ndi_devi_bind_driver_cb)); 5046*7c478bd9Sstevel@tonic-gate } 5047*7c478bd9Sstevel@tonic-gate 5048*7c478bd9Sstevel@tonic-gate /* 5049*7c478bd9Sstevel@tonic-gate * place the devinfo in the ONLINE state. 5050*7c478bd9Sstevel@tonic-gate */ 5051*7c478bd9Sstevel@tonic-gate int 5052*7c478bd9Sstevel@tonic-gate ndi_devi_online(dev_info_t *dip, uint_t flags) 5053*7c478bd9Sstevel@tonic-gate { 5054*7c478bd9Sstevel@tonic-gate int circ, rv; 5055*7c478bd9Sstevel@tonic-gate dev_info_t *pdip = ddi_get_parent(dip); 5056*7c478bd9Sstevel@tonic-gate int branch_event = 0; 5057*7c478bd9Sstevel@tonic-gate 5058*7c478bd9Sstevel@tonic-gate ASSERT(pdip); 5059*7c478bd9Sstevel@tonic-gate 5060*7c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_CONT, "ndi_devi_online: %s%d (%p)\n", 5061*7c478bd9Sstevel@tonic-gate ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip)); 5062*7c478bd9Sstevel@tonic-gate 5063*7c478bd9Sstevel@tonic-gate ndi_devi_enter(pdip, &circ); 5064*7c478bd9Sstevel@tonic-gate /* bind child before merging .conf nodes */ 5065*7c478bd9Sstevel@tonic-gate rv = i_ndi_config_node(dip, DS_BOUND, flags); 5066*7c478bd9Sstevel@tonic-gate if (rv != NDI_SUCCESS) { 5067*7c478bd9Sstevel@tonic-gate ndi_devi_exit(pdip, circ); 5068*7c478bd9Sstevel@tonic-gate return (rv); 5069*7c478bd9Sstevel@tonic-gate } 5070*7c478bd9Sstevel@tonic-gate 5071*7c478bd9Sstevel@tonic-gate /* merge .conf properties */ 5072*7c478bd9Sstevel@tonic-gate (void) i_ndi_make_spec_children(pdip, flags); 5073*7c478bd9Sstevel@tonic-gate 5074*7c478bd9Sstevel@tonic-gate flags |= (NDI_DEVI_ONLINE | NDI_CONFIG); 5075*7c478bd9Sstevel@tonic-gate 5076*7c478bd9Sstevel@tonic-gate if (flags & NDI_NO_EVENT) { 5077*7c478bd9Sstevel@tonic-gate /* 5078*7c478bd9Sstevel@tonic-gate * Caller is specifically asking for not to generate an event. 5079*7c478bd9Sstevel@tonic-gate * Set the following flag so that devi_attach_node() don't 5080*7c478bd9Sstevel@tonic-gate * change the event state. 5081*7c478bd9Sstevel@tonic-gate */ 5082*7c478bd9Sstevel@tonic-gate flags |= NDI_NO_EVENT_STATE_CHNG; 5083*7c478bd9Sstevel@tonic-gate } 5084*7c478bd9Sstevel@tonic-gate 5085*7c478bd9Sstevel@tonic-gate if ((flags & (NDI_NO_EVENT | NDI_BRANCH_EVENT_OP)) == 0 && 5086*7c478bd9Sstevel@tonic-gate ((flags & NDI_CONFIG) || DEVI_NEED_NDI_CONFIG(dip))) { 5087*7c478bd9Sstevel@tonic-gate flags |= NDI_BRANCH_EVENT_OP; 5088*7c478bd9Sstevel@tonic-gate branch_event = 1; 5089*7c478bd9Sstevel@tonic-gate } 5090*7c478bd9Sstevel@tonic-gate 5091*7c478bd9Sstevel@tonic-gate /* 5092*7c478bd9Sstevel@tonic-gate * devi_attach_node() may remove dip on failure 5093*7c478bd9Sstevel@tonic-gate */ 5094*7c478bd9Sstevel@tonic-gate if ((rv = devi_attach_node(dip, flags)) == NDI_SUCCESS) { 5095*7c478bd9Sstevel@tonic-gate if ((flags & NDI_CONFIG) || DEVI_NEED_NDI_CONFIG(dip)) { 5096*7c478bd9Sstevel@tonic-gate (void) ndi_devi_config(dip, flags); 5097*7c478bd9Sstevel@tonic-gate } 5098*7c478bd9Sstevel@tonic-gate 5099*7c478bd9Sstevel@tonic-gate if (branch_event) 5100*7c478bd9Sstevel@tonic-gate (void) i_log_devfs_branch_add(dip); 5101*7c478bd9Sstevel@tonic-gate } 5102*7c478bd9Sstevel@tonic-gate 5103*7c478bd9Sstevel@tonic-gate ndi_devi_exit(pdip, circ); 5104*7c478bd9Sstevel@tonic-gate 5105*7c478bd9Sstevel@tonic-gate /* 5106*7c478bd9Sstevel@tonic-gate * Notify devfs that we have a new node. Devfs needs to invalidate 5107*7c478bd9Sstevel@tonic-gate * cached directory contents. 5108*7c478bd9Sstevel@tonic-gate * 5109*7c478bd9Sstevel@tonic-gate * For PCMCIA devices, it is possible the pdip is not fully 5110*7c478bd9Sstevel@tonic-gate * attached. In this case, calling back into devfs will 5111*7c478bd9Sstevel@tonic-gate * result in a loop or assertion error. Hence, the check 5112*7c478bd9Sstevel@tonic-gate * on node state. 5113*7c478bd9Sstevel@tonic-gate * 5114*7c478bd9Sstevel@tonic-gate * If we own parent lock, this is part of a branch operation. 5115*7c478bd9Sstevel@tonic-gate * We skip the devfs_clean() step because the cache invalidation 5116*7c478bd9Sstevel@tonic-gate * is done higher up in the device tree. 5117*7c478bd9Sstevel@tonic-gate */ 5118*7c478bd9Sstevel@tonic-gate if (rv == NDI_SUCCESS && i_ddi_node_state(pdip) == DS_READY && 5119*7c478bd9Sstevel@tonic-gate !DEVI_BUSY_OWNED(pdip)) 5120*7c478bd9Sstevel@tonic-gate (void) devfs_clean(pdip, NULL, 0); 5121*7c478bd9Sstevel@tonic-gate return (rv); 5122*7c478bd9Sstevel@tonic-gate } 5123*7c478bd9Sstevel@tonic-gate 5124*7c478bd9Sstevel@tonic-gate static void 5125*7c478bd9Sstevel@tonic-gate i_ndi_devi_online_cb(struct async_arg *arg) 5126*7c478bd9Sstevel@tonic-gate { 5127*7c478bd9Sstevel@tonic-gate (void) ndi_devi_online(arg->dip, arg->flags); 5128*7c478bd9Sstevel@tonic-gate kmem_free(arg, sizeof (*arg)); 5129*7c478bd9Sstevel@tonic-gate } 5130*7c478bd9Sstevel@tonic-gate 5131*7c478bd9Sstevel@tonic-gate int 5132*7c478bd9Sstevel@tonic-gate ndi_devi_online_async(dev_info_t *dip, uint_t flags) 5133*7c478bd9Sstevel@tonic-gate { 5134*7c478bd9Sstevel@tonic-gate /* mark child as need config if requested. */ 5135*7c478bd9Sstevel@tonic-gate if (flags & NDI_CONFIG) 5136*7c478bd9Sstevel@tonic-gate DEVI_SET_NDI_CONFIG(dip); 5137*7c478bd9Sstevel@tonic-gate 5138*7c478bd9Sstevel@tonic-gate return (i_ndi_devi_async_common(dip, flags, 5139*7c478bd9Sstevel@tonic-gate (void (*)())i_ndi_devi_online_cb)); 5140*7c478bd9Sstevel@tonic-gate } 5141*7c478bd9Sstevel@tonic-gate 5142*7c478bd9Sstevel@tonic-gate /* 5143*7c478bd9Sstevel@tonic-gate * Take a device node Offline 5144*7c478bd9Sstevel@tonic-gate * To take a device Offline means to detach the device instance from 5145*7c478bd9Sstevel@tonic-gate * the driver and prevent devfs requests from re-attaching the device 5146*7c478bd9Sstevel@tonic-gate * instance. 5147*7c478bd9Sstevel@tonic-gate * 5148*7c478bd9Sstevel@tonic-gate * The flag NDI_DEVI_REMOVE causes removes the device node from 5149*7c478bd9Sstevel@tonic-gate * the driver list and the device tree. In this case, the device 5150*7c478bd9Sstevel@tonic-gate * is assumed to be removed from the system. 5151*7c478bd9Sstevel@tonic-gate */ 5152*7c478bd9Sstevel@tonic-gate int 5153*7c478bd9Sstevel@tonic-gate ndi_devi_offline(dev_info_t *dip, uint_t flags) 5154*7c478bd9Sstevel@tonic-gate { 5155*7c478bd9Sstevel@tonic-gate int circ, rval = 0; 5156*7c478bd9Sstevel@tonic-gate dev_info_t *pdip = ddi_get_parent(dip); 5157*7c478bd9Sstevel@tonic-gate struct brevq_node *brevq = NULL; 5158*7c478bd9Sstevel@tonic-gate 5159*7c478bd9Sstevel@tonic-gate ASSERT(pdip); 5160*7c478bd9Sstevel@tonic-gate 5161*7c478bd9Sstevel@tonic-gate flags |= NDI_DEVI_OFFLINE; 5162*7c478bd9Sstevel@tonic-gate ndi_devi_enter(pdip, &circ); 5163*7c478bd9Sstevel@tonic-gate if (i_ddi_node_state(dip) == DS_READY) { 5164*7c478bd9Sstevel@tonic-gate /* 5165*7c478bd9Sstevel@tonic-gate * If dip is in DS_READY state, there may be cached dv_nodes 5166*7c478bd9Sstevel@tonic-gate * referencing this dip, so we invoke devfs code path. 5167*7c478bd9Sstevel@tonic-gate * Note that we must release busy changing on pdip to 5168*7c478bd9Sstevel@tonic-gate * avoid deadlock against devfs. 5169*7c478bd9Sstevel@tonic-gate */ 5170*7c478bd9Sstevel@tonic-gate char *devname = kmem_alloc(MAXNAMELEN + 1, KM_SLEEP); 5171*7c478bd9Sstevel@tonic-gate (void) ddi_deviname(dip, devname); 5172*7c478bd9Sstevel@tonic-gate ndi_devi_exit(pdip, circ); 5173*7c478bd9Sstevel@tonic-gate 5174*7c478bd9Sstevel@tonic-gate /* 5175*7c478bd9Sstevel@tonic-gate * If we own parent lock, this is part of a branch 5176*7c478bd9Sstevel@tonic-gate * operation. We skip the devfs_clean() step. 5177*7c478bd9Sstevel@tonic-gate */ 5178*7c478bd9Sstevel@tonic-gate if (!DEVI_BUSY_OWNED(pdip)) 5179*7c478bd9Sstevel@tonic-gate rval = devfs_clean(pdip, devname + 1, DV_CLEAN_FORCE); 5180*7c478bd9Sstevel@tonic-gate kmem_free(devname, MAXNAMELEN + 1); 5181*7c478bd9Sstevel@tonic-gate 5182*7c478bd9Sstevel@tonic-gate if (rval == 0) 5183*7c478bd9Sstevel@tonic-gate rval = devi_unconfig_branch(dip, NULL, 5184*7c478bd9Sstevel@tonic-gate flags|NDI_UNCONFIG, &brevq); 5185*7c478bd9Sstevel@tonic-gate if (rval) 5186*7c478bd9Sstevel@tonic-gate return (NDI_FAILURE); 5187*7c478bd9Sstevel@tonic-gate 5188*7c478bd9Sstevel@tonic-gate ndi_devi_enter(pdip, &circ); 5189*7c478bd9Sstevel@tonic-gate } 5190*7c478bd9Sstevel@tonic-gate 5191*7c478bd9Sstevel@tonic-gate init_bound_node_ev(pdip, dip, flags); 5192*7c478bd9Sstevel@tonic-gate 5193*7c478bd9Sstevel@tonic-gate rval = devi_detach_node(dip, flags); 5194*7c478bd9Sstevel@tonic-gate if (brevq) { 5195*7c478bd9Sstevel@tonic-gate if (rval != NDI_SUCCESS) 5196*7c478bd9Sstevel@tonic-gate log_and_free_brevq_dip(dip, brevq); 5197*7c478bd9Sstevel@tonic-gate else 5198*7c478bd9Sstevel@tonic-gate free_brevq(brevq); 5199*7c478bd9Sstevel@tonic-gate } 5200*7c478bd9Sstevel@tonic-gate 5201*7c478bd9Sstevel@tonic-gate ndi_devi_exit(pdip, circ); 5202*7c478bd9Sstevel@tonic-gate 5203*7c478bd9Sstevel@tonic-gate return (rval); 5204*7c478bd9Sstevel@tonic-gate } 5205*7c478bd9Sstevel@tonic-gate 5206*7c478bd9Sstevel@tonic-gate /* 5207*7c478bd9Sstevel@tonic-gate * Find the child dev_info node of parent nexus 'p' whose name 5208*7c478bd9Sstevel@tonic-gate * matches "cname@caddr". Recommend use of ndi_devi_findchild() instead. 5209*7c478bd9Sstevel@tonic-gate */ 5210*7c478bd9Sstevel@tonic-gate dev_info_t * 5211*7c478bd9Sstevel@tonic-gate ndi_devi_find(dev_info_t *pdip, char *cname, char *caddr) 5212*7c478bd9Sstevel@tonic-gate { 5213*7c478bd9Sstevel@tonic-gate dev_info_t *child; 5214*7c478bd9Sstevel@tonic-gate int circ; 5215*7c478bd9Sstevel@tonic-gate 5216*7c478bd9Sstevel@tonic-gate if (pdip == NULL || cname == NULL || caddr == NULL) 5217*7c478bd9Sstevel@tonic-gate return ((dev_info_t *)NULL); 5218*7c478bd9Sstevel@tonic-gate 5219*7c478bd9Sstevel@tonic-gate ndi_devi_enter(pdip, &circ); 5220*7c478bd9Sstevel@tonic-gate child = find_sibling(ddi_get_child(pdip), cname, caddr, 0, NULL); 5221*7c478bd9Sstevel@tonic-gate ndi_devi_exit(pdip, circ); 5222*7c478bd9Sstevel@tonic-gate return (child); 5223*7c478bd9Sstevel@tonic-gate } 5224*7c478bd9Sstevel@tonic-gate 5225*7c478bd9Sstevel@tonic-gate /* 5226*7c478bd9Sstevel@tonic-gate * Find the child dev_info node of parent nexus 'p' whose name 5227*7c478bd9Sstevel@tonic-gate * matches devname "name@addr". Permits caller to hold the parent. 5228*7c478bd9Sstevel@tonic-gate */ 5229*7c478bd9Sstevel@tonic-gate dev_info_t * 5230*7c478bd9Sstevel@tonic-gate ndi_devi_findchild(dev_info_t *pdip, char *devname) 5231*7c478bd9Sstevel@tonic-gate { 5232*7c478bd9Sstevel@tonic-gate dev_info_t *child; 5233*7c478bd9Sstevel@tonic-gate char *cname, *caddr; 5234*7c478bd9Sstevel@tonic-gate char *devstr; 5235*7c478bd9Sstevel@tonic-gate 5236*7c478bd9Sstevel@tonic-gate ASSERT(DEVI_BUSY_OWNED(pdip)); 5237*7c478bd9Sstevel@tonic-gate 5238*7c478bd9Sstevel@tonic-gate devstr = i_ddi_strdup(devname, KM_SLEEP); 5239*7c478bd9Sstevel@tonic-gate i_ddi_parse_name(devstr, &cname, &caddr, NULL); 5240*7c478bd9Sstevel@tonic-gate 5241*7c478bd9Sstevel@tonic-gate if (cname == NULL || caddr == NULL) { 5242*7c478bd9Sstevel@tonic-gate kmem_free(devstr, strlen(devname)+1); 5243*7c478bd9Sstevel@tonic-gate return ((dev_info_t *)NULL); 5244*7c478bd9Sstevel@tonic-gate } 5245*7c478bd9Sstevel@tonic-gate 5246*7c478bd9Sstevel@tonic-gate child = find_sibling(ddi_get_child(pdip), cname, caddr, 0, NULL); 5247*7c478bd9Sstevel@tonic-gate kmem_free(devstr, strlen(devname)+1); 5248*7c478bd9Sstevel@tonic-gate return (child); 5249*7c478bd9Sstevel@tonic-gate } 5250*7c478bd9Sstevel@tonic-gate 5251*7c478bd9Sstevel@tonic-gate /* 5252*7c478bd9Sstevel@tonic-gate * Misc. routines called by framework only 5253*7c478bd9Sstevel@tonic-gate */ 5254*7c478bd9Sstevel@tonic-gate 5255*7c478bd9Sstevel@tonic-gate /* 5256*7c478bd9Sstevel@tonic-gate * Clear the DEVI_MADE_CHILDREN/DEVI_ATTACHED_CHILDREN flags 5257*7c478bd9Sstevel@tonic-gate * if new child spec has been added. 5258*7c478bd9Sstevel@tonic-gate */ 5259*7c478bd9Sstevel@tonic-gate static int 5260*7c478bd9Sstevel@tonic-gate reset_nexus_flags(dev_info_t *dip, void *arg) 5261*7c478bd9Sstevel@tonic-gate { 5262*7c478bd9Sstevel@tonic-gate struct hwc_spec *list; 5263*7c478bd9Sstevel@tonic-gate 5264*7c478bd9Sstevel@tonic-gate if (((DEVI(dip)->devi_flags & DEVI_MADE_CHILDREN) == 0) || 5265*7c478bd9Sstevel@tonic-gate ((list = hwc_get_child_spec(dip, (major_t)(uintptr_t)arg)) == NULL)) 5266*7c478bd9Sstevel@tonic-gate return (DDI_WALK_CONTINUE); 5267*7c478bd9Sstevel@tonic-gate 5268*7c478bd9Sstevel@tonic-gate hwc_free_spec_list(list); 5269*7c478bd9Sstevel@tonic-gate mutex_enter(&DEVI(dip)->devi_lock); 5270*7c478bd9Sstevel@tonic-gate DEVI(dip)->devi_flags &= ~(DEVI_MADE_CHILDREN | DEVI_ATTACHED_CHILDREN); 5271*7c478bd9Sstevel@tonic-gate mutex_exit(&DEVI(dip)->devi_lock); 5272*7c478bd9Sstevel@tonic-gate 5273*7c478bd9Sstevel@tonic-gate return (DDI_WALK_CONTINUE); 5274*7c478bd9Sstevel@tonic-gate } 5275*7c478bd9Sstevel@tonic-gate 5276*7c478bd9Sstevel@tonic-gate /* 5277*7c478bd9Sstevel@tonic-gate * Helper functions, returns NULL if no memory. 5278*7c478bd9Sstevel@tonic-gate */ 5279*7c478bd9Sstevel@tonic-gate 5280*7c478bd9Sstevel@tonic-gate /* 5281*7c478bd9Sstevel@tonic-gate * path_to_major: 5282*7c478bd9Sstevel@tonic-gate * 5283*7c478bd9Sstevel@tonic-gate * Return an alternate driver name binding for the leaf device 5284*7c478bd9Sstevel@tonic-gate * of the given pathname, if there is one. The purpose of this 5285*7c478bd9Sstevel@tonic-gate * function is to deal with generic pathnames. The default action 5286*7c478bd9Sstevel@tonic-gate * for platforms that can't do this (ie: x86 or any platform that 5287*7c478bd9Sstevel@tonic-gate * does not have prom_finddevice functionality, which matches 5288*7c478bd9Sstevel@tonic-gate * nodenames and unit-addresses without the drivers participation) 5289*7c478bd9Sstevel@tonic-gate * is to return (major_t)-1. 5290*7c478bd9Sstevel@tonic-gate * 5291*7c478bd9Sstevel@tonic-gate * Used in loadrootmodules() in the swapgeneric module to 5292*7c478bd9Sstevel@tonic-gate * associate a given pathname with a given leaf driver. 5293*7c478bd9Sstevel@tonic-gate * 5294*7c478bd9Sstevel@tonic-gate */ 5295*7c478bd9Sstevel@tonic-gate major_t 5296*7c478bd9Sstevel@tonic-gate path_to_major(char *path) 5297*7c478bd9Sstevel@tonic-gate { 5298*7c478bd9Sstevel@tonic-gate dev_info_t *dip; 5299*7c478bd9Sstevel@tonic-gate char *p, *q; 5300*7c478bd9Sstevel@tonic-gate dnode_t nodeid; 5301*7c478bd9Sstevel@tonic-gate major_t maj; 5302*7c478bd9Sstevel@tonic-gate 5303*7c478bd9Sstevel@tonic-gate /* 5304*7c478bd9Sstevel@tonic-gate * Get the nodeid of the given pathname, if such a mapping exists. 5305*7c478bd9Sstevel@tonic-gate */ 5306*7c478bd9Sstevel@tonic-gate dip = NULL; 5307*7c478bd9Sstevel@tonic-gate nodeid = prom_finddevice(path); 5308*7c478bd9Sstevel@tonic-gate if (nodeid != OBP_BADNODE) { 5309*7c478bd9Sstevel@tonic-gate /* 5310*7c478bd9Sstevel@tonic-gate * Find the nodeid in our copy of the device tree and return 5311*7c478bd9Sstevel@tonic-gate * whatever name we used to bind this node to a driver. 5312*7c478bd9Sstevel@tonic-gate */ 5313*7c478bd9Sstevel@tonic-gate dip = e_ddi_nodeid_to_dip(nodeid); 5314*7c478bd9Sstevel@tonic-gate } 5315*7c478bd9Sstevel@tonic-gate 5316*7c478bd9Sstevel@tonic-gate if (dip == NULL) { 5317*7c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_WARN, 5318*7c478bd9Sstevel@tonic-gate "path_to_major: can't bind <%s>\n", path)); 5319*7c478bd9Sstevel@tonic-gate return ((major_t)-1); 5320*7c478bd9Sstevel@tonic-gate } 5321*7c478bd9Sstevel@tonic-gate 5322*7c478bd9Sstevel@tonic-gate /* 5323*7c478bd9Sstevel@tonic-gate * If we're bound to something other than the nodename, 5324*7c478bd9Sstevel@tonic-gate * note that in the message buffer and system log. 5325*7c478bd9Sstevel@tonic-gate */ 5326*7c478bd9Sstevel@tonic-gate p = ddi_binding_name(dip); 5327*7c478bd9Sstevel@tonic-gate q = ddi_node_name(dip); 5328*7c478bd9Sstevel@tonic-gate if (p && q && (strcmp(p, q) != 0)) 5329*7c478bd9Sstevel@tonic-gate NDI_CONFIG_DEBUG((CE_NOTE, "path_to_major: %s bound to %s\n", 5330*7c478bd9Sstevel@tonic-gate path, p)); 5331*7c478bd9Sstevel@tonic-gate 5332*7c478bd9Sstevel@tonic-gate maj = ddi_name_to_major(p); 5333*7c478bd9Sstevel@tonic-gate 5334*7c478bd9Sstevel@tonic-gate ndi_rele_devi(dip); /* release node held during walk */ 5335*7c478bd9Sstevel@tonic-gate 5336*7c478bd9Sstevel@tonic-gate return (maj); 5337*7c478bd9Sstevel@tonic-gate } 5338*7c478bd9Sstevel@tonic-gate 5339*7c478bd9Sstevel@tonic-gate /* 5340*7c478bd9Sstevel@tonic-gate * Return the held dip for the specified major and instance, attempting to do 5341*7c478bd9Sstevel@tonic-gate * an attach if specified. Return NULL if the devi can't be found or put in 5342*7c478bd9Sstevel@tonic-gate * the proper state. The caller must release the hold via ddi_release_devi if 5343*7c478bd9Sstevel@tonic-gate * a non-NULL value is returned. 5344*7c478bd9Sstevel@tonic-gate * 5345*7c478bd9Sstevel@tonic-gate * Some callers expect to be able to perform a hold_devi() while in a context 5346*7c478bd9Sstevel@tonic-gate * where using ndi_devi_enter() to ensure the hold might cause deadlock (see 5347*7c478bd9Sstevel@tonic-gate * open-from-attach code in consconfig_dacf.c). Such special-case callers 5348*7c478bd9Sstevel@tonic-gate * must ensure that an ndi_devi_enter(parent)/ndi_devi_hold() from a safe 5349*7c478bd9Sstevel@tonic-gate * context is already active. The hold_devi() implementation must accommodate 5350*7c478bd9Sstevel@tonic-gate * these callers. 5351*7c478bd9Sstevel@tonic-gate */ 5352*7c478bd9Sstevel@tonic-gate static dev_info_t * 5353*7c478bd9Sstevel@tonic-gate hold_devi(major_t major, int instance, int flags) 5354*7c478bd9Sstevel@tonic-gate { 5355*7c478bd9Sstevel@tonic-gate struct devnames *dnp; 5356*7c478bd9Sstevel@tonic-gate dev_info_t *dip; 5357*7c478bd9Sstevel@tonic-gate char *path; 5358*7c478bd9Sstevel@tonic-gate 5359*7c478bd9Sstevel@tonic-gate if ((major >= devcnt) || (instance == -1)) 5360*7c478bd9Sstevel@tonic-gate return (NULL); 5361*7c478bd9Sstevel@tonic-gate 5362*7c478bd9Sstevel@tonic-gate /* try to find the instance in the per driver list */ 5363*7c478bd9Sstevel@tonic-gate dnp = &(devnamesp[major]); 5364*7c478bd9Sstevel@tonic-gate LOCK_DEV_OPS(&(dnp->dn_lock)); 5365*7c478bd9Sstevel@tonic-gate for (dip = dnp->dn_head; dip; 5366*7c478bd9Sstevel@tonic-gate dip = (dev_info_t *)DEVI(dip)->devi_next) { 5367*7c478bd9Sstevel@tonic-gate /* skip node if instance field is not valid */ 5368*7c478bd9Sstevel@tonic-gate if (i_ddi_node_state(dip) < DS_INITIALIZED) 5369*7c478bd9Sstevel@tonic-gate continue; 5370*7c478bd9Sstevel@tonic-gate 5371*7c478bd9Sstevel@tonic-gate /* look for instance match */ 5372*7c478bd9Sstevel@tonic-gate if (DEVI(dip)->devi_instance == instance) { 5373*7c478bd9Sstevel@tonic-gate /* 5374*7c478bd9Sstevel@tonic-gate * To accommodate callers that can't block in 5375*7c478bd9Sstevel@tonic-gate * ndi_devi_enter() we do an ndi_devi_hold(), and 5376*7c478bd9Sstevel@tonic-gate * afterwards check that the node is in a state where 5377*7c478bd9Sstevel@tonic-gate * the hold prevents detach(). If we did not manage to 5378*7c478bd9Sstevel@tonic-gate * prevent detach then we ndi_rele_devi() and perform 5379*7c478bd9Sstevel@tonic-gate * the slow path below (which can result in a blocking 5380*7c478bd9Sstevel@tonic-gate * ndi_devi_enter() while driving attach top-down). 5381*7c478bd9Sstevel@tonic-gate * This code depends on the ordering of 5382*7c478bd9Sstevel@tonic-gate * DEVI_SET_DETACHING and the devi_ref check in the 5383*7c478bd9Sstevel@tonic-gate * detach_node() code path. 5384*7c478bd9Sstevel@tonic-gate */ 5385*7c478bd9Sstevel@tonic-gate ndi_hold_devi(dip); 5386*7c478bd9Sstevel@tonic-gate if ((i_ddi_node_state(dip) >= DS_ATTACHED) && 5387*7c478bd9Sstevel@tonic-gate !DEVI_IS_DETACHING(dip)) { 5388*7c478bd9Sstevel@tonic-gate UNLOCK_DEV_OPS(&(dnp->dn_lock)); 5389*7c478bd9Sstevel@tonic-gate return (dip); /* fast-path with devi held */ 5390*7c478bd9Sstevel@tonic-gate } 5391*7c478bd9Sstevel@tonic-gate ndi_rele_devi(dip); 5392*7c478bd9Sstevel@tonic-gate 5393*7c478bd9Sstevel@tonic-gate /* try slow-path */ 5394*7c478bd9Sstevel@tonic-gate dip = NULL; 5395*7c478bd9Sstevel@tonic-gate break; 5396*7c478bd9Sstevel@tonic-gate } 5397*7c478bd9Sstevel@tonic-gate } 5398*7c478bd9Sstevel@tonic-gate ASSERT(dip == NULL); 5399*7c478bd9Sstevel@tonic-gate UNLOCK_DEV_OPS(&(dnp->dn_lock)); 5400*7c478bd9Sstevel@tonic-gate 5401*7c478bd9Sstevel@tonic-gate if (flags & E_DDI_HOLD_DEVI_NOATTACH) 5402*7c478bd9Sstevel@tonic-gate return (NULL); /* told not to drive attach */ 5403*7c478bd9Sstevel@tonic-gate 5404*7c478bd9Sstevel@tonic-gate /* slow-path may block, so it should not occur from interrupt */ 5405*7c478bd9Sstevel@tonic-gate ASSERT(!servicing_interrupt()); 5406*7c478bd9Sstevel@tonic-gate if (servicing_interrupt()) 5407*7c478bd9Sstevel@tonic-gate return (NULL); 5408*7c478bd9Sstevel@tonic-gate 5409*7c478bd9Sstevel@tonic-gate /* reconstruct the path and drive attach by path through devfs. */ 5410*7c478bd9Sstevel@tonic-gate path = kmem_alloc(MAXPATHLEN, KM_SLEEP); 5411*7c478bd9Sstevel@tonic-gate if (e_ddi_majorinstance_to_path(major, instance, path) == 0) 5412*7c478bd9Sstevel@tonic-gate dip = e_ddi_hold_devi_by_path(path, flags); 5413*7c478bd9Sstevel@tonic-gate kmem_free(path, MAXPATHLEN); 5414*7c478bd9Sstevel@tonic-gate return (dip); /* with devi held */ 5415*7c478bd9Sstevel@tonic-gate } 5416*7c478bd9Sstevel@tonic-gate 5417*7c478bd9Sstevel@tonic-gate /* 5418*7c478bd9Sstevel@tonic-gate * The {e_}ddi_hold_devi{_by_{instance|dev|path}} hold the devinfo node 5419*7c478bd9Sstevel@tonic-gate * associated with the specified arguments. This hold should be released 5420*7c478bd9Sstevel@tonic-gate * by calling ddi_release_devi. 5421*7c478bd9Sstevel@tonic-gate * 5422*7c478bd9Sstevel@tonic-gate * The E_DDI_HOLD_DEVI_NOATTACH flag argument allows the caller to to specify 5423*7c478bd9Sstevel@tonic-gate * a failure return if the node is not already attached. 5424*7c478bd9Sstevel@tonic-gate * 5425*7c478bd9Sstevel@tonic-gate * NOTE: by the time we make e_ddi_hold_devi public, we should be able to reuse 5426*7c478bd9Sstevel@tonic-gate * ddi_hold_devi again. 5427*7c478bd9Sstevel@tonic-gate */ 5428*7c478bd9Sstevel@tonic-gate dev_info_t * 5429*7c478bd9Sstevel@tonic-gate ddi_hold_devi_by_instance(major_t major, int instance, int flags) 5430*7c478bd9Sstevel@tonic-gate { 5431*7c478bd9Sstevel@tonic-gate return (hold_devi(major, instance, flags)); 5432*7c478bd9Sstevel@tonic-gate } 5433*7c478bd9Sstevel@tonic-gate 5434*7c478bd9Sstevel@tonic-gate dev_info_t * 5435*7c478bd9Sstevel@tonic-gate e_ddi_hold_devi_by_dev(dev_t dev, int flags) 5436*7c478bd9Sstevel@tonic-gate { 5437*7c478bd9Sstevel@tonic-gate major_t major = getmajor(dev); 5438*7c478bd9Sstevel@tonic-gate dev_info_t *dip; 5439*7c478bd9Sstevel@tonic-gate struct dev_ops *ops; 5440*7c478bd9Sstevel@tonic-gate dev_info_t *ddip = NULL; 5441*7c478bd9Sstevel@tonic-gate 5442*7c478bd9Sstevel@tonic-gate dip = hold_devi(major, dev_to_instance(dev), flags); 5443*7c478bd9Sstevel@tonic-gate 5444*7c478bd9Sstevel@tonic-gate /* 5445*7c478bd9Sstevel@tonic-gate * The rest of this routine is legacy support for drivers that 5446*7c478bd9Sstevel@tonic-gate * have broken DDI_INFO_DEVT2INSTANCE implementations but may have 5447*7c478bd9Sstevel@tonic-gate * functional DDI_INFO_DEVT2DEVINFO implementations. This code will 5448*7c478bd9Sstevel@tonic-gate * diagnose inconsistency and, for maximum compatibility with legacy 5449*7c478bd9Sstevel@tonic-gate * drivers, give preference to the drivers DDI_INFO_DEVT2DEVINFO 5450*7c478bd9Sstevel@tonic-gate * implementation over the above derived dip based the driver's 5451*7c478bd9Sstevel@tonic-gate * DDI_INFO_DEVT2INSTANCE implementation. This legacy support should 5452*7c478bd9Sstevel@tonic-gate * be removed when DDI_INFO_DEVT2DEVINFO is deprecated. 5453*7c478bd9Sstevel@tonic-gate * 5454*7c478bd9Sstevel@tonic-gate * NOTE: The following code has a race condition. DEVT2DEVINFO 5455*7c478bd9Sstevel@tonic-gate * returns a dip which is not held. By the time we ref ddip, 5456*7c478bd9Sstevel@tonic-gate * it could have been freed. The saving grace is that for 5457*7c478bd9Sstevel@tonic-gate * most drivers, the dip returned from hold_devi() is the 5458*7c478bd9Sstevel@tonic-gate * same one as the one returned by DEVT2DEVINFO, so we are 5459*7c478bd9Sstevel@tonic-gate * safe for drivers with the correct getinfo(9e) impl. 5460*7c478bd9Sstevel@tonic-gate */ 5461*7c478bd9Sstevel@tonic-gate if (((ops = ddi_hold_driver(major)) != NULL) && 5462*7c478bd9Sstevel@tonic-gate CB_DRV_INSTALLED(ops) && ops->devo_getinfo) { 5463*7c478bd9Sstevel@tonic-gate if ((*ops->devo_getinfo)(NULL, DDI_INFO_DEVT2DEVINFO, 5464*7c478bd9Sstevel@tonic-gate (void *)dev, (void **)&ddip) != DDI_SUCCESS) 5465*7c478bd9Sstevel@tonic-gate ddip = NULL; 5466*7c478bd9Sstevel@tonic-gate } 5467*7c478bd9Sstevel@tonic-gate 5468*7c478bd9Sstevel@tonic-gate /* give preference to the driver returned DEVT2DEVINFO dip */ 5469*7c478bd9Sstevel@tonic-gate if (ddip && (dip != ddip)) { 5470*7c478bd9Sstevel@tonic-gate #ifdef DEBUG 5471*7c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "%s: inconsistent getinfo(9E) implementation", 5472*7c478bd9Sstevel@tonic-gate ddi_driver_name(ddip)); 5473*7c478bd9Sstevel@tonic-gate #endif /* DEBUG */ 5474*7c478bd9Sstevel@tonic-gate ndi_hold_devi(ddip); 5475*7c478bd9Sstevel@tonic-gate if (dip) 5476*7c478bd9Sstevel@tonic-gate ndi_rele_devi(dip); 5477*7c478bd9Sstevel@tonic-gate dip = ddip; 5478*7c478bd9Sstevel@tonic-gate } 5479*7c478bd9Sstevel@tonic-gate 5480*7c478bd9Sstevel@tonic-gate if (ops) 5481*7c478bd9Sstevel@tonic-gate ddi_rele_driver(major); 5482*7c478bd9Sstevel@tonic-gate 5483*7c478bd9Sstevel@tonic-gate return (dip); 5484*7c478bd9Sstevel@tonic-gate } 5485*7c478bd9Sstevel@tonic-gate 5486*7c478bd9Sstevel@tonic-gate /* 5487*7c478bd9Sstevel@tonic-gate * For compatibility only. Do not call this function! 5488*7c478bd9Sstevel@tonic-gate */ 5489*7c478bd9Sstevel@tonic-gate dev_info_t * 5490*7c478bd9Sstevel@tonic-gate e_ddi_get_dev_info(dev_t dev, vtype_t type) 5491*7c478bd9Sstevel@tonic-gate { 5492*7c478bd9Sstevel@tonic-gate dev_info_t *dip = NULL; 5493*7c478bd9Sstevel@tonic-gate if (getmajor(dev) >= devcnt) 5494*7c478bd9Sstevel@tonic-gate return (NULL); 5495*7c478bd9Sstevel@tonic-gate 5496*7c478bd9Sstevel@tonic-gate switch (type) { 5497*7c478bd9Sstevel@tonic-gate case VCHR: 5498*7c478bd9Sstevel@tonic-gate case VBLK: 5499*7c478bd9Sstevel@tonic-gate dip = e_ddi_hold_devi_by_dev(dev, 0); 5500*7c478bd9Sstevel@tonic-gate default: 5501*7c478bd9Sstevel@tonic-gate break; 5502*7c478bd9Sstevel@tonic-gate } 5503*7c478bd9Sstevel@tonic-gate 5504*7c478bd9Sstevel@tonic-gate /* 5505*7c478bd9Sstevel@tonic-gate * For compatibility reasons, we can only return the dip with 5506*7c478bd9Sstevel@tonic-gate * the driver ref count held. This is not a safe thing to do. 5507*7c478bd9Sstevel@tonic-gate * For certain broken third-party software, we are willing 5508*7c478bd9Sstevel@tonic-gate * to venture into unknown territory. 5509*7c478bd9Sstevel@tonic-gate */ 5510*7c478bd9Sstevel@tonic-gate if (dip) { 5511*7c478bd9Sstevel@tonic-gate (void) ndi_hold_driver(dip); 5512*7c478bd9Sstevel@tonic-gate ndi_rele_devi(dip); 5513*7c478bd9Sstevel@tonic-gate } 5514*7c478bd9Sstevel@tonic-gate return (dip); 5515*7c478bd9Sstevel@tonic-gate } 5516*7c478bd9Sstevel@tonic-gate 5517*7c478bd9Sstevel@tonic-gate dev_info_t * 5518*7c478bd9Sstevel@tonic-gate e_ddi_hold_devi_by_path(char *path, int flags) 5519*7c478bd9Sstevel@tonic-gate { 5520*7c478bd9Sstevel@tonic-gate dev_info_t *dip; 5521*7c478bd9Sstevel@tonic-gate 5522*7c478bd9Sstevel@tonic-gate /* can't specify NOATTACH by path */ 5523*7c478bd9Sstevel@tonic-gate ASSERT(!(flags & E_DDI_HOLD_DEVI_NOATTACH)); 5524*7c478bd9Sstevel@tonic-gate 5525*7c478bd9Sstevel@tonic-gate return (resolve_pathname(path, &dip, NULL, NULL) ? NULL : dip); 5526*7c478bd9Sstevel@tonic-gate } 5527*7c478bd9Sstevel@tonic-gate 5528*7c478bd9Sstevel@tonic-gate void 5529*7c478bd9Sstevel@tonic-gate e_ddi_hold_devi(dev_info_t *dip) 5530*7c478bd9Sstevel@tonic-gate { 5531*7c478bd9Sstevel@tonic-gate ndi_hold_devi(dip); 5532*7c478bd9Sstevel@tonic-gate } 5533*7c478bd9Sstevel@tonic-gate 5534*7c478bd9Sstevel@tonic-gate void 5535*7c478bd9Sstevel@tonic-gate ddi_release_devi(dev_info_t *dip) 5536*7c478bd9Sstevel@tonic-gate { 5537*7c478bd9Sstevel@tonic-gate ndi_rele_devi(dip); 5538*7c478bd9Sstevel@tonic-gate } 5539*7c478bd9Sstevel@tonic-gate 5540*7c478bd9Sstevel@tonic-gate /* 5541*7c478bd9Sstevel@tonic-gate * Associate a streams queue with a devinfo node 5542*7c478bd9Sstevel@tonic-gate * NOTE: This function is called by STREAM driver's put procedure. 5543*7c478bd9Sstevel@tonic-gate * It cannot block. 5544*7c478bd9Sstevel@tonic-gate */ 5545*7c478bd9Sstevel@tonic-gate void 5546*7c478bd9Sstevel@tonic-gate ddi_assoc_queue_with_devi(queue_t *q, dev_info_t *dip) 5547*7c478bd9Sstevel@tonic-gate { 5548*7c478bd9Sstevel@tonic-gate queue_t *rq = _RD(q); 5549*7c478bd9Sstevel@tonic-gate struct stdata *stp; 5550*7c478bd9Sstevel@tonic-gate vnode_t *vp; 5551*7c478bd9Sstevel@tonic-gate 5552*7c478bd9Sstevel@tonic-gate /* set flag indicating that ddi_assoc_queue_with_devi was called */ 5553*7c478bd9Sstevel@tonic-gate mutex_enter(QLOCK(rq)); 5554*7c478bd9Sstevel@tonic-gate rq->q_flag |= _QASSOCIATED; 5555*7c478bd9Sstevel@tonic-gate mutex_exit(QLOCK(rq)); 5556*7c478bd9Sstevel@tonic-gate 5557*7c478bd9Sstevel@tonic-gate /* get the vnode associated with the queue */ 5558*7c478bd9Sstevel@tonic-gate stp = STREAM(rq); 5559*7c478bd9Sstevel@tonic-gate vp = stp->sd_vnode; 5560*7c478bd9Sstevel@tonic-gate ASSERT(vp); 5561*7c478bd9Sstevel@tonic-gate 5562*7c478bd9Sstevel@tonic-gate /* change the hardware association of the vnode */ 5563*7c478bd9Sstevel@tonic-gate spec_assoc_vp_with_devi(vp, dip); 5564*7c478bd9Sstevel@tonic-gate } 5565*7c478bd9Sstevel@tonic-gate 5566*7c478bd9Sstevel@tonic-gate /* 5567*7c478bd9Sstevel@tonic-gate * ddi_install_driver(name) 5568*7c478bd9Sstevel@tonic-gate * 5569*7c478bd9Sstevel@tonic-gate * Driver installation is currently a byproduct of driver loading. This 5570*7c478bd9Sstevel@tonic-gate * may change. 5571*7c478bd9Sstevel@tonic-gate */ 5572*7c478bd9Sstevel@tonic-gate int 5573*7c478bd9Sstevel@tonic-gate ddi_install_driver(char *name) 5574*7c478bd9Sstevel@tonic-gate { 5575*7c478bd9Sstevel@tonic-gate major_t major = ddi_name_to_major(name); 5576*7c478bd9Sstevel@tonic-gate 5577*7c478bd9Sstevel@tonic-gate if ((major == (major_t)-1) || 5578*7c478bd9Sstevel@tonic-gate (ddi_hold_installed_driver(major) == NULL)) { 5579*7c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 5580*7c478bd9Sstevel@tonic-gate } 5581*7c478bd9Sstevel@tonic-gate ddi_rele_driver(major); 5582*7c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 5583*7c478bd9Sstevel@tonic-gate } 5584*7c478bd9Sstevel@tonic-gate 5585*7c478bd9Sstevel@tonic-gate struct dev_ops * 5586*7c478bd9Sstevel@tonic-gate ddi_hold_driver(major_t major) 5587*7c478bd9Sstevel@tonic-gate { 5588*7c478bd9Sstevel@tonic-gate return (mod_hold_dev_by_major(major)); 5589*7c478bd9Sstevel@tonic-gate } 5590*7c478bd9Sstevel@tonic-gate 5591*7c478bd9Sstevel@tonic-gate 5592*7c478bd9Sstevel@tonic-gate void 5593*7c478bd9Sstevel@tonic-gate ddi_rele_driver(major_t major) 5594*7c478bd9Sstevel@tonic-gate { 5595*7c478bd9Sstevel@tonic-gate mod_rele_dev_by_major(major); 5596*7c478bd9Sstevel@tonic-gate } 5597*7c478bd9Sstevel@tonic-gate 5598*7c478bd9Sstevel@tonic-gate 5599*7c478bd9Sstevel@tonic-gate /* 5600*7c478bd9Sstevel@tonic-gate * This is called during boot to force attachment order of special dips 5601*7c478bd9Sstevel@tonic-gate * dip must be referenced via ndi_hold_devi() 5602*7c478bd9Sstevel@tonic-gate */ 5603*7c478bd9Sstevel@tonic-gate int 5604*7c478bd9Sstevel@tonic-gate i_ddi_attach_node_hierarchy(dev_info_t *dip) 5605*7c478bd9Sstevel@tonic-gate { 5606*7c478bd9Sstevel@tonic-gate dev_info_t *parent; 5607*7c478bd9Sstevel@tonic-gate 5608*7c478bd9Sstevel@tonic-gate if (i_ddi_node_state(dip) == DS_READY) 5609*7c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 5610*7c478bd9Sstevel@tonic-gate 5611*7c478bd9Sstevel@tonic-gate /* 5612*7c478bd9Sstevel@tonic-gate * Attach parent dip 5613*7c478bd9Sstevel@tonic-gate */ 5614*7c478bd9Sstevel@tonic-gate parent = ddi_get_parent(dip); 5615*7c478bd9Sstevel@tonic-gate if (i_ddi_attach_node_hierarchy(parent) != DDI_SUCCESS) 5616*7c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 5617*7c478bd9Sstevel@tonic-gate 5618*7c478bd9Sstevel@tonic-gate /* 5619*7c478bd9Sstevel@tonic-gate * Expand .conf nodes under this parent 5620*7c478bd9Sstevel@tonic-gate */ 5621*7c478bd9Sstevel@tonic-gate (void) i_ndi_make_spec_children(parent, 0); 5622*7c478bd9Sstevel@tonic-gate return (i_ddi_attachchild(dip)); 5623*7c478bd9Sstevel@tonic-gate } 5624*7c478bd9Sstevel@tonic-gate 5625*7c478bd9Sstevel@tonic-gate /* keep this function static */ 5626*7c478bd9Sstevel@tonic-gate static int 5627*7c478bd9Sstevel@tonic-gate attach_driver_nodes(major_t major) 5628*7c478bd9Sstevel@tonic-gate { 5629*7c478bd9Sstevel@tonic-gate struct devnames *dnp; 5630*7c478bd9Sstevel@tonic-gate dev_info_t *dip; 5631*7c478bd9Sstevel@tonic-gate int error = DDI_FAILURE; 5632*7c478bd9Sstevel@tonic-gate 5633*7c478bd9Sstevel@tonic-gate dnp = &devnamesp[major]; 5634*7c478bd9Sstevel@tonic-gate LOCK_DEV_OPS(&dnp->dn_lock); 5635*7c478bd9Sstevel@tonic-gate dip = dnp->dn_head; 5636*7c478bd9Sstevel@tonic-gate while (dip) { 5637*7c478bd9Sstevel@tonic-gate ndi_hold_devi(dip); 5638*7c478bd9Sstevel@tonic-gate UNLOCK_DEV_OPS(&dnp->dn_lock); 5639*7c478bd9Sstevel@tonic-gate if (i_ddi_attach_node_hierarchy(dip) == DDI_SUCCESS) 5640*7c478bd9Sstevel@tonic-gate error = DDI_SUCCESS; 5641*7c478bd9Sstevel@tonic-gate LOCK_DEV_OPS(&dnp->dn_lock); 5642*7c478bd9Sstevel@tonic-gate ndi_rele_devi(dip); 5643*7c478bd9Sstevel@tonic-gate dip = ddi_get_next(dip); 5644*7c478bd9Sstevel@tonic-gate } 5645*7c478bd9Sstevel@tonic-gate if (error == DDI_SUCCESS) 5646*7c478bd9Sstevel@tonic-gate dnp->dn_flags |= DN_NO_AUTODETACH; 5647*7c478bd9Sstevel@tonic-gate UNLOCK_DEV_OPS(&dnp->dn_lock); 5648*7c478bd9Sstevel@tonic-gate 5649*7c478bd9Sstevel@tonic-gate 5650*7c478bd9Sstevel@tonic-gate return (error); 5651*7c478bd9Sstevel@tonic-gate } 5652*7c478bd9Sstevel@tonic-gate 5653*7c478bd9Sstevel@tonic-gate /* 5654*7c478bd9Sstevel@tonic-gate * i_ddi_attach_hw_nodes configures and attaches all hw nodes 5655*7c478bd9Sstevel@tonic-gate * bound to a specific driver. This function replaces calls to 5656*7c478bd9Sstevel@tonic-gate * ddi_hold_installed_driver() for drivers with no .conf 5657*7c478bd9Sstevel@tonic-gate * enumerated nodes. 5658*7c478bd9Sstevel@tonic-gate * 5659*7c478bd9Sstevel@tonic-gate * This facility is typically called at boot time to attach 5660*7c478bd9Sstevel@tonic-gate * platform-specific hardware nodes, such as ppm nodes on xcal 5661*7c478bd9Sstevel@tonic-gate * and grover and keyswitch nodes on cherrystone. It does not 5662*7c478bd9Sstevel@tonic-gate * deal with .conf enumerated node. Calling it beyond the boot 5663*7c478bd9Sstevel@tonic-gate * process is strongly discouraged. 5664*7c478bd9Sstevel@tonic-gate */ 5665*7c478bd9Sstevel@tonic-gate int 5666*7c478bd9Sstevel@tonic-gate i_ddi_attach_hw_nodes(char *driver) 5667*7c478bd9Sstevel@tonic-gate { 5668*7c478bd9Sstevel@tonic-gate major_t major; 5669*7c478bd9Sstevel@tonic-gate 5670*7c478bd9Sstevel@tonic-gate major = ddi_name_to_major(driver); 5671*7c478bd9Sstevel@tonic-gate if (major == (major_t)-1) 5672*7c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 5673*7c478bd9Sstevel@tonic-gate 5674*7c478bd9Sstevel@tonic-gate return (attach_driver_nodes(major)); 5675*7c478bd9Sstevel@tonic-gate } 5676*7c478bd9Sstevel@tonic-gate 5677*7c478bd9Sstevel@tonic-gate /* 5678*7c478bd9Sstevel@tonic-gate * i_ddi_attach_pseudo_node configures pseudo drivers which 5679*7c478bd9Sstevel@tonic-gate * has a single node. The .conf nodes must be enumerated 5680*7c478bd9Sstevel@tonic-gate * before calling this interface. The dip is held attached 5681*7c478bd9Sstevel@tonic-gate * upon returning. 5682*7c478bd9Sstevel@tonic-gate * 5683*7c478bd9Sstevel@tonic-gate * This facility should only be called only at boot time 5684*7c478bd9Sstevel@tonic-gate * by the I/O framework. 5685*7c478bd9Sstevel@tonic-gate */ 5686*7c478bd9Sstevel@tonic-gate dev_info_t * 5687*7c478bd9Sstevel@tonic-gate i_ddi_attach_pseudo_node(char *driver) 5688*7c478bd9Sstevel@tonic-gate { 5689*7c478bd9Sstevel@tonic-gate major_t major; 5690*7c478bd9Sstevel@tonic-gate dev_info_t *dip; 5691*7c478bd9Sstevel@tonic-gate 5692*7c478bd9Sstevel@tonic-gate major = ddi_name_to_major(driver); 5693*7c478bd9Sstevel@tonic-gate if (major == (major_t)-1) 5694*7c478bd9Sstevel@tonic-gate return (NULL); 5695*7c478bd9Sstevel@tonic-gate 5696*7c478bd9Sstevel@tonic-gate if (attach_driver_nodes(major) != DDI_SUCCESS) 5697*7c478bd9Sstevel@tonic-gate return (NULL); 5698*7c478bd9Sstevel@tonic-gate 5699*7c478bd9Sstevel@tonic-gate dip = devnamesp[major].dn_head; 5700*7c478bd9Sstevel@tonic-gate ASSERT(dip && ddi_get_next(dip) == NULL); 5701*7c478bd9Sstevel@tonic-gate ndi_hold_devi(dip); 5702*7c478bd9Sstevel@tonic-gate return (dip); 5703*7c478bd9Sstevel@tonic-gate } 5704*7c478bd9Sstevel@tonic-gate 5705*7c478bd9Sstevel@tonic-gate static void 5706*7c478bd9Sstevel@tonic-gate diplist_to_parent_major(dev_info_t *head, char parents[]) 5707*7c478bd9Sstevel@tonic-gate { 5708*7c478bd9Sstevel@tonic-gate major_t major; 5709*7c478bd9Sstevel@tonic-gate dev_info_t *dip, *pdip; 5710*7c478bd9Sstevel@tonic-gate 5711*7c478bd9Sstevel@tonic-gate for (dip = head; dip != NULL; dip = ddi_get_next(dip)) { 5712*7c478bd9Sstevel@tonic-gate pdip = ddi_get_parent(dip); 5713*7c478bd9Sstevel@tonic-gate ASSERT(pdip); /* disallow rootnex.conf nodes */ 5714*7c478bd9Sstevel@tonic-gate major = ddi_driver_major(pdip); 5715*7c478bd9Sstevel@tonic-gate if ((major != (major_t)-1) && parents[major] == 0) 5716*7c478bd9Sstevel@tonic-gate parents[major] = 1; 5717*7c478bd9Sstevel@tonic-gate } 5718*7c478bd9Sstevel@tonic-gate } 5719*7c478bd9Sstevel@tonic-gate 5720*7c478bd9Sstevel@tonic-gate /* 5721*7c478bd9Sstevel@tonic-gate * Call ddi_hold_installed_driver() on each parent major 5722*7c478bd9Sstevel@tonic-gate * and invoke mt_config_driver() to attach child major. 5723*7c478bd9Sstevel@tonic-gate * This is part of the implementation of ddi_hold_installed_driver. 5724*7c478bd9Sstevel@tonic-gate */ 5725*7c478bd9Sstevel@tonic-gate static int 5726*7c478bd9Sstevel@tonic-gate attach_driver_by_parent(major_t child_major, char parents[]) 5727*7c478bd9Sstevel@tonic-gate { 5728*7c478bd9Sstevel@tonic-gate major_t par_major; 5729*7c478bd9Sstevel@tonic-gate struct mt_config_handle *hdl; 5730*7c478bd9Sstevel@tonic-gate int flags = NDI_DEVI_PERSIST | NDI_NO_EVENT; 5731*7c478bd9Sstevel@tonic-gate 5732*7c478bd9Sstevel@tonic-gate hdl = mt_config_init(NULL, NULL, flags, child_major, MT_CONFIG_OP, 5733*7c478bd9Sstevel@tonic-gate NULL); 5734*7c478bd9Sstevel@tonic-gate for (par_major = 0; par_major < devcnt; par_major++) { 5735*7c478bd9Sstevel@tonic-gate /* disallow recursion on the same driver */ 5736*7c478bd9Sstevel@tonic-gate if (parents[par_major] == 0 || par_major == child_major) 5737*7c478bd9Sstevel@tonic-gate continue; 5738*7c478bd9Sstevel@tonic-gate if (ddi_hold_installed_driver(par_major) == NULL) 5739*7c478bd9Sstevel@tonic-gate continue; 5740*7c478bd9Sstevel@tonic-gate hdl->mtc_parmajor = par_major; 5741*7c478bd9Sstevel@tonic-gate mt_config_driver(hdl); 5742*7c478bd9Sstevel@tonic-gate ddi_rele_driver(par_major); 5743*7c478bd9Sstevel@tonic-gate } 5744*7c478bd9Sstevel@tonic-gate (void) mt_config_fini(hdl); 5745*7c478bd9Sstevel@tonic-gate 5746*7c478bd9Sstevel@tonic-gate return (i_ddi_devs_attached(child_major)); 5747*7c478bd9Sstevel@tonic-gate } 5748*7c478bd9Sstevel@tonic-gate 5749*7c478bd9Sstevel@tonic-gate int 5750*7c478bd9Sstevel@tonic-gate i_ddi_devs_attached(major_t major) 5751*7c478bd9Sstevel@tonic-gate { 5752*7c478bd9Sstevel@tonic-gate dev_info_t *dip; 5753*7c478bd9Sstevel@tonic-gate struct devnames *dnp; 5754*7c478bd9Sstevel@tonic-gate int error = DDI_FAILURE; 5755*7c478bd9Sstevel@tonic-gate 5756*7c478bd9Sstevel@tonic-gate /* check for attached instances */ 5757*7c478bd9Sstevel@tonic-gate dnp = &devnamesp[major]; 5758*7c478bd9Sstevel@tonic-gate LOCK_DEV_OPS(&dnp->dn_lock); 5759*7c478bd9Sstevel@tonic-gate for (dip = dnp->dn_head; dip != NULL; dip = ddi_get_next(dip)) { 5760*7c478bd9Sstevel@tonic-gate if (i_ddi_node_state(dip) >= DS_ATTACHED) { 5761*7c478bd9Sstevel@tonic-gate error = DDI_SUCCESS; 5762*7c478bd9Sstevel@tonic-gate break; 5763*7c478bd9Sstevel@tonic-gate } 5764*7c478bd9Sstevel@tonic-gate } 5765*7c478bd9Sstevel@tonic-gate UNLOCK_DEV_OPS(&dnp->dn_lock); 5766*7c478bd9Sstevel@tonic-gate 5767*7c478bd9Sstevel@tonic-gate return (error); 5768*7c478bd9Sstevel@tonic-gate } 5769*7c478bd9Sstevel@tonic-gate 5770*7c478bd9Sstevel@tonic-gate /* 5771*7c478bd9Sstevel@tonic-gate * ddi_hold_installed_driver configures and attaches all 5772*7c478bd9Sstevel@tonic-gate * instances of the specified driver. To accomplish this 5773*7c478bd9Sstevel@tonic-gate * it configures and attaches all possible parents of 5774*7c478bd9Sstevel@tonic-gate * the driver, enumerated both in h/w nodes and in the 5775*7c478bd9Sstevel@tonic-gate * driver's .conf file. 5776*7c478bd9Sstevel@tonic-gate * 5777*7c478bd9Sstevel@tonic-gate * NOTE: This facility is for compatibility purposes only and will 5778*7c478bd9Sstevel@tonic-gate * eventually go away. Its usage is strongly discouraged. 5779*7c478bd9Sstevel@tonic-gate */ 5780*7c478bd9Sstevel@tonic-gate static void 5781*7c478bd9Sstevel@tonic-gate enter_driver(struct devnames *dnp) 5782*7c478bd9Sstevel@tonic-gate { 5783*7c478bd9Sstevel@tonic-gate mutex_enter(&dnp->dn_lock); 5784*7c478bd9Sstevel@tonic-gate ASSERT(dnp->dn_busy_thread != curthread); 5785*7c478bd9Sstevel@tonic-gate while (dnp->dn_flags & DN_DRIVER_BUSY) 5786*7c478bd9Sstevel@tonic-gate cv_wait(&dnp->dn_wait, &dnp->dn_lock); 5787*7c478bd9Sstevel@tonic-gate dnp->dn_flags |= DN_DRIVER_BUSY; 5788*7c478bd9Sstevel@tonic-gate dnp->dn_busy_thread = curthread; 5789*7c478bd9Sstevel@tonic-gate mutex_exit(&dnp->dn_lock); 5790*7c478bd9Sstevel@tonic-gate } 5791*7c478bd9Sstevel@tonic-gate 5792*7c478bd9Sstevel@tonic-gate static void 5793*7c478bd9Sstevel@tonic-gate exit_driver(struct devnames *dnp) 5794*7c478bd9Sstevel@tonic-gate { 5795*7c478bd9Sstevel@tonic-gate mutex_enter(&dnp->dn_lock); 5796*7c478bd9Sstevel@tonic-gate ASSERT(dnp->dn_busy_thread == curthread); 5797*7c478bd9Sstevel@tonic-gate dnp->dn_flags &= ~DN_DRIVER_BUSY; 5798*7c478bd9Sstevel@tonic-gate dnp->dn_busy_thread = NULL; 5799*7c478bd9Sstevel@tonic-gate cv_broadcast(&dnp->dn_wait); 5800*7c478bd9Sstevel@tonic-gate mutex_exit(&dnp->dn_lock); 5801*7c478bd9Sstevel@tonic-gate } 5802*7c478bd9Sstevel@tonic-gate 5803*7c478bd9Sstevel@tonic-gate struct dev_ops * 5804*7c478bd9Sstevel@tonic-gate ddi_hold_installed_driver(major_t major) 5805*7c478bd9Sstevel@tonic-gate { 5806*7c478bd9Sstevel@tonic-gate struct dev_ops *ops; 5807*7c478bd9Sstevel@tonic-gate struct devnames *dnp; 5808*7c478bd9Sstevel@tonic-gate char *parents; 5809*7c478bd9Sstevel@tonic-gate int error; 5810*7c478bd9Sstevel@tonic-gate 5811*7c478bd9Sstevel@tonic-gate ops = ddi_hold_driver(major); 5812*7c478bd9Sstevel@tonic-gate if (ops == NULL) 5813*7c478bd9Sstevel@tonic-gate return (NULL); 5814*7c478bd9Sstevel@tonic-gate 5815*7c478bd9Sstevel@tonic-gate /* 5816*7c478bd9Sstevel@tonic-gate * Return immediately if all the attach operations associated 5817*7c478bd9Sstevel@tonic-gate * with a ddi_hold_installed_driver() call have already been done. 5818*7c478bd9Sstevel@tonic-gate */ 5819*7c478bd9Sstevel@tonic-gate dnp = &devnamesp[major]; 5820*7c478bd9Sstevel@tonic-gate enter_driver(dnp); 5821*7c478bd9Sstevel@tonic-gate if (dnp->dn_flags & DN_DRIVER_HELD) { 5822*7c478bd9Sstevel@tonic-gate exit_driver(dnp); 5823*7c478bd9Sstevel@tonic-gate if (i_ddi_devs_attached(major) == DDI_SUCCESS) 5824*7c478bd9Sstevel@tonic-gate return (ops); 5825*7c478bd9Sstevel@tonic-gate ddi_rele_driver(major); 5826*7c478bd9Sstevel@tonic-gate return (NULL); 5827*7c478bd9Sstevel@tonic-gate } 5828*7c478bd9Sstevel@tonic-gate 5829*7c478bd9Sstevel@tonic-gate LOCK_DEV_OPS(&dnp->dn_lock); 5830*7c478bd9Sstevel@tonic-gate dnp->dn_flags |= (DN_DRIVER_HELD | DN_NO_AUTODETACH); 5831*7c478bd9Sstevel@tonic-gate UNLOCK_DEV_OPS(&dnp->dn_lock); 5832*7c478bd9Sstevel@tonic-gate 5833*7c478bd9Sstevel@tonic-gate DCOMPATPRINTF((CE_CONT, 5834*7c478bd9Sstevel@tonic-gate "ddi_hold_installed_driver: %s\n", dnp->dn_name)); 5835*7c478bd9Sstevel@tonic-gate 5836*7c478bd9Sstevel@tonic-gate /* 5837*7c478bd9Sstevel@tonic-gate * When the driver has no .conf children, it is sufficient 5838*7c478bd9Sstevel@tonic-gate * to attach existing nodes in the device tree. Nodes not 5839*7c478bd9Sstevel@tonic-gate * enumerated by the OBP are not attached. 5840*7c478bd9Sstevel@tonic-gate */ 5841*7c478bd9Sstevel@tonic-gate if (dnp->dn_pl == NULL) { 5842*7c478bd9Sstevel@tonic-gate if (attach_driver_nodes(major) == DDI_SUCCESS) { 5843*7c478bd9Sstevel@tonic-gate exit_driver(dnp); 5844*7c478bd9Sstevel@tonic-gate return (ops); 5845*7c478bd9Sstevel@tonic-gate } 5846*7c478bd9Sstevel@tonic-gate exit_driver(dnp); 5847*7c478bd9Sstevel@tonic-gate ddi_rele_driver(major); 5848*7c478bd9Sstevel@tonic-gate return (NULL); 5849*7c478bd9Sstevel@tonic-gate } 5850*7c478bd9Sstevel@tonic-gate 5851*7c478bd9Sstevel@tonic-gate /* 5852*7c478bd9Sstevel@tonic-gate * Driver has .conf nodes. We find all possible parents 5853*7c478bd9Sstevel@tonic-gate * and recursively all ddi_hold_installed_driver on the 5854*7c478bd9Sstevel@tonic-gate * parent driver; then we invoke ndi_config_driver() 5855*7c478bd9Sstevel@tonic-gate * on all possible parent node in parallel to speed up 5856*7c478bd9Sstevel@tonic-gate * performance. 5857*7c478bd9Sstevel@tonic-gate */ 5858*7c478bd9Sstevel@tonic-gate parents = kmem_zalloc(devcnt * sizeof (char), KM_SLEEP); 5859*7c478bd9Sstevel@tonic-gate 5860*7c478bd9Sstevel@tonic-gate LOCK_DEV_OPS(&dnp->dn_lock); 5861*7c478bd9Sstevel@tonic-gate /* find .conf parents */ 5862*7c478bd9Sstevel@tonic-gate (void) impl_parlist_to_major(dnp->dn_pl, parents); 5863*7c478bd9Sstevel@tonic-gate /* find hw node parents */ 5864*7c478bd9Sstevel@tonic-gate diplist_to_parent_major(dnp->dn_head, parents); 5865*7c478bd9Sstevel@tonic-gate UNLOCK_DEV_OPS(&dnp->dn_lock); 5866*7c478bd9Sstevel@tonic-gate 5867*7c478bd9Sstevel@tonic-gate error = attach_driver_by_parent(major, parents); 5868*7c478bd9Sstevel@tonic-gate kmem_free(parents, devcnt * sizeof (char)); 5869*7c478bd9Sstevel@tonic-gate if (error == DDI_SUCCESS) { 5870*7c478bd9Sstevel@tonic-gate exit_driver(dnp); 5871*7c478bd9Sstevel@tonic-gate return (ops); 5872*7c478bd9Sstevel@tonic-gate } 5873*7c478bd9Sstevel@tonic-gate 5874*7c478bd9Sstevel@tonic-gate exit_driver(dnp); 5875*7c478bd9Sstevel@tonic-gate ddi_rele_driver(major); 5876*7c478bd9Sstevel@tonic-gate return (NULL); 5877*7c478bd9Sstevel@tonic-gate } 5878*7c478bd9Sstevel@tonic-gate 5879*7c478bd9Sstevel@tonic-gate /* 5880*7c478bd9Sstevel@tonic-gate * Default bus_config entry point for nexus drivers 5881*7c478bd9Sstevel@tonic-gate */ 5882*7c478bd9Sstevel@tonic-gate int 5883*7c478bd9Sstevel@tonic-gate ndi_busop_bus_config(dev_info_t *pdip, uint_t flags, ddi_bus_config_op_t op, 5884*7c478bd9Sstevel@tonic-gate void *arg, dev_info_t **child, clock_t timeout) 5885*7c478bd9Sstevel@tonic-gate { 5886*7c478bd9Sstevel@tonic-gate major_t major; 5887*7c478bd9Sstevel@tonic-gate 5888*7c478bd9Sstevel@tonic-gate /* 5889*7c478bd9Sstevel@tonic-gate * A timeout of 30 minutes or more is probably a mistake 5890*7c478bd9Sstevel@tonic-gate * This is intended to catch uses where timeout is in 5891*7c478bd9Sstevel@tonic-gate * the wrong units. timeout must be in units of ticks. 5892*7c478bd9Sstevel@tonic-gate */ 5893*7c478bd9Sstevel@tonic-gate ASSERT(timeout < SEC_TO_TICK(1800)); 5894*7c478bd9Sstevel@tonic-gate 5895*7c478bd9Sstevel@tonic-gate major = (major_t)-1; 5896*7c478bd9Sstevel@tonic-gate switch (op) { 5897*7c478bd9Sstevel@tonic-gate case BUS_CONFIG_ONE: 5898*7c478bd9Sstevel@tonic-gate NDI_DEBUG(flags, (CE_CONT, "%s%d: bus config %s timeout=%ld\n", 5899*7c478bd9Sstevel@tonic-gate ddi_driver_name(pdip), ddi_get_instance(pdip), 5900*7c478bd9Sstevel@tonic-gate (char *)arg, timeout)); 5901*7c478bd9Sstevel@tonic-gate return (devi_config_one(pdip, (char *)arg, child, flags, 5902*7c478bd9Sstevel@tonic-gate timeout)); 5903*7c478bd9Sstevel@tonic-gate 5904*7c478bd9Sstevel@tonic-gate case BUS_CONFIG_DRIVER: 5905*7c478bd9Sstevel@tonic-gate major = (major_t)(uintptr_t)arg; 5906*7c478bd9Sstevel@tonic-gate /*FALLTHROUGH*/ 5907*7c478bd9Sstevel@tonic-gate case BUS_CONFIG_ALL: 5908*7c478bd9Sstevel@tonic-gate NDI_DEBUG(flags, (CE_CONT, "%s%d: bus config timeout=%ld\n", 5909*7c478bd9Sstevel@tonic-gate ddi_driver_name(pdip), ddi_get_instance(pdip), 5910*7c478bd9Sstevel@tonic-gate timeout)); 5911*7c478bd9Sstevel@tonic-gate if (timeout > 0) { 5912*7c478bd9Sstevel@tonic-gate NDI_DEBUG(flags, (CE_CONT, 5913*7c478bd9Sstevel@tonic-gate "%s%d: bus config all timeout=%ld\n", 5914*7c478bd9Sstevel@tonic-gate ddi_driver_name(pdip), ddi_get_instance(pdip), 5915*7c478bd9Sstevel@tonic-gate timeout)); 5916*7c478bd9Sstevel@tonic-gate delay(timeout); 5917*7c478bd9Sstevel@tonic-gate } 5918*7c478bd9Sstevel@tonic-gate return (config_immediate_children(pdip, flags, major)); 5919*7c478bd9Sstevel@tonic-gate 5920*7c478bd9Sstevel@tonic-gate default: 5921*7c478bd9Sstevel@tonic-gate return (NDI_FAILURE); 5922*7c478bd9Sstevel@tonic-gate } 5923*7c478bd9Sstevel@tonic-gate /*NOTREACHED*/ 5924*7c478bd9Sstevel@tonic-gate } 5925*7c478bd9Sstevel@tonic-gate 5926*7c478bd9Sstevel@tonic-gate /* 5927*7c478bd9Sstevel@tonic-gate * Default busop bus_unconfig handler for nexus drivers 5928*7c478bd9Sstevel@tonic-gate */ 5929*7c478bd9Sstevel@tonic-gate int 5930*7c478bd9Sstevel@tonic-gate ndi_busop_bus_unconfig(dev_info_t *pdip, uint_t flags, ddi_bus_config_op_t op, 5931*7c478bd9Sstevel@tonic-gate void *arg) 5932*7c478bd9Sstevel@tonic-gate { 5933*7c478bd9Sstevel@tonic-gate major_t major; 5934*7c478bd9Sstevel@tonic-gate 5935*7c478bd9Sstevel@tonic-gate major = (major_t)-1; 5936*7c478bd9Sstevel@tonic-gate switch (op) { 5937*7c478bd9Sstevel@tonic-gate case BUS_UNCONFIG_ONE: 5938*7c478bd9Sstevel@tonic-gate NDI_DEBUG(flags, (CE_CONT, "%s%d: bus unconfig %s\n", 5939*7c478bd9Sstevel@tonic-gate ddi_driver_name(pdip), ddi_get_instance(pdip), 5940*7c478bd9Sstevel@tonic-gate (char *)arg)); 5941*7c478bd9Sstevel@tonic-gate return (devi_unconfig_one(pdip, (char *)arg, flags)); 5942*7c478bd9Sstevel@tonic-gate 5943*7c478bd9Sstevel@tonic-gate case BUS_UNCONFIG_DRIVER: 5944*7c478bd9Sstevel@tonic-gate major = (major_t)(uintptr_t)arg; 5945*7c478bd9Sstevel@tonic-gate /*FALLTHROUGH*/ 5946*7c478bd9Sstevel@tonic-gate case BUS_UNCONFIG_ALL: 5947*7c478bd9Sstevel@tonic-gate NDI_DEBUG(flags, (CE_CONT, "%s%d: bus unconfig all\n", 5948*7c478bd9Sstevel@tonic-gate ddi_driver_name(pdip), ddi_get_instance(pdip))); 5949*7c478bd9Sstevel@tonic-gate return (unconfig_immediate_children(pdip, NULL, flags, major)); 5950*7c478bd9Sstevel@tonic-gate 5951*7c478bd9Sstevel@tonic-gate default: 5952*7c478bd9Sstevel@tonic-gate return (NDI_FAILURE); 5953*7c478bd9Sstevel@tonic-gate } 5954*7c478bd9Sstevel@tonic-gate /*NOTREACHED*/ 5955*7c478bd9Sstevel@tonic-gate } 5956*7c478bd9Sstevel@tonic-gate 5957*7c478bd9Sstevel@tonic-gate /* 5958*7c478bd9Sstevel@tonic-gate * dummy functions to be removed 5959*7c478bd9Sstevel@tonic-gate */ 5960*7c478bd9Sstevel@tonic-gate void 5961*7c478bd9Sstevel@tonic-gate impl_rem_dev_props(dev_info_t *dip) 5962*7c478bd9Sstevel@tonic-gate { 5963*7c478bd9Sstevel@tonic-gate _NOTE(ARGUNUSED(dip)) 5964*7c478bd9Sstevel@tonic-gate /* do nothing */ 5965*7c478bd9Sstevel@tonic-gate } 5966*7c478bd9Sstevel@tonic-gate 5967*7c478bd9Sstevel@tonic-gate /* 5968*7c478bd9Sstevel@tonic-gate * Determine if a node is a leaf node. If not sure, return false (0). 5969*7c478bd9Sstevel@tonic-gate */ 5970*7c478bd9Sstevel@tonic-gate static int 5971*7c478bd9Sstevel@tonic-gate is_leaf_node(dev_info_t *dip) 5972*7c478bd9Sstevel@tonic-gate { 5973*7c478bd9Sstevel@tonic-gate major_t major = ddi_driver_major(dip); 5974*7c478bd9Sstevel@tonic-gate 5975*7c478bd9Sstevel@tonic-gate if (major == (major_t)-1) 5976*7c478bd9Sstevel@tonic-gate return (0); 5977*7c478bd9Sstevel@tonic-gate 5978*7c478bd9Sstevel@tonic-gate return (devnamesp[major].dn_flags & DN_LEAF_DRIVER); 5979*7c478bd9Sstevel@tonic-gate } 5980*7c478bd9Sstevel@tonic-gate 5981*7c478bd9Sstevel@tonic-gate /* 5982*7c478bd9Sstevel@tonic-gate * Multithreaded [un]configuration 5983*7c478bd9Sstevel@tonic-gate */ 5984*7c478bd9Sstevel@tonic-gate static struct mt_config_handle * 5985*7c478bd9Sstevel@tonic-gate mt_config_init(dev_info_t *pdip, dev_info_t **dipp, int flags, 5986*7c478bd9Sstevel@tonic-gate major_t major, int op, struct brevq_node **brevqp) 5987*7c478bd9Sstevel@tonic-gate { 5988*7c478bd9Sstevel@tonic-gate struct mt_config_handle *hdl = kmem_alloc(sizeof (*hdl), KM_SLEEP); 5989*7c478bd9Sstevel@tonic-gate 5990*7c478bd9Sstevel@tonic-gate mutex_init(&hdl->mtc_lock, NULL, MUTEX_DEFAULT, NULL); 5991*7c478bd9Sstevel@tonic-gate cv_init(&hdl->mtc_cv, NULL, CV_DEFAULT, NULL); 5992*7c478bd9Sstevel@tonic-gate hdl->mtc_pdip = pdip; 5993*7c478bd9Sstevel@tonic-gate hdl->mtc_fdip = dipp; 5994*7c478bd9Sstevel@tonic-gate hdl->mtc_parmajor = (major_t)-1; 5995*7c478bd9Sstevel@tonic-gate hdl->mtc_flags = flags; 5996*7c478bd9Sstevel@tonic-gate hdl->mtc_major = major; 5997*7c478bd9Sstevel@tonic-gate hdl->mtc_thr_count = 0; 5998*7c478bd9Sstevel@tonic-gate hdl->mtc_op = op; 5999*7c478bd9Sstevel@tonic-gate hdl->mtc_error = 0; 6000*7c478bd9Sstevel@tonic-gate hdl->mtc_brevqp = brevqp; 6001*7c478bd9Sstevel@tonic-gate 6002*7c478bd9Sstevel@tonic-gate #ifdef DEBUG 6003*7c478bd9Sstevel@tonic-gate gethrestime(&hdl->start_time); 6004*7c478bd9Sstevel@tonic-gate hdl->total_time = 0; 6005*7c478bd9Sstevel@tonic-gate #endif /* DEBUG */ 6006*7c478bd9Sstevel@tonic-gate 6007*7c478bd9Sstevel@tonic-gate return (hdl); 6008*7c478bd9Sstevel@tonic-gate } 6009*7c478bd9Sstevel@tonic-gate 6010*7c478bd9Sstevel@tonic-gate #ifdef DEBUG 6011*7c478bd9Sstevel@tonic-gate static int 6012*7c478bd9Sstevel@tonic-gate time_diff_in_msec(timestruc_t start, timestruc_t end) 6013*7c478bd9Sstevel@tonic-gate { 6014*7c478bd9Sstevel@tonic-gate int nsec, sec; 6015*7c478bd9Sstevel@tonic-gate 6016*7c478bd9Sstevel@tonic-gate sec = end.tv_sec - start.tv_sec; 6017*7c478bd9Sstevel@tonic-gate nsec = end.tv_nsec - start.tv_nsec; 6018*7c478bd9Sstevel@tonic-gate if (nsec < 0) { 6019*7c478bd9Sstevel@tonic-gate nsec += NANOSEC; 6020*7c478bd9Sstevel@tonic-gate sec -= 1; 6021*7c478bd9Sstevel@tonic-gate } 6022*7c478bd9Sstevel@tonic-gate 6023*7c478bd9Sstevel@tonic-gate return (sec * (NANOSEC >> 20) + (nsec >> 20)); 6024*7c478bd9Sstevel@tonic-gate } 6025*7c478bd9Sstevel@tonic-gate 6026*7c478bd9Sstevel@tonic-gate #endif /* DEBUG */ 6027*7c478bd9Sstevel@tonic-gate 6028*7c478bd9Sstevel@tonic-gate static int 6029*7c478bd9Sstevel@tonic-gate mt_config_fini(struct mt_config_handle *hdl) 6030*7c478bd9Sstevel@tonic-gate { 6031*7c478bd9Sstevel@tonic-gate int rv; 6032*7c478bd9Sstevel@tonic-gate #ifdef DEBUG 6033*7c478bd9Sstevel@tonic-gate int real_time; 6034*7c478bd9Sstevel@tonic-gate timestruc_t end_time; 6035*7c478bd9Sstevel@tonic-gate #endif /* DEBUG */ 6036*7c478bd9Sstevel@tonic-gate 6037*7c478bd9Sstevel@tonic-gate mutex_enter(&hdl->mtc_lock); 6038*7c478bd9Sstevel@tonic-gate while (hdl->mtc_thr_count > 0) 6039*7c478bd9Sstevel@tonic-gate cv_wait(&hdl->mtc_cv, &hdl->mtc_lock); 6040*7c478bd9Sstevel@tonic-gate rv = hdl->mtc_error; 6041*7c478bd9Sstevel@tonic-gate mutex_exit(&hdl->mtc_lock); 6042*7c478bd9Sstevel@tonic-gate 6043*7c478bd9Sstevel@tonic-gate #ifdef DEBUG 6044*7c478bd9Sstevel@tonic-gate gethrestime(&end_time); 6045*7c478bd9Sstevel@tonic-gate real_time = time_diff_in_msec(hdl->start_time, end_time); 6046*7c478bd9Sstevel@tonic-gate if ((ddidebug & DDI_MTCONFIG) && hdl->mtc_pdip) 6047*7c478bd9Sstevel@tonic-gate cmn_err(CE_NOTE, 6048*7c478bd9Sstevel@tonic-gate "config %s%d: total time %d msec, real time %d msec", 6049*7c478bd9Sstevel@tonic-gate ddi_driver_name(hdl->mtc_pdip), 6050*7c478bd9Sstevel@tonic-gate ddi_get_instance(hdl->mtc_pdip), 6051*7c478bd9Sstevel@tonic-gate hdl->total_time, real_time); 6052*7c478bd9Sstevel@tonic-gate #endif /* DEBUG */ 6053*7c478bd9Sstevel@tonic-gate 6054*7c478bd9Sstevel@tonic-gate cv_destroy(&hdl->mtc_cv); 6055*7c478bd9Sstevel@tonic-gate mutex_destroy(&hdl->mtc_lock); 6056*7c478bd9Sstevel@tonic-gate kmem_free(hdl, sizeof (*hdl)); 6057*7c478bd9Sstevel@tonic-gate 6058*7c478bd9Sstevel@tonic-gate return (rv); 6059*7c478bd9Sstevel@tonic-gate } 6060*7c478bd9Sstevel@tonic-gate 6061*7c478bd9Sstevel@tonic-gate struct mt_config_data { 6062*7c478bd9Sstevel@tonic-gate struct mt_config_handle *mtc_hdl; 6063*7c478bd9Sstevel@tonic-gate dev_info_t *mtc_dip; 6064*7c478bd9Sstevel@tonic-gate major_t mtc_major; 6065*7c478bd9Sstevel@tonic-gate int mtc_flags; 6066*7c478bd9Sstevel@tonic-gate struct brevq_node *mtc_brn; 6067*7c478bd9Sstevel@tonic-gate struct mt_config_data *mtc_next; 6068*7c478bd9Sstevel@tonic-gate }; 6069*7c478bd9Sstevel@tonic-gate 6070*7c478bd9Sstevel@tonic-gate static void 6071*7c478bd9Sstevel@tonic-gate mt_config_thread(void *arg) 6072*7c478bd9Sstevel@tonic-gate { 6073*7c478bd9Sstevel@tonic-gate struct mt_config_data *mcd = (struct mt_config_data *)arg; 6074*7c478bd9Sstevel@tonic-gate struct mt_config_handle *hdl = mcd->mtc_hdl; 6075*7c478bd9Sstevel@tonic-gate dev_info_t *dip = mcd->mtc_dip; 6076*7c478bd9Sstevel@tonic-gate dev_info_t *rdip, **dipp; 6077*7c478bd9Sstevel@tonic-gate major_t major = mcd->mtc_major; 6078*7c478bd9Sstevel@tonic-gate int flags = mcd->mtc_flags; 6079*7c478bd9Sstevel@tonic-gate int rv = 0; 6080*7c478bd9Sstevel@tonic-gate 6081*7c478bd9Sstevel@tonic-gate #ifdef DEBUG 6082*7c478bd9Sstevel@tonic-gate timestruc_t start_time, end_time; 6083*7c478bd9Sstevel@tonic-gate gethrestime(&start_time); 6084*7c478bd9Sstevel@tonic-gate #endif /* DEBUG */ 6085*7c478bd9Sstevel@tonic-gate 6086*7c478bd9Sstevel@tonic-gate rdip = NULL; 6087*7c478bd9Sstevel@tonic-gate dipp = hdl->mtc_fdip ? &rdip : NULL; 6088*7c478bd9Sstevel@tonic-gate 6089*7c478bd9Sstevel@tonic-gate switch (hdl->mtc_op) { 6090*7c478bd9Sstevel@tonic-gate case MT_CONFIG_OP: 6091*7c478bd9Sstevel@tonic-gate rv = devi_config_common(dip, flags, major); 6092*7c478bd9Sstevel@tonic-gate break; 6093*7c478bd9Sstevel@tonic-gate case MT_UNCONFIG_OP: 6094*7c478bd9Sstevel@tonic-gate if (mcd->mtc_brn) { 6095*7c478bd9Sstevel@tonic-gate struct brevq_node *brevq = NULL; 6096*7c478bd9Sstevel@tonic-gate rv = devi_unconfig_common(dip, dipp, flags, major, 6097*7c478bd9Sstevel@tonic-gate &brevq); 6098*7c478bd9Sstevel@tonic-gate mcd->mtc_brn->child = brevq; 6099*7c478bd9Sstevel@tonic-gate } else 6100*7c478bd9Sstevel@tonic-gate rv = devi_unconfig_common(dip, dipp, flags, major, 6101*7c478bd9Sstevel@tonic-gate NULL); 6102*7c478bd9Sstevel@tonic-gate break; 6103*7c478bd9Sstevel@tonic-gate } 6104*7c478bd9Sstevel@tonic-gate 6105*7c478bd9Sstevel@tonic-gate mutex_enter(&hdl->mtc_lock); 6106*7c478bd9Sstevel@tonic-gate #ifdef DEBUG 6107*7c478bd9Sstevel@tonic-gate gethrestime(&end_time); 6108*7c478bd9Sstevel@tonic-gate hdl->total_time += time_diff_in_msec(start_time, end_time); 6109*7c478bd9Sstevel@tonic-gate #endif /* DEBUG */ 6110*7c478bd9Sstevel@tonic-gate if (rv != NDI_SUCCESS) 6111*7c478bd9Sstevel@tonic-gate hdl->mtc_error = rv; 6112*7c478bd9Sstevel@tonic-gate if (hdl->mtc_fdip && *hdl->mtc_fdip == NULL) { 6113*7c478bd9Sstevel@tonic-gate *hdl->mtc_fdip = rdip; 6114*7c478bd9Sstevel@tonic-gate rdip = NULL; 6115*7c478bd9Sstevel@tonic-gate } 6116*7c478bd9Sstevel@tonic-gate 6117*7c478bd9Sstevel@tonic-gate if (--hdl->mtc_thr_count == 0) 6118*7c478bd9Sstevel@tonic-gate cv_broadcast(&hdl->mtc_cv); 6119*7c478bd9Sstevel@tonic-gate mutex_exit(&hdl->mtc_lock); 6120*7c478bd9Sstevel@tonic-gate 6121*7c478bd9Sstevel@tonic-gate if (rdip) { 6122*7c478bd9Sstevel@tonic-gate ASSERT(rv != NDI_SUCCESS); 6123*7c478bd9Sstevel@tonic-gate ndi_rele_devi(rdip); 6124*7c478bd9Sstevel@tonic-gate } 6125*7c478bd9Sstevel@tonic-gate 6126*7c478bd9Sstevel@tonic-gate ndi_rele_devi(dip); 6127*7c478bd9Sstevel@tonic-gate kmem_free(mcd, sizeof (*mcd)); 6128*7c478bd9Sstevel@tonic-gate } 6129*7c478bd9Sstevel@tonic-gate 6130*7c478bd9Sstevel@tonic-gate /* 6131*7c478bd9Sstevel@tonic-gate * Multi-threaded config/unconfig of child nexus 6132*7c478bd9Sstevel@tonic-gate */ 6133*7c478bd9Sstevel@tonic-gate static void 6134*7c478bd9Sstevel@tonic-gate mt_config_children(struct mt_config_handle *hdl) 6135*7c478bd9Sstevel@tonic-gate { 6136*7c478bd9Sstevel@tonic-gate dev_info_t *pdip = hdl->mtc_pdip; 6137*7c478bd9Sstevel@tonic-gate major_t major = hdl->mtc_major; 6138*7c478bd9Sstevel@tonic-gate dev_info_t *dip; 6139*7c478bd9Sstevel@tonic-gate int circ; 6140*7c478bd9Sstevel@tonic-gate struct brevq_node *brn = NULL; 6141*7c478bd9Sstevel@tonic-gate struct mt_config_data *mcd_head = NULL; 6142*7c478bd9Sstevel@tonic-gate struct mt_config_data *mcd_tail = NULL; 6143*7c478bd9Sstevel@tonic-gate struct mt_config_data *mcd; 6144*7c478bd9Sstevel@tonic-gate #ifdef DEBUG 6145*7c478bd9Sstevel@tonic-gate timestruc_t end_time; 6146*7c478bd9Sstevel@tonic-gate 6147*7c478bd9Sstevel@tonic-gate /* Update total_time in handle */ 6148*7c478bd9Sstevel@tonic-gate gethrestime(&end_time); 6149*7c478bd9Sstevel@tonic-gate hdl->total_time += time_diff_in_msec(hdl->start_time, end_time); 6150*7c478bd9Sstevel@tonic-gate #endif 6151*7c478bd9Sstevel@tonic-gate 6152*7c478bd9Sstevel@tonic-gate ndi_devi_enter(pdip, &circ); 6153*7c478bd9Sstevel@tonic-gate dip = ddi_get_child(pdip); 6154*7c478bd9Sstevel@tonic-gate while (dip) { 6155*7c478bd9Sstevel@tonic-gate if (hdl->mtc_op == MT_UNCONFIG_OP && hdl->mtc_brevqp && 6156*7c478bd9Sstevel@tonic-gate !(DEVI_EVREMOVE(dip)) && 6157*7c478bd9Sstevel@tonic-gate i_ddi_node_state(dip) >= DS_INITIALIZED) { 6158*7c478bd9Sstevel@tonic-gate /* 6159*7c478bd9Sstevel@tonic-gate * Enqueue this dip's deviname. 6160*7c478bd9Sstevel@tonic-gate * No need to hold a lock while enqueuing since this 6161*7c478bd9Sstevel@tonic-gate * is the only thread doing the enqueue and no one 6162*7c478bd9Sstevel@tonic-gate * walks the queue while we are in multithreaded 6163*7c478bd9Sstevel@tonic-gate * unconfiguration. 6164*7c478bd9Sstevel@tonic-gate */ 6165*7c478bd9Sstevel@tonic-gate brn = brevq_enqueue(hdl->mtc_brevqp, dip, NULL); 6166*7c478bd9Sstevel@tonic-gate } 6167*7c478bd9Sstevel@tonic-gate 6168*7c478bd9Sstevel@tonic-gate /* 6169*7c478bd9Sstevel@tonic-gate * Hold the child that we are processing so he does not get 6170*7c478bd9Sstevel@tonic-gate * removed. The corrisponding ndi_rele_devi() for children 6171*7c478bd9Sstevel@tonic-gate * that are not being skipped is done at the end of 6172*7c478bd9Sstevel@tonic-gate * mt_config_thread(). 6173*7c478bd9Sstevel@tonic-gate */ 6174*7c478bd9Sstevel@tonic-gate ndi_hold_devi(dip); 6175*7c478bd9Sstevel@tonic-gate 6176*7c478bd9Sstevel@tonic-gate /* 6177*7c478bd9Sstevel@tonic-gate * skip leaf nodes and (for configure) nodes not 6178*7c478bd9Sstevel@tonic-gate * fully attached. 6179*7c478bd9Sstevel@tonic-gate */ 6180*7c478bd9Sstevel@tonic-gate if (is_leaf_node(dip) || 6181*7c478bd9Sstevel@tonic-gate (hdl->mtc_op == MT_CONFIG_OP && 6182*7c478bd9Sstevel@tonic-gate i_ddi_node_state(dip) < DS_READY)) { 6183*7c478bd9Sstevel@tonic-gate ndi_rele_devi(dip); 6184*7c478bd9Sstevel@tonic-gate dip = ddi_get_next_sibling(dip); 6185*7c478bd9Sstevel@tonic-gate continue; 6186*7c478bd9Sstevel@tonic-gate } 6187*7c478bd9Sstevel@tonic-gate 6188*7c478bd9Sstevel@tonic-gate mcd = kmem_alloc(sizeof (*mcd), KM_SLEEP); 6189*7c478bd9Sstevel@tonic-gate mcd->mtc_dip = dip; 6190*7c478bd9Sstevel@tonic-gate mcd->mtc_hdl = hdl; 6191*7c478bd9Sstevel@tonic-gate mcd->mtc_brn = brn; 6192*7c478bd9Sstevel@tonic-gate 6193*7c478bd9Sstevel@tonic-gate /* 6194*7c478bd9Sstevel@tonic-gate * Switch a 'driver' operation to an 'all' operation below a 6195*7c478bd9Sstevel@tonic-gate * node bound to the driver. 6196*7c478bd9Sstevel@tonic-gate */ 6197*7c478bd9Sstevel@tonic-gate if ((major == (major_t)-1) || (major == ddi_driver_major(pdip))) 6198*7c478bd9Sstevel@tonic-gate mcd->mtc_major = (major_t)-1; 6199*7c478bd9Sstevel@tonic-gate else 6200*7c478bd9Sstevel@tonic-gate mcd->mtc_major = major; 6201*7c478bd9Sstevel@tonic-gate 6202*7c478bd9Sstevel@tonic-gate /* 6203*7c478bd9Sstevel@tonic-gate * The unconfig-driver to unconfig-all conversion above 6204*7c478bd9Sstevel@tonic-gate * constitutes an autodetach for NDI_DETACH_DRIVER calls, 6205*7c478bd9Sstevel@tonic-gate * set NDI_AUTODETACH. 6206*7c478bd9Sstevel@tonic-gate */ 6207*7c478bd9Sstevel@tonic-gate mcd->mtc_flags = hdl->mtc_flags; 6208*7c478bd9Sstevel@tonic-gate if ((mcd->mtc_flags & NDI_DETACH_DRIVER) && 6209*7c478bd9Sstevel@tonic-gate (hdl->mtc_op == MT_UNCONFIG_OP) && 6210*7c478bd9Sstevel@tonic-gate (major == ddi_driver_major(pdip))) 6211*7c478bd9Sstevel@tonic-gate mcd->mtc_flags |= NDI_AUTODETACH; 6212*7c478bd9Sstevel@tonic-gate 6213*7c478bd9Sstevel@tonic-gate mutex_enter(&hdl->mtc_lock); 6214*7c478bd9Sstevel@tonic-gate hdl->mtc_thr_count++; 6215*7c478bd9Sstevel@tonic-gate mutex_exit(&hdl->mtc_lock); 6216*7c478bd9Sstevel@tonic-gate 6217*7c478bd9Sstevel@tonic-gate /* 6218*7c478bd9Sstevel@tonic-gate * Add to end of list to process after ndi_devi_exit to avoid 6219*7c478bd9Sstevel@tonic-gate * locking differences depending on value of mtc_off. 6220*7c478bd9Sstevel@tonic-gate */ 6221*7c478bd9Sstevel@tonic-gate mcd->mtc_next = NULL; 6222*7c478bd9Sstevel@tonic-gate if (mcd_head == NULL) 6223*7c478bd9Sstevel@tonic-gate mcd_head = mcd; 6224*7c478bd9Sstevel@tonic-gate else 6225*7c478bd9Sstevel@tonic-gate mcd_tail->mtc_next = mcd; 6226*7c478bd9Sstevel@tonic-gate mcd_tail = mcd; 6227*7c478bd9Sstevel@tonic-gate 6228*7c478bd9Sstevel@tonic-gate dip = ddi_get_next_sibling(dip); 6229*7c478bd9Sstevel@tonic-gate } 6230*7c478bd9Sstevel@tonic-gate ndi_devi_exit(pdip, circ); 6231*7c478bd9Sstevel@tonic-gate 6232*7c478bd9Sstevel@tonic-gate /* go through the list of held children */ 6233*7c478bd9Sstevel@tonic-gate for (mcd = mcd_head; mcd; mcd = mcd_head) { 6234*7c478bd9Sstevel@tonic-gate mcd_head = mcd->mtc_next; 6235*7c478bd9Sstevel@tonic-gate if (mtc_off) 6236*7c478bd9Sstevel@tonic-gate mt_config_thread(mcd); 6237*7c478bd9Sstevel@tonic-gate else 6238*7c478bd9Sstevel@tonic-gate (void) thread_create(NULL, 0, mt_config_thread, mcd, 6239*7c478bd9Sstevel@tonic-gate 0, &p0, TS_RUN, minclsyspri); 6240*7c478bd9Sstevel@tonic-gate } 6241*7c478bd9Sstevel@tonic-gate } 6242*7c478bd9Sstevel@tonic-gate 6243*7c478bd9Sstevel@tonic-gate static void 6244*7c478bd9Sstevel@tonic-gate mt_config_driver(struct mt_config_handle *hdl) 6245*7c478bd9Sstevel@tonic-gate { 6246*7c478bd9Sstevel@tonic-gate major_t par_major = hdl->mtc_parmajor; 6247*7c478bd9Sstevel@tonic-gate major_t major = hdl->mtc_major; 6248*7c478bd9Sstevel@tonic-gate struct devnames *dnp = &devnamesp[par_major]; 6249*7c478bd9Sstevel@tonic-gate dev_info_t *dip; 6250*7c478bd9Sstevel@tonic-gate struct mt_config_data *mcd_head = NULL; 6251*7c478bd9Sstevel@tonic-gate struct mt_config_data *mcd_tail = NULL; 6252*7c478bd9Sstevel@tonic-gate struct mt_config_data *mcd; 6253*7c478bd9Sstevel@tonic-gate #ifdef DEBUG 6254*7c478bd9Sstevel@tonic-gate timestruc_t end_time; 6255*7c478bd9Sstevel@tonic-gate 6256*7c478bd9Sstevel@tonic-gate /* Update total_time in handle */ 6257*7c478bd9Sstevel@tonic-gate gethrestime(&end_time); 6258*7c478bd9Sstevel@tonic-gate hdl->total_time += time_diff_in_msec(hdl->start_time, end_time); 6259*7c478bd9Sstevel@tonic-gate #endif 6260*7c478bd9Sstevel@tonic-gate ASSERT(par_major != (major_t)-1); 6261*7c478bd9Sstevel@tonic-gate ASSERT(major != (major_t)-1); 6262*7c478bd9Sstevel@tonic-gate 6263*7c478bd9Sstevel@tonic-gate LOCK_DEV_OPS(&dnp->dn_lock); 6264*7c478bd9Sstevel@tonic-gate dip = devnamesp[par_major].dn_head; 6265*7c478bd9Sstevel@tonic-gate while (dip) { 6266*7c478bd9Sstevel@tonic-gate /* 6267*7c478bd9Sstevel@tonic-gate * Hold the child that we are processing so he does not get 6268*7c478bd9Sstevel@tonic-gate * removed. The corrisponding ndi_rele_devi() for children 6269*7c478bd9Sstevel@tonic-gate * that are not being skipped is done at the end of 6270*7c478bd9Sstevel@tonic-gate * mt_config_thread(). 6271*7c478bd9Sstevel@tonic-gate */ 6272*7c478bd9Sstevel@tonic-gate ndi_hold_devi(dip); 6273*7c478bd9Sstevel@tonic-gate 6274*7c478bd9Sstevel@tonic-gate /* skip leaf nodes and nodes not fully attached */ 6275*7c478bd9Sstevel@tonic-gate if ((i_ddi_node_state(dip) < DS_READY) || is_leaf_node(dip)) { 6276*7c478bd9Sstevel@tonic-gate ndi_rele_devi(dip); 6277*7c478bd9Sstevel@tonic-gate dip = ddi_get_next(dip); 6278*7c478bd9Sstevel@tonic-gate continue; 6279*7c478bd9Sstevel@tonic-gate } 6280*7c478bd9Sstevel@tonic-gate 6281*7c478bd9Sstevel@tonic-gate mcd = kmem_alloc(sizeof (*mcd), KM_SLEEP); 6282*7c478bd9Sstevel@tonic-gate mcd->mtc_dip = dip; 6283*7c478bd9Sstevel@tonic-gate mcd->mtc_hdl = hdl; 6284*7c478bd9Sstevel@tonic-gate mcd->mtc_major = major; 6285*7c478bd9Sstevel@tonic-gate mcd->mtc_flags = hdl->mtc_flags; 6286*7c478bd9Sstevel@tonic-gate 6287*7c478bd9Sstevel@tonic-gate mutex_enter(&hdl->mtc_lock); 6288*7c478bd9Sstevel@tonic-gate hdl->mtc_thr_count++; 6289*7c478bd9Sstevel@tonic-gate mutex_exit(&hdl->mtc_lock); 6290*7c478bd9Sstevel@tonic-gate 6291*7c478bd9Sstevel@tonic-gate /* 6292*7c478bd9Sstevel@tonic-gate * Add to end of list to process after UNLOCK_DEV_OPS to avoid 6293*7c478bd9Sstevel@tonic-gate * locking differences depending on value of mtc_off. 6294*7c478bd9Sstevel@tonic-gate */ 6295*7c478bd9Sstevel@tonic-gate mcd->mtc_next = NULL; 6296*7c478bd9Sstevel@tonic-gate if (mcd_head == NULL) 6297*7c478bd9Sstevel@tonic-gate mcd_head = mcd; 6298*7c478bd9Sstevel@tonic-gate else 6299*7c478bd9Sstevel@tonic-gate mcd_tail->mtc_next = mcd; 6300*7c478bd9Sstevel@tonic-gate mcd_tail = mcd; 6301*7c478bd9Sstevel@tonic-gate 6302*7c478bd9Sstevel@tonic-gate dip = ddi_get_next(dip); 6303*7c478bd9Sstevel@tonic-gate } 6304*7c478bd9Sstevel@tonic-gate UNLOCK_DEV_OPS(&dnp->dn_lock); 6305*7c478bd9Sstevel@tonic-gate 6306*7c478bd9Sstevel@tonic-gate /* go through the list of held children */ 6307*7c478bd9Sstevel@tonic-gate for (mcd = mcd_head; mcd; mcd = mcd_head) { 6308*7c478bd9Sstevel@tonic-gate mcd_head = mcd->mtc_next; 6309*7c478bd9Sstevel@tonic-gate if (mtc_off) 6310*7c478bd9Sstevel@tonic-gate mt_config_thread(mcd); 6311*7c478bd9Sstevel@tonic-gate else 6312*7c478bd9Sstevel@tonic-gate (void) thread_create(NULL, 0, mt_config_thread, mcd, 6313*7c478bd9Sstevel@tonic-gate 0, &p0, TS_RUN, minclsyspri); 6314*7c478bd9Sstevel@tonic-gate } 6315*7c478bd9Sstevel@tonic-gate } 6316*7c478bd9Sstevel@tonic-gate 6317*7c478bd9Sstevel@tonic-gate /* 6318*7c478bd9Sstevel@tonic-gate * Given the nodeid for a persistent (PROM or SID) node, return 6319*7c478bd9Sstevel@tonic-gate * the corresponding devinfo node 6320*7c478bd9Sstevel@tonic-gate * NOTE: This function will return NULL for .conf nodeids. 6321*7c478bd9Sstevel@tonic-gate */ 6322*7c478bd9Sstevel@tonic-gate dev_info_t * 6323*7c478bd9Sstevel@tonic-gate e_ddi_nodeid_to_dip(dnode_t nodeid) 6324*7c478bd9Sstevel@tonic-gate { 6325*7c478bd9Sstevel@tonic-gate dev_info_t *dip = NULL; 6326*7c478bd9Sstevel@tonic-gate struct devi_nodeid *prev, *elem; 6327*7c478bd9Sstevel@tonic-gate 6328*7c478bd9Sstevel@tonic-gate mutex_enter(&devimap->dno_lock); 6329*7c478bd9Sstevel@tonic-gate 6330*7c478bd9Sstevel@tonic-gate prev = NULL; 6331*7c478bd9Sstevel@tonic-gate for (elem = devimap->dno_head; elem; elem = elem->next) { 6332*7c478bd9Sstevel@tonic-gate if (elem->nodeid == nodeid) { 6333*7c478bd9Sstevel@tonic-gate ndi_hold_devi(elem->dip); 6334*7c478bd9Sstevel@tonic-gate dip = elem->dip; 6335*7c478bd9Sstevel@tonic-gate break; 6336*7c478bd9Sstevel@tonic-gate } 6337*7c478bd9Sstevel@tonic-gate prev = elem; 6338*7c478bd9Sstevel@tonic-gate } 6339*7c478bd9Sstevel@tonic-gate 6340*7c478bd9Sstevel@tonic-gate /* 6341*7c478bd9Sstevel@tonic-gate * Move to head for faster lookup next time 6342*7c478bd9Sstevel@tonic-gate */ 6343*7c478bd9Sstevel@tonic-gate if (elem && prev) { 6344*7c478bd9Sstevel@tonic-gate prev->next = elem->next; 6345*7c478bd9Sstevel@tonic-gate elem->next = devimap->dno_head; 6346*7c478bd9Sstevel@tonic-gate devimap->dno_head = elem; 6347*7c478bd9Sstevel@tonic-gate } 6348*7c478bd9Sstevel@tonic-gate 6349*7c478bd9Sstevel@tonic-gate mutex_exit(&devimap->dno_lock); 6350*7c478bd9Sstevel@tonic-gate return (dip); 6351*7c478bd9Sstevel@tonic-gate } 6352*7c478bd9Sstevel@tonic-gate 6353*7c478bd9Sstevel@tonic-gate static void 6354*7c478bd9Sstevel@tonic-gate free_cache_task(void *arg) 6355*7c478bd9Sstevel@tonic-gate { 6356*7c478bd9Sstevel@tonic-gate ASSERT(arg == NULL); 6357*7c478bd9Sstevel@tonic-gate 6358*7c478bd9Sstevel@tonic-gate mutex_enter(&di_cache.cache_lock); 6359*7c478bd9Sstevel@tonic-gate 6360*7c478bd9Sstevel@tonic-gate /* 6361*7c478bd9Sstevel@tonic-gate * The cache can be invalidated without holding the lock 6362*7c478bd9Sstevel@tonic-gate * but it can be made valid again only while the lock is held. 6363*7c478bd9Sstevel@tonic-gate * So if the cache is invalid when the lock is held, it will 6364*7c478bd9Sstevel@tonic-gate * stay invalid until lock is released. 6365*7c478bd9Sstevel@tonic-gate */ 6366*7c478bd9Sstevel@tonic-gate if (!di_cache.cache_valid) 6367*7c478bd9Sstevel@tonic-gate i_ddi_di_cache_free(&di_cache); 6368*7c478bd9Sstevel@tonic-gate 6369*7c478bd9Sstevel@tonic-gate mutex_exit(&di_cache.cache_lock); 6370*7c478bd9Sstevel@tonic-gate 6371*7c478bd9Sstevel@tonic-gate if (di_cache_debug) 6372*7c478bd9Sstevel@tonic-gate cmn_err(CE_NOTE, "system_taskq: di_cache freed"); 6373*7c478bd9Sstevel@tonic-gate } 6374*7c478bd9Sstevel@tonic-gate 6375*7c478bd9Sstevel@tonic-gate extern int modrootloaded; 6376*7c478bd9Sstevel@tonic-gate 6377*7c478bd9Sstevel@tonic-gate void 6378*7c478bd9Sstevel@tonic-gate i_ddi_di_cache_free(struct di_cache *cache) 6379*7c478bd9Sstevel@tonic-gate { 6380*7c478bd9Sstevel@tonic-gate int error; 6381*7c478bd9Sstevel@tonic-gate 6382*7c478bd9Sstevel@tonic-gate ASSERT(mutex_owned(&cache->cache_lock)); 6383*7c478bd9Sstevel@tonic-gate 6384*7c478bd9Sstevel@tonic-gate if (cache->cache_size) { 6385*7c478bd9Sstevel@tonic-gate ASSERT(cache->cache_size > 0); 6386*7c478bd9Sstevel@tonic-gate ASSERT(cache->cache_data); 6387*7c478bd9Sstevel@tonic-gate 6388*7c478bd9Sstevel@tonic-gate kmem_free(cache->cache_data, cache->cache_size); 6389*7c478bd9Sstevel@tonic-gate cache->cache_data = NULL; 6390*7c478bd9Sstevel@tonic-gate cache->cache_size = 0; 6391*7c478bd9Sstevel@tonic-gate 6392*7c478bd9Sstevel@tonic-gate if (di_cache_debug) 6393*7c478bd9Sstevel@tonic-gate cmn_err(CE_NOTE, "i_ddi_di_cache_free: freed cachemem"); 6394*7c478bd9Sstevel@tonic-gate } else { 6395*7c478bd9Sstevel@tonic-gate ASSERT(cache->cache_data == NULL); 6396*7c478bd9Sstevel@tonic-gate if (di_cache_debug) 6397*7c478bd9Sstevel@tonic-gate cmn_err(CE_NOTE, "i_ddi_di_cache_free: NULL cache"); 6398*7c478bd9Sstevel@tonic-gate } 6399*7c478bd9Sstevel@tonic-gate 6400*7c478bd9Sstevel@tonic-gate if (!modrootloaded || rootvp == NULL || vn_is_readonly(rootvp)) { 6401*7c478bd9Sstevel@tonic-gate if (di_cache_debug) { 6402*7c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "/ not mounted/RDONLY. Skip unlink"); 6403*7c478bd9Sstevel@tonic-gate } 6404*7c478bd9Sstevel@tonic-gate return; 6405*7c478bd9Sstevel@tonic-gate } 6406*7c478bd9Sstevel@tonic-gate 6407*7c478bd9Sstevel@tonic-gate error = vn_remove(DI_CACHE_FILE, UIO_SYSSPACE, RMFILE); 6408*7c478bd9Sstevel@tonic-gate if (di_cache_debug && error && error != ENOENT) { 6409*7c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "%s: unlink failed: %d", DI_CACHE_FILE, error); 6410*7c478bd9Sstevel@tonic-gate } else if (di_cache_debug && !error) { 6411*7c478bd9Sstevel@tonic-gate cmn_err(CE_NOTE, "i_ddi_di_cache_free: unlinked cache file"); 6412*7c478bd9Sstevel@tonic-gate } 6413*7c478bd9Sstevel@tonic-gate } 6414*7c478bd9Sstevel@tonic-gate 6415*7c478bd9Sstevel@tonic-gate void 6416*7c478bd9Sstevel@tonic-gate i_ddi_di_cache_invalidate(int kmflag) 6417*7c478bd9Sstevel@tonic-gate { 6418*7c478bd9Sstevel@tonic-gate uint_t flag; 6419*7c478bd9Sstevel@tonic-gate 6420*7c478bd9Sstevel@tonic-gate if (!modrootloaded || !i_ddi_io_initialized()) { 6421*7c478bd9Sstevel@tonic-gate if (di_cache_debug) 6422*7c478bd9Sstevel@tonic-gate cmn_err(CE_NOTE, "I/O not inited. Skipping invalidate"); 6423*7c478bd9Sstevel@tonic-gate return; 6424*7c478bd9Sstevel@tonic-gate } 6425*7c478bd9Sstevel@tonic-gate 6426*7c478bd9Sstevel@tonic-gate /* 6427*7c478bd9Sstevel@tonic-gate * Invalidate the in-core cache 6428*7c478bd9Sstevel@tonic-gate */ 6429*7c478bd9Sstevel@tonic-gate atomic_and_32(&di_cache.cache_valid, 0); 6430*7c478bd9Sstevel@tonic-gate 6431*7c478bd9Sstevel@tonic-gate flag = (kmflag == KM_SLEEP) ? TQ_SLEEP : TQ_NOSLEEP; 6432*7c478bd9Sstevel@tonic-gate 6433*7c478bd9Sstevel@tonic-gate (void) taskq_dispatch(system_taskq, free_cache_task, NULL, flag); 6434*7c478bd9Sstevel@tonic-gate 6435*7c478bd9Sstevel@tonic-gate if (di_cache_debug) { 6436*7c478bd9Sstevel@tonic-gate cmn_err(CE_NOTE, "invalidation with km_flag: %s", 6437*7c478bd9Sstevel@tonic-gate kmflag == KM_SLEEP ? "KM_SLEEP" : "KM_NOSLEEP"); 6438*7c478bd9Sstevel@tonic-gate } 6439*7c478bd9Sstevel@tonic-gate } 6440*7c478bd9Sstevel@tonic-gate 6441*7c478bd9Sstevel@tonic-gate 6442*7c478bd9Sstevel@tonic-gate static void 6443*7c478bd9Sstevel@tonic-gate i_bind_vhci_node(dev_info_t *dip) 6444*7c478bd9Sstevel@tonic-gate { 6445*7c478bd9Sstevel@tonic-gate char *node_name; 6446*7c478bd9Sstevel@tonic-gate 6447*7c478bd9Sstevel@tonic-gate node_name = i_ddi_strdup(ddi_node_name(dip), KM_SLEEP); 6448*7c478bd9Sstevel@tonic-gate i_ddi_set_binding_name(dip, node_name); 6449*7c478bd9Sstevel@tonic-gate DEVI(dip)->devi_major = ddi_name_to_major(node_name); 6450*7c478bd9Sstevel@tonic-gate i_ddi_set_node_state(dip, DS_BOUND); 6451*7c478bd9Sstevel@tonic-gate } 6452*7c478bd9Sstevel@tonic-gate 6453*7c478bd9Sstevel@tonic-gate 6454*7c478bd9Sstevel@tonic-gate static void 6455*7c478bd9Sstevel@tonic-gate i_free_vhci_bind_name(dev_info_t *dip) 6456*7c478bd9Sstevel@tonic-gate { 6457*7c478bd9Sstevel@tonic-gate if (DEVI(dip)->devi_binding_name) { 6458*7c478bd9Sstevel@tonic-gate kmem_free(DEVI(dip)->devi_binding_name, 6459*7c478bd9Sstevel@tonic-gate sizeof (ddi_node_name(dip))); 6460*7c478bd9Sstevel@tonic-gate } 6461*7c478bd9Sstevel@tonic-gate } 6462*7c478bd9Sstevel@tonic-gate 6463*7c478bd9Sstevel@tonic-gate 6464*7c478bd9Sstevel@tonic-gate static char vhci_node_addr[2]; 6465*7c478bd9Sstevel@tonic-gate 6466*7c478bd9Sstevel@tonic-gate static int 6467*7c478bd9Sstevel@tonic-gate i_init_vhci_node(dev_info_t *dip) 6468*7c478bd9Sstevel@tonic-gate { 6469*7c478bd9Sstevel@tonic-gate add_global_props(dip); 6470*7c478bd9Sstevel@tonic-gate DEVI(dip)->devi_ops = ndi_hold_driver(dip); 6471*7c478bd9Sstevel@tonic-gate if (DEVI(dip)->devi_ops == NULL) 6472*7c478bd9Sstevel@tonic-gate return (-1); 6473*7c478bd9Sstevel@tonic-gate 6474*7c478bd9Sstevel@tonic-gate DEVI(dip)->devi_instance = e_ddi_assign_instance(dip); 6475*7c478bd9Sstevel@tonic-gate e_ddi_keep_instance(dip); 6476*7c478bd9Sstevel@tonic-gate vhci_node_addr[0] = '\0'; 6477*7c478bd9Sstevel@tonic-gate ddi_set_name_addr(dip, vhci_node_addr); 6478*7c478bd9Sstevel@tonic-gate i_ddi_set_node_state(dip, DS_INITIALIZED); 6479*7c478bd9Sstevel@tonic-gate return (0); 6480*7c478bd9Sstevel@tonic-gate } 6481*7c478bd9Sstevel@tonic-gate 6482*7c478bd9Sstevel@tonic-gate static void 6483*7c478bd9Sstevel@tonic-gate i_link_vhci_node(dev_info_t *dip) 6484*7c478bd9Sstevel@tonic-gate { 6485*7c478bd9Sstevel@tonic-gate /* 6486*7c478bd9Sstevel@tonic-gate * scsi_vhci should be kept left most of the device tree. 6487*7c478bd9Sstevel@tonic-gate */ 6488*7c478bd9Sstevel@tonic-gate mutex_enter(&global_vhci_lock); 6489*7c478bd9Sstevel@tonic-gate if (scsi_vhci_dip) { 6490*7c478bd9Sstevel@tonic-gate DEVI(dip)->devi_sibling = DEVI(scsi_vhci_dip)->devi_sibling; 6491*7c478bd9Sstevel@tonic-gate DEVI(scsi_vhci_dip)->devi_sibling = DEVI(dip); 6492*7c478bd9Sstevel@tonic-gate } else { 6493*7c478bd9Sstevel@tonic-gate DEVI(dip)->devi_sibling = DEVI(top_devinfo)->devi_child; 6494*7c478bd9Sstevel@tonic-gate DEVI(top_devinfo)->devi_child = DEVI(dip); 6495*7c478bd9Sstevel@tonic-gate } 6496*7c478bd9Sstevel@tonic-gate mutex_exit(&global_vhci_lock); 6497*7c478bd9Sstevel@tonic-gate } 6498*7c478bd9Sstevel@tonic-gate 6499*7c478bd9Sstevel@tonic-gate 6500*7c478bd9Sstevel@tonic-gate /* 6501*7c478bd9Sstevel@tonic-gate * This a special routine to enumerate vhci node (child of rootnex 6502*7c478bd9Sstevel@tonic-gate * node) without holding the ndi_devi_enter() lock. The device node 6503*7c478bd9Sstevel@tonic-gate * is allocated, initialized and brought into DS_READY state before 6504*7c478bd9Sstevel@tonic-gate * inserting into the device tree. The VHCI node is handcrafted 6505*7c478bd9Sstevel@tonic-gate * here to bring the node to DS_READY, similar to rootnex node. 6506*7c478bd9Sstevel@tonic-gate * 6507*7c478bd9Sstevel@tonic-gate * The global_vhci_lock protects linking the node into the device 6508*7c478bd9Sstevel@tonic-gate * as same lock is held before linking/unlinking any direct child 6509*7c478bd9Sstevel@tonic-gate * of rootnex children. 6510*7c478bd9Sstevel@tonic-gate * 6511*7c478bd9Sstevel@tonic-gate * This routine is a workaround to handle a possible deadlock 6512*7c478bd9Sstevel@tonic-gate * that occurs while trying to enumerate node in a different sub-tree 6513*7c478bd9Sstevel@tonic-gate * during _init/_attach entry points. 6514*7c478bd9Sstevel@tonic-gate */ 6515*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 6516*7c478bd9Sstevel@tonic-gate dev_info_t * 6517*7c478bd9Sstevel@tonic-gate ndi_devi_config_vhci(char *drvname, int flags) 6518*7c478bd9Sstevel@tonic-gate { 6519*7c478bd9Sstevel@tonic-gate struct devnames *dnp; 6520*7c478bd9Sstevel@tonic-gate dev_info_t *dip; 6521*7c478bd9Sstevel@tonic-gate major_t major = ddi_name_to_major(drvname); 6522*7c478bd9Sstevel@tonic-gate 6523*7c478bd9Sstevel@tonic-gate if (major == -1) 6524*7c478bd9Sstevel@tonic-gate return (NULL); 6525*7c478bd9Sstevel@tonic-gate 6526*7c478bd9Sstevel@tonic-gate /* Make sure we create the VHCI node only once */ 6527*7c478bd9Sstevel@tonic-gate dnp = &devnamesp[major]; 6528*7c478bd9Sstevel@tonic-gate LOCK_DEV_OPS(&dnp->dn_lock); 6529*7c478bd9Sstevel@tonic-gate if (dnp->dn_head) { 6530*7c478bd9Sstevel@tonic-gate dip = dnp->dn_head; 6531*7c478bd9Sstevel@tonic-gate UNLOCK_DEV_OPS(&dnp->dn_lock); 6532*7c478bd9Sstevel@tonic-gate return (dip); 6533*7c478bd9Sstevel@tonic-gate } 6534*7c478bd9Sstevel@tonic-gate UNLOCK_DEV_OPS(&dnp->dn_lock); 6535*7c478bd9Sstevel@tonic-gate 6536*7c478bd9Sstevel@tonic-gate /* Allocate the VHCI node */ 6537*7c478bd9Sstevel@tonic-gate ndi_devi_alloc_sleep(top_devinfo, drvname, DEVI_SID_NODEID, &dip); 6538*7c478bd9Sstevel@tonic-gate ndi_hold_devi(dip); 6539*7c478bd9Sstevel@tonic-gate 6540*7c478bd9Sstevel@tonic-gate /* Mark the node as VHCI */ 6541*7c478bd9Sstevel@tonic-gate DEVI(dip)->devi_node_attributes |= DDI_VHCI_NODE; 6542*7c478bd9Sstevel@tonic-gate 6543*7c478bd9Sstevel@tonic-gate i_ddi_add_devimap(dip); 6544*7c478bd9Sstevel@tonic-gate i_bind_vhci_node(dip); 6545*7c478bd9Sstevel@tonic-gate if (i_init_vhci_node(dip) == -1) { 6546*7c478bd9Sstevel@tonic-gate i_free_vhci_bind_name(dip); 6547*7c478bd9Sstevel@tonic-gate ndi_rele_devi(dip); 6548*7c478bd9Sstevel@tonic-gate (void) ndi_devi_free(dip); 6549*7c478bd9Sstevel@tonic-gate return (NULL); 6550*7c478bd9Sstevel@tonic-gate } 6551*7c478bd9Sstevel@tonic-gate 6552*7c478bd9Sstevel@tonic-gate DEVI_SET_ATTACHING(dip); 6553*7c478bd9Sstevel@tonic-gate if (devi_attach(dip, DDI_ATTACH) != DDI_SUCCESS) { 6554*7c478bd9Sstevel@tonic-gate cmn_err(CE_CONT, "Could not attach %s driver", drvname); 6555*7c478bd9Sstevel@tonic-gate e_ddi_free_instance(dip, vhci_node_addr); 6556*7c478bd9Sstevel@tonic-gate i_free_vhci_bind_name(dip); 6557*7c478bd9Sstevel@tonic-gate ndi_rele_devi(dip); 6558*7c478bd9Sstevel@tonic-gate (void) ndi_devi_free(dip); 6559*7c478bd9Sstevel@tonic-gate return (NULL); 6560*7c478bd9Sstevel@tonic-gate } 6561*7c478bd9Sstevel@tonic-gate DEVI_CLR_ATTACHING(dip); 6562*7c478bd9Sstevel@tonic-gate 6563*7c478bd9Sstevel@tonic-gate i_link_vhci_node(dip); 6564*7c478bd9Sstevel@tonic-gate i_ddi_set_node_state(dip, DS_READY); 6565*7c478bd9Sstevel@tonic-gate 6566*7c478bd9Sstevel@tonic-gate LOCK_DEV_OPS(&dnp->dn_lock); 6567*7c478bd9Sstevel@tonic-gate dnp->dn_flags |= DN_DRIVER_HELD; 6568*7c478bd9Sstevel@tonic-gate dnp->dn_head = dip; 6569*7c478bd9Sstevel@tonic-gate UNLOCK_DEV_OPS(&dnp->dn_lock); 6570*7c478bd9Sstevel@tonic-gate 6571*7c478bd9Sstevel@tonic-gate i_ndi_devi_report_status_change(dip, NULL); 6572*7c478bd9Sstevel@tonic-gate 6573*7c478bd9Sstevel@tonic-gate return (dip); 6574*7c478bd9Sstevel@tonic-gate } 6575*7c478bd9Sstevel@tonic-gate 6576*7c478bd9Sstevel@tonic-gate /* 6577*7c478bd9Sstevel@tonic-gate * ibt_hw_is_present() returns 0 when there is no IB hardware actively 6578*7c478bd9Sstevel@tonic-gate * running. This is primarily useful for modules like rpcmod which 6579*7c478bd9Sstevel@tonic-gate * needs a quick check to decide whether or not it should try to use 6580*7c478bd9Sstevel@tonic-gate * InfiniBand 6581*7c478bd9Sstevel@tonic-gate */ 6582*7c478bd9Sstevel@tonic-gate int ib_hw_status = 0; 6583*7c478bd9Sstevel@tonic-gate int 6584*7c478bd9Sstevel@tonic-gate ibt_hw_is_present() 6585*7c478bd9Sstevel@tonic-gate { 6586*7c478bd9Sstevel@tonic-gate return (ib_hw_status); 6587*7c478bd9Sstevel@tonic-gate } 6588