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