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