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 */ 24 25 #include <sys/note.h> 26 #include <sys/t_lock.h> 27 #include <sys/cmn_err.h> 28 #include <sys/instance.h> 29 #include <sys/conf.h> 30 #include <sys/stat.h> 31 #include <sys/ddi.h> 32 #include <sys/hwconf.h> 33 #include <sys/sunddi.h> 34 #include <sys/sunndi.h> 35 #include <sys/ddi_impldefs.h> 36 #include <sys/ndi_impldefs.h> 37 #include <sys/modctl.h> 38 #include <sys/contract/device_impl.h> 39 #include <sys/dacf.h> 40 #include <sys/promif.h> 41 #include <sys/pci.h> 42 #include <sys/cpuvar.h> 43 #include <sys/pathname.h> 44 #include <sys/taskq.h> 45 #include <sys/sysevent.h> 46 #include <sys/sunmdi.h> 47 #include <sys/stream.h> 48 #include <sys/strsubr.h> 49 #include <sys/fs/snode.h> 50 #include <sys/fs/dv_node.h> 51 #include <sys/reboot.h> 52 #include <sys/sysmacros.h> 53 #include <sys/systm.h> 54 #include <sys/fs/sdev_impl.h> 55 #include <sys/sunldi.h> 56 #include <sys/sunldi_impl.h> 57 #include <sys/bootprops.h> 58 #include <sys/varargs.h> 59 #include <sys/modhash.h> 60 #include <sys/instance.h> 61 62 #if defined(__amd64) && !defined(__xpv) 63 #include <sys/iommulib.h> 64 #endif 65 66 #ifdef DEBUG 67 int ddidebug = DDI_AUDIT; 68 #else 69 int ddidebug = 0; 70 #endif 71 72 #define MT_CONFIG_OP 0 73 #define MT_UNCONFIG_OP 1 74 75 /* Multi-threaded configuration */ 76 struct mt_config_handle { 77 kmutex_t mtc_lock; 78 kcondvar_t mtc_cv; 79 int mtc_thr_count; 80 dev_info_t *mtc_pdip; /* parent dip for mt_config_children */ 81 dev_info_t **mtc_fdip; /* "a" dip where unconfigure failed */ 82 major_t mtc_parmajor; /* parent major for mt_config_driver */ 83 major_t mtc_major; 84 int mtc_flags; 85 int mtc_op; /* config or unconfig */ 86 int mtc_error; /* operation error */ 87 struct brevq_node **mtc_brevqp; /* outstanding branch events queue */ 88 #ifdef DEBUG 89 int total_time; 90 timestruc_t start_time; 91 #endif /* DEBUG */ 92 }; 93 94 struct devi_nodeid { 95 pnode_t nodeid; 96 dev_info_t *dip; 97 struct devi_nodeid *next; 98 }; 99 100 struct devi_nodeid_list { 101 kmutex_t dno_lock; /* Protects other fields */ 102 struct devi_nodeid *dno_head; /* list of devi nodeid elements */ 103 struct devi_nodeid *dno_free; /* Free list */ 104 uint_t dno_list_length; /* number of dips in list */ 105 }; 106 107 /* used to keep track of branch remove events to be generated */ 108 struct brevq_node { 109 char *brn_deviname; 110 struct brevq_node *brn_sibling; 111 struct brevq_node *brn_child; 112 }; 113 114 static struct devi_nodeid_list devi_nodeid_list; 115 static struct devi_nodeid_list *devimap = &devi_nodeid_list; 116 117 /* 118 * Well known nodes which are attached first at boot time. 119 */ 120 dev_info_t *top_devinfo; /* root of device tree */ 121 dev_info_t *options_dip; 122 dev_info_t *pseudo_dip; 123 dev_info_t *clone_dip; 124 dev_info_t *scsi_vhci_dip; /* MPXIO dip */ 125 major_t clone_major; 126 127 /* 128 * A non-global zone's /dev is derived from the device tree. 129 * This generation number serves to indicate when a zone's 130 * /dev may need to be updated. 131 */ 132 volatile ulong_t devtree_gen; /* generation number */ 133 134 /* block all future dev_info state changes */ 135 hrtime_t volatile devinfo_freeze = 0; 136 137 /* number of dev_info attaches/detaches currently in progress */ 138 static ulong_t devinfo_attach_detach = 0; 139 140 extern int sys_shutdown; 141 extern kmutex_t global_vhci_lock; 142 143 /* bitset of DS_SYSAVAIL & DS_RECONFIG - no races, no lock */ 144 static int devname_state = 0; 145 146 /* 147 * The devinfo snapshot cache and related variables. 148 * The only field in the di_cache structure that needs initialization 149 * is the mutex (cache_lock). However, since this is an adaptive mutex 150 * (MUTEX_DEFAULT) - it is automatically initialized by being allocated 151 * in zeroed memory (static storage class). Therefore no explicit 152 * initialization of the di_cache structure is needed. 153 */ 154 struct di_cache di_cache = {1}; 155 int di_cache_debug = 0; 156 157 /* For ddvis, which needs pseudo children under PCI */ 158 int pci_allow_pseudo_children = 0; 159 160 /* Allow path-oriented alias driver binding on driver.conf enumerated nodes */ 161 int driver_conf_allow_path_alias = 1; 162 163 /* 164 * The following switch is for service people, in case a 165 * 3rd party driver depends on identify(9e) being called. 166 */ 167 int identify_9e = 0; 168 169 /* 170 * Add flag so behaviour of preventing attach for retired persistant nodes 171 * can be disabled. 172 */ 173 int retire_prevents_attach = 1; 174 175 int mtc_off; /* turn off mt config */ 176 177 int quiesce_debug = 0; 178 179 boolean_t ddi_aliases_present = B_FALSE; 180 ddi_alias_t ddi_aliases; 181 uint_t tsd_ddi_redirect; 182 183 #define DDI_ALIAS_HASH_SIZE (2700) 184 185 static kmem_cache_t *ddi_node_cache; /* devinfo node cache */ 186 static devinfo_log_header_t *devinfo_audit_log; /* devinfo log */ 187 static int devinfo_log_size; /* size in pages */ 188 189 boolean_t ddi_err_panic = B_FALSE; 190 191 static int lookup_compatible(dev_info_t *, uint_t); 192 static char *encode_composite_string(char **, uint_t, size_t *, uint_t); 193 static void link_to_driver_list(dev_info_t *); 194 static void unlink_from_driver_list(dev_info_t *); 195 static void add_to_dn_list(struct devnames *, dev_info_t *); 196 static void remove_from_dn_list(struct devnames *, dev_info_t *); 197 static dev_info_t *find_duplicate_child(); 198 static void add_global_props(dev_info_t *); 199 static void remove_global_props(dev_info_t *); 200 static int uninit_node(dev_info_t *); 201 static void da_log_init(void); 202 static void da_log_enter(dev_info_t *); 203 static int walk_devs(dev_info_t *, int (*f)(dev_info_t *, void *), void *, int); 204 static int reset_nexus_flags(dev_info_t *, void *); 205 static void ddi_optimize_dtree(dev_info_t *); 206 static int is_leaf_node(dev_info_t *); 207 static struct mt_config_handle *mt_config_init(dev_info_t *, dev_info_t **, 208 int, major_t, int, struct brevq_node **); 209 static void mt_config_children(struct mt_config_handle *); 210 static void mt_config_driver(struct mt_config_handle *); 211 static int mt_config_fini(struct mt_config_handle *); 212 static int devi_unconfig_common(dev_info_t *, dev_info_t **, int, major_t, 213 struct brevq_node **); 214 static int 215 ndi_devi_config_obp_args(dev_info_t *parent, char *devnm, 216 dev_info_t **childp, int flags); 217 static void i_link_vhci_node(dev_info_t *); 218 static void ndi_devi_exit_and_wait(dev_info_t *dip, 219 int circular, clock_t end_time); 220 static int ndi_devi_unbind_driver(dev_info_t *dip); 221 222 static int i_ddi_check_retire(dev_info_t *dip); 223 224 static void quiesce_one_device(dev_info_t *, void *); 225 226 dev_info_t *ddi_alias_redirect(char *alias); 227 char *ddi_curr_redirect(char *currpath); 228 229 230 /* 231 * dev_info cache and node management 232 */ 233 234 /* initialize dev_info node cache */ 235 void 236 i_ddi_node_cache_init() 237 { 238 ASSERT(ddi_node_cache == NULL); 239 ddi_node_cache = kmem_cache_create("dev_info_node_cache", 240 sizeof (struct dev_info), 0, NULL, NULL, NULL, NULL, NULL, 0); 241 242 if (ddidebug & DDI_AUDIT) 243 da_log_init(); 244 } 245 246 247 /* 248 * Allocating a dev_info node, callable from interrupt context with KM_NOSLEEP 249 * The allocated node has a reference count of 0. 250 */ 251 dev_info_t * 252 i_ddi_alloc_node(dev_info_t *pdip, char *node_name, pnode_t nodeid, 253 int instance, ddi_prop_t *sys_prop, int flag) 254 { 255 struct dev_info *devi; 256 struct devi_nodeid *elem; 257 static char failed[] = "i_ddi_alloc_node: out of memory"; 258 259 ASSERT(node_name != NULL); 260 261 if ((devi = kmem_cache_alloc(ddi_node_cache, flag)) == NULL) { 262 cmn_err(CE_NOTE, failed); 263 return (NULL); 264 } 265 266 bzero(devi, sizeof (struct dev_info)); 267 268 if (devinfo_audit_log) { 269 devi->devi_audit = kmem_zalloc(sizeof (devinfo_audit_t), flag); 270 if (devi->devi_audit == NULL) 271 goto fail; 272 } 273 274 if ((devi->devi_node_name = i_ddi_strdup(node_name, flag)) == NULL) 275 goto fail; 276 277 /* default binding name is node name */ 278 devi->devi_binding_name = devi->devi_node_name; 279 devi->devi_major = DDI_MAJOR_T_NONE; /* unbound by default */ 280 281 /* 282 * Make a copy of system property 283 */ 284 if (sys_prop && 285 (devi->devi_sys_prop_ptr = i_ddi_prop_list_dup(sys_prop, flag)) 286 == NULL) 287 goto fail; 288 289 /* 290 * Assign devi_nodeid, devi_node_class, devi_node_attributes 291 * according to the following algorithm: 292 * 293 * nodeid arg node class node attributes 294 * 295 * DEVI_PSEUDO_NODEID DDI_NC_PSEUDO A 296 * DEVI_SID_NODEID DDI_NC_PSEUDO A,P 297 * DEVI_SID_HIDDEN_NODEID DDI_NC_PSEUDO A,P,H 298 * DEVI_SID_HP_NODEID DDI_NC_PSEUDO A,P,h 299 * DEVI_SID_HP_HIDDEN_NODEID DDI_NC_PSEUDO A,P,H,h 300 * other DDI_NC_PROM P 301 * 302 * Where A = DDI_AUTO_ASSIGNED_NODEID (auto-assign a nodeid) 303 * and P = DDI_PERSISTENT 304 * and H = DDI_HIDDEN_NODE 305 * and h = DDI_HOTPLUG_NODE 306 * 307 * auto-assigned nodeids are also auto-freed. 308 */ 309 devi->devi_node_attributes = 0; 310 switch (nodeid) { 311 case DEVI_SID_HIDDEN_NODEID: 312 devi->devi_node_attributes |= DDI_HIDDEN_NODE; 313 goto sid; 314 315 case DEVI_SID_HP_NODEID: 316 devi->devi_node_attributes |= DDI_HOTPLUG_NODE; 317 goto sid; 318 319 case DEVI_SID_HP_HIDDEN_NODEID: 320 devi->devi_node_attributes |= DDI_HIDDEN_NODE; 321 devi->devi_node_attributes |= DDI_HOTPLUG_NODE; 322 goto sid; 323 324 case DEVI_SID_NODEID: 325 sid: devi->devi_node_attributes |= DDI_PERSISTENT; 326 if ((elem = kmem_zalloc(sizeof (*elem), flag)) == NULL) 327 goto fail; 328 /*FALLTHROUGH*/ 329 330 case DEVI_PSEUDO_NODEID: 331 devi->devi_node_attributes |= DDI_AUTO_ASSIGNED_NODEID; 332 devi->devi_node_class = DDI_NC_PSEUDO; 333 if (impl_ddi_alloc_nodeid(&devi->devi_nodeid)) { 334 panic("i_ddi_alloc_node: out of nodeids"); 335 /*NOTREACHED*/ 336 } 337 break; 338 339 default: 340 if ((elem = kmem_zalloc(sizeof (*elem), flag)) == NULL) 341 goto fail; 342 343 /* 344 * the nodetype is 'prom', try to 'take' the nodeid now. 345 * This requires memory allocation, so check for failure. 346 */ 347 if (impl_ddi_take_nodeid(nodeid, flag) != 0) { 348 kmem_free(elem, sizeof (*elem)); 349 goto fail; 350 } 351 352 devi->devi_nodeid = nodeid; 353 devi->devi_node_class = DDI_NC_PROM; 354 devi->devi_node_attributes = DDI_PERSISTENT; 355 break; 356 } 357 358 if (ndi_dev_is_persistent_node((dev_info_t *)devi)) { 359 mutex_enter(&devimap->dno_lock); 360 elem->next = devimap->dno_free; 361 devimap->dno_free = elem; 362 mutex_exit(&devimap->dno_lock); 363 } 364 365 /* 366 * Instance is normally initialized to -1. In a few special 367 * cases, the caller may specify an instance (e.g. CPU nodes). 368 */ 369 devi->devi_instance = instance; 370 371 /* 372 * set parent and bus_ctl parent 373 */ 374 devi->devi_parent = DEVI(pdip); 375 devi->devi_bus_ctl = DEVI(pdip); 376 377 NDI_CONFIG_DEBUG((CE_CONT, 378 "i_ddi_alloc_node: name=%s id=%d\n", node_name, devi->devi_nodeid)); 379 380 cv_init(&(devi->devi_cv), NULL, CV_DEFAULT, NULL); 381 mutex_init(&(devi->devi_lock), NULL, MUTEX_DEFAULT, NULL); 382 mutex_init(&(devi->devi_pm_lock), NULL, MUTEX_DEFAULT, NULL); 383 mutex_init(&(devi->devi_pm_busy_lock), NULL, MUTEX_DEFAULT, NULL); 384 385 RIO_TRACE((CE_NOTE, "i_ddi_alloc_node: Initing contract fields: " 386 "dip=%p, name=%s", (void *)devi, node_name)); 387 388 mutex_init(&(devi->devi_ct_lock), NULL, MUTEX_DEFAULT, NULL); 389 cv_init(&(devi->devi_ct_cv), NULL, CV_DEFAULT, NULL); 390 devi->devi_ct_count = -1; /* counter not in use if -1 */ 391 list_create(&(devi->devi_ct), sizeof (cont_device_t), 392 offsetof(cont_device_t, cond_next)); 393 394 i_ddi_set_node_state((dev_info_t *)devi, DS_PROTO); 395 da_log_enter((dev_info_t *)devi); 396 return ((dev_info_t *)devi); 397 398 fail: 399 if (devi->devi_sys_prop_ptr) 400 i_ddi_prop_list_delete(devi->devi_sys_prop_ptr); 401 if (devi->devi_node_name) 402 kmem_free(devi->devi_node_name, strlen(node_name) + 1); 403 if (devi->devi_audit) 404 kmem_free(devi->devi_audit, sizeof (devinfo_audit_t)); 405 kmem_cache_free(ddi_node_cache, devi); 406 cmn_err(CE_NOTE, failed); 407 return (NULL); 408 } 409 410 /* 411 * free a dev_info structure. 412 * NB. Not callable from interrupt since impl_ddi_free_nodeid may block. 413 */ 414 void 415 i_ddi_free_node(dev_info_t *dip) 416 { 417 struct dev_info *devi = DEVI(dip); 418 struct devi_nodeid *elem; 419 420 ASSERT(devi->devi_ref == 0); 421 ASSERT(devi->devi_addr == NULL); 422 ASSERT(devi->devi_node_state == DS_PROTO); 423 ASSERT(devi->devi_child == NULL); 424 ASSERT(devi->devi_hp_hdlp == NULL); 425 426 /* free devi_addr_buf allocated by ddi_set_name_addr() */ 427 if (devi->devi_addr_buf) 428 kmem_free(devi->devi_addr_buf, 2 * MAXNAMELEN); 429 430 if (i_ndi_dev_is_auto_assigned_node(dip)) 431 impl_ddi_free_nodeid(DEVI(dip)->devi_nodeid); 432 433 if (ndi_dev_is_persistent_node(dip)) { 434 mutex_enter(&devimap->dno_lock); 435 ASSERT(devimap->dno_free); 436 elem = devimap->dno_free; 437 devimap->dno_free = elem->next; 438 mutex_exit(&devimap->dno_lock); 439 kmem_free(elem, sizeof (*elem)); 440 } 441 442 if (DEVI(dip)->devi_compat_names) 443 kmem_free(DEVI(dip)->devi_compat_names, 444 DEVI(dip)->devi_compat_length); 445 if (DEVI(dip)->devi_rebinding_name) 446 kmem_free(DEVI(dip)->devi_rebinding_name, 447 strlen(DEVI(dip)->devi_rebinding_name) + 1); 448 449 ddi_prop_remove_all(dip); /* remove driver properties */ 450 if (devi->devi_sys_prop_ptr) 451 i_ddi_prop_list_delete(devi->devi_sys_prop_ptr); 452 if (devi->devi_hw_prop_ptr) 453 i_ddi_prop_list_delete(devi->devi_hw_prop_ptr); 454 455 if (DEVI(dip)->devi_devid_str) 456 ddi_devid_str_free(DEVI(dip)->devi_devid_str); 457 458 i_ddi_set_node_state(dip, DS_INVAL); 459 da_log_enter(dip); 460 if (devi->devi_audit) { 461 kmem_free(devi->devi_audit, sizeof (devinfo_audit_t)); 462 } 463 if (devi->devi_device_class) 464 kmem_free(devi->devi_device_class, 465 strlen(devi->devi_device_class) + 1); 466 cv_destroy(&(devi->devi_cv)); 467 mutex_destroy(&(devi->devi_lock)); 468 mutex_destroy(&(devi->devi_pm_lock)); 469 mutex_destroy(&(devi->devi_pm_busy_lock)); 470 471 RIO_TRACE((CE_NOTE, "i_ddi_free_node: destroying contract fields: " 472 "dip=%p", (void *)dip)); 473 contract_device_remove_dip(dip); 474 ASSERT(devi->devi_ct_count == -1); 475 ASSERT(list_is_empty(&(devi->devi_ct))); 476 cv_destroy(&(devi->devi_ct_cv)); 477 list_destroy(&(devi->devi_ct)); 478 /* free this last since contract_device_remove_dip() uses it */ 479 mutex_destroy(&(devi->devi_ct_lock)); 480 RIO_TRACE((CE_NOTE, "i_ddi_free_node: destroyed all contract fields: " 481 "dip=%p, name=%s", (void *)dip, devi->devi_node_name)); 482 483 kmem_free(devi->devi_node_name, strlen(devi->devi_node_name) + 1); 484 485 /* free event data */ 486 if (devi->devi_ev_path) 487 kmem_free(devi->devi_ev_path, MAXPATHLEN); 488 489 kmem_cache_free(ddi_node_cache, devi); 490 } 491 492 493 /* 494 * Node state transitions 495 */ 496 497 /* 498 * Change the node name 499 */ 500 int 501 ndi_devi_set_nodename(dev_info_t *dip, char *name, int flags) 502 { 503 _NOTE(ARGUNUSED(flags)) 504 char *nname, *oname; 505 506 ASSERT(dip && name); 507 508 oname = DEVI(dip)->devi_node_name; 509 if (strcmp(oname, name) == 0) 510 return (DDI_SUCCESS); 511 512 /* 513 * pcicfg_fix_ethernet requires a name change after node 514 * is linked into the tree. When pcicfg is fixed, we 515 * should only allow name change in DS_PROTO state. 516 */ 517 if (i_ddi_node_state(dip) >= DS_BOUND) { 518 /* 519 * Don't allow name change once node is bound 520 */ 521 cmn_err(CE_NOTE, 522 "ndi_devi_set_nodename: node already bound dip = %p," 523 " %s -> %s", (void *)dip, ddi_node_name(dip), name); 524 return (NDI_FAILURE); 525 } 526 527 nname = i_ddi_strdup(name, KM_SLEEP); 528 DEVI(dip)->devi_node_name = nname; 529 i_ddi_set_binding_name(dip, nname); 530 kmem_free(oname, strlen(oname) + 1); 531 532 da_log_enter(dip); 533 return (NDI_SUCCESS); 534 } 535 536 void 537 i_ddi_add_devimap(dev_info_t *dip) 538 { 539 struct devi_nodeid *elem; 540 541 ASSERT(dip); 542 543 if (!ndi_dev_is_persistent_node(dip)) 544 return; 545 546 ASSERT(ddi_get_parent(dip) == NULL || (DEVI_VHCI_NODE(dip)) || 547 DEVI_BUSY_OWNED(ddi_get_parent(dip))); 548 549 mutex_enter(&devimap->dno_lock); 550 551 ASSERT(devimap->dno_free); 552 553 elem = devimap->dno_free; 554 devimap->dno_free = elem->next; 555 556 elem->nodeid = ddi_get_nodeid(dip); 557 elem->dip = dip; 558 elem->next = devimap->dno_head; 559 devimap->dno_head = elem; 560 561 devimap->dno_list_length++; 562 563 mutex_exit(&devimap->dno_lock); 564 } 565 566 static int 567 i_ddi_remove_devimap(dev_info_t *dip) 568 { 569 struct devi_nodeid *prev, *elem; 570 static const char *fcn = "i_ddi_remove_devimap"; 571 572 ASSERT(dip); 573 574 if (!ndi_dev_is_persistent_node(dip)) 575 return (DDI_SUCCESS); 576 577 mutex_enter(&devimap->dno_lock); 578 579 /* 580 * The following check is done with dno_lock held 581 * to prevent race between dip removal and 582 * e_ddi_prom_node_to_dip() 583 */ 584 if (e_ddi_devi_holdcnt(dip)) { 585 mutex_exit(&devimap->dno_lock); 586 return (DDI_FAILURE); 587 } 588 589 ASSERT(devimap->dno_head); 590 ASSERT(devimap->dno_list_length > 0); 591 592 prev = NULL; 593 for (elem = devimap->dno_head; elem; elem = elem->next) { 594 if (elem->dip == dip) { 595 ASSERT(elem->nodeid == ddi_get_nodeid(dip)); 596 break; 597 } 598 prev = elem; 599 } 600 601 if (elem && prev) 602 prev->next = elem->next; 603 else if (elem) 604 devimap->dno_head = elem->next; 605 else 606 panic("%s: devinfo node(%p) not found", 607 fcn, (void *)dip); 608 609 devimap->dno_list_length--; 610 611 elem->nodeid = 0; 612 elem->dip = NULL; 613 614 elem->next = devimap->dno_free; 615 devimap->dno_free = elem; 616 617 mutex_exit(&devimap->dno_lock); 618 619 return (DDI_SUCCESS); 620 } 621 622 /* 623 * Link this node into the devinfo tree and add to orphan list 624 * Not callable from interrupt context 625 */ 626 static void 627 link_node(dev_info_t *dip) 628 { 629 struct dev_info *devi = DEVI(dip); 630 struct dev_info *parent = devi->devi_parent; 631 dev_info_t **dipp; 632 633 ASSERT(parent); /* never called for root node */ 634 635 NDI_CONFIG_DEBUG((CE_CONT, "link_node: parent = %s child = %s\n", 636 parent->devi_node_name, devi->devi_node_name)); 637 638 /* 639 * Hold the global_vhci_lock before linking any direct 640 * children of rootnex driver. This special lock protects 641 * linking and unlinking for rootnext direct children. 642 */ 643 if ((dev_info_t *)parent == ddi_root_node()) 644 mutex_enter(&global_vhci_lock); 645 646 /* 647 * attach the node to end of the list unless the node is already there 648 */ 649 dipp = (dev_info_t **)(&DEVI(parent)->devi_child); 650 while (*dipp && (*dipp != dip)) { 651 dipp = (dev_info_t **)(&DEVI(*dipp)->devi_sibling); 652 } 653 ASSERT(*dipp == NULL); /* node is not linked */ 654 655 /* 656 * Now that we are in the tree, update the devi-nodeid map. 657 */ 658 i_ddi_add_devimap(dip); 659 660 /* 661 * This is a temporary workaround for Bug 4618861. 662 * We keep the scsi_vhci nexus node on the left side of the devinfo 663 * tree (under the root nexus driver), so that virtual nodes under 664 * scsi_vhci will be SUSPENDed first and RESUMEd last. This ensures 665 * that the pHCI nodes are active during times when their clients 666 * may be depending on them. This workaround embodies the knowledge 667 * that system PM and CPR both traverse the tree left-to-right during 668 * SUSPEND and right-to-left during RESUME. 669 * Extending the workaround to IB Nexus/VHCI 670 * driver also. 671 */ 672 if (strcmp(devi->devi_binding_name, "scsi_vhci") == 0) { 673 /* Add scsi_vhci to beginning of list */ 674 ASSERT((dev_info_t *)parent == top_devinfo); 675 /* scsi_vhci under rootnex */ 676 devi->devi_sibling = parent->devi_child; 677 parent->devi_child = devi; 678 } else if (strcmp(devi->devi_binding_name, "ib") == 0) { 679 i_link_vhci_node(dip); 680 } else { 681 /* Add to end of list */ 682 *dipp = dip; 683 DEVI(dip)->devi_sibling = NULL; 684 } 685 686 /* 687 * Release the global_vhci_lock before linking any direct 688 * children of rootnex driver. 689 */ 690 if ((dev_info_t *)parent == ddi_root_node()) 691 mutex_exit(&global_vhci_lock); 692 693 /* persistent nodes go on orphan list */ 694 if (ndi_dev_is_persistent_node(dip)) 695 add_to_dn_list(&orphanlist, dip); 696 } 697 698 /* 699 * Unlink this node from the devinfo tree 700 */ 701 static int 702 unlink_node(dev_info_t *dip) 703 { 704 struct dev_info *devi = DEVI(dip); 705 struct dev_info *parent = devi->devi_parent; 706 dev_info_t **dipp; 707 ddi_hp_cn_handle_t *hdlp; 708 709 ASSERT(parent != NULL); 710 ASSERT(devi->devi_node_state == DS_LINKED); 711 712 NDI_CONFIG_DEBUG((CE_CONT, "unlink_node: name = %s\n", 713 ddi_node_name(dip))); 714 715 /* check references */ 716 if (devi->devi_ref || i_ddi_remove_devimap(dip) != DDI_SUCCESS) 717 return (DDI_FAILURE); 718 719 /* 720 * Hold the global_vhci_lock before linking any direct 721 * children of rootnex driver. 722 */ 723 if ((dev_info_t *)parent == ddi_root_node()) 724 mutex_enter(&global_vhci_lock); 725 726 dipp = (dev_info_t **)(&DEVI(parent)->devi_child); 727 while (*dipp && (*dipp != dip)) { 728 dipp = (dev_info_t **)(&DEVI(*dipp)->devi_sibling); 729 } 730 if (*dipp) { 731 *dipp = (dev_info_t *)(devi->devi_sibling); 732 devi->devi_sibling = NULL; 733 } else { 734 NDI_CONFIG_DEBUG((CE_NOTE, "unlink_node: %s not linked", 735 devi->devi_node_name)); 736 } 737 738 /* 739 * Release the global_vhci_lock before linking any direct 740 * children of rootnex driver. 741 */ 742 if ((dev_info_t *)parent == ddi_root_node()) 743 mutex_exit(&global_vhci_lock); 744 745 /* Remove node from orphan list */ 746 if (ndi_dev_is_persistent_node(dip)) { 747 remove_from_dn_list(&orphanlist, dip); 748 } 749 750 /* Update parent's hotplug handle list */ 751 for (hdlp = DEVI(parent)->devi_hp_hdlp; hdlp; hdlp = hdlp->next) { 752 if (hdlp->cn_info.cn_child == dip) 753 hdlp->cn_info.cn_child = NULL; 754 } 755 return (DDI_SUCCESS); 756 } 757 758 /* 759 * Bind this devinfo node to a driver. If compat is NON-NULL, try that first. 760 * Else, use the node-name. 761 * 762 * NOTE: IEEE1275 specifies that nodename should be tried before compatible. 763 * Solaris implementation binds nodename after compatible. 764 * 765 * If we find a binding, 766 * - set the binding name to the string, 767 * - set major number to driver major 768 * 769 * If we don't find a binding, 770 * - return failure 771 */ 772 static int 773 bind_node(dev_info_t *dip) 774 { 775 char *p = NULL; 776 major_t major = DDI_MAJOR_T_NONE; 777 struct dev_info *devi = DEVI(dip); 778 dev_info_t *parent = ddi_get_parent(dip); 779 780 ASSERT(devi->devi_node_state == DS_LINKED); 781 782 NDI_CONFIG_DEBUG((CE_CONT, "bind_node: 0x%p(name = %s)\n", 783 (void *)dip, ddi_node_name(dip))); 784 785 mutex_enter(&DEVI(dip)->devi_lock); 786 if (DEVI(dip)->devi_flags & DEVI_NO_BIND) { 787 mutex_exit(&DEVI(dip)->devi_lock); 788 return (DDI_FAILURE); 789 } 790 mutex_exit(&DEVI(dip)->devi_lock); 791 792 /* find the driver with most specific binding using compatible */ 793 major = ddi_compatible_driver_major(dip, &p); 794 if (major == DDI_MAJOR_T_NONE) 795 return (DDI_FAILURE); 796 797 devi->devi_major = major; 798 if (p != NULL) { 799 i_ddi_set_binding_name(dip, p); 800 NDI_CONFIG_DEBUG((CE_CONT, "bind_node: %s bound to %s\n", 801 devi->devi_node_name, p)); 802 } 803 804 /* Link node to per-driver list */ 805 link_to_driver_list(dip); 806 807 /* 808 * reset parent flag so that nexus will merge .conf props 809 */ 810 if (ndi_dev_is_persistent_node(dip)) { 811 mutex_enter(&DEVI(parent)->devi_lock); 812 DEVI(parent)->devi_flags &= 813 ~(DEVI_ATTACHED_CHILDREN|DEVI_MADE_CHILDREN); 814 mutex_exit(&DEVI(parent)->devi_lock); 815 } 816 return (DDI_SUCCESS); 817 } 818 819 /* 820 * Unbind this devinfo node 821 * Called before the node is destroyed or driver is removed from system 822 */ 823 static int 824 unbind_node(dev_info_t *dip) 825 { 826 ASSERT(DEVI(dip)->devi_node_state == DS_BOUND); 827 ASSERT(DEVI(dip)->devi_major != DDI_MAJOR_T_NONE); 828 829 /* check references */ 830 if (DEVI(dip)->devi_ref) 831 return (DDI_FAILURE); 832 833 NDI_CONFIG_DEBUG((CE_CONT, "unbind_node: 0x%p(name = %s)\n", 834 (void *)dip, ddi_node_name(dip))); 835 836 unlink_from_driver_list(dip); 837 838 DEVI(dip)->devi_major = DDI_MAJOR_T_NONE; 839 DEVI(dip)->devi_binding_name = DEVI(dip)->devi_node_name; 840 return (DDI_SUCCESS); 841 } 842 843 /* 844 * Initialize a node: calls the parent nexus' bus_ctl ops to do the operation. 845 * Must hold parent and per-driver list while calling this function. 846 * A successful init_node() returns with an active ndi_hold_devi() hold on 847 * the parent. 848 */ 849 static int 850 init_node(dev_info_t *dip) 851 { 852 int error; 853 dev_info_t *pdip = ddi_get_parent(dip); 854 int (*f)(dev_info_t *, dev_info_t *, ddi_ctl_enum_t, void *, void *); 855 char *path; 856 major_t major; 857 ddi_devid_t devid = NULL; 858 859 ASSERT(i_ddi_node_state(dip) == DS_BOUND); 860 861 /* should be DS_READY except for pcmcia ... */ 862 ASSERT(i_ddi_node_state(pdip) >= DS_PROBED); 863 864 path = kmem_alloc(MAXPATHLEN, KM_SLEEP); 865 (void) ddi_pathname(dip, path); 866 NDI_CONFIG_DEBUG((CE_CONT, "init_node: entry: path %s 0x%p\n", 867 path, (void *)dip)); 868 869 /* 870 * The parent must have a bus_ctl operation. 871 */ 872 if ((DEVI(pdip)->devi_ops->devo_bus_ops == NULL) || 873 (f = DEVI(pdip)->devi_ops->devo_bus_ops->bus_ctl) == NULL) { 874 error = DDI_FAILURE; 875 goto out; 876 } 877 878 add_global_props(dip); 879 880 /* 881 * Invoke the parent's bus_ctl operation with the DDI_CTLOPS_INITCHILD 882 * command to transform the child to canonical form 1. If there 883 * is an error, ddi_remove_child should be called, to clean up. 884 */ 885 error = (*f)(pdip, pdip, DDI_CTLOPS_INITCHILD, dip, NULL); 886 if (error != DDI_SUCCESS) { 887 NDI_CONFIG_DEBUG((CE_CONT, "init_node: %s 0x%p failed\n", 888 path, (void *)dip)); 889 remove_global_props(dip); 890 891 /* 892 * If a nexus INITCHILD implementation calls ddi_devid_regster() 893 * prior to setting devi_addr, the devid is not recorded in 894 * the devid cache (i.e. DEVI_CACHED_DEVID is not set). 895 * With mpxio, while the vhci client path may be missing 896 * from the cache, phci pathinfo paths may have already be 897 * added to the cache, against the client dip, by use of 898 * e_devid_cache_pathinfo(). Because of this, when INITCHILD 899 * of the client fails, we need to purge the client dip from 900 * the cache even if DEVI_CACHED_DEVID is not set - if only 901 * devi_devid_str is set. 902 */ 903 mutex_enter(&DEVI(dip)->devi_lock); 904 if ((DEVI(dip)->devi_flags & DEVI_CACHED_DEVID) || 905 DEVI(dip)->devi_devid_str) { 906 DEVI(dip)->devi_flags &= ~DEVI_CACHED_DEVID; 907 mutex_exit(&DEVI(dip)->devi_lock); 908 ddi_devid_unregister(dip); 909 } else 910 mutex_exit(&DEVI(dip)->devi_lock); 911 912 /* in case nexus driver didn't clear this field */ 913 ddi_set_name_addr(dip, NULL); 914 error = DDI_FAILURE; 915 goto out; 916 } 917 918 ndi_hold_devi(pdip); /* initial hold of parent */ 919 920 /* recompute path after initchild for @addr information */ 921 (void) ddi_pathname(dip, path); 922 923 /* Check for duplicate nodes */ 924 if (find_duplicate_child(pdip, dip) != NULL) { 925 /* 926 * uninit_node() the duplicate - a successful uninit_node() 927 * will release inital hold of parent using ndi_rele_devi(). 928 */ 929 if ((error = uninit_node(dip)) != DDI_SUCCESS) { 930 ndi_rele_devi(pdip); /* release initial hold */ 931 cmn_err(CE_WARN, "init_node: uninit of duplicate " 932 "node %s failed", path); 933 } 934 NDI_CONFIG_DEBUG((CE_CONT, "init_node: duplicate uninit " 935 "%s 0x%p%s\n", path, (void *)dip, 936 (error == DDI_SUCCESS) ? "" : " failed")); 937 error = DDI_FAILURE; 938 goto out; 939 } 940 941 /* 942 * If a devid was registered for a DS_BOUND node then the devid_cache 943 * may not have captured the path. Detect this situation and ensure that 944 * the path enters the cache now that devi_addr is established. 945 */ 946 if (!(DEVI(dip)->devi_flags & DEVI_CACHED_DEVID) && 947 (ddi_devid_get(dip, &devid) == DDI_SUCCESS)) { 948 if (e_devid_cache_register(dip, devid) == DDI_SUCCESS) { 949 mutex_enter(&DEVI(dip)->devi_lock); 950 DEVI(dip)->devi_flags |= DEVI_CACHED_DEVID; 951 mutex_exit(&DEVI(dip)->devi_lock); 952 } 953 954 ddi_devid_free(devid); 955 } 956 957 /* 958 * Check to see if we have a path-oriented driver alias that overrides 959 * the current driver binding. If so, we need to rebind. This check 960 * needs to be delayed until after a successful DDI_CTLOPS_INITCHILD, 961 * so the unit-address is established on the last component of the path. 962 * 963 * NOTE: Allowing a path-oriented alias to change the driver binding 964 * of a driver.conf node results in non-intuitive property behavior. 965 * We provide a tunable (driver_conf_allow_path_alias) to control 966 * this behavior. See uninit_node() for more details. 967 * 968 * NOTE: If you are adding a path-oriented alias for the boot device, 969 * and there is mismatch between OBP and the kernel in regard to 970 * generic name use, like "disk" .vs. "ssd", then you will need 971 * to add a path-oriented alias for both paths. 972 */ 973 major = ddi_name_to_major(path); 974 if (driver_active(major) && (major != DEVI(dip)->devi_major) && 975 (ndi_dev_is_persistent_node(dip) || driver_conf_allow_path_alias)) { 976 977 /* Mark node for rebind processing. */ 978 mutex_enter(&DEVI(dip)->devi_lock); 979 DEVI(dip)->devi_flags |= DEVI_REBIND; 980 mutex_exit(&DEVI(dip)->devi_lock); 981 982 /* 983 * Add an extra hold on the parent to prevent it from ever 984 * having a zero devi_ref during the child rebind process. 985 * This is necessary to ensure that the parent will never 986 * detach(9E) during the rebind. 987 */ 988 ndi_hold_devi(pdip); /* extra hold of parent */ 989 990 /* 991 * uninit_node() current binding - a successful uninit_node() 992 * will release extra hold of parent using ndi_rele_devi(). 993 */ 994 if ((error = uninit_node(dip)) != DDI_SUCCESS) { 995 ndi_rele_devi(pdip); /* release extra hold */ 996 ndi_rele_devi(pdip); /* release initial hold */ 997 cmn_err(CE_WARN, "init_node: uninit for rebind " 998 "of node %s failed", path); 999 goto out; 1000 } 1001 1002 /* Unbind: demote the node back to DS_LINKED. */ 1003 if ((error = ndi_devi_unbind_driver(dip)) != DDI_SUCCESS) { 1004 ndi_rele_devi(pdip); /* release initial hold */ 1005 cmn_err(CE_WARN, "init_node: unbind for rebind " 1006 "of node %s failed", path); 1007 goto out; 1008 } 1009 1010 /* establish rebinding name */ 1011 if (DEVI(dip)->devi_rebinding_name == NULL) 1012 DEVI(dip)->devi_rebinding_name = 1013 i_ddi_strdup(path, KM_SLEEP); 1014 1015 /* 1016 * Now that we are demoted and marked for rebind, repromote. 1017 * We need to do this in steps, instead of just calling 1018 * ddi_initchild, so that we can redo the merge operation 1019 * after we are rebound to the path-bound driver. 1020 * 1021 * Start by rebinding node to the path-bound driver. 1022 */ 1023 if ((error = ndi_devi_bind_driver(dip, 0)) != DDI_SUCCESS) { 1024 ndi_rele_devi(pdip); /* release initial hold */ 1025 cmn_err(CE_WARN, "init_node: rebind " 1026 "of node %s failed", path); 1027 goto out; 1028 } 1029 1030 /* 1031 * If the node is not a driver.conf node then merge 1032 * driver.conf properties from new path-bound driver.conf. 1033 */ 1034 if (ndi_dev_is_persistent_node(dip)) 1035 (void) i_ndi_make_spec_children(pdip, 0); 1036 1037 /* 1038 * Now that we have taken care of merge, repromote back 1039 * to DS_INITIALIZED. 1040 */ 1041 error = ddi_initchild(pdip, dip); 1042 NDI_CONFIG_DEBUG((CE_CONT, "init_node: rebind " 1043 "%s 0x%p\n", path, (void *)dip)); 1044 1045 /* 1046 * Release our initial hold. If ddi_initchild() was 1047 * successful then it will return with the active hold. 1048 */ 1049 ndi_rele_devi(pdip); 1050 goto out; 1051 } 1052 1053 /* 1054 * Apply multi-parent/deep-nexus optimization to the new node 1055 */ 1056 DEVI(dip)->devi_instance = e_ddi_assign_instance(dip); 1057 ddi_optimize_dtree(dip); 1058 error = DDI_SUCCESS; /* return with active hold */ 1059 1060 out: if (error != DDI_SUCCESS) { 1061 /* On failure ensure that DEVI_REBIND is cleared */ 1062 mutex_enter(&DEVI(dip)->devi_lock); 1063 DEVI(dip)->devi_flags &= ~DEVI_REBIND; 1064 mutex_exit(&DEVI(dip)->devi_lock); 1065 } 1066 kmem_free(path, MAXPATHLEN); 1067 return (error); 1068 } 1069 1070 /* 1071 * Uninitialize node 1072 * The per-driver list must be held busy during the call. 1073 * A successful uninit_node() releases the init_node() hold on 1074 * the parent by calling ndi_rele_devi(). 1075 */ 1076 static int 1077 uninit_node(dev_info_t *dip) 1078 { 1079 int node_state_entry; 1080 dev_info_t *pdip; 1081 struct dev_ops *ops; 1082 int (*f)(); 1083 int error; 1084 char *addr; 1085 1086 /* 1087 * Don't check for references here or else a ref-counted 1088 * dip cannot be downgraded by the framework. 1089 */ 1090 node_state_entry = i_ddi_node_state(dip); 1091 ASSERT((node_state_entry == DS_BOUND) || 1092 (node_state_entry == DS_INITIALIZED)); 1093 pdip = ddi_get_parent(dip); 1094 ASSERT(pdip); 1095 1096 NDI_CONFIG_DEBUG((CE_CONT, "uninit_node: 0x%p(%s%d)\n", 1097 (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip))); 1098 1099 if (((ops = ddi_get_driver(pdip)) == NULL) || 1100 (ops->devo_bus_ops == NULL) || 1101 ((f = ops->devo_bus_ops->bus_ctl) == NULL)) { 1102 return (DDI_FAILURE); 1103 } 1104 1105 /* 1106 * save the @addr prior to DDI_CTLOPS_UNINITCHILD for use in 1107 * freeing the instance if it succeeds. 1108 */ 1109 if (node_state_entry == DS_INITIALIZED) { 1110 addr = ddi_get_name_addr(dip); 1111 if (addr) 1112 addr = i_ddi_strdup(addr, KM_SLEEP); 1113 } else { 1114 addr = NULL; 1115 } 1116 1117 error = (*f)(pdip, pdip, DDI_CTLOPS_UNINITCHILD, dip, (void *)NULL); 1118 if (error == DDI_SUCCESS) { 1119 /* ensure that devids are unregistered */ 1120 mutex_enter(&DEVI(dip)->devi_lock); 1121 if ((DEVI(dip)->devi_flags & DEVI_CACHED_DEVID)) { 1122 DEVI(dip)->devi_flags &= ~DEVI_CACHED_DEVID; 1123 mutex_exit(&DEVI(dip)->devi_lock); 1124 ddi_devid_unregister(dip); 1125 } else 1126 mutex_exit(&DEVI(dip)->devi_lock); 1127 1128 /* if uninitchild forgot to set devi_addr to NULL do it now */ 1129 ddi_set_name_addr(dip, NULL); 1130 1131 /* 1132 * Free instance number. This is a no-op if instance has 1133 * been kept by probe_node(). Avoid free when we are called 1134 * from init_node (DS_BOUND) because the instance has not yet 1135 * been assigned. 1136 */ 1137 if (node_state_entry == DS_INITIALIZED) { 1138 e_ddi_free_instance(dip, addr); 1139 DEVI(dip)->devi_instance = -1; 1140 } 1141 1142 /* release the init_node hold */ 1143 ndi_rele_devi(pdip); 1144 1145 remove_global_props(dip); 1146 1147 /* 1148 * NOTE: The decision on whether to allow a path-oriented 1149 * rebind of a driver.conf enumerated node is made by 1150 * init_node() based on driver_conf_allow_path_alias. The 1151 * rebind code below prevents deletion of system properties 1152 * on driver.conf nodes. 1153 * 1154 * When driver_conf_allow_path_alias is set, property behavior 1155 * on rebound driver.conf file is non-intuitive. For a 1156 * driver.conf node, the unit-address properties come from 1157 * the driver.conf file as system properties. Removing system 1158 * properties from a driver.conf node makes the node 1159 * useless (we get node without unit-address properties) - so 1160 * we leave system properties in place. The result is a node 1161 * where system properties come from the node being rebound, 1162 * and global properties come from the driver.conf file 1163 * of the driver we are rebinding to. If we could determine 1164 * that the path-oriented alias driver.conf file defined a 1165 * node at the same unit address, it would be best to use 1166 * that node and avoid the non-intuitive property behavior. 1167 * Unfortunately, the current "merge" code does not support 1168 * this, so we live with the non-intuitive property behavior. 1169 */ 1170 if (!((ndi_dev_is_persistent_node(dip) == 0) && 1171 (DEVI(dip)->devi_flags & DEVI_REBIND))) 1172 e_ddi_prop_remove_all(dip); 1173 } else { 1174 NDI_CONFIG_DEBUG((CE_CONT, "uninit_node failed: 0x%p(%s%d)\n", 1175 (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip))); 1176 } 1177 1178 if (addr) 1179 kmem_free(addr, strlen(addr) + 1); 1180 return (error); 1181 } 1182 1183 /* 1184 * Invoke driver's probe entry point to probe for existence of hardware. 1185 * Keep instance permanent for successful probe and leaf nodes. 1186 * 1187 * Per-driver list must be held busy while calling this function. 1188 */ 1189 static int 1190 probe_node(dev_info_t *dip) 1191 { 1192 int rv; 1193 1194 ASSERT(i_ddi_node_state(dip) == DS_INITIALIZED); 1195 1196 NDI_CONFIG_DEBUG((CE_CONT, "probe_node: 0x%p(%s%d)\n", 1197 (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip))); 1198 1199 /* temporarily hold the driver while we probe */ 1200 DEVI(dip)->devi_ops = ndi_hold_driver(dip); 1201 if (DEVI(dip)->devi_ops == NULL) { 1202 NDI_CONFIG_DEBUG((CE_CONT, 1203 "probe_node: 0x%p(%s%d) cannot load driver\n", 1204 (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip))); 1205 return (DDI_FAILURE); 1206 } 1207 1208 if (identify_9e != 0) 1209 (void) devi_identify(dip); 1210 1211 rv = devi_probe(dip); 1212 1213 /* release the driver now that probe is complete */ 1214 ndi_rele_driver(dip); 1215 DEVI(dip)->devi_ops = NULL; 1216 1217 switch (rv) { 1218 case DDI_PROBE_SUCCESS: /* found */ 1219 case DDI_PROBE_DONTCARE: /* ddi_dev_is_sid */ 1220 e_ddi_keep_instance(dip); /* persist instance */ 1221 rv = DDI_SUCCESS; 1222 break; 1223 1224 case DDI_PROBE_PARTIAL: /* maybe later */ 1225 case DDI_PROBE_FAILURE: /* not found */ 1226 NDI_CONFIG_DEBUG((CE_CONT, 1227 "probe_node: 0x%p(%s%d) no hardware found%s\n", 1228 (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip), 1229 (rv == DDI_PROBE_PARTIAL) ? " yet" : "")); 1230 rv = DDI_FAILURE; 1231 break; 1232 1233 default: 1234 #ifdef DEBUG 1235 cmn_err(CE_WARN, "probe_node: %s%d: illegal probe(9E) value", 1236 ddi_driver_name(dip), ddi_get_instance(dip)); 1237 #endif /* DEBUG */ 1238 rv = DDI_FAILURE; 1239 break; 1240 } 1241 return (rv); 1242 } 1243 1244 /* 1245 * Unprobe a node. Simply reset the node state. 1246 * Per-driver list must be held busy while calling this function. 1247 */ 1248 static int 1249 unprobe_node(dev_info_t *dip) 1250 { 1251 ASSERT(i_ddi_node_state(dip) == DS_PROBED); 1252 1253 /* 1254 * Don't check for references here or else a ref-counted 1255 * dip cannot be downgraded by the framework. 1256 */ 1257 1258 NDI_CONFIG_DEBUG((CE_CONT, "unprobe_node: 0x%p(name = %s)\n", 1259 (void *)dip, ddi_node_name(dip))); 1260 return (DDI_SUCCESS); 1261 } 1262 1263 /* 1264 * Attach devinfo node. 1265 * Per-driver list must be held busy. 1266 */ 1267 static int 1268 attach_node(dev_info_t *dip) 1269 { 1270 int rv; 1271 1272 ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip))); 1273 ASSERT(i_ddi_node_state(dip) == DS_PROBED); 1274 1275 NDI_CONFIG_DEBUG((CE_CONT, "attach_node: 0x%p(%s%d)\n", 1276 (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip))); 1277 1278 /* 1279 * Tell mpxio framework that a node is about to online. 1280 */ 1281 if ((rv = mdi_devi_online(dip, 0)) != NDI_SUCCESS) { 1282 return (DDI_FAILURE); 1283 } 1284 1285 /* no recursive attachment */ 1286 ASSERT(DEVI(dip)->devi_ops == NULL); 1287 1288 /* 1289 * Hold driver the node is bound to. 1290 */ 1291 DEVI(dip)->devi_ops = ndi_hold_driver(dip); 1292 if (DEVI(dip)->devi_ops == NULL) { 1293 /* 1294 * We were able to load driver for probing, so we should 1295 * not get here unless something really bad happened. 1296 */ 1297 cmn_err(CE_WARN, "attach_node: no driver for major %d", 1298 DEVI(dip)->devi_major); 1299 return (DDI_FAILURE); 1300 } 1301 1302 if (NEXUS_DRV(DEVI(dip)->devi_ops)) 1303 DEVI(dip)->devi_taskq = ddi_taskq_create(dip, 1304 "nexus_enum_tq", 1, 1305 TASKQ_DEFAULTPRI, 0); 1306 1307 mutex_enter(&(DEVI(dip)->devi_lock)); 1308 DEVI_SET_ATTACHING(dip); 1309 DEVI_SET_NEED_RESET(dip); 1310 mutex_exit(&(DEVI(dip)->devi_lock)); 1311 1312 rv = devi_attach(dip, DDI_ATTACH); 1313 1314 mutex_enter(&(DEVI(dip)->devi_lock)); 1315 DEVI_CLR_ATTACHING(dip); 1316 1317 if (rv != DDI_SUCCESS) { 1318 DEVI_CLR_NEED_RESET(dip); 1319 mutex_exit(&DEVI(dip)->devi_lock); 1320 1321 /* 1322 * Cleanup dacf reservations 1323 */ 1324 mutex_enter(&dacf_lock); 1325 dacf_clr_rsrvs(dip, DACF_OPID_POSTATTACH); 1326 dacf_clr_rsrvs(dip, DACF_OPID_PREDETACH); 1327 mutex_exit(&dacf_lock); 1328 if (DEVI(dip)->devi_taskq) 1329 ddi_taskq_destroy(DEVI(dip)->devi_taskq); 1330 ddi_remove_minor_node(dip, NULL); 1331 1332 /* release the driver if attach failed */ 1333 ndi_rele_driver(dip); 1334 DEVI(dip)->devi_ops = NULL; 1335 NDI_CONFIG_DEBUG((CE_CONT, "attach_node: 0x%p(%s%d) failed\n", 1336 (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip))); 1337 return (DDI_FAILURE); 1338 } else 1339 mutex_exit(&DEVI(dip)->devi_lock); 1340 1341 /* successful attach, return with driver held */ 1342 1343 return (DDI_SUCCESS); 1344 } 1345 1346 /* 1347 * Detach devinfo node. 1348 * Per-driver list must be held busy. 1349 */ 1350 static int 1351 detach_node(dev_info_t *dip, uint_t flag) 1352 { 1353 struct devnames *dnp; 1354 int rv; 1355 1356 ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip))); 1357 ASSERT(i_ddi_node_state(dip) == DS_ATTACHED); 1358 1359 /* check references */ 1360 if (DEVI(dip)->devi_ref) 1361 return (DDI_FAILURE); 1362 1363 NDI_CONFIG_DEBUG((CE_CONT, "detach_node: 0x%p(%s%d)\n", 1364 (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip))); 1365 1366 /* 1367 * NOTE: If we are processing a pHCI node then the calling code 1368 * must detect this and ndi_devi_enter() in (vHCI, parent(pHCI)) 1369 * order unless pHCI and vHCI are siblings. Code paths leading 1370 * here that must ensure this ordering include: 1371 * unconfig_immediate_children(), devi_unconfig_one(), 1372 * ndi_devi_unconfig_one(), ndi_devi_offline(). 1373 */ 1374 ASSERT(!MDI_PHCI(dip) || 1375 (ddi_get_parent(mdi_devi_get_vdip(dip)) == ddi_get_parent(dip)) || 1376 DEVI_BUSY_OWNED(mdi_devi_get_vdip(dip))); 1377 1378 /* Offline the device node with the mpxio framework. */ 1379 if (mdi_devi_offline(dip, flag) != NDI_SUCCESS) { 1380 return (DDI_FAILURE); 1381 } 1382 1383 /* drain the taskq */ 1384 if (DEVI(dip)->devi_taskq) 1385 ddi_taskq_wait(DEVI(dip)->devi_taskq); 1386 1387 rv = devi_detach(dip, DDI_DETACH); 1388 1389 if (rv != DDI_SUCCESS) { 1390 NDI_CONFIG_DEBUG((CE_CONT, 1391 "detach_node: 0x%p(%s%d) failed\n", 1392 (void *)dip, ddi_driver_name(dip), ddi_get_instance(dip))); 1393 return (DDI_FAILURE); 1394 } 1395 1396 mutex_enter(&(DEVI(dip)->devi_lock)); 1397 DEVI_CLR_NEED_RESET(dip); 1398 mutex_exit(&(DEVI(dip)->devi_lock)); 1399 1400 #if defined(__amd64) && !defined(__xpv) 1401 /* 1402 * Close any iommulib mediated linkage to an IOMMU 1403 */ 1404 if (IOMMU_USED(dip)) 1405 iommulib_nex_close(dip); 1406 #endif 1407 1408 /* destroy the taskq */ 1409 if (DEVI(dip)->devi_taskq) { 1410 ddi_taskq_destroy(DEVI(dip)->devi_taskq); 1411 DEVI(dip)->devi_taskq = NULL; 1412 } 1413 1414 /* Cleanup dacf reservations */ 1415 mutex_enter(&dacf_lock); 1416 dacf_clr_rsrvs(dip, DACF_OPID_POSTATTACH); 1417 dacf_clr_rsrvs(dip, DACF_OPID_PREDETACH); 1418 mutex_exit(&dacf_lock); 1419 1420 /* remove any additional flavors that were added */ 1421 if (DEVI(dip)->devi_flavorv_n > 1 && DEVI(dip)->devi_flavorv != NULL) { 1422 kmem_free(DEVI(dip)->devi_flavorv, 1423 (DEVI(dip)->devi_flavorv_n - 1) * sizeof (void *)); 1424 DEVI(dip)->devi_flavorv = NULL; 1425 } 1426 1427 /* Remove properties and minor nodes in case driver forgots */ 1428 ddi_remove_minor_node(dip, NULL); 1429 ddi_prop_remove_all(dip); 1430 1431 /* a detached node can't have attached or .conf children */ 1432 mutex_enter(&DEVI(dip)->devi_lock); 1433 DEVI(dip)->devi_flags &= ~(DEVI_MADE_CHILDREN|DEVI_ATTACHED_CHILDREN); 1434 mutex_exit(&DEVI(dip)->devi_lock); 1435 1436 /* 1437 * If the instance has successfully detached in detach_driver() context, 1438 * clear DN_DRIVER_HELD for correct ddi_hold_installed_driver() 1439 * behavior. Consumers like qassociate() depend on this (via clnopen()). 1440 */ 1441 if (flag & NDI_DETACH_DRIVER) { 1442 dnp = &(devnamesp[DEVI(dip)->devi_major]); 1443 LOCK_DEV_OPS(&dnp->dn_lock); 1444 dnp->dn_flags &= ~DN_DRIVER_HELD; 1445 UNLOCK_DEV_OPS(&dnp->dn_lock); 1446 } 1447 1448 /* successful detach, release the driver */ 1449 ndi_rele_driver(dip); 1450 DEVI(dip)->devi_ops = NULL; 1451 return (DDI_SUCCESS); 1452 } 1453 1454 /* 1455 * Run dacf post_attach routines 1456 */ 1457 static int 1458 postattach_node(dev_info_t *dip) 1459 { 1460 int rval; 1461 1462 /* 1463 * For hotplug busses like USB, it's possible that devices 1464 * are removed but dip is still around. We don't want to 1465 * run dacf routines as part of detach failure recovery. 1466 * 1467 * Pretend success until we figure out how to prevent 1468 * access to such devinfo nodes. 1469 */ 1470 if (DEVI_IS_DEVICE_REMOVED(dip)) 1471 return (DDI_SUCCESS); 1472 1473 /* 1474 * if dacf_postattach failed, report it to the framework 1475 * so that it can be retried later at the open time. 1476 */ 1477 mutex_enter(&dacf_lock); 1478 rval = dacfc_postattach(dip); 1479 mutex_exit(&dacf_lock); 1480 1481 /* 1482 * Plumbing during postattach may fail because of the 1483 * underlying device is not ready. This will fail ndi_devi_config() 1484 * in dv_filldir() and a warning message is issued. The message 1485 * from here will explain what happened 1486 */ 1487 if (rval != DACF_SUCCESS) { 1488 cmn_err(CE_WARN, "Postattach failed for %s%d\n", 1489 ddi_driver_name(dip), ddi_get_instance(dip)); 1490 return (DDI_FAILURE); 1491 } 1492 1493 return (DDI_SUCCESS); 1494 } 1495 1496 /* 1497 * Run dacf pre-detach routines 1498 */ 1499 static int 1500 predetach_node(dev_info_t *dip, uint_t flag) 1501 { 1502 int ret; 1503 1504 /* 1505 * Don't auto-detach if DDI_FORCEATTACH or DDI_NO_AUTODETACH 1506 * properties are set. 1507 */ 1508 if (flag & NDI_AUTODETACH) { 1509 struct devnames *dnp; 1510 int pflag = DDI_PROP_NOTPROM | DDI_PROP_DONTPASS; 1511 1512 if ((ddi_prop_get_int(DDI_DEV_T_ANY, dip, 1513 pflag, DDI_FORCEATTACH, 0) == 1) || 1514 (ddi_prop_get_int(DDI_DEV_T_ANY, dip, 1515 pflag, DDI_NO_AUTODETACH, 0) == 1)) 1516 return (DDI_FAILURE); 1517 1518 /* check for driver global version of DDI_NO_AUTODETACH */ 1519 dnp = &devnamesp[DEVI(dip)->devi_major]; 1520 LOCK_DEV_OPS(&dnp->dn_lock); 1521 if (dnp->dn_flags & DN_NO_AUTODETACH) { 1522 UNLOCK_DEV_OPS(&dnp->dn_lock); 1523 return (DDI_FAILURE); 1524 } 1525 UNLOCK_DEV_OPS(&dnp->dn_lock); 1526 } 1527 1528 mutex_enter(&dacf_lock); 1529 ret = dacfc_predetach(dip); 1530 mutex_exit(&dacf_lock); 1531 1532 return (ret); 1533 } 1534 1535 /* 1536 * Wrapper for making multiple state transitions 1537 */ 1538 1539 /* 1540 * i_ndi_config_node: upgrade dev_info node into a specified state. 1541 * It is a bit tricky because the locking protocol changes before and 1542 * after a node is bound to a driver. All locks are held external to 1543 * this function. 1544 */ 1545 int 1546 i_ndi_config_node(dev_info_t *dip, ddi_node_state_t state, uint_t flag) 1547 { 1548 _NOTE(ARGUNUSED(flag)) 1549 int rv = DDI_SUCCESS; 1550 1551 ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip))); 1552 1553 while ((i_ddi_node_state(dip) < state) && (rv == DDI_SUCCESS)) { 1554 1555 /* don't allow any more changes to the device tree */ 1556 if (devinfo_freeze) { 1557 rv = DDI_FAILURE; 1558 break; 1559 } 1560 1561 switch (i_ddi_node_state(dip)) { 1562 case DS_PROTO: 1563 /* 1564 * only caller can reference this node, no external 1565 * locking needed. 1566 */ 1567 link_node(dip); 1568 translate_devid((dev_info_t *)dip); 1569 i_ddi_set_node_state(dip, DS_LINKED); 1570 break; 1571 case DS_LINKED: 1572 /* 1573 * Three code path may attempt to bind a node: 1574 * - boot code 1575 * - add_drv 1576 * - hotplug thread 1577 * Boot code is single threaded, add_drv synchronize 1578 * on a userland lock, and hotplug synchronize on 1579 * hotplug_lk. There could be a race between add_drv 1580 * and hotplug thread. We'll live with this until the 1581 * conversion to top-down loading. 1582 */ 1583 if ((rv = bind_node(dip)) == DDI_SUCCESS) 1584 i_ddi_set_node_state(dip, DS_BOUND); 1585 1586 break; 1587 case DS_BOUND: 1588 /* 1589 * The following transitions synchronizes on the 1590 * per-driver busy changing flag, since we already 1591 * have a driver. 1592 */ 1593 if ((rv = init_node(dip)) == DDI_SUCCESS) 1594 i_ddi_set_node_state(dip, DS_INITIALIZED); 1595 break; 1596 case DS_INITIALIZED: 1597 if ((rv = probe_node(dip)) == DDI_SUCCESS) 1598 i_ddi_set_node_state(dip, DS_PROBED); 1599 break; 1600 case DS_PROBED: 1601 /* 1602 * If node is retired and persistent, then prevent 1603 * attach. We can't do this for non-persistent nodes 1604 * as we would lose evidence that the node existed. 1605 */ 1606 if (i_ddi_check_retire(dip) == 1 && 1607 ndi_dev_is_persistent_node(dip) && 1608 retire_prevents_attach == 1) { 1609 rv = DDI_FAILURE; 1610 break; 1611 } 1612 atomic_add_long(&devinfo_attach_detach, 1); 1613 if ((rv = attach_node(dip)) == DDI_SUCCESS) 1614 i_ddi_set_node_state(dip, DS_ATTACHED); 1615 atomic_add_long(&devinfo_attach_detach, -1); 1616 break; 1617 case DS_ATTACHED: 1618 if ((rv = postattach_node(dip)) == DDI_SUCCESS) 1619 i_ddi_set_node_state(dip, DS_READY); 1620 break; 1621 case DS_READY: 1622 break; 1623 default: 1624 /* should never reach here */ 1625 ASSERT("unknown devinfo state"); 1626 } 1627 } 1628 1629 if (ddidebug & DDI_AUDIT) 1630 da_log_enter(dip); 1631 return (rv); 1632 } 1633 1634 /* 1635 * i_ndi_unconfig_node: downgrade dev_info node into a specified state. 1636 */ 1637 int 1638 i_ndi_unconfig_node(dev_info_t *dip, ddi_node_state_t state, uint_t flag) 1639 { 1640 int rv = DDI_SUCCESS; 1641 1642 ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip))); 1643 1644 while ((i_ddi_node_state(dip) > state) && (rv == DDI_SUCCESS)) { 1645 1646 /* don't allow any more changes to the device tree */ 1647 if (devinfo_freeze) { 1648 rv = DDI_FAILURE; 1649 break; 1650 } 1651 1652 switch (i_ddi_node_state(dip)) { 1653 case DS_PROTO: 1654 break; 1655 case DS_LINKED: 1656 /* 1657 * Persistent nodes are only removed by hotplug code 1658 * .conf nodes synchronizes on per-driver list. 1659 */ 1660 if ((rv = unlink_node(dip)) == DDI_SUCCESS) 1661 i_ddi_set_node_state(dip, DS_PROTO); 1662 break; 1663 case DS_BOUND: 1664 /* 1665 * The following transitions synchronizes on the 1666 * per-driver busy changing flag, since we already 1667 * have a driver. 1668 */ 1669 if ((rv = unbind_node(dip)) == DDI_SUCCESS) 1670 i_ddi_set_node_state(dip, DS_LINKED); 1671 break; 1672 case DS_INITIALIZED: 1673 if ((rv = uninit_node(dip)) == DDI_SUCCESS) 1674 i_ddi_set_node_state(dip, DS_BOUND); 1675 break; 1676 case DS_PROBED: 1677 if ((rv = unprobe_node(dip)) == DDI_SUCCESS) 1678 i_ddi_set_node_state(dip, DS_INITIALIZED); 1679 break; 1680 case DS_ATTACHED: 1681 atomic_add_long(&devinfo_attach_detach, 1); 1682 1683 mutex_enter(&(DEVI(dip)->devi_lock)); 1684 DEVI_SET_DETACHING(dip); 1685 mutex_exit(&(DEVI(dip)->devi_lock)); 1686 1687 membar_enter(); /* ensure visibility for hold_devi */ 1688 1689 if ((rv = detach_node(dip, flag)) == DDI_SUCCESS) 1690 i_ddi_set_node_state(dip, DS_PROBED); 1691 1692 mutex_enter(&(DEVI(dip)->devi_lock)); 1693 DEVI_CLR_DETACHING(dip); 1694 mutex_exit(&(DEVI(dip)->devi_lock)); 1695 1696 atomic_add_long(&devinfo_attach_detach, -1); 1697 break; 1698 case DS_READY: 1699 if ((rv = predetach_node(dip, flag)) == DDI_SUCCESS) 1700 i_ddi_set_node_state(dip, DS_ATTACHED); 1701 break; 1702 default: 1703 ASSERT("unknown devinfo state"); 1704 } 1705 } 1706 da_log_enter(dip); 1707 return (rv); 1708 } 1709 1710 /* 1711 * ddi_initchild: transform node to DS_INITIALIZED state 1712 */ 1713 int 1714 ddi_initchild(dev_info_t *parent, dev_info_t *proto) 1715 { 1716 int ret, circ; 1717 1718 ndi_devi_enter(parent, &circ); 1719 ret = i_ndi_config_node(proto, DS_INITIALIZED, 0); 1720 ndi_devi_exit(parent, circ); 1721 1722 return (ret); 1723 } 1724 1725 /* 1726 * ddi_uninitchild: transform node down to DS_BOUND state 1727 */ 1728 int 1729 ddi_uninitchild(dev_info_t *dip) 1730 { 1731 int ret, circ; 1732 dev_info_t *parent = ddi_get_parent(dip); 1733 ASSERT(parent); 1734 1735 ndi_devi_enter(parent, &circ); 1736 ret = i_ndi_unconfig_node(dip, DS_BOUND, 0); 1737 ndi_devi_exit(parent, circ); 1738 1739 return (ret); 1740 } 1741 1742 /* 1743 * i_ddi_attachchild: transform node to DS_READY/i_ddi_devi_attached() state 1744 */ 1745 static int 1746 i_ddi_attachchild(dev_info_t *dip) 1747 { 1748 dev_info_t *parent = ddi_get_parent(dip); 1749 int ret; 1750 1751 ASSERT(parent && DEVI_BUSY_OWNED(parent)); 1752 1753 if ((i_ddi_node_state(dip) < DS_BOUND) || DEVI_IS_DEVICE_OFFLINE(dip)) 1754 return (DDI_FAILURE); 1755 1756 ret = i_ndi_config_node(dip, DS_READY, 0); 1757 if (ret == NDI_SUCCESS) { 1758 ret = DDI_SUCCESS; 1759 } else { 1760 /* 1761 * Take it down to DS_INITIALIZED so pm_pre_probe is run 1762 * on the next attach 1763 */ 1764 (void) i_ndi_unconfig_node(dip, DS_INITIALIZED, 0); 1765 ret = DDI_FAILURE; 1766 } 1767 1768 return (ret); 1769 } 1770 1771 /* 1772 * i_ddi_detachchild: transform node down to DS_PROBED state 1773 * If it fails, put it back to DS_READY state. 1774 * NOTE: A node that fails detach may be at DS_ATTACHED instead 1775 * of DS_READY for a small amount of time - this is the source of 1776 * transient DS_READY->DS_ATTACHED->DS_READY state changes. 1777 */ 1778 static int 1779 i_ddi_detachchild(dev_info_t *dip, uint_t flags) 1780 { 1781 dev_info_t *parent = ddi_get_parent(dip); 1782 int ret; 1783 1784 ASSERT(parent && DEVI_BUSY_OWNED(parent)); 1785 1786 ret = i_ndi_unconfig_node(dip, DS_PROBED, flags); 1787 if (ret != DDI_SUCCESS) 1788 (void) i_ndi_config_node(dip, DS_READY, 0); 1789 else 1790 /* allow pm_pre_probe to reestablish pm state */ 1791 (void) i_ndi_unconfig_node(dip, DS_INITIALIZED, 0); 1792 return (ret); 1793 } 1794 1795 /* 1796 * Add a child and bind to driver 1797 */ 1798 dev_info_t * 1799 ddi_add_child(dev_info_t *pdip, char *name, uint_t nodeid, uint_t unit) 1800 { 1801 int circ; 1802 dev_info_t *dip; 1803 1804 /* allocate a new node */ 1805 dip = i_ddi_alloc_node(pdip, name, nodeid, (int)unit, NULL, KM_SLEEP); 1806 1807 ndi_devi_enter(pdip, &circ); 1808 (void) i_ndi_config_node(dip, DS_BOUND, 0); 1809 ndi_devi_exit(pdip, circ); 1810 return (dip); 1811 } 1812 1813 /* 1814 * ddi_remove_child: remove the dip. The parent must be attached and held 1815 */ 1816 int 1817 ddi_remove_child(dev_info_t *dip, int dummy) 1818 { 1819 _NOTE(ARGUNUSED(dummy)) 1820 int circ, ret; 1821 dev_info_t *parent = ddi_get_parent(dip); 1822 ASSERT(parent); 1823 1824 ndi_devi_enter(parent, &circ); 1825 1826 /* 1827 * If we still have children, for example SID nodes marked 1828 * as persistent but not attached, attempt to remove them. 1829 */ 1830 if (DEVI(dip)->devi_child) { 1831 ret = ndi_devi_unconfig(dip, NDI_DEVI_REMOVE); 1832 if (ret != NDI_SUCCESS) { 1833 ndi_devi_exit(parent, circ); 1834 return (DDI_FAILURE); 1835 } 1836 ASSERT(DEVI(dip)->devi_child == NULL); 1837 } 1838 1839 ret = i_ndi_unconfig_node(dip, DS_PROTO, 0); 1840 ndi_devi_exit(parent, circ); 1841 1842 if (ret != DDI_SUCCESS) 1843 return (ret); 1844 1845 ASSERT(i_ddi_node_state(dip) == DS_PROTO); 1846 i_ddi_free_node(dip); 1847 return (DDI_SUCCESS); 1848 } 1849 1850 /* 1851 * NDI wrappers for ref counting, node allocation, and transitions 1852 */ 1853 1854 /* 1855 * Hold/release the devinfo node itself. 1856 * Caller is assumed to prevent the devi from detaching during this call 1857 */ 1858 void 1859 ndi_hold_devi(dev_info_t *dip) 1860 { 1861 mutex_enter(&DEVI(dip)->devi_lock); 1862 ASSERT(DEVI(dip)->devi_ref >= 0); 1863 DEVI(dip)->devi_ref++; 1864 membar_enter(); /* make sure stores are flushed */ 1865 mutex_exit(&DEVI(dip)->devi_lock); 1866 } 1867 1868 void 1869 ndi_rele_devi(dev_info_t *dip) 1870 { 1871 ASSERT(DEVI(dip)->devi_ref > 0); 1872 1873 mutex_enter(&DEVI(dip)->devi_lock); 1874 DEVI(dip)->devi_ref--; 1875 membar_enter(); /* make sure stores are flushed */ 1876 mutex_exit(&DEVI(dip)->devi_lock); 1877 } 1878 1879 int 1880 e_ddi_devi_holdcnt(dev_info_t *dip) 1881 { 1882 return (DEVI(dip)->devi_ref); 1883 } 1884 1885 /* 1886 * Hold/release the driver the devinfo node is bound to. 1887 */ 1888 struct dev_ops * 1889 ndi_hold_driver(dev_info_t *dip) 1890 { 1891 if (i_ddi_node_state(dip) < DS_BOUND) 1892 return (NULL); 1893 1894 ASSERT(DEVI(dip)->devi_major != -1); 1895 return (mod_hold_dev_by_major(DEVI(dip)->devi_major)); 1896 } 1897 1898 void 1899 ndi_rele_driver(dev_info_t *dip) 1900 { 1901 ASSERT(i_ddi_node_state(dip) >= DS_BOUND); 1902 mod_rele_dev_by_major(DEVI(dip)->devi_major); 1903 } 1904 1905 /* 1906 * Single thread entry into devinfo node for modifying its children (devinfo, 1907 * pathinfo, and minor). To verify in ASSERTS use DEVI_BUSY_OWNED macro. 1908 */ 1909 void 1910 ndi_devi_enter(dev_info_t *dip, int *circular) 1911 { 1912 struct dev_info *devi = DEVI(dip); 1913 ASSERT(dip != NULL); 1914 1915 /* for vHCI, enforce (vHCI, pHCI) ndi_deve_enter() order */ 1916 ASSERT(!MDI_VHCI(dip) || (mdi_devi_pdip_entered(dip) == 0) || 1917 DEVI_BUSY_OWNED(dip)); 1918 1919 mutex_enter(&devi->devi_lock); 1920 if (devi->devi_busy_thread == curthread) { 1921 devi->devi_circular++; 1922 } else { 1923 while (DEVI_BUSY_CHANGING(devi) && !panicstr) 1924 cv_wait(&(devi->devi_cv), &(devi->devi_lock)); 1925 if (panicstr) { 1926 mutex_exit(&devi->devi_lock); 1927 return; 1928 } 1929 devi->devi_flags |= DEVI_BUSY; 1930 devi->devi_busy_thread = curthread; 1931 } 1932 *circular = devi->devi_circular; 1933 mutex_exit(&devi->devi_lock); 1934 } 1935 1936 /* 1937 * Release ndi_devi_enter or successful ndi_devi_tryenter. 1938 */ 1939 void 1940 ndi_devi_exit(dev_info_t *dip, int circular) 1941 { 1942 struct dev_info *devi = DEVI(dip); 1943 struct dev_info *vdevi; 1944 ASSERT(dip != NULL); 1945 1946 if (panicstr) 1947 return; 1948 1949 mutex_enter(&(devi->devi_lock)); 1950 if (circular != 0) { 1951 devi->devi_circular--; 1952 } else { 1953 devi->devi_flags &= ~DEVI_BUSY; 1954 ASSERT(devi->devi_busy_thread == curthread); 1955 devi->devi_busy_thread = NULL; 1956 cv_broadcast(&(devi->devi_cv)); 1957 } 1958 mutex_exit(&(devi->devi_lock)); 1959 1960 /* 1961 * For pHCI exit we issue a broadcast to vHCI for ndi_devi_config_one() 1962 * doing cv_wait on vHCI. 1963 */ 1964 if (MDI_PHCI(dip)) { 1965 vdevi = DEVI(mdi_devi_get_vdip(dip)); 1966 if (vdevi) { 1967 mutex_enter(&(vdevi->devi_lock)); 1968 if (vdevi->devi_flags & DEVI_PHCI_SIGNALS_VHCI) { 1969 vdevi->devi_flags &= ~DEVI_PHCI_SIGNALS_VHCI; 1970 cv_broadcast(&(vdevi->devi_cv)); 1971 } 1972 mutex_exit(&(vdevi->devi_lock)); 1973 } 1974 } 1975 } 1976 1977 /* 1978 * Release ndi_devi_enter and wait for possibility of new children, avoiding 1979 * possibility of missing broadcast before getting to cv_timedwait(). 1980 */ 1981 static void 1982 ndi_devi_exit_and_wait(dev_info_t *dip, int circular, clock_t end_time) 1983 { 1984 struct dev_info *devi = DEVI(dip); 1985 ASSERT(dip != NULL); 1986 1987 if (panicstr) 1988 return; 1989 1990 /* 1991 * We are called to wait for of a new child, and new child can 1992 * only be added if circular is zero. 1993 */ 1994 ASSERT(circular == 0); 1995 1996 /* like ndi_devi_exit with circular of zero */ 1997 mutex_enter(&(devi->devi_lock)); 1998 devi->devi_flags &= ~DEVI_BUSY; 1999 ASSERT(devi->devi_busy_thread == curthread); 2000 devi->devi_busy_thread = NULL; 2001 cv_broadcast(&(devi->devi_cv)); 2002 2003 /* now wait for new children while still holding devi_lock */ 2004 (void) cv_timedwait(&devi->devi_cv, &(devi->devi_lock), end_time); 2005 mutex_exit(&(devi->devi_lock)); 2006 } 2007 2008 /* 2009 * Attempt to single thread entry into devinfo node for modifying its children. 2010 */ 2011 int 2012 ndi_devi_tryenter(dev_info_t *dip, int *circular) 2013 { 2014 int rval = 1; /* assume we enter */ 2015 struct dev_info *devi = DEVI(dip); 2016 ASSERT(dip != NULL); 2017 2018 mutex_enter(&devi->devi_lock); 2019 if (devi->devi_busy_thread == (void *)curthread) { 2020 devi->devi_circular++; 2021 } else { 2022 if (!DEVI_BUSY_CHANGING(devi)) { 2023 devi->devi_flags |= DEVI_BUSY; 2024 devi->devi_busy_thread = (void *)curthread; 2025 } else { 2026 rval = 0; /* devi is busy */ 2027 } 2028 } 2029 *circular = devi->devi_circular; 2030 mutex_exit(&devi->devi_lock); 2031 return (rval); 2032 } 2033 2034 /* 2035 * Allocate and initialize a new dev_info structure. 2036 * 2037 * This routine may be called at interrupt time by a nexus in 2038 * response to a hotplug event, therefore memory allocations are 2039 * not allowed to sleep. 2040 */ 2041 int 2042 ndi_devi_alloc(dev_info_t *parent, char *node_name, pnode_t nodeid, 2043 dev_info_t **ret_dip) 2044 { 2045 ASSERT(node_name != NULL); 2046 ASSERT(ret_dip != NULL); 2047 2048 *ret_dip = i_ddi_alloc_node(parent, node_name, nodeid, -1, NULL, 2049 KM_NOSLEEP); 2050 if (*ret_dip == NULL) { 2051 return (NDI_NOMEM); 2052 } 2053 2054 return (NDI_SUCCESS); 2055 } 2056 2057 /* 2058 * Allocate and initialize a new dev_info structure 2059 * This routine may sleep and should not be called at interrupt time 2060 */ 2061 void 2062 ndi_devi_alloc_sleep(dev_info_t *parent, char *node_name, pnode_t nodeid, 2063 dev_info_t **ret_dip) 2064 { 2065 ASSERT(node_name != NULL); 2066 ASSERT(ret_dip != NULL); 2067 2068 *ret_dip = i_ddi_alloc_node(parent, node_name, nodeid, -1, NULL, 2069 KM_SLEEP); 2070 ASSERT(*ret_dip); 2071 } 2072 2073 /* 2074 * Remove an initialized (but not yet attached) dev_info 2075 * node from it's parent. 2076 */ 2077 int 2078 ndi_devi_free(dev_info_t *dip) 2079 { 2080 ASSERT(dip != NULL); 2081 2082 if (i_ddi_node_state(dip) >= DS_INITIALIZED) 2083 return (DDI_FAILURE); 2084 2085 NDI_CONFIG_DEBUG((CE_CONT, "ndi_devi_free: %s%d (%p)\n", 2086 ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip)); 2087 2088 (void) ddi_remove_child(dip, 0); 2089 2090 return (NDI_SUCCESS); 2091 } 2092 2093 /* 2094 * ndi_devi_bind_driver() binds a driver to a given device. If it fails 2095 * to bind the driver, it returns an appropriate error back. Some drivers 2096 * may want to know if the actually failed to bind. 2097 */ 2098 int 2099 ndi_devi_bind_driver(dev_info_t *dip, uint_t flags) 2100 { 2101 int ret = NDI_FAILURE; 2102 int circ; 2103 dev_info_t *pdip = ddi_get_parent(dip); 2104 ASSERT(pdip); 2105 2106 NDI_CONFIG_DEBUG((CE_CONT, 2107 "ndi_devi_bind_driver: %s%d (%p) flags: %x\n", 2108 ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags)); 2109 2110 ndi_devi_enter(pdip, &circ); 2111 if (i_ndi_config_node(dip, DS_BOUND, flags) == DDI_SUCCESS) 2112 ret = NDI_SUCCESS; 2113 ndi_devi_exit(pdip, circ); 2114 2115 return (ret); 2116 } 2117 2118 /* 2119 * ndi_devi_unbind_driver: unbind the dip 2120 */ 2121 static int 2122 ndi_devi_unbind_driver(dev_info_t *dip) 2123 { 2124 ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip))); 2125 2126 return (i_ndi_unconfig_node(dip, DS_LINKED, 0)); 2127 } 2128 2129 /* 2130 * Misc. help routines called by framework only 2131 */ 2132 2133 /* 2134 * Get the state of node 2135 */ 2136 ddi_node_state_t 2137 i_ddi_node_state(dev_info_t *dip) 2138 { 2139 return (DEVI(dip)->devi_node_state); 2140 } 2141 2142 /* 2143 * Set the state of node 2144 */ 2145 void 2146 i_ddi_set_node_state(dev_info_t *dip, ddi_node_state_t state) 2147 { 2148 DEVI(dip)->devi_node_state = state; 2149 membar_enter(); /* make sure stores are flushed */ 2150 } 2151 2152 /* 2153 * Determine if node is attached. The implementation accommodates transient 2154 * DS_READY->DS_ATTACHED->DS_READY state changes. Outside this file, this 2155 * function should be instead of i_ddi_node_state() DS_ATTACHED/DS_READY 2156 * state checks. 2157 */ 2158 int 2159 i_ddi_devi_attached(dev_info_t *dip) 2160 { 2161 return (DEVI(dip)->devi_node_state >= DS_ATTACHED); 2162 } 2163 2164 /* 2165 * Common function for finding a node in a sibling list given name and addr. 2166 * 2167 * By default, name is matched with devi_node_name. The following 2168 * alternative match strategies are supported: 2169 * 2170 * FIND_NODE_BY_NODENAME: Match on node name - typical use. 2171 * 2172 * FIND_NODE_BY_DRIVER: A match on driver name bound to node is conducted. 2173 * This support is used for support of OBP generic names and 2174 * for the conversion from driver names to generic names. When 2175 * more consistency in the generic name environment is achieved 2176 * (and not needed for upgrade) this support can be removed. 2177 * 2178 * FIND_NODE_BY_ADDR: Match on just the addr. 2179 * This support is only used/needed during boot to match 2180 * a node bound via a path-based driver alias. 2181 * 2182 * If a child is not named (dev_addr == NULL), there are three 2183 * possible actions: 2184 * 2185 * (1) skip it 2186 * (2) FIND_ADDR_BY_INIT: bring child to DS_INITIALIZED state 2187 * (3) FIND_ADDR_BY_CALLBACK: use a caller-supplied callback function 2188 */ 2189 #define FIND_NODE_BY_NODENAME 0x01 2190 #define FIND_NODE_BY_DRIVER 0x02 2191 #define FIND_NODE_BY_ADDR 0x04 2192 #define FIND_ADDR_BY_INIT 0x10 2193 #define FIND_ADDR_BY_CALLBACK 0x20 2194 2195 static dev_info_t * 2196 find_sibling(dev_info_t *head, char *cname, char *caddr, uint_t flag, 2197 int (*callback)(dev_info_t *, char *, int)) 2198 { 2199 dev_info_t *dip; 2200 char *addr, *buf; 2201 major_t major; 2202 uint_t by; 2203 2204 /* only one way to find a node */ 2205 by = flag & 2206 (FIND_NODE_BY_DRIVER | FIND_NODE_BY_NODENAME | FIND_NODE_BY_ADDR); 2207 ASSERT(by && BIT_ONLYONESET(by)); 2208 2209 /* only one way to name a node */ 2210 ASSERT(((flag & FIND_ADDR_BY_INIT) == 0) || 2211 ((flag & FIND_ADDR_BY_CALLBACK) == 0)); 2212 2213 if (by == FIND_NODE_BY_DRIVER) { 2214 major = ddi_name_to_major(cname); 2215 if (major == DDI_MAJOR_T_NONE) 2216 return (NULL); 2217 } 2218 2219 /* preallocate buffer of naming node by callback */ 2220 if (flag & FIND_ADDR_BY_CALLBACK) 2221 buf = kmem_alloc(MAXNAMELEN, KM_SLEEP); 2222 2223 /* 2224 * Walk the child list to find a match 2225 */ 2226 if (head == NULL) 2227 return (NULL); 2228 ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(head))); 2229 for (dip = head; dip; dip = ddi_get_next_sibling(dip)) { 2230 if (by == FIND_NODE_BY_NODENAME) { 2231 /* match node name */ 2232 if (strcmp(cname, DEVI(dip)->devi_node_name) != 0) 2233 continue; 2234 } else if (by == FIND_NODE_BY_DRIVER) { 2235 /* match driver major */ 2236 if (DEVI(dip)->devi_major != major) 2237 continue; 2238 } 2239 2240 if ((addr = DEVI(dip)->devi_addr) == NULL) { 2241 /* name the child based on the flag */ 2242 if (flag & FIND_ADDR_BY_INIT) { 2243 if (ddi_initchild(ddi_get_parent(dip), dip) 2244 != DDI_SUCCESS) 2245 continue; 2246 addr = DEVI(dip)->devi_addr; 2247 } else if (flag & FIND_ADDR_BY_CALLBACK) { 2248 if ((callback == NULL) || (callback( 2249 dip, buf, MAXNAMELEN) != DDI_SUCCESS)) 2250 continue; 2251 addr = buf; 2252 } else { 2253 continue; /* skip */ 2254 } 2255 } 2256 2257 /* match addr */ 2258 ASSERT(addr != NULL); 2259 if (strcmp(caddr, addr) == 0) 2260 break; /* node found */ 2261 2262 } 2263 if (flag & FIND_ADDR_BY_CALLBACK) 2264 kmem_free(buf, MAXNAMELEN); 2265 return (dip); 2266 } 2267 2268 /* 2269 * Find child of pdip with name: cname@caddr 2270 * Called by init_node() to look for duplicate nodes 2271 */ 2272 static dev_info_t * 2273 find_duplicate_child(dev_info_t *pdip, dev_info_t *dip) 2274 { 2275 dev_info_t *dup; 2276 char *cname = DEVI(dip)->devi_node_name; 2277 char *caddr = DEVI(dip)->devi_addr; 2278 2279 /* search nodes before dip */ 2280 dup = find_sibling(ddi_get_child(pdip), cname, caddr, 2281 FIND_NODE_BY_NODENAME, NULL); 2282 if (dup != dip) 2283 return (dup); 2284 2285 /* 2286 * search nodes after dip; normally this is not needed, 2287 */ 2288 return (find_sibling(ddi_get_next_sibling(dip), cname, caddr, 2289 FIND_NODE_BY_NODENAME, NULL)); 2290 } 2291 2292 /* 2293 * Find a child of a given name and address, using a callback to name 2294 * unnamed children. cname is the binding name. 2295 */ 2296 dev_info_t * 2297 ndi_devi_findchild_by_callback(dev_info_t *pdip, char *dname, char *ua, 2298 int (*make_ua)(dev_info_t *, char *, int)) 2299 { 2300 int by = FIND_ADDR_BY_CALLBACK; 2301 2302 ASSERT(DEVI_BUSY_OWNED(pdip)); 2303 by |= dname ? FIND_NODE_BY_DRIVER : FIND_NODE_BY_ADDR; 2304 return (find_sibling(ddi_get_child(pdip), dname, ua, by, make_ua)); 2305 } 2306 2307 /* 2308 * Find a child of a given name and address, invoking initchild to name 2309 * unnamed children. cname is the node name. 2310 */ 2311 static dev_info_t * 2312 find_child_by_name(dev_info_t *pdip, char *cname, char *caddr) 2313 { 2314 dev_info_t *dip; 2315 2316 /* attempt search without changing state of preceding siblings */ 2317 dip = find_sibling(ddi_get_child(pdip), cname, caddr, 2318 FIND_NODE_BY_NODENAME, NULL); 2319 if (dip) 2320 return (dip); 2321 2322 return (find_sibling(ddi_get_child(pdip), cname, caddr, 2323 FIND_NODE_BY_NODENAME|FIND_ADDR_BY_INIT, NULL)); 2324 } 2325 2326 /* 2327 * Find a child of a given name and address, invoking initchild to name 2328 * unnamed children. cname is the node name. 2329 */ 2330 static dev_info_t * 2331 find_child_by_driver(dev_info_t *pdip, char *cname, char *caddr) 2332 { 2333 dev_info_t *dip; 2334 2335 /* attempt search without changing state of preceding siblings */ 2336 dip = find_sibling(ddi_get_child(pdip), cname, caddr, 2337 FIND_NODE_BY_DRIVER, NULL); 2338 if (dip) 2339 return (dip); 2340 2341 return (find_sibling(ddi_get_child(pdip), cname, caddr, 2342 FIND_NODE_BY_DRIVER|FIND_ADDR_BY_INIT, NULL)); 2343 } 2344 2345 /* 2346 * Find a child of a given address, invoking initchild to name 2347 * unnamed children. cname is the node name. 2348 * 2349 * NOTE: This function is only used during boot. One would hope that 2350 * unique sibling unit-addresses on hardware branches of the tree would 2351 * be a requirement to avoid two drivers trying to control the same 2352 * piece of hardware. Unfortunately there are some cases where this 2353 * situation exists (/ssm@0,0/pci@1c,700000 /ssm@0,0/sghsc@1c,700000). 2354 * Until unit-address uniqueness of siblings is guaranteed, use of this 2355 * interface for purposes other than boot should be avoided. 2356 */ 2357 static dev_info_t * 2358 find_child_by_addr(dev_info_t *pdip, char *caddr) 2359 { 2360 dev_info_t *dip; 2361 2362 /* return NULL if called without a unit-address */ 2363 if ((caddr == NULL) || (*caddr == '\0')) 2364 return (NULL); 2365 2366 /* attempt search without changing state of preceding siblings */ 2367 dip = find_sibling(ddi_get_child(pdip), NULL, caddr, 2368 FIND_NODE_BY_ADDR, NULL); 2369 if (dip) 2370 return (dip); 2371 2372 return (find_sibling(ddi_get_child(pdip), NULL, caddr, 2373 FIND_NODE_BY_ADDR|FIND_ADDR_BY_INIT, NULL)); 2374 } 2375 2376 /* 2377 * Deleting a property list. Take care, since some property structures 2378 * may not be fully built. 2379 */ 2380 void 2381 i_ddi_prop_list_delete(ddi_prop_t *prop) 2382 { 2383 while (prop) { 2384 ddi_prop_t *next = prop->prop_next; 2385 if (prop->prop_name) 2386 kmem_free(prop->prop_name, strlen(prop->prop_name) + 1); 2387 if ((prop->prop_len != 0) && prop->prop_val) 2388 kmem_free(prop->prop_val, prop->prop_len); 2389 kmem_free(prop, sizeof (struct ddi_prop)); 2390 prop = next; 2391 } 2392 } 2393 2394 /* 2395 * Duplicate property list 2396 */ 2397 ddi_prop_t * 2398 i_ddi_prop_list_dup(ddi_prop_t *prop, uint_t flag) 2399 { 2400 ddi_prop_t *result, *prev, *copy; 2401 2402 if (prop == NULL) 2403 return (NULL); 2404 2405 result = prev = NULL; 2406 for (; prop != NULL; prop = prop->prop_next) { 2407 ASSERT(prop->prop_name != NULL); 2408 copy = kmem_zalloc(sizeof (struct ddi_prop), flag); 2409 if (copy == NULL) 2410 goto fail; 2411 2412 copy->prop_dev = prop->prop_dev; 2413 copy->prop_flags = prop->prop_flags; 2414 copy->prop_name = i_ddi_strdup(prop->prop_name, flag); 2415 if (copy->prop_name == NULL) 2416 goto fail; 2417 2418 if ((copy->prop_len = prop->prop_len) != 0) { 2419 copy->prop_val = kmem_zalloc(prop->prop_len, flag); 2420 if (copy->prop_val == NULL) 2421 goto fail; 2422 2423 bcopy(prop->prop_val, copy->prop_val, prop->prop_len); 2424 } 2425 2426 if (prev == NULL) 2427 result = prev = copy; 2428 else 2429 prev->prop_next = copy; 2430 prev = copy; 2431 } 2432 return (result); 2433 2434 fail: 2435 i_ddi_prop_list_delete(result); 2436 return (NULL); 2437 } 2438 2439 /* 2440 * Create a reference property list, currently used only for 2441 * driver global properties. Created with ref count of 1. 2442 */ 2443 ddi_prop_list_t * 2444 i_ddi_prop_list_create(ddi_prop_t *props) 2445 { 2446 ddi_prop_list_t *list = kmem_alloc(sizeof (*list), KM_SLEEP); 2447 list->prop_list = props; 2448 list->prop_ref = 1; 2449 return (list); 2450 } 2451 2452 /* 2453 * Increment/decrement reference count. The reference is 2454 * protected by dn_lock. The only interfaces modifying 2455 * dn_global_prop_ptr is in impl_make[free]_parlist(). 2456 */ 2457 void 2458 i_ddi_prop_list_hold(ddi_prop_list_t *prop_list, struct devnames *dnp) 2459 { 2460 ASSERT(prop_list->prop_ref >= 0); 2461 ASSERT(mutex_owned(&dnp->dn_lock)); 2462 prop_list->prop_ref++; 2463 } 2464 2465 void 2466 i_ddi_prop_list_rele(ddi_prop_list_t *prop_list, struct devnames *dnp) 2467 { 2468 ASSERT(prop_list->prop_ref > 0); 2469 ASSERT(mutex_owned(&dnp->dn_lock)); 2470 prop_list->prop_ref--; 2471 2472 if (prop_list->prop_ref == 0) { 2473 i_ddi_prop_list_delete(prop_list->prop_list); 2474 kmem_free(prop_list, sizeof (*prop_list)); 2475 } 2476 } 2477 2478 /* 2479 * Free table of classes by drivers 2480 */ 2481 void 2482 i_ddi_free_exported_classes(char **classes, int n) 2483 { 2484 if ((n == 0) || (classes == NULL)) 2485 return; 2486 2487 kmem_free(classes, n * sizeof (char *)); 2488 } 2489 2490 /* 2491 * Get all classes exported by dip 2492 */ 2493 int 2494 i_ddi_get_exported_classes(dev_info_t *dip, char ***classes) 2495 { 2496 extern void lock_hw_class_list(); 2497 extern void unlock_hw_class_list(); 2498 extern int get_class(const char *, char **); 2499 2500 static char *rootclass = "root"; 2501 int n = 0, nclass = 0; 2502 char **buf; 2503 2504 ASSERT(i_ddi_node_state(dip) >= DS_BOUND); 2505 2506 if (dip == ddi_root_node()) /* rootnode exports class "root" */ 2507 nclass = 1; 2508 lock_hw_class_list(); 2509 nclass += get_class(ddi_driver_name(dip), NULL); 2510 if (nclass == 0) { 2511 unlock_hw_class_list(); 2512 return (0); /* no class exported */ 2513 } 2514 2515 *classes = buf = kmem_alloc(nclass * sizeof (char *), KM_SLEEP); 2516 if (dip == ddi_root_node()) { 2517 *buf++ = rootclass; 2518 n = 1; 2519 } 2520 n += get_class(ddi_driver_name(dip), buf); 2521 unlock_hw_class_list(); 2522 2523 ASSERT(n == nclass); /* make sure buf wasn't overrun */ 2524 return (nclass); 2525 } 2526 2527 /* 2528 * Helper functions, returns NULL if no memory. 2529 */ 2530 char * 2531 i_ddi_strdup(char *str, uint_t flag) 2532 { 2533 char *copy; 2534 2535 if (str == NULL) 2536 return (NULL); 2537 2538 copy = kmem_alloc(strlen(str) + 1, flag); 2539 if (copy == NULL) 2540 return (NULL); 2541 2542 (void) strcpy(copy, str); 2543 return (copy); 2544 } 2545 2546 /* 2547 * Load driver.conf file for major. Load all if major == -1. 2548 * 2549 * This is called 2550 * - early in boot after devnames array is initialized 2551 * - from vfs code when certain file systems are mounted 2552 * - from add_drv when a new driver is added 2553 */ 2554 int 2555 i_ddi_load_drvconf(major_t major) 2556 { 2557 extern int modrootloaded; 2558 2559 major_t low, high, m; 2560 2561 if (major == DDI_MAJOR_T_NONE) { 2562 low = 0; 2563 high = devcnt - 1; 2564 } else { 2565 if (major >= devcnt) 2566 return (EINVAL); 2567 low = high = major; 2568 } 2569 2570 for (m = low; m <= high; m++) { 2571 struct devnames *dnp = &devnamesp[m]; 2572 LOCK_DEV_OPS(&dnp->dn_lock); 2573 dnp->dn_flags &= ~(DN_DRIVER_HELD|DN_DRIVER_INACTIVE); 2574 (void) impl_make_parlist(m); 2575 UNLOCK_DEV_OPS(&dnp->dn_lock); 2576 } 2577 2578 if (modrootloaded) { 2579 ddi_walk_devs(ddi_root_node(), reset_nexus_flags, 2580 (void *)(uintptr_t)major); 2581 } 2582 2583 /* build dn_list from old entries in path_to_inst */ 2584 e_ddi_unorphan_instance_nos(); 2585 return (0); 2586 } 2587 2588 /* 2589 * Unload a specific driver.conf. 2590 * Don't support unload all because it doesn't make any sense 2591 */ 2592 int 2593 i_ddi_unload_drvconf(major_t major) 2594 { 2595 int error; 2596 struct devnames *dnp; 2597 2598 if (major >= devcnt) 2599 return (EINVAL); 2600 2601 /* 2602 * Take the per-driver lock while unloading driver.conf 2603 */ 2604 dnp = &devnamesp[major]; 2605 LOCK_DEV_OPS(&dnp->dn_lock); 2606 error = impl_free_parlist(major); 2607 UNLOCK_DEV_OPS(&dnp->dn_lock); 2608 return (error); 2609 } 2610 2611 /* 2612 * Merge a .conf node. This is called by nexus drivers to augment 2613 * hw node with properties specified in driver.conf file. This function 2614 * takes a callback routine to name nexus children. 2615 * The parent node must be held busy. 2616 * 2617 * It returns DDI_SUCCESS if the node is merged and DDI_FAILURE otherwise. 2618 */ 2619 int 2620 ndi_merge_node(dev_info_t *dip, int (*make_ua)(dev_info_t *, char *, int)) 2621 { 2622 dev_info_t *hwdip; 2623 2624 ASSERT(ndi_dev_is_persistent_node(dip) == 0); 2625 ASSERT(ddi_get_name_addr(dip) != NULL); 2626 2627 hwdip = ndi_devi_findchild_by_callback(ddi_get_parent(dip), 2628 ddi_binding_name(dip), ddi_get_name_addr(dip), make_ua); 2629 2630 /* 2631 * Look for the hardware node that is the target of the merge; 2632 * return failure if not found. 2633 */ 2634 if ((hwdip == NULL) || (hwdip == dip)) { 2635 char *buf = kmem_alloc(MAXNAMELEN, KM_SLEEP); 2636 NDI_CONFIG_DEBUG((CE_WARN, "No HW node to merge conf node %s", 2637 ddi_deviname(dip, buf))); 2638 kmem_free(buf, MAXNAMELEN); 2639 return (DDI_FAILURE); 2640 } 2641 2642 /* 2643 * Make sure the hardware node is uninitialized and has no property. 2644 * This may not be the case if new .conf files are load after some 2645 * hardware nodes have already been initialized and attached. 2646 * 2647 * N.B. We return success here because the node was *intended* 2648 * to be a merge node because there is a hw node with the name. 2649 */ 2650 mutex_enter(&DEVI(hwdip)->devi_lock); 2651 if (ndi_dev_is_persistent_node(hwdip) == 0) { 2652 char *buf; 2653 mutex_exit(&DEVI(hwdip)->devi_lock); 2654 2655 buf = kmem_alloc(MAXNAMELEN, KM_SLEEP); 2656 NDI_CONFIG_DEBUG((CE_NOTE, "Duplicate .conf node %s", 2657 ddi_deviname(dip, buf))); 2658 kmem_free(buf, MAXNAMELEN); 2659 return (DDI_SUCCESS); 2660 } 2661 2662 /* 2663 * If it is possible that the hardware has already been touched 2664 * then don't merge. 2665 */ 2666 if (i_ddi_node_state(hwdip) >= DS_INITIALIZED || 2667 (DEVI(hwdip)->devi_sys_prop_ptr != NULL) || 2668 (DEVI(hwdip)->devi_drv_prop_ptr != NULL)) { 2669 char *buf; 2670 mutex_exit(&DEVI(hwdip)->devi_lock); 2671 2672 buf = kmem_alloc(MAXNAMELEN, KM_SLEEP); 2673 NDI_CONFIG_DEBUG((CE_NOTE, 2674 "!Cannot merge .conf node %s with hw node %p " 2675 "-- not in proper state", 2676 ddi_deviname(dip, buf), (void *)hwdip)); 2677 kmem_free(buf, MAXNAMELEN); 2678 return (DDI_SUCCESS); 2679 } 2680 2681 mutex_enter(&DEVI(dip)->devi_lock); 2682 DEVI(hwdip)->devi_sys_prop_ptr = DEVI(dip)->devi_sys_prop_ptr; 2683 DEVI(hwdip)->devi_drv_prop_ptr = DEVI(dip)->devi_drv_prop_ptr; 2684 DEVI(dip)->devi_sys_prop_ptr = NULL; 2685 DEVI(dip)->devi_drv_prop_ptr = NULL; 2686 mutex_exit(&DEVI(dip)->devi_lock); 2687 mutex_exit(&DEVI(hwdip)->devi_lock); 2688 2689 return (DDI_SUCCESS); 2690 } 2691 2692 /* 2693 * Merge a "wildcard" .conf node. This is called by nexus drivers to 2694 * augment a set of hw node with properties specified in driver.conf file. 2695 * The parent node must be held busy. 2696 * 2697 * There is no failure mode, since the nexus may or may not have child 2698 * node bound the driver specified by the wildcard node. 2699 */ 2700 void 2701 ndi_merge_wildcard_node(dev_info_t *dip) 2702 { 2703 dev_info_t *hwdip; 2704 dev_info_t *pdip = ddi_get_parent(dip); 2705 major_t major = ddi_driver_major(dip); 2706 2707 /* never attempt to merge a hw node */ 2708 ASSERT(ndi_dev_is_persistent_node(dip) == 0); 2709 /* must be bound to a driver major number */ 2710 ASSERT(major != DDI_MAJOR_T_NONE); 2711 2712 /* 2713 * Walk the child list to find all nodes bound to major 2714 * and copy properties. 2715 */ 2716 mutex_enter(&DEVI(dip)->devi_lock); 2717 ASSERT(DEVI_BUSY_OWNED(pdip)); 2718 for (hwdip = ddi_get_child(pdip); hwdip; 2719 hwdip = ddi_get_next_sibling(hwdip)) { 2720 /* 2721 * Skip nodes not bound to same driver 2722 */ 2723 if (ddi_driver_major(hwdip) != major) 2724 continue; 2725 2726 /* 2727 * Skip .conf nodes 2728 */ 2729 if (ndi_dev_is_persistent_node(hwdip) == 0) 2730 continue; 2731 2732 /* 2733 * Make sure the node is uninitialized and has no property. 2734 */ 2735 mutex_enter(&DEVI(hwdip)->devi_lock); 2736 if (i_ddi_node_state(hwdip) >= DS_INITIALIZED || 2737 (DEVI(hwdip)->devi_sys_prop_ptr != NULL) || 2738 (DEVI(hwdip)->devi_drv_prop_ptr != NULL)) { 2739 mutex_exit(&DEVI(hwdip)->devi_lock); 2740 NDI_CONFIG_DEBUG((CE_NOTE, "HW node %p state not " 2741 "suitable for merging wildcard conf node %s", 2742 (void *)hwdip, ddi_node_name(dip))); 2743 continue; 2744 } 2745 2746 DEVI(hwdip)->devi_sys_prop_ptr = 2747 i_ddi_prop_list_dup(DEVI(dip)->devi_sys_prop_ptr, KM_SLEEP); 2748 DEVI(hwdip)->devi_drv_prop_ptr = 2749 i_ddi_prop_list_dup(DEVI(dip)->devi_drv_prop_ptr, KM_SLEEP); 2750 mutex_exit(&DEVI(hwdip)->devi_lock); 2751 } 2752 mutex_exit(&DEVI(dip)->devi_lock); 2753 } 2754 2755 /* 2756 * Return the major number based on the compatible property. This interface 2757 * may be used in situations where we are trying to detect if a better driver 2758 * now exists for a device, so it must use the 'compatible' property. If 2759 * a non-NULL formp is specified and the binding was based on compatible then 2760 * return the pointer to the form used in *formp. 2761 */ 2762 major_t 2763 ddi_compatible_driver_major(dev_info_t *dip, char **formp) 2764 { 2765 struct dev_info *devi = DEVI(dip); 2766 void *compat; 2767 size_t len; 2768 char *p = NULL; 2769 major_t major = DDI_MAJOR_T_NONE; 2770 2771 if (formp) 2772 *formp = NULL; 2773 2774 if (ddi_prop_exists(DDI_DEV_T_NONE, dip, DDI_PROP_DONTPASS, 2775 "ddi-assigned")) { 2776 major = ddi_name_to_major("nulldriver"); 2777 return (major); 2778 } 2779 2780 /* 2781 * Highest precedence binding is a path-oriented alias. Since this 2782 * requires a 'path', this type of binding occurs via more obtuse 2783 * 'rebind'. The need for a path-oriented alias 'rebind' is detected 2784 * after a successful DDI_CTLOPS_INITCHILD to another driver: this is 2785 * is the first point at which the unit-address (or instance) of the 2786 * last component of the path is available (even though the path is 2787 * bound to the wrong driver at this point). 2788 */ 2789 if (devi->devi_flags & DEVI_REBIND) { 2790 p = devi->devi_rebinding_name; 2791 major = ddi_name_to_major(p); 2792 if (driver_active(major)) { 2793 if (formp) 2794 *formp = p; 2795 return (major); 2796 } 2797 2798 /* 2799 * If for some reason devi_rebinding_name no longer resolves 2800 * to a proper driver then clear DEVI_REBIND. 2801 */ 2802 mutex_enter(&devi->devi_lock); 2803 devi->devi_flags &= ~DEVI_REBIND; 2804 mutex_exit(&devi->devi_lock); 2805 } 2806 2807 /* look up compatible property */ 2808 (void) lookup_compatible(dip, KM_SLEEP); 2809 compat = (void *)(devi->devi_compat_names); 2810 len = devi->devi_compat_length; 2811 2812 /* find the highest precedence compatible form with a driver binding */ 2813 while ((p = prom_decode_composite_string(compat, len, p)) != NULL) { 2814 major = ddi_name_to_major(p); 2815 if (driver_active(major)) { 2816 if (formp) 2817 *formp = p; 2818 return (major); 2819 } 2820 } 2821 2822 /* 2823 * none of the compatible forms have a driver binding, see if 2824 * the node name has a driver binding. 2825 */ 2826 major = ddi_name_to_major(ddi_node_name(dip)); 2827 if (driver_active(major)) 2828 return (major); 2829 2830 /* no driver */ 2831 return (DDI_MAJOR_T_NONE); 2832 } 2833 2834 /* 2835 * Static help functions 2836 */ 2837 2838 /* 2839 * lookup the "compatible" property and cache it's contents in the 2840 * device node. 2841 */ 2842 static int 2843 lookup_compatible(dev_info_t *dip, uint_t flag) 2844 { 2845 int rv; 2846 int prop_flags; 2847 uint_t ncompatstrs; 2848 char **compatstrpp; 2849 char *di_compat_strp; 2850 size_t di_compat_strlen; 2851 2852 if (DEVI(dip)->devi_compat_names) { 2853 return (DDI_SUCCESS); 2854 } 2855 2856 prop_flags = DDI_PROP_TYPE_STRING | DDI_PROP_DONTPASS; 2857 2858 if (flag & KM_NOSLEEP) { 2859 prop_flags |= DDI_PROP_DONTSLEEP; 2860 } 2861 2862 if (ndi_dev_is_prom_node(dip) == 0) { 2863 prop_flags |= DDI_PROP_NOTPROM; 2864 } 2865 2866 rv = ddi_prop_lookup_common(DDI_DEV_T_ANY, dip, prop_flags, 2867 "compatible", &compatstrpp, &ncompatstrs, 2868 ddi_prop_fm_decode_strings); 2869 2870 if (rv == DDI_PROP_NOT_FOUND) { 2871 return (DDI_SUCCESS); 2872 } 2873 2874 if (rv != DDI_PROP_SUCCESS) { 2875 return (DDI_FAILURE); 2876 } 2877 2878 /* 2879 * encode the compatible property data in the dev_info node 2880 */ 2881 rv = DDI_SUCCESS; 2882 if (ncompatstrs != 0) { 2883 di_compat_strp = encode_composite_string(compatstrpp, 2884 ncompatstrs, &di_compat_strlen, flag); 2885 if (di_compat_strp != NULL) { 2886 DEVI(dip)->devi_compat_names = di_compat_strp; 2887 DEVI(dip)->devi_compat_length = di_compat_strlen; 2888 } else { 2889 rv = DDI_FAILURE; 2890 } 2891 } 2892 ddi_prop_free(compatstrpp); 2893 return (rv); 2894 } 2895 2896 /* 2897 * Create a composite string from a list of strings. 2898 * 2899 * A composite string consists of a single buffer containing one 2900 * or more NULL terminated strings. 2901 */ 2902 static char * 2903 encode_composite_string(char **strings, uint_t nstrings, size_t *retsz, 2904 uint_t flag) 2905 { 2906 uint_t index; 2907 char **strpp; 2908 uint_t slen; 2909 size_t cbuf_sz = 0; 2910 char *cbuf_p; 2911 char *cbuf_ip; 2912 2913 if (strings == NULL || nstrings == 0 || retsz == NULL) { 2914 return (NULL); 2915 } 2916 2917 for (index = 0, strpp = strings; index < nstrings; index++) 2918 cbuf_sz += strlen(*(strpp++)) + 1; 2919 2920 if ((cbuf_p = kmem_alloc(cbuf_sz, flag)) == NULL) { 2921 cmn_err(CE_NOTE, 2922 "?failed to allocate device node compatstr"); 2923 return (NULL); 2924 } 2925 2926 cbuf_ip = cbuf_p; 2927 for (index = 0, strpp = strings; index < nstrings; index++) { 2928 slen = strlen(*strpp); 2929 bcopy(*(strpp++), cbuf_ip, slen); 2930 cbuf_ip += slen; 2931 *(cbuf_ip++) = '\0'; 2932 } 2933 2934 *retsz = cbuf_sz; 2935 return (cbuf_p); 2936 } 2937 2938 static void 2939 link_to_driver_list(dev_info_t *dip) 2940 { 2941 major_t major = DEVI(dip)->devi_major; 2942 struct devnames *dnp; 2943 2944 ASSERT(major != DDI_MAJOR_T_NONE); 2945 2946 /* 2947 * Remove from orphan list 2948 */ 2949 if (ndi_dev_is_persistent_node(dip)) { 2950 dnp = &orphanlist; 2951 remove_from_dn_list(dnp, dip); 2952 } 2953 2954 /* 2955 * Add to per driver list 2956 */ 2957 dnp = &devnamesp[major]; 2958 add_to_dn_list(dnp, dip); 2959 } 2960 2961 static void 2962 unlink_from_driver_list(dev_info_t *dip) 2963 { 2964 major_t major = DEVI(dip)->devi_major; 2965 struct devnames *dnp; 2966 2967 ASSERT(major != DDI_MAJOR_T_NONE); 2968 2969 /* 2970 * Remove from per-driver list 2971 */ 2972 dnp = &devnamesp[major]; 2973 remove_from_dn_list(dnp, dip); 2974 2975 /* 2976 * Add to orphan list 2977 */ 2978 if (ndi_dev_is_persistent_node(dip)) { 2979 dnp = &orphanlist; 2980 add_to_dn_list(dnp, dip); 2981 } 2982 } 2983 2984 /* 2985 * scan the per-driver list looking for dev_info "dip" 2986 */ 2987 static dev_info_t * 2988 in_dn_list(struct devnames *dnp, dev_info_t *dip) 2989 { 2990 struct dev_info *idevi; 2991 2992 if ((idevi = DEVI(dnp->dn_head)) == NULL) 2993 return (NULL); 2994 2995 while (idevi) { 2996 if (idevi == DEVI(dip)) 2997 return (dip); 2998 idevi = idevi->devi_next; 2999 } 3000 return (NULL); 3001 } 3002 3003 /* 3004 * insert devinfo node 'dip' into the per-driver instance list 3005 * headed by 'dnp' 3006 * 3007 * Nodes on the per-driver list are ordered: HW - SID - PSEUDO. The order is 3008 * required for merging of .conf file data to work properly. 3009 */ 3010 static void 3011 add_to_ordered_dn_list(struct devnames *dnp, dev_info_t *dip) 3012 { 3013 dev_info_t **dipp; 3014 3015 ASSERT(mutex_owned(&(dnp->dn_lock))); 3016 3017 dipp = &dnp->dn_head; 3018 if (ndi_dev_is_prom_node(dip)) { 3019 /* 3020 * Find the first non-prom node or end of list 3021 */ 3022 while (*dipp && (ndi_dev_is_prom_node(*dipp) != 0)) { 3023 dipp = (dev_info_t **)&DEVI(*dipp)->devi_next; 3024 } 3025 } else if (ndi_dev_is_persistent_node(dip)) { 3026 /* 3027 * Find the first non-persistent node 3028 */ 3029 while (*dipp && (ndi_dev_is_persistent_node(*dipp) != 0)) { 3030 dipp = (dev_info_t **)&DEVI(*dipp)->devi_next; 3031 } 3032 } else { 3033 /* 3034 * Find the end of the list 3035 */ 3036 while (*dipp) { 3037 dipp = (dev_info_t **)&DEVI(*dipp)->devi_next; 3038 } 3039 } 3040 3041 DEVI(dip)->devi_next = DEVI(*dipp); 3042 *dipp = dip; 3043 } 3044 3045 /* 3046 * add a list of device nodes to the device node list in the 3047 * devnames structure 3048 */ 3049 static void 3050 add_to_dn_list(struct devnames *dnp, dev_info_t *dip) 3051 { 3052 /* 3053 * Look to see if node already exists 3054 */ 3055 LOCK_DEV_OPS(&(dnp->dn_lock)); 3056 if (in_dn_list(dnp, dip)) { 3057 cmn_err(CE_NOTE, "add_to_dn_list: node %s already in list", 3058 DEVI(dip)->devi_node_name); 3059 } else { 3060 add_to_ordered_dn_list(dnp, dip); 3061 } 3062 UNLOCK_DEV_OPS(&(dnp->dn_lock)); 3063 } 3064 3065 static void 3066 remove_from_dn_list(struct devnames *dnp, dev_info_t *dip) 3067 { 3068 dev_info_t **plist; 3069 3070 LOCK_DEV_OPS(&(dnp->dn_lock)); 3071 3072 plist = (dev_info_t **)&dnp->dn_head; 3073 while (*plist && (*plist != dip)) { 3074 plist = (dev_info_t **)&DEVI(*plist)->devi_next; 3075 } 3076 3077 if (*plist != NULL) { 3078 ASSERT(*plist == dip); 3079 *plist = (dev_info_t *)(DEVI(dip)->devi_next); 3080 DEVI(dip)->devi_next = NULL; 3081 } else { 3082 NDI_CONFIG_DEBUG((CE_NOTE, 3083 "remove_from_dn_list: node %s not found in list", 3084 DEVI(dip)->devi_node_name)); 3085 } 3086 3087 UNLOCK_DEV_OPS(&(dnp->dn_lock)); 3088 } 3089 3090 /* 3091 * Add and remove reference driver global property list 3092 */ 3093 static void 3094 add_global_props(dev_info_t *dip) 3095 { 3096 struct devnames *dnp; 3097 ddi_prop_list_t *plist; 3098 3099 ASSERT(DEVI(dip)->devi_global_prop_list == NULL); 3100 ASSERT(DEVI(dip)->devi_major != DDI_MAJOR_T_NONE); 3101 3102 dnp = &devnamesp[DEVI(dip)->devi_major]; 3103 LOCK_DEV_OPS(&dnp->dn_lock); 3104 plist = dnp->dn_global_prop_ptr; 3105 if (plist == NULL) { 3106 UNLOCK_DEV_OPS(&dnp->dn_lock); 3107 return; 3108 } 3109 i_ddi_prop_list_hold(plist, dnp); 3110 UNLOCK_DEV_OPS(&dnp->dn_lock); 3111 3112 mutex_enter(&DEVI(dip)->devi_lock); 3113 DEVI(dip)->devi_global_prop_list = plist; 3114 mutex_exit(&DEVI(dip)->devi_lock); 3115 } 3116 3117 static void 3118 remove_global_props(dev_info_t *dip) 3119 { 3120 ddi_prop_list_t *proplist; 3121 3122 mutex_enter(&DEVI(dip)->devi_lock); 3123 proplist = DEVI(dip)->devi_global_prop_list; 3124 DEVI(dip)->devi_global_prop_list = NULL; 3125 mutex_exit(&DEVI(dip)->devi_lock); 3126 3127 if (proplist) { 3128 major_t major; 3129 struct devnames *dnp; 3130 3131 major = ddi_driver_major(dip); 3132 ASSERT(major != DDI_MAJOR_T_NONE); 3133 dnp = &devnamesp[major]; 3134 LOCK_DEV_OPS(&dnp->dn_lock); 3135 i_ddi_prop_list_rele(proplist, dnp); 3136 UNLOCK_DEV_OPS(&dnp->dn_lock); 3137 } 3138 } 3139 3140 #ifdef DEBUG 3141 /* 3142 * Set this variable to '0' to disable the optimization, 3143 * and to 2 to print debug message. 3144 */ 3145 static int optimize_dtree = 1; 3146 3147 static void 3148 debug_dtree(dev_info_t *devi, struct dev_info *adevi, char *service) 3149 { 3150 char *adeviname, *buf; 3151 3152 /* 3153 * Don't print unless optimize dtree is set to 2+ 3154 */ 3155 if (optimize_dtree <= 1) 3156 return; 3157 3158 buf = kmem_alloc(MAXNAMELEN, KM_SLEEP); 3159 adeviname = ddi_deviname((dev_info_t *)adevi, buf); 3160 if (*adeviname == '\0') 3161 adeviname = "root"; 3162 3163 cmn_err(CE_CONT, "%s %s -> %s\n", 3164 ddi_deviname(devi, buf), service, adeviname); 3165 3166 kmem_free(buf, MAXNAMELEN); 3167 } 3168 #else /* DEBUG */ 3169 #define debug_dtree(a1, a2, a3) /* nothing */ 3170 #endif /* DEBUG */ 3171 3172 static void 3173 ddi_optimize_dtree(dev_info_t *devi) 3174 { 3175 struct dev_info *pdevi; 3176 struct bus_ops *b; 3177 3178 pdevi = DEVI(devi)->devi_parent; 3179 ASSERT(pdevi); 3180 3181 /* 3182 * Set the unoptimized values 3183 */ 3184 DEVI(devi)->devi_bus_map_fault = pdevi; 3185 DEVI(devi)->devi_bus_dma_map = pdevi; 3186 DEVI(devi)->devi_bus_dma_allochdl = pdevi; 3187 DEVI(devi)->devi_bus_dma_freehdl = pdevi; 3188 DEVI(devi)->devi_bus_dma_bindhdl = pdevi; 3189 DEVI(devi)->devi_bus_dma_bindfunc = 3190 pdevi->devi_ops->devo_bus_ops->bus_dma_bindhdl; 3191 DEVI(devi)->devi_bus_dma_unbindhdl = pdevi; 3192 DEVI(devi)->devi_bus_dma_unbindfunc = 3193 pdevi->devi_ops->devo_bus_ops->bus_dma_unbindhdl; 3194 DEVI(devi)->devi_bus_dma_flush = pdevi; 3195 DEVI(devi)->devi_bus_dma_win = pdevi; 3196 DEVI(devi)->devi_bus_dma_ctl = pdevi; 3197 DEVI(devi)->devi_bus_ctl = pdevi; 3198 3199 #ifdef DEBUG 3200 if (optimize_dtree == 0) 3201 return; 3202 #endif /* DEBUG */ 3203 3204 b = pdevi->devi_ops->devo_bus_ops; 3205 3206 if (i_ddi_map_fault == b->bus_map_fault) { 3207 DEVI(devi)->devi_bus_map_fault = pdevi->devi_bus_map_fault; 3208 debug_dtree(devi, DEVI(devi)->devi_bus_map_fault, 3209 "bus_map_fault"); 3210 } 3211 3212 if (ddi_dma_map == b->bus_dma_map) { 3213 DEVI(devi)->devi_bus_dma_map = pdevi->devi_bus_dma_map; 3214 debug_dtree(devi, DEVI(devi)->devi_bus_dma_map, "bus_dma_map"); 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 /* quiesce() should never fail */ 4185 ASSERT(rc == DDI_SUCCESS); 4186 4187 if (rc != DDI_SUCCESS && should_quiesce) { 4188 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 if (i_ddi_detachchild(dip, flags) != DDI_SUCCESS) { 5898 if (flags & NDI_DEVI_OFFLINE) { 5899 RIO_DEBUG((CE_NOTE, "devi_detach_node: offline failed." 5900 " Calling e_ddi_offline_finalize with result=%d. " 5901 "dip=%p", DDI_FAILURE, (void *)dip)); 5902 e_ddi_offline_finalize(dip, DDI_FAILURE); 5903 } 5904 return (NDI_FAILURE); 5905 } 5906 5907 if (flags & NDI_DEVI_OFFLINE) { 5908 RIO_DEBUG((CE_NOTE, "devi_detach_node: offline succeeded." 5909 " Calling e_ddi_offline_finalize with result=%d, " 5910 "dip=%p", DDI_SUCCESS, (void *)dip)); 5911 e_ddi_offline_finalize(dip, DDI_SUCCESS); 5912 } 5913 5914 if (flags & NDI_AUTODETACH) 5915 return (NDI_SUCCESS); 5916 5917 /* 5918 * For DR, even bound nodes may need to have offline 5919 * flag set. 5920 */ 5921 if (flags & NDI_DEVI_OFFLINE) { 5922 mutex_enter(&(DEVI(dip)->devi_lock)); 5923 DEVI_SET_DEVICE_OFFLINE(dip); 5924 mutex_exit(&(DEVI(dip)->devi_lock)); 5925 } 5926 5927 if (i_ddi_node_state(dip) == DS_INITIALIZED) { 5928 struct dev_info *devi = DEVI(dip); 5929 5930 if (devi->devi_ev_path == NULL) { 5931 devi->devi_ev_path = kmem_alloc(MAXPATHLEN, KM_SLEEP); 5932 (void) ddi_pathname(dip, devi->devi_ev_path); 5933 } 5934 if (flags & NDI_DEVI_OFFLINE) 5935 i_ndi_devi_report_status_change(dip, 5936 devi->devi_ev_path); 5937 5938 if (need_remove_event(dip, flags)) { 5939 /* 5940 * instance and path data are lost in call to 5941 * ddi_uninitchild 5942 */ 5943 devi->devi_ev_instance = ddi_get_instance(dip); 5944 5945 mutex_enter(&(DEVI(dip)->devi_lock)); 5946 DEVI_SET_EVREMOVE(dip); 5947 mutex_exit(&(DEVI(dip)->devi_lock)); 5948 } 5949 } 5950 5951 if (flags & (NDI_UNCONFIG | NDI_DEVI_REMOVE)) { 5952 ret = ddi_uninitchild(dip); 5953 if (ret == NDI_SUCCESS) { 5954 /* 5955 * Remove uninitialized pseudo nodes because 5956 * system props are lost and the node cannot be 5957 * reattached. 5958 */ 5959 if (!ndi_dev_is_persistent_node(dip)) 5960 flags |= NDI_DEVI_REMOVE; 5961 5962 if (flags & NDI_DEVI_REMOVE) { 5963 /* 5964 * NOTE: If there is a consumer of LDI events, 5965 * ddi_uninitchild above would have failed 5966 * because of active devi_ref from ldi_open(). 5967 */ 5968 5969 if (DEVI_EVREMOVE(dip)) { 5970 path = i_ddi_strdup( 5971 DEVI(dip)->devi_ev_path, 5972 KM_SLEEP); 5973 class = 5974 i_ddi_strdup(i_ddi_devi_class(dip), 5975 KM_SLEEP); 5976 driver = 5977 i_ddi_strdup( 5978 (char *)ddi_driver_name(dip), 5979 KM_SLEEP); 5980 instance = DEVI(dip)->devi_ev_instance; 5981 post_event = 1; 5982 } 5983 5984 ret = ddi_remove_child(dip, 0); 5985 if (post_event && ret == NDI_SUCCESS) { 5986 /* Generate EC_DEVFS_DEVI_REMOVE */ 5987 (void) i_log_devfs_remove_devinfo(path, 5988 class, driver, instance, flags); 5989 } 5990 } 5991 5992 } 5993 } 5994 5995 if (path) 5996 strfree(path); 5997 if (class) 5998 strfree(class); 5999 if (driver) 6000 strfree(driver); 6001 6002 return (ret); 6003 } 6004 6005 /* 6006 * unconfigure immediate children of bus nexus device 6007 */ 6008 static int 6009 unconfig_immediate_children( 6010 dev_info_t *dip, 6011 dev_info_t **dipp, 6012 int flags, 6013 major_t major) 6014 { 6015 int rv = NDI_SUCCESS; 6016 int circ, vcirc; 6017 dev_info_t *child; 6018 dev_info_t *vdip = NULL; 6019 dev_info_t *next; 6020 6021 ASSERT(dipp == NULL || *dipp == NULL); 6022 6023 /* 6024 * Scan forward to see if we will be processing a pHCI child. If we 6025 * have a child that is a pHCI and vHCI and pHCI are not siblings then 6026 * enter vHCI before parent(pHCI) to prevent deadlock with mpxio 6027 * Client power management operations. 6028 */ 6029 ndi_devi_enter(dip, &circ); 6030 for (child = ddi_get_child(dip); child; 6031 child = ddi_get_next_sibling(child)) { 6032 /* skip same nodes we skip below */ 6033 if (((major != DDI_MAJOR_T_NONE) && 6034 (major != ddi_driver_major(child))) || 6035 ((flags & NDI_AUTODETACH) && !is_leaf_node(child))) 6036 continue; 6037 6038 if (MDI_PHCI(child)) { 6039 vdip = mdi_devi_get_vdip(child); 6040 /* 6041 * If vHCI and vHCI is not a sibling of pHCI 6042 * then enter in (vHCI, parent(pHCI)) order. 6043 */ 6044 if (vdip && (ddi_get_parent(vdip) != dip)) { 6045 ndi_devi_exit(dip, circ); 6046 6047 /* use mdi_devi_enter ordering */ 6048 ndi_devi_enter(vdip, &vcirc); 6049 ndi_devi_enter(dip, &circ); 6050 break; 6051 } else 6052 vdip = NULL; 6053 } 6054 } 6055 6056 child = ddi_get_child(dip); 6057 while (child) { 6058 next = ddi_get_next_sibling(child); 6059 6060 if ((major != DDI_MAJOR_T_NONE) && 6061 (major != ddi_driver_major(child))) { 6062 child = next; 6063 continue; 6064 } 6065 6066 /* skip nexus nodes during autodetach */ 6067 if ((flags & NDI_AUTODETACH) && !is_leaf_node(child)) { 6068 child = next; 6069 continue; 6070 } 6071 6072 if (devi_detach_node(child, flags) != NDI_SUCCESS) { 6073 if (dipp && *dipp == NULL) { 6074 ndi_hold_devi(child); 6075 *dipp = child; 6076 } 6077 rv = NDI_FAILURE; 6078 } 6079 6080 /* 6081 * Continue upon failure--best effort algorithm 6082 */ 6083 child = next; 6084 } 6085 6086 ndi_devi_exit(dip, circ); 6087 if (vdip) 6088 ndi_devi_exit(vdip, vcirc); 6089 6090 return (rv); 6091 } 6092 6093 /* 6094 * unconfigure grand children of bus nexus device 6095 */ 6096 static int 6097 unconfig_grand_children( 6098 dev_info_t *dip, 6099 dev_info_t **dipp, 6100 int flags, 6101 major_t major, 6102 struct brevq_node **brevqp) 6103 { 6104 struct mt_config_handle *hdl; 6105 6106 if (brevqp) 6107 *brevqp = NULL; 6108 6109 /* multi-threaded configuration of child nexus */ 6110 hdl = mt_config_init(dip, dipp, flags, major, MT_UNCONFIG_OP, brevqp); 6111 mt_config_children(hdl); 6112 6113 return (mt_config_fini(hdl)); /* wait for threads to exit */ 6114 } 6115 6116 /* 6117 * Unconfigure children/descendants of the dip. 6118 * 6119 * If brevqp is not NULL, on return *brevqp is set to a queue of dip's 6120 * child devinames for which branch remove events need to be generated. 6121 */ 6122 static int 6123 devi_unconfig_common( 6124 dev_info_t *dip, 6125 dev_info_t **dipp, 6126 int flags, 6127 major_t major, 6128 struct brevq_node **brevqp) 6129 { 6130 int rv; 6131 int pm_cookie; 6132 int (*f)(); 6133 ddi_bus_config_op_t bus_op; 6134 6135 if (dipp) 6136 *dipp = NULL; 6137 if (brevqp) 6138 *brevqp = NULL; 6139 6140 /* 6141 * Power up the dip if it is powered off. If the flag bit 6142 * NDI_AUTODETACH is set and the dip is not at its full power, 6143 * skip the rest of the branch. 6144 */ 6145 if (pm_pre_unconfig(dip, flags, &pm_cookie, NULL) != DDI_SUCCESS) 6146 return ((flags & NDI_AUTODETACH) ? NDI_SUCCESS : 6147 NDI_FAILURE); 6148 6149 /* 6150 * Some callers, notably SCSI, need to clear out the devfs 6151 * cache together with the unconfig to prevent stale entries. 6152 */ 6153 if (flags & NDI_DEVFS_CLEAN) 6154 (void) devfs_clean(dip, NULL, 0); 6155 6156 rv = unconfig_grand_children(dip, dipp, flags, major, brevqp); 6157 6158 if ((rv != NDI_SUCCESS) && ((flags & NDI_AUTODETACH) == 0)) { 6159 if (brevqp && *brevqp) { 6160 log_and_free_br_events_on_grand_children(dip, *brevqp); 6161 free_brevq(*brevqp); 6162 *brevqp = NULL; 6163 } 6164 pm_post_unconfig(dip, pm_cookie, NULL); 6165 return (rv); 6166 } 6167 6168 if (dipp && *dipp) { 6169 ndi_rele_devi(*dipp); 6170 *dipp = NULL; 6171 } 6172 6173 /* 6174 * It is possible to have a detached nexus with children 6175 * and grandchildren (for example: a branch consisting 6176 * entirely of bound nodes.) Since the nexus is detached 6177 * the bus_unconfig entry point cannot be used to remove 6178 * or unconfigure the descendants. 6179 */ 6180 if (!i_ddi_devi_attached(dip) || 6181 (DEVI(dip)->devi_ops->devo_bus_ops == NULL) || 6182 (DEVI(dip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) || 6183 (f = DEVI(dip)->devi_ops->devo_bus_ops->bus_unconfig) == NULL) { 6184 rv = unconfig_immediate_children(dip, dipp, flags, major); 6185 } else { 6186 /* 6187 * call bus_unconfig entry point 6188 * It should reset nexus flags if unconfigure succeeds. 6189 */ 6190 bus_op = (major == DDI_MAJOR_T_NONE) ? 6191 BUS_UNCONFIG_ALL : BUS_UNCONFIG_DRIVER; 6192 rv = (*f)(dip, flags, bus_op, (void *)(uintptr_t)major); 6193 } 6194 6195 pm_post_unconfig(dip, pm_cookie, NULL); 6196 6197 if (brevqp && *brevqp) 6198 cleanup_br_events_on_grand_children(dip, brevqp); 6199 6200 return (rv); 6201 } 6202 6203 /* 6204 * called by devfs/framework to unconfigure children bound to major 6205 * If NDI_AUTODETACH is specified, this is invoked by either the 6206 * moduninstall daemon or the modunload -i 0 command. 6207 */ 6208 int 6209 ndi_devi_unconfig_driver(dev_info_t *dip, int flags, major_t major) 6210 { 6211 NDI_CONFIG_DEBUG((CE_CONT, 6212 "ndi_devi_unconfig_driver: par = %s%d (%p), flags = 0x%x\n", 6213 ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags)); 6214 6215 return (devi_unconfig_common(dip, NULL, flags, major, NULL)); 6216 } 6217 6218 int 6219 ndi_devi_unconfig(dev_info_t *dip, int flags) 6220 { 6221 NDI_CONFIG_DEBUG((CE_CONT, 6222 "ndi_devi_unconfig: par = %s%d (%p), flags = 0x%x\n", 6223 ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags)); 6224 6225 return (devi_unconfig_common(dip, NULL, flags, DDI_MAJOR_T_NONE, NULL)); 6226 } 6227 6228 int 6229 e_ddi_devi_unconfig(dev_info_t *dip, dev_info_t **dipp, int flags) 6230 { 6231 NDI_CONFIG_DEBUG((CE_CONT, 6232 "e_ddi_devi_unconfig: par = %s%d (%p), flags = 0x%x\n", 6233 ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags)); 6234 6235 return (devi_unconfig_common(dip, dipp, flags, DDI_MAJOR_T_NONE, NULL)); 6236 } 6237 6238 /* 6239 * Unconfigure child by name 6240 */ 6241 static int 6242 devi_unconfig_one(dev_info_t *pdip, char *devnm, int flags) 6243 { 6244 int rv, circ; 6245 dev_info_t *child; 6246 dev_info_t *vdip = NULL; 6247 int v_circ; 6248 6249 ndi_devi_enter(pdip, &circ); 6250 child = ndi_devi_findchild(pdip, devnm); 6251 6252 /* 6253 * If child is pHCI and vHCI and pHCI are not siblings then enter vHCI 6254 * before parent(pHCI) to avoid deadlock with mpxio Client power 6255 * management operations. 6256 */ 6257 if (child && MDI_PHCI(child)) { 6258 vdip = mdi_devi_get_vdip(child); 6259 if (vdip && (ddi_get_parent(vdip) != pdip)) { 6260 ndi_devi_exit(pdip, circ); 6261 6262 /* use mdi_devi_enter ordering */ 6263 ndi_devi_enter(vdip, &v_circ); 6264 ndi_devi_enter(pdip, &circ); 6265 child = ndi_devi_findchild(pdip, devnm); 6266 } else 6267 vdip = NULL; 6268 } 6269 6270 if (child) { 6271 rv = devi_detach_node(child, flags); 6272 } else { 6273 NDI_CONFIG_DEBUG((CE_CONT, 6274 "devi_unconfig_one: %s not found\n", devnm)); 6275 rv = NDI_SUCCESS; 6276 } 6277 6278 ndi_devi_exit(pdip, circ); 6279 if (vdip) 6280 ndi_devi_exit(vdip, v_circ); 6281 6282 return (rv); 6283 } 6284 6285 int 6286 ndi_devi_unconfig_one( 6287 dev_info_t *pdip, 6288 char *devnm, 6289 dev_info_t **dipp, 6290 int flags) 6291 { 6292 int (*f)(); 6293 int circ, rv; 6294 int pm_cookie; 6295 dev_info_t *child; 6296 dev_info_t *vdip = NULL; 6297 int v_circ; 6298 struct brevq_node *brevq = NULL; 6299 6300 ASSERT(i_ddi_devi_attached(pdip)); 6301 6302 NDI_CONFIG_DEBUG((CE_CONT, 6303 "ndi_devi_unconfig_one: par = %s%d (%p), child = %s\n", 6304 ddi_driver_name(pdip), ddi_get_instance(pdip), 6305 (void *)pdip, devnm)); 6306 6307 if (pm_pre_unconfig(pdip, flags, &pm_cookie, devnm) != DDI_SUCCESS) 6308 return (NDI_FAILURE); 6309 6310 if (dipp) 6311 *dipp = NULL; 6312 6313 ndi_devi_enter(pdip, &circ); 6314 child = ndi_devi_findchild(pdip, devnm); 6315 6316 /* 6317 * If child is pHCI and vHCI and pHCI are not siblings then enter vHCI 6318 * before parent(pHCI) to avoid deadlock with mpxio Client power 6319 * management operations. 6320 */ 6321 if (child && MDI_PHCI(child)) { 6322 vdip = mdi_devi_get_vdip(child); 6323 if (vdip && (ddi_get_parent(vdip) != pdip)) { 6324 ndi_devi_exit(pdip, circ); 6325 6326 /* use mdi_devi_enter ordering */ 6327 ndi_devi_enter(vdip, &v_circ); 6328 ndi_devi_enter(pdip, &circ); 6329 child = ndi_devi_findchild(pdip, devnm); 6330 } else 6331 vdip = NULL; 6332 } 6333 6334 if (child == NULL) { 6335 NDI_CONFIG_DEBUG((CE_CONT, "ndi_devi_unconfig_one: %s" 6336 " not found\n", devnm)); 6337 rv = NDI_SUCCESS; 6338 goto out; 6339 } 6340 6341 /* 6342 * Unconfigure children/descendants of named child 6343 */ 6344 rv = devi_unconfig_branch(child, dipp, flags | NDI_UNCONFIG, &brevq); 6345 if (rv != NDI_SUCCESS) 6346 goto out; 6347 6348 init_bound_node_ev(pdip, child, flags); 6349 6350 if ((DEVI(pdip)->devi_ops->devo_bus_ops == NULL) || 6351 (DEVI(pdip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) || 6352 (f = DEVI(pdip)->devi_ops->devo_bus_ops->bus_unconfig) == NULL) { 6353 rv = devi_detach_node(child, flags); 6354 } else { 6355 /* call bus_config entry point */ 6356 rv = (*f)(pdip, flags, BUS_UNCONFIG_ONE, (void *)devnm); 6357 } 6358 6359 if (brevq) { 6360 if (rv != NDI_SUCCESS) 6361 log_and_free_brevq_dip(child, brevq); 6362 else 6363 free_brevq(brevq); 6364 } 6365 6366 if (dipp && rv != NDI_SUCCESS) { 6367 ndi_hold_devi(child); 6368 ASSERT(*dipp == NULL); 6369 *dipp = child; 6370 } 6371 6372 out: 6373 ndi_devi_exit(pdip, circ); 6374 if (vdip) 6375 ndi_devi_exit(vdip, v_circ); 6376 6377 pm_post_unconfig(pdip, pm_cookie, devnm); 6378 6379 return (rv); 6380 } 6381 6382 struct async_arg { 6383 dev_info_t *dip; 6384 uint_t flags; 6385 }; 6386 6387 /* 6388 * Common async handler for: 6389 * ndi_devi_bind_driver_async 6390 * ndi_devi_online_async 6391 */ 6392 static int 6393 i_ndi_devi_async_common(dev_info_t *dip, uint_t flags, void (*func)()) 6394 { 6395 int tqflag; 6396 int kmflag; 6397 struct async_arg *arg; 6398 dev_info_t *pdip = ddi_get_parent(dip); 6399 6400 ASSERT(pdip); 6401 ASSERT(DEVI(pdip)->devi_taskq); 6402 ASSERT(ndi_dev_is_persistent_node(dip)); 6403 6404 if (flags & NDI_NOSLEEP) { 6405 kmflag = KM_NOSLEEP; 6406 tqflag = TQ_NOSLEEP; 6407 } else { 6408 kmflag = KM_SLEEP; 6409 tqflag = TQ_SLEEP; 6410 } 6411 6412 arg = kmem_alloc(sizeof (*arg), kmflag); 6413 if (arg == NULL) 6414 goto fail; 6415 6416 arg->flags = flags; 6417 arg->dip = dip; 6418 if (ddi_taskq_dispatch(DEVI(pdip)->devi_taskq, func, arg, tqflag) == 6419 DDI_SUCCESS) { 6420 return (NDI_SUCCESS); 6421 } 6422 6423 fail: 6424 NDI_CONFIG_DEBUG((CE_CONT, "%s%d: ddi_taskq_dispatch failed", 6425 ddi_driver_name(pdip), ddi_get_instance(pdip))); 6426 6427 if (arg) 6428 kmem_free(arg, sizeof (*arg)); 6429 return (NDI_FAILURE); 6430 } 6431 6432 static void 6433 i_ndi_devi_bind_driver_cb(struct async_arg *arg) 6434 { 6435 (void) ndi_devi_bind_driver(arg->dip, arg->flags); 6436 kmem_free(arg, sizeof (*arg)); 6437 } 6438 6439 int 6440 ndi_devi_bind_driver_async(dev_info_t *dip, uint_t flags) 6441 { 6442 return (i_ndi_devi_async_common(dip, flags, 6443 (void (*)())i_ndi_devi_bind_driver_cb)); 6444 } 6445 6446 /* 6447 * place the devinfo in the ONLINE state. 6448 */ 6449 int 6450 ndi_devi_online(dev_info_t *dip, uint_t flags) 6451 { 6452 int circ, rv; 6453 dev_info_t *pdip = ddi_get_parent(dip); 6454 int branch_event = 0; 6455 6456 ASSERT(pdip); 6457 6458 NDI_CONFIG_DEBUG((CE_CONT, "ndi_devi_online: %s%d (%p)\n", 6459 ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip)); 6460 6461 ndi_devi_enter(pdip, &circ); 6462 /* bind child before merging .conf nodes */ 6463 rv = i_ndi_config_node(dip, DS_BOUND, flags); 6464 if (rv != NDI_SUCCESS) { 6465 ndi_devi_exit(pdip, circ); 6466 return (rv); 6467 } 6468 6469 /* merge .conf properties */ 6470 (void) i_ndi_make_spec_children(pdip, flags); 6471 6472 flags |= (NDI_DEVI_ONLINE | NDI_CONFIG); 6473 6474 if (flags & NDI_NO_EVENT) { 6475 /* 6476 * Caller is specifically asking for not to generate an event. 6477 * Set the following flag so that devi_attach_node() don't 6478 * change the event state. 6479 */ 6480 flags |= NDI_NO_EVENT_STATE_CHNG; 6481 } 6482 6483 if ((flags & (NDI_NO_EVENT | NDI_BRANCH_EVENT_OP)) == 0 && 6484 ((flags & NDI_CONFIG) || DEVI_NEED_NDI_CONFIG(dip))) { 6485 flags |= NDI_BRANCH_EVENT_OP; 6486 branch_event = 1; 6487 } 6488 6489 /* 6490 * devi_attach_node() may remove dip on failure 6491 */ 6492 if ((rv = devi_attach_node(dip, flags)) == NDI_SUCCESS) { 6493 if ((flags & NDI_CONFIG) || DEVI_NEED_NDI_CONFIG(dip)) { 6494 /* 6495 * Hold the attached dip, and exit the parent while 6496 * we drive configuration of children below the 6497 * attached dip. 6498 */ 6499 ndi_hold_devi(dip); 6500 ndi_devi_exit(pdip, circ); 6501 6502 (void) ndi_devi_config(dip, flags); 6503 6504 ndi_devi_enter(pdip, &circ); 6505 ndi_rele_devi(dip); 6506 } 6507 6508 if (branch_event) 6509 (void) i_log_devfs_branch_add(dip); 6510 } 6511 6512 ndi_devi_exit(pdip, circ); 6513 6514 /* 6515 * Notify devfs that we have a new node. Devfs needs to invalidate 6516 * cached directory contents. 6517 * 6518 * For PCMCIA devices, it is possible the pdip is not fully 6519 * attached. In this case, calling back into devfs will 6520 * result in a loop or assertion error. Hence, the check 6521 * on node state. 6522 * 6523 * If we own parent lock, this is part of a branch operation. 6524 * We skip the devfs_clean() step because the cache invalidation 6525 * is done higher up in the device tree. 6526 */ 6527 if (rv == NDI_SUCCESS && i_ddi_devi_attached(pdip) && 6528 !DEVI_BUSY_OWNED(pdip)) 6529 (void) devfs_clean(pdip, NULL, 0); 6530 return (rv); 6531 } 6532 6533 static void 6534 i_ndi_devi_online_cb(struct async_arg *arg) 6535 { 6536 (void) ndi_devi_online(arg->dip, arg->flags); 6537 kmem_free(arg, sizeof (*arg)); 6538 } 6539 6540 int 6541 ndi_devi_online_async(dev_info_t *dip, uint_t flags) 6542 { 6543 /* mark child as need config if requested. */ 6544 if (flags & NDI_CONFIG) { 6545 mutex_enter(&(DEVI(dip)->devi_lock)); 6546 DEVI_SET_NDI_CONFIG(dip); 6547 mutex_exit(&(DEVI(dip)->devi_lock)); 6548 } 6549 6550 return (i_ndi_devi_async_common(dip, flags, 6551 (void (*)())i_ndi_devi_online_cb)); 6552 } 6553 6554 /* 6555 * Take a device node Offline 6556 * To take a device Offline means to detach the device instance from 6557 * the driver and prevent devfs requests from re-attaching the device 6558 * instance. 6559 * 6560 * The flag NDI_DEVI_REMOVE causes removes the device node from 6561 * the driver list and the device tree. In this case, the device 6562 * is assumed to be removed from the system. 6563 */ 6564 int 6565 ndi_devi_offline(dev_info_t *dip, uint_t flags) 6566 { 6567 int circ, rval = 0; 6568 dev_info_t *pdip = ddi_get_parent(dip); 6569 dev_info_t *vdip = NULL; 6570 int v_circ; 6571 struct brevq_node *brevq = NULL; 6572 6573 ASSERT(pdip); 6574 6575 flags |= NDI_DEVI_OFFLINE; 6576 6577 /* 6578 * If child is pHCI and vHCI and pHCI are not siblings then enter vHCI 6579 * before parent(pHCI) to avoid deadlock with mpxio Client power 6580 * management operations. 6581 */ 6582 if (MDI_PHCI(dip)) { 6583 vdip = mdi_devi_get_vdip(dip); 6584 if (vdip && (ddi_get_parent(vdip) != pdip)) 6585 ndi_devi_enter(vdip, &v_circ); 6586 else 6587 vdip = NULL; 6588 } 6589 ndi_devi_enter(pdip, &circ); 6590 6591 if (i_ddi_devi_attached(dip)) { 6592 /* 6593 * If dip is in DS_READY state, there may be cached dv_nodes 6594 * referencing this dip, so we invoke devfs code path. 6595 * Note that we must release busy changing on pdip to 6596 * avoid deadlock against devfs. 6597 */ 6598 char *devname = kmem_alloc(MAXNAMELEN + 1, KM_SLEEP); 6599 (void) ddi_deviname(dip, devname); 6600 6601 ndi_devi_exit(pdip, circ); 6602 if (vdip) 6603 ndi_devi_exit(vdip, v_circ); 6604 6605 /* 6606 * If we are explictly told to clean, then clean. If we own the 6607 * parent lock then this is part of a branch operation, and we 6608 * skip the devfs_clean() step. 6609 * 6610 * NOTE: A thread performing a devfs file system lookup/ 6611 * bus_config can't call devfs_clean to unconfig without 6612 * causing rwlock problems in devfs. For ndi_devi_offline, this 6613 * means that the NDI_DEVFS_CLEAN flag is safe from ioctl code 6614 * or from an async hotplug thread, but is not safe from a 6615 * nexus driver's bus_config implementation. 6616 */ 6617 if ((flags & NDI_DEVFS_CLEAN) || 6618 (!DEVI_BUSY_OWNED(pdip))) 6619 (void) devfs_clean(pdip, devname + 1, DV_CLEAN_FORCE); 6620 6621 kmem_free(devname, MAXNAMELEN + 1); 6622 6623 rval = devi_unconfig_branch(dip, NULL, flags|NDI_UNCONFIG, 6624 &brevq); 6625 6626 if (rval) 6627 return (NDI_FAILURE); 6628 6629 if (vdip) 6630 ndi_devi_enter(vdip, &v_circ); 6631 ndi_devi_enter(pdip, &circ); 6632 } 6633 6634 init_bound_node_ev(pdip, dip, flags); 6635 6636 rval = devi_detach_node(dip, flags); 6637 if (brevq) { 6638 if (rval != NDI_SUCCESS) 6639 log_and_free_brevq_dip(dip, brevq); 6640 else 6641 free_brevq(brevq); 6642 } 6643 6644 ndi_devi_exit(pdip, circ); 6645 if (vdip) 6646 ndi_devi_exit(vdip, v_circ); 6647 6648 return (rval); 6649 } 6650 6651 /* 6652 * Find the child dev_info node of parent nexus 'p' whose unit address 6653 * matches "cname@caddr". Recommend use of ndi_devi_findchild() instead. 6654 */ 6655 dev_info_t * 6656 ndi_devi_find(dev_info_t *pdip, char *cname, char *caddr) 6657 { 6658 dev_info_t *child; 6659 int circ; 6660 6661 if (pdip == NULL || cname == NULL || caddr == NULL) 6662 return ((dev_info_t *)NULL); 6663 6664 ndi_devi_enter(pdip, &circ); 6665 child = find_sibling(ddi_get_child(pdip), cname, caddr, 6666 FIND_NODE_BY_NODENAME, NULL); 6667 ndi_devi_exit(pdip, circ); 6668 return (child); 6669 } 6670 6671 /* 6672 * Find the child dev_info node of parent nexus 'p' whose unit address 6673 * matches devname "name@addr". Permits caller to hold the parent. 6674 */ 6675 dev_info_t * 6676 ndi_devi_findchild(dev_info_t *pdip, char *devname) 6677 { 6678 dev_info_t *child; 6679 char *cname, *caddr; 6680 char *devstr; 6681 6682 ASSERT(DEVI_BUSY_OWNED(pdip)); 6683 6684 devstr = i_ddi_strdup(devname, KM_SLEEP); 6685 i_ddi_parse_name(devstr, &cname, &caddr, NULL); 6686 6687 if (cname == NULL || caddr == NULL) { 6688 kmem_free(devstr, strlen(devname)+1); 6689 return ((dev_info_t *)NULL); 6690 } 6691 6692 child = find_sibling(ddi_get_child(pdip), cname, caddr, 6693 FIND_NODE_BY_NODENAME, NULL); 6694 kmem_free(devstr, strlen(devname)+1); 6695 return (child); 6696 } 6697 6698 /* 6699 * Misc. routines called by framework only 6700 */ 6701 6702 /* 6703 * Clear the DEVI_MADE_CHILDREN/DEVI_ATTACHED_CHILDREN flags 6704 * if new child spec has been added. 6705 */ 6706 static int 6707 reset_nexus_flags(dev_info_t *dip, void *arg) 6708 { 6709 struct hwc_spec *list; 6710 int circ; 6711 6712 if (((DEVI(dip)->devi_flags & DEVI_MADE_CHILDREN) == 0) || 6713 ((list = hwc_get_child_spec(dip, (major_t)(uintptr_t)arg)) == NULL)) 6714 return (DDI_WALK_CONTINUE); 6715 6716 hwc_free_spec_list(list); 6717 6718 /* coordinate child state update */ 6719 ndi_devi_enter(dip, &circ); 6720 mutex_enter(&DEVI(dip)->devi_lock); 6721 DEVI(dip)->devi_flags &= ~(DEVI_MADE_CHILDREN | DEVI_ATTACHED_CHILDREN); 6722 mutex_exit(&DEVI(dip)->devi_lock); 6723 ndi_devi_exit(dip, circ); 6724 6725 return (DDI_WALK_CONTINUE); 6726 } 6727 6728 /* 6729 * Helper functions, returns NULL if no memory. 6730 */ 6731 6732 /* 6733 * path_to_major: 6734 * 6735 * Return an alternate driver name binding for the leaf device 6736 * of the given pathname, if there is one. The purpose of this 6737 * function is to deal with generic pathnames. The default action 6738 * for platforms that can't do this (ie: x86 or any platform that 6739 * does not have prom_finddevice functionality, which matches 6740 * nodenames and unit-addresses without the drivers participation) 6741 * is to return DDI_MAJOR_T_NONE. 6742 * 6743 * Used in loadrootmodules() in the swapgeneric module to 6744 * associate a given pathname with a given leaf driver. 6745 * 6746 */ 6747 major_t 6748 path_to_major(char *path) 6749 { 6750 dev_info_t *dip; 6751 char *p, *q; 6752 pnode_t nodeid; 6753 major_t major; 6754 6755 /* check for path-oriented alias */ 6756 major = ddi_name_to_major(path); 6757 if (driver_active(major)) { 6758 NDI_CONFIG_DEBUG((CE_NOTE, "path_to_major: %s path bound %s\n", 6759 path, ddi_major_to_name(major))); 6760 return (major); 6761 } 6762 6763 /* 6764 * Get the nodeid of the given pathname, if such a mapping exists. 6765 */ 6766 dip = NULL; 6767 nodeid = prom_finddevice(path); 6768 if (nodeid != OBP_BADNODE) { 6769 /* 6770 * Find the nodeid in our copy of the device tree and return 6771 * whatever name we used to bind this node to a driver. 6772 */ 6773 dip = e_ddi_nodeid_to_dip(nodeid); 6774 } 6775 6776 if (dip == NULL) { 6777 NDI_CONFIG_DEBUG((CE_WARN, 6778 "path_to_major: can't bind <%s>\n", path)); 6779 return (DDI_MAJOR_T_NONE); 6780 } 6781 6782 /* 6783 * If we're bound to something other than the nodename, 6784 * note that in the message buffer and system log. 6785 */ 6786 p = ddi_binding_name(dip); 6787 q = ddi_node_name(dip); 6788 if (p && q && (strcmp(p, q) != 0)) 6789 NDI_CONFIG_DEBUG((CE_NOTE, "path_to_major: %s bound to %s\n", 6790 path, p)); 6791 6792 major = ddi_name_to_major(p); 6793 6794 ndi_rele_devi(dip); /* release e_ddi_nodeid_to_dip hold */ 6795 6796 return (major); 6797 } 6798 6799 /* 6800 * Return the held dip for the specified major and instance, attempting to do 6801 * an attach if specified. Return NULL if the devi can't be found or put in 6802 * the proper state. The caller must release the hold via ddi_release_devi if 6803 * a non-NULL value is returned. 6804 * 6805 * Some callers expect to be able to perform a hold_devi() while in a context 6806 * where using ndi_devi_enter() to ensure the hold might cause deadlock (see 6807 * open-from-attach code in consconfig_dacf.c). Such special-case callers 6808 * must ensure that an ndi_devi_enter(parent)/ndi_hold_devi() from a safe 6809 * context is already active. The hold_devi() implementation must accommodate 6810 * these callers. 6811 */ 6812 static dev_info_t * 6813 hold_devi(major_t major, int instance, int flags) 6814 { 6815 struct devnames *dnp; 6816 dev_info_t *dip; 6817 char *path; 6818 char *vpath; 6819 6820 if ((major >= devcnt) || (instance == -1)) 6821 return (NULL); 6822 6823 /* try to find the instance in the per driver list */ 6824 dnp = &(devnamesp[major]); 6825 LOCK_DEV_OPS(&(dnp->dn_lock)); 6826 for (dip = dnp->dn_head; dip; 6827 dip = (dev_info_t *)DEVI(dip)->devi_next) { 6828 /* skip node if instance field is not valid */ 6829 if (i_ddi_node_state(dip) < DS_INITIALIZED) 6830 continue; 6831 6832 /* look for instance match */ 6833 if (DEVI(dip)->devi_instance == instance) { 6834 /* 6835 * To accommodate callers that can't block in 6836 * ndi_devi_enter() we do an ndi_hold_devi(), and 6837 * afterwards check that the node is in a state where 6838 * the hold prevents detach(). If we did not manage to 6839 * prevent detach then we ndi_rele_devi() and perform 6840 * the slow path below (which can result in a blocking 6841 * ndi_devi_enter() while driving attach top-down). 6842 * This code depends on the ordering of 6843 * DEVI_SET_DETACHING and the devi_ref check in the 6844 * detach_node() code path. 6845 */ 6846 ndi_hold_devi(dip); 6847 if (i_ddi_devi_attached(dip) && 6848 !DEVI_IS_DETACHING(dip)) { 6849 UNLOCK_DEV_OPS(&(dnp->dn_lock)); 6850 return (dip); /* fast-path with devi held */ 6851 } 6852 ndi_rele_devi(dip); 6853 6854 /* try slow-path */ 6855 dip = NULL; 6856 break; 6857 } 6858 } 6859 ASSERT(dip == NULL); 6860 UNLOCK_DEV_OPS(&(dnp->dn_lock)); 6861 6862 if (flags & E_DDI_HOLD_DEVI_NOATTACH) 6863 return (NULL); /* told not to drive attach */ 6864 6865 /* slow-path may block, so it should not occur from interrupt */ 6866 ASSERT(!servicing_interrupt()); 6867 if (servicing_interrupt()) 6868 return (NULL); 6869 6870 /* reconstruct the path and drive attach by path through devfs. */ 6871 path = kmem_alloc(MAXPATHLEN, KM_SLEEP); 6872 if (e_ddi_majorinstance_to_path(major, instance, path) == 0) { 6873 dip = e_ddi_hold_devi_by_path(path, flags); 6874 6875 /* 6876 * Verify that we got the correct device - a path_to_inst file 6877 * with a bogus/corrupt path (or a nexus that changes its 6878 * unit-address format) could result in an incorrect answer 6879 * 6880 * Verify major, instance, and path. 6881 */ 6882 vpath = kmem_alloc(MAXPATHLEN, KM_SLEEP); 6883 if (dip && 6884 ((DEVI(dip)->devi_major != major) || 6885 ((DEVI(dip)->devi_instance != instance)) || 6886 (strcmp(path, ddi_pathname(dip, vpath)) != 0))) { 6887 ndi_rele_devi(dip); 6888 dip = NULL; /* no answer better than wrong answer */ 6889 } 6890 kmem_free(vpath, MAXPATHLEN); 6891 } 6892 kmem_free(path, MAXPATHLEN); 6893 return (dip); /* with devi held */ 6894 } 6895 6896 /* 6897 * The {e_}ddi_hold_devi{_by_{instance|dev|path}} hold the devinfo node 6898 * associated with the specified arguments. This hold should be released 6899 * by calling ddi_release_devi. 6900 * 6901 * The E_DDI_HOLD_DEVI_NOATTACH flag argument allows the caller to to specify 6902 * a failure return if the node is not already attached. 6903 * 6904 * NOTE: by the time we make e_ddi_hold_devi public, we should be able to reuse 6905 * ddi_hold_devi again. 6906 */ 6907 dev_info_t * 6908 ddi_hold_devi_by_instance(major_t major, int instance, int flags) 6909 { 6910 return (hold_devi(major, instance, flags)); 6911 } 6912 6913 dev_info_t * 6914 e_ddi_hold_devi_by_dev(dev_t dev, int flags) 6915 { 6916 major_t major = getmajor(dev); 6917 dev_info_t *dip; 6918 struct dev_ops *ops; 6919 dev_info_t *ddip = NULL; 6920 6921 dip = hold_devi(major, dev_to_instance(dev), flags); 6922 6923 /* 6924 * The rest of this routine is legacy support for drivers that 6925 * have broken DDI_INFO_DEVT2INSTANCE implementations but may have 6926 * functional DDI_INFO_DEVT2DEVINFO implementations. This code will 6927 * diagnose inconsistency and, for maximum compatibility with legacy 6928 * drivers, give preference to the drivers DDI_INFO_DEVT2DEVINFO 6929 * implementation over the above derived dip based the driver's 6930 * DDI_INFO_DEVT2INSTANCE implementation. This legacy support should 6931 * be removed when DDI_INFO_DEVT2DEVINFO is deprecated. 6932 * 6933 * NOTE: The following code has a race condition. DEVT2DEVINFO 6934 * returns a dip which is not held. By the time we ref ddip, 6935 * it could have been freed. The saving grace is that for 6936 * most drivers, the dip returned from hold_devi() is the 6937 * same one as the one returned by DEVT2DEVINFO, so we are 6938 * safe for drivers with the correct getinfo(9e) impl. 6939 */ 6940 if (((ops = ddi_hold_driver(major)) != NULL) && 6941 CB_DRV_INSTALLED(ops) && ops->devo_getinfo) { 6942 if ((*ops->devo_getinfo)(NULL, DDI_INFO_DEVT2DEVINFO, 6943 (void *)dev, (void **)&ddip) != DDI_SUCCESS) 6944 ddip = NULL; 6945 } 6946 6947 /* give preference to the driver returned DEVT2DEVINFO dip */ 6948 if (ddip && (dip != ddip)) { 6949 #ifdef DEBUG 6950 cmn_err(CE_WARN, "%s: inconsistent getinfo(9E) implementation", 6951 ddi_driver_name(ddip)); 6952 #endif /* DEBUG */ 6953 ndi_hold_devi(ddip); 6954 if (dip) 6955 ndi_rele_devi(dip); 6956 dip = ddip; 6957 } 6958 6959 if (ops) 6960 ddi_rele_driver(major); 6961 6962 return (dip); 6963 } 6964 6965 /* 6966 * For compatibility only. Do not call this function! 6967 */ 6968 dev_info_t * 6969 e_ddi_get_dev_info(dev_t dev, vtype_t type) 6970 { 6971 dev_info_t *dip = NULL; 6972 if (getmajor(dev) >= devcnt) 6973 return (NULL); 6974 6975 switch (type) { 6976 case VCHR: 6977 case VBLK: 6978 dip = e_ddi_hold_devi_by_dev(dev, 0); 6979 default: 6980 break; 6981 } 6982 6983 /* 6984 * For compatibility reasons, we can only return the dip with 6985 * the driver ref count held. This is not a safe thing to do. 6986 * For certain broken third-party software, we are willing 6987 * to venture into unknown territory. 6988 */ 6989 if (dip) { 6990 (void) ndi_hold_driver(dip); 6991 ndi_rele_devi(dip); 6992 } 6993 return (dip); 6994 } 6995 6996 dev_info_t * 6997 e_ddi_hold_devi_by_path(char *path, int flags) 6998 { 6999 dev_info_t *dip; 7000 7001 /* can't specify NOATTACH by path */ 7002 ASSERT(!(flags & E_DDI_HOLD_DEVI_NOATTACH)); 7003 7004 return (resolve_pathname(path, &dip, NULL, NULL) ? NULL : dip); 7005 } 7006 7007 void 7008 e_ddi_hold_devi(dev_info_t *dip) 7009 { 7010 ndi_hold_devi(dip); 7011 } 7012 7013 void 7014 ddi_release_devi(dev_info_t *dip) 7015 { 7016 ndi_rele_devi(dip); 7017 } 7018 7019 /* 7020 * Associate a streams queue with a devinfo node 7021 * NOTE: This function is called by STREAM driver's put procedure. 7022 * It cannot block. 7023 */ 7024 void 7025 ddi_assoc_queue_with_devi(queue_t *q, dev_info_t *dip) 7026 { 7027 queue_t *rq = _RD(q); 7028 struct stdata *stp; 7029 vnode_t *vp; 7030 7031 /* set flag indicating that ddi_assoc_queue_with_devi was called */ 7032 mutex_enter(QLOCK(rq)); 7033 rq->q_flag |= _QASSOCIATED; 7034 mutex_exit(QLOCK(rq)); 7035 7036 /* get the vnode associated with the queue */ 7037 stp = STREAM(rq); 7038 vp = stp->sd_vnode; 7039 ASSERT(vp); 7040 7041 /* change the hardware association of the vnode */ 7042 spec_assoc_vp_with_devi(vp, dip); 7043 } 7044 7045 /* 7046 * ddi_install_driver(name) 7047 * 7048 * Driver installation is currently a byproduct of driver loading. This 7049 * may change. 7050 */ 7051 int 7052 ddi_install_driver(char *name) 7053 { 7054 major_t major = ddi_name_to_major(name); 7055 7056 if ((major == DDI_MAJOR_T_NONE) || 7057 (ddi_hold_installed_driver(major) == NULL)) { 7058 return (DDI_FAILURE); 7059 } 7060 ddi_rele_driver(major); 7061 return (DDI_SUCCESS); 7062 } 7063 7064 struct dev_ops * 7065 ddi_hold_driver(major_t major) 7066 { 7067 return (mod_hold_dev_by_major(major)); 7068 } 7069 7070 7071 void 7072 ddi_rele_driver(major_t major) 7073 { 7074 mod_rele_dev_by_major(major); 7075 } 7076 7077 7078 /* 7079 * This is called during boot to force attachment order of special dips 7080 * dip must be referenced via ndi_hold_devi() 7081 */ 7082 int 7083 i_ddi_attach_node_hierarchy(dev_info_t *dip) 7084 { 7085 dev_info_t *parent; 7086 int ret, circ; 7087 7088 /* 7089 * Recurse up until attached parent is found. 7090 */ 7091 if (i_ddi_devi_attached(dip)) 7092 return (DDI_SUCCESS); 7093 parent = ddi_get_parent(dip); 7094 if (i_ddi_attach_node_hierarchy(parent) != DDI_SUCCESS) 7095 return (DDI_FAILURE); 7096 7097 /* 7098 * Come top-down, expanding .conf nodes under this parent 7099 * and driving attach. 7100 */ 7101 ndi_devi_enter(parent, &circ); 7102 (void) i_ndi_make_spec_children(parent, 0); 7103 ret = i_ddi_attachchild(dip); 7104 ndi_devi_exit(parent, circ); 7105 7106 return (ret); 7107 } 7108 7109 /* keep this function static */ 7110 static int 7111 attach_driver_nodes(major_t major) 7112 { 7113 struct devnames *dnp; 7114 dev_info_t *dip; 7115 int error = DDI_FAILURE; 7116 7117 dnp = &devnamesp[major]; 7118 LOCK_DEV_OPS(&dnp->dn_lock); 7119 dip = dnp->dn_head; 7120 while (dip) { 7121 ndi_hold_devi(dip); 7122 UNLOCK_DEV_OPS(&dnp->dn_lock); 7123 if (i_ddi_attach_node_hierarchy(dip) == DDI_SUCCESS) 7124 error = DDI_SUCCESS; 7125 /* 7126 * Set the 'ddi-config-driver-node' property on a nexus 7127 * node to cause attach_driver_nodes() to configure all 7128 * immediate children of the nexus. This property should 7129 * be set on nodes with immediate children that bind to 7130 * the same driver as parent. 7131 */ 7132 if ((error == DDI_SUCCESS) && (ddi_prop_exists(DDI_DEV_T_ANY, 7133 dip, DDI_PROP_DONTPASS, "ddi-config-driver-node"))) { 7134 (void) ndi_devi_config(dip, NDI_NO_EVENT); 7135 } 7136 LOCK_DEV_OPS(&dnp->dn_lock); 7137 ndi_rele_devi(dip); 7138 dip = ddi_get_next(dip); 7139 } 7140 if (error == DDI_SUCCESS) 7141 dnp->dn_flags |= DN_NO_AUTODETACH; 7142 UNLOCK_DEV_OPS(&dnp->dn_lock); 7143 7144 7145 return (error); 7146 } 7147 7148 /* 7149 * i_ddi_attach_hw_nodes configures and attaches all hw nodes 7150 * bound to a specific driver. This function replaces calls to 7151 * ddi_hold_installed_driver() for drivers with no .conf 7152 * enumerated nodes. 7153 * 7154 * This facility is typically called at boot time to attach 7155 * platform-specific hardware nodes, such as ppm nodes on xcal 7156 * and grover and keyswitch nodes on cherrystone. It does not 7157 * deal with .conf enumerated node. Calling it beyond the boot 7158 * process is strongly discouraged. 7159 */ 7160 int 7161 i_ddi_attach_hw_nodes(char *driver) 7162 { 7163 major_t major; 7164 7165 major = ddi_name_to_major(driver); 7166 if (major == DDI_MAJOR_T_NONE) 7167 return (DDI_FAILURE); 7168 7169 return (attach_driver_nodes(major)); 7170 } 7171 7172 /* 7173 * i_ddi_attach_pseudo_node configures pseudo drivers which 7174 * has a single node. The .conf nodes must be enumerated 7175 * before calling this interface. The dip is held attached 7176 * upon returning. 7177 * 7178 * This facility should only be called only at boot time 7179 * by the I/O framework. 7180 */ 7181 dev_info_t * 7182 i_ddi_attach_pseudo_node(char *driver) 7183 { 7184 major_t major; 7185 dev_info_t *dip; 7186 7187 major = ddi_name_to_major(driver); 7188 if (major == DDI_MAJOR_T_NONE) 7189 return (NULL); 7190 7191 if (attach_driver_nodes(major) != DDI_SUCCESS) 7192 return (NULL); 7193 7194 dip = devnamesp[major].dn_head; 7195 ASSERT(dip && ddi_get_next(dip) == NULL); 7196 ndi_hold_devi(dip); 7197 return (dip); 7198 } 7199 7200 static void 7201 diplist_to_parent_major(dev_info_t *head, char parents[]) 7202 { 7203 major_t major; 7204 dev_info_t *dip, *pdip; 7205 7206 for (dip = head; dip != NULL; dip = ddi_get_next(dip)) { 7207 pdip = ddi_get_parent(dip); 7208 ASSERT(pdip); /* disallow rootnex.conf nodes */ 7209 major = ddi_driver_major(pdip); 7210 if ((major != DDI_MAJOR_T_NONE) && parents[major] == 0) 7211 parents[major] = 1; 7212 } 7213 } 7214 7215 /* 7216 * Call ddi_hold_installed_driver() on each parent major 7217 * and invoke mt_config_driver() to attach child major. 7218 * This is part of the implementation of ddi_hold_installed_driver. 7219 */ 7220 static int 7221 attach_driver_by_parent(major_t child_major, char parents[]) 7222 { 7223 major_t par_major; 7224 struct mt_config_handle *hdl; 7225 int flags = NDI_DEVI_PERSIST | NDI_NO_EVENT; 7226 7227 hdl = mt_config_init(NULL, NULL, flags, child_major, MT_CONFIG_OP, 7228 NULL); 7229 for (par_major = 0; par_major < devcnt; par_major++) { 7230 /* disallow recursion on the same driver */ 7231 if (parents[par_major] == 0 || par_major == child_major) 7232 continue; 7233 if (ddi_hold_installed_driver(par_major) == NULL) 7234 continue; 7235 hdl->mtc_parmajor = par_major; 7236 mt_config_driver(hdl); 7237 ddi_rele_driver(par_major); 7238 } 7239 (void) mt_config_fini(hdl); 7240 7241 return (i_ddi_devs_attached(child_major)); 7242 } 7243 7244 int 7245 i_ddi_devs_attached(major_t major) 7246 { 7247 dev_info_t *dip; 7248 struct devnames *dnp; 7249 int error = DDI_FAILURE; 7250 7251 /* check for attached instances */ 7252 dnp = &devnamesp[major]; 7253 LOCK_DEV_OPS(&dnp->dn_lock); 7254 for (dip = dnp->dn_head; dip != NULL; dip = ddi_get_next(dip)) { 7255 if (i_ddi_devi_attached(dip)) { 7256 error = DDI_SUCCESS; 7257 break; 7258 } 7259 } 7260 UNLOCK_DEV_OPS(&dnp->dn_lock); 7261 7262 return (error); 7263 } 7264 7265 int 7266 i_ddi_minor_node_count(dev_info_t *ddip, const char *node_type) 7267 { 7268 int circ; 7269 struct ddi_minor_data *dp; 7270 int count = 0; 7271 7272 ndi_devi_enter(ddip, &circ); 7273 for (dp = DEVI(ddip)->devi_minor; dp != NULL; dp = dp->next) { 7274 if (strcmp(dp->ddm_node_type, node_type) == 0) 7275 count++; 7276 } 7277 ndi_devi_exit(ddip, circ); 7278 return (count); 7279 } 7280 7281 /* 7282 * ddi_hold_installed_driver configures and attaches all 7283 * instances of the specified driver. To accomplish this 7284 * it configures and attaches all possible parents of 7285 * the driver, enumerated both in h/w nodes and in the 7286 * driver's .conf file. 7287 * 7288 * NOTE: This facility is for compatibility purposes only and will 7289 * eventually go away. Its usage is strongly discouraged. 7290 */ 7291 static void 7292 enter_driver(struct devnames *dnp) 7293 { 7294 mutex_enter(&dnp->dn_lock); 7295 ASSERT(dnp->dn_busy_thread != curthread); 7296 while (dnp->dn_flags & DN_DRIVER_BUSY) 7297 cv_wait(&dnp->dn_wait, &dnp->dn_lock); 7298 dnp->dn_flags |= DN_DRIVER_BUSY; 7299 dnp->dn_busy_thread = curthread; 7300 mutex_exit(&dnp->dn_lock); 7301 } 7302 7303 static void 7304 exit_driver(struct devnames *dnp) 7305 { 7306 mutex_enter(&dnp->dn_lock); 7307 ASSERT(dnp->dn_busy_thread == curthread); 7308 dnp->dn_flags &= ~DN_DRIVER_BUSY; 7309 dnp->dn_busy_thread = NULL; 7310 cv_broadcast(&dnp->dn_wait); 7311 mutex_exit(&dnp->dn_lock); 7312 } 7313 7314 struct dev_ops * 7315 ddi_hold_installed_driver(major_t major) 7316 { 7317 struct dev_ops *ops; 7318 struct devnames *dnp; 7319 char *parents; 7320 int error; 7321 7322 ops = ddi_hold_driver(major); 7323 if (ops == NULL) 7324 return (NULL); 7325 7326 /* 7327 * Return immediately if all the attach operations associated 7328 * with a ddi_hold_installed_driver() call have already been done. 7329 */ 7330 dnp = &devnamesp[major]; 7331 enter_driver(dnp); 7332 ASSERT(driver_active(major)); 7333 7334 if (dnp->dn_flags & DN_DRIVER_HELD) { 7335 exit_driver(dnp); 7336 if (i_ddi_devs_attached(major) == DDI_SUCCESS) 7337 return (ops); 7338 ddi_rele_driver(major); 7339 return (NULL); 7340 } 7341 7342 LOCK_DEV_OPS(&dnp->dn_lock); 7343 dnp->dn_flags |= (DN_DRIVER_HELD | DN_NO_AUTODETACH); 7344 UNLOCK_DEV_OPS(&dnp->dn_lock); 7345 7346 DCOMPATPRINTF((CE_CONT, 7347 "ddi_hold_installed_driver: %s\n", dnp->dn_name)); 7348 7349 /* 7350 * When the driver has no .conf children, it is sufficient 7351 * to attach existing nodes in the device tree. Nodes not 7352 * enumerated by the OBP are not attached. 7353 */ 7354 if (dnp->dn_pl == NULL) { 7355 if (attach_driver_nodes(major) == DDI_SUCCESS) { 7356 exit_driver(dnp); 7357 return (ops); 7358 } 7359 exit_driver(dnp); 7360 ddi_rele_driver(major); 7361 return (NULL); 7362 } 7363 7364 /* 7365 * Driver has .conf nodes. We find all possible parents 7366 * and recursively all ddi_hold_installed_driver on the 7367 * parent driver; then we invoke ndi_config_driver() 7368 * on all possible parent node in parallel to speed up 7369 * performance. 7370 */ 7371 parents = kmem_zalloc(devcnt * sizeof (char), KM_SLEEP); 7372 7373 LOCK_DEV_OPS(&dnp->dn_lock); 7374 /* find .conf parents */ 7375 (void) impl_parlist_to_major(dnp->dn_pl, parents); 7376 /* find hw node parents */ 7377 diplist_to_parent_major(dnp->dn_head, parents); 7378 UNLOCK_DEV_OPS(&dnp->dn_lock); 7379 7380 error = attach_driver_by_parent(major, parents); 7381 kmem_free(parents, devcnt * sizeof (char)); 7382 if (error == DDI_SUCCESS) { 7383 exit_driver(dnp); 7384 return (ops); 7385 } 7386 7387 exit_driver(dnp); 7388 ddi_rele_driver(major); 7389 return (NULL); 7390 } 7391 7392 /* 7393 * Default bus_config entry point for nexus drivers 7394 */ 7395 int 7396 ndi_busop_bus_config(dev_info_t *pdip, uint_t flags, ddi_bus_config_op_t op, 7397 void *arg, dev_info_t **child, clock_t timeout) 7398 { 7399 major_t major; 7400 7401 /* 7402 * A timeout of 30 minutes or more is probably a mistake 7403 * This is intended to catch uses where timeout is in 7404 * the wrong units. timeout must be in units of ticks. 7405 */ 7406 ASSERT(timeout < SEC_TO_TICK(1800)); 7407 7408 major = DDI_MAJOR_T_NONE; 7409 switch (op) { 7410 case BUS_CONFIG_ONE: 7411 NDI_DEBUG(flags, (CE_CONT, "%s%d: bus config %s timeout=%ld\n", 7412 ddi_driver_name(pdip), ddi_get_instance(pdip), 7413 (char *)arg, timeout)); 7414 return (devi_config_one(pdip, (char *)arg, child, flags, 7415 timeout)); 7416 7417 case BUS_CONFIG_DRIVER: 7418 major = (major_t)(uintptr_t)arg; 7419 /*FALLTHROUGH*/ 7420 case BUS_CONFIG_ALL: 7421 NDI_DEBUG(flags, (CE_CONT, "%s%d: bus config timeout=%ld\n", 7422 ddi_driver_name(pdip), ddi_get_instance(pdip), 7423 timeout)); 7424 if (timeout > 0) { 7425 NDI_DEBUG(flags, (CE_CONT, 7426 "%s%d: bus config all timeout=%ld\n", 7427 ddi_driver_name(pdip), ddi_get_instance(pdip), 7428 timeout)); 7429 delay(timeout); 7430 } 7431 return (config_immediate_children(pdip, flags, major)); 7432 7433 default: 7434 return (NDI_FAILURE); 7435 } 7436 /*NOTREACHED*/ 7437 } 7438 7439 /* 7440 * Default busop bus_unconfig handler for nexus drivers 7441 */ 7442 int 7443 ndi_busop_bus_unconfig(dev_info_t *pdip, uint_t flags, ddi_bus_config_op_t op, 7444 void *arg) 7445 { 7446 major_t major; 7447 7448 major = DDI_MAJOR_T_NONE; 7449 switch (op) { 7450 case BUS_UNCONFIG_ONE: 7451 NDI_DEBUG(flags, (CE_CONT, "%s%d: bus unconfig %s\n", 7452 ddi_driver_name(pdip), ddi_get_instance(pdip), 7453 (char *)arg)); 7454 return (devi_unconfig_one(pdip, (char *)arg, flags)); 7455 7456 case BUS_UNCONFIG_DRIVER: 7457 major = (major_t)(uintptr_t)arg; 7458 /*FALLTHROUGH*/ 7459 case BUS_UNCONFIG_ALL: 7460 NDI_DEBUG(flags, (CE_CONT, "%s%d: bus unconfig all\n", 7461 ddi_driver_name(pdip), ddi_get_instance(pdip))); 7462 return (unconfig_immediate_children(pdip, NULL, flags, major)); 7463 7464 default: 7465 return (NDI_FAILURE); 7466 } 7467 /*NOTREACHED*/ 7468 } 7469 7470 /* 7471 * dummy functions to be removed 7472 */ 7473 void 7474 impl_rem_dev_props(dev_info_t *dip) 7475 { 7476 _NOTE(ARGUNUSED(dip)) 7477 /* do nothing */ 7478 } 7479 7480 /* 7481 * Determine if a node is a leaf node. If not sure, return false (0). 7482 */ 7483 static int 7484 is_leaf_node(dev_info_t *dip) 7485 { 7486 major_t major = ddi_driver_major(dip); 7487 7488 if (major == DDI_MAJOR_T_NONE) 7489 return (0); 7490 7491 return (devnamesp[major].dn_flags & DN_LEAF_DRIVER); 7492 } 7493 7494 /* 7495 * Multithreaded [un]configuration 7496 */ 7497 static struct mt_config_handle * 7498 mt_config_init(dev_info_t *pdip, dev_info_t **dipp, int flags, 7499 major_t major, int op, struct brevq_node **brevqp) 7500 { 7501 struct mt_config_handle *hdl = kmem_alloc(sizeof (*hdl), KM_SLEEP); 7502 7503 mutex_init(&hdl->mtc_lock, NULL, MUTEX_DEFAULT, NULL); 7504 cv_init(&hdl->mtc_cv, NULL, CV_DEFAULT, NULL); 7505 hdl->mtc_pdip = pdip; 7506 hdl->mtc_fdip = dipp; 7507 hdl->mtc_parmajor = DDI_MAJOR_T_NONE; 7508 hdl->mtc_flags = flags; 7509 hdl->mtc_major = major; 7510 hdl->mtc_thr_count = 0; 7511 hdl->mtc_op = op; 7512 hdl->mtc_error = 0; 7513 hdl->mtc_brevqp = brevqp; 7514 7515 #ifdef DEBUG 7516 gethrestime(&hdl->start_time); 7517 hdl->total_time = 0; 7518 #endif /* DEBUG */ 7519 7520 return (hdl); 7521 } 7522 7523 #ifdef DEBUG 7524 static int 7525 time_diff_in_msec(timestruc_t start, timestruc_t end) 7526 { 7527 int nsec, sec; 7528 7529 sec = end.tv_sec - start.tv_sec; 7530 nsec = end.tv_nsec - start.tv_nsec; 7531 if (nsec < 0) { 7532 nsec += NANOSEC; 7533 sec -= 1; 7534 } 7535 7536 return (sec * (NANOSEC >> 20) + (nsec >> 20)); 7537 } 7538 7539 #endif /* DEBUG */ 7540 7541 static int 7542 mt_config_fini(struct mt_config_handle *hdl) 7543 { 7544 int rv; 7545 #ifdef DEBUG 7546 int real_time; 7547 timestruc_t end_time; 7548 #endif /* DEBUG */ 7549 7550 mutex_enter(&hdl->mtc_lock); 7551 while (hdl->mtc_thr_count > 0) 7552 cv_wait(&hdl->mtc_cv, &hdl->mtc_lock); 7553 rv = hdl->mtc_error; 7554 mutex_exit(&hdl->mtc_lock); 7555 7556 #ifdef DEBUG 7557 gethrestime(&end_time); 7558 real_time = time_diff_in_msec(hdl->start_time, end_time); 7559 if ((ddidebug & DDI_MTCONFIG) && hdl->mtc_pdip) 7560 cmn_err(CE_NOTE, 7561 "config %s%d: total time %d msec, real time %d msec", 7562 ddi_driver_name(hdl->mtc_pdip), 7563 ddi_get_instance(hdl->mtc_pdip), 7564 hdl->total_time, real_time); 7565 #endif /* DEBUG */ 7566 7567 cv_destroy(&hdl->mtc_cv); 7568 mutex_destroy(&hdl->mtc_lock); 7569 kmem_free(hdl, sizeof (*hdl)); 7570 7571 return (rv); 7572 } 7573 7574 struct mt_config_data { 7575 struct mt_config_handle *mtc_hdl; 7576 dev_info_t *mtc_dip; 7577 major_t mtc_major; 7578 int mtc_flags; 7579 struct brevq_node *mtc_brn; 7580 struct mt_config_data *mtc_next; 7581 }; 7582 7583 static void 7584 mt_config_thread(void *arg) 7585 { 7586 struct mt_config_data *mcd = (struct mt_config_data *)arg; 7587 struct mt_config_handle *hdl = mcd->mtc_hdl; 7588 dev_info_t *dip = mcd->mtc_dip; 7589 dev_info_t *rdip, **dipp; 7590 major_t major = mcd->mtc_major; 7591 int flags = mcd->mtc_flags; 7592 int rv = 0; 7593 7594 #ifdef DEBUG 7595 timestruc_t start_time, end_time; 7596 gethrestime(&start_time); 7597 #endif /* DEBUG */ 7598 7599 rdip = NULL; 7600 dipp = hdl->mtc_fdip ? &rdip : NULL; 7601 7602 switch (hdl->mtc_op) { 7603 case MT_CONFIG_OP: 7604 rv = devi_config_common(dip, flags, major); 7605 break; 7606 case MT_UNCONFIG_OP: 7607 if (mcd->mtc_brn) { 7608 struct brevq_node *brevq = NULL; 7609 rv = devi_unconfig_common(dip, dipp, flags, major, 7610 &brevq); 7611 mcd->mtc_brn->brn_child = brevq; 7612 } else 7613 rv = devi_unconfig_common(dip, dipp, flags, major, 7614 NULL); 7615 break; 7616 } 7617 7618 mutex_enter(&hdl->mtc_lock); 7619 #ifdef DEBUG 7620 gethrestime(&end_time); 7621 hdl->total_time += time_diff_in_msec(start_time, end_time); 7622 #endif /* DEBUG */ 7623 7624 if ((rv != NDI_SUCCESS) && (hdl->mtc_error == 0)) { 7625 hdl->mtc_error = rv; 7626 #ifdef DEBUG 7627 if ((ddidebug & DDI_DEBUG) && (major != DDI_MAJOR_T_NONE)) { 7628 char *path = kmem_alloc(MAXPATHLEN, KM_SLEEP); 7629 7630 (void) ddi_pathname(dip, path); 7631 cmn_err(CE_NOTE, "mt_config_thread: " 7632 "op %d.%d.%x at %s failed %d", 7633 hdl->mtc_op, major, flags, path, rv); 7634 kmem_free(path, MAXPATHLEN); 7635 } 7636 #endif /* DEBUG */ 7637 } 7638 7639 if (hdl->mtc_fdip && *hdl->mtc_fdip == NULL) { 7640 *hdl->mtc_fdip = rdip; 7641 rdip = NULL; 7642 } 7643 7644 if (rdip) { 7645 ASSERT(rv != NDI_SUCCESS); 7646 ndi_rele_devi(rdip); 7647 } 7648 7649 ndi_rele_devi(dip); 7650 7651 if (--hdl->mtc_thr_count == 0) 7652 cv_broadcast(&hdl->mtc_cv); 7653 mutex_exit(&hdl->mtc_lock); 7654 kmem_free(mcd, sizeof (*mcd)); 7655 } 7656 7657 /* 7658 * Multi-threaded config/unconfig of child nexus 7659 */ 7660 static void 7661 mt_config_children(struct mt_config_handle *hdl) 7662 { 7663 dev_info_t *pdip = hdl->mtc_pdip; 7664 major_t major = hdl->mtc_major; 7665 dev_info_t *dip; 7666 int circ; 7667 struct brevq_node *brn; 7668 struct mt_config_data *mcd_head = NULL; 7669 struct mt_config_data *mcd_tail = NULL; 7670 struct mt_config_data *mcd; 7671 #ifdef DEBUG 7672 timestruc_t end_time; 7673 7674 /* Update total_time in handle */ 7675 gethrestime(&end_time); 7676 hdl->total_time += time_diff_in_msec(hdl->start_time, end_time); 7677 #endif 7678 7679 ndi_devi_enter(pdip, &circ); 7680 dip = ddi_get_child(pdip); 7681 while (dip) { 7682 if (hdl->mtc_op == MT_UNCONFIG_OP && hdl->mtc_brevqp && 7683 !(DEVI_EVREMOVE(dip)) && 7684 i_ddi_node_state(dip) >= DS_INITIALIZED) { 7685 /* 7686 * Enqueue this dip's deviname. 7687 * No need to hold a lock while enqueuing since this 7688 * is the only thread doing the enqueue and no one 7689 * walks the queue while we are in multithreaded 7690 * unconfiguration. 7691 */ 7692 brn = brevq_enqueue(hdl->mtc_brevqp, dip, NULL); 7693 } else 7694 brn = NULL; 7695 7696 /* 7697 * Hold the child that we are processing so he does not get 7698 * removed. The corrisponding ndi_rele_devi() for children 7699 * that are not being skipped is done at the end of 7700 * mt_config_thread(). 7701 */ 7702 ndi_hold_devi(dip); 7703 7704 /* 7705 * skip leaf nodes and (for configure) nodes not 7706 * fully attached. 7707 */ 7708 if (is_leaf_node(dip) || 7709 (hdl->mtc_op == MT_CONFIG_OP && 7710 i_ddi_node_state(dip) < DS_READY)) { 7711 ndi_rele_devi(dip); 7712 dip = ddi_get_next_sibling(dip); 7713 continue; 7714 } 7715 7716 mcd = kmem_alloc(sizeof (*mcd), KM_SLEEP); 7717 mcd->mtc_dip = dip; 7718 mcd->mtc_hdl = hdl; 7719 mcd->mtc_brn = brn; 7720 7721 /* 7722 * Switch a 'driver' operation to an 'all' operation below a 7723 * node bound to the driver. 7724 */ 7725 if ((major == DDI_MAJOR_T_NONE) || 7726 (major == ddi_driver_major(dip))) 7727 mcd->mtc_major = DDI_MAJOR_T_NONE; 7728 else 7729 mcd->mtc_major = major; 7730 7731 /* 7732 * The unconfig-driver to unconfig-all conversion above 7733 * constitutes an autodetach for NDI_DETACH_DRIVER calls, 7734 * set NDI_AUTODETACH. 7735 */ 7736 mcd->mtc_flags = hdl->mtc_flags; 7737 if ((mcd->mtc_flags & NDI_DETACH_DRIVER) && 7738 (hdl->mtc_op == MT_UNCONFIG_OP) && 7739 (major == ddi_driver_major(pdip))) 7740 mcd->mtc_flags |= NDI_AUTODETACH; 7741 7742 mutex_enter(&hdl->mtc_lock); 7743 hdl->mtc_thr_count++; 7744 mutex_exit(&hdl->mtc_lock); 7745 7746 /* 7747 * Add to end of list to process after ndi_devi_exit to avoid 7748 * locking differences depending on value of mtc_off. 7749 */ 7750 mcd->mtc_next = NULL; 7751 if (mcd_head == NULL) 7752 mcd_head = mcd; 7753 else 7754 mcd_tail->mtc_next = mcd; 7755 mcd_tail = mcd; 7756 7757 dip = ddi_get_next_sibling(dip); 7758 } 7759 ndi_devi_exit(pdip, circ); 7760 7761 /* go through the list of held children */ 7762 for (mcd = mcd_head; mcd; mcd = mcd_head) { 7763 mcd_head = mcd->mtc_next; 7764 if (mtc_off || (mcd->mtc_flags & NDI_MTC_OFF)) 7765 mt_config_thread(mcd); 7766 else 7767 (void) thread_create(NULL, 0, mt_config_thread, mcd, 7768 0, &p0, TS_RUN, minclsyspri); 7769 } 7770 } 7771 7772 static void 7773 mt_config_driver(struct mt_config_handle *hdl) 7774 { 7775 major_t par_major = hdl->mtc_parmajor; 7776 major_t major = hdl->mtc_major; 7777 struct devnames *dnp = &devnamesp[par_major]; 7778 dev_info_t *dip; 7779 struct mt_config_data *mcd_head = NULL; 7780 struct mt_config_data *mcd_tail = NULL; 7781 struct mt_config_data *mcd; 7782 #ifdef DEBUG 7783 timestruc_t end_time; 7784 7785 /* Update total_time in handle */ 7786 gethrestime(&end_time); 7787 hdl->total_time += time_diff_in_msec(hdl->start_time, end_time); 7788 #endif 7789 ASSERT(par_major != DDI_MAJOR_T_NONE); 7790 ASSERT(major != DDI_MAJOR_T_NONE); 7791 7792 LOCK_DEV_OPS(&dnp->dn_lock); 7793 dip = devnamesp[par_major].dn_head; 7794 while (dip) { 7795 /* 7796 * Hold the child that we are processing so he does not get 7797 * removed. The corrisponding ndi_rele_devi() for children 7798 * that are not being skipped is done at the end of 7799 * mt_config_thread(). 7800 */ 7801 ndi_hold_devi(dip); 7802 7803 /* skip leaf nodes and nodes not fully attached */ 7804 if (!i_ddi_devi_attached(dip) || is_leaf_node(dip)) { 7805 ndi_rele_devi(dip); 7806 dip = ddi_get_next(dip); 7807 continue; 7808 } 7809 7810 mcd = kmem_alloc(sizeof (*mcd), KM_SLEEP); 7811 mcd->mtc_dip = dip; 7812 mcd->mtc_hdl = hdl; 7813 mcd->mtc_major = major; 7814 mcd->mtc_flags = hdl->mtc_flags; 7815 7816 mutex_enter(&hdl->mtc_lock); 7817 hdl->mtc_thr_count++; 7818 mutex_exit(&hdl->mtc_lock); 7819 7820 /* 7821 * Add to end of list to process after UNLOCK_DEV_OPS to avoid 7822 * locking differences depending on value of mtc_off. 7823 */ 7824 mcd->mtc_next = NULL; 7825 if (mcd_head == NULL) 7826 mcd_head = mcd; 7827 else 7828 mcd_tail->mtc_next = mcd; 7829 mcd_tail = mcd; 7830 7831 dip = ddi_get_next(dip); 7832 } 7833 UNLOCK_DEV_OPS(&dnp->dn_lock); 7834 7835 /* go through the list of held children */ 7836 for (mcd = mcd_head; mcd; mcd = mcd_head) { 7837 mcd_head = mcd->mtc_next; 7838 if (mtc_off || (mcd->mtc_flags & NDI_MTC_OFF)) 7839 mt_config_thread(mcd); 7840 else 7841 (void) thread_create(NULL, 0, mt_config_thread, mcd, 7842 0, &p0, TS_RUN, minclsyspri); 7843 } 7844 } 7845 7846 /* 7847 * Given the nodeid for a persistent (PROM or SID) node, return 7848 * the corresponding devinfo node 7849 * NOTE: This function will return NULL for .conf nodeids. 7850 */ 7851 dev_info_t * 7852 e_ddi_nodeid_to_dip(pnode_t nodeid) 7853 { 7854 dev_info_t *dip = NULL; 7855 struct devi_nodeid *prev, *elem; 7856 7857 mutex_enter(&devimap->dno_lock); 7858 7859 prev = NULL; 7860 for (elem = devimap->dno_head; elem; elem = elem->next) { 7861 if (elem->nodeid == nodeid) { 7862 ndi_hold_devi(elem->dip); 7863 dip = elem->dip; 7864 break; 7865 } 7866 prev = elem; 7867 } 7868 7869 /* 7870 * Move to head for faster lookup next time 7871 */ 7872 if (elem && prev) { 7873 prev->next = elem->next; 7874 elem->next = devimap->dno_head; 7875 devimap->dno_head = elem; 7876 } 7877 7878 mutex_exit(&devimap->dno_lock); 7879 return (dip); 7880 } 7881 7882 static void 7883 free_cache_task(void *arg) 7884 { 7885 ASSERT(arg == NULL); 7886 7887 mutex_enter(&di_cache.cache_lock); 7888 7889 /* 7890 * The cache can be invalidated without holding the lock 7891 * but it can be made valid again only while the lock is held. 7892 * So if the cache is invalid when the lock is held, it will 7893 * stay invalid until lock is released. 7894 */ 7895 if (!di_cache.cache_valid) 7896 i_ddi_di_cache_free(&di_cache); 7897 7898 mutex_exit(&di_cache.cache_lock); 7899 7900 if (di_cache_debug) 7901 cmn_err(CE_NOTE, "system_taskq: di_cache freed"); 7902 } 7903 7904 extern int modrootloaded; 7905 7906 void 7907 i_ddi_di_cache_free(struct di_cache *cache) 7908 { 7909 int error; 7910 extern int sys_shutdown; 7911 7912 ASSERT(mutex_owned(&cache->cache_lock)); 7913 7914 if (cache->cache_size) { 7915 ASSERT(cache->cache_size > 0); 7916 ASSERT(cache->cache_data); 7917 7918 kmem_free(cache->cache_data, cache->cache_size); 7919 cache->cache_data = NULL; 7920 cache->cache_size = 0; 7921 7922 if (di_cache_debug) 7923 cmn_err(CE_NOTE, "i_ddi_di_cache_free: freed cachemem"); 7924 } else { 7925 ASSERT(cache->cache_data == NULL); 7926 if (di_cache_debug) 7927 cmn_err(CE_NOTE, "i_ddi_di_cache_free: NULL cache"); 7928 } 7929 7930 if (!modrootloaded || rootvp == NULL || 7931 vn_is_readonly(rootvp) || sys_shutdown) { 7932 if (di_cache_debug) { 7933 cmn_err(CE_WARN, "/ not mounted/RDONLY. Skip unlink"); 7934 } 7935 return; 7936 } 7937 7938 error = vn_remove(DI_CACHE_FILE, UIO_SYSSPACE, RMFILE); 7939 if (di_cache_debug && error && error != ENOENT) { 7940 cmn_err(CE_WARN, "%s: unlink failed: %d", DI_CACHE_FILE, error); 7941 } else if (di_cache_debug && !error) { 7942 cmn_err(CE_NOTE, "i_ddi_di_cache_free: unlinked cache file"); 7943 } 7944 } 7945 7946 void 7947 i_ddi_di_cache_invalidate() 7948 { 7949 int cache_valid; 7950 7951 if (!modrootloaded || !i_ddi_io_initialized()) { 7952 if (di_cache_debug) 7953 cmn_err(CE_NOTE, "I/O not inited. Skipping invalidate"); 7954 return; 7955 } 7956 7957 /* Increment devtree generation number. */ 7958 atomic_inc_ulong(&devtree_gen); 7959 7960 /* Invalidate the in-core cache and dispatch free on valid->invalid */ 7961 cache_valid = atomic_swap_uint(&di_cache.cache_valid, 0); 7962 if (cache_valid) { 7963 /* 7964 * This is an optimization to start cleaning up a cached 7965 * snapshot early. For this reason, it is OK for 7966 * taskq_dispatach to fail (and it is OK to not track calling 7967 * context relative to sleep, and assume NOSLEEP). 7968 */ 7969 (void) taskq_dispatch(system_taskq, free_cache_task, NULL, 7970 TQ_NOSLEEP); 7971 } 7972 7973 if (di_cache_debug) { 7974 cmn_err(CE_NOTE, "invalidation"); 7975 } 7976 } 7977 7978 7979 static void 7980 i_bind_vhci_node(dev_info_t *dip) 7981 { 7982 DEVI(dip)->devi_major = ddi_name_to_major(ddi_node_name(dip)); 7983 i_ddi_set_node_state(dip, DS_BOUND); 7984 } 7985 7986 static char vhci_node_addr[2]; 7987 7988 static int 7989 i_init_vhci_node(dev_info_t *dip) 7990 { 7991 add_global_props(dip); 7992 DEVI(dip)->devi_ops = ndi_hold_driver(dip); 7993 if (DEVI(dip)->devi_ops == NULL) 7994 return (-1); 7995 7996 DEVI(dip)->devi_instance = e_ddi_assign_instance(dip); 7997 e_ddi_keep_instance(dip); 7998 vhci_node_addr[0] = '\0'; 7999 ddi_set_name_addr(dip, vhci_node_addr); 8000 i_ddi_set_node_state(dip, DS_INITIALIZED); 8001 return (0); 8002 } 8003 8004 static void 8005 i_link_vhci_node(dev_info_t *dip) 8006 { 8007 ASSERT(MUTEX_HELD(&global_vhci_lock)); 8008 8009 /* 8010 * scsi_vhci should be kept left most of the device tree. 8011 */ 8012 if (scsi_vhci_dip) { 8013 DEVI(dip)->devi_sibling = DEVI(scsi_vhci_dip)->devi_sibling; 8014 DEVI(scsi_vhci_dip)->devi_sibling = DEVI(dip); 8015 } else { 8016 DEVI(dip)->devi_sibling = DEVI(top_devinfo)->devi_child; 8017 DEVI(top_devinfo)->devi_child = DEVI(dip); 8018 } 8019 } 8020 8021 8022 /* 8023 * This a special routine to enumerate vhci node (child of rootnex 8024 * node) without holding the ndi_devi_enter() lock. The device node 8025 * is allocated, initialized and brought into DS_READY state before 8026 * inserting into the device tree. The VHCI node is handcrafted 8027 * here to bring the node to DS_READY, similar to rootnex node. 8028 * 8029 * The global_vhci_lock protects linking the node into the device 8030 * as same lock is held before linking/unlinking any direct child 8031 * of rootnex children. 8032 * 8033 * This routine is a workaround to handle a possible deadlock 8034 * that occurs while trying to enumerate node in a different sub-tree 8035 * during _init/_attach entry points. 8036 */ 8037 /*ARGSUSED*/ 8038 dev_info_t * 8039 ndi_devi_config_vhci(char *drvname, int flags) 8040 { 8041 struct devnames *dnp; 8042 dev_info_t *dip; 8043 major_t major = ddi_name_to_major(drvname); 8044 8045 if (major == -1) 8046 return (NULL); 8047 8048 /* Make sure we create the VHCI node only once */ 8049 dnp = &devnamesp[major]; 8050 LOCK_DEV_OPS(&dnp->dn_lock); 8051 if (dnp->dn_head) { 8052 dip = dnp->dn_head; 8053 UNLOCK_DEV_OPS(&dnp->dn_lock); 8054 return (dip); 8055 } 8056 UNLOCK_DEV_OPS(&dnp->dn_lock); 8057 8058 /* Allocate the VHCI node */ 8059 ndi_devi_alloc_sleep(top_devinfo, drvname, DEVI_SID_NODEID, &dip); 8060 ndi_hold_devi(dip); 8061 8062 /* Mark the node as VHCI */ 8063 DEVI(dip)->devi_node_attributes |= DDI_VHCI_NODE; 8064 8065 i_ddi_add_devimap(dip); 8066 i_bind_vhci_node(dip); 8067 if (i_init_vhci_node(dip) == -1) { 8068 ndi_rele_devi(dip); 8069 (void) ndi_devi_free(dip); 8070 return (NULL); 8071 } 8072 8073 mutex_enter(&(DEVI(dip)->devi_lock)); 8074 DEVI_SET_ATTACHING(dip); 8075 mutex_exit(&(DEVI(dip)->devi_lock)); 8076 8077 if (devi_attach(dip, DDI_ATTACH) != DDI_SUCCESS) { 8078 cmn_err(CE_CONT, "Could not attach %s driver", drvname); 8079 e_ddi_free_instance(dip, vhci_node_addr); 8080 ndi_rele_devi(dip); 8081 (void) ndi_devi_free(dip); 8082 return (NULL); 8083 } 8084 mutex_enter(&(DEVI(dip)->devi_lock)); 8085 DEVI_CLR_ATTACHING(dip); 8086 mutex_exit(&(DEVI(dip)->devi_lock)); 8087 8088 mutex_enter(&global_vhci_lock); 8089 i_link_vhci_node(dip); 8090 mutex_exit(&global_vhci_lock); 8091 i_ddi_set_node_state(dip, DS_READY); 8092 8093 LOCK_DEV_OPS(&dnp->dn_lock); 8094 dnp->dn_flags |= DN_DRIVER_HELD; 8095 dnp->dn_head = dip; 8096 UNLOCK_DEV_OPS(&dnp->dn_lock); 8097 8098 i_ndi_devi_report_status_change(dip, NULL); 8099 8100 return (dip); 8101 } 8102 8103 /* 8104 * Maintain DEVI_DEVICE_REMOVED hotplug devi_state for remove/reinsert hotplug 8105 * of open devices. Currently, because of tight coupling between the devfs file 8106 * system and the Solaris device tree, a driver can't always make the device 8107 * tree state (esp devi_node_state) match device hardware hotplug state. Until 8108 * resolved, to overcome this deficiency we use the following interfaces that 8109 * maintain the DEVI_DEVICE_REMOVED devi_state status bit. These interface 8110 * report current state, and drive operation (like events and cache 8111 * invalidation) when a driver changes remove/insert state of an open device. 8112 * 8113 * The ndi_devi_device_isremoved() returns 1 if the device is currently removed. 8114 * 8115 * The ndi_devi_device_remove() interface declares the device as removed, and 8116 * returns 1 if there was a state change associated with this declaration. 8117 * 8118 * The ndi_devi_device_insert() declares the device as inserted, and returns 1 8119 * if there was a state change associated with this declaration. 8120 */ 8121 int 8122 ndi_devi_device_isremoved(dev_info_t *dip) 8123 { 8124 return (DEVI_IS_DEVICE_REMOVED(dip)); 8125 } 8126 8127 int 8128 ndi_devi_device_remove(dev_info_t *dip) 8129 { 8130 ASSERT(dip && ddi_get_parent(dip) && 8131 DEVI_BUSY_OWNED(ddi_get_parent(dip))); 8132 8133 /* Return if already marked removed. */ 8134 if (ndi_devi_device_isremoved(dip)) 8135 return (0); 8136 8137 /* Mark the device as having been physically removed. */ 8138 mutex_enter(&(DEVI(dip)->devi_lock)); 8139 ndi_devi_set_hidden(dip); /* invisible: lookup/snapshot */ 8140 DEVI_SET_DEVICE_REMOVED(dip); 8141 DEVI_SET_EVREMOVE(dip); /* this clears EVADD too */ 8142 mutex_exit(&(DEVI(dip)->devi_lock)); 8143 8144 /* report remove (as 'removed') */ 8145 i_ndi_devi_report_status_change(dip, NULL); 8146 8147 /* 8148 * Invalidate the cache to ensure accurate 8149 * (di_state() & DI_DEVICE_REMOVED). 8150 */ 8151 i_ddi_di_cache_invalidate(); 8152 8153 /* 8154 * Generate sysevent for those interested in removal (either 8155 * directly via private EC_DEVFS or indirectly via devfsadmd 8156 * generated EC_DEV). This will generate LDI DEVICE_REMOVE 8157 * event too. 8158 */ 8159 i_ddi_log_devfs_device_remove(dip); 8160 8161 return (1); /* DEVICE_REMOVED state changed */ 8162 } 8163 8164 int 8165 ndi_devi_device_insert(dev_info_t *dip) 8166 { 8167 ASSERT(dip && ddi_get_parent(dip) && 8168 DEVI_BUSY_OWNED(ddi_get_parent(dip))); 8169 8170 /* Return if not marked removed. */ 8171 if (!ndi_devi_device_isremoved(dip)) 8172 return (0); 8173 8174 /* Mark the device as having been physically reinserted. */ 8175 mutex_enter(&(DEVI(dip)->devi_lock)); 8176 ndi_devi_clr_hidden(dip); /* visible: lookup/snapshot */ 8177 DEVI_SET_DEVICE_REINSERTED(dip); 8178 DEVI_SET_EVADD(dip); /* this clears EVREMOVE too */ 8179 mutex_exit(&(DEVI(dip)->devi_lock)); 8180 8181 /* report insert (as 'online') */ 8182 i_ndi_devi_report_status_change(dip, NULL); 8183 8184 /* 8185 * Invalidate the cache to ensure accurate 8186 * (di_state() & DI_DEVICE_REMOVED). 8187 */ 8188 i_ddi_di_cache_invalidate(); 8189 8190 /* 8191 * Generate sysevent for those interested in removal (either directly 8192 * via EC_DEVFS or indirectly via devfsadmd generated EC_DEV). 8193 */ 8194 i_ddi_log_devfs_device_insert(dip); 8195 8196 return (1); /* DEVICE_REMOVED state changed */ 8197 } 8198 8199 /* 8200 * ibt_hw_is_present() returns 0 when there is no IB hardware actively 8201 * running. This is primarily useful for modules like rpcmod which 8202 * needs a quick check to decide whether or not it should try to use 8203 * InfiniBand 8204 */ 8205 int ib_hw_status = 0; 8206 int 8207 ibt_hw_is_present() 8208 { 8209 return (ib_hw_status); 8210 } 8211 8212 /* 8213 * ASSERT that constraint flag is not set and then set the "retire attempt" 8214 * flag. 8215 */ 8216 int 8217 e_ddi_mark_retiring(dev_info_t *dip, void *arg) 8218 { 8219 char **cons_array = (char **)arg; 8220 char *path; 8221 int constraint; 8222 int i; 8223 8224 constraint = 0; 8225 if (cons_array) { 8226 path = kmem_alloc(MAXPATHLEN, KM_SLEEP); 8227 (void) ddi_pathname(dip, path); 8228 for (i = 0; cons_array[i] != NULL; i++) { 8229 if (strcmp(path, cons_array[i]) == 0) { 8230 constraint = 1; 8231 break; 8232 } 8233 } 8234 kmem_free(path, MAXPATHLEN); 8235 } 8236 8237 mutex_enter(&DEVI(dip)->devi_lock); 8238 ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT)); 8239 DEVI(dip)->devi_flags |= DEVI_RETIRING; 8240 if (constraint) 8241 DEVI(dip)->devi_flags |= DEVI_R_CONSTRAINT; 8242 mutex_exit(&DEVI(dip)->devi_lock); 8243 8244 RIO_VERBOSE((CE_NOTE, "marked dip as undergoing retire process dip=%p", 8245 (void *)dip)); 8246 8247 if (constraint) 8248 RIO_DEBUG((CE_NOTE, "marked dip as constrained, dip=%p", 8249 (void *)dip)); 8250 8251 if (MDI_PHCI(dip)) 8252 mdi_phci_mark_retiring(dip, cons_array); 8253 8254 return (DDI_WALK_CONTINUE); 8255 } 8256 8257 static void 8258 free_array(char **cons_array) 8259 { 8260 int i; 8261 8262 if (cons_array == NULL) 8263 return; 8264 8265 for (i = 0; cons_array[i] != NULL; i++) { 8266 kmem_free(cons_array[i], strlen(cons_array[i]) + 1); 8267 } 8268 kmem_free(cons_array, (i+1) * sizeof (char *)); 8269 } 8270 8271 /* 8272 * Walk *every* node in subtree and check if it blocks, allows or has no 8273 * comment on a proposed retire. 8274 */ 8275 int 8276 e_ddi_retire_notify(dev_info_t *dip, void *arg) 8277 { 8278 int *constraint = (int *)arg; 8279 8280 RIO_DEBUG((CE_NOTE, "retire notify: dip = %p", (void *)dip)); 8281 8282 (void) e_ddi_offline_notify(dip); 8283 8284 mutex_enter(&(DEVI(dip)->devi_lock)); 8285 if (!(DEVI(dip)->devi_flags & DEVI_RETIRING)) { 8286 RIO_DEBUG((CE_WARN, "retire notify: dip in retire " 8287 "subtree is not marked: dip = %p", (void *)dip)); 8288 *constraint = 0; 8289 } else if (DEVI(dip)->devi_flags & DEVI_R_BLOCKED) { 8290 ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT)); 8291 RIO_DEBUG((CE_NOTE, "retire notify: BLOCKED: dip = %p", 8292 (void *)dip)); 8293 *constraint = 0; 8294 } else if (!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT)) { 8295 RIO_DEBUG((CE_NOTE, "retire notify: NO CONSTRAINT: " 8296 "dip = %p", (void *)dip)); 8297 *constraint = 0; 8298 } else { 8299 RIO_DEBUG((CE_NOTE, "retire notify: CONSTRAINT set: " 8300 "dip = %p", (void *)dip)); 8301 } 8302 mutex_exit(&DEVI(dip)->devi_lock); 8303 8304 if (MDI_PHCI(dip)) 8305 mdi_phci_retire_notify(dip, constraint); 8306 8307 return (DDI_WALK_CONTINUE); 8308 } 8309 8310 int 8311 e_ddi_retire_finalize(dev_info_t *dip, void *arg) 8312 { 8313 int constraint = *(int *)arg; 8314 int finalize; 8315 int phci_only; 8316 8317 mutex_enter(&DEVI(dip)->devi_lock); 8318 if (!(DEVI(dip)->devi_flags & DEVI_RETIRING)) { 8319 RIO_DEBUG((CE_WARN, 8320 "retire: unmarked dip(%p) in retire subtree", 8321 (void *)dip)); 8322 ASSERT(!(DEVI(dip)->devi_flags & DEVI_RETIRED)); 8323 ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT)); 8324 ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_BLOCKED)); 8325 mutex_exit(&DEVI(dip)->devi_lock); 8326 return (DDI_WALK_CONTINUE); 8327 } 8328 8329 /* 8330 * retire the device if constraints have been applied 8331 * or if the device is not in use 8332 */ 8333 finalize = 0; 8334 if (constraint) { 8335 ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip))); 8336 8337 ASSERT(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT); 8338 ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_BLOCKED)); 8339 DEVI(dip)->devi_flags &= ~DEVI_R_CONSTRAINT; 8340 DEVI(dip)->devi_flags &= ~DEVI_RETIRING; 8341 DEVI(dip)->devi_flags |= DEVI_RETIRED; 8342 mutex_exit(&DEVI(dip)->devi_lock); 8343 (void) spec_fence_snode(dip, NULL); 8344 RIO_DEBUG((CE_NOTE, "Fenced off: dip = %p", (void *)dip)); 8345 e_ddi_offline_finalize(dip, DDI_SUCCESS); 8346 } else { 8347 if (DEVI(dip)->devi_flags & DEVI_R_BLOCKED) { 8348 ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT)); 8349 DEVI(dip)->devi_flags &= ~DEVI_R_BLOCKED; 8350 DEVI(dip)->devi_flags &= ~DEVI_RETIRING; 8351 /* we have already finalized during notify */ 8352 } else if (DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT) { 8353 DEVI(dip)->devi_flags &= ~DEVI_R_CONSTRAINT; 8354 DEVI(dip)->devi_flags &= ~DEVI_RETIRING; 8355 finalize = 1; 8356 } else { 8357 DEVI(dip)->devi_flags &= ~DEVI_RETIRING; 8358 /* 8359 * even if no contracts, need to call finalize 8360 * to clear the contract barrier on the dip 8361 */ 8362 finalize = 1; 8363 } 8364 mutex_exit(&DEVI(dip)->devi_lock); 8365 RIO_DEBUG((CE_NOTE, "finalize: NOT retired: dip = %p", 8366 (void *)dip)); 8367 if (finalize) 8368 e_ddi_offline_finalize(dip, DDI_FAILURE); 8369 } 8370 8371 /* 8372 * phci_only variable indicates no client checking, just 8373 * offline the PHCI. We set that to 0 to enable client 8374 * checking 8375 */ 8376 phci_only = 0; 8377 if (MDI_PHCI(dip)) 8378 mdi_phci_retire_finalize(dip, phci_only, arg); 8379 8380 return (DDI_WALK_CONTINUE); 8381 } 8382 8383 /* 8384 * Returns 8385 * DDI_SUCCESS if constraints allow retire 8386 * DDI_FAILURE if constraints don't allow retire. 8387 * cons_array is a NULL terminated array of node paths for 8388 * which constraints have already been applied. 8389 */ 8390 int 8391 e_ddi_retire_device(char *path, char **cons_array) 8392 { 8393 dev_info_t *dip; 8394 dev_info_t *pdip; 8395 int circ; 8396 int circ2; 8397 int constraint; 8398 char *devnm; 8399 8400 /* 8401 * First, lookup the device 8402 */ 8403 dip = e_ddi_hold_devi_by_path(path, 0); 8404 if (dip == NULL) { 8405 /* 8406 * device does not exist. This device cannot be 8407 * a critical device since it is not in use. Thus 8408 * this device is always retireable. Return DDI_SUCCESS 8409 * to indicate this. If this device is ever 8410 * instantiated, I/O framework will consult the 8411 * the persistent retire store, mark it as 8412 * retired and fence it off. 8413 */ 8414 RIO_DEBUG((CE_NOTE, "Retire device: device doesn't exist." 8415 " NOP. Just returning SUCCESS. path=%s", path)); 8416 free_array(cons_array); 8417 return (DDI_SUCCESS); 8418 } 8419 8420 RIO_DEBUG((CE_NOTE, "Retire device: found dip = %p.", (void *)dip)); 8421 8422 pdip = ddi_get_parent(dip); 8423 ndi_hold_devi(pdip); 8424 8425 /* 8426 * Run devfs_clean() in case dip has no constraints and is 8427 * not in use, so is retireable but there are dv_nodes holding 8428 * ref-count on the dip. Note that devfs_clean() always returns 8429 * success. 8430 */ 8431 devnm = kmem_alloc(MAXNAMELEN + 1, KM_SLEEP); 8432 (void) ddi_deviname(dip, devnm); 8433 (void) devfs_clean(pdip, devnm + 1, DV_CLEAN_FORCE); 8434 kmem_free(devnm, MAXNAMELEN + 1); 8435 8436 ndi_devi_enter(pdip, &circ); 8437 8438 /* release hold from e_ddi_hold_devi_by_path */ 8439 ndi_rele_devi(dip); 8440 8441 /* 8442 * If it cannot make a determination, is_leaf_node() assumes 8443 * dip is a nexus. 8444 */ 8445 (void) e_ddi_mark_retiring(dip, cons_array); 8446 if (!is_leaf_node(dip)) { 8447 ndi_devi_enter(dip, &circ2); 8448 ddi_walk_devs(ddi_get_child(dip), e_ddi_mark_retiring, 8449 cons_array); 8450 ndi_devi_exit(dip, circ2); 8451 } 8452 free_array(cons_array); 8453 8454 /* 8455 * apply constraints 8456 */ 8457 RIO_DEBUG((CE_NOTE, "retire: subtree retire notify: path = %s", path)); 8458 8459 constraint = 1; /* assume constraints allow retire */ 8460 (void) e_ddi_retire_notify(dip, &constraint); 8461 if (!is_leaf_node(dip)) { 8462 ndi_devi_enter(dip, &circ2); 8463 ddi_walk_devs(ddi_get_child(dip), e_ddi_retire_notify, 8464 &constraint); 8465 ndi_devi_exit(dip, circ2); 8466 } 8467 8468 /* 8469 * Now finalize the retire 8470 */ 8471 (void) e_ddi_retire_finalize(dip, &constraint); 8472 if (!is_leaf_node(dip)) { 8473 ndi_devi_enter(dip, &circ2); 8474 ddi_walk_devs(ddi_get_child(dip), e_ddi_retire_finalize, 8475 &constraint); 8476 ndi_devi_exit(dip, circ2); 8477 } 8478 8479 if (!constraint) { 8480 RIO_DEBUG((CE_WARN, "retire failed: path = %s", path)); 8481 } else { 8482 RIO_DEBUG((CE_NOTE, "retire succeeded: path = %s", path)); 8483 } 8484 8485 ndi_devi_exit(pdip, circ); 8486 ndi_rele_devi(pdip); 8487 return (constraint ? DDI_SUCCESS : DDI_FAILURE); 8488 } 8489 8490 static int 8491 unmark_and_unfence(dev_info_t *dip, void *arg) 8492 { 8493 char *path = (char *)arg; 8494 8495 ASSERT(path); 8496 8497 (void) ddi_pathname(dip, path); 8498 8499 mutex_enter(&DEVI(dip)->devi_lock); 8500 DEVI(dip)->devi_flags &= ~DEVI_RETIRED; 8501 DEVI_SET_DEVICE_ONLINE(dip); 8502 mutex_exit(&DEVI(dip)->devi_lock); 8503 8504 RIO_VERBOSE((CE_NOTE, "Cleared RETIRED flag: dip=%p, path=%s", 8505 (void *)dip, path)); 8506 8507 (void) spec_unfence_snode(dip); 8508 RIO_DEBUG((CE_NOTE, "Unfenced device: %s", path)); 8509 8510 if (MDI_PHCI(dip)) 8511 mdi_phci_unretire(dip); 8512 8513 return (DDI_WALK_CONTINUE); 8514 } 8515 8516 struct find_dip { 8517 char *fd_buf; 8518 char *fd_path; 8519 dev_info_t *fd_dip; 8520 }; 8521 8522 static int 8523 find_dip_fcn(dev_info_t *dip, void *arg) 8524 { 8525 struct find_dip *findp = (struct find_dip *)arg; 8526 8527 (void) ddi_pathname(dip, findp->fd_buf); 8528 8529 if (strcmp(findp->fd_path, findp->fd_buf) != 0) 8530 return (DDI_WALK_CONTINUE); 8531 8532 ndi_hold_devi(dip); 8533 findp->fd_dip = dip; 8534 8535 return (DDI_WALK_TERMINATE); 8536 } 8537 8538 int 8539 e_ddi_unretire_device(char *path) 8540 { 8541 int circ; 8542 int circ2; 8543 char *path2; 8544 dev_info_t *pdip; 8545 dev_info_t *dip; 8546 struct find_dip find_dip; 8547 8548 ASSERT(path); 8549 ASSERT(*path == '/'); 8550 8551 if (strcmp(path, "/") == 0) { 8552 cmn_err(CE_WARN, "Root node cannot be retired. Skipping " 8553 "device unretire: %s", path); 8554 return (0); 8555 } 8556 8557 /* 8558 * We can't lookup the dip (corresponding to path) via 8559 * e_ddi_hold_devi_by_path() because the dip may be offline 8560 * and may not attach. Use ddi_walk_devs() instead; 8561 */ 8562 find_dip.fd_buf = kmem_alloc(MAXPATHLEN, KM_SLEEP); 8563 find_dip.fd_path = path; 8564 find_dip.fd_dip = NULL; 8565 8566 pdip = ddi_root_node(); 8567 8568 ndi_devi_enter(pdip, &circ); 8569 ddi_walk_devs(ddi_get_child(pdip), find_dip_fcn, &find_dip); 8570 ndi_devi_exit(pdip, circ); 8571 8572 kmem_free(find_dip.fd_buf, MAXPATHLEN); 8573 8574 if (find_dip.fd_dip == NULL) { 8575 cmn_err(CE_WARN, "Device not found in device tree. Skipping " 8576 "device unretire: %s", path); 8577 return (0); 8578 } 8579 8580 dip = find_dip.fd_dip; 8581 8582 pdip = ddi_get_parent(dip); 8583 8584 ndi_hold_devi(pdip); 8585 8586 ndi_devi_enter(pdip, &circ); 8587 8588 path2 = kmem_alloc(MAXPATHLEN, KM_SLEEP); 8589 8590 (void) unmark_and_unfence(dip, path2); 8591 if (!is_leaf_node(dip)) { 8592 ndi_devi_enter(dip, &circ2); 8593 ddi_walk_devs(ddi_get_child(dip), unmark_and_unfence, path2); 8594 ndi_devi_exit(dip, circ2); 8595 } 8596 8597 kmem_free(path2, MAXPATHLEN); 8598 8599 /* release hold from find_dip_fcn() */ 8600 ndi_rele_devi(dip); 8601 8602 ndi_devi_exit(pdip, circ); 8603 8604 ndi_rele_devi(pdip); 8605 8606 return (0); 8607 } 8608 8609 /* 8610 * Called before attach on a dip that has been retired. 8611 */ 8612 static int 8613 mark_and_fence(dev_info_t *dip, void *arg) 8614 { 8615 char *fencepath = (char *)arg; 8616 8617 /* 8618 * We have already decided to retire this device. The various 8619 * constraint checking should not be set. 8620 * NOTE that the retire flag may already be set due to 8621 * fenced -> detach -> fenced transitions. 8622 */ 8623 mutex_enter(&DEVI(dip)->devi_lock); 8624 ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT)); 8625 ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_BLOCKED)); 8626 ASSERT(!(DEVI(dip)->devi_flags & DEVI_RETIRING)); 8627 DEVI(dip)->devi_flags |= DEVI_RETIRED; 8628 mutex_exit(&DEVI(dip)->devi_lock); 8629 RIO_VERBOSE((CE_NOTE, "marked as RETIRED dip=%p", (void *)dip)); 8630 8631 if (fencepath) { 8632 (void) spec_fence_snode(dip, NULL); 8633 RIO_DEBUG((CE_NOTE, "Fenced: %s", 8634 ddi_pathname(dip, fencepath))); 8635 } 8636 8637 return (DDI_WALK_CONTINUE); 8638 } 8639 8640 /* 8641 * Checks the retire database and: 8642 * 8643 * - if device is present in the retire database, marks the device retired 8644 * and fences it off. 8645 * - if device is not in retire database, allows the device to attach normally 8646 * 8647 * To be called only by framework attach code on first attach attempt. 8648 * 8649 */ 8650 static int 8651 i_ddi_check_retire(dev_info_t *dip) 8652 { 8653 char *path; 8654 dev_info_t *pdip; 8655 int circ; 8656 int phci_only; 8657 int constraint; 8658 8659 pdip = ddi_get_parent(dip); 8660 8661 /* 8662 * Root dip is treated special and doesn't take this code path. 8663 * Also root can never be retired. 8664 */ 8665 ASSERT(pdip); 8666 ASSERT(DEVI_BUSY_OWNED(pdip)); 8667 ASSERT(i_ddi_node_state(dip) < DS_ATTACHED); 8668 8669 path = kmem_alloc(MAXPATHLEN, KM_SLEEP); 8670 8671 (void) ddi_pathname(dip, path); 8672 8673 RIO_VERBOSE((CE_NOTE, "Checking if dip should attach: dip=%p, path=%s", 8674 (void *)dip, path)); 8675 8676 /* 8677 * Check if this device is in the "retired" store i.e. should 8678 * be retired. If not, we have nothing to do. 8679 */ 8680 if (e_ddi_device_retired(path) == 0) { 8681 RIO_VERBOSE((CE_NOTE, "device is NOT retired: path=%s", path)); 8682 if (DEVI(dip)->devi_flags & DEVI_RETIRED) 8683 (void) e_ddi_unretire_device(path); 8684 kmem_free(path, MAXPATHLEN); 8685 return (0); 8686 } 8687 8688 RIO_DEBUG((CE_NOTE, "attach: device is retired: path=%s", path)); 8689 8690 /* 8691 * Mark dips and fence off snodes (if any) 8692 */ 8693 RIO_DEBUG((CE_NOTE, "attach: Mark and fence subtree: path=%s", path)); 8694 (void) mark_and_fence(dip, path); 8695 if (!is_leaf_node(dip)) { 8696 ndi_devi_enter(dip, &circ); 8697 ddi_walk_devs(ddi_get_child(dip), mark_and_fence, path); 8698 ndi_devi_exit(dip, circ); 8699 } 8700 8701 kmem_free(path, MAXPATHLEN); 8702 8703 /* 8704 * We don't want to check the client. We just want to 8705 * offline the PHCI 8706 */ 8707 phci_only = 1; 8708 constraint = 1; 8709 if (MDI_PHCI(dip)) 8710 mdi_phci_retire_finalize(dip, phci_only, &constraint); 8711 return (1); 8712 } 8713 8714 8715 #define VAL_ALIAS(array, x) (strlen(array[x].pair_alias)) 8716 #define VAL_CURR(array, x) (strlen(array[x].pair_curr)) 8717 #define SWAP(array, x, y) \ 8718 { \ 8719 alias_pair_t tmpair = array[x]; \ 8720 array[x] = array[y]; \ 8721 array[y] = tmpair; \ 8722 } 8723 8724 static int 8725 partition_curr(alias_pair_t *array, int start, int end) 8726 { 8727 int i = start - 1; 8728 int j = end + 1; 8729 int pivot = start; 8730 8731 for (;;) { 8732 do { 8733 j--; 8734 } while (VAL_CURR(array, j) > VAL_CURR(array, pivot)); 8735 8736 do { 8737 i++; 8738 } while (VAL_CURR(array, i) < VAL_CURR(array, pivot)); 8739 8740 if (i < j) 8741 SWAP(array, i, j) 8742 else 8743 return (j); 8744 } 8745 } 8746 8747 static int 8748 partition_aliases(alias_pair_t *array, int start, int end) 8749 { 8750 int i = start - 1; 8751 int j = end + 1; 8752 int pivot = start; 8753 8754 for (;;) { 8755 do { 8756 j--; 8757 } while (VAL_ALIAS(array, j) > VAL_ALIAS(array, pivot)); 8758 8759 do { 8760 i++; 8761 } while (VAL_ALIAS(array, i) < VAL_ALIAS(array, pivot)); 8762 8763 if (i < j) 8764 SWAP(array, i, j) 8765 else 8766 return (j); 8767 } 8768 } 8769 static void 8770 sort_alias_pairs(alias_pair_t *array, int start, int end) 8771 { 8772 int mid; 8773 8774 if (start < end) { 8775 mid = partition_aliases(array, start, end); 8776 sort_alias_pairs(array, start, mid); 8777 sort_alias_pairs(array, mid + 1, end); 8778 } 8779 } 8780 8781 static void 8782 sort_curr_pairs(alias_pair_t *array, int start, int end) 8783 { 8784 int mid; 8785 8786 if (start < end) { 8787 mid = partition_curr(array, start, end); 8788 sort_curr_pairs(array, start, mid); 8789 sort_curr_pairs(array, mid + 1, end); 8790 } 8791 } 8792 8793 static void 8794 create_sorted_pairs(plat_alias_t *pali, int npali) 8795 { 8796 int i; 8797 int j; 8798 int k; 8799 int count; 8800 8801 count = 0; 8802 for (i = 0; i < npali; i++) { 8803 count += pali[i].pali_naliases; 8804 } 8805 8806 ddi_aliases.dali_alias_pairs = kmem_zalloc( 8807 (sizeof (alias_pair_t)) * count, KM_NOSLEEP); 8808 if (ddi_aliases.dali_alias_pairs == NULL) { 8809 cmn_err(CE_PANIC, "alias path-pair alloc failed"); 8810 /*NOTREACHED*/ 8811 } 8812 8813 ddi_aliases.dali_curr_pairs = kmem_zalloc( 8814 (sizeof (alias_pair_t)) * count, KM_NOSLEEP); 8815 if (ddi_aliases.dali_curr_pairs == NULL) { 8816 cmn_err(CE_PANIC, "curr path-pair alloc failed"); 8817 /*NOTREACHED*/ 8818 } 8819 8820 for (i = 0, k = 0; i < npali; i++) { 8821 for (j = 0; j < pali[i].pali_naliases; j++, k++) { 8822 ddi_aliases.dali_alias_pairs[k].pair_curr = 8823 ddi_aliases.dali_curr_pairs[k].pair_curr = 8824 pali[i].pali_current; 8825 ddi_aliases.dali_alias_pairs[k].pair_alias = 8826 ddi_aliases.dali_curr_pairs[k].pair_alias = 8827 pali[i].pali_aliases[j]; 8828 } 8829 } 8830 8831 ASSERT(k == count); 8832 8833 ddi_aliases.dali_num_pairs = count; 8834 8835 /* Now sort the array based on length of pair_alias */ 8836 sort_alias_pairs(ddi_aliases.dali_alias_pairs, 0, count - 1); 8837 sort_curr_pairs(ddi_aliases.dali_curr_pairs, 0, count - 1); 8838 } 8839 8840 void 8841 ddi_register_aliases(plat_alias_t *pali, uint64_t npali) 8842 { 8843 8844 ASSERT((pali == NULL) ^ (npali != 0)); 8845 8846 if (npali == 0) { 8847 ddi_err(DER_PANIC, NULL, "npali == 0"); 8848 /*NOTREACHED*/ 8849 } 8850 8851 if (ddi_aliases_present == B_TRUE) { 8852 ddi_err(DER_PANIC, NULL, "multiple init"); 8853 /*NOTREACHED*/ 8854 } 8855 8856 ddi_aliases.dali_alias_TLB = mod_hash_create_strhash( 8857 "ddi-alias-tlb", DDI_ALIAS_HASH_SIZE, mod_hash_null_valdtor); 8858 if (ddi_aliases.dali_alias_TLB == NULL) { 8859 ddi_err(DER_PANIC, NULL, "alias TLB hash alloc failed"); 8860 /*NOTREACHED*/ 8861 } 8862 8863 ddi_aliases.dali_curr_TLB = mod_hash_create_strhash( 8864 "ddi-curr-tlb", DDI_ALIAS_HASH_SIZE, mod_hash_null_valdtor); 8865 if (ddi_aliases.dali_curr_TLB == NULL) { 8866 ddi_err(DER_PANIC, NULL, "curr TLB hash alloc failed"); 8867 /*NOTREACHED*/ 8868 } 8869 8870 create_sorted_pairs(pali, npali); 8871 8872 tsd_create(&tsd_ddi_redirect, NULL); 8873 8874 ddi_aliases_present = B_TRUE; 8875 } 8876 8877 static dev_info_t * 8878 path_to_dip(char *path) 8879 { 8880 dev_info_t *currdip; 8881 int error; 8882 char *pdup; 8883 8884 pdup = ddi_strdup(path, KM_NOSLEEP); 8885 if (pdup == NULL) { 8886 cmn_err(CE_PANIC, "path strdup failed: %s", path); 8887 /*NOTREACHED*/ 8888 } 8889 8890 error = resolve_pathname(pdup, &currdip, NULL, NULL); 8891 8892 kmem_free(pdup, strlen(path) + 1); 8893 8894 return (error ? NULL : currdip); 8895 } 8896 8897 dev_info_t * 8898 ddi_alias_to_currdip(char *alias, int i) 8899 { 8900 alias_pair_t *pair; 8901 char *curr; 8902 dev_info_t *currdip = NULL; 8903 char *aliasdup; 8904 int rv, len; 8905 8906 pair = &(ddi_aliases.dali_alias_pairs[i]); 8907 len = strlen(pair->pair_alias); 8908 8909 curr = NULL; 8910 aliasdup = ddi_strdup(alias, KM_NOSLEEP); 8911 if (aliasdup == NULL) { 8912 cmn_err(CE_PANIC, "aliasdup alloc failed"); 8913 /*NOTREACHED*/ 8914 } 8915 8916 if (strncmp(alias, pair->pair_alias, len) != 0) 8917 goto out; 8918 8919 if (alias[len] != '/' && alias[len] != '\0') 8920 goto out; 8921 8922 curr = kmem_alloc(MAXPATHLEN, KM_NOSLEEP); 8923 if (curr == NULL) { 8924 cmn_err(CE_PANIC, "curr alloc failed"); 8925 /*NOTREACHED*/ 8926 } 8927 (void) strlcpy(curr, pair->pair_curr, MAXPATHLEN); 8928 if (alias[len] == '/') { 8929 (void) strlcat(curr, "/", MAXPATHLEN); 8930 (void) strlcat(curr, &alias[len + 1], MAXPATHLEN); 8931 } 8932 8933 currdip = path_to_dip(curr); 8934 8935 out: 8936 if (currdip) { 8937 rv = mod_hash_insert(ddi_aliases.dali_alias_TLB, 8938 (mod_hash_key_t)aliasdup, (mod_hash_val_t)curr); 8939 if (rv != 0) { 8940 kmem_free(curr, MAXPATHLEN); 8941 strfree(aliasdup); 8942 } 8943 } else { 8944 rv = mod_hash_insert(ddi_aliases.dali_alias_TLB, 8945 (mod_hash_key_t)aliasdup, (mod_hash_val_t)NULL); 8946 if (rv != 0) { 8947 strfree(aliasdup); 8948 } 8949 if (curr) 8950 kmem_free(curr, MAXPATHLEN); 8951 } 8952 8953 return (currdip); 8954 } 8955 8956 char * 8957 ddi_curr_to_alias(char *curr, int i) 8958 { 8959 alias_pair_t *pair; 8960 char *alias; 8961 char *currdup; 8962 int len; 8963 int rv; 8964 8965 pair = &(ddi_aliases.dali_curr_pairs[i]); 8966 8967 len = strlen(pair->pair_curr); 8968 8969 alias = NULL; 8970 8971 currdup = ddi_strdup(curr, KM_NOSLEEP); 8972 if (currdup == NULL) { 8973 cmn_err(CE_PANIC, "currdup alloc failed"); 8974 /*NOTREACHED*/ 8975 } 8976 8977 if (strncmp(curr, pair->pair_curr, len) != 0) 8978 goto out; 8979 8980 if (curr[len] != '/' && curr[len] != '\0') 8981 goto out; 8982 8983 alias = kmem_alloc(MAXPATHLEN, KM_NOSLEEP); 8984 if (alias == NULL) { 8985 cmn_err(CE_PANIC, "alias alloc failed"); 8986 /*NOTREACHED*/ 8987 } 8988 8989 (void) strlcpy(alias, pair->pair_alias, MAXPATHLEN); 8990 if (curr[len] == '/') { 8991 (void) strlcat(alias, "/", MAXPATHLEN); 8992 (void) strlcat(alias, &curr[len + 1], MAXPATHLEN); 8993 } 8994 8995 if (e_ddi_path_to_instance(alias) == NULL) { 8996 kmem_free(alias, MAXPATHLEN); 8997 alias = NULL; 8998 } 8999 9000 out: 9001 rv = mod_hash_insert(ddi_aliases.dali_curr_TLB, 9002 (mod_hash_key_t)currdup, (mod_hash_val_t)alias); 9003 if (rv != 0) { 9004 strfree(currdup); 9005 } 9006 9007 return (alias); 9008 } 9009 9010 dev_info_t * 9011 ddi_alias_redirect(char *alias) 9012 { 9013 char *curr; 9014 dev_info_t *currdip; 9015 int i; 9016 9017 if (ddi_aliases_present == B_FALSE) 9018 return (NULL); 9019 9020 if (tsd_get(tsd_ddi_redirect)) 9021 return (NULL); 9022 9023 (void) tsd_set(tsd_ddi_redirect, (void *)1); 9024 9025 ASSERT(ddi_aliases.dali_alias_TLB); 9026 ASSERT(ddi_aliases.dali_alias_pairs); 9027 9028 curr = NULL; 9029 if (mod_hash_find(ddi_aliases.dali_alias_TLB, 9030 (mod_hash_key_t)alias, (mod_hash_val_t *)&curr) == 0) { 9031 currdip = curr ? path_to_dip(curr) : NULL; 9032 goto out; 9033 } 9034 9035 /* The TLB has no translation, do it the hard way */ 9036 currdip = NULL; 9037 for (i = ddi_aliases.dali_num_pairs - 1; i >= 0; i--) { 9038 currdip = ddi_alias_to_currdip(alias, i); 9039 if (currdip) 9040 break; 9041 } 9042 out: 9043 (void) tsd_set(tsd_ddi_redirect, NULL); 9044 9045 return (currdip); 9046 } 9047 9048 char * 9049 ddi_curr_redirect(char *curr) 9050 { 9051 char *alias; 9052 int i; 9053 9054 if (ddi_aliases_present == B_FALSE) 9055 return (NULL); 9056 9057 if (tsd_get(tsd_ddi_redirect)) 9058 return (NULL); 9059 9060 (void) tsd_set(tsd_ddi_redirect, (void *)1); 9061 9062 ASSERT(ddi_aliases.dali_curr_TLB); 9063 ASSERT(ddi_aliases.dali_curr_pairs); 9064 9065 alias = NULL; 9066 if (mod_hash_find(ddi_aliases.dali_curr_TLB, 9067 (mod_hash_key_t)curr, (mod_hash_val_t *)&alias) == 0) { 9068 goto out; 9069 } 9070 9071 9072 /* The TLB has no translation, do it the slow way */ 9073 alias = NULL; 9074 for (i = ddi_aliases.dali_num_pairs - 1; i >= 0; i--) { 9075 alias = ddi_curr_to_alias(curr, i); 9076 if (alias) 9077 break; 9078 } 9079 9080 out: 9081 (void) tsd_set(tsd_ddi_redirect, NULL); 9082 9083 return (alias); 9084 } 9085 9086 void 9087 ddi_err(ddi_err_t ade, dev_info_t *rdip, const char *fmt, ...) 9088 { 9089 va_list ap; 9090 char strbuf[256]; 9091 char *buf; 9092 size_t buflen, tlen; 9093 int ce; 9094 int de; 9095 const char *fmtbad = "Invalid arguments to ddi_err()"; 9096 9097 de = DER_CONT; 9098 strbuf[1] = '\0'; 9099 9100 switch (ade) { 9101 case DER_CONS: 9102 strbuf[0] = '^'; 9103 break; 9104 case DER_LOG: 9105 strbuf[0] = '!'; 9106 break; 9107 case DER_VERB: 9108 strbuf[0] = '?'; 9109 break; 9110 default: 9111 strbuf[0] = '\0'; 9112 de = ade; 9113 break; 9114 } 9115 9116 tlen = strlen(strbuf); 9117 buf = strbuf + tlen; 9118 buflen = sizeof (strbuf) - tlen; 9119 9120 if (rdip && ddi_get_instance(rdip) == -1) { 9121 (void) snprintf(buf, buflen, "%s: ", 9122 ddi_driver_name(rdip)); 9123 } else if (rdip) { 9124 (void) snprintf(buf, buflen, "%s%d: ", 9125 ddi_driver_name(rdip), ddi_get_instance(rdip)); 9126 } 9127 9128 tlen = strlen(strbuf); 9129 buf = strbuf + tlen; 9130 buflen = sizeof (strbuf) - tlen; 9131 9132 va_start(ap, fmt); 9133 switch (de) { 9134 case DER_CONT: 9135 (void) vsnprintf(buf, buflen, fmt, ap); 9136 if (ade != DER_CONT) { 9137 (void) strlcat(strbuf, "\n", sizeof (strbuf)); 9138 } 9139 ce = CE_CONT; 9140 break; 9141 case DER_NOTE: 9142 (void) vsnprintf(buf, buflen, fmt, ap); 9143 ce = CE_NOTE; 9144 break; 9145 case DER_WARN: 9146 (void) vsnprintf(buf, buflen, fmt, ap); 9147 ce = CE_WARN; 9148 break; 9149 case DER_MODE: 9150 (void) vsnprintf(buf, buflen, fmt, ap); 9151 if (ddi_err_panic == B_TRUE) { 9152 ce = CE_PANIC; 9153 } else { 9154 ce = CE_WARN; 9155 } 9156 break; 9157 case DER_DEBUG: 9158 (void) snprintf(buf, buflen, "DEBUG: "); 9159 tlen = strlen("DEBUG: "); 9160 (void) vsnprintf(buf + tlen, buflen - tlen, fmt, ap); 9161 ce = CE_CONT; 9162 break; 9163 case DER_PANIC: 9164 (void) vsnprintf(buf, buflen, fmt, ap); 9165 ce = CE_PANIC; 9166 break; 9167 case DER_INVALID: 9168 default: 9169 (void) snprintf(buf, buflen, fmtbad); 9170 tlen = strlen(fmtbad); 9171 (void) vsnprintf(buf + tlen, buflen - tlen, fmt, ap); 9172 ce = CE_PANIC; 9173 break; 9174 } 9175 va_end(ap); 9176 9177 cmn_err(ce, strbuf); 9178 } 9179 9180 /*ARGSUSED*/ 9181 void 9182 ddi_mem_update(uint64_t addr, uint64_t size) 9183 { 9184 #if defined(__x86) && !defined(__xpv) 9185 extern void immu_physmem_update(uint64_t addr, uint64_t size); 9186 immu_physmem_update(addr, size); 9187 #else 9188 /*LINTED*/ 9189 ; 9190 #endif 9191 } 9192