1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 /* 27 * Copyright 2023 Oxide Computer Company 28 */ 29 30 #include <sys/types.h> 31 #include <sys/param.h> 32 #include <sys/systm.h> 33 #include <sys/buf.h> 34 #include <sys/uio.h> 35 #include <sys/cred.h> 36 #include <sys/poll.h> 37 #include <sys/mman.h> 38 #include <sys/kmem.h> 39 #include <sys/model.h> 40 #include <sys/file.h> 41 #include <sys/proc.h> 42 #include <sys/open.h> 43 #include <sys/user.h> 44 #include <sys/t_lock.h> 45 #include <sys/vm.h> 46 #include <sys/stat.h> 47 #include <vm/hat.h> 48 #include <vm/seg.h> 49 #include <vm/as.h> 50 #include <sys/cmn_err.h> 51 #include <sys/debug.h> 52 #include <sys/avintr.h> 53 #include <sys/autoconf.h> 54 #include <sys/sunddi.h> 55 #include <sys/esunddi.h> 56 #include <sys/sunndi.h> 57 #include <sys/ddi.h> 58 #include <sys/kstat.h> 59 #include <sys/conf.h> 60 #include <sys/ddi_impldefs.h> /* include implementation structure defs */ 61 #include <sys/ndi_impldefs.h> 62 #include <sys/hwconf.h> 63 #include <sys/pathname.h> 64 #include <sys/modctl.h> 65 #include <sys/epm.h> 66 #include <sys/devctl.h> 67 #include <sys/callb.h> 68 #include <sys/bootconf.h> 69 #include <sys/dacf_impl.h> 70 #include <sys/nvpair.h> 71 #include <sys/sunmdi.h> 72 #include <sys/fs/dv_node.h> 73 #include <sys/sunldi_impl.h> 74 75 #ifdef __sparc 76 #include <sys/archsystm.h> /* getpil/setpil */ 77 #include <sys/membar.h> /* membar_sync */ 78 #endif 79 80 /* 81 * ndi property handling 82 */ 83 int 84 ndi_prop_update_int(dev_t match_dev, dev_info_t *dip, 85 char *name, int data) 86 { 87 return (ddi_prop_update_common(match_dev, dip, 88 DDI_PROP_HW_DEF | DDI_PROP_TYPE_INT | DDI_PROP_DONTSLEEP, 89 name, &data, 1, ddi_prop_fm_encode_ints)); 90 } 91 92 int 93 ndi_prop_update_int64(dev_t match_dev, dev_info_t *dip, 94 char *name, int64_t data) 95 { 96 return (ddi_prop_update_common(match_dev, dip, 97 DDI_PROP_HW_DEF | DDI_PROP_TYPE_INT64 | DDI_PROP_DONTSLEEP, 98 name, &data, 1, ddi_prop_fm_encode_int64)); 99 } 100 101 int 102 ndi_prop_create_boolean(dev_t match_dev, dev_info_t *dip, 103 char *name) 104 { 105 return (ddi_prop_update_common(match_dev, dip, 106 DDI_PROP_HW_DEF | DDI_PROP_TYPE_ANY | DDI_PROP_DONTSLEEP, 107 name, NULL, 0, ddi_prop_fm_encode_bytes)); 108 } 109 110 int 111 ndi_prop_update_int_array(dev_t match_dev, dev_info_t *dip, 112 char *name, int *data, uint_t nelements) 113 { 114 return (ddi_prop_update_common(match_dev, dip, 115 DDI_PROP_HW_DEF | DDI_PROP_TYPE_INT | DDI_PROP_DONTSLEEP, 116 name, data, nelements, ddi_prop_fm_encode_ints)); 117 } 118 119 int 120 ndi_prop_update_int64_array(dev_t match_dev, dev_info_t *dip, 121 char *name, int64_t *data, uint_t nelements) 122 { 123 return (ddi_prop_update_common(match_dev, dip, 124 DDI_PROP_HW_DEF | DDI_PROP_TYPE_INT64 | DDI_PROP_DONTSLEEP, 125 name, data, nelements, ddi_prop_fm_encode_int64)); 126 } 127 128 int 129 ndi_prop_update_string(dev_t match_dev, dev_info_t *dip, 130 char *name, char *data) 131 { 132 return (ddi_prop_update_common(match_dev, dip, 133 DDI_PROP_HW_DEF | DDI_PROP_TYPE_STRING | DDI_PROP_DONTSLEEP, 134 name, &data, 1, ddi_prop_fm_encode_string)); 135 } 136 137 int 138 ndi_prop_update_string_array(dev_t match_dev, dev_info_t *dip, 139 char *name, char **data, uint_t nelements) 140 { 141 return (ddi_prop_update_common(match_dev, dip, 142 DDI_PROP_HW_DEF | DDI_PROP_TYPE_STRING | DDI_PROP_DONTSLEEP, 143 name, data, nelements, 144 ddi_prop_fm_encode_strings)); 145 } 146 147 int 148 ndi_prop_update_byte_array(dev_t match_dev, dev_info_t *dip, 149 char *name, uchar_t *data, uint_t nelements) 150 { 151 if (nelements == 0) 152 return (DDI_PROP_INVAL_ARG); 153 154 return (ddi_prop_update_common(match_dev, dip, 155 DDI_PROP_HW_DEF | DDI_PROP_TYPE_BYTE | DDI_PROP_DONTSLEEP, 156 name, data, nelements, ddi_prop_fm_encode_bytes)); 157 } 158 159 int 160 ndi_prop_remove(dev_t dev, dev_info_t *dip, char *name) 161 { 162 return (ddi_prop_remove_common(dev, dip, name, DDI_PROP_HW_DEF)); 163 } 164 165 void 166 ndi_prop_remove_all(dev_info_t *dip) 167 { 168 i_ddi_prop_dyn_parent_set(dip, NULL); 169 ddi_prop_remove_all_common(dip, (int)DDI_PROP_HW_DEF); 170 } 171 172 /* 173 * Post an event notification to nexus driver responsible for handling 174 * the event. The responsible nexus is defined in the cookie passed in as 175 * the third parameter. 176 * The dip parameter is an artifact of an older implementation in which all 177 * requests to remove an eventcall would bubble up the tree. Today, this 178 * parameter is ignored. 179 * Input Parameters: 180 * dip - Ignored. 181 * rdip - device driver posting the event 182 * cookie - valid ddi_eventcookie_t, obtained by caller prior to 183 * invocation of this routine 184 * impl_data - used by framework 185 */ 186 /*ARGSUSED*/ 187 int 188 ndi_post_event(dev_info_t *dip, dev_info_t *rdip, 189 ddi_eventcookie_t cookie, void *impl_data) 190 { 191 dev_info_t *ddip; 192 193 ASSERT(cookie); 194 ddip = NDI_EVENT_DDIP(cookie); 195 196 /* 197 * perform sanity checks. These conditions should never be true. 198 */ 199 200 ASSERT(DEVI(ddip)->devi_ops->devo_bus_ops != NULL); 201 ASSERT(DEVI(ddip)->devi_ops->devo_bus_ops->busops_rev >= BUSO_REV_6); 202 ASSERT(DEVI(ddip)->devi_ops->devo_bus_ops->bus_post_event != NULL); 203 204 /* 205 * post the event to the responsible ancestor 206 */ 207 return ((*(DEVI(ddip)->devi_ops->devo_bus_ops->bus_post_event)) 208 (ddip, rdip, cookie, impl_data)); 209 } 210 211 /* 212 * Calls the bus nexus driver's implementation of the 213 * (*bus_remove_eventcall)() interface. 214 */ 215 int 216 ndi_busop_remove_eventcall(dev_info_t *ddip, ddi_callback_id_t id) 217 { 218 219 ASSERT(id); 220 /* check for a correct revno before calling up the device tree. */ 221 ASSERT(DEVI(ddip)->devi_ops->devo_bus_ops != NULL); 222 ASSERT(DEVI(ddip)->devi_ops->devo_bus_ops->busops_rev >= BUSO_REV_6); 223 224 if (DEVI(ddip)->devi_ops->devo_bus_ops->bus_remove_eventcall == NULL) 225 return (DDI_FAILURE); 226 227 /* 228 * request responsible nexus to remove the eventcall 229 */ 230 return ((*(DEVI(ddip)->devi_ops->devo_bus_ops->bus_remove_eventcall)) 231 (ddip, id)); 232 } 233 234 /* 235 * Calls the bus nexus driver's implementation of the 236 * (*bus_add_eventcall)() interface. The dip parameter is an 237 * artifact of an older implementation in which all requests to 238 * add an eventcall would bubble up the tree. Today, this parameter is 239 * ignored. 240 */ 241 /*ARGSUSED*/ 242 int 243 ndi_busop_add_eventcall(dev_info_t *dip, dev_info_t *rdip, 244 ddi_eventcookie_t cookie, void (*callback)(), void *arg, 245 ddi_callback_id_t *cb_id) 246 { 247 dev_info_t *ddip = (dev_info_t *)NDI_EVENT_DDIP(cookie); 248 249 /* 250 * check for a correct revno before calling up the device tree. 251 */ 252 ASSERT(DEVI(ddip)->devi_ops->devo_bus_ops != NULL); 253 ASSERT(DEVI(ddip)->devi_ops->devo_bus_ops->busops_rev >= BUSO_REV_6); 254 255 if (DEVI(ddip)->devi_ops->devo_bus_ops->bus_add_eventcall == NULL) 256 return (DDI_FAILURE); 257 258 /* 259 * request responsible ancestor to add the eventcall 260 */ 261 return ((*(DEVI(ddip)->devi_ops->devo_bus_ops->bus_add_eventcall)) 262 (ddip, rdip, cookie, callback, arg, cb_id)); 263 } 264 265 /* 266 * Calls the bus nexus driver's implementation of the 267 * (*bus_get_eventcookie)() interface up the device tree hierarchy. 268 */ 269 int 270 ndi_busop_get_eventcookie(dev_info_t *dip, dev_info_t *rdip, char *name, 271 ddi_eventcookie_t *event_cookiep) 272 { 273 dev_info_t *pdip = (dev_info_t *)DEVI(dip)->devi_parent; 274 275 /* Can not be called from rootnex. */ 276 ASSERT(pdip); 277 278 /* 279 * check for a correct revno before calling up the device tree. 280 */ 281 ASSERT(DEVI(pdip)->devi_ops->devo_bus_ops != NULL); 282 283 if ((DEVI(pdip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_6) || 284 (DEVI(pdip)->devi_ops->devo_bus_ops->bus_get_eventcookie == NULL)) { 285 #ifdef DEBUG 286 if ((DEVI(pdip)->devi_ops->devo_bus_ops->busops_rev >= 287 BUSO_REV_3) && 288 (DEVI(pdip)->devi_ops->devo_bus_ops->bus_get_eventcookie)) { 289 cmn_err(CE_WARN, 290 "Warning: %s%d busops_rev=%d no longer supported" 291 " by the NDI event framework.\nBUSO_REV_6 or " 292 "greater must be used.", 293 DEVI(pdip)->devi_binding_name, 294 DEVI(pdip)->devi_instance, 295 DEVI(pdip)->devi_ops->devo_bus_ops->busops_rev); 296 } 297 #endif /* DEBUG */ 298 299 return (ndi_busop_get_eventcookie(pdip, rdip, name, 300 event_cookiep)); 301 } 302 303 return ((*(DEVI(pdip)->devi_ops->devo_bus_ops->bus_get_eventcookie)) 304 (pdip, rdip, name, event_cookiep)); 305 } 306 307 /* 308 * Copy in the devctl IOCTL data and return a handle to 309 * the data. 310 */ 311 int 312 ndi_dc_allochdl(void *iocarg, struct devctl_iocdata **rdcp) 313 { 314 struct devctl_iocdata *dcp; 315 char *cpybuf; 316 317 ASSERT(rdcp != NULL); 318 319 dcp = kmem_zalloc(sizeof (*dcp), KM_SLEEP); 320 321 if (get_udatamodel() == DATAMODEL_NATIVE) { 322 if (copyin(iocarg, dcp, sizeof (*dcp)) != 0) { 323 kmem_free(dcp, sizeof (*dcp)); 324 return (NDI_FAULT); 325 } 326 } 327 #ifdef _SYSCALL32_IMPL 328 else { 329 struct devctl_iocdata32 dcp32; 330 331 if (copyin(iocarg, &dcp32, sizeof (dcp32)) != 0) { 332 kmem_free(dcp, sizeof (*dcp)); 333 return (NDI_FAULT); 334 } 335 dcp->cmd = (uint_t)dcp32.cmd; 336 dcp->flags = (uint_t)dcp32.flags; 337 dcp->cpyout_buf = (uint_t *)(uintptr_t)dcp32.cpyout_buf; 338 dcp->nvl_user = (nvlist_t *)(uintptr_t)dcp32.nvl_user; 339 dcp->nvl_usersz = (size_t)dcp32.nvl_usersz; 340 dcp->c_nodename = (char *)(uintptr_t)dcp32.c_nodename; 341 dcp->c_unitaddr = (char *)(uintptr_t)dcp32.c_unitaddr; 342 } 343 #endif 344 if (dcp->c_nodename != NULL) { 345 cpybuf = kmem_alloc(MAXNAMELEN, KM_SLEEP); 346 if (copyinstr(dcp->c_nodename, cpybuf, MAXNAMELEN, 0) != 0) { 347 kmem_free(cpybuf, MAXNAMELEN); 348 kmem_free(dcp, sizeof (*dcp)); 349 return (NDI_FAULT); 350 } 351 cpybuf[MAXNAMELEN - 1] = '\0'; 352 dcp->c_nodename = cpybuf; 353 } 354 355 if (dcp->c_unitaddr != NULL) { 356 cpybuf = kmem_alloc(MAXNAMELEN, KM_SLEEP); 357 if (copyinstr(dcp->c_unitaddr, cpybuf, MAXNAMELEN, 0) != 0) { 358 kmem_free(cpybuf, MAXNAMELEN); 359 if (dcp->c_nodename != NULL) 360 kmem_free(dcp->c_nodename, MAXNAMELEN); 361 kmem_free(dcp, sizeof (*dcp)); 362 return (NDI_FAULT); 363 } 364 cpybuf[MAXNAMELEN - 1] = '\0'; 365 dcp->c_unitaddr = cpybuf; 366 } 367 368 /* 369 * copyin and unpack a user defined nvlist if one was passed 370 */ 371 if (dcp->nvl_user != NULL) { 372 if ((dcp->nvl_usersz == 0) || 373 (dcp->nvl_usersz > DEVCTL_MAX_NVL_USERSZ)) { 374 if (dcp->c_nodename != NULL) 375 kmem_free(dcp->c_nodename, MAXNAMELEN); 376 if (dcp->c_unitaddr != NULL) 377 kmem_free(dcp->c_unitaddr, MAXNAMELEN); 378 kmem_free(dcp, sizeof (*dcp)); 379 return (NDI_FAILURE); 380 } 381 cpybuf = kmem_alloc(dcp->nvl_usersz, KM_SLEEP); 382 if (copyin(dcp->nvl_user, cpybuf, dcp->nvl_usersz) != 0) { 383 kmem_free(cpybuf, dcp->nvl_usersz); 384 if (dcp->c_nodename != NULL) 385 kmem_free(dcp->c_nodename, MAXNAMELEN); 386 if (dcp->c_unitaddr != NULL) 387 kmem_free(dcp->c_unitaddr, MAXNAMELEN); 388 kmem_free(dcp, sizeof (*dcp)); 389 return (NDI_FAULT); 390 } 391 392 if (nvlist_unpack(cpybuf, dcp->nvl_usersz, &dcp->nvl_user, 393 KM_SLEEP)) { 394 kmem_free(cpybuf, dcp->nvl_usersz); 395 if (dcp->c_nodename != NULL) 396 kmem_free(dcp->c_nodename, MAXNAMELEN); 397 if (dcp->c_unitaddr != NULL) 398 kmem_free(dcp->c_unitaddr, MAXNAMELEN); 399 kmem_free(dcp, sizeof (*dcp)); 400 return (NDI_FAULT); 401 } 402 /* 403 * free the buffer containing the packed nvlist 404 */ 405 kmem_free(cpybuf, dcp->nvl_usersz); 406 407 } 408 409 *rdcp = dcp; 410 return (NDI_SUCCESS); 411 } 412 413 /* 414 * free all space allocated to a handle. 415 */ 416 void 417 ndi_dc_freehdl(struct devctl_iocdata *dcp) 418 { 419 ASSERT(dcp != NULL); 420 421 if (dcp->c_nodename != NULL) 422 kmem_free(dcp->c_nodename, MAXNAMELEN); 423 424 if (dcp->c_unitaddr != NULL) 425 kmem_free(dcp->c_unitaddr, MAXNAMELEN); 426 427 nvlist_free(dcp->nvl_user); 428 429 kmem_free(dcp, sizeof (*dcp)); 430 } 431 432 char * 433 ndi_dc_getname(struct devctl_iocdata *dcp) 434 { 435 ASSERT(dcp != NULL); 436 return (dcp->c_nodename); 437 438 } 439 440 char * 441 ndi_dc_getaddr(struct devctl_iocdata *dcp) 442 { 443 ASSERT(dcp != NULL); 444 return (dcp->c_unitaddr); 445 } 446 447 nvlist_t * 448 ndi_dc_get_ap_data(struct devctl_iocdata *dcp) 449 { 450 ASSERT(dcp != NULL); 451 452 return (dcp->nvl_user); 453 } 454 455 /* 456 * Transition the child named by "devname@devaddr" to the online state. 457 * For use by a driver's DEVCTL_DEVICE_ONLINE handler. 458 */ 459 int 460 ndi_devctl_device_online(dev_info_t *dip, struct devctl_iocdata *dcp, 461 uint_t flags) 462 { 463 int rval; 464 char *name; 465 dev_info_t *rdip; 466 467 if (ndi_dc_getname(dcp) == NULL || ndi_dc_getaddr(dcp) == NULL) 468 return (EINVAL); 469 470 name = kmem_alloc(MAXNAMELEN, KM_SLEEP); 471 (void) snprintf(name, MAXNAMELEN, "%s@%s", 472 ndi_dc_getname(dcp), ndi_dc_getaddr(dcp)); 473 474 if ((rval = ndi_devi_config_one(dip, name, &rdip, 475 flags | NDI_DEVI_ONLINE | NDI_CONFIG)) == NDI_SUCCESS) { 476 ndi_rele_devi(rdip); 477 478 /* 479 * Invalidate devfs cached directory contents. For the checks 480 * in the "if" condition see the comment in ndi_devi_online(). 481 */ 482 if (i_ddi_devi_attached(dip) && !DEVI_BUSY_OWNED(dip)) 483 (void) devfs_clean(dip, NULL, 0); 484 485 } else if (rval == NDI_BUSY) { 486 rval = EBUSY; 487 } else if (rval == NDI_FAILURE) { 488 rval = EIO; 489 } 490 491 NDI_DEBUG(flags, (CE_CONT, "%s%d: online: %s: %s\n", 492 ddi_driver_name(dip), ddi_get_instance(dip), name, 493 ((rval == NDI_SUCCESS) ? "ok" : "failed"))); 494 495 kmem_free(name, MAXNAMELEN); 496 497 return (rval); 498 } 499 500 /* 501 * Transition the child named by "devname@devaddr" to the offline state. 502 * For use by a driver's DEVCTL_DEVICE_OFFLINE handler. 503 */ 504 int 505 ndi_devctl_device_offline(dev_info_t *dip, struct devctl_iocdata *dcp, 506 uint_t flags) 507 { 508 int rval; 509 char *name; 510 511 if (ndi_dc_getname(dcp) == NULL || ndi_dc_getaddr(dcp) == NULL) 512 return (EINVAL); 513 514 name = kmem_alloc(MAXNAMELEN, KM_SLEEP); 515 (void) snprintf(name, MAXNAMELEN, "%s@%s", 516 ndi_dc_getname(dcp), ndi_dc_getaddr(dcp)); 517 518 (void) devfs_clean(dip, name, DV_CLEAN_FORCE); 519 rval = ndi_devi_unconfig_one(dip, name, NULL, 520 flags | NDI_DEVI_OFFLINE); 521 522 if (rval == NDI_BUSY) { 523 rval = EBUSY; 524 } else if (rval == NDI_FAILURE) { 525 rval = EIO; 526 } 527 528 NDI_DEBUG(flags, (CE_CONT, "%s%d: offline: %s: %s\n", 529 ddi_driver_name(dip), ddi_get_instance(dip), name, 530 (rval == NDI_SUCCESS) ? "ok" : "failed")); 531 532 kmem_free(name, MAXNAMELEN); 533 534 return (rval); 535 } 536 537 /* 538 * Remove the child named by "devname@devaddr". 539 * For use by a driver's DEVCTL_DEVICE_REMOVE handler. 540 */ 541 int 542 ndi_devctl_device_remove(dev_info_t *dip, struct devctl_iocdata *dcp, 543 uint_t flags) 544 { 545 int rval; 546 char *name; 547 548 if (ndi_dc_getname(dcp) == NULL || ndi_dc_getaddr(dcp) == NULL) 549 return (EINVAL); 550 551 name = kmem_alloc(MAXNAMELEN, KM_SLEEP); 552 (void) snprintf(name, MAXNAMELEN, "%s@%s", 553 ndi_dc_getname(dcp), ndi_dc_getaddr(dcp)); 554 555 (void) devfs_clean(dip, name, DV_CLEAN_FORCE); 556 557 rval = ndi_devi_unconfig_one(dip, name, NULL, flags | NDI_DEVI_REMOVE); 558 559 if (rval == NDI_BUSY) { 560 rval = EBUSY; 561 } else if (rval == NDI_FAILURE) { 562 rval = EIO; 563 } 564 565 NDI_DEBUG(flags, (CE_CONT, "%s%d: remove: %s: %s\n", 566 ddi_driver_name(dip), ddi_get_instance(dip), name, 567 (rval == NDI_SUCCESS) ? "ok" : "failed")); 568 569 kmem_free(name, MAXNAMELEN); 570 571 return (rval); 572 } 573 574 /* 575 * Return devctl state of the child named by "name@addr". 576 * For use by a driver's DEVCTL_DEVICE_GETSTATE handler. 577 */ 578 int 579 ndi_devctl_device_getstate(dev_info_t *parent, struct devctl_iocdata *dcp, 580 uint_t *state) 581 { 582 dev_info_t *dip; 583 char *name, *addr; 584 char *devname; 585 int devnamelen; 586 587 if (parent == NULL || 588 ((name = ndi_dc_getname(dcp)) == NULL) || 589 ((addr = ndi_dc_getaddr(dcp)) == NULL)) 590 return (NDI_FAILURE); 591 592 devnamelen = strlen(name) + strlen(addr) + 2; 593 devname = kmem_alloc(devnamelen, KM_SLEEP); 594 if (strlen(addr) > 0) { 595 (void) snprintf(devname, devnamelen, "%s@%s", name, addr); 596 } else { 597 (void) snprintf(devname, devnamelen, "%s", name); 598 } 599 600 ndi_devi_enter(parent); 601 602 dip = ndi_devi_findchild(parent, devname); 603 kmem_free(devname, devnamelen); 604 605 if (dip == NULL) { 606 ndi_devi_exit(parent); 607 return (NDI_FAILURE); 608 } 609 610 mutex_enter(&(DEVI(dip)->devi_lock)); 611 if (DEVI_IS_DEVICE_OFFLINE(dip)) { 612 *state = DEVICE_OFFLINE; 613 } else if (DEVI_IS_DEVICE_DOWN(dip)) { 614 *state = DEVICE_DOWN; 615 } else { 616 *state = DEVICE_ONLINE; 617 if (devi_stillreferenced(dip) == DEVI_REFERENCED) 618 *state |= DEVICE_BUSY; 619 } 620 621 mutex_exit(&(DEVI(dip)->devi_lock)); 622 ndi_devi_exit(parent); 623 624 return (NDI_SUCCESS); 625 } 626 627 /* 628 * return the current state of the device "dip" 629 * 630 * recommend using ndi_devctl_ioctl() or 631 * ndi_devctl_device_getstate() instead 632 */ 633 int 634 ndi_dc_return_dev_state(dev_info_t *dip, struct devctl_iocdata *dcp) 635 { 636 dev_info_t *pdip; 637 uint_t devstate = 0; 638 639 if ((dip == NULL) || (dcp == NULL)) 640 return (NDI_FAILURE); 641 642 pdip = ddi_get_parent(dip); 643 644 ndi_devi_enter(pdip); 645 mutex_enter(&(DEVI(dip)->devi_lock)); 646 if (DEVI_IS_DEVICE_OFFLINE(dip)) { 647 devstate = DEVICE_OFFLINE; 648 } else if (DEVI_IS_DEVICE_DOWN(dip)) { 649 devstate = DEVICE_DOWN; 650 } else { 651 devstate = DEVICE_ONLINE; 652 if (devi_stillreferenced(dip) == DEVI_REFERENCED) 653 devstate |= DEVICE_BUSY; 654 } 655 656 mutex_exit(&(DEVI(dip)->devi_lock)); 657 ndi_devi_exit(pdip); 658 659 if (copyout(&devstate, dcp->cpyout_buf, sizeof (uint_t)) != 0) 660 return (NDI_FAULT); 661 662 return (NDI_SUCCESS); 663 } 664 665 /* 666 * Return device's bus state 667 * For use by a driver's DEVCTL_BUS_GETSTATE handler. 668 */ 669 int 670 ndi_devctl_bus_getstate(dev_info_t *dip, struct devctl_iocdata *dcp, 671 uint_t *state) 672 { 673 if ((dip == NULL) || (dcp == NULL)) 674 return (NDI_FAILURE); 675 676 return (ndi_get_bus_state(dip, state)); 677 } 678 679 /* 680 * Generic devctl ioctl handler 681 */ 682 int 683 ndi_devctl_ioctl(dev_info_t *dip, int cmd, intptr_t arg, int mode, uint_t flags) 684 { 685 _NOTE(ARGUNUSED(mode)) 686 struct devctl_iocdata *dcp; 687 uint_t state; 688 int rval = ENOTTY; 689 690 /* 691 * read devctl ioctl data 692 */ 693 if (ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS) 694 return (EFAULT); 695 696 switch (cmd) { 697 698 case DEVCTL_BUS_GETSTATE: 699 rval = ndi_devctl_bus_getstate(dip, dcp, &state); 700 if (rval == NDI_SUCCESS) { 701 if (copyout(&state, dcp->cpyout_buf, 702 sizeof (uint_t)) != 0) 703 rval = NDI_FAULT; 704 } 705 break; 706 707 case DEVCTL_DEVICE_ONLINE: 708 rval = ndi_devctl_device_online(dip, dcp, flags); 709 break; 710 711 case DEVCTL_DEVICE_OFFLINE: 712 rval = ndi_devctl_device_offline(dip, dcp, flags); 713 break; 714 715 case DEVCTL_DEVICE_GETSTATE: 716 rval = ndi_devctl_device_getstate(dip, dcp, &state); 717 if (rval == NDI_SUCCESS) { 718 if (copyout(&state, dcp->cpyout_buf, 719 sizeof (uint_t)) != 0) 720 rval = NDI_FAULT; 721 } 722 break; 723 724 case DEVCTL_DEVICE_REMOVE: 725 rval = ndi_devctl_device_remove(dip, dcp, flags); 726 break; 727 728 case DEVCTL_BUS_DEV_CREATE: 729 rval = ndi_dc_devi_create(dcp, dip, 0, NULL); 730 break; 731 732 /* 733 * ioctls for which a generic implementation makes no sense 734 */ 735 case DEVCTL_BUS_RESET: 736 case DEVCTL_BUS_RESETALL: 737 case DEVCTL_DEVICE_RESET: 738 case DEVCTL_AP_CONNECT: 739 case DEVCTL_AP_DISCONNECT: 740 case DEVCTL_AP_INSERT: 741 case DEVCTL_AP_REMOVE: 742 case DEVCTL_AP_CONFIGURE: 743 case DEVCTL_AP_UNCONFIGURE: 744 case DEVCTL_AP_GETSTATE: 745 case DEVCTL_AP_CONTROL: 746 case DEVCTL_BUS_QUIESCE: 747 case DEVCTL_BUS_UNQUIESCE: 748 rval = ENOTSUP; 749 break; 750 } 751 752 ndi_dc_freehdl(dcp); 753 return (rval); 754 } 755 756 /* 757 * Copyout the state of the Attachment Point "ap" to the requesting 758 * user process. 759 */ 760 int 761 ndi_dc_return_ap_state(devctl_ap_state_t *ap, struct devctl_iocdata *dcp) 762 { 763 if ((ap == NULL) || (dcp == NULL)) 764 return (NDI_FAILURE); 765 766 767 if (get_udatamodel() == DATAMODEL_NATIVE) { 768 if (copyout(ap, dcp->cpyout_buf, 769 sizeof (devctl_ap_state_t)) != 0) 770 return (NDI_FAULT); 771 } 772 #ifdef _SYSCALL32_IMPL 773 else { 774 struct devctl_ap_state32 ap_state32; 775 776 ap_state32.ap_rstate = ap->ap_rstate; 777 ap_state32.ap_ostate = ap->ap_ostate; 778 ap_state32.ap_condition = ap->ap_condition; 779 ap_state32.ap_error_code = ap->ap_error_code; 780 ap_state32.ap_in_transition = ap->ap_in_transition; 781 ap_state32.ap_last_change = (time32_t)ap->ap_last_change; 782 if (copyout(&ap_state32, dcp->cpyout_buf, 783 sizeof (devctl_ap_state32_t)) != 0) 784 return (NDI_FAULT); 785 } 786 #endif 787 788 return (NDI_SUCCESS); 789 } 790 791 /* 792 * Copyout the bus state of the bus nexus device "dip" to the requesting 793 * user process. 794 */ 795 int 796 ndi_dc_return_bus_state(dev_info_t *dip, struct devctl_iocdata *dcp) 797 { 798 uint_t devstate = 0; 799 800 if ((dip == NULL) || (dcp == NULL)) 801 return (NDI_FAILURE); 802 803 if (ndi_get_bus_state(dip, &devstate) != NDI_SUCCESS) 804 return (NDI_FAILURE); 805 806 if (copyout(&devstate, dcp->cpyout_buf, sizeof (uint_t)) != 0) 807 return (NDI_FAULT); 808 809 return (NDI_SUCCESS); 810 } 811 812 static int 813 i_dc_devi_create(struct devctl_iocdata *, dev_info_t *, dev_info_t **); 814 815 /* 816 * create a child device node given the property definitions 817 * supplied by the userland process 818 */ 819 int 820 ndi_dc_devi_create(struct devctl_iocdata *dcp, dev_info_t *pdip, int flags, 821 dev_info_t **rdip) 822 { 823 dev_info_t *cdip; 824 int rv; 825 char devnm[MAXNAMELEN]; 826 int nmlen; 827 828 /* 829 * The child device may have been pre-constructed by an earlier 830 * call to this function with the flag DEVCTL_CONSTRUCT set. 831 */ 832 833 if ((cdip = (rdip != NULL) ? *rdip : NULL) == NULL) 834 if ((rv = i_dc_devi_create(dcp, pdip, &cdip)) != 0) 835 return (rv); 836 837 ASSERT(cdip != NULL); 838 839 /* 840 * Return the device node partially constructed if the 841 * DEVCTL_CONSTRUCT flag is set. 842 */ 843 if (flags & DEVCTL_CONSTRUCT) { 844 if (rdip == NULL) { 845 (void) ndi_devi_free(cdip); 846 return (EINVAL); 847 } 848 *rdip = cdip; 849 return (0); 850 } 851 852 /* 853 * Bring the node up to a named but OFFLINE state. The calling 854 * application will need to manage the node from here on. 855 */ 856 if (dcp->flags & DEVCTL_OFFLINE) { 857 /* 858 * In the unlikely event that the dip was somehow attached by 859 * the userland process (and device contracts or LDI opens 860 * were registered against the dip) after it was created by 861 * a previous DEVCTL_CONSTRUCT call, we start notify 862 * proceedings on this dip. Note that we don't need to 863 * return the dip after a failure of the notify since 864 * for a contract or LDI handle to be created the dip was 865 * already available to the user. 866 */ 867 if (e_ddi_offline_notify(cdip) == DDI_FAILURE) { 868 return (EBUSY); 869 } 870 871 /* 872 * hand set the OFFLINE flag to prevent any asynchronous 873 * autoconfiguration operations from attaching this node. 874 */ 875 mutex_enter(&(DEVI(cdip)->devi_lock)); 876 DEVI_SET_DEVICE_OFFLINE(cdip); 877 mutex_exit(&(DEVI(cdip)->devi_lock)); 878 879 e_ddi_offline_finalize(cdip, DDI_SUCCESS); 880 881 rv = ndi_devi_bind_driver(cdip, flags); 882 if (rv != NDI_SUCCESS) { 883 (void) ndi_devi_offline(cdip, NDI_DEVI_REMOVE); 884 return (ENXIO); 885 } 886 887 /* 888 * remove the dev_info node if it failed to bind to a 889 * driver above. 890 */ 891 if (i_ddi_node_state(cdip) < DS_BOUND) { 892 (void) ndi_devi_offline(cdip, NDI_DEVI_REMOVE); 893 return (ENXIO); 894 } 895 896 /* 897 * add the node to the per-driver list and INITCHILD it 898 * to give it a name. 899 */ 900 ndi_devi_enter(pdip); 901 if ((rv = ddi_initchild(pdip, cdip)) != DDI_SUCCESS) { 902 (void) ndi_devi_offline(cdip, NDI_DEVI_REMOVE); 903 ndi_devi_exit(pdip); 904 return (EINVAL); 905 } 906 ndi_devi_exit(pdip); 907 908 } else { 909 /* 910 * Attempt to bring the device ONLINE. If the request to 911 * fails, remove the dev_info node. 912 */ 913 if (ndi_devi_online(cdip, NDI_ONLINE_ATTACH) != NDI_SUCCESS) { 914 (void) ndi_devi_offline(cdip, NDI_DEVI_REMOVE); 915 return (ENXIO); 916 } 917 918 /* 919 * if the node was successfully added but there was 920 * no driver available for the device, remove the node 921 */ 922 if (i_ddi_node_state(cdip) < DS_BOUND) { 923 (void) ndi_devi_offline(cdip, NDI_DEVI_REMOVE); 924 return (ENODEV); 925 } 926 } 927 928 /* 929 * return a handle to the child device 930 * copy out the name of the newly attached child device if 931 * the IOCTL request has provided a copyout buffer. 932 */ 933 if (rdip != NULL) 934 *rdip = cdip; 935 936 if (dcp->cpyout_buf == NULL) 937 return (0); 938 939 ASSERT(ddi_node_name(cdip) != NULL); 940 ASSERT(ddi_get_name_addr(cdip) != NULL); 941 942 nmlen = snprintf(devnm, MAXNAMELEN, "%s@%s", 943 ddi_node_name(cdip), ddi_get_name_addr(cdip)); 944 945 if (copyout(&devnm, dcp->cpyout_buf, nmlen) != 0) { 946 (void) ndi_devi_offline(cdip, NDI_DEVI_REMOVE); 947 return (EFAULT); 948 } 949 return (0); 950 } 951 952 static int 953 i_dc_devi_create(struct devctl_iocdata *dcp, dev_info_t *pdip, 954 dev_info_t **rdip) 955 { 956 957 dev_info_t *cdip; 958 char *cname = NULL; 959 nvlist_t *nvlp = dcp->nvl_user; 960 nvpair_t *npp; 961 char *np; 962 int rv = 0; 963 964 ASSERT(rdip != NULL && *rdip == NULL); 965 966 if ((nvlp == NULL) || 967 (nvlist_lookup_string(nvlp, DC_DEVI_NODENAME, &cname) != 0)) 968 return (EINVAL); 969 970 /* 971 * construct a new dev_info node with a user-provided nodename 972 */ 973 ndi_devi_alloc_sleep(pdip, cname, (pnode_t)DEVI_SID_NODEID, &cdip); 974 975 /* 976 * create hardware properties for each member in the property 977 * list. 978 */ 979 for (npp = nvlist_next_nvpair(nvlp, NULL); (npp != NULL && !rv); 980 npp = nvlist_next_nvpair(nvlp, npp)) { 981 982 np = nvpair_name(npp); 983 984 /* 985 * skip the nodename property 986 */ 987 if (strcmp(np, DC_DEVI_NODENAME) == 0) 988 continue; 989 990 switch (nvpair_type(npp)) { 991 992 case DATA_TYPE_INT32: { 993 int32_t prop_val; 994 995 if ((rv = nvpair_value_int32(npp, &prop_val)) != 0) 996 break; 997 998 (void) ndi_prop_update_int(DDI_DEV_T_NONE, cdip, np, 999 (int)prop_val); 1000 break; 1001 } 1002 1003 case DATA_TYPE_STRING: { 1004 char *prop_val; 1005 1006 if ((rv = nvpair_value_string(npp, &prop_val)) != 0) 1007 break; 1008 1009 (void) ndi_prop_update_string(DDI_DEV_T_NONE, cdip, 1010 np, prop_val); 1011 break; 1012 } 1013 1014 case DATA_TYPE_BYTE_ARRAY: { 1015 uchar_t *val; 1016 uint_t nelms; 1017 1018 if ((rv = nvpair_value_byte_array(npp, &val, 1019 &nelms)) != 0) 1020 break; 1021 1022 (void) ndi_prop_update_byte_array(DDI_DEV_T_NONE, 1023 cdip, np, (uchar_t *)val, nelms); 1024 break; 1025 } 1026 1027 case DATA_TYPE_INT32_ARRAY: { 1028 int32_t *val; 1029 uint_t nelms; 1030 1031 if ((rv = nvpair_value_int32_array(npp, &val, 1032 &nelms)) != 0) 1033 break; 1034 1035 (void) ndi_prop_update_int_array(DDI_DEV_T_NONE, 1036 cdip, np, val, nelms); 1037 break; 1038 } 1039 1040 case DATA_TYPE_STRING_ARRAY: { 1041 char **val; 1042 uint_t nelms; 1043 1044 if ((rv = nvpair_value_string_array(npp, &val, 1045 &nelms)) != 0) 1046 break; 1047 1048 (void) ndi_prop_update_string_array(DDI_DEV_T_NONE, 1049 cdip, np, val, nelms); 1050 break; 1051 } 1052 1053 /* 1054 * unsupported property data type 1055 */ 1056 default: 1057 rv = EINVAL; 1058 } 1059 } 1060 1061 /* 1062 * something above failed 1063 * destroy the partially child device and abort the request 1064 */ 1065 if (rv != 0) { 1066 (void) ndi_devi_free(cdip); 1067 return (rv); 1068 } 1069 1070 *rdip = cdip; 1071 return (0); 1072 } 1073 1074 /* 1075 * return current soft bus state of bus nexus "dip" 1076 */ 1077 int 1078 ndi_get_bus_state(dev_info_t *dip, uint_t *rstate) 1079 { 1080 if (dip == NULL || rstate == NULL) 1081 return (NDI_FAILURE); 1082 1083 if (DEVI(dip)->devi_ops->devo_bus_ops == NULL) 1084 return (NDI_FAILURE); 1085 1086 mutex_enter(&(DEVI(dip)->devi_lock)); 1087 if (DEVI_IS_BUS_QUIESCED(dip)) 1088 *rstate = BUS_QUIESCED; 1089 else if (DEVI_IS_BUS_DOWN(dip)) 1090 *rstate = BUS_SHUTDOWN; 1091 else 1092 *rstate = BUS_ACTIVE; 1093 mutex_exit(&(DEVI(dip)->devi_lock)); 1094 return (NDI_SUCCESS); 1095 } 1096 1097 /* 1098 * Set the soft state of bus nexus "dip" 1099 */ 1100 int 1101 ndi_set_bus_state(dev_info_t *dip, uint_t state) 1102 { 1103 int rv = NDI_SUCCESS; 1104 1105 if (dip == NULL) 1106 return (NDI_FAILURE); 1107 1108 mutex_enter(&(DEVI(dip)->devi_lock)); 1109 1110 switch (state) { 1111 case BUS_QUIESCED: 1112 DEVI_SET_BUS_QUIESCE(dip); 1113 break; 1114 1115 case BUS_ACTIVE: 1116 DEVI_SET_BUS_ACTIVE(dip); 1117 DEVI_SET_BUS_UP(dip); 1118 break; 1119 1120 case BUS_SHUTDOWN: 1121 DEVI_SET_BUS_DOWN(dip); 1122 break; 1123 1124 default: 1125 rv = NDI_FAILURE; 1126 } 1127 1128 mutex_exit(&(DEVI(dip)->devi_lock)); 1129 return (rv); 1130 } 1131 1132 /* 1133 * These dummy functions are obsolete and may be removed. 1134 * Retained for existing driver compatibility only. 1135 * Drivers should be fixed not to use these functions. 1136 * Don't write new code using these obsolete interfaces. 1137 */ 1138 /*ARGSUSED*/ 1139 void 1140 i_ndi_block_device_tree_changes(uint_t *lkcnt) /* obsolete */ 1141 { 1142 /* obsolete dummy function */ 1143 } 1144 1145 /*ARGSUSED*/ 1146 void 1147 i_ndi_allow_device_tree_changes(uint_t lkcnt) /* obsolete */ 1148 { 1149 /* obsolete dummy function */ 1150 } 1151 1152 /* 1153 * Single thread entry into per-driver list 1154 */ 1155 /*ARGSUSED*/ 1156 void 1157 e_ddi_enter_driver_list(struct devnames *dnp, int *listcnt) /* obsolete */ 1158 { 1159 /* obsolete dummy function */ 1160 } 1161 1162 /* 1163 * release the per-driver list 1164 */ 1165 /*ARGSUSED*/ 1166 void 1167 e_ddi_exit_driver_list(struct devnames *dnp, int listcnt) /* obsolete */ 1168 { 1169 /* obsolete dummy function */ 1170 } 1171 1172 /* 1173 * Attempt to enter driver list 1174 */ 1175 /*ARGSUSED*/ 1176 int 1177 e_ddi_tryenter_driver_list(struct devnames *dnp, int *listcnt) /* obsolete */ 1178 { 1179 return (1); /* obsolete dummy function */ 1180 } 1181 1182 /* 1183 * ndi event handling support functions: 1184 * The NDI event support model is as follows: 1185 * 1186 * The nexus driver defines a set of events using some static structures (so 1187 * these structures can be shared by all instances of the nexus driver). 1188 * The nexus driver allocates an event handle and binds the event set 1189 * to this handle. The nexus driver's event busop functions can just 1190 * call the appropriate NDI event support function using this handle 1191 * as the first argument. 1192 * 1193 * The reasoning for tying events to the device tree is that the entity 1194 * generating the callback will typically be one of the device driver's 1195 * ancestors in the tree. 1196 */ 1197 static int ndi_event_debug = 0; 1198 1199 #ifdef DEBUG 1200 #define NDI_EVENT_DEBUG ndi_event_debug 1201 #endif /* DEBUG */ 1202 1203 /* 1204 * allocate a new ndi event handle 1205 */ 1206 int 1207 ndi_event_alloc_hdl(dev_info_t *dip, ddi_iblock_cookie_t cookie, 1208 ndi_event_hdl_t *handle, uint_t flag) 1209 { 1210 struct ndi_event_hdl *ndi_event_hdl; 1211 1212 ndi_event_hdl = kmem_zalloc(sizeof (struct ndi_event_hdl), 1213 ((flag & NDI_NOSLEEP) ? KM_NOSLEEP : KM_SLEEP)); 1214 1215 if (!ndi_event_hdl) { 1216 return (NDI_FAILURE); 1217 } 1218 1219 ndi_event_hdl->ndi_evthdl_dip = dip; 1220 ndi_event_hdl->ndi_evthdl_iblock_cookie = cookie; 1221 mutex_init(&ndi_event_hdl->ndi_evthdl_mutex, NULL, 1222 MUTEX_DRIVER, (void *)cookie); 1223 1224 mutex_init(&ndi_event_hdl->ndi_evthdl_cb_mutex, NULL, 1225 MUTEX_DRIVER, (void *)cookie); 1226 1227 *handle = (ndi_event_hdl_t)ndi_event_hdl; 1228 1229 return (NDI_SUCCESS); 1230 } 1231 1232 /* 1233 * free the ndi event handle 1234 */ 1235 int 1236 ndi_event_free_hdl(ndi_event_hdl_t handle) 1237 { 1238 struct ndi_event_hdl *ndi_event_hdl = (struct ndi_event_hdl *)handle; 1239 ndi_event_cookie_t *cookie; 1240 ndi_event_cookie_t *free; 1241 1242 ASSERT(handle); 1243 1244 mutex_enter(&ndi_event_hdl->ndi_evthdl_mutex); 1245 mutex_enter(&ndi_event_hdl->ndi_evthdl_cb_mutex); 1246 1247 cookie = ndi_event_hdl->ndi_evthdl_cookie_list; 1248 1249 /* deallocate all defined cookies */ 1250 while (cookie != NULL) { 1251 ASSERT(cookie->callback_list == NULL); 1252 free = cookie; 1253 cookie = cookie->next_cookie; 1254 1255 kmem_free(free, sizeof (ndi_event_cookie_t)); 1256 } 1257 1258 1259 mutex_exit(&ndi_event_hdl->ndi_evthdl_cb_mutex); 1260 mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex); 1261 1262 /* destroy mutexes */ 1263 mutex_destroy(&ndi_event_hdl->ndi_evthdl_mutex); 1264 mutex_destroy(&ndi_event_hdl->ndi_evthdl_cb_mutex); 1265 1266 /* free event handle */ 1267 kmem_free(ndi_event_hdl, sizeof (struct ndi_event_hdl)); 1268 1269 return (NDI_SUCCESS); 1270 } 1271 1272 1273 /* 1274 * ndi_event_bind_set() adds a set of events to the NDI event 1275 * handle. 1276 * 1277 * Events generated by high level interrupts should not 1278 * be mixed in the same event set with events generated by 1279 * normal interrupts or kernel events. 1280 * 1281 * This function can be called multiple times to bind 1282 * additional sets to the event handle. 1283 * However, events generated by high level interrupts cannot 1284 * be bound to a handle that already has bound events generated 1285 * by normal interrupts or from kernel context and vice versa. 1286 */ 1287 int 1288 ndi_event_bind_set(ndi_event_hdl_t handle, 1289 ndi_event_set_t *ndi_events, 1290 uint_t flag) 1291 { 1292 struct ndi_event_hdl *ndi_event_hdl; 1293 ndi_event_cookie_t *next, *prev, *new_cookie; 1294 uint_t i, len; 1295 uint_t dup = 0; 1296 uint_t high_plevels, other_plevels; 1297 ndi_event_definition_t *ndi_event_defs; 1298 1299 int km_flag = ((flag & NDI_NOSLEEP) ? KM_NOSLEEP : KM_SLEEP); 1300 1301 ASSERT(handle); 1302 ASSERT(ndi_events); 1303 1304 /* 1305 * binding must be performed during attach/detach 1306 */ 1307 if (!DEVI_IS_ATTACHING(handle->ndi_evthdl_dip) && 1308 !DEVI_IS_DETACHING(handle->ndi_evthdl_dip)) { 1309 cmn_err(CE_WARN, "ndi_event_bind_set must be called within " 1310 "attach or detach"); 1311 return (NDI_FAILURE); 1312 } 1313 1314 /* 1315 * if it is not the correct version or the event set is 1316 * empty, bail out 1317 */ 1318 if (ndi_events->ndi_events_version != NDI_EVENTS_REV1) 1319 return (NDI_FAILURE); 1320 1321 ndi_event_hdl = (struct ndi_event_hdl *)handle; 1322 ndi_event_defs = ndi_events->ndi_event_defs; 1323 high_plevels = other_plevels = 0; 1324 1325 mutex_enter(&ndi_event_hdl->ndi_evthdl_mutex); 1326 1327 /* check for mixing events at high level with the other types */ 1328 for (i = 0; i < ndi_events->ndi_n_events; i++) { 1329 if (ndi_event_defs[i].ndi_event_plevel == EPL_HIGHLEVEL) { 1330 high_plevels++; 1331 } else { 1332 other_plevels++; 1333 } 1334 } 1335 1336 /* 1337 * bail out if high level events are mixed with other types in this 1338 * event set or the set is incompatible with the set in the handle 1339 */ 1340 if ((high_plevels && other_plevels) || 1341 (other_plevels && ndi_event_hdl->ndi_evthdl_high_plevels) || 1342 (high_plevels && ndi_event_hdl->ndi_evthdl_other_plevels)) { 1343 mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex); 1344 1345 return (NDI_FAILURE); 1346 } 1347 1348 /* 1349 * check for duplicate events in both the existing handle 1350 * and the event set, add events if not duplicates 1351 */ 1352 next = ndi_event_hdl->ndi_evthdl_cookie_list; 1353 for (i = 0; i < ndi_events->ndi_n_events; i++) { 1354 while (next != NULL) { 1355 len = strlen(NDI_EVENT_NAME(next)) + 1; 1356 if (strncmp(NDI_EVENT_NAME(next), 1357 ndi_event_defs[i].ndi_event_name, len) == 0) { 1358 dup = 1; 1359 break; 1360 } 1361 1362 prev = next; 1363 next = next->next_cookie; 1364 } 1365 1366 if (dup == 0) { 1367 new_cookie = kmem_zalloc(sizeof (ndi_event_cookie_t), 1368 km_flag); 1369 1370 if (!new_cookie) 1371 return (NDI_FAILURE); 1372 1373 if (ndi_event_hdl->ndi_evthdl_n_events == 0) { 1374 ndi_event_hdl->ndi_evthdl_cookie_list = 1375 new_cookie; 1376 } else { 1377 prev->next_cookie = new_cookie; 1378 } 1379 1380 ndi_event_hdl->ndi_evthdl_n_events++; 1381 1382 /* 1383 * set up new cookie 1384 */ 1385 new_cookie->definition = &ndi_event_defs[i]; 1386 new_cookie->ddip = ndi_event_hdl->ndi_evthdl_dip; 1387 1388 } else { 1389 /* 1390 * event not added, must correct plevel numbers 1391 */ 1392 if (ndi_event_defs[i].ndi_event_plevel == 1393 EPL_HIGHLEVEL) { 1394 high_plevels--; 1395 } else { 1396 other_plevels--; 1397 } 1398 } 1399 1400 dup = 0; 1401 next = ndi_event_hdl->ndi_evthdl_cookie_list; 1402 prev = NULL; 1403 1404 } 1405 1406 ndi_event_hdl->ndi_evthdl_high_plevels += high_plevels; 1407 ndi_event_hdl->ndi_evthdl_other_plevels += other_plevels; 1408 1409 ASSERT((ndi_event_hdl->ndi_evthdl_high_plevels == 0) || 1410 (ndi_event_hdl->ndi_evthdl_other_plevels == 0)); 1411 1412 #ifdef NDI_EVENT_DEBUG 1413 if (ndi_event_debug) { 1414 ndi_event_dump_hdl(ndi_event_hdl, "ndi_event_bind_set"); 1415 } 1416 #endif /* NDI_EVENT_DEBUG */ 1417 1418 mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex); 1419 1420 return (NDI_SUCCESS); 1421 } 1422 1423 /* 1424 * ndi_event_unbind_set() unbinds a set of events, previously 1425 * bound using ndi_event_bind_set(), from the NDI event 1426 * handle. 1427 * 1428 * This routine will unbind all events in the event set. If an event, 1429 * specified in the event set, is not found in the handle, this 1430 * routine will proceed onto the next member of the set as if the event 1431 * was never specified. 1432 * 1433 * The event set may be a subset of the set of events that 1434 * was previously bound to the handle. For example, events 1435 * can be individually unbound. 1436 * 1437 * An event cannot be unbound if callbacks are still 1438 * registered against the event. 1439 */ 1440 /*ARGSUSED*/ 1441 int 1442 ndi_event_unbind_set(ndi_event_hdl_t handle, ndi_event_set_t *ndi_events, 1443 uint_t flag) 1444 { 1445 ndi_event_definition_t *ndi_event_defs; 1446 int len; 1447 uint_t i; 1448 int rval; 1449 ndi_event_cookie_t *cookie_list; 1450 ndi_event_cookie_t *prev = NULL; 1451 1452 ASSERT(ndi_events); 1453 ASSERT(handle); 1454 1455 /* 1456 * binding must be performed during attach/detac 1457 */ 1458 if (!DEVI_IS_ATTACHING(handle->ndi_evthdl_dip) && 1459 !DEVI_IS_DETACHING(handle->ndi_evthdl_dip)) { 1460 cmn_err(CE_WARN, "ndi_event_bind_set must be called within " 1461 "attach or detach"); 1462 return (NDI_FAILURE); 1463 } 1464 1465 /* bail out if ndi_event_set is outdated */ 1466 if (ndi_events->ndi_events_version != NDI_EVENTS_REV1) { 1467 return (NDI_FAILURE); 1468 } 1469 1470 ASSERT(ndi_events->ndi_event_defs); 1471 1472 ndi_event_defs = ndi_events->ndi_event_defs; 1473 1474 mutex_enter(&handle->ndi_evthdl_mutex); 1475 mutex_enter(&handle->ndi_evthdl_cb_mutex); 1476 1477 /* 1478 * Verify that all events in the event set are eligible 1479 * for unbinding(ie. there are no outstanding callbacks). 1480 * If any one of the events are ineligible, fail entire 1481 * operation. 1482 */ 1483 1484 for (i = 0; i < ndi_events->ndi_n_events; i++) { 1485 cookie_list = handle->ndi_evthdl_cookie_list; 1486 while (cookie_list != NULL) { 1487 len = strlen(NDI_EVENT_NAME(cookie_list)) + 1; 1488 if (strncmp(NDI_EVENT_NAME(cookie_list), 1489 ndi_event_defs[i].ndi_event_name, len) == 0) { 1490 1491 ASSERT(cookie_list->callback_list == NULL); 1492 if (cookie_list->callback_list) { 1493 rval = NDI_FAILURE; 1494 goto done; 1495 } 1496 break; 1497 } else { 1498 cookie_list = cookie_list->next_cookie; 1499 } 1500 } 1501 } 1502 1503 /* 1504 * remove all events found within the handle 1505 * If an event is not found, this function will proceed as if the event 1506 * was never specified. 1507 */ 1508 1509 for (i = 0; i < ndi_events->ndi_n_events; i++) { 1510 cookie_list = handle->ndi_evthdl_cookie_list; 1511 prev = NULL; 1512 while (cookie_list != NULL) { 1513 len = strlen(NDI_EVENT_NAME(cookie_list)) + 1; 1514 if (strncmp(NDI_EVENT_NAME(cookie_list), 1515 ndi_event_defs[i].ndi_event_name, len) == 0) { 1516 1517 /* 1518 * can not unbind an event definition with 1519 * outstanding callbacks 1520 */ 1521 if (cookie_list->callback_list) { 1522 rval = NDI_FAILURE; 1523 goto done; 1524 } 1525 1526 /* remove this cookie from the list */ 1527 if (prev != NULL) { 1528 prev->next_cookie = 1529 cookie_list->next_cookie; 1530 } else { 1531 handle->ndi_evthdl_cookie_list = 1532 cookie_list->next_cookie; 1533 } 1534 1535 /* adjust plevel counts */ 1536 if (NDI_EVENT_PLEVEL(cookie_list) == 1537 EPL_HIGHLEVEL) { 1538 handle->ndi_evthdl_high_plevels--; 1539 } else { 1540 handle->ndi_evthdl_other_plevels--; 1541 } 1542 1543 /* adjust cookie count */ 1544 handle->ndi_evthdl_n_events--; 1545 1546 /* free the cookie */ 1547 kmem_free(cookie_list, 1548 sizeof (ndi_event_cookie_t)); 1549 1550 cookie_list = handle->ndi_evthdl_cookie_list; 1551 break; 1552 1553 } else { 1554 prev = cookie_list; 1555 cookie_list = cookie_list->next_cookie; 1556 } 1557 1558 } 1559 1560 } 1561 1562 #ifdef NDI_EVENT_DEBUG 1563 if (ndi_event_debug) { 1564 ndi_event_dump_hdl(handle, "ndi_event_unbind_set"); 1565 } 1566 #endif /* NDI_EVENT_DEBUG */ 1567 1568 rval = NDI_SUCCESS; 1569 1570 done: 1571 mutex_exit(&handle->ndi_evthdl_cb_mutex); 1572 mutex_exit(&handle->ndi_evthdl_mutex); 1573 1574 return (rval); 1575 } 1576 1577 /* 1578 * ndi_event_retrieve_cookie(): 1579 * Return an event cookie for eventname if this nexus driver 1580 * has defined the named event. The event cookie returned 1581 * by this function is used to register callback handlers 1582 * for the event. 1583 * 1584 * ndi_event_retrieve_cookie() is intended to be used in the 1585 * nexus driver's bus_get_eventcookie busop routine. 1586 * 1587 * If the event is not defined by this bus nexus driver, and flag 1588 * does not include NDI_EVENT_NOPASS, then ndi_event_retrieve_cookie() 1589 * will pass the request up the device tree hierarchy by calling 1590 * ndi_busop_get_eventcookie(9N). 1591 * If the event is not defined by this bus nexus driver, and flag 1592 * does include NDI_EVENT_NOPASS, ndi_event_retrieve_cookie() 1593 * will return NDI_FAILURE. The caller may then determine what further 1594 * action to take, such as using a different handle, passing the 1595 * request up the device tree using ndi_busop_get_eventcookie(9N), 1596 * or returning the failure to the caller, thus blocking the 1597 * progress of the request up the tree. 1598 */ 1599 int 1600 ndi_event_retrieve_cookie(ndi_event_hdl_t handle, 1601 dev_info_t *rdip, 1602 char *eventname, 1603 ddi_eventcookie_t *cookiep, 1604 uint_t flag) 1605 { 1606 struct ndi_event_hdl *ndi_event_hdl = (struct ndi_event_hdl *)handle; 1607 int len; 1608 ndi_event_cookie_t *cookie_list; 1609 1610 mutex_enter(&ndi_event_hdl->ndi_evthdl_mutex); 1611 1612 cookie_list = ndi_event_hdl->ndi_evthdl_cookie_list; 1613 /* 1614 * search the cookie list for the event name and return 1615 * cookie if found. 1616 */ 1617 while (cookie_list != NULL) { 1618 1619 len = strlen(NDI_EVENT_NAME(cookie_list)) + 1; 1620 if (strncmp(NDI_EVENT_NAME(cookie_list), eventname, 1621 len) == 0) { 1622 *cookiep = (ddi_eventcookie_t)cookie_list; 1623 1624 mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex); 1625 return (NDI_SUCCESS); 1626 } 1627 1628 cookie_list = cookie_list->next_cookie; 1629 } 1630 1631 mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex); 1632 /* 1633 * event was not found, pass up or return failure 1634 */ 1635 if ((flag & NDI_EVENT_NOPASS) == 0) { 1636 return (ndi_busop_get_eventcookie( 1637 ndi_event_hdl->ndi_evthdl_dip, rdip, eventname, cookiep)); 1638 } else { 1639 return (NDI_FAILURE); 1640 } 1641 } 1642 1643 /* 1644 * check whether this nexus defined this event and look up attributes 1645 */ 1646 static int 1647 ndi_event_is_defined(ndi_event_hdl_t handle, 1648 ddi_eventcookie_t cookie, int *attributes) 1649 { 1650 1651 struct ndi_event_hdl *ndi_event_hdl = (struct ndi_event_hdl *)handle; 1652 ndi_event_cookie_t *cookie_list; 1653 1654 ASSERT(mutex_owned(&handle->ndi_evthdl_mutex)); 1655 1656 cookie_list = ndi_event_hdl->ndi_evthdl_cookie_list; 1657 while (cookie_list != NULL) { 1658 if (cookie_list == NDI_EVENT(cookie)) { 1659 if (attributes) 1660 *attributes = 1661 NDI_EVENT_ATTRIBUTES(cookie_list); 1662 1663 return (NDI_SUCCESS); 1664 } 1665 1666 cookie_list = cookie_list->next_cookie; 1667 } 1668 1669 return (NDI_FAILURE); 1670 } 1671 1672 /* 1673 * ndi_event_add_callback(): adds an event callback registration 1674 * to the event cookie defining this event. 1675 * 1676 * Refer also to bus_add_eventcall(9n) and ndi_busop_add_eventcall(9n). 1677 * 1678 * ndi_event_add_callback(9n) is intended to be used in 1679 * the nexus driver's bus_add_eventcall(9n) busop function. 1680 * 1681 * If the event is not defined by this bus nexus driver, 1682 * ndi_event_add_callback() will return NDI_FAILURE. 1683 */ 1684 int 1685 ndi_event_add_callback(ndi_event_hdl_t handle, dev_info_t *child_dip, 1686 ddi_eventcookie_t cookie, 1687 void (*event_callback)(dev_info_t *, 1688 ddi_eventcookie_t, void *arg, void *impldata), 1689 void *arg, 1690 uint_t flag, 1691 ddi_callback_id_t *cb_id) 1692 { 1693 struct ndi_event_hdl *ndi_event_hdl = (struct ndi_event_hdl *)handle; 1694 int km_flag = ((flag & NDI_NOSLEEP) ? KM_NOSLEEP : KM_SLEEP); 1695 ndi_event_callbacks_t *cb; 1696 1697 mutex_enter(&ndi_event_hdl->ndi_evthdl_mutex); 1698 1699 /* 1700 * if the event was not bound to this handle, return failure 1701 */ 1702 if (ndi_event_is_defined(handle, cookie, NULL) != NDI_SUCCESS) { 1703 1704 mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex); 1705 return (NDI_FAILURE); 1706 1707 } 1708 1709 mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex); 1710 1711 /* 1712 * allocate space for a callback structure 1713 */ 1714 cb = kmem_zalloc(sizeof (ndi_event_callbacks_t), km_flag); 1715 if (cb == NULL) { 1716 return (NDI_FAILURE); 1717 } 1718 1719 mutex_enter(&ndi_event_hdl->ndi_evthdl_mutex); 1720 1721 /* initialize callback structure */ 1722 cb->ndi_evtcb_dip = child_dip; 1723 cb->ndi_evtcb_callback = event_callback; 1724 cb->ndi_evtcb_arg = arg; 1725 cb->ndi_evtcb_cookie = cookie; 1726 cb->devname = (char *)ddi_driver_name(child_dip); 1727 1728 *cb_id = (ddi_callback_id_t)cb; 1729 mutex_enter(&ndi_event_hdl->ndi_evthdl_cb_mutex); 1730 1731 /* add this callback structure to the list */ 1732 if (NDI_EVENT(cookie)->callback_list) { 1733 cb->ndi_evtcb_next = NDI_EVENT(cookie)->callback_list; 1734 NDI_EVENT(cookie)->callback_list->ndi_evtcb_prev = cb; 1735 NDI_EVENT(cookie)->callback_list = cb; 1736 } else { 1737 NDI_EVENT(cookie)->callback_list = cb; 1738 } 1739 #ifdef NDI_EVENT_DEBUG 1740 if (ndi_event_debug) { 1741 ndi_event_dump_hdl(ndi_event_hdl, "ndi_event_add_callback"); 1742 } 1743 #endif /* NDI_EVENT_DEBUG */ 1744 1745 mutex_exit(&ndi_event_hdl->ndi_evthdl_cb_mutex); 1746 mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex); 1747 1748 return (NDI_SUCCESS); 1749 } 1750 1751 /* 1752 * ndi_event_remove_callback(): 1753 * 1754 * ndi_event_remove_callback() removes a callback that was 1755 * previously registered using ndi_event_add_callback(9N). 1756 * Refer also to bus_remove_eventcall(9n) and 1757 * ndi_busop_remove_eventcall(9n). 1758 * ndi_event_remove_callback(9n) is intended to be used in 1759 * the nexus driver's bus_remove_eventcall (9n) busop function. 1760 * If the event is not defined by this bus nexus driver, 1761 * ndi_event_remove_callback() will return NDI_FAILURE. 1762 */ 1763 static void do_ndi_event_remove_callback(struct ndi_event_hdl *ndi_event_hdl, 1764 ddi_callback_id_t cb_id); 1765 1766 int 1767 ndi_event_remove_callback(ndi_event_hdl_t handle, ddi_callback_id_t cb_id) 1768 { 1769 struct ndi_event_hdl *ndi_event_hdl = (struct ndi_event_hdl *)handle; 1770 1771 ASSERT(cb_id); 1772 1773 mutex_enter(&ndi_event_hdl->ndi_evthdl_mutex); 1774 mutex_enter(&ndi_event_hdl->ndi_evthdl_cb_mutex); 1775 1776 do_ndi_event_remove_callback(ndi_event_hdl, cb_id); 1777 1778 mutex_exit(&ndi_event_hdl->ndi_evthdl_cb_mutex); 1779 mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex); 1780 1781 return (NDI_SUCCESS); 1782 } 1783 1784 /*ARGSUSED*/ 1785 static void 1786 do_ndi_event_remove_callback(struct ndi_event_hdl *ndi_event_hdl, 1787 ddi_callback_id_t cb_id) 1788 { 1789 ndi_event_callbacks_t *cb = (ndi_event_callbacks_t *)cb_id; 1790 ASSERT(cb); 1791 1792 ASSERT(mutex_owned(&ndi_event_hdl->ndi_evthdl_mutex)); 1793 ASSERT(mutex_owned(&ndi_event_hdl->ndi_evthdl_cb_mutex)); 1794 1795 /* remove from callback linked list */ 1796 if (cb->ndi_evtcb_prev) { 1797 cb->ndi_evtcb_prev->ndi_evtcb_next = cb->ndi_evtcb_next; 1798 } 1799 1800 if (cb->ndi_evtcb_next) { 1801 cb->ndi_evtcb_next->ndi_evtcb_prev = cb->ndi_evtcb_prev; 1802 } 1803 1804 if (NDI_EVENT(cb->ndi_evtcb_cookie)->callback_list == cb) { 1805 NDI_EVENT(cb->ndi_evtcb_cookie)->callback_list = 1806 cb->ndi_evtcb_next; 1807 } 1808 1809 kmem_free(cb, sizeof (ndi_event_callbacks_t)); 1810 } 1811 1812 /* 1813 * ndi_event_run_callbacks() performs event callbacks for the event 1814 * specified by cookie, if this is among those bound to the 1815 * supplied handle. 1816 * If the event is among those bound to the handle, none, 1817 * some, or all of the handlers registered for the event 1818 * will be called, according to the delivery attributes of 1819 * the event. 1820 * If the event attributes include NDI_EVENT_POST_TO_ALL 1821 * (the default), all the handlers for the event will be 1822 * called in an unspecified order. 1823 * If the event attributes include NDI_EVENT_POST_TO_TGT, only 1824 * the handlers (if any) registered by the driver identified by 1825 * rdip will be called. 1826 * If the event identified by cookie is not bound to the handle, 1827 * NDI_FAILURE will be returned. 1828 */ 1829 int 1830 ndi_event_run_callbacks(ndi_event_hdl_t handle, dev_info_t *child_dip, 1831 ddi_eventcookie_t cookie, void *bus_impldata) 1832 { 1833 struct ndi_event_hdl *ndi_event_hdl = (struct ndi_event_hdl *)handle; 1834 ndi_event_callbacks_t *next, *cb; 1835 int attributes; 1836 1837 mutex_enter(&ndi_event_hdl->ndi_evthdl_mutex); 1838 1839 /* if this is not our event, fail */ 1840 if (ndi_event_is_defined(handle, cookie, &attributes) != 1841 NDI_SUCCESS) { 1842 1843 mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex); 1844 return (NDI_FAILURE); 1845 } 1846 1847 mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex); 1848 1849 #ifdef NDI_EVENT_DEBUG 1850 if (ndi_event_debug) { 1851 cmn_err(CE_CONT, "ndi_event_run_callbacks:\n\t" 1852 "producer dip=%p (%s%d): cookie = %p, name = %s\n", 1853 (void *)ndi_event_hdl->ndi_evthdl_dip, 1854 ddi_node_name(ndi_event_hdl->ndi_evthdl_dip), 1855 ddi_get_instance(ndi_event_hdl->ndi_evthdl_dip), 1856 (void *)cookie, 1857 ndi_event_cookie_to_name(handle, cookie)); 1858 } 1859 #endif /* #ifdef NDI_EVENT_DEBUG */ 1860 1861 1862 /* 1863 * The callback handlers may call conversion functions. The conversion 1864 * functions may hold the ndi_evthdl_mutex during execution. Thus, to 1865 * avoid a recursive mutex problem, only the ndi_evthdl_cb_mutex is 1866 * held. The ndi_evthdl_mutex is not held when running the callbacks. 1867 */ 1868 mutex_enter(&ndi_event_hdl->ndi_evthdl_cb_mutex); 1869 1870 /* perform callbacks */ 1871 next = NDI_EVENT(cookie)->callback_list; 1872 while (next != NULL) { 1873 1874 cb = next; 1875 next = next->ndi_evtcb_next; 1876 1877 ASSERT(cb->ndi_evtcb_cookie == cookie); 1878 1879 if (attributes == NDI_EVENT_POST_TO_TGT && 1880 child_dip != cb->ndi_evtcb_dip) { 1881 continue; 1882 } 1883 1884 cb->ndi_evtcb_callback(cb->ndi_evtcb_dip, cb->ndi_evtcb_cookie, 1885 cb->ndi_evtcb_arg, bus_impldata); 1886 1887 #ifdef NDI_EVENT_DEBUG 1888 if (ndi_event_debug) { 1889 cmn_err(CE_CONT, 1890 "\t\tconsumer dip=%p (%s%d)\n", 1891 (void *)cb->ndi_evtcb_dip, 1892 ddi_node_name(cb->ndi_evtcb_dip), 1893 ddi_get_instance(cb->ndi_evtcb_dip)); 1894 } 1895 #endif 1896 1897 } 1898 1899 mutex_exit(&ndi_event_hdl->ndi_evthdl_cb_mutex); 1900 1901 #ifdef NDI_EVENT_DEBUG 1902 if (ndi_event_debug) { 1903 mutex_enter(&ndi_event_hdl->ndi_evthdl_mutex); 1904 ndi_event_dump_hdl(ndi_event_hdl, "ndi_event_run_callbacks"); 1905 mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex); 1906 } 1907 #endif /* NDI_EVENT_DEBUG */ 1908 1909 return (NDI_SUCCESS); 1910 } 1911 1912 1913 /* 1914 * perform one callback for a specified cookie and just one target 1915 */ 1916 int 1917 ndi_event_do_callback(ndi_event_hdl_t handle, dev_info_t *child_dip, 1918 ddi_eventcookie_t cookie, void *bus_impldata) 1919 { 1920 struct ndi_event_hdl *ndi_event_hdl = (struct ndi_event_hdl *)handle; 1921 ndi_event_callbacks_t *next, *cb; 1922 int attributes; 1923 1924 mutex_enter(&ndi_event_hdl->ndi_evthdl_mutex); 1925 1926 /* if this is not our event, fail */ 1927 if (ndi_event_is_defined(handle, cookie, &attributes) != 1928 NDI_SUCCESS) { 1929 1930 mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex); 1931 1932 return (NDI_FAILURE); 1933 } 1934 1935 mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex); 1936 1937 #ifdef NDI_EVENT_DEBUG 1938 if (ndi_event_debug) { 1939 cmn_err(CE_CONT, "ndi_event_run_callbacks:\n\t" 1940 "producer dip=%p (%s%d): cookie = %p, name = %s\n", 1941 (void *)ndi_event_hdl->ndi_evthdl_dip, 1942 ddi_node_name(ndi_event_hdl->ndi_evthdl_dip), 1943 ddi_get_instance(ndi_event_hdl->ndi_evthdl_dip), 1944 (void *)cookie, 1945 ndi_event_cookie_to_name(handle, cookie)); 1946 } 1947 #endif 1948 1949 1950 /* 1951 * we only grab the cb mutex because the callback handlers 1952 * may call the conversion functions which would cause a recursive 1953 * mutex problem 1954 */ 1955 mutex_enter(&ndi_event_hdl->ndi_evthdl_cb_mutex); 1956 1957 /* perform callbacks */ 1958 for (next = NDI_EVENT(cookie)->callback_list; next != NULL; ) { 1959 cb = next; 1960 next = next->ndi_evtcb_next; 1961 1962 if (cb->ndi_evtcb_dip == child_dip) { 1963 cb->ndi_evtcb_callback(cb->ndi_evtcb_dip, 1964 cb->ndi_evtcb_cookie, cb->ndi_evtcb_arg, 1965 bus_impldata); 1966 1967 #ifdef NDI_EVENT_DEBUG 1968 if (ndi_event_debug) { 1969 cmn_err(CE_CONT, 1970 "\t\tconsumer dip=%p (%s%d)\n", 1971 (void *)cb->ndi_evtcb_dip, 1972 ddi_node_name(cb->ndi_evtcb_dip), 1973 ddi_get_instance(cb->ndi_evtcb_dip)); 1974 } 1975 #endif 1976 break; 1977 } 1978 } 1979 1980 mutex_exit(&ndi_event_hdl->ndi_evthdl_cb_mutex); 1981 1982 #ifdef NDI_EVENT_DEBUG 1983 if (ndi_event_debug) { 1984 mutex_enter(&ndi_event_hdl->ndi_evthdl_mutex); 1985 ndi_event_dump_hdl(ndi_event_hdl, "ndi_event_run_callbacks"); 1986 mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex); 1987 } 1988 #endif /* NDI_EVENT_DEBUG */ 1989 1990 return (NDI_SUCCESS); 1991 } 1992 1993 1994 /* 1995 * ndi_event_tag_to_cookie: utility function to find an event cookie 1996 * given an event tag 1997 */ 1998 ddi_eventcookie_t 1999 ndi_event_tag_to_cookie(ndi_event_hdl_t handle, int event_tag) 2000 { 2001 struct ndi_event_hdl *ndi_event_hdl = (struct ndi_event_hdl *)handle; 2002 ndi_event_cookie_t *list; 2003 2004 mutex_enter(&ndi_event_hdl->ndi_evthdl_mutex); 2005 2006 list = ndi_event_hdl->ndi_evthdl_cookie_list; 2007 while (list != NULL) { 2008 if (NDI_EVENT_TAG(list) == event_tag) { 2009 mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex); 2010 return ((ddi_eventcookie_t)list); 2011 } 2012 2013 list = list->next_cookie; 2014 } 2015 2016 mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex); 2017 return (NULL); 2018 } 2019 2020 /* 2021 * ndi_event_cookie_to_tag: utility function to find a event tag 2022 * given an event_cookie 2023 */ 2024 int 2025 ndi_event_cookie_to_tag(ndi_event_hdl_t handle, ddi_eventcookie_t cookie) 2026 { 2027 struct ndi_event_hdl *ndi_event_hdl = (struct ndi_event_hdl *)handle; 2028 ndi_event_cookie_t *list; 2029 2030 mutex_enter(&ndi_event_hdl->ndi_evthdl_mutex); 2031 2032 list = ndi_event_hdl->ndi_evthdl_cookie_list; 2033 2034 while (list != NULL) { 2035 if ((ddi_eventcookie_t)list == cookie) { 2036 mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex); 2037 return (NDI_EVENT_TAG(list)); 2038 } 2039 2040 list = list->next_cookie; 2041 } 2042 2043 mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex); 2044 return (NDI_FAILURE); 2045 2046 } 2047 2048 /* 2049 * ndi_event_cookie_to_name: utility function to find an event name 2050 * given an event_cookie 2051 */ 2052 char * 2053 ndi_event_cookie_to_name(ndi_event_hdl_t handle, ddi_eventcookie_t cookie) 2054 { 2055 struct ndi_event_hdl *ndi_event_hdl = (struct ndi_event_hdl *)handle; 2056 ndi_event_cookie_t *list; 2057 2058 mutex_enter(&ndi_event_hdl->ndi_evthdl_mutex); 2059 2060 list = ndi_event_hdl->ndi_evthdl_cookie_list; 2061 2062 while (list != NULL) { 2063 if (list == NDI_EVENT(cookie)) { 2064 mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex); 2065 return (NDI_EVENT_NAME(list)); 2066 } 2067 2068 list = list->next_cookie; 2069 } 2070 2071 mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex); 2072 return (NULL); 2073 } 2074 2075 /* 2076 * ndi_event_tag_to_name: utility function to find an event name 2077 * given an event tag 2078 */ 2079 char * 2080 ndi_event_tag_to_name(ndi_event_hdl_t handle, int event_tag) 2081 { 2082 struct ndi_event_hdl *ndi_event_hdl = (struct ndi_event_hdl *)handle; 2083 ndi_event_cookie_t *list; 2084 2085 mutex_enter(&ndi_event_hdl->ndi_evthdl_mutex); 2086 2087 list = ndi_event_hdl->ndi_evthdl_cookie_list; 2088 2089 while (list) { 2090 if (NDI_EVENT_TAG(list) == event_tag) { 2091 mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex); 2092 return (NDI_EVENT_NAME(list)); 2093 } 2094 2095 list = list->next_cookie; 2096 } 2097 2098 mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex); 2099 2100 return (NULL); 2101 } 2102 2103 #ifdef NDI_EVENT_DEBUG 2104 void 2105 ndi_event_dump_hdl(struct ndi_event_hdl *hdl, char *location) 2106 { 2107 2108 2109 ndi_event_callbacks_t *next; 2110 ndi_event_cookie_t *list; 2111 2112 ASSERT(mutex_owned(&hdl->ndi_evthdl_mutex)); 2113 list = hdl->ndi_evthdl_cookie_list; 2114 2115 cmn_err(CE_CONT, "%s: event handle (%p): dip = %p (%s%d)\n", 2116 location, (void *)hdl, (void *)hdl->ndi_evthdl_dip, 2117 ddi_node_name(hdl->ndi_evthdl_dip), 2118 ddi_get_instance(hdl->ndi_evthdl_dip)); 2119 cmn_err(CE_CONT, "\thigh=%d other=%d n=%d\n", 2120 hdl->ndi_evthdl_high_plevels, hdl->ndi_evthdl_other_plevels, 2121 hdl->ndi_evthdl_n_events); 2122 2123 cmn_err(CE_CONT, "\tevent cookies:\n"); 2124 while (list) { 2125 cmn_err(CE_CONT, "\t\ttag=%d name=%s p=%d a=%x dd=%p\n", 2126 NDI_EVENT_TAG(list), NDI_EVENT_NAME(list), 2127 NDI_EVENT_PLEVEL(list), NDI_EVENT_ATTRIBUTES(list), 2128 (void *)NDI_EVENT_DDIP(list)); 2129 cmn_err(CE_CONT, "\t\tcallbacks:\n"); 2130 for (next = list->callback_list; next != NULL; 2131 next = next->ndi_evtcb_next) { 2132 cmn_err(CE_CONT, 2133 "\t\t dip=%p (%s%d) cookie=%p arg=%p\n", 2134 (void*)next->ndi_evtcb_dip, 2135 ddi_driver_name(next->ndi_evtcb_dip), 2136 ddi_get_instance(next->ndi_evtcb_dip), 2137 (void *)next->ndi_evtcb_cookie, 2138 next->ndi_evtcb_arg); 2139 } 2140 2141 list = list->next_cookie; 2142 } 2143 2144 cmn_err(CE_CONT, "\n"); 2145 } 2146 #endif 2147 2148 int 2149 ndi_dev_is_prom_node(dev_info_t *dip) 2150 { 2151 return (DEVI(dip)->devi_node_class == DDI_NC_PROM); 2152 } 2153 2154 int 2155 ndi_dev_is_pseudo_node(dev_info_t *dip) 2156 { 2157 /* 2158 * NOTE: this does NOT mean the pseudo branch of the device tree, 2159 * it means the node was created by software (DEVI_SID_NODEID || 2160 * DEVI_PSEUDO_NODEID || DEVI_SID_HIDDEN_NODEID) instead of being 2161 * generated from a PROM node. 2162 */ 2163 return (DEVI(dip)->devi_node_class == DDI_NC_PSEUDO); 2164 } 2165 2166 int 2167 ndi_dev_is_persistent_node(dev_info_t *dip) 2168 { 2169 return ((DEVI(dip)->devi_node_attributes & DDI_PERSISTENT) != 0); 2170 } 2171 2172 int 2173 ndi_dev_is_hidden_node(dev_info_t *dip) 2174 { 2175 return ((DEVI(dip)->devi_node_attributes & DDI_HIDDEN_NODE) != 0); 2176 } 2177 2178 int 2179 ndi_dev_is_hotplug_node(dev_info_t *dip) 2180 { 2181 return ((DEVI(dip)->devi_node_attributes & DDI_HOTPLUG_NODE) != 0); 2182 } 2183 2184 void 2185 ndi_devi_set_hidden(dev_info_t *dip) 2186 { 2187 DEVI(dip)->devi_node_attributes |= DDI_HIDDEN_NODE; 2188 } 2189 2190 void 2191 ndi_devi_clr_hidden(dev_info_t *dip) 2192 { 2193 DEVI(dip)->devi_node_attributes &= ~DDI_HIDDEN_NODE; 2194 } 2195 2196 int 2197 i_ndi_dev_is_auto_assigned_node(dev_info_t *dip) 2198 { 2199 return ((DEVI(dip)->devi_node_attributes & 2200 DDI_AUTO_ASSIGNED_NODEID) != 0); 2201 } 2202 2203 void 2204 i_ndi_set_node_class(dev_info_t *dip, ddi_node_class_t c) 2205 { 2206 DEVI(dip)->devi_node_class = c; 2207 } 2208 2209 ddi_node_class_t 2210 i_ndi_get_node_class(dev_info_t *dip) 2211 { 2212 return (DEVI(dip)->devi_node_class); 2213 } 2214 2215 void 2216 i_ndi_set_node_attributes(dev_info_t *dip, int p) 2217 { 2218 DEVI(dip)->devi_node_attributes = p; 2219 } 2220 2221 int 2222 i_ndi_get_node_attributes(dev_info_t *dip) 2223 { 2224 return (DEVI(dip)->devi_node_attributes); 2225 } 2226 2227 void 2228 i_ndi_set_nodeid(dev_info_t *dip, int n) 2229 { 2230 DEVI(dip)->devi_nodeid = n; 2231 } 2232 2233 void 2234 ndi_set_acc_fault(ddi_acc_handle_t ah) 2235 { 2236 i_ddi_acc_set_fault(ah); 2237 } 2238 2239 void 2240 ndi_clr_acc_fault(ddi_acc_handle_t ah) 2241 { 2242 i_ddi_acc_clr_fault(ah); 2243 } 2244 2245 void 2246 ndi_set_dma_fault(ddi_dma_handle_t dh) 2247 { 2248 i_ddi_dma_set_fault(dh); 2249 } 2250 2251 void 2252 ndi_clr_dma_fault(ddi_dma_handle_t dh) 2253 { 2254 i_ddi_dma_clr_fault(dh); 2255 } 2256 2257 /* 2258 * The default fault-handler, called when the event posted by 2259 * ddi_dev_report_fault() reaches rootnex. 2260 */ 2261 static void 2262 i_ddi_fault_handler(dev_info_t *dip, struct ddi_fault_event_data *fedp) 2263 { 2264 ASSERT(fedp); 2265 2266 mutex_enter(&(DEVI(dip)->devi_lock)); 2267 if (!DEVI_IS_DEVICE_OFFLINE(dip)) { 2268 switch (fedp->f_impact) { 2269 case DDI_SERVICE_LOST: 2270 DEVI_SET_DEVICE_DOWN(dip); 2271 break; 2272 2273 case DDI_SERVICE_DEGRADED: 2274 DEVI_SET_DEVICE_DEGRADED(dip); 2275 break; 2276 2277 case DDI_SERVICE_UNAFFECTED: 2278 default: 2279 break; 2280 2281 case DDI_SERVICE_RESTORED: 2282 DEVI_SET_DEVICE_UP(dip); 2283 break; 2284 } 2285 } 2286 mutex_exit(&(DEVI(dip)->devi_lock)); 2287 } 2288 2289 /* 2290 * The default fault-logger, called when the event posted by 2291 * ddi_dev_report_fault() reaches rootnex. 2292 */ 2293 /*ARGSUSED*/ 2294 static void 2295 i_ddi_fault_logger(dev_info_t *rdip, struct ddi_fault_event_data *fedp) 2296 { 2297 ddi_devstate_t newstate; 2298 const char *action; 2299 const char *servstate; 2300 const char *location; 2301 int bad; 2302 int changed; 2303 int level; 2304 int still; 2305 2306 ASSERT(fedp); 2307 2308 bad = 0; 2309 switch (fedp->f_location) { 2310 case DDI_DATAPATH_FAULT: 2311 location = "in datapath to"; 2312 break; 2313 case DDI_DEVICE_FAULT: 2314 location = "in"; 2315 break; 2316 case DDI_EXTERNAL_FAULT: 2317 location = "external to"; 2318 break; 2319 default: 2320 location = "somewhere near"; 2321 bad = 1; 2322 break; 2323 } 2324 2325 newstate = ddi_get_devstate(fedp->f_dip); 2326 switch (newstate) { 2327 case DDI_DEVSTATE_OFFLINE: 2328 servstate = "unavailable"; 2329 break; 2330 case DDI_DEVSTATE_DOWN: 2331 servstate = "unavailable"; 2332 break; 2333 case DDI_DEVSTATE_QUIESCED: 2334 servstate = "suspended"; 2335 break; 2336 case DDI_DEVSTATE_DEGRADED: 2337 servstate = "degraded"; 2338 break; 2339 default: 2340 servstate = "available"; 2341 break; 2342 } 2343 2344 changed = (newstate != fedp->f_oldstate); 2345 level = (newstate < fedp->f_oldstate) ? CE_WARN : CE_NOTE; 2346 switch (fedp->f_impact) { 2347 case DDI_SERVICE_LOST: 2348 case DDI_SERVICE_DEGRADED: 2349 case DDI_SERVICE_UNAFFECTED: 2350 /* fault detected; service [still] <servstate> */ 2351 action = "fault detected"; 2352 still = !changed; 2353 break; 2354 2355 case DDI_SERVICE_RESTORED: 2356 if (newstate != DDI_DEVSTATE_UP) { 2357 /* fault cleared; service still <servstate> */ 2358 action = "fault cleared"; 2359 still = 1; 2360 } else if (changed) { 2361 /* fault cleared; service <servstate> */ 2362 action = "fault cleared"; 2363 still = 0; 2364 } else { 2365 /* no fault; service <servstate> */ 2366 action = "no fault"; 2367 still = 0; 2368 } 2369 break; 2370 2371 default: 2372 bad = 1; 2373 still = 0; 2374 break; 2375 } 2376 2377 cmn_err(level, "!%s%d: %s %s device; service %s%s"+(bad|changed), 2378 ddi_driver_name(fedp->f_dip), ddi_get_instance(fedp->f_dip), 2379 bad ? "invalid report of fault" : action, 2380 location, still ? "still " : "", servstate); 2381 2382 cmn_err(level, "!%s%d: %s"+(bad|changed), 2383 ddi_driver_name(fedp->f_dip), ddi_get_instance(fedp->f_dip), 2384 fedp->f_message); 2385 } 2386 2387 /* 2388 * Platform-settable pointers to fault handler and logger functions. 2389 * These are called by the default rootnex event-posting code when 2390 * a fault event reaches rootnex. 2391 */ 2392 void (*plat_fault_handler)(dev_info_t *, struct ddi_fault_event_data *) = 2393 i_ddi_fault_handler; 2394 void (*plat_fault_logger)(dev_info_t *, struct ddi_fault_event_data *) = 2395 i_ddi_fault_logger; 2396 2397 /* 2398 * Rootnex event definitions ... 2399 */ 2400 enum rootnex_event_tags { 2401 ROOTNEX_FAULT_EVENT 2402 }; 2403 static ndi_event_hdl_t rootnex_event_hdl; 2404 static ndi_event_definition_t rootnex_event_set[] = { 2405 { 2406 ROOTNEX_FAULT_EVENT, 2407 DDI_DEVI_FAULT_EVENT, 2408 EPL_INTERRUPT, 2409 NDI_EVENT_POST_TO_ALL 2410 } 2411 }; 2412 static ndi_event_set_t rootnex_events = { 2413 NDI_EVENTS_REV1, 2414 sizeof (rootnex_event_set) / sizeof (rootnex_event_set[0]), 2415 rootnex_event_set 2416 }; 2417 2418 /* 2419 * Initialize rootnex event handle 2420 */ 2421 void 2422 i_ddi_rootnex_init_events(dev_info_t *dip) 2423 { 2424 if (ndi_event_alloc_hdl(dip, (ddi_iblock_cookie_t)(LOCK_LEVEL-1), 2425 &rootnex_event_hdl, NDI_SLEEP) == NDI_SUCCESS) { 2426 if (ndi_event_bind_set(rootnex_event_hdl, 2427 &rootnex_events, NDI_SLEEP) != NDI_SUCCESS) { 2428 (void) ndi_event_free_hdl(rootnex_event_hdl); 2429 rootnex_event_hdl = NULL; 2430 } 2431 } 2432 } 2433 2434 /* 2435 * Event-handling functions for rootnex 2436 * These provide the standard implementation of fault handling 2437 */ 2438 /*ARGSUSED*/ 2439 int 2440 i_ddi_rootnex_get_eventcookie(dev_info_t *dip, dev_info_t *rdip, 2441 char *eventname, ddi_eventcookie_t *cookiep) 2442 { 2443 if (rootnex_event_hdl == NULL) 2444 return (NDI_FAILURE); 2445 return (ndi_event_retrieve_cookie(rootnex_event_hdl, rdip, eventname, 2446 cookiep, NDI_EVENT_NOPASS)); 2447 } 2448 2449 /*ARGSUSED*/ 2450 int 2451 i_ddi_rootnex_add_eventcall(dev_info_t *dip, dev_info_t *rdip, 2452 ddi_eventcookie_t eventid, void (*handler)(dev_info_t *dip, 2453 ddi_eventcookie_t event, void *arg, void *impl_data), void *arg, 2454 ddi_callback_id_t *cb_id) 2455 { 2456 if (rootnex_event_hdl == NULL) 2457 return (NDI_FAILURE); 2458 return (ndi_event_add_callback(rootnex_event_hdl, rdip, 2459 eventid, handler, arg, NDI_SLEEP, cb_id)); 2460 } 2461 2462 /*ARGSUSED*/ 2463 int 2464 i_ddi_rootnex_remove_eventcall(dev_info_t *dip, ddi_callback_id_t cb_id) 2465 { 2466 if (rootnex_event_hdl == NULL) 2467 return (NDI_FAILURE); 2468 2469 return (ndi_event_remove_callback(rootnex_event_hdl, cb_id)); 2470 } 2471 2472 /*ARGSUSED*/ 2473 int 2474 i_ddi_rootnex_post_event(dev_info_t *dip, dev_info_t *rdip, 2475 ddi_eventcookie_t eventid, void *impl_data) 2476 { 2477 int tag; 2478 2479 if (rootnex_event_hdl == NULL) 2480 return (NDI_FAILURE); 2481 2482 tag = ndi_event_cookie_to_tag(rootnex_event_hdl, eventid); 2483 if (tag == ROOTNEX_FAULT_EVENT) { 2484 (*plat_fault_handler)(rdip, impl_data); 2485 (*plat_fault_logger)(rdip, impl_data); 2486 } 2487 return (ndi_event_run_callbacks(rootnex_event_hdl, rdip, 2488 eventid, impl_data)); 2489 } 2490 2491 /* 2492 * ndi_set_bus_private/ndi_get_bus_private: 2493 * Get/set device bus private data in devinfo. 2494 */ 2495 void 2496 ndi_set_bus_private(dev_info_t *dip, boolean_t up, uint32_t port_type, 2497 void *data) 2498 { 2499 if (up) { 2500 DEVI(dip)->devi_bus.port_up.info.port.type = port_type; 2501 DEVI(dip)->devi_bus.port_up.priv_p = data; 2502 } else { 2503 DEVI(dip)->devi_bus.port_down.info.port.type = port_type; 2504 DEVI(dip)->devi_bus.port_down.priv_p = data; 2505 } 2506 } 2507 2508 void * 2509 ndi_get_bus_private(dev_info_t *dip, boolean_t up) 2510 { 2511 if (up) 2512 return (DEVI(dip)->devi_bus.port_up.priv_p); 2513 else 2514 return (DEVI(dip)->devi_bus.port_down.priv_p); 2515 } 2516 2517 boolean_t 2518 ndi_port_type(dev_info_t *dip, boolean_t up, uint32_t port_type) 2519 { 2520 if (up) { 2521 return ((DEVI(dip)->devi_bus.port_up.info.port.type) == 2522 port_type); 2523 } else { 2524 return ((DEVI(dip)->devi_bus.port_down.info.port.type) == 2525 port_type); 2526 } 2527 } 2528 2529 /* Interfaces for 'self' to set/get a child's flavor */ 2530 void 2531 ndi_flavor_set(dev_info_t *child, ndi_flavor_t child_flavor) 2532 { 2533 DEVI(child)->devi_flavor = child_flavor; 2534 } 2535 2536 ndi_flavor_t 2537 ndi_flavor_get(dev_info_t *child) 2538 { 2539 return (DEVI(child)->devi_flavor); 2540 } 2541 2542 /* 2543 * Interfaces to maintain flavor-specific private data of flavored 2544 * children of self. 2545 * 2546 * The flavor count always includes the default (0) vanilla flavor, 2547 * but storage for the vanilla flavor data pointer is in the same 2548 * place that ddi_[sg]et_driver_private uses, so the flavorv 2549 * storage is just for flavors 1..{nflavors-1}. 2550 */ 2551 void 2552 ndi_flavorv_alloc(dev_info_t *self, int nflavors) 2553 { 2554 ASSERT(nflavors > 0 && (DEVI(self)->devi_flavorv == NULL || 2555 nflavors == DEVI(self)->devi_flavorv_n)); 2556 if (nflavors <= 1 || (DEVI(self)->devi_flavorv)) { 2557 return; 2558 } 2559 DEVI(self)->devi_flavorv = 2560 kmem_zalloc((nflavors - 1) * sizeof (void *), KM_SLEEP); 2561 DEVI(self)->devi_flavorv_n = nflavors; 2562 } 2563 2564 void 2565 ndi_flavorv_set(dev_info_t *self, ndi_flavor_t child_flavor, void *v) 2566 { 2567 if (child_flavor == NDI_FLAVOR_VANILLA) { 2568 ddi_set_driver_private(self, v); 2569 } else { 2570 ASSERT(child_flavor < DEVI(self)->devi_flavorv_n && 2571 DEVI(self)->devi_flavorv != NULL); 2572 if (child_flavor > DEVI(self)->devi_flavorv_n || 2573 DEVI(self)->devi_flavorv == NULL) { 2574 return; 2575 } 2576 DEVI(self)->devi_flavorv[child_flavor - 1] = v; 2577 } 2578 } 2579 2580 void * 2581 ndi_flavorv_get(dev_info_t *self, ndi_flavor_t child_flavor) 2582 { 2583 if (child_flavor == NDI_FLAVOR_VANILLA) { 2584 return (ddi_get_driver_private(self)); 2585 } else { 2586 ASSERT(child_flavor < DEVI(self)->devi_flavorv_n && 2587 DEVI(self)->devi_flavorv != NULL); 2588 if (child_flavor > DEVI(self)->devi_flavorv_n || 2589 DEVI(self)->devi_flavorv == NULL) { 2590 return (NULL); 2591 } 2592 return (DEVI(self)->devi_flavorv[child_flavor - 1]); 2593 } 2594 } 2595