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