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