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