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