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