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