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