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