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