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