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