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