1 /*
   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 
  22 /*
  23  * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
  24  * Copyright 2018 Nexenta Systems, Inc.
  25  * Copyright 2012 Garrett D'Amore <garrett@damore.org>.  All rights reserved.
  26  * Copyright (c) 2013, Joyent, Inc. All rights reserved.
  27  * Copyright (c) 2016 by Delphix. All rights reserved.
  28  */
  29 
  30 #include <sys/note.h>
  31 #include <sys/t_lock.h>
  32 #include <sys/cmn_err.h>
  33 #include <sys/instance.h>
  34 #include <sys/conf.h>
  35 #include <sys/stat.h>
  36 #include <sys/ddi.h>
  37 #include <sys/hwconf.h>
  38 #include <sys/sunddi.h>
  39 #include <sys/sunndi.h>
  40 #include <sys/ddi_impldefs.h>
  41 #include <sys/ndi_impldefs.h>
  42 #include <sys/modctl.h>
  43 #include <sys/contract/device_impl.h>
  44 #include <sys/dacf.h>
  45 #include <sys/promif.h>
  46 #include <sys/pci.h>
  47 #include <sys/cpuvar.h>
  48 #include <sys/pathname.h>
  49 #include <sys/taskq.h>
  50 #include <sys/sysevent.h>
  51 #include <sys/sunmdi.h>
  52 #include <sys/stream.h>
  53 #include <sys/strsubr.h>
  54 #include <sys/fs/snode.h>
  55 #include <sys/fs/dv_node.h>
  56 #include <sys/reboot.h>
  57 #include <sys/sysmacros.h>
  58 #include <sys/systm.h>
  59 #include <sys/fs/sdev_impl.h>
  60 #include <sys/sunldi.h>
  61 #include <sys/sunldi_impl.h>
  62 #include <sys/bootprops.h>
  63 #include <sys/varargs.h>
  64 #include <sys/modhash.h>
  65 #include <sys/instance.h>
  66 
  67 #if defined(__amd64) && !defined(__xpv)
  68 #include <sys/iommulib.h>
  69 #endif
  70 
  71 #ifdef DEBUG
  72 int ddidebug = DDI_AUDIT;
  73 #else
  74 int ddidebug = 0;
  75 #endif
  76 
  77 #define MT_CONFIG_OP    0
  78 #define MT_UNCONFIG_OP  1
  79 
  80 /* Multi-threaded configuration */
  81 struct mt_config_handle {
  82         kmutex_t mtc_lock;
  83         kcondvar_t mtc_cv;
  84         int mtc_thr_count;
  85         dev_info_t *mtc_pdip;   /* parent dip for mt_config_children */
  86         dev_info_t **mtc_fdip;  /* "a" dip where unconfigure failed */
  87         major_t mtc_parmajor;   /* parent major for mt_config_driver */
  88         major_t mtc_major;
  89         int mtc_flags;
  90         int mtc_op;             /* config or unconfig */
  91         int mtc_error;          /* operation error */
  92         struct brevq_node **mtc_brevqp; /* outstanding branch events queue */
  93 #ifdef DEBUG
  94         int total_time;
  95         timestruc_t start_time;
  96 #endif /* DEBUG */
  97 };
  98 
  99 struct devi_nodeid {
 100         pnode_t nodeid;
 101         dev_info_t *dip;
 102         struct devi_nodeid *next;
 103 };
 104 
 105 struct devi_nodeid_list {
 106         kmutex_t dno_lock;              /* Protects other fields */
 107         struct devi_nodeid *dno_head;   /* list of devi nodeid elements */
 108         struct devi_nodeid *dno_free;   /* Free list */
 109         uint_t dno_list_length;         /* number of dips in list */
 110 };
 111 
 112 /* used to keep track of branch remove events to be generated */
 113 struct brevq_node {
 114         char *brn_deviname;
 115         struct brevq_node *brn_sibling;
 116         struct brevq_node *brn_child;
 117 };
 118 
 119 static struct devi_nodeid_list devi_nodeid_list;
 120 static struct devi_nodeid_list *devimap = &devi_nodeid_list;
 121 
 122 /*
 123  * Well known nodes which are attached first at boot time.
 124  */
 125 dev_info_t *top_devinfo;                /* root of device tree */
 126 dev_info_t *options_dip;
 127 dev_info_t *pseudo_dip;
 128 dev_info_t *clone_dip;
 129 dev_info_t *scsi_vhci_dip;              /* MPXIO dip */
 130 major_t clone_major;
 131 
 132 /*
 133  * A non-global zone's /dev is derived from the device tree.
 134  * This generation number serves to indicate when a zone's
 135  * /dev may need to be updated.
 136  */
 137 volatile ulong_t devtree_gen;           /* generation number */
 138 
 139 /* block all future dev_info state changes */
 140 hrtime_t volatile devinfo_freeze = 0;
 141 
 142 /* number of dev_info attaches/detaches currently in progress */
 143 static ulong_t devinfo_attach_detach = 0;
 144 
 145 extern int      sys_shutdown;
 146 extern kmutex_t global_vhci_lock;
 147 
 148 /* bitset of DS_SYSAVAIL & DS_RECONFIG - no races, no lock */
 149 static int devname_state = 0;
 150 
 151 /*
 152  * The devinfo snapshot cache and related variables.
 153  * The only field in the di_cache structure that needs initialization
 154  * is the mutex (cache_lock). However, since this is an adaptive mutex
 155  * (MUTEX_DEFAULT) - it is automatically initialized by being allocated
 156  * in zeroed memory (static storage class). Therefore no explicit
 157  * initialization of the di_cache structure is needed.
 158  */
 159 struct di_cache di_cache = {1};
 160 int             di_cache_debug = 0;
 161 
 162 /* For ddvis, which needs pseudo children under PCI */
 163 int pci_allow_pseudo_children = 0;
 164 
 165 /* Allow path-oriented alias driver binding on driver.conf enumerated nodes */
 166 int driver_conf_allow_path_alias = 1;
 167 
 168 /*
 169  * The following switch is for service people, in case a
 170  * 3rd party driver depends on identify(9e) being called.
 171  */
 172 int identify_9e = 0;
 173 
 174 /*
 175  * Add flag so behaviour of preventing attach for retired persistant nodes
 176  * can be disabled.
 177  */
 178 int retire_prevents_attach = 1;
 179 
 180 int mtc_off;                                    /* turn off mt config */
 181 
 182 int quiesce_debug = 0;
 183 
 184 boolean_t ddi_aliases_present = B_FALSE;
 185 ddi_alias_t ddi_aliases;
 186 uint_t tsd_ddi_redirect;
 187 
 188 #define DDI_ALIAS_HASH_SIZE     (2700)
 189 
 190 static kmem_cache_t *ddi_node_cache;            /* devinfo node cache */
 191 static devinfo_log_header_t *devinfo_audit_log; /* devinfo log */
 192 static int devinfo_log_size;                    /* size in pages */
 193 
 194 boolean_t ddi_err_panic = B_FALSE;
 195 
 196 static int lookup_compatible(dev_info_t *, uint_t);
 197 static char *encode_composite_string(char **, uint_t, size_t *, uint_t);
 198 static void link_to_driver_list(dev_info_t *);
 199 static void unlink_from_driver_list(dev_info_t *);
 200 static void add_to_dn_list(struct devnames *, dev_info_t *);
 201 static void remove_from_dn_list(struct devnames *, dev_info_t *);
 202 static dev_info_t *find_duplicate_child();
 203 static void add_global_props(dev_info_t *);
 204 static void remove_global_props(dev_info_t *);
 205 static int uninit_node(dev_info_t *);
 206 static void da_log_init(void);
 207 static void da_log_enter(dev_info_t *);
 208 static int walk_devs(dev_info_t *, int (*f)(dev_info_t *, void *), void *, int);
 209 static int reset_nexus_flags(dev_info_t *, void *);
 210 static void ddi_optimize_dtree(dev_info_t *);
 211 static int is_leaf_node(dev_info_t *);
 212 static struct mt_config_handle *mt_config_init(dev_info_t *, dev_info_t **,
 213     int, major_t, int, struct brevq_node **);
 214 static void mt_config_children(struct mt_config_handle *);
 215 static void mt_config_driver(struct mt_config_handle *);
 216 static int mt_config_fini(struct mt_config_handle *);
 217 static int devi_unconfig_common(dev_info_t *, dev_info_t **, int, major_t,
 218     struct brevq_node **);
 219 static int
 220 ndi_devi_config_obp_args(dev_info_t *parent, char *devnm,
 221     dev_info_t **childp, int flags);
 222 static void i_link_vhci_node(dev_info_t *);
 223 static void ndi_devi_exit_and_wait(dev_info_t *dip,
 224     int circular, clock_t end_time);
 225 static int ndi_devi_unbind_driver(dev_info_t *dip);
 226 
 227 static int i_ddi_check_retire(dev_info_t *dip);
 228 
 229 static void quiesce_one_device(dev_info_t *, void *);
 230 
 231 dev_info_t *ddi_alias_redirect(char *alias);
 232 char *ddi_curr_redirect(char *currpath);
 233 
 234 
 235 /*
 236  * dev_info cache and node management
 237  */
 238 
 239 /* initialize dev_info node cache */
 240 void
 241 i_ddi_node_cache_init()
 242 {
 243         ASSERT(ddi_node_cache == NULL);
 244         ddi_node_cache = kmem_cache_create("dev_info_node_cache",
 245             sizeof (struct dev_info), 0, NULL, NULL, NULL, NULL, NULL, 0);
 246 
 247         if (ddidebug & DDI_AUDIT)
 248                 da_log_init();
 249 }
 250 
 251 
 252 /*
 253  * Allocating a dev_info node, callable from interrupt context with KM_NOSLEEP
 254  * The allocated node has a reference count of 0.
 255  */
 256 dev_info_t *
 257 i_ddi_alloc_node(dev_info_t *pdip, char *node_name, pnode_t nodeid,
 258     int instance, ddi_prop_t *sys_prop, int flag)
 259 {
 260         struct dev_info *devi;
 261         struct devi_nodeid *elem;
 262         static char failed[] = "i_ddi_alloc_node: out of memory";
 263 
 264         ASSERT(node_name != NULL);
 265 
 266         if ((devi = kmem_cache_alloc(ddi_node_cache, flag)) == NULL) {
 267                 cmn_err(CE_NOTE, failed);
 268                 return (NULL);
 269         }
 270 
 271         bzero(devi, sizeof (struct dev_info));
 272 
 273         if (devinfo_audit_log) {
 274                 devi->devi_audit = kmem_zalloc(sizeof (devinfo_audit_t), flag);
 275                 if (devi->devi_audit == NULL)
 276                         goto fail;
 277         }
 278 
 279         if ((devi->devi_node_name = i_ddi_strdup(node_name, flag)) == NULL)
 280                 goto fail;
 281 
 282         /* default binding name is node name */
 283         devi->devi_binding_name = devi->devi_node_name;
 284         devi->devi_major = DDI_MAJOR_T_NONE; /* unbound by default */
 285 
 286         /*
 287          * Make a copy of system property
 288          */
 289         if (sys_prop &&
 290             (devi->devi_sys_prop_ptr = i_ddi_prop_list_dup(sys_prop, flag))
 291             == NULL)
 292                 goto fail;
 293 
 294         /*
 295          * Assign devi_nodeid, devi_node_class, devi_node_attributes
 296          * according to the following algorithm:
 297          *
 298          * nodeid arg                   node class              node attributes
 299          *
 300          * DEVI_PSEUDO_NODEID           DDI_NC_PSEUDO           A
 301          * DEVI_SID_NODEID              DDI_NC_PSEUDO           A,P
 302          * DEVI_SID_HIDDEN_NODEID       DDI_NC_PSEUDO           A,P,H
 303          * DEVI_SID_HP_NODEID           DDI_NC_PSEUDO           A,P,h
 304          * DEVI_SID_HP_HIDDEN_NODEID    DDI_NC_PSEUDO           A,P,H,h
 305          * other                        DDI_NC_PROM             P
 306          *
 307          * Where A = DDI_AUTO_ASSIGNED_NODEID (auto-assign a nodeid)
 308          * and   P = DDI_PERSISTENT
 309          * and   H = DDI_HIDDEN_NODE
 310          * and   h = DDI_HOTPLUG_NODE
 311          *
 312          * auto-assigned nodeids are also auto-freed.
 313          */
 314         devi->devi_node_attributes = 0;
 315         switch (nodeid) {
 316         case DEVI_SID_HIDDEN_NODEID:
 317                 devi->devi_node_attributes |= DDI_HIDDEN_NODE;
 318                 goto sid;
 319 
 320         case DEVI_SID_HP_NODEID:
 321                 devi->devi_node_attributes |= DDI_HOTPLUG_NODE;
 322                 goto sid;
 323 
 324         case DEVI_SID_HP_HIDDEN_NODEID:
 325                 devi->devi_node_attributes |= DDI_HIDDEN_NODE;
 326                 devi->devi_node_attributes |= DDI_HOTPLUG_NODE;
 327                 goto sid;
 328 
 329         case DEVI_SID_NODEID:
 330 sid:            devi->devi_node_attributes |= DDI_PERSISTENT;
 331                 if ((elem = kmem_zalloc(sizeof (*elem), flag)) == NULL)
 332                         goto fail;
 333                 /*FALLTHROUGH*/
 334 
 335         case DEVI_PSEUDO_NODEID:
 336                 devi->devi_node_attributes |= DDI_AUTO_ASSIGNED_NODEID;
 337                 devi->devi_node_class = DDI_NC_PSEUDO;
 338                 if (impl_ddi_alloc_nodeid(&devi->devi_nodeid)) {
 339                         panic("i_ddi_alloc_node: out of nodeids");
 340                         /*NOTREACHED*/
 341                 }
 342                 break;
 343 
 344         default:
 345                 if ((elem = kmem_zalloc(sizeof (*elem), flag)) == NULL)
 346                         goto fail;
 347 
 348                 /*
 349                  * the nodetype is 'prom', try to 'take' the nodeid now.
 350                  * This requires memory allocation, so check for failure.
 351                  */
 352                 if (impl_ddi_take_nodeid(nodeid, flag) != 0) {
 353                         kmem_free(elem, sizeof (*elem));
 354                         goto fail;
 355                 }
 356 
 357                 devi->devi_nodeid = nodeid;
 358                 devi->devi_node_class = DDI_NC_PROM;
 359                 devi->devi_node_attributes = DDI_PERSISTENT;
 360                 break;
 361         }
 362 
 363         if (ndi_dev_is_persistent_node((dev_info_t *)devi)) {
 364                 mutex_enter(&devimap->dno_lock);
 365                 elem->next = devimap->dno_free;
 366                 devimap->dno_free = elem;
 367                 mutex_exit(&devimap->dno_lock);
 368         }
 369 
 370         /*
 371          * Instance is normally initialized to -1. In a few special
 372          * cases, the caller may specify an instance (e.g. CPU nodes).
 373          */
 374         devi->devi_instance = instance;
 375 
 376         /*
 377          * set parent and bus_ctl parent
 378          */
 379         devi->devi_parent = DEVI(pdip);
 380         devi->devi_bus_ctl = DEVI(pdip);
 381 
 382         NDI_CONFIG_DEBUG((CE_CONT,
 383             "i_ddi_alloc_node: name=%s id=%d\n", node_name, devi->devi_nodeid));
 384 
 385         cv_init(&(devi->devi_cv), NULL, CV_DEFAULT, NULL);
 386         mutex_init(&(devi->devi_lock), NULL, MUTEX_DEFAULT, NULL);
 387         mutex_init(&(devi->devi_pm_lock), NULL, MUTEX_DEFAULT, NULL);
 388         mutex_init(&(devi->devi_pm_busy_lock), NULL, MUTEX_DEFAULT, NULL);
 389 
 390         RIO_TRACE((CE_NOTE, "i_ddi_alloc_node: Initing contract fields: "
 391             "dip=%p, name=%s", (void *)devi, node_name));
 392 
 393         mutex_init(&(devi->devi_ct_lock), NULL, MUTEX_DEFAULT, NULL);
 394         cv_init(&(devi->devi_ct_cv), NULL, CV_DEFAULT, NULL);
 395         devi->devi_ct_count = -1;    /* counter not in use if -1 */
 396         list_create(&(devi->devi_ct), sizeof (cont_device_t),
 397             offsetof(cont_device_t, cond_next));
 398 
 399         i_ddi_set_node_state((dev_info_t *)devi, DS_PROTO);
 400         da_log_enter((dev_info_t *)devi);
 401         return ((dev_info_t *)devi);
 402 
 403 fail:
 404         if (devi->devi_sys_prop_ptr)
 405                 i_ddi_prop_list_delete(devi->devi_sys_prop_ptr);
 406         if (devi->devi_node_name)
 407                 kmem_free(devi->devi_node_name, strlen(node_name) + 1);
 408         if (devi->devi_audit)
 409                 kmem_free(devi->devi_audit, sizeof (devinfo_audit_t));
 410         kmem_cache_free(ddi_node_cache, devi);
 411         cmn_err(CE_NOTE, failed);
 412         return (NULL);
 413 }
 414 
 415 /*
 416  * free a dev_info structure.
 417  * NB. Not callable from interrupt since impl_ddi_free_nodeid may block.
 418  */
 419 void
 420 i_ddi_free_node(dev_info_t *dip)
 421 {
 422         struct dev_info *devi = DEVI(dip);
 423         struct devi_nodeid *elem;
 424 
 425         ASSERT(devi->devi_ref == 0);
 426         ASSERT(devi->devi_addr == NULL);
 427         ASSERT(devi->devi_node_state == DS_PROTO);
 428         ASSERT(devi->devi_child == NULL);
 429         ASSERT(devi->devi_hp_hdlp == NULL);
 430 
 431         /* free devi_addr_buf allocated by ddi_set_name_addr() */
 432         if (devi->devi_addr_buf)
 433                 kmem_free(devi->devi_addr_buf, 2 * MAXNAMELEN);
 434 
 435         if (i_ndi_dev_is_auto_assigned_node(dip))
 436                 impl_ddi_free_nodeid(DEVI(dip)->devi_nodeid);
 437 
 438         if (ndi_dev_is_persistent_node(dip)) {
 439                 mutex_enter(&devimap->dno_lock);
 440                 ASSERT(devimap->dno_free);
 441                 elem = devimap->dno_free;
 442                 devimap->dno_free = elem->next;
 443                 mutex_exit(&devimap->dno_lock);
 444                 kmem_free(elem, sizeof (*elem));
 445         }
 446 
 447         if (DEVI(dip)->devi_compat_names)
 448                 kmem_free(DEVI(dip)->devi_compat_names,
 449                     DEVI(dip)->devi_compat_length);
 450         if (DEVI(dip)->devi_rebinding_name)
 451                 kmem_free(DEVI(dip)->devi_rebinding_name,
 452                     strlen(DEVI(dip)->devi_rebinding_name) + 1);
 453 
 454         ddi_prop_remove_all(dip);       /* remove driver properties */
 455         if (devi->devi_sys_prop_ptr)
 456                 i_ddi_prop_list_delete(devi->devi_sys_prop_ptr);
 457         if (devi->devi_hw_prop_ptr)
 458                 i_ddi_prop_list_delete(devi->devi_hw_prop_ptr);
 459 
 460         if (DEVI(dip)->devi_devid_str)
 461                 ddi_devid_str_free(DEVI(dip)->devi_devid_str);
 462 
 463         i_ddi_set_node_state(dip, DS_INVAL);
 464         da_log_enter(dip);
 465         if (devi->devi_audit) {
 466                 kmem_free(devi->devi_audit, sizeof (devinfo_audit_t));
 467         }
 468         if (devi->devi_device_class)
 469                 kmem_free(devi->devi_device_class,
 470                     strlen(devi->devi_device_class) + 1);
 471         cv_destroy(&(devi->devi_cv));
 472         mutex_destroy(&(devi->devi_lock));
 473         mutex_destroy(&(devi->devi_pm_lock));
 474         mutex_destroy(&(devi->devi_pm_busy_lock));
 475 
 476         RIO_TRACE((CE_NOTE, "i_ddi_free_node: destroying contract fields: "
 477             "dip=%p", (void *)dip));
 478         contract_device_remove_dip(dip);
 479         ASSERT(devi->devi_ct_count == -1);
 480         ASSERT(list_is_empty(&(devi->devi_ct)));
 481         cv_destroy(&(devi->devi_ct_cv));
 482         list_destroy(&(devi->devi_ct));
 483         /* free this last since contract_device_remove_dip() uses it */
 484         mutex_destroy(&(devi->devi_ct_lock));
 485         RIO_TRACE((CE_NOTE, "i_ddi_free_node: destroyed all contract fields: "
 486             "dip=%p, name=%s", (void *)dip, devi->devi_node_name));
 487 
 488         kmem_free(devi->devi_node_name, strlen(devi->devi_node_name) + 1);
 489 
 490         /* free event data */
 491         if (devi->devi_ev_path)
 492                 kmem_free(devi->devi_ev_path, MAXPATHLEN);
 493 
 494         kmem_cache_free(ddi_node_cache, devi);
 495 }
 496 
 497 
 498 /*
 499  * Node state transitions
 500  */
 501 
 502 /*
 503  * Change the node name
 504  */
 505 int
 506 ndi_devi_set_nodename(dev_info_t *dip, char *name, int flags)
 507 {
 508         _NOTE(ARGUNUSED(flags))
 509         char *nname, *oname;
 510 
 511         ASSERT(dip && name);
 512 
 513         oname = DEVI(dip)->devi_node_name;
 514         if (strcmp(oname, name) == 0)
 515                 return (DDI_SUCCESS);
 516 
 517         /*
 518          * pcicfg_fix_ethernet requires a name change after node
 519          * is linked into the tree. When pcicfg is fixed, we
 520          * should only allow name change in DS_PROTO state.
 521          */
 522         if (i_ddi_node_state(dip) >= DS_BOUND) {
 523                 /*
 524                  * Don't allow name change once node is bound
 525                  */
 526                 cmn_err(CE_NOTE,
 527                     "ndi_devi_set_nodename: node already bound dip = %p,"
 528                     " %s -> %s", (void *)dip, ddi_node_name(dip), name);
 529                 return (NDI_FAILURE);
 530         }
 531 
 532         nname = i_ddi_strdup(name, KM_SLEEP);
 533         DEVI(dip)->devi_node_name = nname;
 534         i_ddi_set_binding_name(dip, nname);
 535         kmem_free(oname, strlen(oname) + 1);
 536 
 537         da_log_enter(dip);
 538         return (NDI_SUCCESS);
 539 }
 540 
 541 void
 542 i_ddi_add_devimap(dev_info_t *dip)
 543 {
 544         struct devi_nodeid *elem;
 545 
 546         ASSERT(dip);
 547 
 548         if (!ndi_dev_is_persistent_node(dip))
 549                 return;
 550 
 551         ASSERT(ddi_get_parent(dip) == NULL || (DEVI_VHCI_NODE(dip)) ||
 552             DEVI_BUSY_OWNED(ddi_get_parent(dip)));
 553 
 554         mutex_enter(&devimap->dno_lock);
 555 
 556         ASSERT(devimap->dno_free);
 557 
 558         elem = devimap->dno_free;
 559         devimap->dno_free = elem->next;
 560 
 561         elem->nodeid = ddi_get_nodeid(dip);
 562         elem->dip = dip;
 563         elem->next = devimap->dno_head;
 564         devimap->dno_head = elem;
 565 
 566         devimap->dno_list_length++;
 567 
 568         mutex_exit(&devimap->dno_lock);
 569 }
 570 
 571 static int
 572 i_ddi_remove_devimap(dev_info_t *dip)
 573 {
 574         struct devi_nodeid *prev, *elem;
 575         static const char *fcn = "i_ddi_remove_devimap";
 576 
 577         ASSERT(dip);
 578 
 579         if (!ndi_dev_is_persistent_node(dip))
 580                 return (DDI_SUCCESS);
 581 
 582         mutex_enter(&devimap->dno_lock);
 583 
 584         /*
 585          * The following check is done with dno_lock held
 586          * to prevent race between dip removal and
 587          * e_ddi_prom_node_to_dip()
 588          */
 589         if (e_ddi_devi_holdcnt(dip)) {
 590                 mutex_exit(&devimap->dno_lock);
 591                 return (DDI_FAILURE);
 592         }
 593 
 594         ASSERT(devimap->dno_head);
 595         ASSERT(devimap->dno_list_length > 0);
 596 
 597         prev = NULL;
 598         for (elem = devimap->dno_head; elem; elem = elem->next) {
 599                 if (elem->dip == dip) {
 600                         ASSERT(elem->nodeid == ddi_get_nodeid(dip));
 601                         break;
 602                 }
 603                 prev = elem;
 604         }
 605 
 606         if (elem && prev)
 607                 prev->next = elem->next;
 608         else if (elem)
 609                 devimap->dno_head = elem->next;
 610         else
 611                 panic("%s: devinfo node(%p) not found",
 612                     fcn, (void *)dip);
 613 
 614         devimap->dno_list_length--;
 615 
 616         elem->nodeid = 0;
 617         elem->dip = NULL;
 618 
 619         elem->next = devimap->dno_free;
 620         devimap->dno_free = elem;
 621 
 622         mutex_exit(&devimap->dno_lock);
 623 
 624         return (DDI_SUCCESS);
 625 }
 626 
 627 /*
 628  * Link this node into the devinfo tree and add to orphan list
 629  * Not callable from interrupt context
 630  */
 631 static void
 632 link_node(dev_info_t *dip)
 633 {
 634         struct dev_info *devi = DEVI(dip);
 635         struct dev_info *parent = devi->devi_parent;
 636         dev_info_t **dipp;
 637 
 638         ASSERT(parent); /* never called for root node */
 639 
 640         NDI_CONFIG_DEBUG((CE_CONT, "link_node: parent = %s child = %s\n",
 641             parent->devi_node_name, devi->devi_node_name));
 642 
 643         /*
 644          * Hold the global_vhci_lock before linking any direct
 645          * children of rootnex driver. This special lock protects
 646          * linking and unlinking for rootnext direct children.
 647          */
 648         if ((dev_info_t *)parent == ddi_root_node())
 649                 mutex_enter(&global_vhci_lock);
 650 
 651         /*
 652          * attach the node to end of the list unless the node is already there
 653          */
 654         dipp = (dev_info_t **)(&DEVI(parent)->devi_child);
 655         while (*dipp && (*dipp != dip)) {
 656                 dipp = (dev_info_t **)(&DEVI(*dipp)->devi_sibling);
 657         }
 658         ASSERT(*dipp == NULL);  /* node is not linked */
 659 
 660         /*
 661          * Now that we are in the tree, update the devi-nodeid map.
 662          */
 663         i_ddi_add_devimap(dip);
 664 
 665         /*
 666          * This is a temporary workaround for Bug 4618861.
 667          * We keep the scsi_vhci nexus node on the left side of the devinfo
 668          * tree (under the root nexus driver), so that virtual nodes under
 669          * scsi_vhci will be SUSPENDed first and RESUMEd last.  This ensures
 670          * that the pHCI nodes are active during times when their clients
 671          * may be depending on them.  This workaround embodies the knowledge
 672          * that system PM and CPR both traverse the tree left-to-right during
 673          * SUSPEND and right-to-left during RESUME.
 674          * Extending the workaround to IB Nexus/VHCI
 675          * driver also.
 676          */
 677         if (strcmp(devi->devi_binding_name, "scsi_vhci") == 0) {
 678                 /* Add scsi_vhci to beginning of list */
 679                 ASSERT((dev_info_t *)parent == top_devinfo);
 680                 /* scsi_vhci under rootnex */
 681                 devi->devi_sibling = parent->devi_child;
 682                 parent->devi_child = devi;
 683         } else if (strcmp(devi->devi_binding_name, "ib") == 0) {
 684                 i_link_vhci_node(dip);
 685         } else {
 686                 /* Add to end of list */
 687                 *dipp = dip;
 688                 DEVI(dip)->devi_sibling = NULL;
 689         }
 690 
 691         /*
 692          * Release the global_vhci_lock before linking any direct
 693          * children of rootnex driver.
 694          */
 695         if ((dev_info_t *)parent == ddi_root_node())
 696                 mutex_exit(&global_vhci_lock);
 697 
 698         /* persistent nodes go on orphan list */
 699         if (ndi_dev_is_persistent_node(dip))
 700                 add_to_dn_list(&orphanlist, dip);
 701 }
 702 
 703 /*
 704  * Unlink this node from the devinfo tree
 705  */
 706 static int
 707 unlink_node(dev_info_t *dip)
 708 {
 709         struct dev_info *devi = DEVI(dip);
 710         struct dev_info *parent = devi->devi_parent;
 711         dev_info_t **dipp;
 712         ddi_hp_cn_handle_t *hdlp;
 713 
 714         ASSERT(parent != NULL);
 715         ASSERT(devi->devi_node_state == DS_LINKED);
 716 
 717         NDI_CONFIG_DEBUG((CE_CONT, "unlink_node: name = %s\n",
 718             ddi_node_name(dip)));
 719 
 720         /* check references */
 721         if (devi->devi_ref || i_ddi_remove_devimap(dip) != DDI_SUCCESS)
 722                 return (DDI_FAILURE);
 723 
 724         /*
 725          * Hold the global_vhci_lock before linking any direct
 726          * children of rootnex driver.
 727          */
 728         if ((dev_info_t *)parent == ddi_root_node())
 729                 mutex_enter(&global_vhci_lock);
 730 
 731         dipp = (dev_info_t **)(&DEVI(parent)->devi_child);
 732         while (*dipp && (*dipp != dip)) {
 733                 dipp = (dev_info_t **)(&DEVI(*dipp)->devi_sibling);
 734         }
 735         if (*dipp) {
 736                 *dipp = (dev_info_t *)(devi->devi_sibling);
 737                 devi->devi_sibling = NULL;
 738         } else {
 739                 NDI_CONFIG_DEBUG((CE_NOTE, "unlink_node: %s not linked",
 740                     devi->devi_node_name));
 741         }
 742 
 743         /*
 744          * Release the global_vhci_lock before linking any direct
 745          * children of rootnex driver.
 746          */
 747         if ((dev_info_t *)parent == ddi_root_node())
 748                 mutex_exit(&global_vhci_lock);
 749 
 750         /* Remove node from orphan list */
 751         if (ndi_dev_is_persistent_node(dip)) {
 752                 remove_from_dn_list(&orphanlist, dip);
 753         }
 754 
 755         /* Update parent's hotplug handle list */
 756         for (hdlp = DEVI(parent)->devi_hp_hdlp; hdlp; hdlp = hdlp->next) {
 757                 if (hdlp->cn_info.cn_child == dip)
 758                         hdlp->cn_info.cn_child = NULL;
 759         }
 760         return (DDI_SUCCESS);
 761 }
 762 
 763 /*
 764  * Bind this devinfo node to a driver. If compat is NON-NULL, try that first.
 765  * Else, use the node-name.
 766  *
 767  * NOTE: IEEE1275 specifies that nodename should be tried before compatible.
 768  *      Solaris implementation binds nodename after compatible.
 769  *
 770  * If we find a binding,
 771  * - set the binding name to the string,
 772  * - set major number to driver major
 773  *
 774  * If we don't find a binding,
 775  * - return failure
 776  */
 777 static int
 778 bind_node(dev_info_t *dip)
 779 {
 780         char *p = NULL;
 781         major_t major = DDI_MAJOR_T_NONE;
 782         struct dev_info *devi = DEVI(dip);
 783         dev_info_t *parent = ddi_get_parent(dip);
 784 
 785         ASSERT(devi->devi_node_state == DS_LINKED);
 786 
 787         NDI_CONFIG_DEBUG((CE_CONT, "bind_node: 0x%p(name = %s)\n",
 788             (void *)dip, ddi_node_name(dip)));
 789 
 790         mutex_enter(&DEVI(dip)->devi_lock);
 791         if (DEVI(dip)->devi_flags & DEVI_NO_BIND) {
 792                 mutex_exit(&DEVI(dip)->devi_lock);
 793                 return (DDI_FAILURE);
 794         }
 795         mutex_exit(&DEVI(dip)->devi_lock);
 796 
 797         /* find the driver with most specific binding using compatible */
 798         major = ddi_compatible_driver_major(dip, &p);
 799         if (major == DDI_MAJOR_T_NONE)
 800                 return (DDI_FAILURE);
 801 
 802         devi->devi_major = major;
 803         if (p != NULL) {
 804                 i_ddi_set_binding_name(dip, p);
 805                 NDI_CONFIG_DEBUG((CE_CONT, "bind_node: %s bound to %s\n",
 806                     devi->devi_node_name, p));
 807         }
 808 
 809         /* Link node to per-driver list */
 810         link_to_driver_list(dip);
 811 
 812         /*
 813          * reset parent flag so that nexus will merge .conf props
 814          */
 815         if (ndi_dev_is_persistent_node(dip)) {
 816                 mutex_enter(&DEVI(parent)->devi_lock);
 817                 DEVI(parent)->devi_flags &=
 818                     ~(DEVI_ATTACHED_CHILDREN|DEVI_MADE_CHILDREN);
 819                 mutex_exit(&DEVI(parent)->devi_lock);
 820         }
 821         return (DDI_SUCCESS);
 822 }
 823 
 824 /*
 825  * Unbind this devinfo node
 826  * Called before the node is destroyed or driver is removed from system
 827  */
 828 static int
 829 unbind_node(dev_info_t *dip)
 830 {
 831         ASSERT(DEVI(dip)->devi_node_state == DS_BOUND);
 832         ASSERT(DEVI(dip)->devi_major != DDI_MAJOR_T_NONE);
 833 
 834         /* check references */
 835         if (DEVI(dip)->devi_ref)
 836                 return (DDI_FAILURE);
 837 
 838         NDI_CONFIG_DEBUG((CE_CONT, "unbind_node: 0x%p(name = %s)\n",
 839             (void *)dip, ddi_node_name(dip)));
 840 
 841         unlink_from_driver_list(dip);
 842 
 843         DEVI(dip)->devi_major = DDI_MAJOR_T_NONE;
 844         DEVI(dip)->devi_binding_name = DEVI(dip)->devi_node_name;
 845         return (DDI_SUCCESS);
 846 }
 847 
 848 /*
 849  * Initialize a node: calls the parent nexus' bus_ctl ops to do the operation.
 850  * Must hold parent and per-driver list while calling this function.
 851  * A successful init_node() returns with an active ndi_hold_devi() hold on
 852  * the parent.
 853  */
 854 static int
 855 init_node(dev_info_t *dip)
 856 {
 857         int error;
 858         dev_info_t *pdip = ddi_get_parent(dip);
 859         int (*f)(dev_info_t *, dev_info_t *, ddi_ctl_enum_t, void *, void *);
 860         char *path;
 861         major_t major;
 862         ddi_devid_t devid = NULL;
 863 
 864         ASSERT(i_ddi_node_state(dip) == DS_BOUND);
 865 
 866         /* should be DS_READY except for pcmcia ... */
 867         ASSERT(i_ddi_node_state(pdip) >= DS_PROBED);
 868 
 869         path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
 870         (void) ddi_pathname(dip, path);
 871         NDI_CONFIG_DEBUG((CE_CONT, "init_node: entry: path %s 0x%p\n",
 872             path, (void *)dip));
 873 
 874         /*
 875          * The parent must have a bus_ctl operation.
 876          */
 877         if ((DEVI(pdip)->devi_ops->devo_bus_ops == NULL) ||
 878             (f = DEVI(pdip)->devi_ops->devo_bus_ops->bus_ctl) == NULL) {
 879                 error = DDI_FAILURE;
 880                 goto out;
 881         }
 882 
 883         add_global_props(dip);
 884 
 885         /*
 886          * Invoke the parent's bus_ctl operation with the DDI_CTLOPS_INITCHILD
 887          * command to transform the child to canonical form 1. If there
 888          * is an error, ddi_remove_child should be called, to clean up.
 889          */
 890         error = (*f)(pdip, pdip, DDI_CTLOPS_INITCHILD, dip, NULL);
 891         if (error != DDI_SUCCESS) {
 892                 NDI_CONFIG_DEBUG((CE_CONT, "init_node: %s 0x%p failed\n",
 893                     path, (void *)dip));
 894                 remove_global_props(dip);
 895 
 896                 /*
 897                  * If a nexus INITCHILD implementation calls ddi_devid_regster()
 898                  * prior to setting devi_addr, the devid is not recorded in
 899                  * the devid cache (i.e. DEVI_CACHED_DEVID is not set).
 900                  * With mpxio, while the vhci client path may be missing
 901                  * from the cache, phci pathinfo paths may have already be
 902                  * added to the cache, against the client dip, by use of
 903                  * e_devid_cache_pathinfo().  Because of this, when INITCHILD
 904                  * of the client fails, we need to purge the client dip from
 905                  * the cache even if DEVI_CACHED_DEVID is not set - if only
 906                  * devi_devid_str is set.
 907                  */
 908                 mutex_enter(&DEVI(dip)->devi_lock);
 909                 if ((DEVI(dip)->devi_flags & DEVI_CACHED_DEVID) ||
 910                     DEVI(dip)->devi_devid_str) {
 911                         DEVI(dip)->devi_flags &= ~DEVI_CACHED_DEVID;
 912                         mutex_exit(&DEVI(dip)->devi_lock);
 913                         ddi_devid_unregister(dip);
 914                 } else
 915                         mutex_exit(&DEVI(dip)->devi_lock);
 916 
 917                 /* in case nexus driver didn't clear this field */
 918                 ddi_set_name_addr(dip, NULL);
 919                 error = DDI_FAILURE;
 920                 goto out;
 921         }
 922 
 923         ndi_hold_devi(pdip);                    /* initial hold of parent */
 924 
 925         /* recompute path after initchild for @addr information */
 926         (void) ddi_pathname(dip, path);
 927 
 928         /* Check for duplicate nodes */
 929         if (find_duplicate_child(pdip, dip) != NULL) {
 930                 /*
 931                  * uninit_node() the duplicate - a successful uninit_node()
 932                  * will release inital hold of parent using ndi_rele_devi().
 933                  */
 934                 if ((error = uninit_node(dip)) != DDI_SUCCESS) {
 935                         ndi_rele_devi(pdip);    /* release initial hold */
 936                         cmn_err(CE_WARN, "init_node: uninit of duplicate "
 937                             "node %s failed", path);
 938                 }
 939                 NDI_CONFIG_DEBUG((CE_CONT, "init_node: duplicate uninit "
 940                     "%s 0x%p%s\n", path, (void *)dip,
 941                     (error == DDI_SUCCESS) ? "" : " failed"));
 942                 error = DDI_FAILURE;
 943                 goto out;
 944         }
 945 
 946         /*
 947          * If a devid was registered for a DS_BOUND node then the devid_cache
 948          * may not have captured the path. Detect this situation and ensure that
 949          * the path enters the cache now that devi_addr is established.
 950          */
 951         if (!(DEVI(dip)->devi_flags & DEVI_CACHED_DEVID) &&
 952             (ddi_devid_get(dip, &devid) == DDI_SUCCESS)) {
 953                 if (e_devid_cache_register(dip, devid) == DDI_SUCCESS) {
 954                         mutex_enter(&DEVI(dip)->devi_lock);
 955                         DEVI(dip)->devi_flags |= DEVI_CACHED_DEVID;
 956                         mutex_exit(&DEVI(dip)->devi_lock);
 957                 }
 958 
 959                 ddi_devid_free(devid);
 960         }
 961 
 962         /*
 963          * Check to see if we have a path-oriented driver alias that overrides
 964          * the current driver binding. If so, we need to rebind. This check
 965          * needs to be delayed until after a successful DDI_CTLOPS_INITCHILD,
 966          * so the unit-address is established on the last component of the path.
 967          *
 968          * NOTE: Allowing a path-oriented alias to change the driver binding
 969          * of a driver.conf node results in non-intuitive property behavior.
 970          * We provide a tunable (driver_conf_allow_path_alias) to control
 971          * this behavior. See uninit_node() for more details.
 972          *
 973          * NOTE: If you are adding a path-oriented alias for the boot device,
 974          * and there is mismatch between OBP and the kernel in regard to
 975          * generic name use, like "disk" .vs. "ssd", then you will need
 976          * to add a path-oriented alias for both paths.
 977          */
 978         major = ddi_name_to_major(path);
 979         if (driver_active(major) && (major != DEVI(dip)->devi_major) &&
 980             (ndi_dev_is_persistent_node(dip) || driver_conf_allow_path_alias)) {
 981 
 982                 /* Mark node for rebind processing. */
 983                 mutex_enter(&DEVI(dip)->devi_lock);
 984                 DEVI(dip)->devi_flags |= DEVI_REBIND;
 985                 mutex_exit(&DEVI(dip)->devi_lock);
 986 
 987                 /*
 988                  * Add an extra hold on the parent to prevent it from ever
 989                  * having a zero devi_ref during the child rebind process.
 990                  * This is necessary to ensure that the parent will never
 991                  * detach(9E) during the rebind.
 992                  */
 993                 ndi_hold_devi(pdip);            /* extra hold of parent */
 994 
 995                 /*
 996                  * uninit_node() current binding - a successful uninit_node()
 997                  * will release extra hold of parent using ndi_rele_devi().
 998                  */
 999                 if ((error = uninit_node(dip)) != DDI_SUCCESS) {
1000                         ndi_rele_devi(pdip);    /* release extra hold */
1001                         ndi_rele_devi(pdip);    /* release initial hold */
1002                         cmn_err(CE_WARN, "init_node: uninit for rebind "
1003                             "of node %s failed", path);
1004                         goto out;
1005                 }
1006 
1007                 /* Unbind: demote the node back to DS_LINKED.  */
1008                 if ((error = ndi_devi_unbind_driver(dip)) != DDI_SUCCESS) {
1009                         ndi_rele_devi(pdip);    /* release initial hold */
1010                         cmn_err(CE_WARN, "init_node: unbind for rebind "
1011                             "of node %s failed", path);
1012                         goto out;
1013                 }
1014 
1015                 /* establish rebinding name */
1016                 if (DEVI(dip)->devi_rebinding_name == NULL)
1017                         DEVI(dip)->devi_rebinding_name =
1018                             i_ddi_strdup(path, KM_SLEEP);
1019 
1020                 /*
1021                  * Now that we are demoted and marked for rebind, repromote.
1022                  * We need to do this in steps, instead of just calling
1023                  * ddi_initchild, so that we can redo the merge operation
1024                  * after we are rebound to the path-bound driver.
1025                  *
1026                  * Start by rebinding node to the path-bound driver.
1027                  */
1028                 if ((error = ndi_devi_bind_driver(dip, 0)) != DDI_SUCCESS) {
1029                         ndi_rele_devi(pdip);    /* release initial hold */
1030                         cmn_err(CE_WARN, "init_node: rebind "
1031                             "of node %s failed", path);
1032                         goto out;
1033                 }
1034 
1035                 /*
1036                  * If the node is not a driver.conf node then merge
1037                  * driver.conf properties from new path-bound driver.conf.
1038                  */
1039                 if (ndi_dev_is_persistent_node(dip))
1040                         (void) i_ndi_make_spec_children(pdip, 0);
1041 
1042                 /*
1043                  * Now that we have taken care of merge, repromote back
1044                  * to DS_INITIALIZED.
1045                  */
1046                 error = ddi_initchild(pdip, dip);
1047                 NDI_CONFIG_DEBUG((CE_CONT, "init_node: rebind "
1048                     "%s 0x%p\n", path, (void *)dip));
1049 
1050                 /*
1051                  * Release our initial hold. If ddi_initchild() was
1052                  * successful then it will return with the active hold.
1053                  */
1054                 ndi_rele_devi(pdip);
1055                 goto out;
1056         }
1057 
1058         /*
1059          * Apply multi-parent/deep-nexus optimization to the new node
1060          */
1061         DEVI(dip)->devi_instance = e_ddi_assign_instance(dip);
1062         ddi_optimize_dtree(dip);
1063         error = DDI_SUCCESS;            /* return with active hold */
1064 
1065 out:    if (error != DDI_SUCCESS) {
1066                 /* On failure ensure that DEVI_REBIND is cleared */
1067                 mutex_enter(&DEVI(dip)->devi_lock);
1068                 DEVI(dip)->devi_flags &= ~DEVI_REBIND;
1069                 mutex_exit(&DEVI(dip)->devi_lock);
1070         }
1071         kmem_free(path, MAXPATHLEN);
1072         return (error);
1073 }
1074 
1075 /*
1076  * Uninitialize node
1077  * The per-driver list must be held busy during the call.
1078  * A successful uninit_node() releases the init_node() hold on
1079  * the parent by calling ndi_rele_devi().
1080  */
1081 static int
1082 uninit_node(dev_info_t *dip)
1083 {
1084         int node_state_entry;
1085         dev_info_t *pdip;
1086         struct dev_ops *ops;
1087         int (*f)();
1088         int error;
1089         char *addr;
1090 
1091         /*
1092          * Don't check for references here or else a ref-counted
1093          * dip cannot be downgraded by the framework.
1094          */
1095         node_state_entry = i_ddi_node_state(dip);
1096         ASSERT((node_state_entry == DS_BOUND) ||
1097             (node_state_entry == DS_INITIALIZED));
1098         pdip = ddi_get_parent(dip);
1099         ASSERT(pdip);
1100 
1101         NDI_CONFIG_DEBUG((CE_CONT, "uninit_node: 0x%p(%s%d)\n",
1102             (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
1103 
1104         if (((ops = ddi_get_driver(pdip)) == NULL) ||
1105             (ops->devo_bus_ops == NULL) ||
1106             ((f = ops->devo_bus_ops->bus_ctl) == NULL)) {
1107                 return (DDI_FAILURE);
1108         }
1109 
1110         /*
1111          * save the @addr prior to DDI_CTLOPS_UNINITCHILD for use in
1112          * freeing the instance if it succeeds.
1113          */
1114         if (node_state_entry == DS_INITIALIZED) {
1115                 addr = ddi_get_name_addr(dip);
1116                 if (addr)
1117                         addr = i_ddi_strdup(addr, KM_SLEEP);
1118         } else {
1119                 addr = NULL;
1120         }
1121 
1122         error = (*f)(pdip, pdip, DDI_CTLOPS_UNINITCHILD, dip, (void *)NULL);
1123         if (error == DDI_SUCCESS) {
1124                 /* ensure that devids are unregistered */
1125                 mutex_enter(&DEVI(dip)->devi_lock);
1126                 if ((DEVI(dip)->devi_flags & DEVI_CACHED_DEVID)) {
1127                         DEVI(dip)->devi_flags &= ~DEVI_CACHED_DEVID;
1128                         mutex_exit(&DEVI(dip)->devi_lock);
1129                         ddi_devid_unregister(dip);
1130                 } else
1131                         mutex_exit(&DEVI(dip)->devi_lock);
1132 
1133                 /* if uninitchild forgot to set devi_addr to NULL do it now */
1134                 ddi_set_name_addr(dip, NULL);
1135 
1136                 /*
1137                  * Free instance number. This is a no-op if instance has
1138                  * been kept by probe_node().  Avoid free when we are called
1139                  * from init_node (DS_BOUND) because the instance has not yet
1140                  * been assigned.
1141                  */
1142                 if (node_state_entry == DS_INITIALIZED) {
1143                         e_ddi_free_instance(dip, addr);
1144                         DEVI(dip)->devi_instance = -1;
1145                 }
1146 
1147                 /* release the init_node hold */
1148                 ndi_rele_devi(pdip);
1149 
1150                 remove_global_props(dip);
1151 
1152                 /*
1153                  * NOTE: The decision on whether to allow a path-oriented
1154                  * rebind of a driver.conf enumerated node is made by
1155                  * init_node() based on driver_conf_allow_path_alias. The
1156                  * rebind code below prevents deletion of system properties
1157                  * on driver.conf nodes.
1158                  *
1159                  * When driver_conf_allow_path_alias is set, property behavior
1160                  * on rebound driver.conf file is non-intuitive. For a
1161                  * driver.conf node, the unit-address properties come from
1162                  * the driver.conf file as system properties. Removing system
1163                  * properties from a driver.conf node makes the node
1164                  * useless (we get node without unit-address properties) - so
1165                  * we leave system properties in place. The result is a node
1166                  * where system properties come from the node being rebound,
1167                  * and global properties come from the driver.conf file
1168                  * of the driver we are rebinding to.  If we could determine
1169                  * that the path-oriented alias driver.conf file defined a
1170                  * node at the same unit address, it would be best to use
1171                  * that node and avoid the non-intuitive property behavior.
1172                  * Unfortunately, the current "merge" code does not support
1173                  * this, so we live with the non-intuitive property behavior.
1174                  */
1175                 if (!((ndi_dev_is_persistent_node(dip) == 0) &&
1176                     (DEVI(dip)->devi_flags & DEVI_REBIND)))
1177                         e_ddi_prop_remove_all(dip);
1178         } else {
1179                 NDI_CONFIG_DEBUG((CE_CONT, "uninit_node failed: 0x%p(%s%d)\n",
1180                     (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
1181         }
1182 
1183         if (addr)
1184                 kmem_free(addr, strlen(addr) + 1);
1185         return (error);
1186 }
1187 
1188 /*
1189  * Invoke driver's probe entry point to probe for existence of hardware.
1190  * Keep instance permanent for successful probe and leaf nodes.
1191  *
1192  * Per-driver list must be held busy while calling this function.
1193  */
1194 static int
1195 probe_node(dev_info_t *dip)
1196 {
1197         int rv;
1198 
1199         ASSERT(i_ddi_node_state(dip) == DS_INITIALIZED);
1200 
1201         NDI_CONFIG_DEBUG((CE_CONT, "probe_node: 0x%p(%s%d)\n",
1202             (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
1203 
1204         /* temporarily hold the driver while we probe */
1205         DEVI(dip)->devi_ops = ndi_hold_driver(dip);
1206         if (DEVI(dip)->devi_ops == NULL) {
1207                 NDI_CONFIG_DEBUG((CE_CONT,
1208                     "probe_node: 0x%p(%s%d) cannot load driver\n",
1209                     (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
1210                 return (DDI_FAILURE);
1211         }
1212 
1213         if (identify_9e != 0)
1214                 (void) devi_identify(dip);
1215 
1216         rv = devi_probe(dip);
1217 
1218         /* release the driver now that probe is complete */
1219         ndi_rele_driver(dip);
1220         DEVI(dip)->devi_ops = NULL;
1221 
1222         switch (rv) {
1223         case DDI_PROBE_SUCCESS:                 /* found */
1224         case DDI_PROBE_DONTCARE:                /* ddi_dev_is_sid */
1225                 e_ddi_keep_instance(dip);       /* persist instance */
1226                 rv = DDI_SUCCESS;
1227                 break;
1228 
1229         case DDI_PROBE_PARTIAL:                 /* maybe later */
1230         case DDI_PROBE_FAILURE:                 /* not found */
1231                 NDI_CONFIG_DEBUG((CE_CONT,
1232                     "probe_node: 0x%p(%s%d) no hardware found%s\n",
1233                     (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip),
1234                     (rv == DDI_PROBE_PARTIAL) ? " yet" : ""));
1235                 rv = DDI_FAILURE;
1236                 break;
1237 
1238         default:
1239 #ifdef  DEBUG
1240                 cmn_err(CE_WARN, "probe_node: %s%d: illegal probe(9E) value",
1241                     ddi_driver_name(dip), ddi_get_instance(dip));
1242 #endif  /* DEBUG */
1243                 rv = DDI_FAILURE;
1244                 break;
1245         }
1246         return (rv);
1247 }
1248 
1249 /*
1250  * Unprobe a node. Simply reset the node state.
1251  * Per-driver list must be held busy while calling this function.
1252  */
1253 static int
1254 unprobe_node(dev_info_t *dip)
1255 {
1256         ASSERT(i_ddi_node_state(dip) == DS_PROBED);
1257 
1258         /*
1259          * Don't check for references here or else a ref-counted
1260          * dip cannot be downgraded by the framework.
1261          */
1262 
1263         NDI_CONFIG_DEBUG((CE_CONT, "unprobe_node: 0x%p(name = %s)\n",
1264             (void *)dip, ddi_node_name(dip)));
1265         return (DDI_SUCCESS);
1266 }
1267 
1268 /*
1269  * Attach devinfo node.
1270  * Per-driver list must be held busy.
1271  */
1272 static int
1273 attach_node(dev_info_t *dip)
1274 {
1275         int rv;
1276 
1277         ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
1278         ASSERT(i_ddi_node_state(dip) == DS_PROBED);
1279 
1280         NDI_CONFIG_DEBUG((CE_CONT, "attach_node: 0x%p(%s%d)\n",
1281             (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
1282 
1283         /*
1284          * Tell mpxio framework that a node is about to online.
1285          */
1286         if ((rv = mdi_devi_online(dip, 0)) != NDI_SUCCESS) {
1287                 return (DDI_FAILURE);
1288         }
1289 
1290         /* no recursive attachment */
1291         ASSERT(DEVI(dip)->devi_ops == NULL);
1292 
1293         /*
1294          * Hold driver the node is bound to.
1295          */
1296         DEVI(dip)->devi_ops = ndi_hold_driver(dip);
1297         if (DEVI(dip)->devi_ops == NULL) {
1298                 /*
1299                  * We were able to load driver for probing, so we should
1300                  * not get here unless something really bad happened.
1301                  */
1302                 cmn_err(CE_WARN, "attach_node: no driver for major %d",
1303                     DEVI(dip)->devi_major);
1304                 return (DDI_FAILURE);
1305         }
1306 
1307         if (NEXUS_DRV(DEVI(dip)->devi_ops))
1308                 DEVI(dip)->devi_taskq = ddi_taskq_create(dip,
1309                     "nexus_enum_tq", 1,
1310                     TASKQ_DEFAULTPRI, 0);
1311 
1312         mutex_enter(&(DEVI(dip)->devi_lock));
1313         DEVI_SET_ATTACHING(dip);
1314         DEVI_SET_NEED_RESET(dip);
1315         mutex_exit(&(DEVI(dip)->devi_lock));
1316 
1317         rv = devi_attach(dip, DDI_ATTACH);
1318 
1319         mutex_enter(&(DEVI(dip)->devi_lock));
1320         DEVI_CLR_ATTACHING(dip);
1321 
1322         if (rv != DDI_SUCCESS) {
1323                 DEVI_CLR_NEED_RESET(dip);
1324                 mutex_exit(&DEVI(dip)->devi_lock);
1325 
1326                 /*
1327                  * Cleanup dacf reservations
1328                  */
1329                 mutex_enter(&dacf_lock);
1330                 dacf_clr_rsrvs(dip, DACF_OPID_POSTATTACH);
1331                 dacf_clr_rsrvs(dip, DACF_OPID_PREDETACH);
1332                 mutex_exit(&dacf_lock);
1333                 if (DEVI(dip)->devi_taskq)
1334                         ddi_taskq_destroy(DEVI(dip)->devi_taskq);
1335                 ddi_remove_minor_node(dip, NULL);
1336 
1337                 /* release the driver if attach failed */
1338                 ndi_rele_driver(dip);
1339                 DEVI(dip)->devi_ops = NULL;
1340                 NDI_CONFIG_DEBUG((CE_CONT, "attach_node: 0x%p(%s%d) failed\n",
1341                     (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
1342                 return (DDI_FAILURE);
1343         } else
1344                 mutex_exit(&DEVI(dip)->devi_lock);
1345 
1346         /* successful attach, return with driver held */
1347 
1348         return (DDI_SUCCESS);
1349 }
1350 
1351 /*
1352  * Detach devinfo node.
1353  * Per-driver list must be held busy.
1354  */
1355 static int
1356 detach_node(dev_info_t *dip, uint_t flag)
1357 {
1358         struct devnames *dnp;
1359         int             rv;
1360 
1361         ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
1362         ASSERT(i_ddi_node_state(dip) == DS_ATTACHED);
1363 
1364         /* Check references */
1365         if (DEVI(dip)->devi_ref != 0 && !DEVI(dip)->devi_gone)
1366                 return (DDI_FAILURE);
1367 
1368         NDI_CONFIG_DEBUG((CE_CONT, "detach_node: 0x%p(%s%d)\n",
1369             (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
1370 
1371         /*
1372          * NOTE: If we are processing a pHCI node then the calling code
1373          * must detect this and ndi_devi_enter() in (vHCI, parent(pHCI))
1374          * order unless pHCI and vHCI are siblings.  Code paths leading
1375          * here that must ensure this ordering include:
1376          * unconfig_immediate_children(), devi_unconfig_one(),
1377          * ndi_devi_unconfig_one(), ndi_devi_offline().
1378          */
1379         ASSERT(!MDI_PHCI(dip) ||
1380             (ddi_get_parent(mdi_devi_get_vdip(dip)) == ddi_get_parent(dip)) ||
1381             DEVI_BUSY_OWNED(mdi_devi_get_vdip(dip)));
1382 
1383         /* Offline the device node with the mpxio framework. */
1384         if (mdi_devi_offline(dip, flag) != NDI_SUCCESS) {
1385                 return (DDI_FAILURE);
1386         }
1387 
1388         /* drain the taskq */
1389         if (DEVI(dip)->devi_taskq)
1390                 ddi_taskq_wait(DEVI(dip)->devi_taskq);
1391 
1392         rv = devi_detach(dip, DDI_DETACH);
1393 
1394         if (rv != DDI_SUCCESS) {
1395                 NDI_CONFIG_DEBUG((CE_CONT,
1396                     "detach_node: 0x%p(%s%d) failed\n",
1397                     (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
1398                 return (DDI_FAILURE);
1399         }
1400 
1401         mutex_enter(&(DEVI(dip)->devi_lock));
1402         DEVI_CLR_NEED_RESET(dip);
1403         mutex_exit(&(DEVI(dip)->devi_lock));
1404 
1405 #if defined(__amd64) && !defined(__xpv)
1406         /*
1407          * Close any iommulib mediated linkage to an IOMMU
1408          */
1409         if (IOMMU_USED(dip))
1410                 iommulib_nex_close(dip);
1411 #endif
1412 
1413         /* destroy the taskq */
1414         if (DEVI(dip)->devi_taskq) {
1415                 ddi_taskq_destroy(DEVI(dip)->devi_taskq);
1416                 DEVI(dip)->devi_taskq = NULL;
1417         }
1418 
1419         /* Cleanup dacf reservations */
1420         mutex_enter(&dacf_lock);
1421         dacf_clr_rsrvs(dip, DACF_OPID_POSTATTACH);
1422         dacf_clr_rsrvs(dip, DACF_OPID_PREDETACH);
1423         mutex_exit(&dacf_lock);
1424 
1425         /* remove any additional flavors that were added */
1426         if (DEVI(dip)->devi_flavorv_n > 1 && DEVI(dip)->devi_flavorv != NULL) {
1427                 kmem_free(DEVI(dip)->devi_flavorv,
1428                     (DEVI(dip)->devi_flavorv_n - 1) * sizeof (void *));
1429                 DEVI(dip)->devi_flavorv = NULL;
1430         }
1431 
1432         /* Remove properties and minor nodes in case driver forgots */
1433         ddi_remove_minor_node(dip, NULL);
1434         ddi_prop_remove_all(dip);
1435 
1436         /* a detached node can't have attached or .conf children */
1437         mutex_enter(&DEVI(dip)->devi_lock);
1438         DEVI(dip)->devi_flags &= ~(DEVI_MADE_CHILDREN|DEVI_ATTACHED_CHILDREN);
1439         mutex_exit(&DEVI(dip)->devi_lock);
1440 
1441         /*
1442          * If the instance has successfully detached in detach_driver() context,
1443          * clear DN_DRIVER_HELD for correct ddi_hold_installed_driver()
1444          * behavior. Consumers like qassociate() depend on this (via clnopen()).
1445          */
1446         if (flag & NDI_DETACH_DRIVER) {
1447                 dnp = &(devnamesp[DEVI(dip)->devi_major]);
1448                 LOCK_DEV_OPS(&dnp->dn_lock);
1449                 dnp->dn_flags &= ~DN_DRIVER_HELD;
1450                 UNLOCK_DEV_OPS(&dnp->dn_lock);
1451         }
1452 
1453         /* successful detach, release the driver */
1454         ndi_rele_driver(dip);
1455         DEVI(dip)->devi_ops = NULL;
1456         return (DDI_SUCCESS);
1457 }
1458 
1459 /*
1460  * Run dacf post_attach routines
1461  */
1462 static int
1463 postattach_node(dev_info_t *dip)
1464 {
1465         int rval;
1466 
1467         /*
1468          * For hotplug busses like USB, it's possible that devices
1469          * are removed but dip is still around. We don't want to
1470          * run dacf routines as part of detach failure recovery.
1471          *
1472          * Pretend success until we figure out how to prevent
1473          * access to such devinfo nodes.
1474          */
1475         if (DEVI_IS_DEVICE_REMOVED(dip))
1476                 return (DDI_SUCCESS);
1477 
1478         /*
1479          * if dacf_postattach failed, report it to the framework
1480          * so that it can be retried later at the open time.
1481          */
1482         mutex_enter(&dacf_lock);
1483         rval = dacfc_postattach(dip);
1484         mutex_exit(&dacf_lock);
1485 
1486         /*
1487          * Plumbing during postattach may fail because of the
1488          * underlying device is not ready. This will fail ndi_devi_config()
1489          * in dv_filldir() and a warning message is issued. The message
1490          * from here will explain what happened
1491          */
1492         if (rval != DACF_SUCCESS) {
1493                 cmn_err(CE_WARN, "Postattach failed for %s%d\n",
1494                     ddi_driver_name(dip), ddi_get_instance(dip));
1495                 return (DDI_FAILURE);
1496         }
1497 
1498         return (DDI_SUCCESS);
1499 }
1500 
1501 /*
1502  * Run dacf pre-detach routines
1503  */
1504 static int
1505 predetach_node(dev_info_t *dip, uint_t flag)
1506 {
1507         int ret;
1508 
1509         /*
1510          * Don't auto-detach if DDI_FORCEATTACH or DDI_NO_AUTODETACH
1511          * properties are set.
1512          */
1513         if (flag & NDI_AUTODETACH) {
1514                 struct devnames *dnp;
1515                 int pflag = DDI_PROP_NOTPROM | DDI_PROP_DONTPASS;
1516 
1517                 if ((ddi_prop_get_int(DDI_DEV_T_ANY, dip,
1518                     pflag, DDI_FORCEATTACH, 0) == 1) ||
1519                     (ddi_prop_get_int(DDI_DEV_T_ANY, dip,
1520                     pflag, DDI_NO_AUTODETACH, 0) == 1))
1521                         return (DDI_FAILURE);
1522 
1523                 /* check for driver global version of DDI_NO_AUTODETACH */
1524                 dnp = &devnamesp[DEVI(dip)->devi_major];
1525                 LOCK_DEV_OPS(&dnp->dn_lock);
1526                 if (dnp->dn_flags & DN_NO_AUTODETACH) {
1527                         UNLOCK_DEV_OPS(&dnp->dn_lock);
1528                         return (DDI_FAILURE);
1529                 }
1530                 UNLOCK_DEV_OPS(&dnp->dn_lock);
1531         }
1532 
1533         mutex_enter(&dacf_lock);
1534         ret = dacfc_predetach(dip);
1535         mutex_exit(&dacf_lock);
1536 
1537         return (ret);
1538 }
1539 
1540 /*
1541  * Wrapper for making multiple state transitions
1542  */
1543 
1544 /*
1545  * i_ndi_config_node: upgrade dev_info node into a specified state.
1546  * It is a bit tricky because the locking protocol changes before and
1547  * after a node is bound to a driver. All locks are held external to
1548  * this function.
1549  */
1550 int
1551 i_ndi_config_node(dev_info_t *dip, ddi_node_state_t state, uint_t flag)
1552 {
1553         _NOTE(ARGUNUSED(flag))
1554         int rv = DDI_SUCCESS;
1555 
1556         ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
1557 
1558         while ((i_ddi_node_state(dip) < state) && (rv == DDI_SUCCESS)) {
1559 
1560                 /* don't allow any more changes to the device tree */
1561                 if (devinfo_freeze) {
1562                         rv = DDI_FAILURE;
1563                         break;
1564                 }
1565 
1566                 switch (i_ddi_node_state(dip)) {
1567                 case DS_PROTO:
1568                         /*
1569                          * only caller can reference this node, no external
1570                          * locking needed.
1571                          */
1572                         link_node(dip);
1573                         translate_devid((dev_info_t *)dip);
1574                         i_ddi_set_node_state(dip, DS_LINKED);
1575                         break;
1576                 case DS_LINKED:
1577                         /*
1578                          * Three code path may attempt to bind a node:
1579                          * - boot code
1580                          * - add_drv
1581                          * - hotplug thread
1582                          * Boot code is single threaded, add_drv synchronize
1583                          * on a userland lock, and hotplug synchronize on
1584                          * hotplug_lk. There could be a race between add_drv
1585                          * and hotplug thread. We'll live with this until the
1586                          * conversion to top-down loading.
1587                          */
1588                         if ((rv = bind_node(dip)) == DDI_SUCCESS)
1589                                 i_ddi_set_node_state(dip, DS_BOUND);
1590 
1591                         break;
1592                 case DS_BOUND:
1593                         /*
1594                          * The following transitions synchronizes on the
1595                          * per-driver busy changing flag, since we already
1596                          * have a driver.
1597                          */
1598                         if ((rv = init_node(dip)) == DDI_SUCCESS)
1599                                 i_ddi_set_node_state(dip, DS_INITIALIZED);
1600                         break;
1601                 case DS_INITIALIZED:
1602                         if ((rv = probe_node(dip)) == DDI_SUCCESS)
1603                                 i_ddi_set_node_state(dip, DS_PROBED);
1604                         break;
1605                 case DS_PROBED:
1606                         /*
1607                          * If node is retired and persistent, then prevent
1608                          * attach. We can't do this for non-persistent nodes
1609                          * as we would lose evidence that the node existed.
1610                          */
1611                         if (i_ddi_check_retire(dip) == 1 &&
1612                             ndi_dev_is_persistent_node(dip) &&
1613                             retire_prevents_attach == 1) {
1614                                 rv = DDI_FAILURE;
1615                                 break;
1616                         }
1617                         atomic_inc_ulong(&devinfo_attach_detach);
1618                         if ((rv = attach_node(dip)) == DDI_SUCCESS)
1619                                 i_ddi_set_node_state(dip, DS_ATTACHED);
1620                         atomic_dec_ulong(&devinfo_attach_detach);
1621                         break;
1622                 case DS_ATTACHED:
1623                         if ((rv = postattach_node(dip)) == DDI_SUCCESS)
1624                                 i_ddi_set_node_state(dip, DS_READY);
1625                         break;
1626                 case DS_READY:
1627                         break;
1628                 default:
1629                         /* should never reach here */
1630                         ASSERT("unknown devinfo state");
1631                 }
1632         }
1633 
1634         if (ddidebug & DDI_AUDIT)
1635                 da_log_enter(dip);
1636         return (rv);
1637 }
1638 
1639 /*
1640  * i_ndi_unconfig_node: downgrade dev_info node into a specified state.
1641  */
1642 int
1643 i_ndi_unconfig_node(dev_info_t *dip, ddi_node_state_t state, uint_t flag)
1644 {
1645         int     rv = DDI_SUCCESS;
1646 
1647         ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
1648 
1649         while ((i_ddi_node_state(dip) > state) && (rv == DDI_SUCCESS)) {
1650 
1651                 /* don't allow any more changes to the device tree */
1652                 if (devinfo_freeze) {
1653                         rv = DDI_FAILURE;
1654                         break;
1655                 }
1656 
1657                 switch (i_ddi_node_state(dip)) {
1658                 case DS_PROTO:
1659                         break;
1660                 case DS_LINKED:
1661                         /*
1662                          * Persistent nodes are only removed by hotplug code
1663                          * .conf nodes synchronizes on per-driver list.
1664                          */
1665                         if ((rv = unlink_node(dip)) == DDI_SUCCESS)
1666                                 i_ddi_set_node_state(dip, DS_PROTO);
1667                         break;
1668                 case DS_BOUND:
1669                         /*
1670                          * The following transitions synchronizes on the
1671                          * per-driver busy changing flag, since we already
1672                          * have a driver.
1673                          */
1674                         if ((rv = unbind_node(dip)) == DDI_SUCCESS)
1675                                 i_ddi_set_node_state(dip, DS_LINKED);
1676                         break;
1677                 case DS_INITIALIZED:
1678                         if ((rv = uninit_node(dip)) == DDI_SUCCESS)
1679                                 i_ddi_set_node_state(dip, DS_BOUND);
1680                         break;
1681                 case DS_PROBED:
1682                         if ((rv = unprobe_node(dip)) == DDI_SUCCESS)
1683                                 i_ddi_set_node_state(dip, DS_INITIALIZED);
1684                         break;
1685                 case DS_ATTACHED:
1686                         atomic_inc_ulong(&devinfo_attach_detach);
1687 
1688                         mutex_enter(&(DEVI(dip)->devi_lock));
1689                         DEVI_SET_DETACHING(dip);
1690                         mutex_exit(&(DEVI(dip)->devi_lock));
1691 
1692                         membar_enter(); /* ensure visibility for hold_devi */
1693 
1694                         if ((rv = detach_node(dip, flag)) == DDI_SUCCESS)
1695                                 i_ddi_set_node_state(dip, DS_PROBED);
1696 
1697                         mutex_enter(&(DEVI(dip)->devi_lock));
1698                         DEVI_CLR_DETACHING(dip);
1699                         mutex_exit(&(DEVI(dip)->devi_lock));
1700 
1701                         atomic_dec_ulong(&devinfo_attach_detach);
1702                         break;
1703                 case DS_READY:
1704                         if ((rv = predetach_node(dip, flag)) == DDI_SUCCESS)
1705                                 i_ddi_set_node_state(dip, DS_ATTACHED);
1706                         break;
1707                 default:
1708                         ASSERT("unknown devinfo state");
1709                 }
1710         }
1711         da_log_enter(dip);
1712         return (rv);
1713 }
1714 
1715 /*
1716  * ddi_initchild: transform node to DS_INITIALIZED state
1717  */
1718 int
1719 ddi_initchild(dev_info_t *parent, dev_info_t *proto)
1720 {
1721         int ret, circ;
1722 
1723         ndi_devi_enter(parent, &circ);
1724         ret = i_ndi_config_node(proto, DS_INITIALIZED, 0);
1725         ndi_devi_exit(parent, circ);
1726 
1727         return (ret);
1728 }
1729 
1730 /*
1731  * ddi_uninitchild: transform node down to DS_BOUND state
1732  */
1733 int
1734 ddi_uninitchild(dev_info_t *dip)
1735 {
1736         int ret, circ;
1737         dev_info_t *parent = ddi_get_parent(dip);
1738         ASSERT(parent);
1739 
1740         ndi_devi_enter(parent, &circ);
1741         ret = i_ndi_unconfig_node(dip, DS_BOUND, 0);
1742         ndi_devi_exit(parent, circ);
1743 
1744         return (ret);
1745 }
1746 
1747 /*
1748  * i_ddi_attachchild: transform node to DS_READY/i_ddi_devi_attached() state
1749  */
1750 static int
1751 i_ddi_attachchild(dev_info_t *dip)
1752 {
1753         dev_info_t      *parent = ddi_get_parent(dip);
1754         int             ret;
1755 
1756         ASSERT(parent && DEVI_BUSY_OWNED(parent));
1757 
1758         if ((i_ddi_node_state(dip) < DS_BOUND) || DEVI_IS_DEVICE_OFFLINE(dip))
1759                 return (DDI_FAILURE);
1760 
1761         ret = i_ndi_config_node(dip, DS_READY, 0);
1762         if (ret == NDI_SUCCESS) {
1763                 ret = DDI_SUCCESS;
1764         } else {
1765                 /*
1766                  * Take it down to DS_INITIALIZED so pm_pre_probe is run
1767                  * on the next attach
1768                  */
1769                 (void) i_ndi_unconfig_node(dip, DS_INITIALIZED, 0);
1770                 ret = DDI_FAILURE;
1771         }
1772 
1773         return (ret);
1774 }
1775 
1776 /*
1777  * i_ddi_detachchild: transform node down to DS_PROBED state
1778  *      If it fails, put it back to DS_READY state.
1779  * NOTE: A node that fails detach may be at DS_ATTACHED instead
1780  * of DS_READY for a small amount of time - this is the source of
1781  * transient DS_READY->DS_ATTACHED->DS_READY state changes.
1782  */
1783 static int
1784 i_ddi_detachchild(dev_info_t *dip, uint_t flags)
1785 {
1786         dev_info_t      *parent = ddi_get_parent(dip);
1787         int             ret;
1788 
1789         ASSERT(parent && DEVI_BUSY_OWNED(parent));
1790 
1791         ret = i_ndi_unconfig_node(dip, DS_PROBED, flags);
1792         if (ret != DDI_SUCCESS)
1793                 (void) i_ndi_config_node(dip, DS_READY, 0);
1794         else
1795                 /* allow pm_pre_probe to reestablish pm state */
1796                 (void) i_ndi_unconfig_node(dip, DS_INITIALIZED, 0);
1797         return (ret);
1798 }
1799 
1800 /*
1801  * Add a child and bind to driver
1802  */
1803 dev_info_t *
1804 ddi_add_child(dev_info_t *pdip, char *name, uint_t nodeid, uint_t unit)
1805 {
1806         int circ;
1807         dev_info_t *dip;
1808 
1809         /* allocate a new node */
1810         dip = i_ddi_alloc_node(pdip, name, nodeid, (int)unit, NULL, KM_SLEEP);
1811 
1812         ndi_devi_enter(pdip, &circ);
1813         (void) i_ndi_config_node(dip, DS_BOUND, 0);
1814         ndi_devi_exit(pdip, circ);
1815         return (dip);
1816 }
1817 
1818 /*
1819  * ddi_remove_child: remove the dip. The parent must be attached and held
1820  */
1821 int
1822 ddi_remove_child(dev_info_t *dip, int dummy)
1823 {
1824         _NOTE(ARGUNUSED(dummy))
1825         int circ, ret;
1826         dev_info_t *parent = ddi_get_parent(dip);
1827         ASSERT(parent);
1828 
1829         ndi_devi_enter(parent, &circ);
1830 
1831         /*
1832          * If we still have children, for example SID nodes marked
1833          * as persistent but not attached, attempt to remove them.
1834          */
1835         if (DEVI(dip)->devi_child) {
1836                 ret = ndi_devi_unconfig(dip, NDI_DEVI_REMOVE);
1837                 if (ret != NDI_SUCCESS) {
1838                         ndi_devi_exit(parent, circ);
1839                         return (DDI_FAILURE);
1840                 }
1841                 ASSERT(DEVI(dip)->devi_child == NULL);
1842         }
1843 
1844         ret = i_ndi_unconfig_node(dip, DS_PROTO, 0);
1845         ndi_devi_exit(parent, circ);
1846 
1847         if (ret != DDI_SUCCESS)
1848                 return (ret);
1849 
1850         ASSERT(i_ddi_node_state(dip) == DS_PROTO);
1851         i_ddi_free_node(dip);
1852         return (DDI_SUCCESS);
1853 }
1854 
1855 /*
1856  * NDI wrappers for ref counting, node allocation, and transitions
1857  */
1858 
1859 /*
1860  * Hold/release the devinfo node itself.
1861  * Caller is assumed to prevent the devi from detaching during this call
1862  */
1863 void
1864 ndi_hold_devi(dev_info_t *dip)
1865 {
1866         mutex_enter(&DEVI(dip)->devi_lock);
1867         ASSERT(DEVI(dip)->devi_ref >= 0);
1868         DEVI(dip)->devi_ref++;
1869         membar_enter();                 /* make sure stores are flushed */
1870         mutex_exit(&DEVI(dip)->devi_lock);
1871 }
1872 
1873 void
1874 ndi_rele_devi(dev_info_t *dip)
1875 {
1876         ASSERT(DEVI(dip)->devi_ref > 0);
1877 
1878         mutex_enter(&DEVI(dip)->devi_lock);
1879         DEVI(dip)->devi_ref--;
1880         membar_enter();                 /* make sure stores are flushed */
1881         mutex_exit(&DEVI(dip)->devi_lock);
1882 }
1883 
1884 int
1885 e_ddi_devi_holdcnt(dev_info_t *dip)
1886 {
1887         return (DEVI(dip)->devi_ref);
1888 }
1889 
1890 /*
1891  * Hold/release the driver the devinfo node is bound to.
1892  */
1893 struct dev_ops *
1894 ndi_hold_driver(dev_info_t *dip)
1895 {
1896         if (i_ddi_node_state(dip) < DS_BOUND)
1897                 return (NULL);
1898 
1899         ASSERT(DEVI(dip)->devi_major != -1);
1900         return (mod_hold_dev_by_major(DEVI(dip)->devi_major));
1901 }
1902 
1903 void
1904 ndi_rele_driver(dev_info_t *dip)
1905 {
1906         ASSERT(i_ddi_node_state(dip) >= DS_BOUND);
1907         mod_rele_dev_by_major(DEVI(dip)->devi_major);
1908 }
1909 
1910 /*
1911  * Single thread entry into devinfo node for modifying its children (devinfo,
1912  * pathinfo, and minor). To verify in ASSERTS use DEVI_BUSY_OWNED macro.
1913  */
1914 void
1915 ndi_devi_enter(dev_info_t *dip, int *circular)
1916 {
1917         struct dev_info *devi = DEVI(dip);
1918         ASSERT(dip != NULL);
1919 
1920         /* for vHCI, enforce (vHCI, pHCI) ndi_deve_enter() order */
1921         ASSERT(!MDI_VHCI(dip) || (mdi_devi_pdip_entered(dip) == 0) ||
1922             DEVI_BUSY_OWNED(dip));
1923 
1924         mutex_enter(&devi->devi_lock);
1925         if (devi->devi_busy_thread == curthread) {
1926                 devi->devi_circular++;
1927         } else {
1928                 while (DEVI_BUSY_CHANGING(devi) && !panicstr)
1929                         cv_wait(&(devi->devi_cv), &(devi->devi_lock));
1930                 if (panicstr) {
1931                         mutex_exit(&devi->devi_lock);
1932                         return;
1933                 }
1934                 devi->devi_flags |= DEVI_BUSY;
1935                 devi->devi_busy_thread = curthread;
1936         }
1937         *circular = devi->devi_circular;
1938         mutex_exit(&devi->devi_lock);
1939 }
1940 
1941 /*
1942  * Release ndi_devi_enter or successful ndi_devi_tryenter.
1943  */
1944 void
1945 ndi_devi_exit(dev_info_t *dip, int circular)
1946 {
1947         struct dev_info *devi = DEVI(dip);
1948         struct dev_info *vdevi;
1949         ASSERT(dip != NULL);
1950 
1951         if (panicstr)
1952                 return;
1953 
1954         mutex_enter(&(devi->devi_lock));
1955         if (circular != 0) {
1956                 devi->devi_circular--;
1957         } else {
1958                 devi->devi_flags &= ~DEVI_BUSY;
1959                 ASSERT(devi->devi_busy_thread == curthread);
1960                 devi->devi_busy_thread = NULL;
1961                 cv_broadcast(&(devi->devi_cv));
1962         }
1963         mutex_exit(&(devi->devi_lock));
1964 
1965         /*
1966          * For pHCI exit we issue a broadcast to vHCI for ndi_devi_config_one()
1967          * doing cv_wait on vHCI.
1968          */
1969         if (MDI_PHCI(dip)) {
1970                 vdevi = DEVI(mdi_devi_get_vdip(dip));
1971                 if (vdevi) {
1972                         mutex_enter(&(vdevi->devi_lock));
1973                         if (vdevi->devi_flags & DEVI_PHCI_SIGNALS_VHCI) {
1974                                 vdevi->devi_flags &= ~DEVI_PHCI_SIGNALS_VHCI;
1975                                 cv_broadcast(&(vdevi->devi_cv));
1976                         }
1977                         mutex_exit(&(vdevi->devi_lock));
1978                 }
1979         }
1980 }
1981 
1982 /*
1983  * Release ndi_devi_enter and wait for possibility of new children, avoiding
1984  * possibility of missing broadcast before getting to cv_timedwait().
1985  */
1986 static void
1987 ndi_devi_exit_and_wait(dev_info_t *dip, int circular, clock_t end_time)
1988 {
1989         struct dev_info *devi = DEVI(dip);
1990         ASSERT(dip != NULL);
1991 
1992         if (panicstr)
1993                 return;
1994 
1995         /*
1996          * We are called to wait for of a new child, and new child can
1997          * only be added if circular is zero.
1998          */
1999         ASSERT(circular == 0);
2000 
2001         /* like ndi_devi_exit with circular of zero */
2002         mutex_enter(&(devi->devi_lock));
2003         devi->devi_flags &= ~DEVI_BUSY;
2004         ASSERT(devi->devi_busy_thread == curthread);
2005         devi->devi_busy_thread = NULL;
2006         cv_broadcast(&(devi->devi_cv));
2007 
2008         /* now wait for new children while still holding devi_lock */
2009         (void) cv_timedwait(&devi->devi_cv, &(devi->devi_lock), end_time);
2010         mutex_exit(&(devi->devi_lock));
2011 }
2012 
2013 /*
2014  * Attempt to single thread entry into devinfo node for modifying its children.
2015  */
2016 int
2017 ndi_devi_tryenter(dev_info_t *dip, int *circular)
2018 {
2019         int rval = 1;              /* assume we enter */
2020         struct dev_info *devi = DEVI(dip);
2021         ASSERT(dip != NULL);
2022 
2023         mutex_enter(&devi->devi_lock);
2024         if (devi->devi_busy_thread == (void *)curthread) {
2025                 devi->devi_circular++;
2026         } else {
2027                 if (!DEVI_BUSY_CHANGING(devi)) {
2028                         devi->devi_flags |= DEVI_BUSY;
2029                         devi->devi_busy_thread = (void *)curthread;
2030                 } else {
2031                         rval = 0;       /* devi is busy */
2032                 }
2033         }
2034         *circular = devi->devi_circular;
2035         mutex_exit(&devi->devi_lock);
2036         return (rval);
2037 }
2038 
2039 /*
2040  * Allocate and initialize a new dev_info structure.
2041  *
2042  * This routine may be called at interrupt time by a nexus in
2043  * response to a hotplug event, therefore memory allocations are
2044  * not allowed to sleep.
2045  */
2046 int
2047 ndi_devi_alloc(dev_info_t *parent, char *node_name, pnode_t nodeid,
2048     dev_info_t **ret_dip)
2049 {
2050         ASSERT(node_name != NULL);
2051         ASSERT(ret_dip != NULL);
2052 
2053         *ret_dip = i_ddi_alloc_node(parent, node_name, nodeid, -1, NULL,
2054             KM_NOSLEEP);
2055         if (*ret_dip == NULL) {
2056                 return (NDI_NOMEM);
2057         }
2058 
2059         return (NDI_SUCCESS);
2060 }
2061 
2062 /*
2063  * Allocate and initialize a new dev_info structure
2064  * This routine may sleep and should not be called at interrupt time
2065  */
2066 void
2067 ndi_devi_alloc_sleep(dev_info_t *parent, char *node_name, pnode_t nodeid,
2068     dev_info_t **ret_dip)
2069 {
2070         ASSERT(node_name != NULL);
2071         ASSERT(ret_dip != NULL);
2072 
2073         *ret_dip = i_ddi_alloc_node(parent, node_name, nodeid, -1, NULL,
2074             KM_SLEEP);
2075         ASSERT(*ret_dip);
2076 }
2077 
2078 /*
2079  * Remove an initialized (but not yet attached) dev_info
2080  * node from it's parent.
2081  */
2082 int
2083 ndi_devi_free(dev_info_t *dip)
2084 {
2085         ASSERT(dip != NULL);
2086 
2087         if (i_ddi_node_state(dip) >= DS_INITIALIZED)
2088                 return (DDI_FAILURE);
2089 
2090         NDI_CONFIG_DEBUG((CE_CONT, "ndi_devi_free: %s%d (%p)\n",
2091             ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip));
2092 
2093         (void) ddi_remove_child(dip, 0);
2094 
2095         return (NDI_SUCCESS);
2096 }
2097 
2098 /*
2099  * ndi_devi_bind_driver() binds a driver to a given device. If it fails
2100  * to bind the driver, it returns an appropriate error back. Some drivers
2101  * may want to know if the actually failed to bind.
2102  */
2103 int
2104 ndi_devi_bind_driver(dev_info_t *dip, uint_t flags)
2105 {
2106         int ret = NDI_FAILURE;
2107         int circ;
2108         dev_info_t *pdip = ddi_get_parent(dip);
2109         ASSERT(pdip);
2110 
2111         NDI_CONFIG_DEBUG((CE_CONT,
2112             "ndi_devi_bind_driver: %s%d (%p) flags: %x\n",
2113             ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
2114 
2115         ndi_devi_enter(pdip, &circ);
2116         if (i_ndi_config_node(dip, DS_BOUND, flags) == DDI_SUCCESS)
2117                 ret = NDI_SUCCESS;
2118         ndi_devi_exit(pdip, circ);
2119 
2120         return (ret);
2121 }
2122 
2123 /*
2124  * ndi_devi_unbind_driver: unbind the dip
2125  */
2126 static int
2127 ndi_devi_unbind_driver(dev_info_t *dip)
2128 {
2129         ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
2130 
2131         return (i_ndi_unconfig_node(dip, DS_LINKED, 0));
2132 }
2133 
2134 /*
2135  * Misc. help routines called by framework only
2136  */
2137 
2138 /*
2139  * Get the state of node
2140  */
2141 ddi_node_state_t
2142 i_ddi_node_state(dev_info_t *dip)
2143 {
2144         return (DEVI(dip)->devi_node_state);
2145 }
2146 
2147 /*
2148  * Set the state of node
2149  */
2150 void
2151 i_ddi_set_node_state(dev_info_t *dip, ddi_node_state_t state)
2152 {
2153         DEVI(dip)->devi_node_state = state;
2154         membar_enter();                 /* make sure stores are flushed */
2155 }
2156 
2157 /*
2158  * Determine if node is attached. The implementation accommodates transient
2159  * DS_READY->DS_ATTACHED->DS_READY state changes.  Outside this file, this
2160  * function should be instead of i_ddi_node_state() DS_ATTACHED/DS_READY
2161  * state checks.
2162  */
2163 int
2164 i_ddi_devi_attached(dev_info_t *dip)
2165 {
2166         return (DEVI(dip)->devi_node_state >= DS_ATTACHED);
2167 }
2168 
2169 /*
2170  * Common function for finding a node in a sibling list given name and addr.
2171  *
2172  * By default, name is matched with devi_node_name. The following
2173  * alternative match strategies are supported:
2174  *
2175  *      FIND_NODE_BY_NODENAME: Match on node name - typical use.
2176  *
2177  *      FIND_NODE_BY_DRIVER: A match on driver name bound to node is conducted.
2178  *              This support is used for support of OBP generic names and
2179  *              for the conversion from driver names to generic names. When
2180  *              more consistency in the generic name environment is achieved
2181  *              (and not needed for upgrade) this support can be removed.
2182  *
2183  *      FIND_NODE_BY_ADDR: Match on just the addr.
2184  *              This support is only used/needed during boot to match
2185  *              a node bound via a path-based driver alias.
2186  *
2187  * If a child is not named (dev_addr == NULL), there are three
2188  * possible actions:
2189  *
2190  *      (1) skip it
2191  *      (2) FIND_ADDR_BY_INIT: bring child to DS_INITIALIZED state
2192  *      (3) FIND_ADDR_BY_CALLBACK: use a caller-supplied callback function
2193  */
2194 #define FIND_NODE_BY_NODENAME   0x01
2195 #define FIND_NODE_BY_DRIVER     0x02
2196 #define FIND_NODE_BY_ADDR       0x04
2197 #define FIND_ADDR_BY_INIT       0x10
2198 #define FIND_ADDR_BY_CALLBACK   0x20
2199 
2200 static dev_info_t *
2201 find_sibling(dev_info_t *head, char *cname, char *caddr, uint_t flag,
2202     int (*callback)(dev_info_t *, char *, int))
2203 {
2204         dev_info_t      *dip;
2205         char            *addr, *buf;
2206         major_t         major;
2207         uint_t          by;
2208 
2209         /* only one way to find a node */
2210         by = flag &
2211             (FIND_NODE_BY_DRIVER | FIND_NODE_BY_NODENAME | FIND_NODE_BY_ADDR);
2212         ASSERT(by && BIT_ONLYONESET(by));
2213 
2214         /* only one way to name a node */
2215         ASSERT(((flag & FIND_ADDR_BY_INIT) == 0) ||
2216             ((flag & FIND_ADDR_BY_CALLBACK) == 0));
2217 
2218         if (by == FIND_NODE_BY_DRIVER) {
2219                 major = ddi_name_to_major(cname);
2220                 if (major == DDI_MAJOR_T_NONE)
2221                         return (NULL);
2222         }
2223 
2224         /* preallocate buffer of naming node by callback */
2225         if (flag & FIND_ADDR_BY_CALLBACK)
2226                 buf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
2227 
2228         /*
2229          * Walk the child list to find a match
2230          */
2231         if (head == NULL)
2232                 return (NULL);
2233         ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(head)));
2234         for (dip = head; dip; dip = ddi_get_next_sibling(dip)) {
2235                 if (by == FIND_NODE_BY_NODENAME) {
2236                         /* match node name */
2237                         if (strcmp(cname, DEVI(dip)->devi_node_name) != 0)
2238                                 continue;
2239                 } else if (by == FIND_NODE_BY_DRIVER) {
2240                         /* match driver major */
2241                         if (DEVI(dip)->devi_major != major)
2242                                 continue;
2243                 }
2244 
2245                 if ((addr = DEVI(dip)->devi_addr) == NULL) {
2246                         /* name the child based on the flag */
2247                         if (flag & FIND_ADDR_BY_INIT) {
2248                                 if (ddi_initchild(ddi_get_parent(dip), dip)
2249                                     != DDI_SUCCESS)
2250                                         continue;
2251                                 addr = DEVI(dip)->devi_addr;
2252                         } else if (flag & FIND_ADDR_BY_CALLBACK) {
2253                                 if ((callback == NULL) || (callback(
2254                                     dip, buf, MAXNAMELEN) != DDI_SUCCESS))
2255                                         continue;
2256                                 addr = buf;
2257                         } else {
2258                                 continue;       /* skip */
2259                         }
2260                 }
2261 
2262                 /* match addr */
2263                 ASSERT(addr != NULL);
2264                 if (strcmp(caddr, addr) == 0)
2265                         break;  /* node found */
2266 
2267         }
2268         if (flag & FIND_ADDR_BY_CALLBACK)
2269                 kmem_free(buf, MAXNAMELEN);
2270         return (dip);
2271 }
2272 
2273 /*
2274  * Find child of pdip with name: cname@caddr
2275  * Called by init_node() to look for duplicate nodes
2276  */
2277 static dev_info_t *
2278 find_duplicate_child(dev_info_t *pdip, dev_info_t *dip)
2279 {
2280         dev_info_t *dup;
2281         char *cname = DEVI(dip)->devi_node_name;
2282         char *caddr = DEVI(dip)->devi_addr;
2283 
2284         /* search nodes before dip */
2285         dup = find_sibling(ddi_get_child(pdip), cname, caddr,
2286             FIND_NODE_BY_NODENAME, NULL);
2287         if (dup != dip)
2288                 return (dup);
2289 
2290         /*
2291          * search nodes after dip; normally this is not needed,
2292          */
2293         return (find_sibling(ddi_get_next_sibling(dip), cname, caddr,
2294             FIND_NODE_BY_NODENAME, NULL));
2295 }
2296 
2297 /*
2298  * Find a child of a given name and address, using a callback to name
2299  * unnamed children. cname is the binding name.
2300  */
2301 dev_info_t *
2302 ndi_devi_findchild_by_callback(dev_info_t *pdip, char *dname, char *ua,
2303     int (*make_ua)(dev_info_t *, char *, int))
2304 {
2305         int     by = FIND_ADDR_BY_CALLBACK;
2306 
2307         ASSERT(DEVI_BUSY_OWNED(pdip));
2308         by |= dname ? FIND_NODE_BY_DRIVER : FIND_NODE_BY_ADDR;
2309         return (find_sibling(ddi_get_child(pdip), dname, ua, by, make_ua));
2310 }
2311 
2312 /*
2313  * Find a child of a given name and address, invoking initchild to name
2314  * unnamed children. cname is the node name.
2315  */
2316 static dev_info_t *
2317 find_child_by_name(dev_info_t *pdip, char *cname, char *caddr)
2318 {
2319         dev_info_t      *dip;
2320 
2321         /* attempt search without changing state of preceding siblings */
2322         dip = find_sibling(ddi_get_child(pdip), cname, caddr,
2323             FIND_NODE_BY_NODENAME, NULL);
2324         if (dip)
2325                 return (dip);
2326 
2327         return (find_sibling(ddi_get_child(pdip), cname, caddr,
2328             FIND_NODE_BY_NODENAME|FIND_ADDR_BY_INIT, NULL));
2329 }
2330 
2331 /*
2332  * Find a child of a given name and address, invoking initchild to name
2333  * unnamed children. cname is the node name.
2334  */
2335 static dev_info_t *
2336 find_child_by_driver(dev_info_t *pdip, char *cname, char *caddr)
2337 {
2338         dev_info_t      *dip;
2339 
2340         /* attempt search without changing state of preceding siblings */
2341         dip = find_sibling(ddi_get_child(pdip), cname, caddr,
2342             FIND_NODE_BY_DRIVER, NULL);
2343         if (dip)
2344                 return (dip);
2345 
2346         return (find_sibling(ddi_get_child(pdip), cname, caddr,
2347             FIND_NODE_BY_DRIVER|FIND_ADDR_BY_INIT, NULL));
2348 }
2349 
2350 /*
2351  * Find a child of a given address, invoking initchild to name
2352  * unnamed children. cname is the node name.
2353  *
2354  * NOTE: This function is only used during boot. One would hope that
2355  * unique sibling unit-addresses on hardware branches of the tree would
2356  * be a requirement to avoid two drivers trying to control the same
2357  * piece of hardware. Unfortunately there are some cases where this
2358  * situation exists (/ssm@0,0/pci@1c,700000 /ssm@0,0/sghsc@1c,700000).
2359  * Until unit-address uniqueness of siblings is guaranteed, use of this
2360  * interface for purposes other than boot should be avoided.
2361  */
2362 static dev_info_t *
2363 find_child_by_addr(dev_info_t *pdip, char *caddr)
2364 {
2365         dev_info_t      *dip;
2366 
2367         /* return NULL if called without a unit-address */
2368         if ((caddr == NULL) || (*caddr == '\0'))
2369                 return (NULL);
2370 
2371         /* attempt search without changing state of preceding siblings */
2372         dip = find_sibling(ddi_get_child(pdip), NULL, caddr,
2373             FIND_NODE_BY_ADDR, NULL);
2374         if (dip)
2375                 return (dip);
2376 
2377         return (find_sibling(ddi_get_child(pdip), NULL, caddr,
2378             FIND_NODE_BY_ADDR|FIND_ADDR_BY_INIT, NULL));
2379 }
2380 
2381 /*
2382  * Deleting a property list. Take care, since some property structures
2383  * may not be fully built.
2384  */
2385 void
2386 i_ddi_prop_list_delete(ddi_prop_t *prop)
2387 {
2388         while (prop) {
2389                 ddi_prop_t *next = prop->prop_next;
2390                 if (prop->prop_name)
2391                         kmem_free(prop->prop_name, strlen(prop->prop_name) + 1);
2392                 if ((prop->prop_len != 0) && prop->prop_val)
2393                         kmem_free(prop->prop_val, prop->prop_len);
2394                 kmem_free(prop, sizeof (struct ddi_prop));
2395                 prop = next;
2396         }
2397 }
2398 
2399 /*
2400  * Duplicate property list
2401  */
2402 ddi_prop_t *
2403 i_ddi_prop_list_dup(ddi_prop_t *prop, uint_t flag)
2404 {
2405         ddi_prop_t *result, *prev, *copy;
2406 
2407         if (prop == NULL)
2408                 return (NULL);
2409 
2410         result = prev = NULL;
2411         for (; prop != NULL; prop = prop->prop_next) {
2412                 ASSERT(prop->prop_name != NULL);
2413                 copy = kmem_zalloc(sizeof (struct ddi_prop), flag);
2414                 if (copy == NULL)
2415                         goto fail;
2416 
2417                 copy->prop_dev = prop->prop_dev;
2418                 copy->prop_flags = prop->prop_flags;
2419                 copy->prop_name = i_ddi_strdup(prop->prop_name, flag);
2420                 if (copy->prop_name == NULL)
2421                         goto fail;
2422 
2423                 if ((copy->prop_len = prop->prop_len) != 0) {
2424                         copy->prop_val = kmem_zalloc(prop->prop_len, flag);
2425                         if (copy->prop_val == NULL)
2426                                 goto fail;
2427 
2428                         bcopy(prop->prop_val, copy->prop_val, prop->prop_len);
2429                 }
2430 
2431                 if (prev == NULL)
2432                         result = prev = copy;
2433                 else
2434                         prev->prop_next = copy;
2435                 prev = copy;
2436         }
2437         return (result);
2438 
2439 fail:
2440         i_ddi_prop_list_delete(result);
2441         return (NULL);
2442 }
2443 
2444 /*
2445  * Create a reference property list, currently used only for
2446  * driver global properties. Created with ref count of 1.
2447  */
2448 ddi_prop_list_t *
2449 i_ddi_prop_list_create(ddi_prop_t *props)
2450 {
2451         ddi_prop_list_t *list = kmem_alloc(sizeof (*list), KM_SLEEP);
2452         list->prop_list = props;
2453         list->prop_ref = 1;
2454         return (list);
2455 }
2456 
2457 /*
2458  * Increment/decrement reference count. The reference is
2459  * protected by dn_lock. The only interfaces modifying
2460  * dn_global_prop_ptr is in impl_make[free]_parlist().
2461  */
2462 void
2463 i_ddi_prop_list_hold(ddi_prop_list_t *prop_list, struct devnames *dnp)
2464 {
2465         ASSERT(prop_list->prop_ref >= 0);
2466         ASSERT(mutex_owned(&dnp->dn_lock));
2467         prop_list->prop_ref++;
2468 }
2469 
2470 void
2471 i_ddi_prop_list_rele(ddi_prop_list_t *prop_list, struct devnames *dnp)
2472 {
2473         ASSERT(prop_list->prop_ref > 0);
2474         ASSERT(mutex_owned(&dnp->dn_lock));
2475         prop_list->prop_ref--;
2476 
2477         if (prop_list->prop_ref == 0) {
2478                 i_ddi_prop_list_delete(prop_list->prop_list);
2479                 kmem_free(prop_list, sizeof (*prop_list));
2480         }
2481 }
2482 
2483 /*
2484  * Free table of classes by drivers
2485  */
2486 void
2487 i_ddi_free_exported_classes(char **classes, int n)
2488 {
2489         if ((n == 0) || (classes == NULL))
2490                 return;
2491 
2492         kmem_free(classes, n * sizeof (char *));
2493 }
2494 
2495 /*
2496  * Get all classes exported by dip
2497  */
2498 int
2499 i_ddi_get_exported_classes(dev_info_t *dip, char ***classes)
2500 {
2501         extern void lock_hw_class_list();
2502         extern void unlock_hw_class_list();
2503         extern int get_class(const char *, char **);
2504 
2505         static char *rootclass = "root";
2506         int n = 0, nclass = 0;
2507         char **buf;
2508 
2509         ASSERT(i_ddi_node_state(dip) >= DS_BOUND);
2510 
2511         if (dip == ddi_root_node())     /* rootnode exports class "root" */
2512                 nclass = 1;
2513         lock_hw_class_list();
2514         nclass += get_class(ddi_driver_name(dip), NULL);
2515         if (nclass == 0) {
2516                 unlock_hw_class_list();
2517                 return (0);             /* no class exported */
2518         }
2519 
2520         *classes = buf = kmem_alloc(nclass * sizeof (char *), KM_SLEEP);
2521         if (dip == ddi_root_node()) {
2522                 *buf++ = rootclass;
2523                 n = 1;
2524         }
2525         n += get_class(ddi_driver_name(dip), buf);
2526         unlock_hw_class_list();
2527 
2528         ASSERT(n == nclass);    /* make sure buf wasn't overrun */
2529         return (nclass);
2530 }
2531 
2532 /*
2533  * Helper functions, returns NULL if no memory.
2534  */
2535 char *
2536 i_ddi_strdup(char *str, uint_t flag)
2537 {
2538         char *copy;
2539 
2540         if (str == NULL)
2541                 return (NULL);
2542 
2543         copy = kmem_alloc(strlen(str) + 1, flag);
2544         if (copy == NULL)
2545                 return (NULL);
2546 
2547         (void) strcpy(copy, str);
2548         return (copy);
2549 }
2550 
2551 /*
2552  * Load driver.conf file for major. Load all if major == -1.
2553  *
2554  * This is called
2555  * - early in boot after devnames array is initialized
2556  * - from vfs code when certain file systems are mounted
2557  * - from add_drv when a new driver is added
2558  */
2559 int
2560 i_ddi_load_drvconf(major_t major)
2561 {
2562         extern int modrootloaded;
2563 
2564         major_t low, high, m;
2565 
2566         if (major == DDI_MAJOR_T_NONE) {
2567                 low = 0;
2568                 high = devcnt - 1;
2569         } else {
2570                 if (major >= devcnt)
2571                         return (EINVAL);
2572                 low = high = major;
2573         }
2574 
2575         for (m = low; m <= high; m++) {
2576                 struct devnames *dnp = &devnamesp[m];
2577                 LOCK_DEV_OPS(&dnp->dn_lock);
2578                 dnp->dn_flags &= ~(DN_DRIVER_HELD|DN_DRIVER_INACTIVE);
2579                 (void) impl_make_parlist(m);
2580                 UNLOCK_DEV_OPS(&dnp->dn_lock);
2581         }
2582 
2583         if (modrootloaded) {
2584                 ddi_walk_devs(ddi_root_node(), reset_nexus_flags,
2585                     (void *)(uintptr_t)major);
2586         }
2587 
2588         /* build dn_list from old entries in path_to_inst */
2589         e_ddi_unorphan_instance_nos();
2590         return (0);
2591 }
2592 
2593 /*
2594  * Unload a specific driver.conf.
2595  * Don't support unload all because it doesn't make any sense
2596  */
2597 int
2598 i_ddi_unload_drvconf(major_t major)
2599 {
2600         int error;
2601         struct devnames *dnp;
2602 
2603         if (major >= devcnt)
2604                 return (EINVAL);
2605 
2606         /*
2607          * Take the per-driver lock while unloading driver.conf
2608          */
2609         dnp = &devnamesp[major];
2610         LOCK_DEV_OPS(&dnp->dn_lock);
2611         error = impl_free_parlist(major);
2612         UNLOCK_DEV_OPS(&dnp->dn_lock);
2613         return (error);
2614 }
2615 
2616 /*
2617  * Merge a .conf node. This is called by nexus drivers to augment
2618  * hw node with properties specified in driver.conf file. This function
2619  * takes a callback routine to name nexus children.
2620  * The parent node must be held busy.
2621  *
2622  * It returns DDI_SUCCESS if the node is merged and DDI_FAILURE otherwise.
2623  */
2624 int
2625 ndi_merge_node(dev_info_t *dip, int (*make_ua)(dev_info_t *, char *, int))
2626 {
2627         dev_info_t *hwdip;
2628 
2629         ASSERT(ndi_dev_is_persistent_node(dip) == 0);
2630         ASSERT(ddi_get_name_addr(dip) != NULL);
2631 
2632         hwdip = ndi_devi_findchild_by_callback(ddi_get_parent(dip),
2633             ddi_binding_name(dip), ddi_get_name_addr(dip), make_ua);
2634 
2635         /*
2636          * Look for the hardware node that is the target of the merge;
2637          * return failure if not found.
2638          */
2639         if ((hwdip == NULL) || (hwdip == dip)) {
2640                 char *buf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
2641                 NDI_CONFIG_DEBUG((CE_WARN, "No HW node to merge conf node %s",
2642                     ddi_deviname(dip, buf)));
2643                 kmem_free(buf, MAXNAMELEN);
2644                 return (DDI_FAILURE);
2645         }
2646 
2647         /*
2648          * Make sure the hardware node is uninitialized and has no property.
2649          * This may not be the case if new .conf files are load after some
2650          * hardware nodes have already been initialized and attached.
2651          *
2652          * N.B. We return success here because the node was *intended*
2653          *      to be a merge node because there is a hw node with the name.
2654          */
2655         mutex_enter(&DEVI(hwdip)->devi_lock);
2656         if (ndi_dev_is_persistent_node(hwdip) == 0) {
2657                 char *buf;
2658                 mutex_exit(&DEVI(hwdip)->devi_lock);
2659 
2660                 buf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
2661                 NDI_CONFIG_DEBUG((CE_NOTE, "Duplicate .conf node %s",
2662                     ddi_deviname(dip, buf)));
2663                 kmem_free(buf, MAXNAMELEN);
2664                 return (DDI_SUCCESS);
2665         }
2666 
2667         /*
2668          * If it is possible that the hardware has already been touched
2669          * then don't merge.
2670          */
2671         if (i_ddi_node_state(hwdip) >= DS_INITIALIZED ||
2672             (DEVI(hwdip)->devi_sys_prop_ptr != NULL) ||
2673             (DEVI(hwdip)->devi_drv_prop_ptr != NULL)) {
2674                 char *buf;
2675                 mutex_exit(&DEVI(hwdip)->devi_lock);
2676 
2677                 buf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
2678                 NDI_CONFIG_DEBUG((CE_NOTE,
2679                     "!Cannot merge .conf node %s with hw node %p "
2680                     "-- not in proper state",
2681                     ddi_deviname(dip, buf), (void *)hwdip));
2682                 kmem_free(buf, MAXNAMELEN);
2683                 return (DDI_SUCCESS);
2684         }
2685 
2686         mutex_enter(&DEVI(dip)->devi_lock);
2687         DEVI(hwdip)->devi_sys_prop_ptr = DEVI(dip)->devi_sys_prop_ptr;
2688         DEVI(hwdip)->devi_drv_prop_ptr = DEVI(dip)->devi_drv_prop_ptr;
2689         DEVI(dip)->devi_sys_prop_ptr = NULL;
2690         DEVI(dip)->devi_drv_prop_ptr = NULL;
2691         mutex_exit(&DEVI(dip)->devi_lock);
2692         mutex_exit(&DEVI(hwdip)->devi_lock);
2693 
2694         return (DDI_SUCCESS);
2695 }
2696 
2697 /*
2698  * Merge a "wildcard" .conf node. This is called by nexus drivers to
2699  * augment a set of hw node with properties specified in driver.conf file.
2700  * The parent node must be held busy.
2701  *
2702  * There is no failure mode, since the nexus may or may not have child
2703  * node bound the driver specified by the wildcard node.
2704  */
2705 void
2706 ndi_merge_wildcard_node(dev_info_t *dip)
2707 {
2708         dev_info_t *hwdip;
2709         dev_info_t *pdip = ddi_get_parent(dip);
2710         major_t major = ddi_driver_major(dip);
2711 
2712         /* never attempt to merge a hw node */
2713         ASSERT(ndi_dev_is_persistent_node(dip) == 0);
2714         /* must be bound to a driver major number */
2715         ASSERT(major != DDI_MAJOR_T_NONE);
2716 
2717         /*
2718          * Walk the child list to find all nodes bound to major
2719          * and copy properties.
2720          */
2721         mutex_enter(&DEVI(dip)->devi_lock);
2722         ASSERT(DEVI_BUSY_OWNED(pdip));
2723         for (hwdip = ddi_get_child(pdip); hwdip;
2724             hwdip = ddi_get_next_sibling(hwdip)) {
2725                 /*
2726                  * Skip nodes not bound to same driver
2727                  */
2728                 if (ddi_driver_major(hwdip) != major)
2729                         continue;
2730 
2731                 /*
2732                  * Skip .conf nodes
2733                  */
2734                 if (ndi_dev_is_persistent_node(hwdip) == 0)
2735                         continue;
2736 
2737                 /*
2738                  * Make sure the node is uninitialized and has no property.
2739                  */
2740                 mutex_enter(&DEVI(hwdip)->devi_lock);
2741                 if (i_ddi_node_state(hwdip) >= DS_INITIALIZED ||
2742                     (DEVI(hwdip)->devi_sys_prop_ptr != NULL) ||
2743                     (DEVI(hwdip)->devi_drv_prop_ptr != NULL)) {
2744                         mutex_exit(&DEVI(hwdip)->devi_lock);
2745                         NDI_CONFIG_DEBUG((CE_NOTE, "HW node %p state not "
2746                             "suitable for merging wildcard conf node %s",
2747                             (void *)hwdip, ddi_node_name(dip)));
2748                         continue;
2749                 }
2750 
2751                 DEVI(hwdip)->devi_sys_prop_ptr =
2752                     i_ddi_prop_list_dup(DEVI(dip)->devi_sys_prop_ptr, KM_SLEEP);
2753                 DEVI(hwdip)->devi_drv_prop_ptr =
2754                     i_ddi_prop_list_dup(DEVI(dip)->devi_drv_prop_ptr, KM_SLEEP);
2755                 mutex_exit(&DEVI(hwdip)->devi_lock);
2756         }
2757         mutex_exit(&DEVI(dip)->devi_lock);
2758 }
2759 
2760 /*
2761  * Return the major number based on the compatible property. This interface
2762  * may be used in situations where we are trying to detect if a better driver
2763  * now exists for a device, so it must use the 'compatible' property.  If
2764  * a non-NULL formp is specified and the binding was based on compatible then
2765  * return the pointer to the form used in *formp.
2766  */
2767 major_t
2768 ddi_compatible_driver_major(dev_info_t *dip, char **formp)
2769 {
2770         struct dev_info *devi = DEVI(dip);
2771         void            *compat;
2772         size_t          len;
2773         char            *p = NULL;
2774         major_t         major = DDI_MAJOR_T_NONE;
2775 
2776         if (formp)
2777                 *formp = NULL;
2778 
2779         if (ddi_prop_exists(DDI_DEV_T_NONE, dip, DDI_PROP_DONTPASS,
2780             "ddi-assigned")) {
2781                 major = ddi_name_to_major("nulldriver");
2782                 return (major);
2783         }
2784 
2785         /*
2786          * Highest precedence binding is a path-oriented alias. Since this
2787          * requires a 'path', this type of binding occurs via more obtuse
2788          * 'rebind'. The need for a path-oriented alias 'rebind' is detected
2789          * after a successful DDI_CTLOPS_INITCHILD to another driver: this is
2790          * is the first point at which the unit-address (or instance) of the
2791          * last component of the path is available (even though the path is
2792          * bound to the wrong driver at this point).
2793          */
2794         if (devi->devi_flags & DEVI_REBIND) {
2795                 p = devi->devi_rebinding_name;
2796                 major = ddi_name_to_major(p);
2797                 if (driver_active(major)) {
2798                         if (formp)
2799                                 *formp = p;
2800                         return (major);
2801                 }
2802 
2803                 /*
2804                  * If for some reason devi_rebinding_name no longer resolves
2805                  * to a proper driver then clear DEVI_REBIND.
2806                  */
2807                 mutex_enter(&devi->devi_lock);
2808                 devi->devi_flags &= ~DEVI_REBIND;
2809                 mutex_exit(&devi->devi_lock);
2810         }
2811 
2812         /* look up compatible property */
2813         (void) lookup_compatible(dip, KM_SLEEP);
2814         compat = (void *)(devi->devi_compat_names);
2815         len = devi->devi_compat_length;
2816 
2817         /* find the highest precedence compatible form with a driver binding */
2818         while ((p = prom_decode_composite_string(compat, len, p)) != NULL) {
2819                 major = ddi_name_to_major(p);
2820                 if (driver_active(major)) {
2821                         if (formp)
2822                                 *formp = p;
2823                         return (major);
2824                 }
2825         }
2826 
2827         /*
2828          * none of the compatible forms have a driver binding, see if
2829          * the node name has a driver binding.
2830          */
2831         major = ddi_name_to_major(ddi_node_name(dip));
2832         if (driver_active(major))
2833                 return (major);
2834 
2835         /* no driver */
2836         return (DDI_MAJOR_T_NONE);
2837 }
2838 
2839 /*
2840  * Static help functions
2841  */
2842 
2843 /*
2844  * lookup the "compatible" property and cache it's contents in the
2845  * device node.
2846  */
2847 static int
2848 lookup_compatible(dev_info_t *dip, uint_t flag)
2849 {
2850         int rv;
2851         int prop_flags;
2852         uint_t ncompatstrs;
2853         char **compatstrpp;
2854         char *di_compat_strp;
2855         size_t di_compat_strlen;
2856 
2857         if (DEVI(dip)->devi_compat_names) {
2858                 return (DDI_SUCCESS);
2859         }
2860 
2861         prop_flags = DDI_PROP_TYPE_STRING | DDI_PROP_DONTPASS;
2862 
2863         if (flag & KM_NOSLEEP) {
2864                 prop_flags |= DDI_PROP_DONTSLEEP;
2865         }
2866 
2867         if (ndi_dev_is_prom_node(dip) == 0) {
2868                 prop_flags |= DDI_PROP_NOTPROM;
2869         }
2870 
2871         rv = ddi_prop_lookup_common(DDI_DEV_T_ANY, dip, prop_flags,
2872             "compatible", &compatstrpp, &ncompatstrs,
2873             ddi_prop_fm_decode_strings);
2874 
2875         if (rv == DDI_PROP_NOT_FOUND) {
2876                 return (DDI_SUCCESS);
2877         }
2878 
2879         if (rv != DDI_PROP_SUCCESS) {
2880                 return (DDI_FAILURE);
2881         }
2882 
2883         /*
2884          * encode the compatible property data in the dev_info node
2885          */
2886         rv = DDI_SUCCESS;
2887         if (ncompatstrs != 0) {
2888                 di_compat_strp = encode_composite_string(compatstrpp,
2889                     ncompatstrs, &di_compat_strlen, flag);
2890                 if (di_compat_strp != NULL) {
2891                         DEVI(dip)->devi_compat_names = di_compat_strp;
2892                         DEVI(dip)->devi_compat_length = di_compat_strlen;
2893                 } else {
2894                         rv = DDI_FAILURE;
2895                 }
2896         }
2897         ddi_prop_free(compatstrpp);
2898         return (rv);
2899 }
2900 
2901 /*
2902  * Create a composite string from a list of strings.
2903  *
2904  * A composite string consists of a single buffer containing one
2905  * or more NULL terminated strings.
2906  */
2907 static char *
2908 encode_composite_string(char **strings, uint_t nstrings, size_t *retsz,
2909     uint_t flag)
2910 {
2911         uint_t index;
2912         char  **strpp;
2913         uint_t slen;
2914         size_t cbuf_sz = 0;
2915         char *cbuf_p;
2916         char *cbuf_ip;
2917 
2918         if (strings == NULL || nstrings == 0 || retsz == NULL) {
2919                 return (NULL);
2920         }
2921 
2922         for (index = 0, strpp = strings; index < nstrings; index++)
2923                 cbuf_sz += strlen(*(strpp++)) + 1;
2924 
2925         if ((cbuf_p = kmem_alloc(cbuf_sz, flag)) == NULL) {
2926                 cmn_err(CE_NOTE,
2927                     "?failed to allocate device node compatstr");
2928                 return (NULL);
2929         }
2930 
2931         cbuf_ip = cbuf_p;
2932         for (index = 0, strpp = strings; index < nstrings; index++) {
2933                 slen = strlen(*strpp);
2934                 bcopy(*(strpp++), cbuf_ip, slen);
2935                 cbuf_ip += slen;
2936                 *(cbuf_ip++) = '\0';
2937         }
2938 
2939         *retsz = cbuf_sz;
2940         return (cbuf_p);
2941 }
2942 
2943 static void
2944 link_to_driver_list(dev_info_t *dip)
2945 {
2946         major_t major = DEVI(dip)->devi_major;
2947         struct devnames *dnp;
2948 
2949         ASSERT(major != DDI_MAJOR_T_NONE);
2950 
2951         /*
2952          * Remove from orphan list
2953          */
2954         if (ndi_dev_is_persistent_node(dip)) {
2955                 dnp = &orphanlist;
2956                 remove_from_dn_list(dnp, dip);
2957         }
2958 
2959         /*
2960          * Add to per driver list
2961          */
2962         dnp = &devnamesp[major];
2963         add_to_dn_list(dnp, dip);
2964 }
2965 
2966 static void
2967 unlink_from_driver_list(dev_info_t *dip)
2968 {
2969         major_t major = DEVI(dip)->devi_major;
2970         struct devnames *dnp;
2971 
2972         ASSERT(major != DDI_MAJOR_T_NONE);
2973 
2974         /*
2975          * Remove from per-driver list
2976          */
2977         dnp = &devnamesp[major];
2978         remove_from_dn_list(dnp, dip);
2979 
2980         /*
2981          * Add to orphan list
2982          */
2983         if (ndi_dev_is_persistent_node(dip)) {
2984                 dnp = &orphanlist;
2985                 add_to_dn_list(dnp, dip);
2986         }
2987 }
2988 
2989 /*
2990  * scan the per-driver list looking for dev_info "dip"
2991  */
2992 static dev_info_t *
2993 in_dn_list(struct devnames *dnp, dev_info_t *dip)
2994 {
2995         struct dev_info *idevi;
2996 
2997         if ((idevi = DEVI(dnp->dn_head)) == NULL)
2998                 return (NULL);
2999 
3000         while (idevi) {
3001                 if (idevi == DEVI(dip))
3002                         return (dip);
3003                 idevi = idevi->devi_next;
3004         }
3005         return (NULL);
3006 }
3007 
3008 /*
3009  * insert devinfo node 'dip' into the per-driver instance list
3010  * headed by 'dnp'
3011  *
3012  * Nodes on the per-driver list are ordered: HW - SID - PSEUDO.  The order is
3013  * required for merging of .conf file data to work properly.
3014  */
3015 static void
3016 add_to_ordered_dn_list(struct devnames *dnp, dev_info_t *dip)
3017 {
3018         dev_info_t **dipp;
3019 
3020         ASSERT(mutex_owned(&(dnp->dn_lock)));
3021 
3022         dipp = &dnp->dn_head;
3023         if (ndi_dev_is_prom_node(dip)) {
3024                 /*
3025                  * Find the first non-prom node or end of list
3026                  */
3027                 while (*dipp && (ndi_dev_is_prom_node(*dipp) != 0)) {
3028                         dipp = (dev_info_t **)&DEVI(*dipp)->devi_next;
3029                 }
3030         } else if (ndi_dev_is_persistent_node(dip)) {
3031                 /*
3032                  * Find the first non-persistent node
3033                  */
3034                 while (*dipp && (ndi_dev_is_persistent_node(*dipp) != 0)) {
3035                         dipp = (dev_info_t **)&DEVI(*dipp)->devi_next;
3036                 }
3037         } else {
3038                 /*
3039                  * Find the end of the list
3040                  */
3041                 while (*dipp) {
3042                         dipp = (dev_info_t **)&DEVI(*dipp)->devi_next;
3043                 }
3044         }
3045 
3046         DEVI(dip)->devi_next = DEVI(*dipp);
3047         *dipp = dip;
3048 }
3049 
3050 /*
3051  * add a list of device nodes to the device node list in the
3052  * devnames structure
3053  */
3054 static void
3055 add_to_dn_list(struct devnames *dnp, dev_info_t *dip)
3056 {
3057         /*
3058          * Look to see if node already exists
3059          */
3060         LOCK_DEV_OPS(&(dnp->dn_lock));
3061         if (in_dn_list(dnp, dip)) {
3062                 cmn_err(CE_NOTE, "add_to_dn_list: node %s already in list",
3063                     DEVI(dip)->devi_node_name);
3064         } else {
3065                 add_to_ordered_dn_list(dnp, dip);
3066         }
3067         UNLOCK_DEV_OPS(&(dnp->dn_lock));
3068 }
3069 
3070 static void
3071 remove_from_dn_list(struct devnames *dnp, dev_info_t *dip)
3072 {
3073         dev_info_t **plist;
3074 
3075         LOCK_DEV_OPS(&(dnp->dn_lock));
3076 
3077         plist = (dev_info_t **)&dnp->dn_head;
3078         while (*plist && (*plist != dip)) {
3079                 plist = (dev_info_t **)&DEVI(*plist)->devi_next;
3080         }
3081 
3082         if (*plist != NULL) {
3083                 ASSERT(*plist == dip);
3084                 *plist = (dev_info_t *)(DEVI(dip)->devi_next);
3085                 DEVI(dip)->devi_next = NULL;
3086         } else {
3087                 NDI_CONFIG_DEBUG((CE_NOTE,
3088                     "remove_from_dn_list: node %s not found in list",
3089                     DEVI(dip)->devi_node_name));
3090         }
3091 
3092         UNLOCK_DEV_OPS(&(dnp->dn_lock));
3093 }
3094 
3095 /*
3096  * Add and remove reference driver global property list
3097  */
3098 static void
3099 add_global_props(dev_info_t *dip)
3100 {
3101         struct devnames *dnp;
3102         ddi_prop_list_t *plist;
3103 
3104         ASSERT(DEVI(dip)->devi_global_prop_list == NULL);
3105         ASSERT(DEVI(dip)->devi_major != DDI_MAJOR_T_NONE);
3106 
3107         dnp = &devnamesp[DEVI(dip)->devi_major];
3108         LOCK_DEV_OPS(&dnp->dn_lock);
3109         plist = dnp->dn_global_prop_ptr;
3110         if (plist == NULL) {
3111                 UNLOCK_DEV_OPS(&dnp->dn_lock);
3112                 return;
3113         }
3114         i_ddi_prop_list_hold(plist, dnp);
3115         UNLOCK_DEV_OPS(&dnp->dn_lock);
3116 
3117         mutex_enter(&DEVI(dip)->devi_lock);
3118         DEVI(dip)->devi_global_prop_list = plist;
3119         mutex_exit(&DEVI(dip)->devi_lock);
3120 }
3121 
3122 static void
3123 remove_global_props(dev_info_t *dip)
3124 {
3125         ddi_prop_list_t *proplist;
3126 
3127         mutex_enter(&DEVI(dip)->devi_lock);
3128         proplist = DEVI(dip)->devi_global_prop_list;
3129         DEVI(dip)->devi_global_prop_list = NULL;
3130         mutex_exit(&DEVI(dip)->devi_lock);
3131 
3132         if (proplist) {
3133                 major_t major;
3134                 struct devnames *dnp;
3135 
3136                 major = ddi_driver_major(dip);
3137                 ASSERT(major != DDI_MAJOR_T_NONE);
3138                 dnp = &devnamesp[major];
3139                 LOCK_DEV_OPS(&dnp->dn_lock);
3140                 i_ddi_prop_list_rele(proplist, dnp);
3141                 UNLOCK_DEV_OPS(&dnp->dn_lock);
3142         }
3143 }
3144 
3145 #ifdef DEBUG
3146 /*
3147  * Set this variable to '0' to disable the optimization,
3148  * and to 2 to print debug message.
3149  */
3150 static int optimize_dtree = 1;
3151 
3152 static void
3153 debug_dtree(dev_info_t *devi, struct dev_info *adevi, char *service)
3154 {
3155         char *adeviname, *buf;
3156 
3157         /*
3158          * Don't print unless optimize dtree is set to 2+
3159          */
3160         if (optimize_dtree <= 1)
3161                 return;
3162 
3163         buf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
3164         adeviname = ddi_deviname((dev_info_t *)adevi, buf);
3165         if (*adeviname == '\0')
3166                 adeviname = "root";
3167 
3168         cmn_err(CE_CONT, "%s %s -> %s\n",
3169             ddi_deviname(devi, buf), service, adeviname);
3170 
3171         kmem_free(buf, MAXNAMELEN);
3172 }
3173 #else /* DEBUG */
3174 #define debug_dtree(a1, a2, a3)  /* nothing */
3175 #endif  /* DEBUG */
3176 
3177 static void
3178 ddi_optimize_dtree(dev_info_t *devi)
3179 {
3180         struct dev_info *pdevi;
3181         struct bus_ops *b;
3182 
3183         pdevi = DEVI(devi)->devi_parent;
3184         ASSERT(pdevi);
3185 
3186         /*
3187          * Set the unoptimized values
3188          */
3189         DEVI(devi)->devi_bus_map_fault = pdevi;
3190         DEVI(devi)->devi_bus_dma_allochdl = pdevi;
3191         DEVI(devi)->devi_bus_dma_freehdl = pdevi;
3192         DEVI(devi)->devi_bus_dma_bindhdl = pdevi;
3193         DEVI(devi)->devi_bus_dma_bindfunc =
3194             pdevi->devi_ops->devo_bus_ops->bus_dma_bindhdl;
3195         DEVI(devi)->devi_bus_dma_unbindhdl = pdevi;
3196         DEVI(devi)->devi_bus_dma_unbindfunc =
3197             pdevi->devi_ops->devo_bus_ops->bus_dma_unbindhdl;
3198         DEVI(devi)->devi_bus_dma_flush = pdevi;
3199         DEVI(devi)->devi_bus_dma_win = pdevi;
3200         DEVI(devi)->devi_bus_dma_ctl = pdevi;
3201         DEVI(devi)->devi_bus_ctl = pdevi;
3202 
3203 #ifdef DEBUG
3204         if (optimize_dtree == 0)
3205                 return;
3206 #endif /* DEBUG */
3207 
3208         b = pdevi->devi_ops->devo_bus_ops;
3209 
3210         if (i_ddi_map_fault == b->bus_map_fault) {
3211                 DEVI(devi)->devi_bus_map_fault = pdevi->devi_bus_map_fault;
3212                 debug_dtree(devi, DEVI(devi)->devi_bus_map_fault,
3213                     "bus_map_fault");
3214         }
3215 
3216         if (ddi_dma_allochdl == b->bus_dma_allochdl) {
3217                 DEVI(devi)->devi_bus_dma_allochdl =
3218                     pdevi->devi_bus_dma_allochdl;
3219                 debug_dtree(devi, DEVI(devi)->devi_bus_dma_allochdl,
3220                     "bus_dma_allochdl");
3221         }
3222 
3223         if (ddi_dma_freehdl == b->bus_dma_freehdl) {
3224                 DEVI(devi)->devi_bus_dma_freehdl = pdevi->devi_bus_dma_freehdl;
3225                 debug_dtree(devi, DEVI(devi)->devi_bus_dma_freehdl,
3226                     "bus_dma_freehdl");
3227         }
3228 
3229         if (ddi_dma_bindhdl == b->bus_dma_bindhdl) {
3230                 DEVI(devi)->devi_bus_dma_bindhdl = pdevi->devi_bus_dma_bindhdl;
3231                 DEVI(devi)->devi_bus_dma_bindfunc =
3232                     pdevi->devi_bus_dma_bindhdl->devi_ops->
3233                     devo_bus_ops->bus_dma_bindhdl;
3234                 debug_dtree(devi, DEVI(devi)->devi_bus_dma_bindhdl,
3235                     "bus_dma_bindhdl");
3236         }
3237 
3238         if (ddi_dma_unbindhdl == b->bus_dma_unbindhdl) {
3239                 DEVI(devi)->devi_bus_dma_unbindhdl =
3240                     pdevi->devi_bus_dma_unbindhdl;
3241                 DEVI(devi)->devi_bus_dma_unbindfunc =
3242                     pdevi->devi_bus_dma_unbindhdl->devi_ops->
3243                     devo_bus_ops->bus_dma_unbindhdl;
3244                 debug_dtree(devi, DEVI(devi)->devi_bus_dma_unbindhdl,
3245                     "bus_dma_unbindhdl");
3246         }
3247 
3248         if (ddi_dma_flush == b->bus_dma_flush) {
3249                 DEVI(devi)->devi_bus_dma_flush = pdevi->devi_bus_dma_flush;
3250                 debug_dtree(devi, DEVI(devi)->devi_bus_dma_flush,
3251                     "bus_dma_flush");
3252         }
3253 
3254         if (ddi_dma_win == b->bus_dma_win) {
3255                 DEVI(devi)->devi_bus_dma_win = pdevi->devi_bus_dma_win;
3256                 debug_dtree(devi, DEVI(devi)->devi_bus_dma_win,
3257                     "bus_dma_win");
3258         }
3259 
3260         if (ddi_dma_mctl == b->bus_dma_ctl) {
3261                 DEVI(devi)->devi_bus_dma_ctl = pdevi->devi_bus_dma_ctl;
3262                 debug_dtree(devi, DEVI(devi)->devi_bus_dma_ctl, "bus_dma_ctl");
3263         }
3264 
3265         if (ddi_ctlops == b->bus_ctl) {
3266                 DEVI(devi)->devi_bus_ctl = pdevi->devi_bus_ctl;
3267                 debug_dtree(devi, DEVI(devi)->devi_bus_ctl, "bus_ctl");
3268         }
3269 }
3270 
3271 #define MIN_DEVINFO_LOG_SIZE    max_ncpus
3272 #define MAX_DEVINFO_LOG_SIZE    max_ncpus * 10
3273 
3274 static void
3275 da_log_init()
3276 {
3277         devinfo_log_header_t *dh;
3278         int logsize = devinfo_log_size;
3279 
3280         if (logsize == 0)
3281                 logsize = MIN_DEVINFO_LOG_SIZE;
3282         else if (logsize > MAX_DEVINFO_LOG_SIZE)
3283                 logsize = MAX_DEVINFO_LOG_SIZE;
3284 
3285         dh = kmem_alloc(logsize * PAGESIZE, KM_SLEEP);
3286         mutex_init(&dh->dh_lock, NULL, MUTEX_DEFAULT, NULL);
3287         dh->dh_max = ((logsize * PAGESIZE) - sizeof (*dh)) /
3288             sizeof (devinfo_audit_t) + 1;
3289         dh->dh_curr = -1;
3290         dh->dh_hits = 0;
3291 
3292         devinfo_audit_log = dh;
3293 }
3294 
3295 /*
3296  * Log the stack trace in per-devinfo audit structure and also enter
3297  * it into a system wide log for recording the time history.
3298  */
3299 static void
3300 da_log_enter(dev_info_t *dip)
3301 {
3302         devinfo_audit_t *da_log, *da = DEVI(dip)->devi_audit;
3303         devinfo_log_header_t *dh = devinfo_audit_log;
3304 
3305         if (devinfo_audit_log == NULL)
3306                 return;
3307 
3308         ASSERT(da != NULL);
3309 
3310         da->da_devinfo = dip;
3311         da->da_timestamp = gethrtime();
3312         da->da_thread = curthread;
3313         da->da_node_state = DEVI(dip)->devi_node_state;
3314         da->da_device_state = DEVI(dip)->devi_state;
3315         da->da_depth = getpcstack(da->da_stack, DDI_STACK_DEPTH);
3316 
3317         /*
3318          * Copy into common log and note the location for tracing history
3319          */
3320         mutex_enter(&dh->dh_lock);
3321         dh->dh_hits++;
3322         dh->dh_curr++;
3323         if (dh->dh_curr >= dh->dh_max)
3324                 dh->dh_curr -= dh->dh_max;
3325         da_log = &dh->dh_entry[dh->dh_curr];
3326         mutex_exit(&dh->dh_lock);
3327 
3328         bcopy(da, da_log, sizeof (devinfo_audit_t));
3329         da->da_lastlog = da_log;
3330 }
3331 
3332 static void
3333 attach_drivers()
3334 {
3335         int i;
3336         for (i = 0; i < devcnt; i++) {
3337                 struct devnames *dnp = &devnamesp[i];
3338                 if ((dnp->dn_flags & DN_FORCE_ATTACH) &&
3339                     (ddi_hold_installed_driver((major_t)i) != NULL))
3340                         ddi_rele_driver((major_t)i);
3341         }
3342 }
3343 
3344 /*
3345  * Launch a thread to force attach drivers. This avoids penalty on boot time.
3346  */
3347 void
3348 i_ddi_forceattach_drivers()
3349 {
3350 
3351         /*
3352          * Attach IB VHCI driver before the force-attach thread attaches the
3353          * IB HCA driver. IB HCA driver will fail if IB Nexus has not yet
3354          * been attached.
3355          */
3356         (void) ddi_hold_installed_driver(ddi_name_to_major("ib"));
3357 
3358         (void) thread_create(NULL, 0, (void (*)())attach_drivers, NULL, 0, &p0,
3359             TS_RUN, minclsyspri);
3360 }
3361 
3362 /*
3363  * This is a private DDI interface for optimizing boot performance.
3364  * I/O subsystem initialization is considered complete when devfsadm
3365  * is executed.
3366  *
3367  * NOTE: The start of syseventd happens to be a convenient indicator
3368  *      of the completion of I/O initialization during boot.
3369  *      The implementation should be replaced by something more robust.
3370  */
3371 int
3372 i_ddi_io_initialized()
3373 {
3374         extern int sysevent_daemon_init;
3375         return (sysevent_daemon_init);
3376 }
3377 
3378 /*
3379  * May be used to determine system boot state
3380  * "Available" means the system is for the most part up
3381  * and initialized, with all system services either up or
3382  * capable of being started.  This state is set by devfsadm
3383  * during the boot process.  The /dev filesystem infers
3384  * from this when implicit reconfig can be performed,
3385  * ie, devfsadm can be invoked.  Please avoid making
3386  * further use of this unless it's really necessary.
3387  */
3388 int
3389 i_ddi_sysavail()
3390 {
3391         return (devname_state & DS_SYSAVAIL);
3392 }
3393 
3394 /*
3395  * May be used to determine if boot is a reconfigure boot.
3396  */
3397 int
3398 i_ddi_reconfig()
3399 {
3400         return (devname_state & DS_RECONFIG);
3401 }
3402 
3403 /*
3404  * Note system services are up, inform /dev.
3405  */
3406 void
3407 i_ddi_set_sysavail()
3408 {
3409         if ((devname_state & DS_SYSAVAIL) == 0) {
3410                 devname_state |= DS_SYSAVAIL;
3411                 sdev_devstate_change();
3412         }
3413 }
3414 
3415 /*
3416  * Note reconfiguration boot, inform /dev.
3417  */
3418 void
3419 i_ddi_set_reconfig()
3420 {
3421         if ((devname_state & DS_RECONFIG) == 0) {
3422                 devname_state |= DS_RECONFIG;
3423                 sdev_devstate_change();
3424         }
3425 }
3426 
3427 
3428 /*
3429  * device tree walking
3430  */
3431 
3432 struct walk_elem {
3433         struct walk_elem *next;
3434         dev_info_t *dip;
3435 };
3436 
3437 static void
3438 free_list(struct walk_elem *list)
3439 {
3440         while (list) {
3441                 struct walk_elem *next = list->next;
3442                 kmem_free(list, sizeof (*list));
3443                 list = next;
3444         }
3445 }
3446 
3447 static void
3448 append_node(struct walk_elem **list, dev_info_t *dip)
3449 {
3450         struct walk_elem *tail;
3451         struct walk_elem *elem = kmem_alloc(sizeof (*elem), KM_SLEEP);
3452 
3453         elem->next = NULL;
3454         elem->dip = dip;
3455 
3456         if (*list == NULL) {
3457                 *list = elem;
3458                 return;
3459         }
3460 
3461         tail = *list;
3462         while (tail->next)
3463                 tail = tail->next;
3464 
3465         tail->next = elem;
3466 }
3467 
3468 /*
3469  * The implementation of ddi_walk_devs().
3470  */
3471 static int
3472 walk_devs(dev_info_t *dip, int (*f)(dev_info_t *, void *), void *arg,
3473     int do_locking)
3474 {
3475         struct walk_elem *head = NULL;
3476 
3477         /*
3478          * Do it in two passes. First pass invoke callback on each
3479          * dip on the sibling list. Second pass invoke callback on
3480          * children of each dip.
3481          */
3482         while (dip) {
3483                 switch ((*f)(dip, arg)) {
3484                 case DDI_WALK_TERMINATE:
3485                         free_list(head);
3486                         return (DDI_WALK_TERMINATE);
3487 
3488                 case DDI_WALK_PRUNESIB:
3489                         /* ignore sibling by setting dip to NULL */
3490                         append_node(&head, dip);
3491                         dip = NULL;
3492                         break;
3493 
3494                 case DDI_WALK_PRUNECHILD:
3495                         /* don't worry about children */
3496                         dip = ddi_get_next_sibling(dip);
3497                         break;
3498 
3499                 case DDI_WALK_CONTINUE:
3500                 default:
3501                         append_node(&head, dip);
3502                         dip = ddi_get_next_sibling(dip);
3503                         break;
3504                 }
3505 
3506         }
3507 
3508         /* second pass */
3509         while (head) {
3510                 int circ;
3511                 struct walk_elem *next = head->next;
3512 
3513                 if (do_locking)
3514                         ndi_devi_enter(head->dip, &circ);
3515                 if (walk_devs(ddi_get_child(head->dip), f, arg, do_locking) ==
3516                     DDI_WALK_TERMINATE) {
3517                         if (do_locking)
3518                                 ndi_devi_exit(head->dip, circ);
3519                         free_list(head);
3520                         return (DDI_WALK_TERMINATE);
3521                 }
3522                 if (do_locking)
3523                         ndi_devi_exit(head->dip, circ);
3524                 kmem_free(head, sizeof (*head));
3525                 head = next;
3526         }
3527 
3528         return (DDI_WALK_CONTINUE);
3529 }
3530 
3531 /*
3532  * This general-purpose routine traverses the tree of dev_info nodes,
3533  * starting from the given node, and calls the given function for each
3534  * node that it finds with the current node and the pointer arg (which
3535  * can point to a structure of information that the function
3536  * needs) as arguments.
3537  *
3538  * It does the walk a layer at a time, not depth-first. The given function
3539  * must return one of the following values:
3540  *      DDI_WALK_CONTINUE
3541  *      DDI_WALK_PRUNESIB
3542  *      DDI_WALK_PRUNECHILD
3543  *      DDI_WALK_TERMINATE
3544  *
3545  * N.B. Since we walk the sibling list, the caller must ensure that
3546  *      the parent of dip is held against changes, unless the parent
3547  *      is rootnode.  ndi_devi_enter() on the parent is sufficient.
3548  *
3549  *      To avoid deadlock situations, caller must not attempt to
3550  *      configure/unconfigure/remove device node in (*f)(), nor should
3551  *      it attempt to recurse on other nodes in the system. Any
3552  *      ndi_devi_enter() done by (*f)() must occur 'at-or-below' the
3553  *      node entered prior to ddi_walk_devs(). Furthermore, if (*f)()
3554  *      does any multi-threading (in framework *or* in driver) then the
3555  *      ndi_devi_enter() calls done by dependent threads must be
3556  *      'strictly-below'.
3557  *
3558  *      This is not callable from device autoconfiguration routines.
3559  *      They include, but not limited to, _init(9e), _fini(9e), probe(9e),
3560  *      attach(9e), and detach(9e).
3561  */
3562 
3563 void
3564 ddi_walk_devs(dev_info_t *dip, int (*f)(dev_info_t *, void *), void *arg)
3565 {
3566 
3567         ASSERT(dip == NULL || ddi_get_parent(dip) == NULL ||
3568             DEVI_BUSY_OWNED(ddi_get_parent(dip)));
3569 
3570         (void) walk_devs(dip, f, arg, 1);
3571 }
3572 
3573 /*
3574  * This is a general-purpose routine traverses the per-driver list
3575  * and calls the given function for each node. must return one of
3576  * the following values:
3577  *      DDI_WALK_CONTINUE
3578  *      DDI_WALK_TERMINATE
3579  *
3580  * N.B. The same restrictions from ddi_walk_devs() apply.
3581  */
3582 
3583 void
3584 e_ddi_walk_driver(char *drv, int (*f)(dev_info_t *, void *), void *arg)
3585 {
3586         major_t major;
3587         struct devnames *dnp;
3588         dev_info_t *dip;
3589 
3590         major = ddi_name_to_major(drv);
3591         if (major == DDI_MAJOR_T_NONE)
3592                 return;
3593 
3594         dnp = &devnamesp[major];
3595         LOCK_DEV_OPS(&dnp->dn_lock);
3596         dip = dnp->dn_head;
3597         while (dip) {
3598                 ndi_hold_devi(dip);
3599                 UNLOCK_DEV_OPS(&dnp->dn_lock);
3600                 if ((*f)(dip, arg) == DDI_WALK_TERMINATE) {
3601                         ndi_rele_devi(dip);
3602                         return;
3603                 }
3604                 LOCK_DEV_OPS(&dnp->dn_lock);
3605                 ndi_rele_devi(dip);
3606                 dip = ddi_get_next(dip);
3607         }
3608         UNLOCK_DEV_OPS(&dnp->dn_lock);
3609 }
3610 
3611 /*
3612  * argument to i_find_devi, a devinfo node search callback function.
3613  */
3614 struct match_info {
3615         dev_info_t      *dip;           /* result */
3616         char            *nodename;      /* if non-null, nodename must match */
3617         int             instance;       /* if != -1, instance must match */
3618         int             attached;       /* if != 0, i_ddi_devi_attached() */
3619 };
3620 
3621 static int
3622 i_find_devi(dev_info_t *dip, void *arg)
3623 {
3624         struct match_info *info = (struct match_info *)arg;
3625 
3626         if (((info->nodename == NULL) ||
3627             (strcmp(ddi_node_name(dip), info->nodename) == 0)) &&
3628             ((info->instance == -1) ||
3629             (ddi_get_instance(dip) == info->instance)) &&
3630             ((info->attached == 0) || i_ddi_devi_attached(dip))) {
3631                 info->dip = dip;
3632                 ndi_hold_devi(dip);
3633                 return (DDI_WALK_TERMINATE);
3634         }
3635 
3636         return (DDI_WALK_CONTINUE);
3637 }
3638 
3639 /*
3640  * Find dip with a known node name and instance and return with it held
3641  */
3642 dev_info_t *
3643 ddi_find_devinfo(char *nodename, int instance, int attached)
3644 {
3645         struct match_info       info;
3646 
3647         info.nodename = nodename;
3648         info.instance = instance;
3649         info.attached = attached;
3650         info.dip = NULL;
3651 
3652         ddi_walk_devs(ddi_root_node(), i_find_devi, &info);
3653         return (info.dip);
3654 }
3655 
3656 extern ib_boot_prop_t *iscsiboot_prop;
3657 static void
3658 i_ddi_parse_iscsi_name(char *name, char **nodename, char **addrname,
3659     char **minorname)
3660 {
3661         char *cp, *colon;
3662         static char nulladdrname[] = "";
3663 
3664         /* default values */
3665         if (nodename)
3666                 *nodename = name;
3667         if (addrname)
3668                 *addrname = nulladdrname;
3669         if (minorname)
3670                 *minorname = NULL;
3671 
3672         cp = colon = name;
3673         while (*cp != '\0') {
3674                 if (addrname && *cp == '@') {
3675                         *addrname = cp + 1;
3676                         *cp = '\0';
3677                 } else if (minorname && *cp == ':') {
3678                         *minorname = cp + 1;
3679                         colon = cp;
3680                 }
3681                 ++cp;
3682         }
3683         if (colon != name) {
3684                 *colon = '\0';
3685         }
3686 }
3687 
3688 /*
3689  * Parse for name, addr, and minor names. Some args may be NULL.
3690  */
3691 void
3692 i_ddi_parse_name(char *name, char **nodename, char **addrname, char **minorname)
3693 {
3694         char *cp;
3695         static char nulladdrname[] = "";
3696 
3697         /* default values */
3698         if (nodename)
3699                 *nodename = name;
3700         if (addrname)
3701                 *addrname = nulladdrname;
3702         if (minorname)
3703                 *minorname = NULL;
3704 
3705         cp = name;
3706         while (*cp != '\0') {
3707                 if (addrname && *cp == '@') {
3708                         *addrname = cp + 1;
3709                         *cp = '\0';
3710                 } else if (minorname && *cp == ':') {
3711                         *minorname = cp + 1;
3712                         *cp = '\0';
3713                 }
3714                 ++cp;
3715         }
3716 }
3717 
3718 static char *
3719 child_path_to_driver(dev_info_t *parent, char *child_name, char *unit_address)
3720 {
3721         char *p, *drvname = NULL;
3722         major_t maj;
3723 
3724         /*
3725          * Construct the pathname and ask the implementation
3726          * if it can do a driver = f(pathname) for us, if not
3727          * we'll just default to using the node-name that
3728          * was given to us.  We want to do this first to
3729          * allow the platform to use 'generic' names for
3730          * legacy device drivers.
3731          */
3732         p = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
3733         (void) ddi_pathname(parent, p);
3734         (void) strcat(p, "/");
3735         (void) strcat(p, child_name);
3736         if (unit_address && *unit_address) {
3737                 (void) strcat(p, "@");
3738                 (void) strcat(p, unit_address);
3739         }
3740 
3741         /*
3742          * Get the binding. If there is none, return the child_name
3743          * and let the caller deal with it.
3744          */
3745         maj = path_to_major(p);
3746 
3747         kmem_free(p, MAXPATHLEN);
3748 
3749         if (maj != DDI_MAJOR_T_NONE)
3750                 drvname = ddi_major_to_name(maj);
3751         if (drvname == NULL)
3752                 drvname = child_name;
3753 
3754         return (drvname);
3755 }
3756 
3757 
3758 #define PCI_EX_CLASS    "pciexclass"
3759 #define PCI_EX          "pciex"
3760 #define PCI_CLASS       "pciclass"
3761 #define PCI             "pci"
3762 
3763 int
3764 ddi_is_pci_dip(dev_info_t *dip)
3765 {
3766         char    *prop = NULL;
3767 
3768         if (ddi_prop_lookup_string(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
3769             "compatible", &prop) == DDI_PROP_SUCCESS) {
3770                 ASSERT(prop);
3771                 if (strncmp(prop, PCI_EX_CLASS, sizeof (PCI_EX_CLASS) - 1)
3772                     == 0 ||
3773                     strncmp(prop, PCI_EX, sizeof (PCI_EX)- 1)
3774                     == 0 ||
3775                     strncmp(prop, PCI_CLASS, sizeof (PCI_CLASS) - 1)
3776                     == 0 ||
3777                     strncmp(prop, PCI, sizeof (PCI) - 1)
3778                     == 0) {
3779                         ddi_prop_free(prop);
3780                         return (1);
3781                 }
3782         }
3783 
3784         if (prop != NULL) {
3785                 ddi_prop_free(prop);
3786         }
3787 
3788         return (0);
3789 }
3790 
3791 /*
3792  * Given the pathname of a device, fill in the dev_info_t value and/or the
3793  * dev_t value and/or the spectype, depending on which parameters are non-NULL.
3794  * If there is an error, this function returns -1.
3795  *
3796  * NOTE: If this function returns the dev_info_t structure, then it
3797  * does so with a hold on the devi. Caller should ensure that they get
3798  * decremented via ddi_release_devi() or ndi_rele_devi();
3799  *
3800  * This function can be invoked in the boot case for a pathname without
3801  * device argument (:xxxx), traditionally treated as a minor name.
3802  * In this case, we do the following
3803  * (1) search the minor node of type DDM_DEFAULT.
3804  * (2) if no DDM_DEFAULT minor exists, then the first non-alias minor is chosen.
3805  * (3) if neither exists, a dev_t is faked with minor number = instance.
3806  * As of S9 FCS, no instance of #1 exists. #2 is used by several platforms
3807  * to default the boot partition to :a possibly by other OBP definitions.
3808  * #3 is used for booting off network interfaces, most SPARC network
3809  * drivers support Style-2 only, so only DDM_ALIAS minor exists.
3810  *
3811  * It is possible for OBP to present device args at the end of the path as
3812  * well as in the middle. For example, with IB the following strings are
3813  * valid boot paths.
3814  *      a /pci@8,700000/ib@1,2:port=1,pkey=ff,dhcp,...
3815  *      b /pci@8,700000/ib@1,1:port=1/ioc@xxxxxx,yyyyyyy:dhcp
3816  * Case (a), we first look for minor node "port=1,pkey...".
3817  * Failing that, we will pass "port=1,pkey..." to the bus_config
3818  * entry point of ib (HCA) driver.
3819  * Case (b), configure ib@1,1 as usual. Then invoke ib's bus_config
3820  * with argument "ioc@xxxxxxx,yyyyyyy:port=1". After configuring
3821  * the ioc, look for minor node dhcp. If not found, pass ":dhcp"
3822  * to ioc's bus_config entry point.
3823  */
3824 int
3825 resolve_pathname(char *pathname, dev_info_t **dipp, dev_t *devtp,
3826     int *spectypep)
3827 {
3828         int                     error;
3829         dev_info_t              *parent, *child;
3830         struct pathname         pn;
3831         char                    *component, *config_name;
3832         char                    *minorname = NULL;
3833         char                    *prev_minor = NULL;
3834         dev_t                   devt = NODEV;
3835         int                     spectype;
3836         struct ddi_minor_data   *dmn;
3837         int                     circ;
3838 
3839         if (*pathname != '/')
3840                 return (EINVAL);
3841         parent = ddi_root_node();       /* Begin at the top of the tree */
3842 
3843         if (error = pn_get(pathname, UIO_SYSSPACE, &pn))
3844                 return (error);
3845         pn_skipslash(&pn);
3846 
3847         ASSERT(i_ddi_devi_attached(parent));
3848         ndi_hold_devi(parent);
3849 
3850         component = kmem_alloc(MAXNAMELEN, KM_SLEEP);
3851         config_name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
3852 
3853         while (pn_pathleft(&pn)) {
3854                 /* remember prev minor (:xxx) in the middle of path */
3855                 if (minorname)
3856                         prev_minor = i_ddi_strdup(minorname, KM_SLEEP);
3857 
3858                 /* Get component and chop off minorname */
3859                 (void) pn_getcomponent(&pn, component);
3860                 if ((iscsiboot_prop != NULL) &&
3861                     (strcmp((DEVI(parent)->devi_node_name), "iscsi") == 0)) {
3862                         i_ddi_parse_iscsi_name(component, NULL, NULL,
3863                             &minorname);
3864                 } else {
3865                         i_ddi_parse_name(component, NULL, NULL, &minorname);
3866                 }
3867                 if (prev_minor == NULL) {
3868                         (void) snprintf(config_name, MAXNAMELEN, "%s",
3869                             component);
3870                 } else {
3871                         (void) snprintf(config_name, MAXNAMELEN, "%s:%s",
3872                             component, prev_minor);
3873                         kmem_free(prev_minor, strlen(prev_minor) + 1);
3874                         prev_minor = NULL;
3875                 }
3876 
3877                 /*
3878                  * Find and configure the child
3879                  */
3880                 if (ndi_devi_config_one(parent, config_name, &child,
3881                     NDI_PROMNAME | NDI_NO_EVENT) != NDI_SUCCESS) {
3882                         ndi_rele_devi(parent);
3883                         pn_free(&pn);
3884                         kmem_free(component, MAXNAMELEN);
3885                         kmem_free(config_name, MAXNAMELEN);
3886                         return (-1);
3887                 }
3888 
3889                 ASSERT(i_ddi_devi_attached(child));
3890                 ndi_rele_devi(parent);
3891                 parent = child;
3892                 pn_skipslash(&pn);
3893         }
3894 
3895         /*
3896          * First look for a minor node matching minorname.
3897          * Failing that, try to pass minorname to bus_config().
3898          */
3899         if (minorname && i_ddi_minorname_to_devtspectype(parent,
3900             minorname, &devt, &spectype) == DDI_FAILURE) {
3901                 (void) snprintf(config_name, MAXNAMELEN, "%s", minorname);
3902                 if (ndi_devi_config_obp_args(parent,
3903                     config_name, &child, 0) != NDI_SUCCESS) {
3904                         ndi_rele_devi(parent);
3905                         pn_free(&pn);
3906                         kmem_free(component, MAXNAMELEN);
3907                         kmem_free(config_name, MAXNAMELEN);
3908                         NDI_CONFIG_DEBUG((CE_NOTE,
3909                             "%s: minor node not found\n", pathname));
3910                         return (-1);
3911                 }
3912                 minorname = NULL;       /* look for default minor */
3913                 ASSERT(i_ddi_devi_attached(child));
3914                 ndi_rele_devi(parent);
3915                 parent = child;
3916         }
3917 
3918         if (devtp || spectypep) {
3919                 if (minorname == NULL) {
3920                         /*
3921                          * Search for a default entry with an active
3922                          * ndi_devi_enter to protect the devi_minor list.
3923                          */
3924                         ndi_devi_enter(parent, &circ);
3925                         for (dmn = DEVI(parent)->devi_minor; dmn;
3926                             dmn = dmn->next) {
3927                                 if (dmn->type == DDM_DEFAULT) {
3928                                         devt = dmn->ddm_dev;
3929                                         spectype = dmn->ddm_spec_type;
3930                                         break;
3931                                 }
3932                         }
3933 
3934                         if (devt == NODEV) {
3935                                 /*
3936                                  * No default minor node, try the first one;
3937                                  * else, assume 1-1 instance-minor mapping
3938                                  */
3939                                 dmn = DEVI(parent)->devi_minor;
3940                                 if (dmn && ((dmn->type == DDM_MINOR) ||
3941                                     (dmn->type == DDM_INTERNAL_PATH))) {
3942                                         devt = dmn->ddm_dev;
3943                                         spectype = dmn->ddm_spec_type;
3944                                 } else {
3945                                         devt = makedevice(
3946                                             DEVI(parent)->devi_major,
3947                                             ddi_get_instance(parent));
3948                                         spectype = S_IFCHR;
3949                                 }
3950                         }
3951                         ndi_devi_exit(parent, circ);
3952                 }
3953                 if (devtp)
3954                         *devtp = devt;
3955                 if (spectypep)
3956                         *spectypep = spectype;
3957         }
3958 
3959         pn_free(&pn);
3960         kmem_free(component, MAXNAMELEN);
3961         kmem_free(config_name, MAXNAMELEN);
3962 
3963         /*
3964          * If there is no error, return the appropriate parameters
3965          */
3966         if (dipp != NULL)
3967                 *dipp = parent;
3968         else {
3969                 /*
3970                  * We should really keep the ref count to keep the node from
3971                  * detaching but ddi_pathname_to_dev_t() specifies a NULL dipp,
3972                  * so we have no way of passing back the held dip.  Not holding
3973                  * the dip allows detaches to occur - which can cause problems
3974                  * for subsystems which call ddi_pathname_to_dev_t (console).
3975                  *
3976                  * Instead of holding the dip, we place a ddi-no-autodetach
3977                  * property on the node to prevent auto detaching.
3978                  *
3979                  * The right fix is to remove ddi_pathname_to_dev_t and replace
3980                  * it, and all references, with a call that specifies a dipp.
3981                  * In addition, the callers of this new interfaces would then
3982                  * need to call ndi_rele_devi when the reference is complete.
3983                  *
3984                  */
3985                 (void) ddi_prop_update_int(DDI_DEV_T_NONE, parent,
3986                     DDI_NO_AUTODETACH, 1);
3987                 ndi_rele_devi(parent);
3988         }
3989 
3990         return (0);
3991 }
3992 
3993 /*
3994  * Given the pathname of a device, return the dev_t of the corresponding
3995  * device.  Returns NODEV on failure.
3996  *
3997  * Note that this call sets the DDI_NO_AUTODETACH property on the devinfo node.
3998  */
3999 dev_t
4000 ddi_pathname_to_dev_t(char *pathname)
4001 {
4002         dev_t devt;
4003         int error;
4004 
4005         error = resolve_pathname(pathname, NULL, &devt, NULL);
4006 
4007         return (error ? NODEV : devt);
4008 }
4009 
4010 /*
4011  * Translate a prom pathname to kernel devfs pathname.
4012  * Caller is assumed to allocate devfspath memory of
4013  * size at least MAXPATHLEN
4014  *
4015  * The prom pathname may not include minor name, but
4016  * devfs pathname has a minor name portion.
4017  */
4018 int
4019 i_ddi_prompath_to_devfspath(char *prompath, char *devfspath)
4020 {
4021         dev_t           devt = (dev_t)NODEV;
4022         dev_info_t      *dip = NULL;
4023         char            *minor_name = NULL;
4024         int             spectype;
4025         int             error;
4026         int             circ;
4027 
4028         error = resolve_pathname(prompath, &dip, &devt, &spectype);
4029         if (error)
4030                 return (DDI_FAILURE);
4031         ASSERT(dip && devt != NODEV);
4032 
4033         /*
4034          * Get in-kernel devfs pathname
4035          */
4036         (void) ddi_pathname(dip, devfspath);
4037 
4038         ndi_devi_enter(dip, &circ);
4039         minor_name = i_ddi_devtspectype_to_minorname(dip, devt, spectype);
4040         if (minor_name) {
4041                 (void) strcat(devfspath, ":");
4042                 (void) strcat(devfspath, minor_name);
4043         } else {
4044                 /*
4045                  * If minor_name is NULL, we have an alias minor node.
4046                  * So manufacture a path to the corresponding clone minor.
4047                  */
4048                 (void) snprintf(devfspath, MAXPATHLEN, "%s:%s",
4049                     CLONE_PATH, ddi_driver_name(dip));
4050         }
4051         ndi_devi_exit(dip, circ);
4052 
4053         /* release hold from resolve_pathname() */
4054         ndi_rele_devi(dip);
4055         return (0);
4056 }
4057 
4058 /*
4059  * This function is intended to identify drivers that must quiesce for fast
4060  * reboot to succeed.  It does not claim to have more knowledge about the device
4061  * than its driver.  If a driver has implemented quiesce(), it will be invoked;
4062  * if a so identified driver does not manage any device that needs to be
4063  * quiesced, it must explicitly set its devo_quiesce dev_op to
4064  * ddi_quiesce_not_needed.
4065  */
4066 static int skip_pseudo = 1;     /* Skip pseudo devices */
4067 static int skip_non_hw = 1;     /* Skip devices with no hardware property */
4068 static int
4069 should_implement_quiesce(dev_info_t *dip)
4070 {
4071         struct dev_info *devi = DEVI(dip);
4072         dev_info_t *pdip;
4073 
4074         /*
4075          * If dip is pseudo and skip_pseudo is set, driver doesn't have to
4076          * implement quiesce().
4077          */
4078         if (skip_pseudo &&
4079             strncmp(ddi_binding_name(dip), "pseudo", sizeof ("pseudo")) == 0)
4080                 return (0);
4081 
4082         /*
4083          * If parent dip is pseudo and skip_pseudo is set, driver doesn't have
4084          * to implement quiesce().
4085          */
4086         if (skip_pseudo && (pdip = ddi_get_parent(dip)) != NULL &&
4087             strncmp(ddi_binding_name(pdip), "pseudo", sizeof ("pseudo")) == 0)
4088                 return (0);
4089 
4090         /*
4091          * If not attached, driver doesn't have to implement quiesce().
4092          */
4093         if (!i_ddi_devi_attached(dip))
4094                 return (0);
4095 
4096         /*
4097          * If dip has no hardware property and skip_non_hw is set,
4098          * driver doesn't have to implement quiesce().
4099          */
4100         if (skip_non_hw && devi->devi_hw_prop_ptr == NULL)
4101                 return (0);
4102 
4103         return (1);
4104 }
4105 
4106 static int
4107 driver_has_quiesce(struct dev_ops *ops)
4108 {
4109         if ((ops->devo_rev >= 4) && (ops->devo_quiesce != nodev) &&
4110             (ops->devo_quiesce != NULL) && (ops->devo_quiesce != nulldev) &&
4111             (ops->devo_quiesce != ddi_quiesce_not_supported))
4112                 return (1);
4113         else
4114                 return (0);
4115 }
4116 
4117 /*
4118  * Check to see if a driver has implemented the quiesce() DDI function.
4119  */
4120 int
4121 check_driver_quiesce(dev_info_t *dip, void *arg)
4122 {
4123         struct dev_ops *ops;
4124 
4125         if (!should_implement_quiesce(dip))
4126                 return (DDI_WALK_CONTINUE);
4127 
4128         if ((ops = ddi_get_driver(dip)) == NULL)
4129                 return (DDI_WALK_CONTINUE);
4130 
4131         if (driver_has_quiesce(ops)) {
4132                 if ((quiesce_debug & 0x2) == 0x2) {
4133                         if (ops->devo_quiesce == ddi_quiesce_not_needed)
4134                                 cmn_err(CE_CONT, "%s does not need to be "
4135                                     "quiesced", ddi_driver_name(dip));
4136                         else
4137                                 cmn_err(CE_CONT, "%s has quiesce routine",
4138                                     ddi_driver_name(dip));
4139                 }
4140         } else {
4141                 if (arg != NULL)
4142                         *((int *)arg) = -1;
4143                 cmn_err(CE_WARN, "%s has no quiesce()", ddi_driver_name(dip));
4144         }
4145 
4146         return (DDI_WALK_CONTINUE);
4147 }
4148 
4149 /*
4150  * Quiesce device.
4151  */
4152 static void
4153 quiesce_one_device(dev_info_t *dip, void *arg)
4154 {
4155         struct dev_ops *ops;
4156         int should_quiesce = 0;
4157 
4158         /*
4159          * If the device is not attached it doesn't need to be quiesced.
4160          */
4161         if (!i_ddi_devi_attached(dip))
4162                 return;
4163 
4164         if ((ops = ddi_get_driver(dip)) == NULL)
4165                 return;
4166 
4167         should_quiesce = should_implement_quiesce(dip);
4168 
4169         /*
4170          * If there's an implementation of quiesce(), always call it even if
4171          * some of the drivers don't have quiesce() or quiesce() have failed
4172          * so we can do force fast reboot.  The implementation of quiesce()
4173          * should not negatively affect a regular reboot.
4174          */
4175         if (driver_has_quiesce(ops)) {
4176                 int rc = DDI_SUCCESS;
4177 
4178                 if (ops->devo_quiesce == ddi_quiesce_not_needed)
4179                         return;
4180 
4181                 rc = devi_quiesce(dip);
4182 
4183                 if (rc != DDI_SUCCESS && should_quiesce) {
4184 #ifdef DEBUG
4185                         cmn_err(CE_WARN, "quiesce() failed for %s%d",
4186                             ddi_driver_name(dip), ddi_get_instance(dip));
4187 #endif /* DEBUG */
4188                         if (arg != NULL)
4189                                 *((int *)arg) = -1;
4190                 }
4191         } else if (should_quiesce && arg != NULL) {
4192                 *((int *)arg) = -1;
4193         }
4194 }
4195 
4196 /*
4197  * Traverse the dev info tree in a breadth-first manner so that we quiesce
4198  * children first.  All subtrees under the parent of dip will be quiesced.
4199  */
4200 void
4201 quiesce_devices(dev_info_t *dip, void *arg)
4202 {
4203         /*
4204          * if we're reached here, the device tree better not be changing.
4205          * so either devinfo_freeze better be set or we better be panicing.
4206          */
4207         ASSERT(devinfo_freeze || panicstr);
4208 
4209         for (; dip != NULL; dip = ddi_get_next_sibling(dip)) {
4210                 quiesce_devices(ddi_get_child(dip), arg);
4211 
4212                 quiesce_one_device(dip, arg);
4213         }
4214 }
4215 
4216 /*
4217  * Reset all the pure leaf drivers on the system at halt time
4218  */
4219 static int
4220 reset_leaf_device(dev_info_t *dip, void *arg)
4221 {
4222         _NOTE(ARGUNUSED(arg))
4223         struct dev_ops *ops;
4224 
4225         /* if the device doesn't need to be reset then there's nothing to do */
4226         if (!DEVI_NEED_RESET(dip))
4227                 return (DDI_WALK_CONTINUE);
4228 
4229         /*
4230          * if the device isn't a char/block device or doesn't have a
4231          * reset entry point then there's nothing to do.
4232          */
4233         ops = ddi_get_driver(dip);
4234         if ((ops == NULL) || (ops->devo_cb_ops == NULL) ||
4235             (ops->devo_reset == nodev) || (ops->devo_reset == nulldev) ||
4236             (ops->devo_reset == NULL))
4237                 return (DDI_WALK_CONTINUE);
4238 
4239         if (DEVI_IS_ATTACHING(dip) || DEVI_IS_DETACHING(dip)) {
4240                 static char path[MAXPATHLEN];
4241 
4242                 /*
4243                  * bad news, this device has blocked in it's attach or
4244                  * detach routine, which means it not safe to call it's
4245                  * devo_reset() entry point.
4246                  */
4247                 cmn_err(CE_WARN, "unable to reset device: %s",
4248                     ddi_pathname(dip, path));
4249                 return (DDI_WALK_CONTINUE);
4250         }
4251 
4252         NDI_CONFIG_DEBUG((CE_NOTE, "resetting %s%d\n",
4253             ddi_driver_name(dip), ddi_get_instance(dip)));
4254 
4255         (void) devi_reset(dip, DDI_RESET_FORCE);
4256         return (DDI_WALK_CONTINUE);
4257 }
4258 
4259 void
4260 reset_leaves(void)
4261 {
4262         /*
4263          * if we're reached here, the device tree better not be changing.
4264          * so either devinfo_freeze better be set or we better be panicing.
4265          */
4266         ASSERT(devinfo_freeze || panicstr);
4267 
4268         (void) walk_devs(top_devinfo, reset_leaf_device, NULL, 0);
4269 }
4270 
4271 
4272 /*
4273  * devtree_freeze() must be called before quiesce_devices() and reset_leaves()
4274  * during a normal system shutdown.  It attempts to ensure that there are no
4275  * outstanding attach or detach operations in progress when quiesce_devices() or
4276  * reset_leaves()is invoked.  It must be called before the system becomes
4277  * single-threaded because device attach and detach are multi-threaded
4278  * operations.  (note that during system shutdown the system doesn't actually
4279  * become single-thread since other threads still exist, but the shutdown thread
4280  * will disable preemption for itself, raise it's pil, and stop all the other
4281  * cpus in the system there by effectively making the system single-threaded.)
4282  */
4283 void
4284 devtree_freeze(void)
4285 {
4286         int delayed = 0;
4287 
4288         /* if we're panicing then the device tree isn't going to be changing */
4289         if (panicstr)
4290                 return;
4291 
4292         /* stop all dev_info state changes in the device tree */
4293         devinfo_freeze = gethrtime();
4294 
4295         /*
4296          * if we're not panicing and there are on-going attach or detach
4297          * operations, wait for up to 3 seconds for them to finish.  This
4298          * is a randomly chosen interval but this should be ok because:
4299          * - 3 seconds is very small relative to the deadman timer.
4300          * - normal attach and detach operations should be very quick.
4301          * - attach and detach operations are fairly rare.
4302          */
4303         while (!panicstr && atomic_add_long_nv(&devinfo_attach_detach, 0) &&
4304             (delayed < 3)) {
4305                 delayed += 1;
4306 
4307                 /* do a sleeping wait for one second */
4308                 ASSERT(!servicing_interrupt());
4309                 delay(drv_usectohz(MICROSEC));
4310         }
4311 }
4312 
4313 static int
4314 bind_dip(dev_info_t *dip, void *arg)
4315 {
4316         _NOTE(ARGUNUSED(arg))
4317         char    *path;
4318         major_t major, pmajor;
4319 
4320         /*
4321          * If the node is currently bound to the wrong driver, try to unbind
4322          * so that we can rebind to the correct driver.
4323          */
4324         if (i_ddi_node_state(dip) >= DS_BOUND) {
4325                 major = ddi_compatible_driver_major(dip, NULL);
4326                 if ((DEVI(dip)->devi_major == major) &&
4327                     (i_ddi_node_state(dip) >= DS_INITIALIZED)) {
4328                         /*
4329                          * Check for a path-oriented driver alias that
4330                          * takes precedence over current driver binding.
4331                          */
4332                         path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
4333                         (void) ddi_pathname(dip, path);
4334                         pmajor = ddi_name_to_major(path);
4335                         if (driver_active(pmajor))
4336                                 major = pmajor;
4337                         kmem_free(path, MAXPATHLEN);
4338                 }
4339 
4340                 /* attempt unbind if current driver is incorrect */
4341                 if (driver_active(major) &&
4342                     (major != DEVI(dip)->devi_major))
4343                         (void) ndi_devi_unbind_driver(dip);
4344         }
4345 
4346         /* If unbound, try to bind to a driver */
4347         if (i_ddi_node_state(dip) < DS_BOUND)
4348                 (void) ndi_devi_bind_driver(dip, 0);
4349 
4350         return (DDI_WALK_CONTINUE);
4351 }
4352 
4353 void
4354 i_ddi_bind_devs(void)
4355 {
4356         /* flush devfs so that ndi_devi_unbind_driver will work when possible */
4357         (void) devfs_clean(top_devinfo, NULL, 0);
4358 
4359         ddi_walk_devs(top_devinfo, bind_dip, (void *)NULL);
4360 }
4361 
4362 /* callback data for unbind_children_by_alias() */
4363 typedef struct unbind_data {
4364         major_t drv_major;
4365         char    *drv_alias;
4366         int     ndevs_bound;
4367         int     unbind_errors;
4368 } unbind_data_t;
4369 
4370 /*
4371  * A utility function provided for testing and support convenience
4372  * Called for each device during an upgrade_drv -d bound to the alias
4373  * that cannot be unbound due to device in use.
4374  */
4375 static void
4376 unbind_alias_dev_in_use(dev_info_t *dip, char *alias)
4377 {
4378         if (moddebug & MODDEBUG_BINDING) {
4379                 cmn_err(CE_CONT, "%s%d: state %d: bound to %s\n",
4380                     ddi_driver_name(dip), ddi_get_instance(dip),
4381                     i_ddi_node_state(dip), alias);
4382         }
4383 }
4384 
4385 /*
4386  * walkdevs callback for unbind devices bound to specific driver
4387  * and alias.  Invoked within the context of update_drv -d <alias>.
4388  */
4389 static int
4390 unbind_children_by_alias(dev_info_t *dip, void *arg)
4391 {
4392         int             circ;
4393         dev_info_t      *cdip;
4394         dev_info_t      *next;
4395         unbind_data_t   *ub = (unbind_data_t *)(uintptr_t)arg;
4396         int             rv;
4397 
4398         /*
4399          * We are called from update_drv to try to unbind a specific
4400          * set of aliases for a driver.  Unbind what persistent nodes
4401          * we can, and return the number of nodes which cannot be unbound.
4402          * If not all nodes can be unbound, update_drv leaves the
4403          * state of the driver binding files unchanged, except in
4404          * the case of -f.
4405          */
4406         ndi_devi_enter(dip, &circ);
4407         for (cdip = ddi_get_child(dip); cdip; cdip = next) {
4408                 next = ddi_get_next_sibling(cdip);
4409                 if ((ddi_driver_major(cdip) != ub->drv_major) ||
4410                     (strcmp(DEVI(cdip)->devi_node_name, ub->drv_alias) != 0))
4411                         continue;
4412                 if (i_ddi_node_state(cdip) >= DS_BOUND) {
4413                         rv = ndi_devi_unbind_driver(cdip);
4414                         if (rv != DDI_SUCCESS ||
4415                             (i_ddi_node_state(cdip) >= DS_BOUND)) {
4416                                 unbind_alias_dev_in_use(cdip, ub->drv_alias);
4417                                 ub->ndevs_bound++;
4418                                 continue;
4419                         }
4420                         if (ndi_dev_is_persistent_node(cdip) == 0)
4421                                 (void) ddi_remove_child(cdip, 0);
4422                 }
4423         }
4424         ndi_devi_exit(dip, circ);
4425 
4426         return (DDI_WALK_CONTINUE);
4427 }
4428 
4429 /*
4430  * Unbind devices by driver & alias
4431  * Context: update_drv [-f] -d -i <alias> <driver>
4432  */
4433 int
4434 i_ddi_unbind_devs_by_alias(major_t major, char *alias)
4435 {
4436         unbind_data_t   *ub;
4437         int             rv;
4438 
4439         ub = kmem_zalloc(sizeof (*ub), KM_SLEEP);
4440         ub->drv_major = major;
4441         ub->drv_alias = alias;
4442         ub->ndevs_bound = 0;
4443         ub->unbind_errors = 0;
4444 
4445         /* flush devfs so that ndi_devi_unbind_driver will work when possible */
4446         (void) devfs_clean(top_devinfo, NULL, 0);
4447         ddi_walk_devs(top_devinfo, unbind_children_by_alias,
4448             (void *)(uintptr_t)ub);
4449 
4450         /* return the number of devices remaining bound to the alias */
4451         rv = ub->ndevs_bound + ub->unbind_errors;
4452         kmem_free(ub, sizeof (*ub));
4453         return (rv);
4454 }
4455 
4456 /*
4457  * walkdevs callback for unbind devices by driver
4458  */
4459 static int
4460 unbind_children_by_driver(dev_info_t *dip, void *arg)
4461 {
4462         int             circ;
4463         dev_info_t      *cdip;
4464         dev_info_t      *next;
4465         major_t         major = (major_t)(uintptr_t)arg;
4466         int             rv;
4467 
4468         /*
4469          * We are called either from rem_drv or update_drv when reloading
4470          * a driver.conf file. In either case, we unbind persistent nodes
4471          * and destroy .conf nodes. In the case of rem_drv, this will be
4472          * the final state. In the case of update_drv,  i_ddi_bind_devs()
4473          * may be invoked later to re-enumerate (new) driver.conf rebind
4474          * persistent nodes.
4475          */
4476         ndi_devi_enter(dip, &circ);
4477         for (cdip = ddi_get_child(dip); cdip; cdip = next) {
4478                 next = ddi_get_next_sibling(cdip);
4479                 if (ddi_driver_major(cdip) != major)
4480                         continue;
4481                 if (i_ddi_node_state(cdip) >= DS_BOUND) {
4482                         rv = ndi_devi_unbind_driver(cdip);
4483                         if (rv == DDI_FAILURE ||
4484                             (i_ddi_node_state(cdip) >= DS_BOUND))
4485                                 continue;
4486                         if (ndi_dev_is_persistent_node(cdip) == 0)
4487                                 (void) ddi_remove_child(cdip, 0);
4488                 }
4489         }
4490         ndi_devi_exit(dip, circ);
4491 
4492         return (DDI_WALK_CONTINUE);
4493 }
4494 
4495 /*
4496  * Unbind devices by driver
4497  * Context: rem_drv or unload driver.conf
4498  */
4499 void
4500 i_ddi_unbind_devs(major_t major)
4501 {
4502         /* flush devfs so that ndi_devi_unbind_driver will work when possible */
4503         (void) devfs_clean(top_devinfo, NULL, 0);
4504         ddi_walk_devs(top_devinfo, unbind_children_by_driver,
4505             (void *)(uintptr_t)major);
4506 }
4507 
4508 /*
4509  * I/O Hotplug control
4510  */
4511 
4512 /*
4513  * create and attach a dev_info node from a .conf file spec
4514  */
4515 static void
4516 init_spec_child(dev_info_t *pdip, struct hwc_spec *specp, uint_t flags)
4517 {
4518         _NOTE(ARGUNUSED(flags))
4519         dev_info_t *dip;
4520         char *node_name;
4521 
4522         if (((node_name = specp->hwc_devi_name) == NULL) ||
4523             (ddi_name_to_major(node_name) == DDI_MAJOR_T_NONE)) {
4524                 char *tmp = node_name;
4525                 if (tmp == NULL)
4526                         tmp = "<none>";
4527                 cmn_err(CE_CONT,
4528                     "init_spec_child: parent=%s, bad spec (%s)\n",
4529                     ddi_node_name(pdip), tmp);
4530                 return;
4531         }
4532 
4533         dip = i_ddi_alloc_node(pdip, node_name, (pnode_t)DEVI_PSEUDO_NODEID,
4534             -1, specp->hwc_devi_sys_prop_ptr, KM_SLEEP);
4535 
4536         if (dip == NULL)
4537                 return;
4538 
4539         if (ddi_initchild(pdip, dip) != DDI_SUCCESS)
4540                 (void) ddi_remove_child(dip, 0);
4541 }
4542 
4543 /*
4544  * Lookup hwc specs from hash tables and make children from the spec
4545  * Because some .conf children are "merge" nodes, we also initialize
4546  * .conf children to merge properties onto hardware nodes.
4547  *
4548  * The pdip must be held busy.
4549  */
4550 int
4551 i_ndi_make_spec_children(dev_info_t *pdip, uint_t flags)
4552 {
4553         extern struct hwc_spec *hwc_get_child_spec(dev_info_t *, major_t);
4554         int                     circ;
4555         struct hwc_spec         *list, *spec;
4556 
4557         ndi_devi_enter(pdip, &circ);
4558         if (DEVI(pdip)->devi_flags & DEVI_MADE_CHILDREN) {
4559                 ndi_devi_exit(pdip, circ);
4560                 return (DDI_SUCCESS);
4561         }
4562 
4563         list = hwc_get_child_spec(pdip, DDI_MAJOR_T_NONE);
4564         for (spec = list; spec != NULL; spec = spec->hwc_next) {
4565                 init_spec_child(pdip, spec, flags);
4566         }
4567         hwc_free_spec_list(list);
4568 
4569         mutex_enter(&DEVI(pdip)->devi_lock);
4570         DEVI(pdip)->devi_flags |= DEVI_MADE_CHILDREN;
4571         mutex_exit(&DEVI(pdip)->devi_lock);
4572         ndi_devi_exit(pdip, circ);
4573         return (DDI_SUCCESS);
4574 }
4575 
4576 /*
4577  * Run initchild on all child nodes such that instance assignment
4578  * for multiport network cards are contiguous.
4579  *
4580  * The pdip must be held busy.
4581  */
4582 static void
4583 i_ndi_init_hw_children(dev_info_t *pdip, uint_t flags)
4584 {
4585         dev_info_t *dip;
4586 
4587         ASSERT(DEVI(pdip)->devi_flags & DEVI_MADE_CHILDREN);
4588 
4589         /* contiguous instance assignment */
4590         e_ddi_enter_instance();
4591         dip = ddi_get_child(pdip);
4592         while (dip) {
4593                 if (ndi_dev_is_persistent_node(dip))
4594                         (void) i_ndi_config_node(dip, DS_INITIALIZED, flags);
4595                 dip = ddi_get_next_sibling(dip);
4596         }
4597         e_ddi_exit_instance();
4598 }
4599 
4600 /*
4601  * report device status
4602  */
4603 static void
4604 i_ndi_devi_report_status_change(dev_info_t *dip, char *path)
4605 {
4606         char *status;
4607 
4608         if (!DEVI_NEED_REPORT(dip) ||
4609             (i_ddi_node_state(dip) < DS_INITIALIZED) ||
4610             ndi_dev_is_hidden_node(dip)) {
4611                 return;
4612         }
4613 
4614         /* Invalidate the devinfo snapshot cache */
4615         i_ddi_di_cache_invalidate();
4616 
4617         if (DEVI_IS_DEVICE_REMOVED(dip)) {
4618                 status = "removed";
4619         } else if (DEVI_IS_DEVICE_OFFLINE(dip)) {
4620                 status = "offline";
4621         } else if (DEVI_IS_DEVICE_DOWN(dip)) {
4622                 status = "down";
4623         } else if (DEVI_IS_BUS_QUIESCED(dip)) {
4624                 status = "quiesced";
4625         } else if (DEVI_IS_BUS_DOWN(dip)) {
4626                 status = "down";
4627         } else if (i_ddi_devi_attached(dip)) {
4628                 status = "online";
4629         } else {
4630                 status = "unknown";
4631         }
4632 
4633         if (path == NULL) {
4634                 path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
4635                 cmn_err(CE_CONT, "?%s (%s%d) %s\n",
4636                     ddi_pathname(dip, path), ddi_driver_name(dip),
4637                     ddi_get_instance(dip), status);
4638                 kmem_free(path, MAXPATHLEN);
4639         } else {
4640                 cmn_err(CE_CONT, "?%s (%s%d) %s\n",
4641                     path, ddi_driver_name(dip),
4642                     ddi_get_instance(dip), status);
4643         }
4644 
4645         mutex_enter(&(DEVI(dip)->devi_lock));
4646         DEVI_REPORT_DONE(dip);
4647         mutex_exit(&(DEVI(dip)->devi_lock));
4648 }
4649 
4650 /*
4651  * log a notification that a dev_info node has been configured.
4652  */
4653 static int
4654 i_log_devfs_add_devinfo(dev_info_t *dip, uint_t flags)
4655 {
4656         int                     se_err;
4657         char                    *pathname;
4658         sysevent_t              *ev;
4659         sysevent_id_t           eid;
4660         sysevent_value_t        se_val;
4661         sysevent_attr_list_t    *ev_attr_list = NULL;
4662         char                    *class_name;
4663         int                     no_transport = 0;
4664 
4665         ASSERT(dip && ddi_get_parent(dip) &&
4666             DEVI_BUSY_OWNED(ddi_get_parent(dip)));
4667 
4668         /* do not generate ESC_DEVFS_DEVI_ADD event during boot */
4669         if (!i_ddi_io_initialized())
4670                 return (DDI_SUCCESS);
4671 
4672         /* Invalidate the devinfo snapshot cache */
4673         i_ddi_di_cache_invalidate();
4674 
4675         ev = sysevent_alloc(EC_DEVFS, ESC_DEVFS_DEVI_ADD, EP_DDI, SE_SLEEP);
4676 
4677         pathname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
4678 
4679         (void) ddi_pathname(dip, pathname);
4680         ASSERT(strlen(pathname));
4681 
4682         se_val.value_type = SE_DATA_TYPE_STRING;
4683         se_val.value.sv_string = pathname;
4684         if (sysevent_add_attr(&ev_attr_list, DEVFS_PATHNAME,
4685             &se_val, SE_SLEEP) != 0) {
4686                 goto fail;
4687         }
4688 
4689         /* add the device class attribute */
4690         if ((class_name = i_ddi_devi_class(dip)) != NULL) {
4691                 se_val.value_type = SE_DATA_TYPE_STRING;
4692                 se_val.value.sv_string = class_name;
4693 
4694                 if (sysevent_add_attr(&ev_attr_list,
4695                     DEVFS_DEVI_CLASS, &se_val, SE_SLEEP) != 0) {
4696                         sysevent_free_attr(ev_attr_list);
4697                         goto fail;
4698                 }
4699         }
4700 
4701         /*
4702          * must log a branch event too unless NDI_BRANCH_EVENT_OP is set,
4703          * in which case the branch event will be logged by the caller
4704          * after the entire branch has been configured.
4705          */
4706         if ((flags & NDI_BRANCH_EVENT_OP) == 0) {
4707                 /*
4708                  * Instead of logging a separate branch event just add
4709                  * DEVFS_BRANCH_EVENT attribute. It indicates devfsadmd to
4710                  * generate a EC_DEV_BRANCH event.
4711                  */
4712                 se_val.value_type = SE_DATA_TYPE_INT32;
4713                 se_val.value.sv_int32 = 1;
4714                 if (sysevent_add_attr(&ev_attr_list,
4715                     DEVFS_BRANCH_EVENT, &se_val, SE_SLEEP) != 0) {
4716                         sysevent_free_attr(ev_attr_list);
4717                         goto fail;
4718                 }
4719         }
4720 
4721         if (sysevent_attach_attributes(ev, ev_attr_list) != 0) {
4722                 sysevent_free_attr(ev_attr_list);
4723                 goto fail;
4724         }
4725 
4726         if ((se_err = log_sysevent(ev, SE_SLEEP, &eid)) != 0) {
4727                 if (se_err == SE_NO_TRANSPORT)
4728                         no_transport = 1;
4729                 goto fail;
4730         }
4731 
4732         sysevent_free(ev);
4733         kmem_free(pathname, MAXPATHLEN);
4734 
4735         return (DDI_SUCCESS);
4736 
4737 fail:
4738         cmn_err(CE_WARN, "failed to log ESC_DEVFS_DEVI_ADD event for %s%s",
4739             pathname, (no_transport) ? " (syseventd not responding)" : "");
4740 
4741         cmn_err(CE_WARN, "/dev may not be current for driver %s. "
4742             "Run devfsadm -i %s",
4743             ddi_driver_name(dip), ddi_driver_name(dip));
4744 
4745         sysevent_free(ev);
4746         kmem_free(pathname, MAXPATHLEN);
4747         return (DDI_SUCCESS);
4748 }
4749 
4750 /*
4751  * log a notification that a dev_info node has been unconfigured.
4752  */
4753 static int
4754 i_log_devfs_remove_devinfo(char *pathname, char *class_name, char *driver_name,
4755     int instance, uint_t flags)
4756 {
4757         sysevent_t              *ev;
4758         sysevent_id_t           eid;
4759         sysevent_value_t        se_val;
4760         sysevent_attr_list_t    *ev_attr_list = NULL;
4761         int                     se_err;
4762         int                     no_transport = 0;
4763 
4764         if (!i_ddi_io_initialized())
4765                 return (DDI_SUCCESS);
4766 
4767         /* Invalidate the devinfo snapshot cache */
4768         i_ddi_di_cache_invalidate();
4769 
4770         ev = sysevent_alloc(EC_DEVFS, ESC_DEVFS_DEVI_REMOVE, EP_DDI, SE_SLEEP);
4771 
4772         se_val.value_type = SE_DATA_TYPE_STRING;
4773         se_val.value.sv_string = pathname;
4774         if (sysevent_add_attr(&ev_attr_list, DEVFS_PATHNAME,
4775             &se_val, SE_SLEEP) != 0) {
4776                 goto fail;
4777         }
4778 
4779         if (class_name) {
4780                 /* add the device class, driver name and instance attributes */
4781 
4782                 se_val.value_type = SE_DATA_TYPE_STRING;
4783                 se_val.value.sv_string = class_name;
4784                 if (sysevent_add_attr(&ev_attr_list,
4785                     DEVFS_DEVI_CLASS, &se_val, SE_SLEEP) != 0) {
4786                         sysevent_free_attr(ev_attr_list);
4787                         goto fail;
4788                 }
4789 
4790                 se_val.value_type = SE_DATA_TYPE_STRING;
4791                 se_val.value.sv_string = driver_name;
4792                 if (sysevent_add_attr(&ev_attr_list,
4793                     DEVFS_DRIVER_NAME, &se_val, SE_SLEEP) != 0) {
4794                         sysevent_free_attr(ev_attr_list);
4795                         goto fail;
4796                 }
4797 
4798                 se_val.value_type = SE_DATA_TYPE_INT32;
4799                 se_val.value.sv_int32 = instance;
4800                 if (sysevent_add_attr(&ev_attr_list,
4801                     DEVFS_INSTANCE, &se_val, SE_SLEEP) != 0) {
4802                         sysevent_free_attr(ev_attr_list);
4803                         goto fail;
4804                 }
4805         }
4806 
4807         /*
4808          * must log a branch event too unless NDI_BRANCH_EVENT_OP is set,
4809          * in which case the branch event will be logged by the caller
4810          * after the entire branch has been unconfigured.
4811          */
4812         if ((flags & NDI_BRANCH_EVENT_OP) == 0) {
4813                 /*
4814                  * Instead of logging a separate branch event just add
4815                  * DEVFS_BRANCH_EVENT attribute. It indicates devfsadmd to
4816                  * generate a EC_DEV_BRANCH event.
4817                  */
4818                 se_val.value_type = SE_DATA_TYPE_INT32;
4819                 se_val.value.sv_int32 = 1;
4820                 if (sysevent_add_attr(&ev_attr_list,
4821                     DEVFS_BRANCH_EVENT, &se_val, SE_SLEEP) != 0) {
4822                         sysevent_free_attr(ev_attr_list);
4823                         goto fail;
4824                 }
4825         }
4826 
4827         if (sysevent_attach_attributes(ev, ev_attr_list) != 0) {
4828                 sysevent_free_attr(ev_attr_list);
4829                 goto fail;
4830         }
4831 
4832         if ((se_err = log_sysevent(ev, SE_SLEEP, &eid)) != 0) {
4833                 if (se_err == SE_NO_TRANSPORT)
4834                         no_transport = 1;
4835                 goto fail;
4836         }
4837 
4838         sysevent_free(ev);
4839         return (DDI_SUCCESS);
4840 
4841 fail:
4842         sysevent_free(ev);
4843         cmn_err(CE_WARN, "failed to log ESC_DEVFS_DEVI_REMOVE event for %s%s",
4844             pathname, (no_transport) ? " (syseventd not responding)" : "");
4845         return (DDI_SUCCESS);
4846 }
4847 
4848 static void
4849 i_ddi_log_devfs_device_remove(dev_info_t *dip)
4850 {
4851         char    *path;
4852 
4853         ASSERT(dip && ddi_get_parent(dip) &&
4854             DEVI_BUSY_OWNED(ddi_get_parent(dip)));
4855         ASSERT(DEVI_IS_DEVICE_REMOVED(dip));
4856 
4857         ASSERT(i_ddi_node_state(dip) >= DS_INITIALIZED);
4858         if (i_ddi_node_state(dip) < DS_INITIALIZED)
4859                 return;
4860 
4861         /* Inform LDI_EV_DEVICE_REMOVE callbacks. */
4862         ldi_invoke_finalize(dip, DDI_DEV_T_ANY, 0, LDI_EV_DEVICE_REMOVE,
4863             LDI_EV_SUCCESS, NULL);
4864 
4865         /* Generate EC_DEVFS_DEVI_REMOVE sysevent. */
4866         path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
4867         (void) i_log_devfs_remove_devinfo(ddi_pathname(dip, path),
4868             i_ddi_devi_class(dip), (char *)ddi_driver_name(dip),
4869             ddi_get_instance(dip), 0);
4870         kmem_free(path, MAXPATHLEN);
4871 }
4872 
4873 static void
4874 i_ddi_log_devfs_device_insert(dev_info_t *dip)
4875 {
4876         ASSERT(dip && ddi_get_parent(dip) &&
4877             DEVI_BUSY_OWNED(ddi_get_parent(dip)));
4878         ASSERT(!DEVI_IS_DEVICE_REMOVED(dip));
4879 
4880         (void) i_log_devfs_add_devinfo(dip, 0);
4881 }
4882 
4883 
4884 /*
4885  * log an event that a dev_info branch has been configured or unconfigured.
4886  */
4887 static int
4888 i_log_devfs_branch(char *node_path, char *subclass)
4889 {
4890         int se_err;
4891         sysevent_t *ev;
4892         sysevent_id_t eid;
4893         sysevent_value_t se_val;
4894         sysevent_attr_list_t *ev_attr_list = NULL;
4895         int no_transport = 0;
4896 
4897         /* do not generate the event during boot */
4898         if (!i_ddi_io_initialized())
4899                 return (DDI_SUCCESS);
4900 
4901         /* Invalidate the devinfo snapshot cache */
4902         i_ddi_di_cache_invalidate();
4903 
4904         ev = sysevent_alloc(EC_DEVFS, subclass, EP_DDI, SE_SLEEP);
4905 
4906         se_val.value_type = SE_DATA_TYPE_STRING;
4907         se_val.value.sv_string = node_path;
4908 
4909         if (sysevent_add_attr(&ev_attr_list, DEVFS_PATHNAME,
4910             &se_val, SE_SLEEP) != 0) {
4911                 goto fail;
4912         }
4913 
4914         if (sysevent_attach_attributes(ev, ev_attr_list) != 0) {
4915                 sysevent_free_attr(ev_attr_list);
4916                 goto fail;
4917         }
4918 
4919         if ((se_err = log_sysevent(ev, SE_SLEEP, &eid)) != 0) {
4920                 if (se_err == SE_NO_TRANSPORT)
4921                         no_transport = 1;
4922                 goto fail;
4923         }
4924 
4925         sysevent_free(ev);
4926         return (DDI_SUCCESS);
4927 
4928 fail:
4929         cmn_err(CE_WARN, "failed to log %s branch event for %s%s",
4930             subclass, node_path,
4931             (no_transport) ? " (syseventd not responding)" : "");
4932 
4933         sysevent_free(ev);
4934         return (DDI_FAILURE);
4935 }
4936 
4937 /*
4938  * log an event that a dev_info tree branch has been configured.
4939  */
4940 static int
4941 i_log_devfs_branch_add(dev_info_t *dip)
4942 {
4943         char *node_path;
4944         int rv;
4945 
4946         node_path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
4947         (void) ddi_pathname(dip, node_path);
4948         rv = i_log_devfs_branch(node_path, ESC_DEVFS_BRANCH_ADD);
4949         kmem_free(node_path, MAXPATHLEN);
4950 
4951         return (rv);
4952 }
4953 
4954 /*
4955  * log an event that a dev_info tree branch has been unconfigured.
4956  */
4957 static int
4958 i_log_devfs_branch_remove(char *node_path)
4959 {
4960         return (i_log_devfs_branch(node_path, ESC_DEVFS_BRANCH_REMOVE));
4961 }
4962 
4963 /*
4964  * enqueue the dip's deviname on the branch event queue.
4965  */
4966 static struct brevq_node *
4967 brevq_enqueue(struct brevq_node **brevqp, dev_info_t *dip,
4968     struct brevq_node *child)
4969 {
4970         struct brevq_node *brn;
4971         char *deviname;
4972 
4973         deviname = kmem_alloc(MAXNAMELEN, KM_SLEEP);
4974         (void) ddi_deviname(dip, deviname);
4975 
4976         brn = kmem_zalloc(sizeof (*brn), KM_SLEEP);
4977         brn->brn_deviname = i_ddi_strdup(deviname, KM_SLEEP);
4978         kmem_free(deviname, MAXNAMELEN);
4979         brn->brn_child = child;
4980         brn->brn_sibling = *brevqp;
4981         *brevqp = brn;
4982 
4983         return (brn);
4984 }
4985 
4986 /*
4987  * free the memory allocated for the elements on the branch event queue.
4988  */
4989 static void
4990 free_brevq(struct brevq_node *brevq)
4991 {
4992         struct brevq_node *brn, *next_brn;
4993 
4994         for (brn = brevq; brn != NULL; brn = next_brn) {
4995                 next_brn = brn->brn_sibling;
4996                 ASSERT(brn->brn_child == NULL);
4997                 kmem_free(brn->brn_deviname, strlen(brn->brn_deviname) + 1);
4998                 kmem_free(brn, sizeof (*brn));
4999         }
5000 }
5001 
5002 /*
5003  * log the events queued up on the branch event queue and free the
5004  * associated memory.
5005  *
5006  * node_path must have been allocated with at least MAXPATHLEN bytes.
5007  */
5008 static void
5009 log_and_free_brevq(char *node_path, struct brevq_node *brevq)
5010 {
5011         struct brevq_node *brn;
5012         char *p;
5013 
5014         p = node_path + strlen(node_path);
5015         for (brn = brevq; brn != NULL; brn = brn->brn_sibling) {
5016                 (void) strcpy(p, brn->brn_deviname);
5017                 (void) i_log_devfs_branch_remove(node_path);
5018         }
5019         *p = '\0';
5020 
5021         free_brevq(brevq);
5022 }
5023 
5024 /*
5025  * log the events queued up on the branch event queue and free the
5026  * associated memory. Same as the previous function but operates on dip.
5027  */
5028 static void
5029 log_and_free_brevq_dip(dev_info_t *dip, struct brevq_node *brevq)
5030 {
5031         char *path;
5032 
5033         path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
5034         (void) ddi_pathname(dip, path);
5035         log_and_free_brevq(path, brevq);
5036         kmem_free(path, MAXPATHLEN);
5037 }
5038 
5039 /*
5040  * log the outstanding branch remove events for the grand children of the dip
5041  * and free the associated memory.
5042  */
5043 static void
5044 log_and_free_br_events_on_grand_children(dev_info_t *dip,
5045     struct brevq_node *brevq)
5046 {
5047         struct brevq_node *brn;
5048         char *path;
5049         char *p;
5050 
5051         path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
5052         (void) ddi_pathname(dip, path);
5053         p = path + strlen(path);
5054         for (brn = brevq; brn != NULL; brn = brn->brn_sibling) {
5055                 if (brn->brn_child) {
5056                         (void) strcpy(p, brn->brn_deviname);
5057                         /* now path contains the node path to the dip's child */
5058                         log_and_free_brevq(path, brn->brn_child);
5059                         brn->brn_child = NULL;
5060                 }
5061         }
5062         kmem_free(path, MAXPATHLEN);
5063 }
5064 
5065 /*
5066  * log and cleanup branch remove events for the grand children of the dip.
5067  */
5068 static void
5069 cleanup_br_events_on_grand_children(dev_info_t *dip, struct brevq_node **brevqp)
5070 {
5071         dev_info_t *child;
5072         struct brevq_node *brevq, *brn, *prev_brn, *next_brn;
5073         char *path;
5074         int circ;
5075 
5076         path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
5077         prev_brn = NULL;
5078         brevq = *brevqp;
5079 
5080         ndi_devi_enter(dip, &circ);
5081         for (brn = brevq; brn != NULL; brn = next_brn) {
5082                 next_brn = brn->brn_sibling;
5083                 for (child = ddi_get_child(dip); child != NULL;
5084                     child = ddi_get_next_sibling(child)) {
5085                         if (i_ddi_node_state(child) >= DS_INITIALIZED) {
5086                                 (void) ddi_deviname(child, path);
5087                                 if (strcmp(path, brn->brn_deviname) == 0)
5088                                         break;
5089                         }
5090                 }
5091 
5092                 if (child != NULL && !(DEVI_EVREMOVE(child))) {
5093                         /*
5094                          * Event state is not REMOVE. So branch remove event
5095                          * is not going be generated on brn->brn_child.
5096                          * If any branch remove events were queued up on
5097                          * brn->brn_child log them and remove the brn
5098                          * from the queue.
5099                          */
5100                         if (brn->brn_child) {
5101                                 (void) ddi_pathname(dip, path);
5102                                 (void) strcat(path, brn->brn_deviname);
5103                                 log_and_free_brevq(path, brn->brn_child);
5104                         }
5105 
5106                         if (prev_brn)
5107                                 prev_brn->brn_sibling = next_brn;
5108                         else
5109                                 *brevqp = next_brn;
5110 
5111                         kmem_free(brn->brn_deviname,
5112                             strlen(brn->brn_deviname) + 1);
5113                         kmem_free(brn, sizeof (*brn));
5114                 } else {
5115                         /*
5116                          * Free up the outstanding branch remove events
5117                          * queued on brn->brn_child since brn->brn_child
5118                          * itself is eligible for branch remove event.
5119                          */
5120                         if (brn->brn_child) {
5121                                 free_brevq(brn->brn_child);
5122                                 brn->brn_child = NULL;
5123                         }
5124                         prev_brn = brn;
5125                 }
5126         }
5127 
5128         ndi_devi_exit(dip, circ);
5129         kmem_free(path, MAXPATHLEN);
5130 }
5131 
5132 static int
5133 need_remove_event(dev_info_t *dip, int flags)
5134 {
5135         if ((flags & (NDI_NO_EVENT | NDI_AUTODETACH)) == 0 &&
5136             (flags & (NDI_DEVI_OFFLINE | NDI_UNCONFIG | NDI_DEVI_REMOVE)) &&
5137             !(DEVI_EVREMOVE(dip)))
5138                 return (1);
5139         else
5140                 return (0);
5141 }
5142 
5143 /*
5144  * Unconfigure children/descendants of the dip.
5145  *
5146  * If the operation involves a branch event NDI_BRANCH_EVENT_OP is set
5147  * through out the unconfiguration. On successful return *brevqp is set to
5148  * a queue of dip's child devinames for which branch remove events need
5149  * to be generated.
5150  */
5151 static int
5152 devi_unconfig_branch(dev_info_t *dip, dev_info_t **dipp, int flags,
5153     struct brevq_node **brevqp)
5154 {
5155         int rval;
5156 
5157         *brevqp = NULL;
5158 
5159         if ((!(flags & NDI_BRANCH_EVENT_OP)) && need_remove_event(dip, flags))
5160                 flags |= NDI_BRANCH_EVENT_OP;
5161 
5162         if (flags & NDI_BRANCH_EVENT_OP) {
5163                 rval = devi_unconfig_common(dip, dipp, flags, DDI_MAJOR_T_NONE,
5164                     brevqp);
5165 
5166                 if (rval != NDI_SUCCESS && (*brevqp)) {
5167                         log_and_free_brevq_dip(dip, *brevqp);
5168                         *brevqp = NULL;
5169                 }
5170         } else
5171                 rval = devi_unconfig_common(dip, dipp, flags, DDI_MAJOR_T_NONE,
5172                     NULL);
5173 
5174         return (rval);
5175 }
5176 
5177 /*
5178  * If the dip is already bound to a driver transition to DS_INITIALIZED
5179  * in order to generate an event in the case where the node was left in
5180  * DS_BOUND state since boot (never got attached) and the node is now
5181  * being offlined.
5182  */
5183 static void
5184 init_bound_node_ev(dev_info_t *pdip, dev_info_t *dip, int flags)
5185 {
5186         if (need_remove_event(dip, flags) &&
5187             i_ddi_node_state(dip) == DS_BOUND &&
5188             i_ddi_devi_attached(pdip) && !DEVI_IS_DEVICE_OFFLINE(dip))
5189                 (void) ddi_initchild(pdip, dip);
5190 }
5191 
5192 /*
5193  * attach a node/branch with parent already held busy
5194  */
5195 static int
5196 devi_attach_node(dev_info_t *dip, uint_t flags)
5197 {
5198         dev_info_t *pdip = ddi_get_parent(dip);
5199 
5200         ASSERT(pdip && DEVI_BUSY_OWNED(pdip));
5201 
5202         mutex_enter(&(DEVI(dip)->devi_lock));
5203         if (flags & NDI_DEVI_ONLINE) {
5204                 if (!i_ddi_devi_attached(dip))
5205                         DEVI_SET_REPORT(dip);
5206                 DEVI_SET_DEVICE_ONLINE(dip);
5207         }
5208         if (DEVI_IS_DEVICE_OFFLINE(dip)) {
5209                 mutex_exit(&(DEVI(dip)->devi_lock));
5210                 return (NDI_FAILURE);
5211         }
5212         mutex_exit(&(DEVI(dip)->devi_lock));
5213 
5214         if (i_ddi_attachchild(dip) != DDI_SUCCESS) {
5215                 mutex_enter(&(DEVI(dip)->devi_lock));
5216                 DEVI_SET_EVUNINIT(dip);
5217                 mutex_exit(&(DEVI(dip)->devi_lock));
5218 
5219                 if (ndi_dev_is_persistent_node(dip))
5220                         (void) ddi_uninitchild(dip);
5221                 else {
5222                         /*
5223                          * Delete .conf nodes and nodes that are not
5224                          * well formed.
5225                          */
5226                         (void) ddi_remove_child(dip, 0);
5227                 }
5228                 return (NDI_FAILURE);
5229         }
5230 
5231         i_ndi_devi_report_status_change(dip, NULL);
5232 
5233         /*
5234          * log an event, but not during devfs lookups in which case
5235          * NDI_NO_EVENT is set.
5236          */
5237         if ((flags & NDI_NO_EVENT) == 0 && !(DEVI_EVADD(dip))) {
5238                 (void) i_log_devfs_add_devinfo(dip, flags);
5239 
5240                 mutex_enter(&(DEVI(dip)->devi_lock));
5241                 DEVI_SET_EVADD(dip);
5242                 mutex_exit(&(DEVI(dip)->devi_lock));
5243         } else if (!(flags & NDI_NO_EVENT_STATE_CHNG)) {
5244                 mutex_enter(&(DEVI(dip)->devi_lock));
5245                 DEVI_SET_EVADD(dip);
5246                 mutex_exit(&(DEVI(dip)->devi_lock));
5247         }
5248 
5249         return (NDI_SUCCESS);
5250 }
5251 
5252 /* internal function to config immediate children */
5253 static int
5254 config_immediate_children(dev_info_t *pdip, uint_t flags, major_t major)
5255 {
5256         dev_info_t      *child, *next;
5257         int             circ;
5258 
5259         ASSERT(i_ddi_devi_attached(pdip));
5260 
5261         if (!NEXUS_DRV(ddi_get_driver(pdip)))
5262                 return (NDI_SUCCESS);
5263 
5264         NDI_CONFIG_DEBUG((CE_CONT,
5265             "config_immediate_children: %s%d (%p), flags=%x\n",
5266             ddi_driver_name(pdip), ddi_get_instance(pdip),
5267             (void *)pdip, flags));
5268 
5269         ndi_devi_enter(pdip, &circ);
5270 
5271         if (flags & NDI_CONFIG_REPROBE) {
5272                 mutex_enter(&DEVI(pdip)->devi_lock);
5273                 DEVI(pdip)->devi_flags &= ~DEVI_MADE_CHILDREN;
5274                 mutex_exit(&DEVI(pdip)->devi_lock);
5275         }
5276         (void) i_ndi_make_spec_children(pdip, flags);
5277         i_ndi_init_hw_children(pdip, flags);
5278 
5279         child = ddi_get_child(pdip);
5280         while (child) {
5281                 /* NOTE: devi_attach_node() may remove the dip */
5282                 next = ddi_get_next_sibling(child);
5283 
5284                 /*
5285                  * Configure all nexus nodes or leaf nodes with
5286                  * matching driver major
5287                  */
5288                 if ((major == DDI_MAJOR_T_NONE) ||
5289                     (major == ddi_driver_major(child)) ||
5290                     ((flags & NDI_CONFIG) && (is_leaf_node(child) == 0)))
5291                         (void) devi_attach_node(child, flags);
5292                 child = next;
5293         }
5294 
5295         ndi_devi_exit(pdip, circ);
5296 
5297         return (NDI_SUCCESS);
5298 }
5299 
5300 /* internal function to config grand children */
5301 static int
5302 config_grand_children(dev_info_t *pdip, uint_t flags, major_t major)
5303 {
5304         struct mt_config_handle *hdl;
5305 
5306         /* multi-threaded configuration of child nexus */
5307         hdl = mt_config_init(pdip, NULL, flags, major, MT_CONFIG_OP, NULL);
5308         mt_config_children(hdl);
5309 
5310         return (mt_config_fini(hdl));   /* wait for threads to exit */
5311 }
5312 
5313 /*
5314  * Common function for device tree configuration,
5315  * either BUS_CONFIG_ALL or BUS_CONFIG_DRIVER.
5316  * The NDI_CONFIG flag causes recursive configuration of
5317  * grandchildren, devfs usage should not recurse.
5318  */
5319 static int
5320 devi_config_common(dev_info_t *dip, int flags, major_t major)
5321 {
5322         int error;
5323         int (*f)();
5324 
5325         if (!i_ddi_devi_attached(dip))
5326                 return (NDI_FAILURE);
5327 
5328         if (pm_pre_config(dip, NULL) != DDI_SUCCESS)
5329                 return (NDI_FAILURE);
5330 
5331         if ((DEVI(dip)->devi_ops->devo_bus_ops == NULL) ||
5332             (DEVI(dip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
5333             (f = DEVI(dip)->devi_ops->devo_bus_ops->bus_config) == NULL) {
5334                 error = config_immediate_children(dip, flags, major);
5335         } else {
5336                 /* call bus_config entry point */
5337                 ddi_bus_config_op_t bus_op = (major == DDI_MAJOR_T_NONE) ?
5338                     BUS_CONFIG_ALL : BUS_CONFIG_DRIVER;
5339                 error = (*f)(dip,
5340                     flags, bus_op, (void *)(uintptr_t)major, NULL, 0);
5341         }
5342 
5343         if (error) {
5344                 pm_post_config(dip, NULL);
5345                 return (error);
5346         }
5347 
5348         /*
5349          * Some callers, notably SCSI, need to mark the devfs cache
5350          * to be rebuilt together with the config operation.
5351          */
5352         if (flags & NDI_DEVFS_CLEAN)
5353                 (void) devfs_clean(dip, NULL, 0);
5354 
5355         if (flags & NDI_CONFIG)
5356                 (void) config_grand_children(dip, flags, major);
5357 
5358         pm_post_config(dip, NULL);
5359 
5360         return (NDI_SUCCESS);
5361 }
5362 
5363 /*
5364  * Framework entry point for BUS_CONFIG_ALL
5365  */
5366 int
5367 ndi_devi_config(dev_info_t *dip, int flags)
5368 {
5369         NDI_CONFIG_DEBUG((CE_CONT,
5370             "ndi_devi_config: par = %s%d (%p), flags = 0x%x\n",
5371             ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
5372 
5373         return (devi_config_common(dip, flags, DDI_MAJOR_T_NONE));
5374 }
5375 
5376 /*
5377  * Framework entry point for BUS_CONFIG_DRIVER, bound to major
5378  */
5379 int
5380 ndi_devi_config_driver(dev_info_t *dip, int flags, major_t major)
5381 {
5382         /* don't abuse this function */
5383         ASSERT(major != DDI_MAJOR_T_NONE);
5384 
5385         NDI_CONFIG_DEBUG((CE_CONT,
5386             "ndi_devi_config_driver: par = %s%d (%p), flags = 0x%x\n",
5387             ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
5388 
5389         return (devi_config_common(dip, flags, major));
5390 }
5391 
5392 /*
5393  * Called by nexus drivers to configure its children.
5394  */
5395 static int
5396 devi_config_one(dev_info_t *pdip, char *devnm, dev_info_t **cdipp,
5397     uint_t flags, clock_t timeout)
5398 {
5399         dev_info_t      *vdip = NULL;
5400         char            *drivername = NULL;
5401         int             find_by_addr = 0;
5402         char            *name, *addr;
5403         int             v_circ, p_circ;
5404         clock_t         end_time;       /* 60 sec */
5405         int             probed;
5406         dev_info_t      *cdip;
5407         mdi_pathinfo_t  *cpip;
5408 
5409         *cdipp = NULL;
5410 
5411         if (!NEXUS_DRV(ddi_get_driver(pdip)))
5412                 return (NDI_FAILURE);
5413 
5414         /* split name into "name@addr" parts */
5415         i_ddi_parse_name(devnm, &name, &addr, NULL);
5416 
5417         /*
5418          * If the nexus is a pHCI and we are not processing a pHCI from
5419          * mdi bus_config code then we need to know the vHCI.
5420          */
5421         if (MDI_PHCI(pdip))
5422                 vdip = mdi_devi_get_vdip(pdip);
5423 
5424         /*
5425          * We may have a genericname on a system that creates drivername
5426          * nodes (from .conf files).  Find the drivername by nodeid. If we
5427          * can't find a node with devnm as the node name then we search by
5428          * drivername.  This allows an implementation to supply a genericly
5429          * named boot path (disk) and locate drivename nodes (sd).  The
5430          * NDI_PROMNAME flag does not apply to /devices/pseudo paths.
5431          */
5432         if ((flags & NDI_PROMNAME) && (pdip != pseudo_dip)) {
5433                 drivername = child_path_to_driver(pdip, name, addr);
5434                 find_by_addr = 1;
5435         }
5436 
5437         /*
5438          * Determine end_time: This routine should *not* be called with a
5439          * constant non-zero timeout argument, the caller should be adjusting
5440          * the timeout argument relative to when it *started* its asynchronous
5441          * enumeration.
5442          */
5443         if (timeout > 0)
5444                 end_time = ddi_get_lbolt() + timeout;
5445 
5446         for (;;) {
5447                 /*
5448                  * For pHCI, enter (vHCI, pHCI) and search for pathinfo/client
5449                  * child - break out of for(;;) loop if child found.
5450                  * NOTE: Lock order for ndi_devi_enter is (vHCI, pHCI).
5451                  */
5452                 if (vdip) {
5453                         /* use mdi_devi_enter ordering */
5454                         ndi_devi_enter(vdip, &v_circ);
5455                         ndi_devi_enter(pdip, &p_circ);
5456                         cpip = mdi_pi_find(pdip, NULL, addr);
5457                         cdip = mdi_pi_get_client(cpip);
5458                         if (cdip)
5459                                 break;
5460                 } else
5461                         ndi_devi_enter(pdip, &p_circ);
5462 
5463                 /*
5464                  * When not a  vHCI or not all pHCI devices are required to
5465                  * enumerated under the vHCI (NDI_MDI_FALLBACK) search for
5466                  * devinfo child.
5467                  */
5468                 if ((vdip == NULL) || (flags & NDI_MDI_FALLBACK)) {
5469                         /* determine if .conf nodes already built */
5470                         probed = (DEVI(pdip)->devi_flags & DEVI_MADE_CHILDREN);
5471 
5472                         /*
5473                          * Search for child by name, if not found then search
5474                          * for a node bound to the drivername driver with the
5475                          * specified "@addr". Break out of for(;;) loop if
5476                          * child found.  To support path-oriented aliases
5477                          * binding on boot-device, we do a search_by_addr too.
5478                          */
5479 again:                  (void) i_ndi_make_spec_children(pdip, flags);
5480                         cdip = find_child_by_name(pdip, name, addr);
5481                         if ((cdip == NULL) && drivername)
5482                                 cdip = find_child_by_driver(pdip,
5483                                     drivername, addr);
5484                         if ((cdip == NULL) && find_by_addr)
5485                                 cdip = find_child_by_addr(pdip, addr);
5486                         if (cdip)
5487                                 break;
5488 
5489                         /*
5490                          * determine if we should reenumerate .conf nodes
5491                          * and look for child again.
5492                          */
5493                         if (probed &&
5494                             i_ddi_io_initialized() &&
5495                             (flags & NDI_CONFIG_REPROBE) &&
5496                             ((timeout <= 0) || (ddi_get_lbolt() >= end_time))) {
5497                                 probed = 0;
5498                                 mutex_enter(&DEVI(pdip)->devi_lock);
5499                                 DEVI(pdip)->devi_flags &= ~DEVI_MADE_CHILDREN;
5500                                 mutex_exit(&DEVI(pdip)->devi_lock);
5501                                 goto again;
5502                         }
5503                 }
5504 
5505                 /* break out of for(;;) if time expired */
5506                 if ((timeout <= 0) || (ddi_get_lbolt() >= end_time))
5507                         break;
5508 
5509                 /*
5510                  * Child not found, exit and wait for asynchronous enumeration
5511                  * to add child (or timeout). The addition of a new child (vhci
5512                  * or phci) requires the asynchronous enumeration thread to
5513                  * ndi_devi_enter/ndi_devi_exit. This exit will signal devi_cv
5514                  * and cause us to return from ndi_devi_exit_and_wait, after
5515                  * which we loop and search for the requested child again.
5516                  */
5517                 NDI_DEBUG(flags, (CE_CONT,
5518                     "%s%d: waiting for child %s@%s, timeout %ld",
5519                     ddi_driver_name(pdip), ddi_get_instance(pdip),
5520                     name, addr, timeout));
5521                 if (vdip) {
5522                         /*
5523                          * Mark vHCI for pHCI ndi_devi_exit broadcast.
5524                          */
5525                         mutex_enter(&DEVI(vdip)->devi_lock);
5526                         DEVI(vdip)->devi_flags |=
5527                             DEVI_PHCI_SIGNALS_VHCI;
5528                         mutex_exit(&DEVI(vdip)->devi_lock);
5529                         ndi_devi_exit(pdip, p_circ);
5530 
5531                         /*
5532                          * NB: There is a small race window from above
5533                          * ndi_devi_exit() of pdip to cv_wait() in
5534                          * ndi_devi_exit_and_wait() which can result in
5535                          * not immediately finding a new pHCI child
5536                          * of a pHCI that uses NDI_MDI_FAILBACK.
5537                          */
5538                         ndi_devi_exit_and_wait(vdip, v_circ, end_time);
5539                 } else {
5540                         ndi_devi_exit_and_wait(pdip, p_circ, end_time);
5541                 }
5542         }
5543 
5544         /* done with paddr, fixup i_ddi_parse_name '@'->'\0' change */
5545         if (addr && *addr != '\0')
5546                 *(addr - 1) = '@';
5547 
5548         /* attach and hold the child, returning pointer to child */
5549         if (cdip && (devi_attach_node(cdip, flags) == NDI_SUCCESS)) {
5550                 ndi_hold_devi(cdip);
5551                 *cdipp = cdip;
5552         }
5553 
5554         ndi_devi_exit(pdip, p_circ);
5555         if (vdip)
5556                 ndi_devi_exit(vdip, v_circ);
5557         return (*cdipp ? NDI_SUCCESS : NDI_FAILURE);
5558 }
5559 
5560 /*
5561  * Enumerate and attach a child specified by name 'devnm'.
5562  * Called by devfs lookup and DR to perform a BUS_CONFIG_ONE.
5563  * Note: devfs does not make use of NDI_CONFIG to configure
5564  * an entire branch.
5565  */
5566 int
5567 ndi_devi_config_one(dev_info_t *pdip, char *devnm, dev_info_t **dipp, int flags)
5568 {
5569         int error;
5570         int (*f)();
5571         char *nmdup;
5572         int duplen;
5573         int branch_event = 0;
5574 
5575         ASSERT(pdip);
5576         ASSERT(devnm);
5577         ASSERT(dipp);
5578         ASSERT(i_ddi_devi_attached(pdip));
5579 
5580         NDI_CONFIG_DEBUG((CE_CONT,
5581             "ndi_devi_config_one: par = %s%d (%p), child = %s\n",
5582             ddi_driver_name(pdip), ddi_get_instance(pdip),
5583             (void *)pdip, devnm));
5584 
5585         *dipp = NULL;
5586 
5587         if (pm_pre_config(pdip, devnm) != DDI_SUCCESS) {
5588                 cmn_err(CE_WARN, "preconfig failed: %s", devnm);
5589                 return (NDI_FAILURE);
5590         }
5591 
5592         if ((flags & (NDI_NO_EVENT | NDI_BRANCH_EVENT_OP)) == 0 &&
5593             (flags & NDI_CONFIG)) {
5594                 flags |= NDI_BRANCH_EVENT_OP;
5595                 branch_event = 1;
5596         }
5597 
5598         nmdup = strdup(devnm);
5599         duplen = strlen(devnm) + 1;
5600 
5601         if ((DEVI(pdip)->devi_ops->devo_bus_ops == NULL) ||
5602             (DEVI(pdip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
5603             (f = DEVI(pdip)->devi_ops->devo_bus_ops->bus_config) == NULL) {
5604                 error = devi_config_one(pdip, devnm, dipp, flags, 0);
5605         } else {
5606                 /* call bus_config entry point */
5607                 error = (*f)(pdip, flags, BUS_CONFIG_ONE, (void *)devnm, dipp);
5608         }
5609 
5610         if (error) {
5611                 *dipp = NULL;
5612         }
5613 
5614         /*
5615          * if we fail to lookup and this could be an alias, lookup currdip
5616          * To prevent recursive lookups into the same hash table, only
5617          * do the currdip lookups once the hash table init is complete.
5618          * Use tsd so that redirection doesn't recurse
5619          */
5620         if (error) {
5621                 char *alias = kmem_alloc(MAXPATHLEN, KM_NOSLEEP);
5622                 if (alias == NULL) {
5623                         ddi_err(DER_PANIC, pdip, "alias alloc failed: %s",
5624                             nmdup);
5625                 }
5626                 (void) ddi_pathname(pdip, alias);
5627                 (void) strlcat(alias, "/", MAXPATHLEN);
5628                 (void) strlcat(alias, nmdup, MAXPATHLEN);
5629 
5630                 *dipp = ddi_alias_redirect(alias);
5631                 error = (*dipp ? NDI_SUCCESS : NDI_FAILURE);
5632 
5633                 kmem_free(alias, MAXPATHLEN);
5634         }
5635         kmem_free(nmdup, duplen);
5636 
5637         if (error || !(flags & NDI_CONFIG)) {
5638                 pm_post_config(pdip, devnm);
5639                 return (error);
5640         }
5641 
5642         /*
5643          * DR usage (i.e. call with NDI_CONFIG) recursively configures
5644          * grandchildren, performing a BUS_CONFIG_ALL from the node attached
5645          * by the BUS_CONFIG_ONE.
5646          */
5647         ASSERT(*dipp);
5648         error = devi_config_common(*dipp, flags, DDI_MAJOR_T_NONE);
5649 
5650         pm_post_config(pdip, devnm);
5651 
5652         if (branch_event)
5653                 (void) i_log_devfs_branch_add(*dipp);
5654 
5655         return (error);
5656 }
5657 
5658 /*
5659  * Enumerate and attach a child specified by name 'devnm'.
5660  * Called during configure the OBP options. This configures
5661  * only one node.
5662  */
5663 static int
5664 ndi_devi_config_obp_args(dev_info_t *parent, char *devnm,
5665     dev_info_t **childp, int flags)
5666 {
5667         int error;
5668         int (*f)();
5669 
5670         ASSERT(childp);
5671         ASSERT(i_ddi_devi_attached(parent));
5672 
5673         NDI_CONFIG_DEBUG((CE_CONT, "ndi_devi_config_obp_args: "
5674             "par = %s%d (%p), child = %s\n", ddi_driver_name(parent),
5675             ddi_get_instance(parent), (void *)parent, devnm));
5676 
5677         if ((DEVI(parent)->devi_ops->devo_bus_ops == NULL) ||
5678             (DEVI(parent)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
5679             (f = DEVI(parent)->devi_ops->devo_bus_ops->bus_config) == NULL) {
5680                 error = NDI_FAILURE;
5681         } else {
5682                 /* call bus_config entry point */
5683                 error = (*f)(parent, flags,
5684                     BUS_CONFIG_OBP_ARGS, (void *)devnm, childp);
5685         }
5686         return (error);
5687 }
5688 
5689 /*
5690  * Pay attention, the following is a bit tricky:
5691  * There are three possible cases when constraints are applied
5692  *
5693  *      - A constraint is applied and the offline is disallowed.
5694  *        Simply return failure and block the offline
5695  *
5696  *      - A constraint is applied and the offline is allowed.
5697  *        Mark the dip as having passed the constraint and allow
5698  *        offline to proceed.
5699  *
5700  *      - A constraint is not applied. Allow the offline to proceed for now.
5701  *
5702  * In the latter two cases we allow the offline to proceed. If the
5703  * offline succeeds (no users) everything is fine. It is ok for an unused
5704  * device to be offlined even if no constraints were imposed on the offline.
5705  * If the offline fails because there are users, we look at the constraint
5706  * flag on the dip. If the constraint flag is set (implying that it passed
5707  * a constraint) we allow the dip to be retired. If not, we don't allow
5708  * the retire. This ensures that we don't allow unconstrained retire.
5709  */
5710 int
5711 e_ddi_offline_notify(dev_info_t *dip)
5712 {
5713         int retval;
5714         int constraint;
5715         int failure;
5716 
5717         RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): entered: dip=%p",
5718             (void *) dip));
5719 
5720         constraint = 0;
5721         failure = 0;
5722 
5723         /*
5724          * Start with userland constraints first - applied via device contracts
5725          */
5726         retval = contract_device_offline(dip, DDI_DEV_T_ANY, 0);
5727         switch (retval) {
5728         case CT_NACK:
5729                 RIO_DEBUG((CE_NOTE, "Received NACK for dip=%p", (void *)dip));
5730                 failure = 1;
5731                 goto out;
5732         case CT_ACK:
5733                 constraint = 1;
5734                 RIO_DEBUG((CE_NOTE, "Received ACK for dip=%p", (void *)dip));
5735                 break;
5736         case CT_NONE:
5737                 /* no contracts */
5738                 RIO_DEBUG((CE_NOTE, "No contracts on dip=%p", (void *)dip));
5739                 break;
5740         default:
5741                 ASSERT(retval == CT_NONE);
5742         }
5743 
5744         /*
5745          * Next, use LDI to impose kernel constraints
5746          */
5747         retval = ldi_invoke_notify(dip, DDI_DEV_T_ANY, 0, LDI_EV_OFFLINE, NULL);
5748         switch (retval) {
5749         case LDI_EV_FAILURE:
5750                 contract_device_negend(dip, DDI_DEV_T_ANY, 0, CT_EV_FAILURE);
5751                 RIO_DEBUG((CE_NOTE, "LDI callback failed on dip=%p",
5752                     (void *)dip));
5753                 failure = 1;
5754                 goto out;
5755         case LDI_EV_SUCCESS:
5756                 constraint = 1;
5757                 RIO_DEBUG((CE_NOTE, "LDI callback success on dip=%p",
5758                     (void *)dip));
5759                 break;
5760         case LDI_EV_NONE:
5761                 /* no matching LDI callbacks */
5762                 RIO_DEBUG((CE_NOTE, "No LDI callbacks for dip=%p",
5763                     (void *)dip));
5764                 break;
5765         default:
5766                 ASSERT(retval == LDI_EV_NONE);
5767         }
5768 
5769 out:
5770         mutex_enter(&(DEVI(dip)->devi_lock));
5771         if ((DEVI(dip)->devi_flags & DEVI_RETIRING) && failure) {
5772                 RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): setting "
5773                     "BLOCKED flag. dip=%p", (void *)dip));
5774                 DEVI(dip)->devi_flags |= DEVI_R_BLOCKED;
5775                 if (DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT) {
5776                         RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): "
5777                             "blocked. clearing RCM CONSTRAINT flag. dip=%p",
5778                             (void *)dip));
5779                         DEVI(dip)->devi_flags &= ~DEVI_R_CONSTRAINT;
5780                 }
5781         } else if ((DEVI(dip)->devi_flags & DEVI_RETIRING) && constraint) {
5782                 RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): setting "
5783                     "CONSTRAINT flag. dip=%p", (void *)dip));
5784                 DEVI(dip)->devi_flags |= DEVI_R_CONSTRAINT;
5785         } else if ((DEVI(dip)->devi_flags & DEVI_RETIRING) &&
5786             ((DEVI(dip)->devi_ops != NULL &&
5787             DEVI(dip)->devi_ops->devo_bus_ops != NULL) ||
5788             DEVI(dip)->devi_ref == 0)) {
5789                 /* also allow retire if nexus or if device is not in use */
5790                 RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): device not in "
5791                     "use. Setting CONSTRAINT flag. dip=%p", (void *)dip));
5792                 DEVI(dip)->devi_flags |= DEVI_R_CONSTRAINT;
5793         } else {
5794                 /*
5795                  * Note: We cannot ASSERT here that DEVI_R_CONSTRAINT is
5796                  * not set, since other sources (such as RCM) may have
5797                  * set the flag.
5798                  */
5799                 RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): not setting "
5800                     "constraint flag. dip=%p", (void *)dip));
5801         }
5802         mutex_exit(&(DEVI(dip)->devi_lock));
5803 
5804 
5805         RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): exit: dip=%p",
5806             (void *) dip));
5807 
5808         return (failure ? DDI_FAILURE : DDI_SUCCESS);
5809 }
5810 
5811 void
5812 e_ddi_offline_finalize(dev_info_t *dip, int result)
5813 {
5814         RIO_DEBUG((CE_NOTE, "e_ddi_offline_finalize(): entry: result=%s, "
5815             "dip=%p", result == DDI_SUCCESS ? "SUCCESS" : "FAILURE",
5816             (void *)dip));
5817 
5818         contract_device_negend(dip, DDI_DEV_T_ANY, 0,  result == DDI_SUCCESS ?
5819             CT_EV_SUCCESS : CT_EV_FAILURE);
5820 
5821         ldi_invoke_finalize(dip, DDI_DEV_T_ANY, 0,
5822             LDI_EV_OFFLINE, result == DDI_SUCCESS ?
5823             LDI_EV_SUCCESS : LDI_EV_FAILURE, NULL);
5824 
5825         RIO_VERBOSE((CE_NOTE, "e_ddi_offline_finalize(): exit: dip=%p",
5826             (void *)dip));
5827 }
5828 
5829 void
5830 e_ddi_degrade_finalize(dev_info_t *dip)
5831 {
5832         RIO_DEBUG((CE_NOTE, "e_ddi_degrade_finalize(): entry: "
5833             "result always = DDI_SUCCESS, dip=%p", (void *)dip));
5834 
5835         contract_device_degrade(dip, DDI_DEV_T_ANY, 0);
5836         contract_device_negend(dip, DDI_DEV_T_ANY, 0, CT_EV_SUCCESS);
5837 
5838         ldi_invoke_finalize(dip, DDI_DEV_T_ANY, 0, LDI_EV_DEGRADE,
5839             LDI_EV_SUCCESS, NULL);
5840 
5841         RIO_VERBOSE((CE_NOTE, "e_ddi_degrade_finalize(): exit: dip=%p",
5842             (void *)dip));
5843 }
5844 
5845 void
5846 e_ddi_undegrade_finalize(dev_info_t *dip)
5847 {
5848         RIO_DEBUG((CE_NOTE, "e_ddi_undegrade_finalize(): entry: "
5849             "result always = DDI_SUCCESS, dip=%p", (void *)dip));
5850 
5851         contract_device_undegrade(dip, DDI_DEV_T_ANY, 0);
5852         contract_device_negend(dip, DDI_DEV_T_ANY, 0, CT_EV_SUCCESS);
5853 
5854         RIO_VERBOSE((CE_NOTE, "e_ddi_undegrade_finalize(): exit: dip=%p",
5855             (void *)dip));
5856 }
5857 
5858 /*
5859  * detach a node with parent already held busy
5860  */
5861 static int
5862 devi_detach_node(dev_info_t *dip, uint_t flags)
5863 {
5864         dev_info_t *pdip = ddi_get_parent(dip);
5865         int ret = NDI_SUCCESS;
5866         ddi_eventcookie_t cookie;
5867         char *path = NULL;
5868         char *class = NULL;
5869         char *driver = NULL;
5870         int instance = -1;
5871         int post_event = 0;
5872 
5873         ASSERT(pdip && DEVI_BUSY_OWNED(pdip));
5874 
5875         /*
5876          * Invoke notify if offlining
5877          */
5878         if (flags & NDI_DEVI_OFFLINE) {
5879                 RIO_DEBUG((CE_NOTE, "devi_detach_node: offlining dip=%p",
5880                     (void *)dip));
5881                 if (e_ddi_offline_notify(dip) != DDI_SUCCESS) {
5882                         RIO_DEBUG((CE_NOTE, "devi_detach_node: offline NACKed"
5883                             "dip=%p", (void *)dip));
5884                         return (NDI_FAILURE);
5885                 }
5886         }
5887 
5888         if (flags & NDI_POST_EVENT) {
5889                 if (i_ddi_devi_attached(pdip)) {
5890                         if (ddi_get_eventcookie(dip, DDI_DEVI_REMOVE_EVENT,
5891                             &cookie) == NDI_SUCCESS)
5892                                 (void) ndi_post_event(dip, dip, cookie, NULL);
5893                 }
5894         }
5895 
5896         /*
5897          * dv_mknod places a hold on the dev_info_t for each devfs node
5898          * created.  If we're to succeed in detaching this device, we must
5899          * first release all outstanding references held by devfs.
5900          */
5901         (void) devfs_clean(pdip, NULL, DV_CLEAN_FORCE);
5902 
5903         if (i_ddi_detachchild(dip, flags) != DDI_SUCCESS) {
5904                 if (flags & NDI_DEVI_OFFLINE) {
5905                         RIO_DEBUG((CE_NOTE, "devi_detach_node: offline failed."
5906                             " Calling e_ddi_offline_finalize with result=%d. "
5907                             "dip=%p", DDI_FAILURE, (void *)dip));
5908                         e_ddi_offline_finalize(dip, DDI_FAILURE);
5909                 }
5910                 return (NDI_FAILURE);
5911         }
5912 
5913         if (flags & NDI_DEVI_OFFLINE) {
5914                 RIO_DEBUG((CE_NOTE, "devi_detach_node: offline succeeded."
5915                     " Calling e_ddi_offline_finalize with result=%d, "
5916                     "dip=%p", DDI_SUCCESS, (void *)dip));
5917                 e_ddi_offline_finalize(dip, DDI_SUCCESS);
5918         }
5919 
5920         if (flags & NDI_AUTODETACH)
5921                 return (NDI_SUCCESS);
5922 
5923         /*
5924          * For DR, even bound nodes may need to have offline
5925          * flag set.
5926          */
5927         if (flags & NDI_DEVI_OFFLINE) {
5928                 mutex_enter(&(DEVI(dip)->devi_lock));
5929                 DEVI_SET_DEVICE_OFFLINE(dip);
5930                 mutex_exit(&(DEVI(dip)->devi_lock));
5931         }
5932 
5933         if (i_ddi_node_state(dip) == DS_INITIALIZED) {
5934                 struct dev_info *devi = DEVI(dip);
5935 
5936                 if (devi->devi_ev_path == NULL) {
5937                         devi->devi_ev_path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
5938                         (void) ddi_pathname(dip, devi->devi_ev_path);
5939                 }
5940                 if (flags & NDI_DEVI_OFFLINE)
5941                         i_ndi_devi_report_status_change(dip,
5942                             devi->devi_ev_path);
5943 
5944                 if (need_remove_event(dip, flags)) {
5945                         /*
5946                          * instance and path data are lost in call to
5947                          * ddi_uninitchild
5948                          */
5949                         devi->devi_ev_instance = ddi_get_instance(dip);
5950 
5951                         mutex_enter(&(DEVI(dip)->devi_lock));
5952                         DEVI_SET_EVREMOVE(dip);
5953                         mutex_exit(&(DEVI(dip)->devi_lock));
5954                 }
5955         }
5956 
5957         if (flags & (NDI_UNCONFIG | NDI_DEVI_REMOVE)) {
5958                 ret = ddi_uninitchild(dip);
5959                 if (ret == NDI_SUCCESS) {
5960                         /*
5961                          * Remove uninitialized pseudo nodes because
5962                          * system props are lost and the node cannot be
5963                          * reattached.
5964                          */
5965                         if (!ndi_dev_is_persistent_node(dip))
5966                                 flags |= NDI_DEVI_REMOVE;
5967 
5968                         if (flags & NDI_DEVI_REMOVE) {
5969                                 /*
5970                                  * NOTE: If there is a consumer of LDI events,
5971                                  * ddi_uninitchild above would have failed
5972                                  * because of active devi_ref from ldi_open().
5973                                  */
5974 
5975                                 if (DEVI_EVREMOVE(dip)) {
5976                                         path = i_ddi_strdup(
5977                                             DEVI(dip)->devi_ev_path,
5978                                             KM_SLEEP);
5979                                         class =
5980                                             i_ddi_strdup(i_ddi_devi_class(dip),
5981                                             KM_SLEEP);
5982                                         driver =
5983                                             i_ddi_strdup(
5984                                             (char *)ddi_driver_name(dip),
5985                                             KM_SLEEP);
5986                                         instance = DEVI(dip)->devi_ev_instance;
5987                                         post_event = 1;
5988                                 }
5989 
5990                                 ret = ddi_remove_child(dip, 0);
5991                                 if (post_event && ret == NDI_SUCCESS) {
5992                                         /* Generate EC_DEVFS_DEVI_REMOVE */
5993                                         (void) i_log_devfs_remove_devinfo(path,
5994                                             class, driver, instance, flags);
5995                                 }
5996                         }
5997 
5998                 }
5999         }
6000 
6001         if (path)
6002                 strfree(path);
6003         if (class)
6004                 strfree(class);
6005         if (driver)
6006                 strfree(driver);
6007 
6008         return (ret);
6009 }
6010 
6011 /*
6012  * unconfigure immediate children of bus nexus device
6013  */
6014 static int
6015 unconfig_immediate_children(
6016         dev_info_t *dip,
6017         dev_info_t **dipp,
6018         int flags,
6019         major_t major)
6020 {
6021         int rv = NDI_SUCCESS;
6022         int circ, vcirc;
6023         dev_info_t *child;
6024         dev_info_t *vdip = NULL;
6025         dev_info_t *next;
6026 
6027         ASSERT(dipp == NULL || *dipp == NULL);
6028 
6029         /*
6030          * Scan forward to see if we will be processing a pHCI child. If we
6031          * have a child that is a pHCI and vHCI and pHCI are not siblings then
6032          * enter vHCI before parent(pHCI) to prevent deadlock with mpxio
6033          * Client power management operations.
6034          */
6035         ndi_devi_enter(dip, &circ);
6036         for (child = ddi_get_child(dip); child;
6037             child = ddi_get_next_sibling(child)) {
6038                 /* skip same nodes we skip below */
6039                 if (((major != DDI_MAJOR_T_NONE) &&
6040                     (major != ddi_driver_major(child))) ||
6041                     ((flags & NDI_AUTODETACH) && !is_leaf_node(child)))
6042                         continue;
6043 
6044                 if (MDI_PHCI(child)) {
6045                         vdip = mdi_devi_get_vdip(child);
6046                         /*
6047                          * If vHCI and vHCI is not a sibling of pHCI
6048                          * then enter in (vHCI, parent(pHCI)) order.
6049                          */
6050                         if (vdip && (ddi_get_parent(vdip) != dip)) {
6051                                 ndi_devi_exit(dip, circ);
6052 
6053                                 /* use mdi_devi_enter ordering */
6054                                 ndi_devi_enter(vdip, &vcirc);
6055                                 ndi_devi_enter(dip, &circ);
6056                                 break;
6057                         } else
6058                                 vdip = NULL;
6059                 }
6060         }
6061 
6062         child = ddi_get_child(dip);
6063         while (child) {
6064                 next = ddi_get_next_sibling(child);
6065 
6066                 if ((major != DDI_MAJOR_T_NONE) &&
6067                     (major != ddi_driver_major(child))) {
6068                         child = next;
6069                         continue;
6070                 }
6071 
6072                 /* skip nexus nodes during autodetach */
6073                 if ((flags & NDI_AUTODETACH) && !is_leaf_node(child)) {
6074                         child = next;
6075                         continue;
6076                 }
6077 
6078                 if (devi_detach_node(child, flags) != NDI_SUCCESS) {
6079                         if (dipp && *dipp == NULL) {
6080                                 ndi_hold_devi(child);
6081                                 *dipp = child;
6082                         }
6083                         rv = NDI_FAILURE;
6084                 }
6085 
6086                 /*
6087                  * Continue upon failure--best effort algorithm
6088                  */
6089                 child = next;
6090         }
6091 
6092         ndi_devi_exit(dip, circ);
6093         if (vdip)
6094                 ndi_devi_exit(vdip, vcirc);
6095 
6096         return (rv);
6097 }
6098 
6099 /*
6100  * unconfigure grand children of bus nexus device
6101  */
6102 static int
6103 unconfig_grand_children(
6104         dev_info_t *dip,
6105         dev_info_t **dipp,
6106         int flags,
6107         major_t major,
6108         struct brevq_node **brevqp)
6109 {
6110         struct mt_config_handle *hdl;
6111 
6112         if (brevqp)
6113                 *brevqp = NULL;
6114 
6115         /* multi-threaded configuration of child nexus */
6116         hdl = mt_config_init(dip, dipp, flags, major, MT_UNCONFIG_OP, brevqp);
6117         mt_config_children(hdl);
6118 
6119         return (mt_config_fini(hdl));   /* wait for threads to exit */
6120 }
6121 
6122 /*
6123  * Unconfigure children/descendants of the dip.
6124  *
6125  * If brevqp is not NULL, on return *brevqp is set to a queue of dip's
6126  * child devinames for which branch remove events need to be generated.
6127  */
6128 static int
6129 devi_unconfig_common(
6130         dev_info_t *dip,
6131         dev_info_t **dipp,
6132         int flags,
6133         major_t major,
6134         struct brevq_node **brevqp)
6135 {
6136         int rv;
6137         int pm_cookie;
6138         int (*f)();
6139         ddi_bus_config_op_t bus_op;
6140 
6141         if (dipp)
6142                 *dipp = NULL;
6143         if (brevqp)
6144                 *brevqp = NULL;
6145 
6146         /*
6147          * Power up the dip if it is powered off.  If the flag bit
6148          * NDI_AUTODETACH is set and the dip is not at its full power,
6149          * skip the rest of the branch.
6150          */
6151         if (pm_pre_unconfig(dip, flags, &pm_cookie, NULL) != DDI_SUCCESS)
6152                 return ((flags & NDI_AUTODETACH) ? NDI_SUCCESS :
6153                     NDI_FAILURE);
6154 
6155         /*
6156          * Some callers, notably SCSI, need to clear out the devfs
6157          * cache together with the unconfig to prevent stale entries.
6158          */
6159         if (flags & NDI_DEVFS_CLEAN)
6160                 (void) devfs_clean(dip, NULL, 0);
6161 
6162         rv = unconfig_grand_children(dip, dipp, flags, major, brevqp);
6163 
6164         if ((rv != NDI_SUCCESS) && ((flags & NDI_AUTODETACH) == 0)) {
6165                 if (brevqp && *brevqp) {
6166                         log_and_free_br_events_on_grand_children(dip, *brevqp);
6167                         free_brevq(*brevqp);
6168                         *brevqp = NULL;
6169                 }
6170                 pm_post_unconfig(dip, pm_cookie, NULL);
6171                 return (rv);
6172         }
6173 
6174         if (dipp && *dipp) {
6175                 ndi_rele_devi(*dipp);
6176                 *dipp = NULL;
6177         }
6178 
6179         /*
6180          * It is possible to have a detached nexus with children
6181          * and grandchildren (for example: a branch consisting
6182          * entirely of bound nodes.) Since the nexus is detached
6183          * the bus_unconfig entry point cannot be used to remove
6184          * or unconfigure the descendants.
6185          */
6186         if (!i_ddi_devi_attached(dip) ||
6187             (DEVI(dip)->devi_ops->devo_bus_ops == NULL) ||
6188             (DEVI(dip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
6189             (f = DEVI(dip)->devi_ops->devo_bus_ops->bus_unconfig) == NULL) {
6190                 rv = unconfig_immediate_children(dip, dipp, flags, major);
6191         } else {
6192                 /*
6193                  * call bus_unconfig entry point
6194                  * It should reset nexus flags if unconfigure succeeds.
6195                  */
6196                 bus_op = (major == DDI_MAJOR_T_NONE) ?
6197                     BUS_UNCONFIG_ALL : BUS_UNCONFIG_DRIVER;
6198                 rv = (*f)(dip, flags, bus_op, (void *)(uintptr_t)major);
6199         }
6200 
6201         pm_post_unconfig(dip, pm_cookie, NULL);
6202 
6203         if (brevqp && *brevqp)
6204                 cleanup_br_events_on_grand_children(dip, brevqp);
6205 
6206         return (rv);
6207 }
6208 
6209 /*
6210  * called by devfs/framework to unconfigure children bound to major
6211  * If NDI_AUTODETACH is specified, this is invoked by either the
6212  * moduninstall daemon or the modunload -i 0 command.
6213  */
6214 int
6215 ndi_devi_unconfig_driver(dev_info_t *dip, int flags, major_t major)
6216 {
6217         NDI_CONFIG_DEBUG((CE_CONT,
6218             "ndi_devi_unconfig_driver: par = %s%d (%p), flags = 0x%x\n",
6219             ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
6220 
6221         return (devi_unconfig_common(dip, NULL, flags, major, NULL));
6222 }
6223 
6224 int
6225 ndi_devi_unconfig(dev_info_t *dip, int flags)
6226 {
6227         NDI_CONFIG_DEBUG((CE_CONT,
6228             "ndi_devi_unconfig: par = %s%d (%p), flags = 0x%x\n",
6229             ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
6230 
6231         return (devi_unconfig_common(dip, NULL, flags, DDI_MAJOR_T_NONE, NULL));
6232 }
6233 
6234 int
6235 e_ddi_devi_unconfig(dev_info_t *dip, dev_info_t **dipp, int flags)
6236 {
6237         NDI_CONFIG_DEBUG((CE_CONT,
6238             "e_ddi_devi_unconfig: par = %s%d (%p), flags = 0x%x\n",
6239             ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
6240 
6241         return (devi_unconfig_common(dip, dipp, flags, DDI_MAJOR_T_NONE, NULL));
6242 }
6243 
6244 /*
6245  * Unconfigure child by name
6246  */
6247 static int
6248 devi_unconfig_one(dev_info_t *pdip, char *devnm, int flags)
6249 {
6250         int             rv, circ;
6251         dev_info_t      *child;
6252         dev_info_t      *vdip = NULL;
6253         int             v_circ;
6254 
6255         ndi_devi_enter(pdip, &circ);
6256         child = ndi_devi_findchild(pdip, devnm);
6257 
6258         /*
6259          * If child is pHCI and vHCI and pHCI are not siblings then enter vHCI
6260          * before parent(pHCI) to avoid deadlock with mpxio Client power
6261          * management operations.
6262          */
6263         if (child && MDI_PHCI(child)) {
6264                 vdip = mdi_devi_get_vdip(child);
6265                 if (vdip && (ddi_get_parent(vdip) != pdip)) {
6266                         ndi_devi_exit(pdip, circ);
6267 
6268                         /* use mdi_devi_enter ordering */
6269                         ndi_devi_enter(vdip, &v_circ);
6270                         ndi_devi_enter(pdip, &circ);
6271                         child = ndi_devi_findchild(pdip, devnm);
6272                 } else
6273                         vdip = NULL;
6274         }
6275 
6276         if (child) {
6277                 rv = devi_detach_node(child, flags);
6278         } else {
6279                 NDI_CONFIG_DEBUG((CE_CONT,
6280                     "devi_unconfig_one: %s not found\n", devnm));
6281                 rv = NDI_SUCCESS;
6282         }
6283 
6284         ndi_devi_exit(pdip, circ);
6285         if (vdip)
6286                 ndi_devi_exit(vdip, v_circ);
6287 
6288         return (rv);
6289 }
6290 
6291 int
6292 ndi_devi_unconfig_one(
6293         dev_info_t *pdip,
6294         char *devnm,
6295         dev_info_t **dipp,
6296         int flags)
6297 {
6298         int             (*f)();
6299         int             circ, rv;
6300         int             pm_cookie;
6301         dev_info_t      *child;
6302         dev_info_t      *vdip = NULL;
6303         int             v_circ;
6304         struct brevq_node *brevq = NULL;
6305 
6306         ASSERT(i_ddi_devi_attached(pdip));
6307 
6308         NDI_CONFIG_DEBUG((CE_CONT,
6309             "ndi_devi_unconfig_one: par = %s%d (%p), child = %s\n",
6310             ddi_driver_name(pdip), ddi_get_instance(pdip),
6311             (void *)pdip, devnm));
6312 
6313         if (pm_pre_unconfig(pdip, flags, &pm_cookie, devnm) != DDI_SUCCESS)
6314                 return (NDI_FAILURE);
6315 
6316         if (dipp)
6317                 *dipp = NULL;
6318 
6319         ndi_devi_enter(pdip, &circ);
6320         child = ndi_devi_findchild(pdip, devnm);
6321 
6322         /*
6323          * If child is pHCI and vHCI and pHCI are not siblings then enter vHCI
6324          * before parent(pHCI) to avoid deadlock with mpxio Client power
6325          * management operations.
6326          */
6327         if (child && MDI_PHCI(child)) {
6328                 vdip = mdi_devi_get_vdip(child);
6329                 if (vdip && (ddi_get_parent(vdip) != pdip)) {
6330                         ndi_devi_exit(pdip, circ);
6331 
6332                         /* use mdi_devi_enter ordering */
6333                         ndi_devi_enter(vdip, &v_circ);
6334                         ndi_devi_enter(pdip, &circ);
6335                         child = ndi_devi_findchild(pdip, devnm);
6336                 } else
6337                         vdip = NULL;
6338         }
6339 
6340         if (child == NULL) {
6341                 NDI_CONFIG_DEBUG((CE_CONT, "ndi_devi_unconfig_one: %s"
6342                     " not found\n", devnm));
6343                 rv = NDI_SUCCESS;
6344                 goto out;
6345         }
6346 
6347         /*
6348          * Unconfigure children/descendants of named child
6349          */
6350         rv = devi_unconfig_branch(child, dipp, flags | NDI_UNCONFIG, &brevq);
6351         if (rv != NDI_SUCCESS)
6352                 goto out;
6353 
6354         init_bound_node_ev(pdip, child, flags);
6355 
6356         if ((DEVI(pdip)->devi_ops->devo_bus_ops == NULL) ||
6357             (DEVI(pdip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
6358             (f = DEVI(pdip)->devi_ops->devo_bus_ops->bus_unconfig) == NULL) {
6359                 rv = devi_detach_node(child, flags);
6360         } else {
6361                 /* call bus_config entry point */
6362                 rv = (*f)(pdip, flags, BUS_UNCONFIG_ONE, (void *)devnm);
6363         }
6364 
6365         if (brevq) {
6366                 if (rv != NDI_SUCCESS)
6367                         log_and_free_brevq_dip(child, brevq);
6368                 else
6369                         free_brevq(brevq);
6370         }
6371 
6372         if (dipp && rv != NDI_SUCCESS) {
6373                 ndi_hold_devi(child);
6374                 ASSERT(*dipp == NULL);
6375                 *dipp = child;
6376         }
6377 
6378 out:
6379         ndi_devi_exit(pdip, circ);
6380         if (vdip)
6381                 ndi_devi_exit(vdip, v_circ);
6382 
6383         pm_post_unconfig(pdip, pm_cookie, devnm);
6384 
6385         return (rv);
6386 }
6387 
6388 struct async_arg {
6389         dev_info_t *dip;
6390         uint_t flags;
6391 };
6392 
6393 /*
6394  * Common async handler for:
6395  *      ndi_devi_bind_driver_async
6396  *      ndi_devi_online_async
6397  */
6398 static int
6399 i_ndi_devi_async_common(dev_info_t *dip, uint_t flags, void (*func)())
6400 {
6401         int tqflag;
6402         int kmflag;
6403         struct async_arg *arg;
6404         dev_info_t *pdip = ddi_get_parent(dip);
6405 
6406         ASSERT(pdip);
6407         ASSERT(DEVI(pdip)->devi_taskq);
6408         ASSERT(ndi_dev_is_persistent_node(dip));
6409 
6410         if (flags & NDI_NOSLEEP) {
6411                 kmflag = KM_NOSLEEP;
6412                 tqflag = TQ_NOSLEEP;
6413         } else {
6414                 kmflag = KM_SLEEP;
6415                 tqflag = TQ_SLEEP;
6416         }
6417 
6418         arg = kmem_alloc(sizeof (*arg), kmflag);
6419         if (arg == NULL)
6420                 goto fail;
6421 
6422         arg->flags = flags;
6423         arg->dip = dip;
6424         if (ddi_taskq_dispatch(DEVI(pdip)->devi_taskq, func, arg, tqflag) ==
6425             DDI_SUCCESS) {
6426                 return (NDI_SUCCESS);
6427         }
6428 
6429 fail:
6430         NDI_CONFIG_DEBUG((CE_CONT, "%s%d: ddi_taskq_dispatch failed",
6431             ddi_driver_name(pdip), ddi_get_instance(pdip)));
6432 
6433         if (arg)
6434                 kmem_free(arg, sizeof (*arg));
6435         return (NDI_FAILURE);
6436 }
6437 
6438 static void
6439 i_ndi_devi_bind_driver_cb(struct async_arg *arg)
6440 {
6441         (void) ndi_devi_bind_driver(arg->dip, arg->flags);
6442         kmem_free(arg, sizeof (*arg));
6443 }
6444 
6445 int
6446 ndi_devi_bind_driver_async(dev_info_t *dip, uint_t flags)
6447 {
6448         return (i_ndi_devi_async_common(dip, flags,
6449             (void (*)())i_ndi_devi_bind_driver_cb));
6450 }
6451 
6452 /*
6453  * place the devinfo in the ONLINE state.
6454  */
6455 int
6456 ndi_devi_online(dev_info_t *dip, uint_t flags)
6457 {
6458         int circ, rv;
6459         dev_info_t *pdip = ddi_get_parent(dip);
6460         int branch_event = 0;
6461 
6462         ASSERT(pdip);
6463 
6464         NDI_CONFIG_DEBUG((CE_CONT, "ndi_devi_online: %s%d (%p)\n",
6465             ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip));
6466 
6467         ndi_devi_enter(pdip, &circ);
6468         /* bind child before merging .conf nodes */
6469         rv = i_ndi_config_node(dip, DS_BOUND, flags);
6470         if (rv != NDI_SUCCESS) {
6471                 ndi_devi_exit(pdip, circ);
6472                 return (rv);
6473         }
6474 
6475         /* merge .conf properties */
6476         (void) i_ndi_make_spec_children(pdip, flags);
6477 
6478         flags |= (NDI_DEVI_ONLINE | NDI_CONFIG);
6479 
6480         if (flags & NDI_NO_EVENT) {
6481                 /*
6482                  * Caller is specifically asking for not to generate an event.
6483                  * Set the following flag so that devi_attach_node() don't
6484                  * change the event state.
6485                  */
6486                 flags |= NDI_NO_EVENT_STATE_CHNG;
6487         }
6488 
6489         if ((flags & (NDI_NO_EVENT | NDI_BRANCH_EVENT_OP)) == 0 &&
6490             ((flags & NDI_CONFIG) || DEVI_NEED_NDI_CONFIG(dip))) {
6491                 flags |= NDI_BRANCH_EVENT_OP;
6492                 branch_event = 1;
6493         }
6494 
6495         /*
6496          * devi_attach_node() may remove dip on failure
6497          */
6498         if ((rv = devi_attach_node(dip, flags)) == NDI_SUCCESS) {
6499                 if ((flags & NDI_CONFIG) || DEVI_NEED_NDI_CONFIG(dip)) {
6500                         /*
6501                          * Hold the attached dip, and exit the parent while
6502                          * we drive configuration of children below the
6503                          * attached dip.
6504                          */
6505                         ndi_hold_devi(dip);
6506                         ndi_devi_exit(pdip, circ);
6507 
6508                         (void) ndi_devi_config(dip, flags);
6509 
6510                         ndi_devi_enter(pdip, &circ);
6511                         ndi_rele_devi(dip);
6512                 }
6513 
6514                 if (branch_event)
6515                         (void) i_log_devfs_branch_add(dip);
6516         }
6517 
6518         ndi_devi_exit(pdip, circ);
6519 
6520         /*
6521          * Notify devfs that we have a new node. Devfs needs to invalidate
6522          * cached directory contents.
6523          *
6524          * For PCMCIA devices, it is possible the pdip is not fully
6525          * attached. In this case, calling back into devfs will
6526          * result in a loop or assertion error. Hence, the check
6527          * on node state.
6528          *
6529          * If we own parent lock, this is part of a branch operation.
6530          * We skip the devfs_clean() step because the cache invalidation
6531          * is done higher up in the device tree.
6532          */
6533         if (rv == NDI_SUCCESS && i_ddi_devi_attached(pdip) &&
6534             !DEVI_BUSY_OWNED(pdip))
6535                 (void) devfs_clean(pdip, NULL, 0);
6536         return (rv);
6537 }
6538 
6539 static void
6540 i_ndi_devi_online_cb(struct async_arg *arg)
6541 {
6542         (void) ndi_devi_online(arg->dip, arg->flags);
6543         kmem_free(arg, sizeof (*arg));
6544 }
6545 
6546 int
6547 ndi_devi_online_async(dev_info_t *dip, uint_t flags)
6548 {
6549         /* mark child as need config if requested. */
6550         if (flags & NDI_CONFIG) {
6551                 mutex_enter(&(DEVI(dip)->devi_lock));
6552                 DEVI_SET_NDI_CONFIG(dip);
6553                 mutex_exit(&(DEVI(dip)->devi_lock));
6554         }
6555 
6556         return (i_ndi_devi_async_common(dip, flags,
6557             (void (*)())i_ndi_devi_online_cb));
6558 }
6559 
6560 /*
6561  * Take a device node Offline
6562  * To take a device Offline means to detach the device instance from
6563  * the driver and prevent devfs requests from re-attaching the device
6564  * instance.
6565  *
6566  * The flag NDI_DEVI_REMOVE causes removes the device node from
6567  * the driver list and the device tree. In this case, the device
6568  * is assumed to be removed from the system.
6569  */
6570 int
6571 ndi_devi_offline(dev_info_t *dip, uint_t flags)
6572 {
6573         int             circ, rval = 0;
6574         dev_info_t      *pdip = ddi_get_parent(dip);
6575         dev_info_t      *vdip = NULL;
6576         int             v_circ;
6577         struct brevq_node *brevq = NULL;
6578 
6579         ASSERT(pdip);
6580 
6581         flags |= NDI_DEVI_OFFLINE;
6582 
6583         /*
6584          * If child is pHCI and vHCI and pHCI are not siblings then enter vHCI
6585          * before parent(pHCI) to avoid deadlock with mpxio Client power
6586          * management operations.
6587          */
6588         if (MDI_PHCI(dip)) {
6589                 vdip = mdi_devi_get_vdip(dip);
6590                 if (vdip && (ddi_get_parent(vdip) != pdip))
6591                         ndi_devi_enter(vdip, &v_circ);
6592                 else
6593                         vdip = NULL;
6594         }
6595         ndi_devi_enter(pdip, &circ);
6596 
6597         if (i_ddi_devi_attached(dip)) {
6598                 /*
6599                  * If dip is in DS_READY state, there may be cached dv_nodes
6600                  * referencing this dip, so we invoke devfs code path.
6601                  * Note that we must release busy changing on pdip to
6602                  * avoid deadlock against devfs.
6603                  */
6604                 char *devname = kmem_alloc(MAXNAMELEN + 1, KM_SLEEP);
6605                 (void) ddi_deviname(dip, devname);
6606 
6607                 ndi_devi_exit(pdip, circ);
6608                 if (vdip)
6609                         ndi_devi_exit(vdip, v_circ);
6610 
6611                 /*
6612                  * If we are explictly told to clean, then clean. If we own the
6613                  * parent lock then this is part of a branch operation, and we
6614                  * skip the devfs_clean() step.
6615                  *
6616                  * NOTE: A thread performing a devfs file system lookup/
6617                  * bus_config can't call devfs_clean to unconfig without
6618                  * causing rwlock problems in devfs. For ndi_devi_offline, this
6619                  * means that the NDI_DEVFS_CLEAN flag is safe from ioctl code
6620                  * or from an async hotplug thread, but is not safe from a
6621                  * nexus driver's bus_config implementation.
6622                  */
6623                 if ((flags & NDI_DEVFS_CLEAN) ||
6624                     (!DEVI_BUSY_OWNED(pdip)))
6625                         (void) devfs_clean(pdip, devname + 1, DV_CLEAN_FORCE);
6626 
6627                 kmem_free(devname, MAXNAMELEN + 1);
6628 
6629                 rval = devi_unconfig_branch(dip, NULL, flags|NDI_UNCONFIG,
6630                     &brevq);
6631 
6632                 if (rval)
6633                         return (NDI_FAILURE);
6634 
6635                 if (vdip)
6636                         ndi_devi_enter(vdip, &v_circ);
6637                 ndi_devi_enter(pdip, &circ);
6638         }
6639 
6640         init_bound_node_ev(pdip, dip, flags);
6641 
6642         rval = devi_detach_node(dip, flags);
6643         if (brevq) {
6644                 if (rval != NDI_SUCCESS)
6645                         log_and_free_brevq_dip(dip, brevq);
6646                 else
6647                         free_brevq(brevq);
6648         }
6649 
6650         ndi_devi_exit(pdip, circ);
6651         if (vdip)
6652                 ndi_devi_exit(vdip, v_circ);
6653 
6654         return (rval);
6655 }
6656 
6657 /*
6658  * Find the child dev_info node of parent nexus 'p' whose unit address
6659  * matches "cname@caddr".  Recommend use of ndi_devi_findchild() instead.
6660  */
6661 dev_info_t *
6662 ndi_devi_find(dev_info_t *pdip, char *cname, char *caddr)
6663 {
6664         dev_info_t *child;
6665         int circ;
6666 
6667         if (pdip == NULL || cname == NULL || caddr == NULL)
6668                 return ((dev_info_t *)NULL);
6669 
6670         ndi_devi_enter(pdip, &circ);
6671         child = find_sibling(ddi_get_child(pdip), cname, caddr,
6672             FIND_NODE_BY_NODENAME, NULL);
6673         ndi_devi_exit(pdip, circ);
6674         return (child);
6675 }
6676 
6677 /*
6678  * Find the child dev_info node of parent nexus 'p' whose unit address
6679  * matches devname "name@addr".  Permits caller to hold the parent.
6680  */
6681 dev_info_t *
6682 ndi_devi_findchild(dev_info_t *pdip, char *devname)
6683 {
6684         dev_info_t *child;
6685         char    *cname, *caddr;
6686         char    *devstr;
6687 
6688         ASSERT(DEVI_BUSY_OWNED(pdip));
6689 
6690         devstr = i_ddi_strdup(devname, KM_SLEEP);
6691         i_ddi_parse_name(devstr, &cname, &caddr, NULL);
6692 
6693         if (cname == NULL || caddr == NULL) {
6694                 kmem_free(devstr, strlen(devname)+1);
6695                 return ((dev_info_t *)NULL);
6696         }
6697 
6698         child = find_sibling(ddi_get_child(pdip), cname, caddr,
6699             FIND_NODE_BY_NODENAME, NULL);
6700         kmem_free(devstr, strlen(devname)+1);
6701         return (child);
6702 }
6703 
6704 /*
6705  * Misc. routines called by framework only
6706  */
6707 
6708 /*
6709  * Clear the DEVI_MADE_CHILDREN/DEVI_ATTACHED_CHILDREN flags
6710  * if new child spec has been added.
6711  */
6712 static int
6713 reset_nexus_flags(dev_info_t *dip, void *arg)
6714 {
6715         struct hwc_spec *list;
6716         int             circ;
6717 
6718         if (((DEVI(dip)->devi_flags & DEVI_MADE_CHILDREN) == 0) ||
6719             ((list = hwc_get_child_spec(dip, (major_t)(uintptr_t)arg)) == NULL))
6720                 return (DDI_WALK_CONTINUE);
6721 
6722         hwc_free_spec_list(list);
6723 
6724         /* coordinate child state update */
6725         ndi_devi_enter(dip, &circ);
6726         mutex_enter(&DEVI(dip)->devi_lock);
6727         DEVI(dip)->devi_flags &= ~(DEVI_MADE_CHILDREN | DEVI_ATTACHED_CHILDREN);
6728         mutex_exit(&DEVI(dip)->devi_lock);
6729         ndi_devi_exit(dip, circ);
6730 
6731         return (DDI_WALK_CONTINUE);
6732 }
6733 
6734 /*
6735  * Helper functions, returns NULL if no memory.
6736  */
6737 
6738 /*
6739  * path_to_major:
6740  *
6741  * Return an alternate driver name binding for the leaf device
6742  * of the given pathname, if there is one. The purpose of this
6743  * function is to deal with generic pathnames. The default action
6744  * for platforms that can't do this (ie: x86 or any platform that
6745  * does not have prom_finddevice functionality, which matches
6746  * nodenames and unit-addresses without the drivers participation)
6747  * is to return DDI_MAJOR_T_NONE.
6748  *
6749  * Used in loadrootmodules() in the swapgeneric module to
6750  * associate a given pathname with a given leaf driver.
6751  *
6752  */
6753 major_t
6754 path_to_major(char *path)
6755 {
6756         dev_info_t *dip;
6757         char *p, *q;
6758         pnode_t nodeid;
6759         major_t major;
6760 
6761         /* check for path-oriented alias */
6762         major = ddi_name_to_major(path);
6763         if (driver_active(major)) {
6764                 NDI_CONFIG_DEBUG((CE_NOTE, "path_to_major: %s path bound %s\n",
6765                     path, ddi_major_to_name(major)));
6766                 return (major);
6767         }
6768 
6769         /*
6770          * Get the nodeid of the given pathname, if such a mapping exists.
6771          */
6772         dip = NULL;
6773         nodeid = prom_finddevice(path);
6774         if (nodeid != OBP_BADNODE) {
6775                 /*
6776                  * Find the nodeid in our copy of the device tree and return
6777                  * whatever name we used to bind this node to a driver.
6778                  */
6779                 dip = e_ddi_nodeid_to_dip(nodeid);
6780         }
6781 
6782         if (dip == NULL) {
6783                 NDI_CONFIG_DEBUG((CE_WARN,
6784                     "path_to_major: can't bind <%s>\n", path));
6785                 return (DDI_MAJOR_T_NONE);
6786         }
6787 
6788         /*
6789          * If we're bound to something other than the nodename,
6790          * note that in the message buffer and system log.
6791          */
6792         p = ddi_binding_name(dip);
6793         q = ddi_node_name(dip);
6794         if (p && q && (strcmp(p, q) != 0))
6795                 NDI_CONFIG_DEBUG((CE_NOTE, "path_to_major: %s bound to %s\n",
6796                     path, p));
6797 
6798         major = ddi_name_to_major(p);
6799 
6800         ndi_rele_devi(dip);             /* release e_ddi_nodeid_to_dip hold */
6801 
6802         return (major);
6803 }
6804 
6805 /*
6806  * Return the held dip for the specified major and instance, attempting to do
6807  * an attach if specified. Return NULL if the devi can't be found or put in
6808  * the proper state. The caller must release the hold via ddi_release_devi if
6809  * a non-NULL value is returned.
6810  *
6811  * Some callers expect to be able to perform a hold_devi() while in a context
6812  * where using ndi_devi_enter() to ensure the hold might cause deadlock (see
6813  * open-from-attach code in consconfig_dacf.c). Such special-case callers
6814  * must ensure that an ndi_devi_enter(parent)/ndi_hold_devi() from a safe
6815  * context is already active. The hold_devi() implementation must accommodate
6816  * these callers.
6817  */
6818 static dev_info_t *
6819 hold_devi(major_t major, int instance, int flags)
6820 {
6821         struct devnames *dnp;
6822         dev_info_t      *dip;
6823         char            *path;
6824         char            *vpath;
6825 
6826         if ((major >= devcnt) || (instance == -1))
6827                 return (NULL);
6828 
6829         /* try to find the instance in the per driver list */
6830         dnp = &(devnamesp[major]);
6831         LOCK_DEV_OPS(&(dnp->dn_lock));
6832         for (dip = dnp->dn_head; dip;
6833             dip = (dev_info_t *)DEVI(dip)->devi_next) {
6834                 /* skip node if instance field is not valid */
6835                 if (i_ddi_node_state(dip) < DS_INITIALIZED)
6836                         continue;
6837 
6838                 /* look for instance match */
6839                 if (DEVI(dip)->devi_instance == instance) {
6840                         /*
6841                          * To accommodate callers that can't block in
6842                          * ndi_devi_enter() we do an ndi_hold_devi(), and
6843                          * afterwards check that the node is in a state where
6844                          * the hold prevents detach(). If we did not manage to
6845                          * prevent detach then we ndi_rele_devi() and perform
6846                          * the slow path below (which can result in a blocking
6847                          * ndi_devi_enter() while driving attach top-down).
6848                          * This code depends on the ordering of
6849                          * DEVI_SET_DETACHING and the devi_ref check in the
6850                          * detach_node() code path.
6851                          */
6852                         ndi_hold_devi(dip);
6853                         if (i_ddi_devi_attached(dip) &&
6854                             !DEVI_IS_DETACHING(dip)) {
6855                                 UNLOCK_DEV_OPS(&(dnp->dn_lock));
6856                                 return (dip);   /* fast-path with devi held */
6857                         }
6858                         ndi_rele_devi(dip);
6859 
6860                         /* try slow-path */
6861                         dip = NULL;
6862                         break;
6863                 }
6864         }
6865         ASSERT(dip == NULL);
6866         UNLOCK_DEV_OPS(&(dnp->dn_lock));
6867 
6868         if (flags & E_DDI_HOLD_DEVI_NOATTACH)
6869                 return (NULL);          /* told not to drive attach */
6870 
6871         /* slow-path may block, so it should not occur from interrupt */
6872         ASSERT(!servicing_interrupt());
6873         if (servicing_interrupt())
6874                 return (NULL);
6875 
6876         /* reconstruct the path and drive attach by path through devfs. */
6877         path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
6878         if (e_ddi_majorinstance_to_path(major, instance, path) == 0) {
6879                 dip = e_ddi_hold_devi_by_path(path, flags);
6880 
6881                 /*
6882                  * Verify that we got the correct device - a path_to_inst file
6883                  * with a bogus/corrupt path (or a nexus that changes its
6884                  * unit-address format) could result in an incorrect answer
6885                  *
6886                  * Verify major, instance, and path.
6887                  */
6888                 vpath = kmem_alloc(MAXPATHLEN, KM_SLEEP);
6889                 if (dip &&
6890                     ((DEVI(dip)->devi_major != major) ||
6891                     ((DEVI(dip)->devi_instance != instance)) ||
6892                     (strcmp(path, ddi_pathname(dip, vpath)) != 0))) {
6893                         ndi_rele_devi(dip);
6894                         dip = NULL;     /* no answer better than wrong answer */
6895                 }
6896                 kmem_free(vpath, MAXPATHLEN);
6897         }
6898         kmem_free(path, MAXPATHLEN);
6899         return (dip);                   /* with devi held */
6900 }
6901 
6902 /*
6903  * The {e_}ddi_hold_devi{_by_{instance|dev|path}} hold the devinfo node
6904  * associated with the specified arguments.  This hold should be released
6905  * by calling ddi_release_devi.
6906  *
6907  * The E_DDI_HOLD_DEVI_NOATTACH flag argument allows the caller to to specify
6908  * a failure return if the node is not already attached.
6909  *
6910  * NOTE: by the time we make e_ddi_hold_devi public, we should be able to reuse
6911  * ddi_hold_devi again.
6912  */
6913 dev_info_t *
6914 ddi_hold_devi_by_instance(major_t major, int instance, int flags)
6915 {
6916         return (hold_devi(major, instance, flags));
6917 }
6918 
6919 dev_info_t *
6920 e_ddi_hold_devi_by_dev(dev_t dev, int flags)
6921 {
6922         major_t major = getmajor(dev);
6923         dev_info_t      *dip;
6924         struct dev_ops  *ops;
6925         dev_info_t      *ddip = NULL;
6926 
6927         dip = hold_devi(major, dev_to_instance(dev), flags);
6928 
6929         /*
6930          * The rest of this routine is legacy support for drivers that
6931          * have broken DDI_INFO_DEVT2INSTANCE implementations but may have
6932          * functional DDI_INFO_DEVT2DEVINFO implementations.  This code will
6933          * diagnose inconsistency and, for maximum compatibility with legacy
6934          * drivers, give preference to the drivers DDI_INFO_DEVT2DEVINFO
6935          * implementation over the above derived dip based the driver's
6936          * DDI_INFO_DEVT2INSTANCE implementation. This legacy support should
6937          * be removed when DDI_INFO_DEVT2DEVINFO is deprecated.
6938          *
6939          * NOTE: The following code has a race condition. DEVT2DEVINFO
6940          *      returns a dip which is not held. By the time we ref ddip,
6941          *      it could have been freed. The saving grace is that for
6942          *      most drivers, the dip returned from hold_devi() is the
6943          *      same one as the one returned by DEVT2DEVINFO, so we are
6944          *      safe for drivers with the correct getinfo(9e) impl.
6945          */
6946         if (((ops = ddi_hold_driver(major)) != NULL) &&
6947             CB_DRV_INSTALLED(ops) && ops->devo_getinfo)  {
6948                 if ((*ops->devo_getinfo)(NULL, DDI_INFO_DEVT2DEVINFO,
6949                     (void *)dev, (void **)&ddip) != DDI_SUCCESS)
6950                         ddip = NULL;
6951         }
6952 
6953         /* give preference to the driver returned DEVT2DEVINFO dip */
6954         if (ddip && (dip != ddip)) {
6955 #ifdef  DEBUG
6956                 cmn_err(CE_WARN, "%s: inconsistent getinfo(9E) implementation",
6957                     ddi_driver_name(ddip));
6958 #endif  /* DEBUG */
6959                 ndi_hold_devi(ddip);
6960                 if (dip)
6961                         ndi_rele_devi(dip);
6962                 dip = ddip;
6963         }
6964 
6965         if (ops)
6966                 ddi_rele_driver(major);
6967 
6968         return (dip);
6969 }
6970 
6971 /*
6972  * For compatibility only. Do not call this function!
6973  */
6974 dev_info_t *
6975 e_ddi_get_dev_info(dev_t dev, vtype_t type)
6976 {
6977         dev_info_t *dip = NULL;
6978         if (getmajor(dev) >= devcnt)
6979                 return (NULL);
6980 
6981         switch (type) {
6982         case VCHR:
6983         case VBLK:
6984                 dip = e_ddi_hold_devi_by_dev(dev, 0);
6985         default:
6986                 break;
6987         }
6988 
6989         /*
6990          * For compatibility reasons, we can only return the dip with
6991          * the driver ref count held. This is not a safe thing to do.
6992          * For certain broken third-party software, we are willing
6993          * to venture into unknown territory.
6994          */
6995         if (dip) {
6996                 (void) ndi_hold_driver(dip);
6997                 ndi_rele_devi(dip);
6998         }
6999         return (dip);
7000 }
7001 
7002 dev_info_t *
7003 e_ddi_hold_devi_by_path(char *path, int flags)
7004 {
7005         dev_info_t      *dip;
7006 
7007         /* can't specify NOATTACH by path */
7008         ASSERT(!(flags & E_DDI_HOLD_DEVI_NOATTACH));
7009 
7010         return (resolve_pathname(path, &dip, NULL, NULL) ? NULL : dip);
7011 }
7012 
7013 void
7014 e_ddi_hold_devi(dev_info_t *dip)
7015 {
7016         ndi_hold_devi(dip);
7017 }
7018 
7019 void
7020 ddi_release_devi(dev_info_t *dip)
7021 {
7022         ndi_rele_devi(dip);
7023 }
7024 
7025 /*
7026  * Associate a streams queue with a devinfo node
7027  * NOTE: This function is called by STREAM driver's put procedure.
7028  *      It cannot block.
7029  */
7030 void
7031 ddi_assoc_queue_with_devi(queue_t *q, dev_info_t *dip)
7032 {
7033         queue_t *rq = _RD(q);
7034         struct stdata *stp;
7035         vnode_t *vp;
7036 
7037         /* set flag indicating that ddi_assoc_queue_with_devi was called */
7038         mutex_enter(QLOCK(rq));
7039         rq->q_flag |= _QASSOCIATED;
7040         mutex_exit(QLOCK(rq));
7041 
7042         /* get the vnode associated with the queue */
7043         stp = STREAM(rq);
7044         vp = stp->sd_vnode;
7045         ASSERT(vp);
7046 
7047         /* change the hardware association of the vnode */
7048         spec_assoc_vp_with_devi(vp, dip);
7049 }
7050 
7051 /*
7052  * ddi_install_driver(name)
7053  *
7054  * Driver installation is currently a byproduct of driver loading.  This
7055  * may change.
7056  */
7057 int
7058 ddi_install_driver(char *name)
7059 {
7060         major_t major = ddi_name_to_major(name);
7061 
7062         if ((major == DDI_MAJOR_T_NONE) ||
7063             (ddi_hold_installed_driver(major) == NULL)) {
7064                 return (DDI_FAILURE);
7065         }
7066         ddi_rele_driver(major);
7067         return (DDI_SUCCESS);
7068 }
7069 
7070 struct dev_ops *
7071 ddi_hold_driver(major_t major)
7072 {
7073         return (mod_hold_dev_by_major(major));
7074 }
7075 
7076 
7077 void
7078 ddi_rele_driver(major_t major)
7079 {
7080         mod_rele_dev_by_major(major);
7081 }
7082 
7083 
7084 /*
7085  * This is called during boot to force attachment order of special dips
7086  * dip must be referenced via ndi_hold_devi()
7087  */
7088 int
7089 i_ddi_attach_node_hierarchy(dev_info_t *dip)
7090 {
7091         dev_info_t      *parent;
7092         int             ret, circ;
7093 
7094         /*
7095          * Recurse up until attached parent is found.
7096          */
7097         if (i_ddi_devi_attached(dip))
7098                 return (DDI_SUCCESS);
7099         parent = ddi_get_parent(dip);
7100         if (i_ddi_attach_node_hierarchy(parent) != DDI_SUCCESS)
7101                 return (DDI_FAILURE);
7102 
7103         /*
7104          * Come top-down, expanding .conf nodes under this parent
7105          * and driving attach.
7106          */
7107         ndi_devi_enter(parent, &circ);
7108         (void) i_ndi_make_spec_children(parent, 0);
7109         ret = i_ddi_attachchild(dip);
7110         ndi_devi_exit(parent, circ);
7111 
7112         return (ret);
7113 }
7114 
7115 /* keep this function static */
7116 static int
7117 attach_driver_nodes(major_t major)
7118 {
7119         struct devnames *dnp;
7120         dev_info_t *dip;
7121         int error = DDI_FAILURE;
7122 
7123         dnp = &devnamesp[major];
7124         LOCK_DEV_OPS(&dnp->dn_lock);
7125         dip = dnp->dn_head;
7126         while (dip) {
7127                 ndi_hold_devi(dip);
7128                 UNLOCK_DEV_OPS(&dnp->dn_lock);
7129                 if (i_ddi_attach_node_hierarchy(dip) == DDI_SUCCESS)
7130                         error = DDI_SUCCESS;
7131                 /*
7132                  * Set the 'ddi-config-driver-node' property on a nexus
7133                  * node to cause attach_driver_nodes() to configure all
7134                  * immediate children of the nexus. This property should
7135                  * be set on nodes with immediate children that bind to
7136                  * the same driver as parent.
7137                  */
7138                 if ((error == DDI_SUCCESS) && (ddi_prop_exists(DDI_DEV_T_ANY,
7139                     dip, DDI_PROP_DONTPASS, "ddi-config-driver-node"))) {
7140                         (void) ndi_devi_config(dip, NDI_NO_EVENT);
7141                 }
7142                 LOCK_DEV_OPS(&dnp->dn_lock);
7143                 ndi_rele_devi(dip);
7144                 dip = ddi_get_next(dip);
7145         }
7146         if (error == DDI_SUCCESS)
7147                 dnp->dn_flags |= DN_NO_AUTODETACH;
7148         UNLOCK_DEV_OPS(&dnp->dn_lock);
7149 
7150 
7151         return (error);
7152 }
7153 
7154 /*
7155  * i_ddi_attach_hw_nodes configures and attaches all hw nodes
7156  * bound to a specific driver. This function replaces calls to
7157  * ddi_hold_installed_driver() for drivers with no .conf
7158  * enumerated nodes.
7159  *
7160  * This facility is typically called at boot time to attach
7161  * platform-specific hardware nodes, such as ppm nodes on xcal
7162  * and grover and keyswitch nodes on cherrystone. It does not
7163  * deal with .conf enumerated node. Calling it beyond the boot
7164  * process is strongly discouraged.
7165  */
7166 int
7167 i_ddi_attach_hw_nodes(char *driver)
7168 {
7169         major_t major;
7170 
7171         major = ddi_name_to_major(driver);
7172         if (major == DDI_MAJOR_T_NONE)
7173                 return (DDI_FAILURE);
7174 
7175         return (attach_driver_nodes(major));
7176 }
7177 
7178 /*
7179  * i_ddi_attach_pseudo_node configures pseudo drivers which
7180  * has a single node. The .conf nodes must be enumerated
7181  * before calling this interface. The dip is held attached
7182  * upon returning.
7183  *
7184  * This facility should only be called only at boot time
7185  * by the I/O framework.
7186  */
7187 dev_info_t *
7188 i_ddi_attach_pseudo_node(char *driver)
7189 {
7190         major_t major;
7191         dev_info_t *dip;
7192 
7193         major = ddi_name_to_major(driver);
7194         if (major == DDI_MAJOR_T_NONE)
7195                 return (NULL);
7196 
7197         if (attach_driver_nodes(major) != DDI_SUCCESS)
7198                 return (NULL);
7199 
7200         dip = devnamesp[major].dn_head;
7201         ASSERT(dip && ddi_get_next(dip) == NULL);
7202         ndi_hold_devi(dip);
7203         return (dip);
7204 }
7205 
7206 static void
7207 diplist_to_parent_major(dev_info_t *head, char parents[])
7208 {
7209         major_t major;
7210         dev_info_t *dip, *pdip;
7211 
7212         for (dip = head; dip != NULL; dip = ddi_get_next(dip)) {
7213                 pdip = ddi_get_parent(dip);
7214                 ASSERT(pdip);   /* disallow rootnex.conf nodes */
7215                 major = ddi_driver_major(pdip);
7216                 if ((major != DDI_MAJOR_T_NONE) && parents[major] == 0)
7217                         parents[major] = 1;
7218         }
7219 }
7220 
7221 /*
7222  * Call ddi_hold_installed_driver() on each parent major
7223  * and invoke mt_config_driver() to attach child major.
7224  * This is part of the implementation of ddi_hold_installed_driver.
7225  */
7226 static int
7227 attach_driver_by_parent(major_t child_major, char parents[])
7228 {
7229         major_t par_major;
7230         struct mt_config_handle *hdl;
7231         int flags = NDI_DEVI_PERSIST | NDI_NO_EVENT;
7232 
7233         hdl = mt_config_init(NULL, NULL, flags, child_major, MT_CONFIG_OP,
7234             NULL);
7235         for (par_major = 0; par_major < devcnt; par_major++) {
7236                 /* disallow recursion on the same driver */
7237                 if (parents[par_major] == 0 || par_major == child_major)
7238                         continue;
7239                 if (ddi_hold_installed_driver(par_major) == NULL)
7240                         continue;
7241                 hdl->mtc_parmajor = par_major;
7242                 mt_config_driver(hdl);
7243                 ddi_rele_driver(par_major);
7244         }
7245         (void) mt_config_fini(hdl);
7246 
7247         return (i_ddi_devs_attached(child_major));
7248 }
7249 
7250 int
7251 i_ddi_devs_attached(major_t major)
7252 {
7253         dev_info_t *dip;
7254         struct devnames *dnp;
7255         int error = DDI_FAILURE;
7256 
7257         /* check for attached instances */
7258         dnp = &devnamesp[major];
7259         LOCK_DEV_OPS(&dnp->dn_lock);
7260         for (dip = dnp->dn_head; dip != NULL; dip = ddi_get_next(dip)) {
7261                 if (i_ddi_devi_attached(dip)) {
7262                         error = DDI_SUCCESS;
7263                         break;
7264                 }
7265         }
7266         UNLOCK_DEV_OPS(&dnp->dn_lock);
7267 
7268         return (error);
7269 }
7270 
7271 int
7272 i_ddi_minor_node_count(dev_info_t *ddip, const char *node_type)
7273 {
7274         int                     circ;
7275         struct ddi_minor_data   *dp;
7276         int                     count = 0;
7277 
7278         ndi_devi_enter(ddip, &circ);
7279         for (dp = DEVI(ddip)->devi_minor; dp != NULL; dp = dp->next) {
7280                 if (strcmp(dp->ddm_node_type, node_type) == 0)
7281                         count++;
7282         }
7283         ndi_devi_exit(ddip, circ);
7284         return (count);
7285 }
7286 
7287 /*
7288  * ddi_hold_installed_driver configures and attaches all
7289  * instances of the specified driver. To accomplish this
7290  * it configures and attaches all possible parents of
7291  * the driver, enumerated both in h/w nodes and in the
7292  * driver's .conf file.
7293  *
7294  * NOTE: This facility is for compatibility purposes only and will
7295  *      eventually go away. Its usage is strongly discouraged.
7296  */
7297 static void
7298 enter_driver(struct devnames *dnp)
7299 {
7300         mutex_enter(&dnp->dn_lock);
7301         ASSERT(dnp->dn_busy_thread != curthread);
7302         while (dnp->dn_flags & DN_DRIVER_BUSY)
7303                 cv_wait(&dnp->dn_wait, &dnp->dn_lock);
7304         dnp->dn_flags |= DN_DRIVER_BUSY;
7305         dnp->dn_busy_thread = curthread;
7306         mutex_exit(&dnp->dn_lock);
7307 }
7308 
7309 static void
7310 exit_driver(struct devnames *dnp)
7311 {
7312         mutex_enter(&dnp->dn_lock);
7313         ASSERT(dnp->dn_busy_thread == curthread);
7314         dnp->dn_flags &= ~DN_DRIVER_BUSY;
7315         dnp->dn_busy_thread = NULL;
7316         cv_broadcast(&dnp->dn_wait);
7317         mutex_exit(&dnp->dn_lock);
7318 }
7319 
7320 struct dev_ops *
7321 ddi_hold_installed_driver(major_t major)
7322 {
7323         struct dev_ops *ops;
7324         struct devnames *dnp;
7325         char *parents;
7326         int error;
7327 
7328         ops = ddi_hold_driver(major);
7329         if (ops == NULL)
7330                 return (NULL);
7331 
7332         /*
7333          * Return immediately if all the attach operations associated
7334          * with a ddi_hold_installed_driver() call have already been done.
7335          */
7336         dnp = &devnamesp[major];
7337         enter_driver(dnp);
7338         ASSERT(driver_active(major));
7339 
7340         if (dnp->dn_flags & DN_DRIVER_HELD) {
7341                 exit_driver(dnp);
7342                 if (i_ddi_devs_attached(major) == DDI_SUCCESS)
7343                         return (ops);
7344                 ddi_rele_driver(major);
7345                 return (NULL);
7346         }
7347 
7348         LOCK_DEV_OPS(&dnp->dn_lock);
7349         dnp->dn_flags |= (DN_DRIVER_HELD | DN_NO_AUTODETACH);
7350         UNLOCK_DEV_OPS(&dnp->dn_lock);
7351 
7352         DCOMPATPRINTF((CE_CONT,
7353             "ddi_hold_installed_driver: %s\n", dnp->dn_name));
7354 
7355         /*
7356          * When the driver has no .conf children, it is sufficient
7357          * to attach existing nodes in the device tree. Nodes not
7358          * enumerated by the OBP are not attached.
7359          */
7360         if (dnp->dn_pl == NULL) {
7361                 if (attach_driver_nodes(major) == DDI_SUCCESS) {
7362                         exit_driver(dnp);
7363                         return (ops);
7364                 }
7365                 exit_driver(dnp);
7366                 ddi_rele_driver(major);
7367                 return (NULL);
7368         }
7369 
7370         /*
7371          * Driver has .conf nodes. We find all possible parents
7372          * and recursively all ddi_hold_installed_driver on the
7373          * parent driver; then we invoke ndi_config_driver()
7374          * on all possible parent node in parallel to speed up
7375          * performance.
7376          */
7377         parents = kmem_zalloc(devcnt * sizeof (char), KM_SLEEP);
7378 
7379         LOCK_DEV_OPS(&dnp->dn_lock);
7380         /* find .conf parents */
7381         (void) impl_parlist_to_major(dnp->dn_pl, parents);
7382         /* find hw node parents */
7383         diplist_to_parent_major(dnp->dn_head, parents);
7384         UNLOCK_DEV_OPS(&dnp->dn_lock);
7385 
7386         error = attach_driver_by_parent(major, parents);
7387         kmem_free(parents, devcnt * sizeof (char));
7388         if (error == DDI_SUCCESS) {
7389                 exit_driver(dnp);
7390                 return (ops);
7391         }
7392 
7393         exit_driver(dnp);
7394         ddi_rele_driver(major);
7395         return (NULL);
7396 }
7397 
7398 /*
7399  * Default bus_config entry point for nexus drivers
7400  */
7401 int
7402 ndi_busop_bus_config(dev_info_t *pdip, uint_t flags, ddi_bus_config_op_t op,
7403     void *arg, dev_info_t **child, clock_t timeout)
7404 {
7405         major_t major;
7406 
7407         /*
7408          * A timeout of 30 minutes or more is probably a mistake
7409          * This is intended to catch uses where timeout is in
7410          * the wrong units.  timeout must be in units of ticks.
7411          */
7412         ASSERT(timeout < SEC_TO_TICK(1800));
7413 
7414         major = DDI_MAJOR_T_NONE;
7415         switch (op) {
7416         case BUS_CONFIG_ONE:
7417                 NDI_DEBUG(flags, (CE_CONT, "%s%d: bus config %s timeout=%ld\n",
7418                     ddi_driver_name(pdip), ddi_get_instance(pdip),
7419                     (char *)arg, timeout));
7420                 return (devi_config_one(pdip, (char *)arg, child, flags,
7421                     timeout));
7422 
7423         case BUS_CONFIG_DRIVER:
7424                 major = (major_t)(uintptr_t)arg;
7425                 /*FALLTHROUGH*/
7426         case BUS_CONFIG_ALL:
7427                 NDI_DEBUG(flags, (CE_CONT, "%s%d: bus config timeout=%ld\n",
7428                     ddi_driver_name(pdip), ddi_get_instance(pdip),
7429                     timeout));
7430                 if (timeout > 0) {
7431                         NDI_DEBUG(flags, (CE_CONT,
7432                             "%s%d: bus config all timeout=%ld\n",
7433                             ddi_driver_name(pdip), ddi_get_instance(pdip),
7434                             timeout));
7435                         delay(timeout);
7436                 }
7437                 return (config_immediate_children(pdip, flags, major));
7438 
7439         default:
7440                 return (NDI_FAILURE);
7441         }
7442         /*NOTREACHED*/
7443 }
7444 
7445 /*
7446  * Default busop bus_unconfig handler for nexus drivers
7447  */
7448 int
7449 ndi_busop_bus_unconfig(dev_info_t *pdip, uint_t flags, ddi_bus_config_op_t op,
7450     void *arg)
7451 {
7452         major_t major;
7453 
7454         major = DDI_MAJOR_T_NONE;
7455         switch (op) {
7456         case BUS_UNCONFIG_ONE:
7457                 NDI_DEBUG(flags, (CE_CONT, "%s%d: bus unconfig %s\n",
7458                     ddi_driver_name(pdip), ddi_get_instance(pdip),
7459                     (char *)arg));
7460                 return (devi_unconfig_one(pdip, (char *)arg, flags));
7461 
7462         case BUS_UNCONFIG_DRIVER:
7463                 major = (major_t)(uintptr_t)arg;
7464                 /*FALLTHROUGH*/
7465         case BUS_UNCONFIG_ALL:
7466                 NDI_DEBUG(flags, (CE_CONT, "%s%d: bus unconfig all\n",
7467                     ddi_driver_name(pdip), ddi_get_instance(pdip)));
7468                 return (unconfig_immediate_children(pdip, NULL, flags, major));
7469 
7470         default:
7471                 return (NDI_FAILURE);
7472         }
7473         /*NOTREACHED*/
7474 }
7475 
7476 /*
7477  * dummy functions to be removed
7478  */
7479 void
7480 impl_rem_dev_props(dev_info_t *dip)
7481 {
7482         _NOTE(ARGUNUSED(dip))
7483         /* do nothing */
7484 }
7485 
7486 /*
7487  * Determine if a node is a leaf node. If not sure, return false (0).
7488  */
7489 static int
7490 is_leaf_node(dev_info_t *dip)
7491 {
7492         major_t major = ddi_driver_major(dip);
7493 
7494         if (major == DDI_MAJOR_T_NONE)
7495                 return (0);
7496 
7497         return (devnamesp[major].dn_flags & DN_LEAF_DRIVER);
7498 }
7499 
7500 /*
7501  * Multithreaded [un]configuration
7502  */
7503 static struct mt_config_handle *
7504 mt_config_init(dev_info_t *pdip, dev_info_t **dipp, int flags,
7505     major_t major, int op, struct brevq_node **brevqp)
7506 {
7507         struct mt_config_handle *hdl = kmem_alloc(sizeof (*hdl), KM_SLEEP);
7508 
7509         mutex_init(&hdl->mtc_lock, NULL, MUTEX_DEFAULT, NULL);
7510         cv_init(&hdl->mtc_cv, NULL, CV_DEFAULT, NULL);
7511         hdl->mtc_pdip = pdip;
7512         hdl->mtc_fdip = dipp;
7513         hdl->mtc_parmajor = DDI_MAJOR_T_NONE;
7514         hdl->mtc_flags = flags;
7515         hdl->mtc_major = major;
7516         hdl->mtc_thr_count = 0;
7517         hdl->mtc_op = op;
7518         hdl->mtc_error = 0;
7519         hdl->mtc_brevqp = brevqp;
7520 
7521 #ifdef DEBUG
7522         gethrestime(&hdl->start_time);
7523         hdl->total_time = 0;
7524 #endif /* DEBUG */
7525 
7526         return (hdl);
7527 }
7528 
7529 #ifdef DEBUG
7530 static int
7531 time_diff_in_msec(timestruc_t start, timestruc_t end)
7532 {
7533         int     nsec, sec;
7534 
7535         sec = end.tv_sec - start.tv_sec;
7536         nsec = end.tv_nsec - start.tv_nsec;
7537         if (nsec < 0) {
7538                 nsec += NANOSEC;
7539                 sec -= 1;
7540         }
7541 
7542         return (sec * (NANOSEC >> 20) + (nsec >> 20));
7543 }
7544 
7545 #endif  /* DEBUG */
7546 
7547 static int
7548 mt_config_fini(struct mt_config_handle *hdl)
7549 {
7550         int             rv;
7551 #ifdef DEBUG
7552         int             real_time;
7553         timestruc_t     end_time;
7554 #endif /* DEBUG */
7555 
7556         mutex_enter(&hdl->mtc_lock);
7557         while (hdl->mtc_thr_count > 0)
7558                 cv_wait(&hdl->mtc_cv, &hdl->mtc_lock);
7559         rv = hdl->mtc_error;
7560         mutex_exit(&hdl->mtc_lock);
7561 
7562 #ifdef DEBUG
7563         gethrestime(&end_time);
7564         real_time = time_diff_in_msec(hdl->start_time, end_time);
7565         if ((ddidebug & DDI_MTCONFIG) && hdl->mtc_pdip)
7566                 cmn_err(CE_NOTE,
7567                     "config %s%d: total time %d msec, real time %d msec",
7568                     ddi_driver_name(hdl->mtc_pdip),
7569                     ddi_get_instance(hdl->mtc_pdip),
7570                     hdl->total_time, real_time);
7571 #endif /* DEBUG */
7572 
7573         cv_destroy(&hdl->mtc_cv);
7574         mutex_destroy(&hdl->mtc_lock);
7575         kmem_free(hdl, sizeof (*hdl));
7576 
7577         return (rv);
7578 }
7579 
7580 struct mt_config_data {
7581         struct mt_config_handle *mtc_hdl;
7582         dev_info_t              *mtc_dip;
7583         major_t                 mtc_major;
7584         int                     mtc_flags;
7585         struct brevq_node       *mtc_brn;
7586         struct mt_config_data   *mtc_next;
7587 };
7588 
7589 static void
7590 mt_config_thread(void *arg)
7591 {
7592         struct mt_config_data   *mcd = (struct mt_config_data *)arg;
7593         struct mt_config_handle *hdl = mcd->mtc_hdl;
7594         dev_info_t              *dip = mcd->mtc_dip;
7595         dev_info_t              *rdip, **dipp;
7596         major_t                 major = mcd->mtc_major;
7597         int                     flags = mcd->mtc_flags;
7598         int                     rv = 0;
7599 
7600 #ifdef DEBUG
7601         timestruc_t start_time, end_time;
7602         gethrestime(&start_time);
7603 #endif /* DEBUG */
7604 
7605         rdip = NULL;
7606         dipp = hdl->mtc_fdip ? &rdip : NULL;
7607 
7608         switch (hdl->mtc_op) {
7609         case MT_CONFIG_OP:
7610                 rv = devi_config_common(dip, flags, major);
7611                 break;
7612         case MT_UNCONFIG_OP:
7613                 if (mcd->mtc_brn) {
7614                         struct brevq_node *brevq = NULL;
7615                         rv = devi_unconfig_common(dip, dipp, flags, major,
7616                             &brevq);
7617                         mcd->mtc_brn->brn_child = brevq;
7618                 } else
7619                         rv = devi_unconfig_common(dip, dipp, flags, major,
7620                             NULL);
7621                 break;
7622         }
7623 
7624         mutex_enter(&hdl->mtc_lock);
7625 #ifdef DEBUG
7626         gethrestime(&end_time);
7627         hdl->total_time += time_diff_in_msec(start_time, end_time);
7628 #endif /* DEBUG */
7629 
7630         if ((rv != NDI_SUCCESS) && (hdl->mtc_error == 0)) {
7631                 hdl->mtc_error = rv;
7632 #ifdef  DEBUG
7633                 if ((ddidebug & DDI_DEBUG) && (major != DDI_MAJOR_T_NONE)) {
7634                         char    *path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
7635 
7636                         (void) ddi_pathname(dip, path);
7637                         cmn_err(CE_NOTE, "mt_config_thread: "
7638                             "op %d.%d.%x at %s failed %d",
7639                             hdl->mtc_op, major, flags, path, rv);
7640                         kmem_free(path, MAXPATHLEN);
7641                 }
7642 #endif  /* DEBUG */
7643         }
7644 
7645         if (hdl->mtc_fdip && *hdl->mtc_fdip == NULL) {
7646                 *hdl->mtc_fdip = rdip;
7647                 rdip = NULL;
7648         }
7649 
7650         if (rdip) {
7651                 ASSERT(rv != NDI_SUCCESS);
7652                 ndi_rele_devi(rdip);
7653         }
7654 
7655         ndi_rele_devi(dip);
7656 
7657         if (--hdl->mtc_thr_count == 0)
7658                 cv_broadcast(&hdl->mtc_cv);
7659         mutex_exit(&hdl->mtc_lock);
7660         kmem_free(mcd, sizeof (*mcd));
7661 }
7662 
7663 /*
7664  * Multi-threaded config/unconfig of child nexus
7665  */
7666 static void
7667 mt_config_children(struct mt_config_handle *hdl)
7668 {
7669         dev_info_t              *pdip = hdl->mtc_pdip;
7670         major_t                 major = hdl->mtc_major;
7671         dev_info_t              *dip;
7672         int                     circ;
7673         struct brevq_node       *brn;
7674         struct mt_config_data   *mcd_head = NULL;
7675         struct mt_config_data   *mcd_tail = NULL;
7676         struct mt_config_data   *mcd;
7677 #ifdef DEBUG
7678         timestruc_t             end_time;
7679 
7680         /* Update total_time in handle */
7681         gethrestime(&end_time);
7682         hdl->total_time += time_diff_in_msec(hdl->start_time, end_time);
7683 #endif
7684 
7685         ndi_devi_enter(pdip, &circ);
7686         dip = ddi_get_child(pdip);
7687         while (dip) {
7688                 if (hdl->mtc_op == MT_UNCONFIG_OP && hdl->mtc_brevqp &&
7689                     !(DEVI_EVREMOVE(dip)) &&
7690                     i_ddi_node_state(dip) >= DS_INITIALIZED) {
7691                         /*
7692                          * Enqueue this dip's deviname.
7693                          * No need to hold a lock while enqueuing since this
7694                          * is the only thread doing the enqueue and no one
7695                          * walks the queue while we are in multithreaded
7696                          * unconfiguration.
7697                          */
7698                         brn = brevq_enqueue(hdl->mtc_brevqp, dip, NULL);
7699                 } else
7700                         brn = NULL;
7701 
7702                 /*
7703                  * Hold the child that we are processing so it does not get
7704                  * removed. The corrisponding ndi_rele_devi() for children
7705                  * that are not being skipped is done at the end of
7706                  * mt_config_thread().
7707                  */
7708                 ndi_hold_devi(dip);
7709 
7710                 /*
7711                  * skip leaf nodes and (for configure) nodes not
7712                  * fully attached.
7713                  */
7714                 if (is_leaf_node(dip) ||
7715                     (hdl->mtc_op == MT_CONFIG_OP &&
7716                     i_ddi_node_state(dip) < DS_READY)) {
7717                         ndi_rele_devi(dip);
7718                         dip = ddi_get_next_sibling(dip);
7719                         continue;
7720                 }
7721 
7722                 mcd = kmem_alloc(sizeof (*mcd), KM_SLEEP);
7723                 mcd->mtc_dip = dip;
7724                 mcd->mtc_hdl = hdl;
7725                 mcd->mtc_brn = brn;
7726 
7727                 /*
7728                  * Switch a 'driver' operation to an 'all' operation below a
7729                  * node bound to the driver.
7730                  */
7731                 if ((major == DDI_MAJOR_T_NONE) ||
7732                     (major == ddi_driver_major(dip)))
7733                         mcd->mtc_major = DDI_MAJOR_T_NONE;
7734                 else
7735                         mcd->mtc_major = major;
7736 
7737                 /*
7738                  * The unconfig-driver to unconfig-all conversion above
7739                  * constitutes an autodetach for NDI_DETACH_DRIVER calls,
7740                  * set NDI_AUTODETACH.
7741                  */
7742                 mcd->mtc_flags = hdl->mtc_flags;
7743                 if ((mcd->mtc_flags & NDI_DETACH_DRIVER) &&
7744                     (hdl->mtc_op == MT_UNCONFIG_OP) &&
7745                     (major == ddi_driver_major(pdip)))
7746                         mcd->mtc_flags |= NDI_AUTODETACH;
7747 
7748                 mutex_enter(&hdl->mtc_lock);
7749                 hdl->mtc_thr_count++;
7750                 mutex_exit(&hdl->mtc_lock);
7751 
7752                 /*
7753                  * Add to end of list to process after ndi_devi_exit to avoid
7754                  * locking differences depending on value of mtc_off.
7755                  */
7756                 mcd->mtc_next = NULL;
7757                 if (mcd_head == NULL)
7758                         mcd_head = mcd;
7759                 else
7760                         mcd_tail->mtc_next = mcd;
7761                 mcd_tail = mcd;
7762 
7763                 dip = ddi_get_next_sibling(dip);
7764         }
7765         ndi_devi_exit(pdip, circ);
7766 
7767         /* go through the list of held children */
7768         for (mcd = mcd_head; mcd; mcd = mcd_head) {
7769                 mcd_head = mcd->mtc_next;
7770                 if (mtc_off || (mcd->mtc_flags & NDI_MTC_OFF))
7771                         mt_config_thread(mcd);
7772                 else
7773                         (void) thread_create(NULL, 0, mt_config_thread, mcd,
7774                             0, &p0, TS_RUN, minclsyspri);
7775         }
7776 }
7777 
7778 static void
7779 mt_config_driver(struct mt_config_handle *hdl)
7780 {
7781         major_t                 par_major = hdl->mtc_parmajor;
7782         major_t                 major = hdl->mtc_major;
7783         struct devnames         *dnp = &devnamesp[par_major];
7784         dev_info_t              *dip;
7785         struct mt_config_data   *mcd_head = NULL;
7786         struct mt_config_data   *mcd_tail = NULL;
7787         struct mt_config_data   *mcd;
7788 #ifdef DEBUG
7789         timestruc_t             end_time;
7790 
7791         /* Update total_time in handle */
7792         gethrestime(&end_time);
7793         hdl->total_time += time_diff_in_msec(hdl->start_time, end_time);
7794 #endif
7795         ASSERT(par_major != DDI_MAJOR_T_NONE);
7796         ASSERT(major != DDI_MAJOR_T_NONE);
7797 
7798         LOCK_DEV_OPS(&dnp->dn_lock);
7799         dip = devnamesp[par_major].dn_head;
7800         while (dip) {
7801                 /*
7802                  * Hold the child that we are processing so it does not get
7803                  * removed. The corrisponding ndi_rele_devi() for children
7804                  * that are not being skipped is done at the end of
7805                  * mt_config_thread().
7806                  */
7807                 ndi_hold_devi(dip);
7808 
7809                 /* skip leaf nodes and nodes not fully attached */
7810                 if (!i_ddi_devi_attached(dip) || is_leaf_node(dip)) {
7811                         ndi_rele_devi(dip);
7812                         dip = ddi_get_next(dip);
7813                         continue;
7814                 }
7815 
7816                 mcd = kmem_alloc(sizeof (*mcd), KM_SLEEP);
7817                 mcd->mtc_dip = dip;
7818                 mcd->mtc_hdl = hdl;
7819                 mcd->mtc_major = major;
7820                 mcd->mtc_flags = hdl->mtc_flags;
7821 
7822                 mutex_enter(&hdl->mtc_lock);
7823                 hdl->mtc_thr_count++;
7824                 mutex_exit(&hdl->mtc_lock);
7825 
7826                 /*
7827                  * Add to end of list to process after UNLOCK_DEV_OPS to avoid
7828                  * locking differences depending on value of mtc_off.
7829                  */
7830                 mcd->mtc_next = NULL;
7831                 if (mcd_head == NULL)
7832                         mcd_head = mcd;
7833                 else
7834                         mcd_tail->mtc_next = mcd;
7835                 mcd_tail = mcd;
7836 
7837                 dip = ddi_get_next(dip);
7838         }
7839         UNLOCK_DEV_OPS(&dnp->dn_lock);
7840 
7841         /* go through the list of held children */
7842         for (mcd = mcd_head; mcd; mcd = mcd_head) {
7843                 mcd_head = mcd->mtc_next;
7844                 if (mtc_off || (mcd->mtc_flags & NDI_MTC_OFF))
7845                         mt_config_thread(mcd);
7846                 else
7847                         (void) thread_create(NULL, 0, mt_config_thread, mcd,
7848                             0, &p0, TS_RUN, minclsyspri);
7849         }
7850 }
7851 
7852 /*
7853  * Given the nodeid for a persistent (PROM or SID) node, return
7854  * the corresponding devinfo node
7855  * NOTE: This function will return NULL for .conf nodeids.
7856  */
7857 dev_info_t *
7858 e_ddi_nodeid_to_dip(pnode_t nodeid)
7859 {
7860         dev_info_t              *dip = NULL;
7861         struct devi_nodeid      *prev, *elem;
7862 
7863         mutex_enter(&devimap->dno_lock);
7864 
7865         prev = NULL;
7866         for (elem = devimap->dno_head; elem; elem = elem->next) {
7867                 if (elem->nodeid == nodeid) {
7868                         ndi_hold_devi(elem->dip);
7869                         dip = elem->dip;
7870                         break;
7871                 }
7872                 prev = elem;
7873         }
7874 
7875         /*
7876          * Move to head for faster lookup next time
7877          */
7878         if (elem && prev) {
7879                 prev->next = elem->next;
7880                 elem->next = devimap->dno_head;
7881                 devimap->dno_head = elem;
7882         }
7883 
7884         mutex_exit(&devimap->dno_lock);
7885         return (dip);
7886 }
7887 
7888 static void
7889 free_cache_task(void *arg)
7890 {
7891         ASSERT(arg == NULL);
7892 
7893         mutex_enter(&di_cache.cache_lock);
7894 
7895         /*
7896          * The cache can be invalidated without holding the lock
7897          * but it can be made valid again only while the lock is held.
7898          * So if the cache is invalid when the lock is held, it will
7899          * stay invalid until lock is released.
7900          */
7901         if (!di_cache.cache_valid)
7902                 i_ddi_di_cache_free(&di_cache);
7903 
7904         mutex_exit(&di_cache.cache_lock);
7905 
7906         if (di_cache_debug)
7907                 cmn_err(CE_NOTE, "system_taskq: di_cache freed");
7908 }
7909 
7910 extern int modrootloaded;
7911 
7912 void
7913 i_ddi_di_cache_free(struct di_cache *cache)
7914 {
7915         int     error;
7916         extern int sys_shutdown;
7917 
7918         ASSERT(mutex_owned(&cache->cache_lock));
7919 
7920         if (cache->cache_size) {
7921                 ASSERT(cache->cache_size > 0);
7922                 ASSERT(cache->cache_data);
7923 
7924                 kmem_free(cache->cache_data, cache->cache_size);
7925                 cache->cache_data = NULL;
7926                 cache->cache_size = 0;
7927 
7928                 if (di_cache_debug)
7929                         cmn_err(CE_NOTE, "i_ddi_di_cache_free: freed cachemem");
7930         } else {
7931                 ASSERT(cache->cache_data == NULL);
7932                 if (di_cache_debug)
7933                         cmn_err(CE_NOTE, "i_ddi_di_cache_free: NULL cache");
7934         }
7935 
7936         if (!modrootloaded || rootvp == NULL ||
7937             vn_is_readonly(rootvp) || sys_shutdown) {
7938                 if (di_cache_debug) {
7939                         cmn_err(CE_WARN, "/ not mounted/RDONLY. Skip unlink");
7940                 }
7941                 return;
7942         }
7943 
7944         error = vn_remove(DI_CACHE_FILE, UIO_SYSSPACE, RMFILE);
7945         if (di_cache_debug && error && error != ENOENT) {
7946                 cmn_err(CE_WARN, "%s: unlink failed: %d", DI_CACHE_FILE, error);
7947         } else if (di_cache_debug && !error) {
7948                 cmn_err(CE_NOTE, "i_ddi_di_cache_free: unlinked cache file");
7949         }
7950 }
7951 
7952 void
7953 i_ddi_di_cache_invalidate()
7954 {
7955         int     cache_valid;
7956 
7957         if (!modrootloaded || !i_ddi_io_initialized()) {
7958                 if (di_cache_debug)
7959                         cmn_err(CE_NOTE, "I/O not inited. Skipping invalidate");
7960                 return;
7961         }
7962 
7963         /* Increment devtree generation number. */
7964         atomic_inc_ulong(&devtree_gen);
7965 
7966         /* Invalidate the in-core cache and dispatch free on valid->invalid */
7967         cache_valid = atomic_swap_uint(&di_cache.cache_valid, 0);
7968         if (cache_valid) {
7969                 /*
7970                  * This is an optimization to start cleaning up a cached
7971                  * snapshot early.  For this reason, it is OK for
7972                  * taskq_dispatach to fail (and it is OK to not track calling
7973                  * context relative to sleep, and assume NOSLEEP).
7974                  */
7975                 (void) taskq_dispatch(system_taskq, free_cache_task, NULL,
7976                     TQ_NOSLEEP);
7977         }
7978 
7979         if (di_cache_debug) {
7980                 cmn_err(CE_NOTE, "invalidation");
7981         }
7982 }
7983 
7984 
7985 static void
7986 i_bind_vhci_node(dev_info_t *dip)
7987 {
7988         DEVI(dip)->devi_major = ddi_name_to_major(ddi_node_name(dip));
7989         i_ddi_set_node_state(dip, DS_BOUND);
7990 }
7991 
7992 static char vhci_node_addr[2];
7993 
7994 static int
7995 i_init_vhci_node(dev_info_t *dip)
7996 {
7997         add_global_props(dip);
7998         DEVI(dip)->devi_ops = ndi_hold_driver(dip);
7999         if (DEVI(dip)->devi_ops == NULL)
8000                 return (-1);
8001 
8002         DEVI(dip)->devi_instance = e_ddi_assign_instance(dip);
8003         e_ddi_keep_instance(dip);
8004         vhci_node_addr[0]       = '\0';
8005         ddi_set_name_addr(dip, vhci_node_addr);
8006         i_ddi_set_node_state(dip, DS_INITIALIZED);
8007         return (0);
8008 }
8009 
8010 static void
8011 i_link_vhci_node(dev_info_t *dip)
8012 {
8013         ASSERT(MUTEX_HELD(&global_vhci_lock));
8014 
8015         /*
8016          * scsi_vhci should be kept left most of the device tree.
8017          */
8018         if (scsi_vhci_dip) {
8019                 DEVI(dip)->devi_sibling = DEVI(scsi_vhci_dip)->devi_sibling;
8020                 DEVI(scsi_vhci_dip)->devi_sibling = DEVI(dip);
8021         } else {
8022                 DEVI(dip)->devi_sibling = DEVI(top_devinfo)->devi_child;
8023                 DEVI(top_devinfo)->devi_child = DEVI(dip);
8024         }
8025 }
8026 
8027 
8028 /*
8029  * This a special routine to enumerate vhci node (child of rootnex
8030  * node) without holding the ndi_devi_enter() lock. The device node
8031  * is allocated, initialized and brought into DS_READY state before
8032  * inserting into the device tree. The VHCI node is handcrafted
8033  * here to bring the node to DS_READY, similar to rootnex node.
8034  *
8035  * The global_vhci_lock protects linking the node into the device
8036  * as same lock is held before linking/unlinking any direct child
8037  * of rootnex children.
8038  *
8039  * This routine is a workaround to handle a possible deadlock
8040  * that occurs while trying to enumerate node in a different sub-tree
8041  * during _init/_attach entry points.
8042  */
8043 /*ARGSUSED*/
8044 dev_info_t *
8045 ndi_devi_config_vhci(char *drvname, int flags)
8046 {
8047         struct devnames         *dnp;
8048         dev_info_t              *dip;
8049         major_t                 major = ddi_name_to_major(drvname);
8050 
8051         if (major == -1)
8052                 return (NULL);
8053 
8054         /* Make sure we create the VHCI node only once */
8055         dnp = &devnamesp[major];
8056         LOCK_DEV_OPS(&dnp->dn_lock);
8057         if (dnp->dn_head) {
8058                 dip = dnp->dn_head;
8059                 UNLOCK_DEV_OPS(&dnp->dn_lock);
8060                 return (dip);
8061         }
8062         UNLOCK_DEV_OPS(&dnp->dn_lock);
8063 
8064         /* Allocate the VHCI node */
8065         ndi_devi_alloc_sleep(top_devinfo, drvname, DEVI_SID_NODEID, &dip);
8066         ndi_hold_devi(dip);
8067 
8068         /* Mark the node as VHCI */
8069         DEVI(dip)->devi_node_attributes |= DDI_VHCI_NODE;
8070 
8071         i_ddi_add_devimap(dip);
8072         i_bind_vhci_node(dip);
8073         if (i_init_vhci_node(dip) == -1) {
8074                 ndi_rele_devi(dip);
8075                 (void) ndi_devi_free(dip);
8076                 return (NULL);
8077         }
8078 
8079         mutex_enter(&(DEVI(dip)->devi_lock));
8080         DEVI_SET_ATTACHING(dip);
8081         mutex_exit(&(DEVI(dip)->devi_lock));
8082 
8083         if (devi_attach(dip, DDI_ATTACH) != DDI_SUCCESS) {
8084                 cmn_err(CE_CONT, "Could not attach %s driver", drvname);
8085                 e_ddi_free_instance(dip, vhci_node_addr);
8086                 ndi_rele_devi(dip);
8087                 (void) ndi_devi_free(dip);
8088                 return (NULL);
8089         }
8090         mutex_enter(&(DEVI(dip)->devi_lock));
8091         DEVI_CLR_ATTACHING(dip);
8092         mutex_exit(&(DEVI(dip)->devi_lock));
8093 
8094         mutex_enter(&global_vhci_lock);
8095         i_link_vhci_node(dip);
8096         mutex_exit(&global_vhci_lock);
8097         i_ddi_set_node_state(dip, DS_READY);
8098 
8099         LOCK_DEV_OPS(&dnp->dn_lock);
8100         dnp->dn_flags |= DN_DRIVER_HELD;
8101         dnp->dn_head = dip;
8102         UNLOCK_DEV_OPS(&dnp->dn_lock);
8103 
8104         i_ndi_devi_report_status_change(dip, NULL);
8105 
8106         return (dip);
8107 }
8108 
8109 /*
8110  * Maintain DEVI_DEVICE_REMOVED hotplug devi_state for remove/reinsert hotplug
8111  * of open devices. Currently, because of tight coupling between the devfs file
8112  * system and the Solaris device tree, a driver can't always make the device
8113  * tree state (esp devi_node_state) match device hardware hotplug state. Until
8114  * resolved, to overcome this deficiency we use the following interfaces that
8115  * maintain the DEVI_DEVICE_REMOVED devi_state status bit.  These interface
8116  * report current state, and drive operation (like events and cache
8117  * invalidation) when a driver changes remove/insert state of an open device.
8118  *
8119  * The ndi_devi_device_isremoved() returns 1 if the device is currently removed.
8120  *
8121  * The ndi_devi_device_remove() interface declares the device as removed, and
8122  * returns 1 if there was a state change associated with this declaration.
8123  *
8124  * The ndi_devi_device_insert() declares the device as inserted, and returns 1
8125  * if there was a state change associated with this declaration.
8126  */
8127 int
8128 ndi_devi_device_isremoved(dev_info_t *dip)
8129 {
8130         return (DEVI_IS_DEVICE_REMOVED(dip));
8131 }
8132 
8133 int
8134 ndi_devi_device_remove(dev_info_t *dip)
8135 {
8136         ASSERT(dip && ddi_get_parent(dip) &&
8137             DEVI_BUSY_OWNED(ddi_get_parent(dip)));
8138 
8139         /* Return if already marked removed. */
8140         if (ndi_devi_device_isremoved(dip))
8141                 return (0);
8142 
8143         /* Mark the device as having been physically removed. */
8144         mutex_enter(&(DEVI(dip)->devi_lock));
8145         ndi_devi_set_hidden(dip);       /* invisible: lookup/snapshot */
8146         DEVI_SET_DEVICE_REMOVED(dip);
8147         DEVI_SET_EVREMOVE(dip);         /* this clears EVADD too */
8148         mutex_exit(&(DEVI(dip)->devi_lock));
8149 
8150         /* report remove (as 'removed') */
8151         i_ndi_devi_report_status_change(dip, NULL);
8152 
8153         /*
8154          * Invalidate the cache to ensure accurate
8155          * (di_state() & DI_DEVICE_REMOVED).
8156          */
8157         i_ddi_di_cache_invalidate();
8158 
8159         /*
8160          * Generate sysevent for those interested in removal (either
8161          * directly via private EC_DEVFS or indirectly via devfsadmd
8162          * generated EC_DEV). This will generate LDI DEVICE_REMOVE
8163          * event too.
8164          */
8165         i_ddi_log_devfs_device_remove(dip);
8166 
8167         return (1);             /* DEVICE_REMOVED state changed */
8168 }
8169 
8170 int
8171 ndi_devi_device_insert(dev_info_t *dip)
8172 {
8173         ASSERT(dip && ddi_get_parent(dip) &&
8174             DEVI_BUSY_OWNED(ddi_get_parent(dip)));
8175 
8176         /* Return if not marked removed. */
8177         if (!ndi_devi_device_isremoved(dip))
8178                 return (0);
8179 
8180         /* Mark the device as having been physically reinserted. */
8181         mutex_enter(&(DEVI(dip)->devi_lock));
8182         ndi_devi_clr_hidden(dip);       /* visible: lookup/snapshot */
8183         DEVI_SET_DEVICE_REINSERTED(dip);
8184         DEVI_SET_EVADD(dip);            /* this clears EVREMOVE too */
8185         mutex_exit(&(DEVI(dip)->devi_lock));
8186 
8187         /* report insert (as 'online') */
8188         i_ndi_devi_report_status_change(dip, NULL);
8189 
8190         /*
8191          * Invalidate the cache to ensure accurate
8192          * (di_state() & DI_DEVICE_REMOVED).
8193          */
8194         i_ddi_di_cache_invalidate();
8195 
8196         /*
8197          * Generate sysevent for those interested in removal (either directly
8198          * via EC_DEVFS or indirectly via devfsadmd generated EC_DEV).
8199          */
8200         i_ddi_log_devfs_device_insert(dip);
8201 
8202         return (1);             /* DEVICE_REMOVED state changed */
8203 }
8204 
8205 /*
8206  * ibt_hw_is_present() returns 0 when there is no IB hardware actively
8207  * running.  This is primarily useful for modules like rpcmod which
8208  * needs a quick check to decide whether or not it should try to use
8209  * InfiniBand
8210  */
8211 int ib_hw_status = 0;
8212 int
8213 ibt_hw_is_present()
8214 {
8215         return (ib_hw_status);
8216 }
8217 
8218 /*
8219  * ASSERT that constraint flag is not set and then set the "retire attempt"
8220  * flag.
8221  */
8222 int
8223 e_ddi_mark_retiring(dev_info_t *dip, void *arg)
8224 {
8225         char    **cons_array = (char **)arg;
8226         char    *path;
8227         int     constraint;
8228         int     i;
8229 
8230         constraint = 0;
8231         if (cons_array) {
8232                 path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
8233                 (void) ddi_pathname(dip, path);
8234                 for (i = 0; cons_array[i] != NULL; i++) {
8235                         if (strcmp(path, cons_array[i]) == 0) {
8236                                 constraint = 1;
8237                                 break;
8238                         }
8239                 }
8240                 kmem_free(path, MAXPATHLEN);
8241         }
8242 
8243         mutex_enter(&DEVI(dip)->devi_lock);
8244         ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT));
8245         DEVI(dip)->devi_flags |= DEVI_RETIRING;
8246         if (constraint)
8247                 DEVI(dip)->devi_flags |= DEVI_R_CONSTRAINT;
8248         mutex_exit(&DEVI(dip)->devi_lock);
8249 
8250         RIO_VERBOSE((CE_NOTE, "marked dip as undergoing retire process dip=%p",
8251             (void *)dip));
8252 
8253         if (constraint)
8254                 RIO_DEBUG((CE_NOTE, "marked dip as constrained, dip=%p",
8255                     (void *)dip));
8256 
8257         if (MDI_PHCI(dip))
8258                 mdi_phci_mark_retiring(dip, cons_array);
8259 
8260         return (DDI_WALK_CONTINUE);
8261 }
8262 
8263 static void
8264 free_array(char **cons_array)
8265 {
8266         int     i;
8267 
8268         if (cons_array == NULL)
8269                 return;
8270 
8271         for (i = 0; cons_array[i] != NULL; i++) {
8272                 kmem_free(cons_array[i], strlen(cons_array[i]) + 1);
8273         }
8274         kmem_free(cons_array, (i+1) * sizeof (char *));
8275 }
8276 
8277 /*
8278  * Walk *every* node in subtree and check if it blocks, allows or has no
8279  * comment on a proposed retire.
8280  */
8281 int
8282 e_ddi_retire_notify(dev_info_t *dip, void *arg)
8283 {
8284         int     *constraint = (int *)arg;
8285 
8286         RIO_DEBUG((CE_NOTE, "retire notify: dip = %p", (void *)dip));
8287 
8288         (void) e_ddi_offline_notify(dip);
8289 
8290         mutex_enter(&(DEVI(dip)->devi_lock));
8291         if (!(DEVI(dip)->devi_flags & DEVI_RETIRING)) {
8292                 RIO_DEBUG((CE_WARN, "retire notify: dip in retire "
8293                     "subtree is not marked: dip = %p", (void *)dip));
8294                 *constraint = 0;
8295         } else if (DEVI(dip)->devi_flags & DEVI_R_BLOCKED) {
8296                 ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT));
8297                 RIO_DEBUG((CE_NOTE, "retire notify: BLOCKED: dip = %p",
8298                     (void *)dip));
8299                 *constraint = 0;
8300         } else if (!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT)) {
8301                 RIO_DEBUG((CE_NOTE, "retire notify: NO CONSTRAINT: "
8302                     "dip = %p", (void *)dip));
8303                 *constraint = 0;
8304         } else {
8305                 RIO_DEBUG((CE_NOTE, "retire notify: CONSTRAINT set: "
8306                     "dip = %p", (void *)dip));
8307         }
8308         mutex_exit(&DEVI(dip)->devi_lock);
8309 
8310         if (MDI_PHCI(dip))
8311                 mdi_phci_retire_notify(dip, constraint);
8312 
8313         return (DDI_WALK_CONTINUE);
8314 }
8315 
8316 int
8317 e_ddi_retire_finalize(dev_info_t *dip, void *arg)
8318 {
8319         int constraint = *(int *)arg;
8320         int finalize;
8321         int phci_only;
8322 
8323         mutex_enter(&DEVI(dip)->devi_lock);
8324         if (!(DEVI(dip)->devi_flags & DEVI_RETIRING)) {
8325                 RIO_DEBUG((CE_WARN,
8326                     "retire: unmarked dip(%p) in retire subtree",
8327                     (void *)dip));
8328                 ASSERT(!(DEVI(dip)->devi_flags & DEVI_RETIRED));
8329                 ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT));
8330                 ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_BLOCKED));
8331                 mutex_exit(&DEVI(dip)->devi_lock);
8332                 return (DDI_WALK_CONTINUE);
8333         }
8334 
8335         /*
8336          * retire the device if constraints have been applied
8337          * or if the device is not in use
8338          */
8339         finalize = 0;
8340         if (constraint) {
8341                 ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
8342 
8343                 ASSERT(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT);
8344                 ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_BLOCKED));
8345                 DEVI(dip)->devi_flags &= ~DEVI_R_CONSTRAINT;
8346                 DEVI(dip)->devi_flags &= ~DEVI_RETIRING;
8347                 DEVI(dip)->devi_flags |= DEVI_RETIRED;
8348                 mutex_exit(&DEVI(dip)->devi_lock);
8349                 (void) spec_fence_snode(dip, NULL);
8350                 RIO_DEBUG((CE_NOTE, "Fenced off: dip = %p", (void *)dip));
8351                 e_ddi_offline_finalize(dip, DDI_SUCCESS);
8352         } else {
8353                 if (DEVI(dip)->devi_flags & DEVI_R_BLOCKED) {
8354                         ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT));
8355                         DEVI(dip)->devi_flags &= ~DEVI_R_BLOCKED;
8356                         DEVI(dip)->devi_flags &= ~DEVI_RETIRING;
8357                         /* we have already finalized during notify */
8358                 } else if (DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT) {
8359                         DEVI(dip)->devi_flags &= ~DEVI_R_CONSTRAINT;
8360                         DEVI(dip)->devi_flags &= ~DEVI_RETIRING;
8361                         finalize = 1;
8362                 } else {
8363                         DEVI(dip)->devi_flags &= ~DEVI_RETIRING;
8364                         /*
8365                          * even if no contracts, need to call finalize
8366                          * to clear the contract barrier on the dip
8367                          */
8368                         finalize = 1;
8369                 }
8370                 mutex_exit(&DEVI(dip)->devi_lock);
8371                 RIO_DEBUG((CE_NOTE, "finalize: NOT retired: dip = %p",
8372                     (void *)dip));
8373                 if (finalize)
8374                         e_ddi_offline_finalize(dip, DDI_FAILURE);
8375         }
8376 
8377         /*
8378          * phci_only variable indicates no client checking, just
8379          * offline the PHCI. We set that to 0 to enable client
8380          * checking
8381          */
8382         phci_only = 0;
8383         if (MDI_PHCI(dip))
8384                 mdi_phci_retire_finalize(dip, phci_only, arg);
8385 
8386         return (DDI_WALK_CONTINUE);
8387 }
8388 
8389 /*
8390  * Returns
8391  *      DDI_SUCCESS if constraints allow retire
8392  *      DDI_FAILURE if constraints don't allow retire.
8393  * cons_array is a NULL terminated array of node paths for
8394  * which constraints have already been applied.
8395  */
8396 int
8397 e_ddi_retire_device(char *path, char **cons_array)
8398 {
8399         dev_info_t      *dip;
8400         dev_info_t      *pdip;
8401         int             circ;
8402         int             circ2;
8403         int             constraint;
8404         char            *devnm;
8405 
8406         /*
8407          * First, lookup the device
8408          */
8409         dip = e_ddi_hold_devi_by_path(path, 0);
8410         if (dip == NULL) {
8411                 /*
8412                  * device does not exist. This device cannot be
8413                  * a critical device since it is not in use. Thus
8414                  * this device is always retireable. Return DDI_SUCCESS
8415                  * to indicate this. If this device is ever
8416                  * instantiated, I/O framework will consult the
8417                  * the persistent retire store, mark it as
8418                  * retired and fence it off.
8419                  */
8420                 RIO_DEBUG((CE_NOTE, "Retire device: device doesn't exist."
8421                     " NOP. Just returning SUCCESS. path=%s", path));
8422                 free_array(cons_array);
8423                 return (DDI_SUCCESS);
8424         }
8425 
8426         RIO_DEBUG((CE_NOTE, "Retire device: found dip = %p.", (void *)dip));
8427 
8428         pdip = ddi_get_parent(dip);
8429         ndi_hold_devi(pdip);
8430 
8431         /*
8432          * Run devfs_clean() in case dip has no constraints and is
8433          * not in use, so is retireable but there are dv_nodes holding
8434          * ref-count on the dip. Note that devfs_clean() always returns
8435          * success.
8436          */
8437         devnm = kmem_alloc(MAXNAMELEN + 1, KM_SLEEP);
8438         (void) ddi_deviname(dip, devnm);
8439         (void) devfs_clean(pdip, devnm + 1, DV_CLEAN_FORCE);
8440         kmem_free(devnm, MAXNAMELEN + 1);
8441 
8442         ndi_devi_enter(pdip, &circ);
8443 
8444         /* release hold from e_ddi_hold_devi_by_path */
8445         ndi_rele_devi(dip);
8446 
8447         /*
8448          * If it cannot make a determination, is_leaf_node() assumes
8449          * dip is a nexus.
8450          */
8451         (void) e_ddi_mark_retiring(dip, cons_array);
8452         if (!is_leaf_node(dip)) {
8453                 ndi_devi_enter(dip, &circ2);
8454                 ddi_walk_devs(ddi_get_child(dip), e_ddi_mark_retiring,
8455                     cons_array);
8456                 ndi_devi_exit(dip, circ2);
8457         }
8458         free_array(cons_array);
8459 
8460         /*
8461          * apply constraints
8462          */
8463         RIO_DEBUG((CE_NOTE, "retire: subtree retire notify: path = %s", path));
8464 
8465         constraint = 1; /* assume constraints allow retire */
8466         (void) e_ddi_retire_notify(dip, &constraint);
8467         if (!is_leaf_node(dip)) {
8468                 ndi_devi_enter(dip, &circ2);
8469                 ddi_walk_devs(ddi_get_child(dip), e_ddi_retire_notify,
8470                     &constraint);
8471                 ndi_devi_exit(dip, circ2);
8472         }
8473 
8474         /*
8475          * Now finalize the retire
8476          */
8477         (void) e_ddi_retire_finalize(dip, &constraint);
8478         if (!is_leaf_node(dip)) {
8479                 ndi_devi_enter(dip, &circ2);
8480                 ddi_walk_devs(ddi_get_child(dip), e_ddi_retire_finalize,
8481                     &constraint);
8482                 ndi_devi_exit(dip, circ2);
8483         }
8484 
8485         if (!constraint) {
8486                 RIO_DEBUG((CE_WARN, "retire failed: path = %s", path));
8487         } else {
8488                 RIO_DEBUG((CE_NOTE, "retire succeeded: path = %s", path));
8489         }
8490 
8491         ndi_devi_exit(pdip, circ);
8492         ndi_rele_devi(pdip);
8493         return (constraint ? DDI_SUCCESS : DDI_FAILURE);
8494 }
8495 
8496 static int
8497 unmark_and_unfence(dev_info_t *dip, void *arg)
8498 {
8499         char    *path = (char *)arg;
8500 
8501         ASSERT(path);
8502 
8503         (void) ddi_pathname(dip, path);
8504 
8505         mutex_enter(&DEVI(dip)->devi_lock);
8506         DEVI(dip)->devi_flags &= ~DEVI_RETIRED;
8507         DEVI_SET_DEVICE_ONLINE(dip);
8508         mutex_exit(&DEVI(dip)->devi_lock);
8509 
8510         RIO_VERBOSE((CE_NOTE, "Cleared RETIRED flag: dip=%p, path=%s",
8511             (void *)dip, path));
8512 
8513         (void) spec_unfence_snode(dip);
8514         RIO_DEBUG((CE_NOTE, "Unfenced device: %s", path));
8515 
8516         if (MDI_PHCI(dip))
8517                 mdi_phci_unretire(dip);
8518 
8519         return (DDI_WALK_CONTINUE);
8520 }
8521 
8522 struct find_dip {
8523         char    *fd_buf;
8524         char    *fd_path;
8525         dev_info_t *fd_dip;
8526 };
8527 
8528 static int
8529 find_dip_fcn(dev_info_t *dip, void *arg)
8530 {
8531         struct find_dip *findp = (struct find_dip *)arg;
8532 
8533         (void) ddi_pathname(dip, findp->fd_buf);
8534 
8535         if (strcmp(findp->fd_path, findp->fd_buf) != 0)
8536                 return (DDI_WALK_CONTINUE);
8537 
8538         ndi_hold_devi(dip);
8539         findp->fd_dip = dip;
8540 
8541         return (DDI_WALK_TERMINATE);
8542 }
8543 
8544 int
8545 e_ddi_unretire_device(char *path)
8546 {
8547         int             circ;
8548         int             circ2;
8549         char            *path2;
8550         dev_info_t      *pdip;
8551         dev_info_t      *dip;
8552         struct find_dip  find_dip;
8553 
8554         ASSERT(path);
8555         ASSERT(*path == '/');
8556 
8557         if (strcmp(path, "/") == 0) {
8558                 cmn_err(CE_WARN, "Root node cannot be retired. Skipping "
8559                     "device unretire: %s", path);
8560                 return (0);
8561         }
8562 
8563         /*
8564          * We can't lookup the dip (corresponding to path) via
8565          * e_ddi_hold_devi_by_path() because the dip may be offline
8566          * and may not attach. Use ddi_walk_devs() instead;
8567          */
8568         find_dip.fd_buf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
8569         find_dip.fd_path = path;
8570         find_dip.fd_dip = NULL;
8571 
8572         pdip = ddi_root_node();
8573 
8574         ndi_devi_enter(pdip, &circ);
8575         ddi_walk_devs(ddi_get_child(pdip), find_dip_fcn, &find_dip);
8576         ndi_devi_exit(pdip, circ);
8577 
8578         kmem_free(find_dip.fd_buf, MAXPATHLEN);
8579 
8580         if (find_dip.fd_dip == NULL) {
8581                 cmn_err(CE_WARN, "Device not found in device tree. Skipping "
8582                     "device unretire: %s", path);
8583                 return (0);
8584         }
8585 
8586         dip = find_dip.fd_dip;
8587 
8588         pdip = ddi_get_parent(dip);
8589 
8590         ndi_hold_devi(pdip);
8591 
8592         ndi_devi_enter(pdip, &circ);
8593 
8594         path2 = kmem_alloc(MAXPATHLEN, KM_SLEEP);
8595 
8596         (void) unmark_and_unfence(dip, path2);
8597         if (!is_leaf_node(dip)) {
8598                 ndi_devi_enter(dip, &circ2);
8599                 ddi_walk_devs(ddi_get_child(dip), unmark_and_unfence, path2);
8600                 ndi_devi_exit(dip, circ2);
8601         }
8602 
8603         kmem_free(path2, MAXPATHLEN);
8604 
8605         /* release hold from find_dip_fcn() */
8606         ndi_rele_devi(dip);
8607 
8608         ndi_devi_exit(pdip, circ);
8609 
8610         ndi_rele_devi(pdip);
8611 
8612         return (0);
8613 }
8614 
8615 /*
8616  * Called before attach on a dip that has been retired.
8617  */
8618 static int
8619 mark_and_fence(dev_info_t *dip, void *arg)
8620 {
8621         char    *fencepath = (char *)arg;
8622 
8623         /*
8624          * We have already decided to retire this device. The various
8625          * constraint checking should not be set.
8626          * NOTE that the retire flag may already be set due to
8627          * fenced -> detach -> fenced transitions.
8628          */
8629         mutex_enter(&DEVI(dip)->devi_lock);
8630         ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT));
8631         ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_BLOCKED));
8632         ASSERT(!(DEVI(dip)->devi_flags & DEVI_RETIRING));
8633         DEVI(dip)->devi_flags |= DEVI_RETIRED;
8634         mutex_exit(&DEVI(dip)->devi_lock);
8635         RIO_VERBOSE((CE_NOTE, "marked as RETIRED dip=%p", (void *)dip));
8636 
8637         if (fencepath) {
8638                 (void) spec_fence_snode(dip, NULL);
8639                 RIO_DEBUG((CE_NOTE, "Fenced: %s",
8640                     ddi_pathname(dip, fencepath)));
8641         }
8642 
8643         return (DDI_WALK_CONTINUE);
8644 }
8645 
8646 /*
8647  * Checks the retire database and:
8648  *
8649  * - if device is present in the retire database, marks the device retired
8650  *   and fences it off.
8651  * - if device is not in retire database, allows the device to attach normally
8652  *
8653  * To be called only by framework attach code on first attach attempt.
8654  *
8655  */
8656 static int
8657 i_ddi_check_retire(dev_info_t *dip)
8658 {
8659         char            *path;
8660         dev_info_t      *pdip;
8661         int             circ;
8662         int             phci_only;
8663         int             constraint;
8664 
8665         pdip = ddi_get_parent(dip);
8666 
8667         /*
8668          * Root dip is treated special and doesn't take this code path.
8669          * Also root can never be retired.
8670          */
8671         ASSERT(pdip);
8672         ASSERT(DEVI_BUSY_OWNED(pdip));
8673         ASSERT(i_ddi_node_state(dip) < DS_ATTACHED);
8674 
8675         path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
8676 
8677         (void) ddi_pathname(dip, path);
8678 
8679         RIO_VERBOSE((CE_NOTE, "Checking if dip should attach: dip=%p, path=%s",
8680             (void *)dip, path));
8681 
8682         /*
8683          * Check if this device is in the "retired" store i.e.  should
8684          * be retired. If not, we have nothing to do.
8685          */
8686         if (e_ddi_device_retired(path) == 0) {
8687                 RIO_VERBOSE((CE_NOTE, "device is NOT retired: path=%s", path));
8688                 if (DEVI(dip)->devi_flags & DEVI_RETIRED)
8689                         (void) e_ddi_unretire_device(path);
8690                 kmem_free(path, MAXPATHLEN);
8691                 return (0);
8692         }
8693 
8694         RIO_DEBUG((CE_NOTE, "attach: device is retired: path=%s", path));
8695 
8696         /*
8697          * Mark dips and fence off snodes (if any)
8698          */
8699         RIO_DEBUG((CE_NOTE, "attach: Mark and fence subtree: path=%s", path));
8700         (void) mark_and_fence(dip, path);
8701         if (!is_leaf_node(dip)) {
8702                 ndi_devi_enter(dip, &circ);
8703                 ddi_walk_devs(ddi_get_child(dip), mark_and_fence, path);
8704                 ndi_devi_exit(dip, circ);
8705         }
8706 
8707         kmem_free(path, MAXPATHLEN);
8708 
8709         /*
8710          * We don't want to check the client. We just want to
8711          * offline the PHCI
8712          */
8713         phci_only = 1;
8714         constraint = 1;
8715         if (MDI_PHCI(dip))
8716                 mdi_phci_retire_finalize(dip, phci_only, &constraint);
8717         return (1);
8718 }
8719 
8720 
8721 #define VAL_ALIAS(array, x)     (strlen(array[x].pair_alias))
8722 #define VAL_CURR(array, x)      (strlen(array[x].pair_curr))
8723 #define SWAP(array, x, y)                       \
8724 {                                               \
8725         alias_pair_t tmpair = array[x];         \
8726         array[x] = array[y];                    \
8727         array[y] = tmpair;                      \
8728 }
8729 
8730 static int
8731 partition_curr(alias_pair_t *array, int start, int end)
8732 {
8733         int     i = start - 1;
8734         int     j = end + 1;
8735         int     pivot = start;
8736 
8737         for (;;) {
8738                 do {
8739                         j--;
8740                 } while (VAL_CURR(array, j) > VAL_CURR(array, pivot));
8741 
8742                 do {
8743                         i++;
8744                 } while (VAL_CURR(array, i) < VAL_CURR(array, pivot));
8745 
8746                 if (i < j)
8747                         SWAP(array, i, j)
8748                 else
8749                         return (j);
8750         }
8751 }
8752 
8753 static int
8754 partition_aliases(alias_pair_t *array, int start, int end)
8755 {
8756         int     i = start - 1;
8757         int     j = end + 1;
8758         int     pivot = start;
8759 
8760         for (;;) {
8761                 do {
8762                         j--;
8763                 } while (VAL_ALIAS(array, j) > VAL_ALIAS(array, pivot));
8764 
8765                 do {
8766                         i++;
8767                 } while (VAL_ALIAS(array, i) < VAL_ALIAS(array, pivot));
8768 
8769                 if (i < j)
8770                         SWAP(array, i, j)
8771                 else
8772                         return (j);
8773         }
8774 }
8775 static void
8776 sort_alias_pairs(alias_pair_t *array, int start, int end)
8777 {
8778         int mid;
8779 
8780         if (start < end) {
8781                 mid = partition_aliases(array, start, end);
8782                 sort_alias_pairs(array, start, mid);
8783                 sort_alias_pairs(array, mid + 1, end);
8784         }
8785 }
8786 
8787 static void
8788 sort_curr_pairs(alias_pair_t *array, int start, int end)
8789 {
8790         int mid;
8791 
8792         if (start < end) {
8793                 mid = partition_curr(array, start, end);
8794                 sort_curr_pairs(array, start, mid);
8795                 sort_curr_pairs(array, mid + 1, end);
8796         }
8797 }
8798 
8799 static void
8800 create_sorted_pairs(plat_alias_t *pali, int npali)
8801 {
8802         int             i;
8803         int             j;
8804         int             k;
8805         int             count;
8806 
8807         count = 0;
8808         for (i = 0; i < npali; i++) {
8809                 count += pali[i].pali_naliases;
8810         }
8811 
8812         ddi_aliases.dali_alias_pairs = kmem_zalloc(
8813             (sizeof (alias_pair_t)) * count, KM_NOSLEEP);
8814         if (ddi_aliases.dali_alias_pairs == NULL) {
8815                 cmn_err(CE_PANIC, "alias path-pair alloc failed");
8816                 /*NOTREACHED*/
8817         }
8818 
8819         ddi_aliases.dali_curr_pairs = kmem_zalloc(
8820             (sizeof (alias_pair_t)) * count, KM_NOSLEEP);
8821         if (ddi_aliases.dali_curr_pairs == NULL) {
8822                 cmn_err(CE_PANIC, "curr path-pair alloc failed");
8823                 /*NOTREACHED*/
8824         }
8825 
8826         for (i = 0, k = 0; i < npali; i++) {
8827                 for (j = 0; j < pali[i].pali_naliases; j++, k++) {
8828                         ddi_aliases.dali_alias_pairs[k].pair_curr =
8829                             ddi_aliases.dali_curr_pairs[k].pair_curr =
8830                             pali[i].pali_current;
8831                         ddi_aliases.dali_alias_pairs[k].pair_alias =
8832                             ddi_aliases.dali_curr_pairs[k].pair_alias =
8833                             pali[i].pali_aliases[j];
8834                 }
8835         }
8836 
8837         ASSERT(k == count);
8838 
8839         ddi_aliases.dali_num_pairs = count;
8840 
8841         /* Now sort the array based on length of pair_alias */
8842         sort_alias_pairs(ddi_aliases.dali_alias_pairs, 0, count - 1);
8843         sort_curr_pairs(ddi_aliases.dali_curr_pairs, 0, count - 1);
8844 }
8845 
8846 void
8847 ddi_register_aliases(plat_alias_t *pali, uint64_t npali)
8848 {
8849 
8850         ASSERT((pali == NULL) ^ (npali != 0));
8851 
8852         if (npali == 0) {
8853                 ddi_err(DER_PANIC, NULL, "npali == 0");
8854                 /*NOTREACHED*/
8855         }
8856 
8857         if (ddi_aliases_present == B_TRUE) {
8858                 ddi_err(DER_PANIC, NULL, "multiple init");
8859                 /*NOTREACHED*/
8860         }
8861 
8862         ddi_aliases.dali_alias_TLB = mod_hash_create_strhash(
8863             "ddi-alias-tlb", DDI_ALIAS_HASH_SIZE, mod_hash_null_valdtor);
8864         if (ddi_aliases.dali_alias_TLB == NULL) {
8865                 ddi_err(DER_PANIC, NULL, "alias TLB hash alloc failed");
8866                 /*NOTREACHED*/
8867         }
8868 
8869         ddi_aliases.dali_curr_TLB = mod_hash_create_strhash(
8870             "ddi-curr-tlb", DDI_ALIAS_HASH_SIZE, mod_hash_null_valdtor);
8871         if (ddi_aliases.dali_curr_TLB == NULL) {
8872                 ddi_err(DER_PANIC, NULL, "curr TLB hash alloc failed");
8873                 /*NOTREACHED*/
8874         }
8875 
8876         create_sorted_pairs(pali, npali);
8877 
8878         tsd_create(&tsd_ddi_redirect, NULL);
8879 
8880         ddi_aliases_present = B_TRUE;
8881 }
8882 
8883 static dev_info_t *
8884 path_to_dip(char *path)
8885 {
8886         dev_info_t      *currdip;
8887         int             error;
8888         char            *pdup;
8889 
8890         pdup = ddi_strdup(path, KM_NOSLEEP);
8891         if (pdup == NULL) {
8892                 cmn_err(CE_PANIC, "path strdup failed: %s", path);
8893                 /*NOTREACHED*/
8894         }
8895 
8896         error = resolve_pathname(pdup, &currdip, NULL, NULL);
8897 
8898         kmem_free(pdup, strlen(path) + 1);
8899 
8900         return (error ? NULL : currdip);
8901 }
8902 
8903 dev_info_t *
8904 ddi_alias_to_currdip(char *alias, int i)
8905 {
8906         alias_pair_t *pair;
8907         char *curr;
8908         dev_info_t *currdip = NULL;
8909         char *aliasdup;
8910         int rv, len;
8911 
8912         pair = &(ddi_aliases.dali_alias_pairs[i]);
8913         len = strlen(pair->pair_alias);
8914 
8915         curr = NULL;
8916         aliasdup = ddi_strdup(alias, KM_NOSLEEP);
8917         if (aliasdup == NULL) {
8918                 cmn_err(CE_PANIC, "aliasdup alloc failed");
8919                 /*NOTREACHED*/
8920         }
8921 
8922         if (strncmp(alias, pair->pair_alias, len)  != 0)
8923                 goto out;
8924 
8925         if (alias[len] != '/' && alias[len] != '\0')
8926                 goto out;
8927 
8928         curr = kmem_alloc(MAXPATHLEN, KM_NOSLEEP);
8929         if (curr == NULL) {
8930                 cmn_err(CE_PANIC, "curr alloc failed");
8931                 /*NOTREACHED*/
8932         }
8933         (void) strlcpy(curr, pair->pair_curr, MAXPATHLEN);
8934         if (alias[len] == '/') {
8935                 (void) strlcat(curr, "/", MAXPATHLEN);
8936                 (void) strlcat(curr, &alias[len + 1], MAXPATHLEN);
8937         }
8938 
8939         currdip = path_to_dip(curr);
8940 
8941 out:
8942         if (currdip) {
8943                 rv = mod_hash_insert(ddi_aliases.dali_alias_TLB,
8944                     (mod_hash_key_t)aliasdup, (mod_hash_val_t)curr);
8945                 if (rv != 0) {
8946                         kmem_free(curr, MAXPATHLEN);
8947                         strfree(aliasdup);
8948                 }
8949         } else {
8950                 rv = mod_hash_insert(ddi_aliases.dali_alias_TLB,
8951                     (mod_hash_key_t)aliasdup, (mod_hash_val_t)NULL);
8952                 if (rv != 0) {
8953                         strfree(aliasdup);
8954                 }
8955                 if (curr)
8956                         kmem_free(curr, MAXPATHLEN);
8957         }
8958 
8959         return (currdip);
8960 }
8961 
8962 char *
8963 ddi_curr_to_alias(char *curr, int i)
8964 {
8965         alias_pair_t    *pair;
8966         char            *alias;
8967         char            *currdup;
8968         int             len;
8969         int             rv;
8970 
8971         pair = &(ddi_aliases.dali_curr_pairs[i]);
8972 
8973         len = strlen(pair->pair_curr);
8974 
8975         alias = NULL;
8976 
8977         currdup = ddi_strdup(curr, KM_NOSLEEP);
8978         if (currdup == NULL) {
8979                 cmn_err(CE_PANIC, "currdup alloc failed");
8980                 /*NOTREACHED*/
8981         }
8982 
8983         if (strncmp(curr, pair->pair_curr, len) != 0)
8984                 goto out;
8985 
8986         if (curr[len] != '/' && curr[len] != '\0')
8987                 goto out;
8988 
8989         alias = kmem_alloc(MAXPATHLEN, KM_NOSLEEP);
8990         if (alias == NULL) {
8991                 cmn_err(CE_PANIC, "alias alloc failed");
8992                 /*NOTREACHED*/
8993         }
8994 
8995         (void) strlcpy(alias, pair->pair_alias, MAXPATHLEN);
8996         if (curr[len] == '/') {
8997                 (void) strlcat(alias, "/", MAXPATHLEN);
8998                 (void) strlcat(alias, &curr[len + 1], MAXPATHLEN);
8999         }
9000 
9001         if (e_ddi_path_to_instance(alias) == NULL) {
9002                 kmem_free(alias, MAXPATHLEN);
9003                 alias = NULL;
9004         }
9005 
9006 out:
9007         rv = mod_hash_insert(ddi_aliases.dali_curr_TLB,
9008             (mod_hash_key_t)currdup, (mod_hash_val_t)alias);
9009         if (rv != 0) {
9010                 strfree(currdup);
9011         }
9012 
9013         return (alias);
9014 }
9015 
9016 dev_info_t *
9017 ddi_alias_redirect(char *alias)
9018 {
9019         char            *curr;
9020         dev_info_t      *currdip;
9021         int             i;
9022 
9023         if (ddi_aliases_present == B_FALSE)
9024                 return (NULL);
9025 
9026         if (tsd_get(tsd_ddi_redirect))
9027                 return (NULL);
9028 
9029         (void) tsd_set(tsd_ddi_redirect, (void *)1);
9030 
9031         ASSERT(ddi_aliases.dali_alias_TLB);
9032         ASSERT(ddi_aliases.dali_alias_pairs);
9033 
9034         curr = NULL;
9035         if (mod_hash_find(ddi_aliases.dali_alias_TLB,
9036             (mod_hash_key_t)alias, (mod_hash_val_t *)&curr) == 0) {
9037                 currdip = curr ? path_to_dip(curr) : NULL;
9038                 goto out;
9039         }
9040 
9041         /* The TLB has no translation, do it the hard way */
9042         currdip = NULL;
9043         for (i = ddi_aliases.dali_num_pairs - 1; i >= 0; i--) {
9044                 currdip = ddi_alias_to_currdip(alias, i);
9045                 if (currdip)
9046                         break;
9047         }
9048 out:
9049         (void) tsd_set(tsd_ddi_redirect, NULL);
9050 
9051         return (currdip);
9052 }
9053 
9054 char *
9055 ddi_curr_redirect(char *curr)
9056 {
9057         char *alias;
9058         int i;
9059 
9060         if (ddi_aliases_present == B_FALSE)
9061                 return (NULL);
9062 
9063         if (tsd_get(tsd_ddi_redirect))
9064                 return (NULL);
9065 
9066         (void) tsd_set(tsd_ddi_redirect, (void *)1);
9067 
9068         ASSERT(ddi_aliases.dali_curr_TLB);
9069         ASSERT(ddi_aliases.dali_curr_pairs);
9070 
9071         alias = NULL;
9072         if (mod_hash_find(ddi_aliases.dali_curr_TLB,
9073             (mod_hash_key_t)curr, (mod_hash_val_t *)&alias) == 0) {
9074                 goto out;
9075         }
9076 
9077 
9078         /* The TLB has no translation, do it the slow way */
9079         alias = NULL;
9080         for (i = ddi_aliases.dali_num_pairs - 1; i >= 0; i--) {
9081                 alias = ddi_curr_to_alias(curr, i);
9082                 if (alias)
9083                         break;
9084         }
9085 
9086 out:
9087         (void) tsd_set(tsd_ddi_redirect, NULL);
9088 
9089         return (alias);
9090 }
9091 
9092 void
9093 ddi_err(ddi_err_t ade, dev_info_t *rdip, const char *fmt, ...)
9094 {
9095         va_list ap;
9096         char strbuf[256];
9097         char *buf;
9098         size_t buflen, tlen;
9099         int ce;
9100         int de;
9101         const char *fmtbad = "Invalid arguments to ddi_err()";
9102 
9103         de = DER_CONT;
9104         strbuf[1] = '\0';
9105 
9106         switch (ade) {
9107         case DER_CONS:
9108                 strbuf[0] = '^';
9109                 break;
9110         case DER_LOG:
9111                 strbuf[0] = '!';
9112                 break;
9113         case DER_VERB:
9114                 strbuf[0] = '?';
9115                 break;
9116         default:
9117                 strbuf[0] = '\0';
9118                 de = ade;
9119                 break;
9120         }
9121 
9122         tlen = strlen(strbuf);
9123         buf = strbuf + tlen;
9124         buflen = sizeof (strbuf) - tlen;
9125 
9126         if (rdip && ddi_get_instance(rdip) == -1) {
9127                 (void) snprintf(buf, buflen, "%s: ",
9128                     ddi_driver_name(rdip));
9129         } else if (rdip) {
9130                 (void) snprintf(buf, buflen, "%s%d: ",
9131                     ddi_driver_name(rdip), ddi_get_instance(rdip));
9132         }
9133 
9134         tlen = strlen(strbuf);
9135         buf = strbuf + tlen;
9136         buflen = sizeof (strbuf) - tlen;
9137 
9138         va_start(ap, fmt);
9139         switch (de) {
9140         case DER_CONT:
9141                 (void) vsnprintf(buf, buflen, fmt, ap);
9142                 if (ade != DER_CONT) {
9143                         (void) strlcat(strbuf, "\n", sizeof (strbuf));
9144                 }
9145                 ce = CE_CONT;
9146                 break;
9147         case DER_NOTE:
9148                 (void) vsnprintf(buf, buflen, fmt, ap);
9149                 ce = CE_NOTE;
9150                 break;
9151         case DER_WARN:
9152                 (void) vsnprintf(buf, buflen, fmt, ap);
9153                 ce = CE_WARN;
9154                 break;
9155         case DER_MODE:
9156                 (void) vsnprintf(buf, buflen, fmt, ap);
9157                 if (ddi_err_panic == B_TRUE) {
9158                         ce = CE_PANIC;
9159                 } else {
9160                         ce = CE_WARN;
9161                 }
9162                 break;
9163         case DER_DEBUG:
9164                 (void) snprintf(buf, buflen, "DEBUG: ");
9165                 tlen = strlen("DEBUG: ");
9166                 (void) vsnprintf(buf + tlen, buflen - tlen, fmt, ap);
9167                 ce = CE_CONT;
9168                 break;
9169         case DER_PANIC:
9170                 (void) vsnprintf(buf, buflen, fmt, ap);
9171                 ce = CE_PANIC;
9172                 break;
9173         case DER_INVALID:
9174         default:
9175                 (void) snprintf(buf, buflen, fmtbad);
9176                 tlen = strlen(fmtbad);
9177                 (void) vsnprintf(buf + tlen, buflen - tlen, fmt, ap);
9178                 ce = CE_PANIC;
9179                 break;
9180         }
9181         va_end(ap);
9182 
9183         cmn_err(ce, strbuf);
9184 }
9185 
9186 /*ARGSUSED*/
9187 void
9188 ddi_mem_update(uint64_t addr, uint64_t size)
9189 {
9190 #if defined(__x86) && !defined(__xpv)
9191         extern void immu_physmem_update(uint64_t addr, uint64_t size);
9192         immu_physmem_update(addr, size);
9193 #else
9194         /*LINTED*/
9195         ;
9196 #endif
9197 }