1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright (c) 1992, 2010, Oracle and/or its affiliates. All rights reserved. 23 */ 24 25 /* 26 * Instance number assignment code 27 */ 28 29 #include <sys/types.h> 30 #include <sys/param.h> 31 #include <sys/errno.h> 32 #include <sys/systm.h> 33 #include <sys/kobj.h> 34 #include <sys/t_lock.h> 35 #include <sys/kmem.h> 36 #include <sys/cmn_err.h> 37 #include <sys/ddi.h> 38 #include <sys/sunddi.h> 39 #include <sys/autoconf.h> 40 #include <sys/systeminfo.h> 41 #include <sys/hwconf.h> 42 #include <sys/reboot.h> 43 #include <sys/ddi_impldefs.h> 44 #include <sys/instance.h> 45 #include <sys/debug.h> 46 #include <sys/sysevent.h> 47 #include <sys/modctl.h> 48 #include <sys/console.h> 49 #include <sys/cladm.h> 50 #include <sys/sysmacros.h> 51 #include <sys/crc32.h> 52 53 54 static void in_preassign_instance(void); 55 static void i_log_devfs_instance_mod(void); 56 static int in_get_infile(char *); 57 static void in_removenode(struct devnames *dnp, in_node_t *mp, in_node_t *ap); 58 static in_node_t *in_alloc_node(char *name, char *addr); 59 static int in_eqstr(char *a, char *b); 60 static char *in_name_addr(char **cpp, char **addrp); 61 static in_node_t *in_devwalk(dev_info_t *dip, in_node_t **ap, char *addr); 62 static void in_dealloc_node(in_node_t *np); 63 static in_node_t *in_make_path(char *path); 64 static void in_enlist(in_node_t *ap, in_node_t *np); 65 static int in_inuse(int instance, char *name); 66 static void in_hashdrv(in_drv_t *dp); 67 static in_drv_t *in_drvwalk(in_node_t *np, char *binding_name); 68 static in_drv_t *in_alloc_drv(char *bindingname); 69 static void in_endrv(in_node_t *np, in_drv_t *dp); 70 static void in_dq_drv(in_drv_t *np); 71 static void in_removedrv(struct devnames *dnp, in_drv_t *mp); 72 static int in_pathin(char *cp, int instance, char *bname, struct bind **args); 73 static int in_next_instance_block(major_t, int); 74 static int in_next_instance(major_t); 75 76 #pragma weak plat_ioaliases_init 77 78 79 /* external functions */ 80 extern char *i_binding_to_drv_name(char *bname); 81 extern void plat_ioaliases_init(void); 82 83 /* 84 * This plus devnames defines the entire software state of the instance world. 85 */ 86 typedef struct in_softstate { 87 in_node_t *ins_root; /* the root of our instance tree */ 88 in_drv_t *ins_no_major; /* majorless drv entries */ 89 /* 90 * Used to serialize access to data structures 91 */ 92 void *ins_thread; 93 kmutex_t ins_serial; 94 kcondvar_t ins_serial_cv; 95 int ins_busy; 96 boolean_t ins_dirty; /* instance info needs flush */ 97 } in_softstate_t; 98 99 static in_softstate_t e_ddi_inst_state; 100 101 /* 102 * State transition information: 103 * e_ddi_inst_state contains, among other things, the root of a tree of 104 * device nodes used to track instance number assignments. 105 * Each device node may contain multiple driver bindings, represented 106 * by a linked list of in_drv_t nodes, each with an instance assignment 107 * (except for root node). Each in_drv node can be in one of 3 states, 108 * indicated by ind_state: 109 * 110 * IN_UNKNOWN: Each node created in this state. The instance number of 111 * this node is not known. ind_instance is set to -1. 112 * IN_PROVISIONAL: When a node is assigned an instance number in 113 * e_ddi_assign_instance(), its state is set to IN_PROVISIONAL. 114 * Subsequently, the framework will always call either 115 * e_ddi_keep_instance() which makes the node IN_PERMANENT 116 * or e_ddi_free_instance(), which deletes the node. 117 * IN_PERMANENT: 118 * If e_ddi_keep_instance() is called on an IN_PROVISIONAL node, 119 * its state is set to IN_PERMANENT. 120 */ 121 122 static char *instance_file = INSTANCE_FILE; 123 static char *instance_file_backup = INSTANCE_FILE INSTANCE_FILE_SUFFIX; 124 125 /* 126 * Return values for in_get_infile(). 127 */ 128 #define PTI_FOUND 0 129 #define PTI_NOT_FOUND 1 130 #define PTI_REBUILD 2 131 132 133 /* 134 * Path to instance file magic string used for first time boot after 135 * an install. If this is the first string in the file we will 136 * automatically rebuild the file. 137 */ 138 #define PTI_MAGIC_STR "#path_to_inst_bootstrap_1" 139 #define PTI_MAGIC_STR_LEN (sizeof (PTI_MAGIC_STR) - 1) 140 141 void 142 e_ddi_instance_init(void) 143 { 144 char *file; 145 int rebuild = 1; 146 struct in_drv *dp; 147 148 mutex_init(&e_ddi_inst_state.ins_serial, NULL, MUTEX_DEFAULT, NULL); 149 cv_init(&e_ddi_inst_state.ins_serial_cv, NULL, CV_DEFAULT, NULL); 150 151 /* 152 * Only one thread is allowed to change the state of the instance 153 * number assignments on the system at any given time. 154 * Note that this is not really necessary, as we are single-threaded 155 * here, but it won't hurt, and it allows us to keep ASSERTS for 156 * our assumptions in the code. 157 */ 158 e_ddi_enter_instance(); 159 160 /* 161 * Init the ioaliases if the platform supports it 162 */ 163 if (&plat_ioaliases_init) 164 plat_ioaliases_init(); 165 166 /* 167 * Create the root node, instance zallocs to 0. 168 * The name and address of this node never get examined, we always 169 * start searching with its first child. 170 */ 171 ASSERT(e_ddi_inst_state.ins_root == NULL); 172 e_ddi_inst_state.ins_root = in_alloc_node(NULL, NULL); 173 dp = in_alloc_drv("rootnex"); 174 in_endrv(e_ddi_inst_state.ins_root, dp); 175 176 file = instance_file; 177 switch (in_get_infile(file)) { 178 default: 179 case PTI_NOT_FOUND: 180 /* make sure path_to_inst is recreated */ 181 boothowto |= RB_RECONFIG; 182 183 /* 184 * Something is wrong. First try the backup file. 185 * If not found, rebuild path_to_inst. Emit a 186 * message about the problem. 187 */ 188 cmn_err(CE_WARN, "%s empty or not found", file); 189 190 file = instance_file_backup; 191 if (in_get_infile(file) != PTI_FOUND) { 192 cmn_err(CE_NOTE, "rebuilding device instance data"); 193 break; 194 } 195 cmn_err(CE_NOTE, "using backup instance data in %s", file); 196 /*FALLTHROUGH*/ 197 198 case PTI_FOUND: 199 /* 200 * We've got a readable file 201 * parse the file into the instance tree 202 */ 203 (void) read_binding_file(file, NULL, in_pathin); 204 rebuild = 0; 205 break; 206 207 case PTI_REBUILD: 208 /* 209 * path_to_inst has magic str requesting a create 210 * Convert boot to reconfig boot to ensure /dev is 211 * in sync with new path_to_inst. 212 */ 213 boothowto |= RB_RECONFIG; 214 cmn_err(CE_CONT, 215 "?Using default device instance data\n"); 216 break; 217 } 218 219 /* 220 * The OBP device tree has been copied to the kernel and 221 * bound to drivers at this point. We walk the per-driver 222 * list to preassign instances. Since the bus addr is 223 * unknown at this point, we cannot place the instance 224 * number in the instance tree. This will be done at 225 * a later time. 226 */ 227 if (rebuild) 228 in_preassign_instance(); 229 230 e_ddi_exit_instance(); 231 } 232 233 static void 234 in_preassign_instance() 235 { 236 major_t m; 237 extern major_t devcnt; 238 239 for (m = 0; m < devcnt; m++) { 240 struct devnames *dnp = &devnamesp[m]; 241 dev_info_t *dip = dnp->dn_head; 242 while (dip) { 243 DEVI(dip)->devi_instance = dnp->dn_instance; 244 dnp->dn_instance++; 245 dip = ddi_get_next(dip); 246 } 247 } 248 } 249 250 /* 251 * Checks to see if the /etc/path_to_inst file exists and whether or not 252 * it has the magic string in it. 253 * 254 * Returns one of the following: 255 * 256 * PTI_FOUND - We have found the /etc/path_to_inst file 257 * PTI_REBUILD - We have found the /etc/path_to_inst file and the 258 * first line was PTI_MAGIC_STR. 259 * PTI_NOT_FOUND - We did not find the /etc/path_to_inst file 260 * 261 */ 262 static int 263 in_get_infile(char *filename) 264 { 265 struct _buf *file; 266 int return_val; 267 char buf[PTI_MAGIC_STR_LEN]; 268 269 /* 270 * Try to open the file. 271 */ 272 if ((file = kobj_open_file(filename)) == (struct _buf *)-1) { 273 return (PTI_NOT_FOUND); 274 } 275 return_val = PTI_FOUND; 276 277 /* 278 * Read the first PTI_MAGIC_STR_LEN bytes from the file to see if 279 * it contains the magic string. If there aren't that many bytes 280 * in the file, then assume file is correct and no magic string 281 * and move on. 282 */ 283 switch (kobj_read_file(file, buf, PTI_MAGIC_STR_LEN, 0)) { 284 285 case PTI_MAGIC_STR_LEN: 286 /* 287 * If the first PTI_MAGIC_STR_LEN bytes are the magic string 288 * then return PTI_REBUILD. 289 */ 290 if (strncmp(PTI_MAGIC_STR, buf, PTI_MAGIC_STR_LEN) == 0) 291 return_val = PTI_REBUILD; 292 break; 293 294 case 0: 295 /* 296 * If the file is zero bytes in length, then consider the 297 * file to not be found 298 */ 299 return_val = PTI_NOT_FOUND; 300 301 default: /* Do nothing we have a good file */ 302 break; 303 } 304 305 kobj_close_file(file); 306 return (return_val); 307 } 308 309 int 310 is_pseudo_device(dev_info_t *dip) 311 { 312 dev_info_t *pdip; 313 314 for (pdip = ddi_get_parent(dip); pdip && pdip != ddi_root_node(); 315 pdip = ddi_get_parent(pdip)) { 316 if (strcmp(ddi_get_name(pdip), DEVI_PSEUDO_NEXNAME) == 0) 317 return (1); 318 } 319 return (0); 320 } 321 322 323 static void 324 in_set_instance(dev_info_t *dip, in_drv_t *dp, major_t major) 325 { 326 /* use preassigned instance if available */ 327 if (DEVI(dip)->devi_instance != -1) 328 dp->ind_instance = DEVI(dip)->devi_instance; 329 else 330 dp->ind_instance = in_next_instance(major); 331 } 332 333 /* 334 * Return 1 if instance block was assigned for the path. 335 * 336 * For multi-port NIC cards, sequential instance assignment across all 337 * ports on a card is highly desirable since the ppa is typically the 338 * same as the instance number, and the ppa is used in the NIC's public 339 * /dev name. This sequential assignment typically occurs as a result 340 * of in_preassign_instance() after initial install, or by 341 * i_ndi_init_hw_children() for NIC ports that share a common parent. 342 * 343 * Some NIC cards however use multi-function bridge chips, and to 344 * support sequential instance assignment accross all ports, without 345 * disabling multi-threaded attach, we have a (currently) undocumented 346 * hack to allocate instance numbers in contiguous blocks based on 347 * driver.conf properties. 348 * 349 * ^ 350 * /---------- ------------\ 351 * pci@0 pci@0,1 MULTI-FUNCTION BRIDGE CHIP 352 * / \ / \ 353 * FJSV,e4ta@4 FJSV,e4ta@4,1 FJSV,e4ta@6 FJSV,e4ta@6,1 NIC PORTS 354 * n n+2 n+2 n+3 INSTANCE 355 * 356 * For the above example, the following driver.conf properties would be 357 * used to guarantee sequential instance number assignment. 358 * 359 * ddi-instance-blocks ="ib-FJSVe4ca", "ib-FJSVe4ta", "ib-generic"; 360 * ib-FJSVe4ca = "/pci@0/FJSV,e4ca@4", "/pci@0/FJSV,e4ca@4,1", 361 * "/pci@0,1/FJSV,e4ca@6", "/pci@0,1/FJSV,e4ca@6,1"; 362 * ib-FJSVe4ta = "/pci@0/FJSV,e4ta@4", "/pci@0/FJSV,e4ta@4,1", 363 * "/pci@0,1/FJSV,e4ta@6", "/pci@0,1/FJSV,e4ta@6,1"; 364 * ib-generic = "/pci@0/network@4", "/pci@0/network@4,1", 365 * "/pci@0,1/network@6", "/pci@0,1/network@6,1"; 366 * 367 * The value of the 'ddi-instance-blocks' property references a series 368 * of card specific properties, like 'ib-FJSV-e4ta', who's value 369 * defines a single 'instance block'. The 'instance block' describes 370 * all the paths below a multi-function bridge, where each path is 371 * called an 'instance path'. The 'instance block' property value is a 372 * series of 'instance paths'. The number of 'instance paths' in an 373 * 'instance block' defines the size of the instance block, and the 374 * ordering of the 'instance paths' defines the instance number 375 * assignment order for paths going through the 'instance block'. 376 * 377 * In the instance assignment code below, if a (path, driver) that 378 * currently has no instance number has a path that goes through an 379 * 'instance block', then block instance number allocation occurs. The 380 * block allocation code will find a sequential set of unused instance 381 * numbers, and assign instance numbers for all the paths in the 382 * 'instance block'. Each path is assigned a persistent instance 383 * number, even paths that don't exist in the device tree or fail 384 * probe(9E). 385 */ 386 static int 387 in_assign_instance_block(dev_info_t *dip) 388 { 389 char **ibn; /* instance block names */ 390 uint_t nibn; /* number of instance block names */ 391 uint_t ibni; /* ibn index */ 392 char *driver; 393 major_t major; 394 char *path; 395 char *addr; 396 int plen; 397 char **ibp; /* instance block paths */ 398 uint_t nibp; /* number of paths in instance block */ 399 uint_t ibpi; /* ibp index */ 400 int ibplen; /* length of instance block path */ 401 char *ipath; 402 int instance_base; 403 int splice; 404 int i; 405 406 /* check for fresh install case (in miniroot) */ 407 if (DEVI(dip)->devi_instance != -1) 408 return (0); /* already assigned */ 409 410 /* 411 * Check to see if we need to allocate a block of contiguous instance 412 * numbers by looking for the 'ddi-instance-blocks' property. 413 */ 414 if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, 415 "ddi-instance-blocks", &ibn, &nibn) != DDI_SUCCESS) 416 return (0); /* no instance block needed */ 417 418 /* 419 * Get information out about node we are processing. 420 * 421 * NOTE: Since the node is not yet at DS_INITIALIZED, ddi_pathname() 422 * will not return the unit-address of the final path component even 423 * though the node has an established devi_addr unit-address - so we 424 * need to add the unit-address by hand. 425 */ 426 driver = (char *)ddi_driver_name(dip); 427 major = ddi_driver_major(dip); 428 path = kmem_alloc(MAXPATHLEN, KM_SLEEP); 429 (void) ddi_pathname(dip, path); 430 if ((addr = ddi_get_name_addr(dip)) != NULL) { 431 (void) strcat(path, "@"); 432 (void) strcat(path, addr); 433 } 434 plen = strlen(path); 435 436 /* loop through instance block names */ 437 for (ibni = 0; ibni < nibn; ibni++) { 438 if (ibn[ibni] == NULL) 439 continue; 440 441 /* lookup instance block */ 442 if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, dip, 443 DDI_PROP_DONTPASS, ibn[ibni], 444 &ibp, &nibp) != DDI_SUCCESS) { 445 cmn_err(CE_WARN, 446 "no devinition for instance block '%s' in %s.conf", 447 ibn[ibni], driver); 448 continue; 449 } 450 451 /* Does 'path' go through this instance block? */ 452 for (ibpi = 0; ibpi < nibp; ibpi++) { 453 if (ibp[ibpi] == NULL) 454 continue; 455 ibplen = strlen(ibp[ibpi]); 456 if ((ibplen <= plen) && 457 (strcmp(ibp[ibpi], path + plen - ibplen) == 0)) 458 break; 459 460 } 461 if (ibpi >= nibp) { 462 ddi_prop_free(ibp); 463 continue; /* no try next instance block */ 464 } 465 466 /* yes, allocate and assign instances for all paths in block */ 467 468 /* 469 * determine where we splice in instance paths and verify 470 * that none of the paths are too long. 471 */ 472 splice = plen - ibplen; 473 for (i = 0; i < nibp; i++) { 474 if ((splice + strlen(ibp[i])+ 1) >= MAXPATHLEN) { 475 cmn_err(CE_WARN, 476 "path %d through instance block '%s' from " 477 "%s.conf too long", i, ibn[ibni], driver); 478 break; 479 } 480 } 481 if (i < nibp) { 482 ddi_prop_free(ibp); 483 continue; /* too long */ 484 } 485 486 /* allocate the instance block - no more failures */ 487 instance_base = in_next_instance_block(major, nibp); 488 489 ipath = kmem_alloc(MAXPATHLEN, KM_SLEEP); 490 for (ibpi = 0; ibpi < nibp; ibpi++) { 491 if (ibp[ibpi] == NULL) 492 continue; 493 (void) strcpy(ipath, path); 494 (void) strcpy(ipath + splice, ibp[ibpi]); 495 (void) in_pathin(ipath, 496 instance_base + ibpi, driver, NULL); 497 } 498 499 /* free allocations */ 500 kmem_free(ipath, MAXPATHLEN); 501 ddi_prop_free(ibp); 502 kmem_free(path, MAXPATHLEN); 503 ddi_prop_free(ibn); 504 505 /* notify devfsadmd to sync of path_to_inst file */ 506 mutex_enter(&e_ddi_inst_state.ins_serial); 507 i_log_devfs_instance_mod(); 508 e_ddi_inst_state.ins_dirty = B_TRUE; 509 mutex_exit(&e_ddi_inst_state.ins_serial); 510 return (1); 511 } 512 513 /* our path did not go through any of of the instance blocks */ 514 kmem_free(path, MAXPATHLEN); 515 ddi_prop_free(ibn); 516 return (0); 517 } 518 519 /* 520 * Look up an instance number for a dev_info node, and assign one if it does 521 * not have one (the dev_info node has devi_name and devi_addr already set). 522 */ 523 uint_t 524 e_ddi_assign_instance(dev_info_t *dip) 525 { 526 char *name; 527 in_node_t *ap, *np; 528 in_drv_t *dp; 529 major_t major; 530 uint_t ret; 531 char *bname; 532 533 /* 534 * Allow implementation to override 535 */ 536 if ((ret = impl_assign_instance(dip)) != (uint_t)-1) 537 return (ret); 538 539 /* 540 * If this is a pseudo-device, use the instance number 541 * assigned by the pseudo nexus driver. The mutex is 542 * not needed since the instance tree is not used. 543 */ 544 if (is_pseudo_device(dip)) { 545 return (ddi_get_instance(dip)); 546 } 547 548 /* 549 * Only one thread is allowed to change the state of the instance 550 * number assignments on the system at any given time. 551 */ 552 e_ddi_enter_instance(); 553 554 /* 555 * Look for instance node, allocate one if not found 556 */ 557 np = in_devwalk(dip, &ap, NULL); 558 if (np == NULL) { 559 if (in_assign_instance_block(dip)) { 560 np = in_devwalk(dip, &ap, NULL); 561 } else { 562 name = ddi_node_name(dip); 563 np = in_alloc_node(name, ddi_get_name_addr(dip)); 564 ASSERT(np != NULL); 565 in_enlist(ap, np); /* insert into tree */ 566 } 567 } 568 ASSERT(np == in_devwalk(dip, &ap, NULL)); 569 570 /* 571 * Link the devinfo node and in_node_t 572 */ 573 if (DEVI(dip)->devi_in_node || np->in_devi) { 574 ddi_err(DER_MODE, dip, "devinfo and instance node (%p) " 575 "interlink fields are not NULL", (void *)np); 576 } 577 DEVI(dip)->devi_in_node = np; 578 np->in_devi = dip; 579 580 /* 581 * Look for driver entry, allocate one if not found 582 */ 583 bname = (char *)ddi_driver_name(dip); 584 dp = in_drvwalk(np, bname); 585 if (dp == NULL) { 586 587 if (ddi_aliases_present == B_TRUE) { 588 e_ddi_borrow_instance(dip, np); 589 } 590 591 if ((dp = in_drvwalk(np, bname)) == NULL) { 592 dp = in_alloc_drv(bname); 593 ASSERT(dp != NULL); 594 major = ddi_driver_major(dip); 595 ASSERT(major != DDI_MAJOR_T_NONE); 596 in_endrv(np, dp); 597 in_set_instance(dip, dp, major); 598 dp->ind_state = IN_PROVISIONAL; 599 in_hashdrv(dp); 600 } else { 601 dp->ind_state = IN_BORROWED; 602 } 603 } 604 605 ret = dp->ind_instance; 606 607 e_ddi_exit_instance(); 608 return (ret); 609 } 610 611 static int 612 mkpathname(char *path, in_node_t *np, int len) 613 { 614 int len_needed; 615 616 if (np == e_ddi_inst_state.ins_root) 617 return (DDI_SUCCESS); 618 619 if (mkpathname(path, np->in_parent, len) == DDI_FAILURE) 620 return (DDI_FAILURE); 621 622 len_needed = strlen(path); 623 len_needed += strlen(np->in_node_name) + 1; /* for '/' */ 624 if (np->in_unit_addr) { 625 len_needed += strlen(np->in_unit_addr) + 1; /* for '@' */ 626 } 627 len_needed += 1; /* for '\0' */ 628 629 /* 630 * XX complain 631 */ 632 if (len_needed > len) 633 return (DDI_FAILURE); 634 635 if (np->in_unit_addr[0] == '\0') 636 (void) sprintf(path+strlen(path), "/%s", np->in_node_name); 637 else 638 (void) sprintf(path+strlen(path), "/%s@%s", np->in_node_name, 639 np->in_unit_addr); 640 641 return (DDI_SUCCESS); 642 } 643 644 /* 645 * produce the path to the given instance of a major number. 646 * path must hold MAXPATHLEN string 647 */ 648 int 649 e_ddi_instance_majorinstance_to_path(major_t major, uint_t inst, char *path) 650 { 651 struct devnames *dnp; 652 in_drv_t *dp; 653 int ret; 654 655 e_ddi_enter_instance(); 656 657 /* look for the instance threaded off major */ 658 dnp = &devnamesp[major]; 659 for (dp = dnp->dn_inlist; dp != NULL; dp = dp->ind_next) 660 if (dp->ind_instance == inst) 661 break; 662 663 /* produce path from the node that uses the instance */ 664 if (dp) { 665 *path = 0; 666 ret = mkpathname(path, dp->ind_node, MAXPATHLEN); 667 } else 668 ret = DDI_FAILURE; 669 670 e_ddi_exit_instance(); 671 return (ret); 672 } 673 674 /* 675 * Allocate a sequential block of instance numbers for the specified driver, 676 * and return the base instance number of the block. The implementation 677 * depends on the list being sorted in ascending instance number sequence. 678 * When there are no 'holes' in the allocation sequence, dn_instance is the 679 * next available instance number. When dn_instance is IN_SEARCHME, hole(s) 680 * exists and a slower code path executes which tries to fill holes. 681 */ 682 static int 683 in_next_instance_block(major_t major, int block_size) 684 { 685 unsigned int prev; 686 struct devnames *dnp; 687 in_drv_t *dp; 688 int base; 689 int hole; 690 691 dnp = &devnamesp[major]; 692 ASSERT(major != DDI_MAJOR_T_NONE); 693 ASSERT(e_ddi_inst_state.ins_busy); 694 ASSERT(block_size); 695 696 /* check to see if we can do a quick allocation */ 697 if (dnp->dn_instance != IN_SEARCHME) { 698 base = dnp->dn_instance; 699 dnp->dn_instance += block_size; 700 return (base); 701 } 702 dp = dnp->dn_inlist; 703 704 /* no existing entries, allocate block at 0 */ 705 if (dp == NULL) { 706 dnp->dn_instance = block_size; 707 return (0); 708 } 709 710 prev = dp->ind_instance; 711 if (prev >= block_size) 712 return (0); /* we fit in hole at beginning */ 713 714 /* search the list for a large enough hole */ 715 for (dp = dp->ind_next, hole = 0; dp; dp = dp->ind_next) { 716 if (dp->ind_instance != (prev + 1)) 717 hole++; /* we have a hole */ 718 if (dp->ind_instance >= (prev + block_size + 1)) 719 break; /* we fit in hole */ 720 prev = dp->ind_instance; 721 } 722 723 /* 724 * If hole is zero then all holes are patched and we can resume 725 * quick allocations. 726 */ 727 if (hole == 0) 728 dnp->dn_instance = prev + 1 + block_size; 729 730 return (prev + 1); 731 } 732 733 /* assign instance block of size 1 */ 734 static int 735 in_next_instance(major_t major) 736 { 737 return (in_next_instance_block(major, 1)); 738 } 739 740 /* 741 * This call causes us to *forget* the instance number we've generated 742 * for a given device if it was not permanent. 743 */ 744 void 745 e_ddi_free_instance(dev_info_t *dip, char *addr) 746 { 747 char *name; 748 in_node_t *np; 749 in_node_t *ap; /* ancestor node */ 750 major_t major; 751 struct devnames *dnp; 752 in_drv_t *dp; /* in_drv entry */ 753 754 /* 755 * Allow implementation override 756 */ 757 if (impl_free_instance(dip) == DDI_SUCCESS) 758 return; 759 760 /* 761 * If this is a pseudo-device, no instance number 762 * was assigned. 763 */ 764 if (is_pseudo_device(dip)) { 765 return; 766 } 767 768 name = (char *)ddi_driver_name(dip); 769 major = ddi_driver_major(dip); 770 ASSERT(major != DDI_MAJOR_T_NONE); 771 dnp = &devnamesp[major]; 772 /* 773 * Only one thread is allowed to change the state of the instance 774 * number assignments on the system at any given time. 775 */ 776 e_ddi_enter_instance(); 777 np = in_devwalk(dip, &ap, addr); 778 ASSERT(np); 779 780 /* 781 * Break the interlink between dip and np 782 */ 783 if (DEVI(dip)->devi_in_node != np || np->in_devi != dip) { 784 ddi_err(DER_MODE, dip, "devinfo node linked to " 785 "wrong instance node: %p", (void *)np); 786 } 787 DEVI(dip)->devi_in_node = NULL; 788 np->in_devi = NULL; 789 790 dp = in_drvwalk(np, name); 791 ASSERT(dp); 792 if (dp->ind_state == IN_PROVISIONAL) { 793 in_removedrv(dnp, dp); 794 } else if (dp->ind_state == IN_BORROWED) { 795 dp->ind_state = IN_PERMANENT; 796 e_ddi_return_instance(dip, addr, np); 797 } 798 if (np->in_drivers == NULL) { 799 in_removenode(dnp, np, ap); 800 } 801 e_ddi_exit_instance(); 802 } 803 804 /* 805 * This makes our memory of an instance assignment permanent 806 */ 807 void 808 e_ddi_keep_instance(dev_info_t *dip) 809 { 810 in_node_t *np, *ap; 811 in_drv_t *dp; 812 813 /* Don't make nulldriver instance assignments permanent */ 814 if (ddi_driver_major(dip) == nulldriver_major) 815 return; 816 817 /* 818 * Allow implementation override 819 */ 820 if (impl_keep_instance(dip) == DDI_SUCCESS) 821 return; 822 823 /* 824 * Nothing to do for pseudo devices. 825 */ 826 if (is_pseudo_device(dip)) 827 return; 828 829 /* 830 * Only one thread is allowed to change the state of the instance 831 * number assignments on the system at any given time. 832 */ 833 e_ddi_enter_instance(); 834 np = in_devwalk(dip, &ap, NULL); 835 ASSERT(np); 836 dp = in_drvwalk(np, (char *)ddi_driver_name(dip)); 837 ASSERT(dp); 838 839 mutex_enter(&e_ddi_inst_state.ins_serial); 840 if (dp->ind_state == IN_PROVISIONAL || dp->ind_state == IN_BORROWED) { 841 dp->ind_state = IN_PERMANENT; 842 i_log_devfs_instance_mod(); 843 e_ddi_inst_state.ins_dirty = B_TRUE; 844 } 845 mutex_exit(&e_ddi_inst_state.ins_serial); 846 e_ddi_exit_instance(); 847 } 848 849 /* 850 * A new major has been added to the system. Run through the orphan list 851 * and try to attach each one to a driver's list. 852 */ 853 void 854 e_ddi_unorphan_instance_nos() 855 { 856 in_drv_t *dp, *ndp; 857 858 /* 859 * disconnect the orphan list, and call in_hashdrv for each item 860 * on it 861 */ 862 863 /* 864 * Only one thread is allowed to change the state of the instance 865 * number assignments on the system at any given time. 866 */ 867 e_ddi_enter_instance(); 868 if (e_ddi_inst_state.ins_no_major == NULL) { 869 e_ddi_exit_instance(); 870 return; 871 } 872 /* 873 * Hash instance list to devnames structure of major. 874 * Note that if there is not a valid major number for the 875 * node, in_hashdrv will put it back on the no_major list. 876 */ 877 dp = e_ddi_inst_state.ins_no_major; 878 e_ddi_inst_state.ins_no_major = NULL; 879 while (dp) { 880 ndp = dp->ind_next; 881 ASSERT(dp->ind_state != IN_UNKNOWN); 882 dp->ind_next = NULL; 883 in_hashdrv(dp); 884 dp = ndp; 885 } 886 e_ddi_exit_instance(); 887 } 888 889 static void 890 in_removenode(struct devnames *dnp, in_node_t *mp, in_node_t *ap) 891 { 892 in_node_t *np; 893 894 ASSERT(e_ddi_inst_state.ins_busy); 895 896 /* 897 * Assertion: parents are always instantiated by the framework 898 * before their children, destroyed after them 899 */ 900 ASSERT(mp->in_child == NULL); 901 /* 902 * Assertion: drv entries are always removed before their owning nodes 903 */ 904 ASSERT(mp->in_drivers == NULL); 905 /* 906 * Take the node out of the tree 907 */ 908 if (ap->in_child == mp) { 909 ap->in_child = mp->in_sibling; 910 in_dealloc_node(mp); 911 return; 912 } else { 913 for (np = ap->in_child; np; np = np->in_sibling) { 914 if (np->in_sibling == mp) { 915 np->in_sibling = mp->in_sibling; 916 in_dealloc_node(mp); 917 return; 918 } 919 } 920 } 921 panic("in_removenode dnp %p mp %p", (void *)dnp, (void *)mp); 922 } 923 924 /* 925 * Recursive ascent 926 * 927 * This now only does half the job. It finds the node, then the caller 928 * has to search the node for the binding name 929 */ 930 static in_node_t * 931 in_devwalk(dev_info_t *dip, in_node_t **ap, char *addr) 932 { 933 in_node_t *np; 934 char *name; 935 936 ASSERT(dip); 937 ASSERT(e_ddi_inst_state.ins_busy); 938 if (dip == ddi_root_node()) { 939 *ap = NULL; 940 return (e_ddi_inst_state.ins_root); 941 } 942 /* 943 * call up to find parent, then look through the list of kids 944 * for a match 945 */ 946 np = in_devwalk(ddi_get_parent(dip), ap, NULL); 947 if (np == NULL) 948 return (np); 949 *ap = np; 950 np = np->in_child; 951 name = ddi_node_name(dip); 952 if (addr == NULL) 953 addr = ddi_get_name_addr(dip); 954 955 while (np) { 956 if (in_eqstr(np->in_node_name, name) && 957 in_eqstr(np->in_unit_addr, addr)) { 958 return (np); 959 } 960 np = np->in_sibling; 961 } 962 963 return (np); 964 } 965 966 /* 967 * Create a node specified by cp and assign it the given instance no. 968 */ 969 static int 970 in_pathin(char *cp, int instance, char *bname, struct bind **args) 971 { 972 in_node_t *np; 973 in_drv_t *dp; 974 char *name; 975 976 ASSERT(e_ddi_inst_state.ins_busy); 977 ASSERT(args == NULL); 978 979 /* 980 * Give a warning to the console. 981 * return value ignored 982 */ 983 if (cp[0] != '/' || instance == -1 || bname == NULL) { 984 cmn_err(CE_WARN, 985 "invalid instance file entry %s %d", 986 cp, instance); 987 return (0); 988 } 989 990 if ((name = i_binding_to_drv_name(bname)) != NULL) 991 bname = name; 992 993 np = in_make_path(cp); 994 ASSERT(np); 995 996 dp = in_drvwalk(np, bname); 997 if (dp != NULL) { 998 cmn_err(CE_WARN, 999 "multiple instance number assignments for " 1000 "'%s' (driver %s), %d used", 1001 cp, bname, dp->ind_instance); 1002 return (0); 1003 } 1004 1005 if (in_inuse(instance, bname)) { 1006 cmn_err(CE_WARN, 1007 "instance already in use: %s %d", cp, instance); 1008 return (0); 1009 } 1010 1011 dp = in_alloc_drv(bname); 1012 in_endrv(np, dp); 1013 dp->ind_instance = instance; 1014 dp->ind_state = IN_PERMANENT; 1015 in_hashdrv(dp); 1016 1017 return (0); 1018 } 1019 1020 /* 1021 * Create (or find) the node named by path by recursively descending from the 1022 * root's first child (we ignore the root, which is never named) 1023 */ 1024 static in_node_t * 1025 in_make_path(char *path) 1026 { 1027 in_node_t *ap; /* ancestor pointer */ 1028 in_node_t *np; /* working node pointer */ 1029 in_node_t *rp; /* return node pointer */ 1030 char buf[MAXPATHLEN]; /* copy of string so we can change it */ 1031 char *cp, *name, *addr; 1032 1033 ASSERT(e_ddi_inst_state.ins_busy); 1034 1035 if (path == NULL || path[0] != '/') 1036 return (NULL); 1037 1038 (void) snprintf(buf, sizeof (buf), "%s", path); 1039 cp = buf + 1; /* skip over initial '/' in path */ 1040 name = in_name_addr(&cp, &addr); 1041 1042 /* 1043 * In S9 and earlier releases, the path_to_inst file 1044 * SunCluster was prepended with "/node@#". This was 1045 * removed in S10. We skip the prefix if the prefix 1046 * still exists in /etc/path_to_inst. It is needed for 1047 * various forms of Solaris upgrade to work properly 1048 * in the SunCluster environment. 1049 */ 1050 if ((cluster_bootflags & CLUSTER_CONFIGURED) && 1051 (strcmp(name, "node") == 0)) 1052 name = in_name_addr(&cp, &addr); 1053 1054 ap = e_ddi_inst_state.ins_root; 1055 np = e_ddi_inst_state.ins_root->in_child; 1056 rp = np; 1057 while (name) { 1058 while (name && np) { 1059 if (in_eqstr(name, np->in_node_name) && 1060 in_eqstr(addr, np->in_unit_addr)) { 1061 name = in_name_addr(&cp, &addr); 1062 if (name == NULL) 1063 return (np); 1064 ap = np; 1065 np = np->in_child; 1066 } else { 1067 np = np->in_sibling; 1068 } 1069 } 1070 np = in_alloc_node(name, addr); 1071 in_enlist(ap, np); /* insert into tree */ 1072 rp = np; /* value to return if we quit */ 1073 ap = np; /* new parent */ 1074 np = NULL; /* can have no children */ 1075 name = in_name_addr(&cp, &addr); 1076 } 1077 1078 return (rp); 1079 } 1080 1081 /* 1082 * Insert node np into the tree as one of ap's children. 1083 */ 1084 static void 1085 in_enlist(in_node_t *ap, in_node_t *np) 1086 { 1087 in_node_t *mp; 1088 ASSERT(e_ddi_inst_state.ins_busy); 1089 /* 1090 * Make this node some other node's child or child's sibling 1091 */ 1092 ASSERT(ap && np); 1093 if (ap->in_child == NULL) { 1094 ap->in_child = np; 1095 } else { 1096 for (mp = ap->in_child; mp; mp = mp->in_sibling) 1097 if (mp->in_sibling == NULL) { 1098 mp->in_sibling = np; 1099 break; 1100 } 1101 } 1102 np->in_parent = ap; 1103 } 1104 1105 /* 1106 * Insert drv entry dp onto a node's driver list 1107 */ 1108 static void 1109 in_endrv(in_node_t *np, in_drv_t *dp) 1110 { 1111 in_drv_t *mp; 1112 ASSERT(e_ddi_inst_state.ins_busy); 1113 ASSERT(np && dp); 1114 mp = np->in_drivers; 1115 np->in_drivers = dp; 1116 dp->ind_next_drv = mp; 1117 dp->ind_node = np; 1118 } 1119 1120 /* 1121 * Parse the next name out of the path, null terminate it and update cp. 1122 * caller has copied string so we can mess with it. 1123 * Upon return *cpp points to the next section to be parsed, *addrp points 1124 * to the current address substring (or NULL if none) and we return the 1125 * current name substring (or NULL if none). name and address substrings 1126 * are null terminated in place. 1127 */ 1128 1129 static char * 1130 in_name_addr(char **cpp, char **addrp) 1131 { 1132 char *namep; /* return value holder */ 1133 char *ap; /* pointer to '@' in string */ 1134 char *sp; /* pointer to '/' in string */ 1135 1136 if (*cpp == NULL || **cpp == '\0') { 1137 *addrp = NULL; 1138 return (NULL); 1139 } 1140 namep = *cpp; 1141 sp = strchr(*cpp, '/'); 1142 if (sp != NULL) { /* more to follow */ 1143 *sp = '\0'; 1144 *cpp = sp + 1; 1145 } else { /* this is last component. */ 1146 *cpp = NULL; 1147 } 1148 ap = strchr(namep, '@'); 1149 if (ap == NULL) { 1150 *addrp = NULL; 1151 } else { 1152 *ap = '\0'; /* terminate the name */ 1153 *addrp = ap + 1; 1154 } 1155 return (namep); 1156 } 1157 1158 /* 1159 * Allocate a node and storage for name and addr strings, and fill them in. 1160 */ 1161 static in_node_t * 1162 in_alloc_node(char *name, char *addr) 1163 { 1164 in_node_t *np; 1165 char *cp; 1166 size_t namelen; 1167 1168 ASSERT(e_ddi_inst_state.ins_busy); 1169 /* 1170 * Has name or will become root 1171 */ 1172 ASSERT(name || e_ddi_inst_state.ins_root == NULL); 1173 if (addr == NULL) 1174 addr = ""; 1175 if (name == NULL) 1176 namelen = 0; 1177 else 1178 namelen = strlen(name) + 1; 1179 cp = kmem_zalloc(sizeof (in_node_t) + namelen + strlen(addr) + 1, 1180 KM_SLEEP); 1181 np = (in_node_t *)cp; 1182 if (name) { 1183 np->in_node_name = cp + sizeof (in_node_t); 1184 (void) strcpy(np->in_node_name, name); 1185 } 1186 np->in_unit_addr = cp + sizeof (in_node_t) + namelen; 1187 (void) strcpy(np->in_unit_addr, addr); 1188 return (np); 1189 } 1190 1191 /* 1192 * Allocate a drv entry and storage for binding name string, and fill it in. 1193 */ 1194 static in_drv_t * 1195 in_alloc_drv(char *bindingname) 1196 { 1197 in_drv_t *dp; 1198 char *cp; 1199 size_t namelen; 1200 1201 ASSERT(e_ddi_inst_state.ins_busy); 1202 /* 1203 * Has name or will become root 1204 */ 1205 ASSERT(bindingname || e_ddi_inst_state.ins_root == NULL); 1206 if (bindingname == NULL) 1207 namelen = 0; 1208 else 1209 namelen = strlen(bindingname) + 1; 1210 cp = kmem_zalloc(sizeof (in_drv_t) + namelen, KM_SLEEP); 1211 dp = (in_drv_t *)cp; 1212 if (bindingname) { 1213 dp->ind_driver_name = cp + sizeof (in_drv_t); 1214 (void) strcpy(dp->ind_driver_name, bindingname); 1215 } 1216 dp->ind_state = IN_UNKNOWN; 1217 dp->ind_instance = -1; 1218 return (dp); 1219 } 1220 1221 static void 1222 in_dealloc_node(in_node_t *np) 1223 { 1224 /* 1225 * The root node can never be de-allocated 1226 */ 1227 ASSERT(np->in_node_name && np->in_unit_addr); 1228 ASSERT(e_ddi_inst_state.ins_busy); 1229 kmem_free(np, sizeof (in_node_t) + strlen(np->in_node_name) 1230 + strlen(np->in_unit_addr) + 2); 1231 } 1232 1233 static void 1234 in_dealloc_drv(in_drv_t *dp) 1235 { 1236 ASSERT(dp->ind_driver_name); 1237 ASSERT(e_ddi_inst_state.ins_busy); 1238 kmem_free(dp, sizeof (in_drv_t) + strlen(dp->ind_driver_name) 1239 + 1); 1240 } 1241 1242 /* 1243 * Handle the various possible versions of "no address" 1244 */ 1245 static int 1246 in_eqstr(char *a, char *b) 1247 { 1248 if (a == b) /* covers case where both are nulls */ 1249 return (1); 1250 if (a == NULL && *b == 0) 1251 return (1); 1252 if (b == NULL && *a == 0) 1253 return (1); 1254 if (a == NULL || b == NULL) 1255 return (0); 1256 return (strcmp(a, b) == 0); 1257 } 1258 1259 /* 1260 * Returns true if instance no. is already in use by named driver 1261 */ 1262 static int 1263 in_inuse(int instance, char *name) 1264 { 1265 major_t major; 1266 in_drv_t *dp; 1267 struct devnames *dnp; 1268 1269 ASSERT(e_ddi_inst_state.ins_busy); 1270 /* 1271 * For now, if we've never heard of this device we assume it is not 1272 * in use, since we can't tell 1273 * XXX could do the weaker search through the nomajor list checking 1274 * XXX for the same name 1275 */ 1276 if ((major = ddi_name_to_major(name)) == DDI_MAJOR_T_NONE) 1277 return (0); 1278 dnp = &devnamesp[major]; 1279 1280 dp = dnp->dn_inlist; 1281 while (dp) { 1282 if (dp->ind_instance == instance) 1283 return (1); 1284 dp = dp->ind_next; 1285 } 1286 return (0); 1287 } 1288 1289 static void 1290 in_hashdrv(in_drv_t *dp) 1291 { 1292 struct devnames *dnp; 1293 in_drv_t *mp, *pp; 1294 major_t major; 1295 1296 /* hash to no major list */ 1297 major = ddi_name_to_major(dp->ind_driver_name); 1298 if (major == DDI_MAJOR_T_NONE) { 1299 dp->ind_next = e_ddi_inst_state.ins_no_major; 1300 e_ddi_inst_state.ins_no_major = dp; 1301 return; 1302 } 1303 1304 /* 1305 * dnp->dn_inlist is sorted by instance number. 1306 * Adding a new instance entry may introduce holes, 1307 * set dn_instance to IN_SEARCHME so the next instance 1308 * assignment may fill in holes. 1309 */ 1310 dnp = &devnamesp[major]; 1311 pp = mp = dnp->dn_inlist; 1312 if (mp == NULL || dp->ind_instance < mp->ind_instance) { 1313 /* prepend as the first entry, turn on IN_SEARCHME */ 1314 dnp->dn_instance = IN_SEARCHME; 1315 dp->ind_next = mp; 1316 dnp->dn_inlist = dp; 1317 return; 1318 } 1319 1320 ASSERT(mp->ind_instance != dp->ind_instance); 1321 while (mp->ind_instance < dp->ind_instance && mp->ind_next) { 1322 pp = mp; 1323 mp = mp->ind_next; 1324 ASSERT(mp->ind_instance != dp->ind_instance); 1325 } 1326 1327 if (mp->ind_instance < dp->ind_instance) { /* end of list */ 1328 dp->ind_next = NULL; 1329 mp->ind_next = dp; 1330 } else { 1331 ASSERT(dnp->dn_instance == IN_SEARCHME); 1332 dp->ind_next = pp->ind_next; 1333 pp->ind_next = dp; 1334 } 1335 } 1336 1337 /* 1338 * Remove a driver entry from the list, given a previous pointer 1339 */ 1340 static void 1341 in_removedrv(struct devnames *dnp, in_drv_t *mp) 1342 { 1343 in_drv_t *dp; 1344 in_drv_t *prevp; 1345 1346 if (dnp->dn_inlist == mp) { /* head of list */ 1347 dnp->dn_inlist = mp->ind_next; 1348 dnp->dn_instance = IN_SEARCHME; 1349 in_dq_drv(mp); 1350 in_dealloc_drv(mp); 1351 return; 1352 } 1353 prevp = dnp->dn_inlist; 1354 for (dp = prevp->ind_next; dp; dp = dp->ind_next) { 1355 if (dp == mp) { /* found it */ 1356 break; 1357 } 1358 prevp = dp; 1359 } 1360 1361 ASSERT(dp == mp); 1362 dnp->dn_instance = IN_SEARCHME; 1363 prevp->ind_next = mp->ind_next; 1364 in_dq_drv(mp); 1365 in_dealloc_drv(mp); 1366 } 1367 1368 static void 1369 in_dq_drv(in_drv_t *mp) 1370 { 1371 struct in_node *node = mp->ind_node; 1372 in_drv_t *ptr, *prev; 1373 1374 if (mp == node->in_drivers) { 1375 node->in_drivers = mp->ind_next_drv; 1376 return; 1377 } 1378 prev = node->in_drivers; 1379 for (ptr = prev->ind_next_drv; ptr != (struct in_drv *)NULL; 1380 ptr = ptr->ind_next_drv) { 1381 if (ptr == mp) { 1382 prev->ind_next_drv = ptr->ind_next_drv; 1383 return; 1384 } 1385 prev = ptr; 1386 } 1387 panic("in_dq_drv: in_drv not found on node driver list"); 1388 } 1389 1390 1391 in_drv_t * 1392 in_drvwalk(in_node_t *np, char *binding_name) 1393 { 1394 char *name; 1395 in_drv_t *dp = np->in_drivers; 1396 while (dp) { 1397 if ((name = i_binding_to_drv_name(dp->ind_driver_name)) 1398 == NULL) { 1399 name = dp->ind_driver_name; 1400 } 1401 if (strcmp(binding_name, name) == 0) { 1402 break; 1403 } 1404 dp = dp->ind_next_drv; 1405 } 1406 return (dp); 1407 } 1408 1409 1410 1411 static void 1412 i_log_devfs_instance_mod(void) 1413 { 1414 sysevent_t *ev; 1415 sysevent_id_t eid; 1416 static int sent_one = 0; 1417 1418 /* 1419 * Prevent unnecessary event generation. Do not generate more than 1420 * one event during boot. 1421 */ 1422 if (sent_one && !i_ddi_io_initialized()) 1423 return; 1424 1425 ev = sysevent_alloc(EC_DEVFS, ESC_DEVFS_INSTANCE_MOD, EP_DDI, 1426 SE_NOSLEEP); 1427 if (ev == NULL) { 1428 return; 1429 } 1430 if (log_sysevent(ev, SE_NOSLEEP, &eid) != 0) { 1431 cmn_err(CE_WARN, "i_log_devfs_instance_mod: failed to post " 1432 "event"); 1433 } else { 1434 sent_one = 1; 1435 } 1436 sysevent_free(ev); 1437 } 1438 1439 void 1440 e_ddi_enter_instance(void) 1441 { 1442 mutex_enter(&e_ddi_inst_state.ins_serial); 1443 if (e_ddi_inst_state.ins_thread == curthread) 1444 e_ddi_inst_state.ins_busy++; 1445 else { 1446 while (e_ddi_inst_state.ins_busy) 1447 cv_wait(&e_ddi_inst_state.ins_serial_cv, 1448 &e_ddi_inst_state.ins_serial); 1449 e_ddi_inst_state.ins_thread = curthread; 1450 e_ddi_inst_state.ins_busy = 1; 1451 } 1452 mutex_exit(&e_ddi_inst_state.ins_serial); 1453 } 1454 1455 void 1456 e_ddi_exit_instance(void) 1457 { 1458 mutex_enter(&e_ddi_inst_state.ins_serial); 1459 e_ddi_inst_state.ins_busy--; 1460 if (e_ddi_inst_state.ins_busy == 0) { 1461 cv_broadcast(&e_ddi_inst_state.ins_serial_cv); 1462 e_ddi_inst_state.ins_thread = NULL; 1463 } 1464 mutex_exit(&e_ddi_inst_state.ins_serial); 1465 } 1466 1467 int 1468 e_ddi_instance_is_clean(void) 1469 { 1470 return (e_ddi_inst_state.ins_dirty == B_FALSE); 1471 } 1472 1473 void 1474 e_ddi_instance_set_clean(void) 1475 { 1476 e_ddi_inst_state.ins_dirty = B_FALSE; 1477 } 1478 1479 in_node_t * 1480 e_ddi_instance_root(void) 1481 { 1482 return (e_ddi_inst_state.ins_root); 1483 } 1484 1485 /* 1486 * Visit a node in the instance tree 1487 */ 1488 static int 1489 in_walk_instances(in_node_t *np, char *path, char *this, 1490 int (*f)(const char *, in_node_t *, in_drv_t *, void *), void *arg) 1491 { 1492 in_drv_t *dp; 1493 int rval = INST_WALK_CONTINUE; 1494 char *next; 1495 1496 while (np != NULL) { 1497 1498 if (np->in_unit_addr[0] == 0) 1499 (void) sprintf(this, "/%s", np->in_node_name); 1500 else 1501 (void) sprintf(this, "/%s@%s", np->in_node_name, 1502 np->in_unit_addr); 1503 next = this + strlen(this); 1504 1505 for (dp = np->in_drivers; dp; dp = dp->ind_next_drv) { 1506 if (dp->ind_state == IN_PERMANENT) { 1507 rval = (*f)(path, np, dp, arg); 1508 if (rval == INST_WALK_TERMINATE) 1509 break; 1510 } 1511 } 1512 1513 if (np->in_child) { 1514 rval = in_walk_instances(np->in_child, 1515 path, next, f, arg); 1516 if (rval == INST_WALK_TERMINATE) 1517 break; 1518 } 1519 1520 np = np->in_sibling; 1521 } 1522 1523 return (rval); 1524 } 1525 1526 /* 1527 * A general interface for walking the instance tree, 1528 * calling a user-supplied callback for each node. 1529 */ 1530 int 1531 e_ddi_walk_instances(int (*f)(const char *, 1532 in_node_t *, in_drv_t *, void *), void *arg) 1533 { 1534 in_node_t *root; 1535 int rval; 1536 char *path; 1537 1538 path = kmem_zalloc(MAXPATHLEN, KM_SLEEP); 1539 1540 e_ddi_enter_instance(); 1541 root = e_ddi_instance_root(); 1542 rval = in_walk_instances(root->in_child, path, path, f, arg); 1543 1544 e_ddi_exit_instance(); 1545 1546 kmem_free(path, MAXPATHLEN); 1547 return (rval); 1548 } 1549 1550 in_node_t * 1551 e_ddi_path_to_instance(char *path) 1552 { 1553 in_node_t *np; 1554 1555 np = in_make_path(path); 1556 if (np && np->in_drivers && np->in_drivers->ind_state == IN_PERMANENT) { 1557 return (np); 1558 } 1559 return (NULL); 1560 } 1561 1562 void 1563 e_ddi_borrow_instance(dev_info_t *cdip, in_node_t *cnp) 1564 { 1565 char *alias; 1566 in_node_t *anp; 1567 char *curr = kmem_alloc(MAXPATHLEN, KM_NOSLEEP); 1568 1569 if (curr == NULL) { 1570 ddi_err(DER_PANIC, cdip, "curr alloc failed"); 1571 /*NOTREACHED*/ 1572 } 1573 1574 (void) ddi_pathname(cdip, curr); 1575 1576 if (cnp->in_drivers) { 1577 ddi_err(DER_PANIC, cdip, "cnp has instance: %p", cnp); 1578 /*NOTREACHED*/ 1579 } 1580 1581 alias = ddi_curr_redirect(curr); 1582 kmem_free(curr, MAXPATHLEN); 1583 1584 if (alias && (anp = e_ddi_path_to_instance(alias)) != NULL) { 1585 cnp->in_drivers = anp->in_drivers; 1586 anp->in_drivers = NULL; 1587 } 1588 } 1589 1590 void 1591 e_ddi_return_instance(dev_info_t *cdip, char *addr, in_node_t *cnp) 1592 { 1593 in_node_t *anp; 1594 char *alias; 1595 char *curr = kmem_alloc(MAXPATHLEN, KM_NOSLEEP); 1596 1597 if (curr == NULL) { 1598 ddi_err(DER_PANIC, cdip, "alloc of curr failed"); 1599 /*NOTREACHED*/ 1600 } 1601 1602 (void) ddi_pathname(cdip, curr); 1603 if (addr) { 1604 (void) strlcat(curr, "@", MAXPATHLEN); 1605 (void) strlcat(curr, addr, MAXPATHLEN); 1606 1607 } 1608 if (cnp->in_drivers == NULL) { 1609 ddi_err(DER_PANIC, cdip, "cnp has no inst: %p", cnp); 1610 /*NOTREACHED*/ 1611 } 1612 1613 alias = ddi_curr_redirect(curr); 1614 kmem_free(curr, MAXPATHLEN); 1615 1616 if (alias && (anp = e_ddi_path_to_instance(alias)) != NULL) { 1617 ASSERT(anp->in_drivers == NULL); 1618 anp->in_drivers = cnp->in_drivers; 1619 cnp->in_drivers = NULL; 1620 } 1621 } 1622