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