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 /* 751 * If hole is zero then all holes are patched and we can resume 752 * quick allocations. 753 */ 754 if (hole == 0) 755 dnp->dn_instance = prev + 1 + block_size; 756 757 return (prev + 1); 758 } 759 760 /* assign instance block of size 1 */ 761 static int 762 in_next_instance(major_t major) 763 { 764 return (in_next_instance_block(major, 1)); 765 } 766 767 /* 768 * This call causes us to *forget* the instance number we've generated 769 * for a given device if it was not permanent. 770 */ 771 void 772 e_ddi_free_instance(dev_info_t *dip, char *addr) 773 { 774 char *name; 775 in_node_t *np; 776 in_node_t *ap; /* ancestor node */ 777 major_t major; 778 struct devnames *dnp; 779 in_drv_t *dp; /* in_drv entry */ 780 781 /* 782 * Allow implementation override 783 */ 784 if (impl_free_instance(dip) == DDI_SUCCESS) 785 return; 786 787 /* 788 * If this is a pseudo-device, no instance number 789 * was assigned. 790 */ 791 if (is_pseudo_device(dip)) { 792 return; 793 } 794 795 name = (char *)ddi_driver_name(dip); 796 major = ddi_driver_major(dip); 797 ASSERT(major != DDI_MAJOR_T_NONE); 798 dnp = &devnamesp[major]; 799 /* 800 * Only one thread is allowed to change the state of the instance 801 * number assignments on the system at any given time. 802 */ 803 e_ddi_enter_instance(); 804 np = in_devwalk(dip, &ap, addr); 805 ASSERT(np); 806 807 /* 808 * Break the interlink between dip and np 809 */ 810 if (DEVI(dip)->devi_in_node != np || np->in_devi != dip) { 811 ddi_err(DER_MODE, dip, "devinfo node linked to " 812 "wrong instance node: %p", (void *)np); 813 } 814 DEVI(dip)->devi_in_node = NULL; 815 np->in_devi = NULL; 816 817 dp = in_drvwalk(np, name); 818 ASSERT(dp); 819 if (dp->ind_state == IN_PROVISIONAL) { 820 in_removedrv(dnp, dp); 821 } else if (dp->ind_state == IN_BORROWED) { 822 dp->ind_state = IN_PERMANENT; 823 e_ddi_return_instance(dip, addr, np); 824 } 825 if (np->in_drivers == NULL) { 826 in_removenode(dnp, np, ap); 827 } 828 e_ddi_exit_instance(); 829 } 830 831 /* 832 * This makes our memory of an instance assignment permanent 833 */ 834 void 835 e_ddi_keep_instance(dev_info_t *dip) 836 { 837 in_node_t *np, *ap; 838 in_drv_t *dp; 839 840 /* Don't make nulldriver instance assignments permanent */ 841 if (ddi_driver_major(dip) == nulldriver_major) 842 return; 843 844 /* 845 * Allow implementation override 846 */ 847 if (impl_keep_instance(dip) == DDI_SUCCESS) 848 return; 849 850 /* 851 * Nothing to do for pseudo devices. 852 */ 853 if (is_pseudo_device(dip)) 854 return; 855 856 /* 857 * Only one thread is allowed to change the state of the instance 858 * number assignments on the system at any given time. 859 */ 860 e_ddi_enter_instance(); 861 np = in_devwalk(dip, &ap, NULL); 862 ASSERT(np); 863 dp = in_drvwalk(np, (char *)ddi_driver_name(dip)); 864 ASSERT(dp); 865 866 mutex_enter(&e_ddi_inst_state.ins_serial); 867 if (dp->ind_state == IN_PROVISIONAL || dp->ind_state == IN_BORROWED) { 868 dp->ind_state = IN_PERMANENT; 869 i_log_devfs_instance_mod(); 870 e_ddi_inst_state.ins_dirty = B_TRUE; 871 } 872 mutex_exit(&e_ddi_inst_state.ins_serial); 873 e_ddi_exit_instance(); 874 } 875 876 /* 877 * A new major has been added to the system. Run through the orphan list 878 * and try to attach each one to a driver's list. 879 */ 880 void 881 e_ddi_unorphan_instance_nos() 882 { 883 in_drv_t *dp, *ndp; 884 885 /* 886 * disconnect the orphan list, and call in_hashdrv for each item 887 * on it 888 */ 889 890 /* 891 * Only one thread is allowed to change the state of the instance 892 * number assignments on the system at any given time. 893 */ 894 e_ddi_enter_instance(); 895 if (e_ddi_inst_state.ins_no_major == NULL) { 896 e_ddi_exit_instance(); 897 return; 898 } 899 /* 900 * Hash instance list to devnames structure of major. 901 * Note that if there is not a valid major number for the 902 * node, in_hashdrv will put it back on the no_major list. 903 */ 904 dp = e_ddi_inst_state.ins_no_major; 905 e_ddi_inst_state.ins_no_major = NULL; 906 while (dp) { 907 ndp = dp->ind_next; 908 ASSERT(dp->ind_state != IN_UNKNOWN); 909 dp->ind_next = NULL; 910 in_hashdrv(dp); 911 dp = ndp; 912 } 913 e_ddi_exit_instance(); 914 } 915 916 static void 917 in_removenode(struct devnames *dnp, in_node_t *mp, in_node_t *ap) 918 { 919 in_node_t *np; 920 921 ASSERT(e_ddi_inst_state.ins_busy); 922 923 /* 924 * Assertion: parents are always instantiated by the framework 925 * before their children, destroyed after them 926 */ 927 ASSERT(mp->in_child == NULL); 928 /* 929 * Assertion: drv entries are always removed before their owning nodes 930 */ 931 ASSERT(mp->in_drivers == NULL); 932 /* 933 * Take the node out of the tree 934 */ 935 if (ap->in_child == mp) { 936 ap->in_child = mp->in_sibling; 937 in_dealloc_node(mp); 938 return; 939 } else { 940 for (np = ap->in_child; np; np = np->in_sibling) { 941 if (np->in_sibling == mp) { 942 np->in_sibling = mp->in_sibling; 943 in_dealloc_node(mp); 944 return; 945 } 946 } 947 } 948 panic("in_removenode dnp %p mp %p", (void *)dnp, (void *)mp); 949 } 950 951 /* 952 * Recursive ascent 953 * 954 * This now only does half the job. It finds the node, then the caller 955 * has to search the node for the binding name 956 */ 957 static in_node_t * 958 in_devwalk(dev_info_t *dip, in_node_t **ap, char *addr) 959 { 960 in_node_t *np; 961 char *name; 962 963 ASSERT(dip); 964 ASSERT(e_ddi_inst_state.ins_busy); 965 if (dip == ddi_root_node()) { 966 *ap = NULL; 967 return (e_ddi_inst_state.ins_root); 968 } 969 /* 970 * call up to find parent, then look through the list of kids 971 * for a match 972 */ 973 np = in_devwalk(ddi_get_parent(dip), ap, NULL); 974 if (np == NULL) 975 return (np); 976 *ap = np; 977 np = np->in_child; 978 name = ddi_node_name(dip); 979 if (addr == NULL) 980 addr = ddi_get_name_addr(dip); 981 982 while (np) { 983 if (in_eqstr(np->in_node_name, name) && 984 in_eqstr(np->in_unit_addr, addr)) { 985 return (np); 986 } 987 np = np->in_sibling; 988 } 989 990 return (np); 991 } 992 993 /* 994 * Create a node specified by cp and assign it the given instance no. 995 */ 996 static int 997 in_pathin(char *cp, int instance, char *bname, struct bind **args) 998 { 999 in_node_t *np; 1000 in_drv_t *dp; 1001 char *name; 1002 1003 ASSERT(e_ddi_inst_state.ins_busy); 1004 ASSERT(args == NULL); 1005 1006 /* 1007 * Give a warning to the console. 1008 * return value ignored 1009 */ 1010 if (cp[0] != '/' || instance == -1 || bname == NULL) { 1011 cmn_err(CE_WARN, 1012 "invalid instance file entry %s %d", 1013 cp, instance); 1014 return (0); 1015 } 1016 1017 if ((name = i_binding_to_drv_name(bname)) != NULL) 1018 bname = name; 1019 1020 np = in_make_path(cp); 1021 ASSERT(np); 1022 1023 dp = in_drvwalk(np, bname); 1024 if (dp != NULL) { 1025 cmn_err(CE_WARN, 1026 "multiple instance number assignments for " 1027 "'%s' (driver %s), %d used", 1028 cp, bname, dp->ind_instance); 1029 return (0); 1030 } 1031 1032 if (in_inuse(instance, bname)) { 1033 cmn_err(CE_WARN, 1034 "instance already in use: %s %d", cp, instance); 1035 return (0); 1036 } 1037 1038 dp = in_alloc_drv(bname); 1039 in_endrv(np, dp); 1040 dp->ind_instance = instance; 1041 dp->ind_state = IN_PERMANENT; 1042 in_hashdrv(dp); 1043 1044 return (0); 1045 } 1046 1047 /* 1048 * Create (or find) the node named by path by recursively descending from the 1049 * root's first child (we ignore the root, which is never named) 1050 */ 1051 static in_node_t * 1052 in_make_path(char *path) 1053 { 1054 in_node_t *ap; /* ancestor pointer */ 1055 in_node_t *np; /* working node pointer */ 1056 in_node_t *rp; /* return node pointer */ 1057 char buf[MAXPATHLEN]; /* copy of string so we can change it */ 1058 char *cp, *name, *addr; 1059 1060 ASSERT(e_ddi_inst_state.ins_busy); 1061 1062 if (path == NULL || path[0] != '/') 1063 return (NULL); 1064 1065 (void) snprintf(buf, sizeof (buf), "%s", path); 1066 cp = buf + 1; /* skip over initial '/' in path */ 1067 name = in_name_addr(&cp, &addr); 1068 1069 /* 1070 * In S9 and earlier releases, the path_to_inst file 1071 * SunCluster was prepended with "/node@#". This was 1072 * removed in S10. We skip the prefix if the prefix 1073 * still exists in /etc/path_to_inst. It is needed for 1074 * various forms of Solaris upgrade to work properly 1075 * in the SunCluster environment. 1076 */ 1077 if ((cluster_bootflags & CLUSTER_CONFIGURED) && 1078 (strcmp(name, "node") == 0)) 1079 name = in_name_addr(&cp, &addr); 1080 1081 ap = e_ddi_inst_state.ins_root; 1082 np = e_ddi_inst_state.ins_root->in_child; 1083 rp = np; 1084 while (name) { 1085 while (name && np) { 1086 if (in_eqstr(name, np->in_node_name) && 1087 in_eqstr(addr, np->in_unit_addr)) { 1088 name = in_name_addr(&cp, &addr); 1089 if (name == NULL) 1090 return (np); 1091 ap = np; 1092 np = np->in_child; 1093 } else { 1094 np = np->in_sibling; 1095 } 1096 } 1097 np = in_alloc_node(name, addr); 1098 in_enlist(ap, np); /* insert into tree */ 1099 rp = np; /* value to return if we quit */ 1100 ap = np; /* new parent */ 1101 np = NULL; /* can have no children */ 1102 name = in_name_addr(&cp, &addr); 1103 } 1104 1105 return (rp); 1106 } 1107 1108 /* 1109 * Insert node np into the tree as one of ap's children. 1110 */ 1111 static void 1112 in_enlist(in_node_t *ap, in_node_t *np) 1113 { 1114 in_node_t *mp; 1115 ASSERT(e_ddi_inst_state.ins_busy); 1116 /* 1117 * Make this node some other node's child or child's sibling 1118 */ 1119 ASSERT(ap && np); 1120 if (ap->in_child == NULL) { 1121 ap->in_child = np; 1122 } else { 1123 for (mp = ap->in_child; mp; mp = mp->in_sibling) 1124 if (mp->in_sibling == NULL) { 1125 mp->in_sibling = np; 1126 break; 1127 } 1128 } 1129 np->in_parent = ap; 1130 } 1131 1132 /* 1133 * Insert drv entry dp onto a node's driver list 1134 */ 1135 static void 1136 in_endrv(in_node_t *np, in_drv_t *dp) 1137 { 1138 in_drv_t *mp; 1139 ASSERT(e_ddi_inst_state.ins_busy); 1140 ASSERT(np && dp); 1141 mp = np->in_drivers; 1142 np->in_drivers = dp; 1143 dp->ind_next_drv = mp; 1144 dp->ind_node = np; 1145 } 1146 1147 /* 1148 * Parse the next name out of the path, null terminate it and update cp. 1149 * caller has copied string so we can mess with it. 1150 * Upon return *cpp points to the next section to be parsed, *addrp points 1151 * to the current address substring (or NULL if none) and we return the 1152 * current name substring (or NULL if none). name and address substrings 1153 * are null terminated in place. 1154 */ 1155 1156 static char * 1157 in_name_addr(char **cpp, char **addrp) 1158 { 1159 char *namep; /* return value holder */ 1160 char *ap; /* pointer to '@' in string */ 1161 char *sp; /* pointer to '/' in string */ 1162 1163 if (*cpp == NULL || **cpp == '\0') { 1164 *addrp = NULL; 1165 return (NULL); 1166 } 1167 namep = *cpp; 1168 sp = strchr(*cpp, '/'); 1169 if (sp != NULL) { /* more to follow */ 1170 *sp = '\0'; 1171 *cpp = sp + 1; 1172 } else { /* this is last component. */ 1173 *cpp = NULL; 1174 } 1175 ap = strchr(namep, '@'); 1176 if (ap == NULL) { 1177 *addrp = NULL; 1178 } else { 1179 *ap = '\0'; /* terminate the name */ 1180 *addrp = ap + 1; 1181 } 1182 return (namep); 1183 } 1184 1185 /* 1186 * Allocate a node and storage for name and addr strings, and fill them in. 1187 */ 1188 static in_node_t * 1189 in_alloc_node(char *name, char *addr) 1190 { 1191 in_node_t *np; 1192 char *cp; 1193 size_t namelen; 1194 1195 ASSERT(e_ddi_inst_state.ins_busy); 1196 /* 1197 * Has name or will become root 1198 */ 1199 ASSERT(name || e_ddi_inst_state.ins_root == NULL); 1200 if (addr == NULL) 1201 addr = ""; 1202 if (name == NULL) 1203 namelen = 0; 1204 else 1205 namelen = strlen(name) + 1; 1206 cp = kmem_zalloc(sizeof (in_node_t) + namelen + strlen(addr) + 1, 1207 KM_SLEEP); 1208 np = (in_node_t *)cp; 1209 if (name) { 1210 np->in_node_name = cp + sizeof (in_node_t); 1211 (void) strcpy(np->in_node_name, name); 1212 } 1213 np->in_unit_addr = cp + sizeof (in_node_t) + namelen; 1214 (void) strcpy(np->in_unit_addr, addr); 1215 return (np); 1216 } 1217 1218 /* 1219 * Allocate a drv entry and storage for binding name string, and fill it in. 1220 */ 1221 static in_drv_t * 1222 in_alloc_drv(char *bindingname) 1223 { 1224 in_drv_t *dp; 1225 char *cp; 1226 size_t namelen; 1227 1228 ASSERT(e_ddi_inst_state.ins_busy); 1229 /* 1230 * Has name or will become root 1231 */ 1232 ASSERT(bindingname || e_ddi_inst_state.ins_root == NULL); 1233 if (bindingname == NULL) 1234 namelen = 0; 1235 else 1236 namelen = strlen(bindingname) + 1; 1237 cp = kmem_zalloc(sizeof (in_drv_t) + namelen, KM_SLEEP); 1238 dp = (in_drv_t *)cp; 1239 if (bindingname) { 1240 dp->ind_driver_name = cp + sizeof (in_drv_t); 1241 (void) strcpy(dp->ind_driver_name, bindingname); 1242 } 1243 dp->ind_state = IN_UNKNOWN; 1244 dp->ind_instance = -1; 1245 return (dp); 1246 } 1247 1248 static void 1249 in_dealloc_node(in_node_t *np) 1250 { 1251 /* 1252 * The root node can never be de-allocated 1253 */ 1254 ASSERT(np->in_node_name && np->in_unit_addr); 1255 ASSERT(e_ddi_inst_state.ins_busy); 1256 kmem_free(np, sizeof (in_node_t) + strlen(np->in_node_name) 1257 + strlen(np->in_unit_addr) + 2); 1258 } 1259 1260 static void 1261 in_dealloc_drv(in_drv_t *dp) 1262 { 1263 ASSERT(dp->ind_driver_name); 1264 ASSERT(e_ddi_inst_state.ins_busy); 1265 kmem_free(dp, sizeof (in_drv_t) + strlen(dp->ind_driver_name) 1266 + 1); 1267 } 1268 1269 /* 1270 * Handle the various possible versions of "no address" 1271 */ 1272 static int 1273 in_eqstr(char *a, char *b) 1274 { 1275 if (a == b) /* covers case where both are nulls */ 1276 return (1); 1277 if (a == NULL && *b == 0) 1278 return (1); 1279 if (b == NULL && *a == 0) 1280 return (1); 1281 if (a == NULL || b == NULL) 1282 return (0); 1283 return (strcmp(a, b) == 0); 1284 } 1285 1286 /* 1287 * Returns true if instance no. is already in use by named driver 1288 */ 1289 static int 1290 in_inuse(int instance, char *name) 1291 { 1292 major_t major; 1293 in_drv_t *dp; 1294 struct devnames *dnp; 1295 1296 ASSERT(e_ddi_inst_state.ins_busy); 1297 /* 1298 * For now, if we've never heard of this device we assume it is not 1299 * in use, since we can't tell 1300 * XXX could do the weaker search through the nomajor list checking 1301 * XXX for the same name 1302 */ 1303 if ((major = ddi_name_to_major(name)) == DDI_MAJOR_T_NONE) 1304 return (0); 1305 dnp = &devnamesp[major]; 1306 1307 dp = dnp->dn_inlist; 1308 while (dp) { 1309 if (dp->ind_instance == instance) 1310 return (1); 1311 dp = dp->ind_next; 1312 } 1313 return (0); 1314 } 1315 1316 static void 1317 in_hashdrv(in_drv_t *dp) 1318 { 1319 struct devnames *dnp; 1320 in_drv_t *mp, *pp; 1321 major_t major; 1322 1323 /* hash to no major list */ 1324 major = ddi_name_to_major(dp->ind_driver_name); 1325 if (major == DDI_MAJOR_T_NONE) { 1326 dp->ind_next = e_ddi_inst_state.ins_no_major; 1327 e_ddi_inst_state.ins_no_major = dp; 1328 return; 1329 } 1330 1331 /* 1332 * dnp->dn_inlist is sorted by instance number. 1333 * Adding a new instance entry may introduce holes, 1334 * set dn_instance to IN_SEARCHME so the next instance 1335 * assignment may fill in holes. 1336 */ 1337 dnp = &devnamesp[major]; 1338 pp = mp = dnp->dn_inlist; 1339 if (mp == NULL || dp->ind_instance < mp->ind_instance) { 1340 /* prepend as the first entry, turn on IN_SEARCHME */ 1341 dnp->dn_instance = IN_SEARCHME; 1342 dp->ind_next = mp; 1343 dnp->dn_inlist = dp; 1344 return; 1345 } 1346 1347 ASSERT(mp->ind_instance != dp->ind_instance); 1348 while (mp->ind_instance < dp->ind_instance && mp->ind_next) { 1349 pp = mp; 1350 mp = mp->ind_next; 1351 ASSERT(mp->ind_instance != dp->ind_instance); 1352 } 1353 1354 if (mp->ind_instance < dp->ind_instance) { /* end of list */ 1355 dp->ind_next = NULL; 1356 mp->ind_next = dp; 1357 } else { 1358 ASSERT(dnp->dn_instance == IN_SEARCHME); 1359 dp->ind_next = pp->ind_next; 1360 pp->ind_next = dp; 1361 } 1362 } 1363 1364 /* 1365 * Remove a driver entry from the list, given a previous pointer 1366 */ 1367 static void 1368 in_removedrv(struct devnames *dnp, in_drv_t *mp) 1369 { 1370 in_drv_t *dp; 1371 in_drv_t *prevp; 1372 1373 if (dnp->dn_inlist == mp) { /* head of list */ 1374 dnp->dn_inlist = mp->ind_next; 1375 dnp->dn_instance = IN_SEARCHME; 1376 in_dq_drv(mp); 1377 in_dealloc_drv(mp); 1378 return; 1379 } 1380 prevp = dnp->dn_inlist; 1381 for (dp = prevp->ind_next; dp; dp = dp->ind_next) { 1382 if (dp == mp) { /* found it */ 1383 break; 1384 } 1385 prevp = dp; 1386 } 1387 1388 ASSERT(dp == mp); 1389 dnp->dn_instance = IN_SEARCHME; 1390 prevp->ind_next = mp->ind_next; 1391 in_dq_drv(mp); 1392 in_dealloc_drv(mp); 1393 } 1394 1395 static void 1396 in_dq_drv(in_drv_t *mp) 1397 { 1398 struct in_node *node = mp->ind_node; 1399 in_drv_t *ptr, *prev; 1400 1401 if (mp == node->in_drivers) { 1402 node->in_drivers = mp->ind_next_drv; 1403 return; 1404 } 1405 prev = node->in_drivers; 1406 for (ptr = prev->ind_next_drv; ptr != (struct in_drv *)NULL; 1407 ptr = ptr->ind_next_drv) { 1408 if (ptr == mp) { 1409 prev->ind_next_drv = ptr->ind_next_drv; 1410 return; 1411 } 1412 prev = ptr; 1413 } 1414 panic("in_dq_drv: in_drv not found on node driver list"); 1415 } 1416 1417 1418 in_drv_t * 1419 in_drvwalk(in_node_t *np, char *binding_name) 1420 { 1421 char *name; 1422 in_drv_t *dp = np->in_drivers; 1423 while (dp) { 1424 if ((name = i_binding_to_drv_name(dp->ind_driver_name)) 1425 == NULL) { 1426 name = dp->ind_driver_name; 1427 } 1428 if (strcmp(binding_name, name) == 0) { 1429 break; 1430 } 1431 dp = dp->ind_next_drv; 1432 } 1433 return (dp); 1434 } 1435 1436 1437 1438 static void 1439 i_log_devfs_instance_mod(void) 1440 { 1441 sysevent_t *ev; 1442 sysevent_id_t eid; 1443 static int sent_one = 0; 1444 1445 /* 1446 * Prevent unnecessary event generation. Do not generate more than 1447 * one event during boot. 1448 */ 1449 if (sent_one && !i_ddi_io_initialized()) 1450 return; 1451 1452 ev = sysevent_alloc(EC_DEVFS, ESC_DEVFS_INSTANCE_MOD, EP_DDI, 1453 SE_NOSLEEP); 1454 if (ev == NULL) { 1455 return; 1456 } 1457 if (log_sysevent(ev, SE_NOSLEEP, &eid) != 0) { 1458 cmn_err(CE_WARN, "i_log_devfs_instance_mod: failed to post " 1459 "event"); 1460 } else { 1461 sent_one = 1; 1462 } 1463 sysevent_free(ev); 1464 } 1465 1466 void 1467 e_ddi_enter_instance(void) 1468 { 1469 mutex_enter(&e_ddi_inst_state.ins_serial); 1470 if (e_ddi_inst_state.ins_thread == curthread) 1471 e_ddi_inst_state.ins_busy++; 1472 else { 1473 while (e_ddi_inst_state.ins_busy) 1474 cv_wait(&e_ddi_inst_state.ins_serial_cv, 1475 &e_ddi_inst_state.ins_serial); 1476 e_ddi_inst_state.ins_thread = curthread; 1477 e_ddi_inst_state.ins_busy = 1; 1478 } 1479 mutex_exit(&e_ddi_inst_state.ins_serial); 1480 } 1481 1482 void 1483 e_ddi_exit_instance(void) 1484 { 1485 mutex_enter(&e_ddi_inst_state.ins_serial); 1486 e_ddi_inst_state.ins_busy--; 1487 if (e_ddi_inst_state.ins_busy == 0) { 1488 cv_broadcast(&e_ddi_inst_state.ins_serial_cv); 1489 e_ddi_inst_state.ins_thread = NULL; 1490 } 1491 mutex_exit(&e_ddi_inst_state.ins_serial); 1492 } 1493 1494 int 1495 e_ddi_instance_is_clean(void) 1496 { 1497 return (e_ddi_inst_state.ins_dirty == B_FALSE); 1498 } 1499 1500 void 1501 e_ddi_instance_set_clean(void) 1502 { 1503 e_ddi_inst_state.ins_dirty = B_FALSE; 1504 } 1505 1506 in_node_t * 1507 e_ddi_instance_root(void) 1508 { 1509 return (e_ddi_inst_state.ins_root); 1510 } 1511 1512 /* 1513 * Visit a node in the instance tree 1514 */ 1515 static int 1516 in_walk_instances(in_node_t *np, char *path, char *this, 1517 int (*f)(const char *, in_node_t *, in_drv_t *, void *), void *arg) 1518 { 1519 in_drv_t *dp; 1520 int rval = INST_WALK_CONTINUE; 1521 char *next; 1522 1523 while (np != NULL) { 1524 1525 if (np->in_unit_addr[0] == 0) 1526 (void) sprintf(this, "/%s", np->in_node_name); 1527 else 1528 (void) sprintf(this, "/%s@%s", np->in_node_name, 1529 np->in_unit_addr); 1530 next = this + strlen(this); 1531 1532 for (dp = np->in_drivers; dp; dp = dp->ind_next_drv) { 1533 if (dp->ind_state == IN_PERMANENT) { 1534 rval = (*f)(path, np, dp, arg); 1535 if (rval == INST_WALK_TERMINATE) 1536 break; 1537 } 1538 } 1539 1540 if (np->in_child) { 1541 rval = in_walk_instances(np->in_child, 1542 path, next, f, arg); 1543 if (rval == INST_WALK_TERMINATE) 1544 break; 1545 } 1546 1547 np = np->in_sibling; 1548 } 1549 1550 return (rval); 1551 } 1552 1553 /* 1554 * A general interface for walking the instance tree, 1555 * calling a user-supplied callback for each node. 1556 */ 1557 int 1558 e_ddi_walk_instances(int (*f)(const char *, 1559 in_node_t *, in_drv_t *, void *), void *arg) 1560 { 1561 in_node_t *root; 1562 int rval; 1563 char *path; 1564 1565 path = kmem_zalloc(MAXPATHLEN, KM_SLEEP); 1566 1567 e_ddi_enter_instance(); 1568 root = e_ddi_instance_root(); 1569 rval = in_walk_instances(root->in_child, path, path, f, arg); 1570 1571 e_ddi_exit_instance(); 1572 1573 kmem_free(path, MAXPATHLEN); 1574 return (rval); 1575 } 1576 1577 in_node_t * 1578 e_ddi_path_to_instance(char *path) 1579 { 1580 in_node_t *np; 1581 1582 np = in_make_path(path); 1583 if (np && np->in_drivers && np->in_drivers->ind_state == IN_PERMANENT) { 1584 return (np); 1585 } 1586 return (NULL); 1587 } 1588 1589 void 1590 e_ddi_borrow_instance(dev_info_t *cdip, in_node_t *cnp) 1591 { 1592 char *alias; 1593 in_node_t *anp; 1594 char *curr = kmem_alloc(MAXPATHLEN, KM_NOSLEEP); 1595 1596 if (curr == NULL) { 1597 ddi_err(DER_PANIC, cdip, "curr alloc failed"); 1598 /*NOTREACHED*/ 1599 } 1600 1601 (void) ddi_pathname(cdip, curr); 1602 1603 if (cnp->in_drivers) { 1604 ddi_err(DER_PANIC, cdip, "cnp has instance: %p", cnp); 1605 /*NOTREACHED*/ 1606 } 1607 1608 alias = ddi_curr_redirect(curr); 1609 kmem_free(curr, MAXPATHLEN); 1610 1611 if (alias && (anp = e_ddi_path_to_instance(alias)) != NULL) { 1612 cnp->in_drivers = anp->in_drivers; 1613 anp->in_drivers = NULL; 1614 } 1615 } 1616 1617 void 1618 e_ddi_return_instance(dev_info_t *cdip, char *addr, in_node_t *cnp) 1619 { 1620 in_node_t *anp; 1621 char *alias; 1622 char *curr = kmem_alloc(MAXPATHLEN, KM_NOSLEEP); 1623 1624 if (curr == NULL) { 1625 ddi_err(DER_PANIC, cdip, "alloc of curr failed"); 1626 /*NOTREACHED*/ 1627 } 1628 1629 (void) ddi_pathname(cdip, curr); 1630 if (addr) { 1631 (void) strlcat(curr, "@", MAXPATHLEN); 1632 (void) strlcat(curr, addr, MAXPATHLEN); 1633 1634 } 1635 if (cnp->in_drivers == NULL) { 1636 ddi_err(DER_PANIC, cdip, "cnp has no inst: %p", cnp); 1637 /*NOTREACHED*/ 1638 } 1639 1640 alias = ddi_curr_redirect(curr); 1641 kmem_free(curr, MAXPATHLEN); 1642 1643 if (alias && (anp = e_ddi_path_to_instance(alias)) != NULL) { 1644 ASSERT(anp->in_drivers == NULL); 1645 anp->in_drivers = cnp->in_drivers; 1646 cnp->in_drivers = NULL; 1647 } 1648 } 1649