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