include: gcc 7's cpp has problems with the line continuations in .x files
[unleashed.git] / kernel / os / sunndi.c
blob9a1b8a121d0fe1ffd8f3d2c52593a4156d21faba
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #include <sys/types.h>
27 #include <sys/param.h>
28 #include <sys/systm.h>
29 #include <sys/buf.h>
30 #include <sys/uio.h>
31 #include <sys/cred.h>
32 #include <sys/poll.h>
33 #include <sys/mman.h>
34 #include <sys/kmem.h>
35 #include <sys/model.h>
36 #include <sys/file.h>
37 #include <sys/proc.h>
38 #include <sys/open.h>
39 #include <sys/user.h>
40 #include <sys/t_lock.h>
41 #include <sys/vm.h>
42 #include <sys/stat.h>
43 #include <vm/hat.h>
44 #include <vm/seg.h>
45 #include <vm/as.h>
46 #include <sys/cmn_err.h>
47 #include <sys/debug.h>
48 #include <sys/avintr.h>
49 #include <sys/autoconf.h>
50 #include <sys/sunddi.h>
51 #include <sys/esunddi.h>
52 #include <sys/sunndi.h>
53 #include <sys/ddi.h>
54 #include <sys/kstat.h>
55 #include <sys/conf.h>
56 #include <sys/ddi_impldefs.h> /* include implementation structure defs */
57 #include <sys/ndi_impldefs.h>
58 #include <sys/hwconf.h>
59 #include <sys/pathname.h>
60 #include <sys/modctl.h>
61 #include <sys/epm.h>
62 #include <sys/devctl.h>
63 #include <sys/callb.h>
64 #include <sys/bootconf.h>
65 #include <sys/dacf_impl.h>
66 #include <sys/nvpair.h>
67 #include <sys/sunmdi.h>
68 #include <sys/fs/dv_node.h>
69 #include <sys/sunldi_impl.h>
73 * ndi property handling
75 int
76 ndi_prop_update_int(dev_t match_dev, dev_info_t *dip,
77 char *name, int data)
79 return (ddi_prop_update_common(match_dev, dip,
80 DDI_PROP_HW_DEF | DDI_PROP_TYPE_INT | DDI_PROP_DONTSLEEP,
81 name, &data, 1, ddi_prop_fm_encode_ints));
84 int
85 ndi_prop_update_int64(dev_t match_dev, dev_info_t *dip,
86 char *name, int64_t data)
88 return (ddi_prop_update_common(match_dev, dip,
89 DDI_PROP_HW_DEF | DDI_PROP_TYPE_INT64 | DDI_PROP_DONTSLEEP,
90 name, &data, 1, ddi_prop_fm_encode_int64));
93 int
94 ndi_prop_create_boolean(dev_t match_dev, dev_info_t *dip,
95 char *name)
97 return (ddi_prop_update_common(match_dev, dip,
98 DDI_PROP_HW_DEF | DDI_PROP_TYPE_ANY | DDI_PROP_DONTSLEEP,
99 name, NULL, 0, ddi_prop_fm_encode_bytes));
103 ndi_prop_update_int_array(dev_t match_dev, dev_info_t *dip,
104 char *name, int *data, uint_t nelements)
106 return (ddi_prop_update_common(match_dev, dip,
107 DDI_PROP_HW_DEF | DDI_PROP_TYPE_INT | DDI_PROP_DONTSLEEP,
108 name, data, nelements, ddi_prop_fm_encode_ints));
112 ndi_prop_update_int64_array(dev_t match_dev, dev_info_t *dip,
113 char *name, int64_t *data, uint_t nelements)
115 return (ddi_prop_update_common(match_dev, dip,
116 DDI_PROP_HW_DEF | DDI_PROP_TYPE_INT64 | DDI_PROP_DONTSLEEP,
117 name, data, nelements, ddi_prop_fm_encode_int64));
121 ndi_prop_update_string(dev_t match_dev, dev_info_t *dip,
122 char *name, char *data)
124 return (ddi_prop_update_common(match_dev, dip,
125 DDI_PROP_HW_DEF | DDI_PROP_TYPE_STRING | DDI_PROP_DONTSLEEP,
126 name, &data, 1, ddi_prop_fm_encode_string));
130 ndi_prop_update_string_array(dev_t match_dev, dev_info_t *dip,
131 char *name, char **data, uint_t nelements)
133 return (ddi_prop_update_common(match_dev, dip,
134 DDI_PROP_HW_DEF | DDI_PROP_TYPE_STRING | DDI_PROP_DONTSLEEP,
135 name, data, nelements,
136 ddi_prop_fm_encode_strings));
140 ndi_prop_update_byte_array(dev_t match_dev, dev_info_t *dip,
141 char *name, uchar_t *data, uint_t nelements)
143 if (nelements == 0)
144 return (DDI_PROP_INVAL_ARG);
146 return (ddi_prop_update_common(match_dev, dip,
147 DDI_PROP_HW_DEF | DDI_PROP_TYPE_BYTE | DDI_PROP_DONTSLEEP,
148 name, data, nelements, ddi_prop_fm_encode_bytes));
152 ndi_prop_remove(dev_t dev, dev_info_t *dip, char *name)
154 return (ddi_prop_remove_common(dev, dip, name, DDI_PROP_HW_DEF));
157 void
158 ndi_prop_remove_all(dev_info_t *dip)
160 i_ddi_prop_dyn_parent_set(dip, NULL);
161 ddi_prop_remove_all_common(dip, (int)DDI_PROP_HW_DEF);
165 * Post an event notification to nexus driver responsible for handling
166 * the event. The responsible nexus is defined in the cookie passed in as
167 * the third parameter.
168 * The dip parameter is an artifact of an older implementation in which all
169 * requests to remove an eventcall would bubble up the tree. Today, this
170 * parameter is ignored.
171 * Input Parameters:
172 * dip - Ignored.
173 * rdip - device driver posting the event
174 * cookie - valid ddi_eventcookie_t, obtained by caller prior to
175 * invocation of this routine
176 * impl_data - used by framework
178 /*ARGSUSED*/
180 ndi_post_event(dev_info_t *dip, dev_info_t *rdip,
181 ddi_eventcookie_t cookie, void *impl_data)
183 dev_info_t *ddip;
185 ASSERT(cookie);
186 ddip = NDI_EVENT_DDIP(cookie);
189 * perform sanity checks. These conditions should never be true.
192 ASSERT(DEVI(ddip)->devi_ops->devo_bus_ops != NULL);
193 ASSERT(DEVI(ddip)->devi_ops->devo_bus_ops->busops_rev >= BUSO_REV_6);
194 ASSERT(DEVI(ddip)->devi_ops->devo_bus_ops->bus_post_event != NULL);
197 * post the event to the responsible ancestor
199 return ((*(DEVI(ddip)->devi_ops->devo_bus_ops->bus_post_event))
200 (ddip, rdip, cookie, impl_data));
204 * Calls the bus nexus driver's implementation of the
205 * (*bus_remove_eventcall)() interface.
208 ndi_busop_remove_eventcall(dev_info_t *ddip, ddi_callback_id_t id)
211 ASSERT(id);
212 /* check for a correct revno before calling up the device tree. */
213 ASSERT(DEVI(ddip)->devi_ops->devo_bus_ops != NULL);
214 ASSERT(DEVI(ddip)->devi_ops->devo_bus_ops->busops_rev >= BUSO_REV_6);
216 if (DEVI(ddip)->devi_ops->devo_bus_ops->bus_remove_eventcall == NULL)
217 return (DDI_FAILURE);
220 * request responsible nexus to remove the eventcall
222 return ((*(DEVI(ddip)->devi_ops->devo_bus_ops->bus_remove_eventcall))
223 (ddip, id));
227 * Calls the bus nexus driver's implementation of the
228 * (*bus_add_eventcall)() interface. The dip parameter is an
229 * artifact of an older implementation in which all requests to
230 * add an eventcall would bubble up the tree. Today, this parameter is
231 * ignored.
233 /*ARGSUSED*/
235 ndi_busop_add_eventcall(dev_info_t *dip, dev_info_t *rdip,
236 ddi_eventcookie_t cookie, void (*callback)(), void *arg,
237 ddi_callback_id_t *cb_id)
239 dev_info_t *ddip = (dev_info_t *)NDI_EVENT_DDIP(cookie);
242 * check for a correct revno before calling up the device tree.
244 ASSERT(DEVI(ddip)->devi_ops->devo_bus_ops != NULL);
245 ASSERT(DEVI(ddip)->devi_ops->devo_bus_ops->busops_rev >= BUSO_REV_6);
247 if (DEVI(ddip)->devi_ops->devo_bus_ops->bus_add_eventcall == NULL)
248 return (DDI_FAILURE);
251 * request responsible ancestor to add the eventcall
253 return ((*(DEVI(ddip)->devi_ops->devo_bus_ops->bus_add_eventcall))
254 (ddip, rdip, cookie, callback, arg, cb_id));
258 * Calls the bus nexus driver's implementation of the
259 * (*bus_get_eventcookie)() interface up the device tree hierarchy.
262 ndi_busop_get_eventcookie(dev_info_t *dip, dev_info_t *rdip, char *name,
263 ddi_eventcookie_t *event_cookiep)
265 dev_info_t *pdip = (dev_info_t *)DEVI(dip)->devi_parent;
267 /* Can not be called from rootnex. */
268 ASSERT(pdip);
271 * check for a correct revno before calling up the device tree.
273 ASSERT(DEVI(pdip)->devi_ops->devo_bus_ops != NULL);
275 if ((DEVI(pdip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_6) ||
276 (DEVI(pdip)->devi_ops->devo_bus_ops->bus_get_eventcookie == NULL)) {
277 #ifdef DEBUG
278 if ((DEVI(pdip)->devi_ops->devo_bus_ops->busops_rev >=
279 BUSO_REV_3) &&
280 (DEVI(pdip)->devi_ops->devo_bus_ops->bus_get_eventcookie)) {
281 cmn_err(CE_WARN,
282 "Warning: %s%d busops_rev=%d no longer supported"
283 " by the NDI event framework.\nBUSO_REV_6 or "
284 "greater must be used.",
285 DEVI(pdip)->devi_binding_name,
286 DEVI(pdip)->devi_instance,
287 DEVI(pdip)->devi_ops->devo_bus_ops->busops_rev);
289 #endif /* DEBUG */
291 return (ndi_busop_get_eventcookie(pdip, rdip, name,
292 event_cookiep));
295 return ((*(DEVI(pdip)->devi_ops->devo_bus_ops->bus_get_eventcookie))
296 (pdip, rdip, name, event_cookiep));
300 * Copy in the devctl IOCTL data and return a handle to
301 * the data.
304 ndi_dc_allochdl(void *iocarg, struct devctl_iocdata **rdcp)
306 struct devctl_iocdata *dcp;
307 char *cpybuf;
309 ASSERT(rdcp != NULL);
311 dcp = kmem_zalloc(sizeof (*dcp), KM_SLEEP);
313 if (get_udatamodel() == DATAMODEL_NATIVE) {
314 if (copyin(iocarg, dcp, sizeof (*dcp)) != 0) {
315 kmem_free(dcp, sizeof (*dcp));
316 return (NDI_FAULT);
319 #ifdef _SYSCALL32_IMPL
320 else {
321 struct devctl_iocdata32 dcp32;
323 if (copyin(iocarg, &dcp32, sizeof (dcp32)) != 0) {
324 kmem_free(dcp, sizeof (*dcp));
325 return (NDI_FAULT);
327 dcp->cmd = (uint_t)dcp32.cmd;
328 dcp->flags = (uint_t)dcp32.flags;
329 dcp->cpyout_buf = (uint_t *)(uintptr_t)dcp32.cpyout_buf;
330 dcp->nvl_user = (nvlist_t *)(uintptr_t)dcp32.nvl_user;
331 dcp->nvl_usersz = (size_t)dcp32.nvl_usersz;
332 dcp->c_nodename = (char *)(uintptr_t)dcp32.c_nodename;
333 dcp->c_unitaddr = (char *)(uintptr_t)dcp32.c_unitaddr;
335 #endif
336 if (dcp->c_nodename != NULL) {
337 cpybuf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
338 if (copyinstr(dcp->c_nodename, cpybuf, MAXNAMELEN, 0) != 0) {
339 kmem_free(cpybuf, MAXNAMELEN);
340 kmem_free(dcp, sizeof (*dcp));
341 return (NDI_FAULT);
343 cpybuf[MAXNAMELEN - 1] = '\0';
344 dcp->c_nodename = cpybuf;
347 if (dcp->c_unitaddr != NULL) {
348 cpybuf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
349 if (copyinstr(dcp->c_unitaddr, cpybuf, MAXNAMELEN, 0) != 0) {
350 kmem_free(cpybuf, MAXNAMELEN);
351 if (dcp->c_nodename != NULL)
352 kmem_free(dcp->c_nodename, MAXNAMELEN);
353 kmem_free(dcp, sizeof (*dcp));
354 return (NDI_FAULT);
356 cpybuf[MAXNAMELEN - 1] = '\0';
357 dcp->c_unitaddr = cpybuf;
361 * copyin and unpack a user defined nvlist if one was passed
363 if (dcp->nvl_user != NULL) {
364 if ((dcp->nvl_usersz == 0) ||
365 (dcp->nvl_usersz > DEVCTL_MAX_NVL_USERSZ)) {
366 if (dcp->c_nodename != NULL)
367 kmem_free(dcp->c_nodename, MAXNAMELEN);
368 if (dcp->c_unitaddr != NULL)
369 kmem_free(dcp->c_unitaddr, MAXNAMELEN);
370 kmem_free(dcp, sizeof (*dcp));
371 return (NDI_FAILURE);
373 cpybuf = kmem_alloc(dcp->nvl_usersz, KM_SLEEP);
374 if (copyin(dcp->nvl_user, cpybuf, dcp->nvl_usersz) != 0) {
375 kmem_free(cpybuf, dcp->nvl_usersz);
376 if (dcp->c_nodename != NULL)
377 kmem_free(dcp->c_nodename, MAXNAMELEN);
378 if (dcp->c_unitaddr != NULL)
379 kmem_free(dcp->c_unitaddr, MAXNAMELEN);
380 kmem_free(dcp, sizeof (*dcp));
381 return (NDI_FAULT);
384 if (nvlist_unpack(cpybuf, dcp->nvl_usersz, &dcp->nvl_user,
385 KM_SLEEP)) {
386 kmem_free(cpybuf, dcp->nvl_usersz);
387 if (dcp->c_nodename != NULL)
388 kmem_free(dcp->c_nodename, MAXNAMELEN);
389 if (dcp->c_unitaddr != NULL)
390 kmem_free(dcp->c_unitaddr, MAXNAMELEN);
391 kmem_free(dcp, sizeof (*dcp));
392 return (NDI_FAULT);
395 * free the buffer containing the packed nvlist
397 kmem_free(cpybuf, dcp->nvl_usersz);
401 *rdcp = dcp;
402 return (NDI_SUCCESS);
406 * free all space allocated to a handle.
408 void
409 ndi_dc_freehdl(struct devctl_iocdata *dcp)
411 ASSERT(dcp != NULL);
413 if (dcp->c_nodename != NULL)
414 kmem_free(dcp->c_nodename, MAXNAMELEN);
416 if (dcp->c_unitaddr != NULL)
417 kmem_free(dcp->c_unitaddr, MAXNAMELEN);
419 nvlist_free(dcp->nvl_user);
421 kmem_free(dcp, sizeof (*dcp));
424 char *
425 ndi_dc_getname(struct devctl_iocdata *dcp)
427 ASSERT(dcp != NULL);
428 return (dcp->c_nodename);
432 char *
433 ndi_dc_getaddr(struct devctl_iocdata *dcp)
435 ASSERT(dcp != NULL);
436 return (dcp->c_unitaddr);
439 nvlist_t *
440 ndi_dc_get_ap_data(struct devctl_iocdata *dcp)
442 ASSERT(dcp != NULL);
444 return (dcp->nvl_user);
448 * Transition the child named by "devname@devaddr" to the online state.
449 * For use by a driver's DEVCTL_DEVICE_ONLINE handler.
452 ndi_devctl_device_online(dev_info_t *dip, struct devctl_iocdata *dcp,
453 uint_t flags)
455 int rval;
456 char *name;
457 dev_info_t *rdip;
459 if (ndi_dc_getname(dcp) == NULL || ndi_dc_getaddr(dcp) == NULL)
460 return (EINVAL);
462 name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
463 (void) snprintf(name, MAXNAMELEN, "%s@%s",
464 ndi_dc_getname(dcp), ndi_dc_getaddr(dcp));
466 if ((rval = ndi_devi_config_one(dip, name, &rdip,
467 flags | NDI_DEVI_ONLINE | NDI_CONFIG)) == NDI_SUCCESS) {
468 ndi_rele_devi(rdip);
471 * Invalidate devfs cached directory contents. For the checks
472 * in the "if" condition see the comment in ndi_devi_online().
474 if (i_ddi_devi_attached(dip) && !DEVI_BUSY_OWNED(dip))
475 (void) devfs_clean(dip, NULL, 0);
477 } else if (rval == NDI_BUSY) {
478 rval = EBUSY;
479 } else if (rval == NDI_FAILURE) {
480 rval = EIO;
483 NDI_DEBUG(flags, (CE_CONT, "%s%d: online: %s: %s\n",
484 ddi_driver_name(dip), ddi_get_instance(dip), name,
485 ((rval == NDI_SUCCESS) ? "ok" : "failed")));
487 kmem_free(name, MAXNAMELEN);
489 return (rval);
493 * Transition the child named by "devname@devaddr" to the offline state.
494 * For use by a driver's DEVCTL_DEVICE_OFFLINE handler.
497 ndi_devctl_device_offline(dev_info_t *dip, struct devctl_iocdata *dcp,
498 uint_t flags)
500 int rval;
501 char *name;
503 if (ndi_dc_getname(dcp) == NULL || ndi_dc_getaddr(dcp) == NULL)
504 return (EINVAL);
506 name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
507 (void) snprintf(name, MAXNAMELEN, "%s@%s",
508 ndi_dc_getname(dcp), ndi_dc_getaddr(dcp));
510 (void) devfs_clean(dip, name, DV_CLEAN_FORCE);
511 rval = ndi_devi_unconfig_one(dip, name, NULL,
512 flags | NDI_DEVI_OFFLINE);
514 if (rval == NDI_BUSY) {
515 rval = EBUSY;
516 } else if (rval == NDI_FAILURE) {
517 rval = EIO;
520 NDI_DEBUG(flags, (CE_CONT, "%s%d: offline: %s: %s\n",
521 ddi_driver_name(dip), ddi_get_instance(dip), name,
522 (rval == NDI_SUCCESS) ? "ok" : "failed"));
524 kmem_free(name, MAXNAMELEN);
526 return (rval);
530 * Remove the child named by "devname@devaddr".
531 * For use by a driver's DEVCTL_DEVICE_REMOVE handler.
534 ndi_devctl_device_remove(dev_info_t *dip, struct devctl_iocdata *dcp,
535 uint_t flags)
537 int rval;
538 char *name;
540 if (ndi_dc_getname(dcp) == NULL || ndi_dc_getaddr(dcp) == NULL)
541 return (EINVAL);
543 name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
544 (void) snprintf(name, MAXNAMELEN, "%s@%s",
545 ndi_dc_getname(dcp), ndi_dc_getaddr(dcp));
547 (void) devfs_clean(dip, name, DV_CLEAN_FORCE);
549 rval = ndi_devi_unconfig_one(dip, name, NULL, flags | NDI_DEVI_REMOVE);
551 if (rval == NDI_BUSY) {
552 rval = EBUSY;
553 } else if (rval == NDI_FAILURE) {
554 rval = EIO;
557 NDI_DEBUG(flags, (CE_CONT, "%s%d: remove: %s: %s\n",
558 ddi_driver_name(dip), ddi_get_instance(dip), name,
559 (rval == NDI_SUCCESS) ? "ok" : "failed"));
561 kmem_free(name, MAXNAMELEN);
563 return (rval);
567 * Return devctl state of the child named by "name@addr".
568 * For use by a driver's DEVCTL_DEVICE_GETSTATE handler.
571 ndi_devctl_device_getstate(dev_info_t *parent, struct devctl_iocdata *dcp,
572 uint_t *state)
574 dev_info_t *dip;
575 char *name, *addr;
576 char *devname;
577 int devnamelen;
578 int circ;
580 if (parent == NULL ||
581 ((name = ndi_dc_getname(dcp)) == NULL) ||
582 ((addr = ndi_dc_getaddr(dcp)) == NULL))
583 return (NDI_FAILURE);
585 devnamelen = strlen(name) + strlen(addr) + 2;
586 devname = kmem_alloc(devnamelen, KM_SLEEP);
587 if (strlen(addr) > 0) {
588 (void) snprintf(devname, devnamelen, "%s@%s", name, addr);
589 } else {
590 (void) snprintf(devname, devnamelen, "%s", name);
593 ndi_devi_enter(parent, &circ);
595 dip = ndi_devi_findchild(parent, devname);
596 kmem_free(devname, devnamelen);
598 if (dip == NULL) {
599 ndi_devi_exit(parent, circ);
600 return (NDI_FAILURE);
603 mutex_enter(&(DEVI(dip)->devi_lock));
604 if (DEVI_IS_DEVICE_OFFLINE(dip)) {
605 *state = DEVICE_OFFLINE;
606 } else if (DEVI_IS_DEVICE_DOWN(dip)) {
607 *state = DEVICE_DOWN;
608 } else {
609 *state = DEVICE_ONLINE;
610 if (devi_stillreferenced(dip) == DEVI_REFERENCED)
611 *state |= DEVICE_BUSY;
614 mutex_exit(&(DEVI(dip)->devi_lock));
615 ndi_devi_exit(parent, circ);
617 return (NDI_SUCCESS);
621 * return the current state of the device "dip"
623 * recommend using ndi_devctl_ioctl() or
624 * ndi_devctl_device_getstate() instead
627 ndi_dc_return_dev_state(dev_info_t *dip, struct devctl_iocdata *dcp)
629 dev_info_t *pdip;
630 uint_t devstate = 0;
631 int circ;
633 if ((dip == NULL) || (dcp == NULL))
634 return (NDI_FAILURE);
636 pdip = ddi_get_parent(dip);
638 ndi_devi_enter(pdip, &circ);
639 mutex_enter(&(DEVI(dip)->devi_lock));
640 if (DEVI_IS_DEVICE_OFFLINE(dip)) {
641 devstate = DEVICE_OFFLINE;
642 } else if (DEVI_IS_DEVICE_DOWN(dip)) {
643 devstate = DEVICE_DOWN;
644 } else {
645 devstate = DEVICE_ONLINE;
646 if (devi_stillreferenced(dip) == DEVI_REFERENCED)
647 devstate |= DEVICE_BUSY;
650 mutex_exit(&(DEVI(dip)->devi_lock));
651 ndi_devi_exit(pdip, circ);
653 if (copyout(&devstate, dcp->cpyout_buf, sizeof (uint_t)) != 0)
654 return (NDI_FAULT);
656 return (NDI_SUCCESS);
660 * Return device's bus state
661 * For use by a driver's DEVCTL_BUS_GETSTATE handler.
664 ndi_devctl_bus_getstate(dev_info_t *dip, struct devctl_iocdata *dcp,
665 uint_t *state)
667 if ((dip == NULL) || (dcp == NULL))
668 return (NDI_FAILURE);
670 return (ndi_get_bus_state(dip, state));
674 * Generic devctl ioctl handler
677 ndi_devctl_ioctl(dev_info_t *dip, int cmd, intptr_t arg, int mode, uint_t flags)
679 _NOTE(ARGUNUSED(mode))
680 struct devctl_iocdata *dcp;
681 uint_t state;
682 int rval = ENOTTY;
685 * read devctl ioctl data
687 if (ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS)
688 return (EFAULT);
690 switch (cmd) {
692 case DEVCTL_BUS_GETSTATE:
693 rval = ndi_devctl_bus_getstate(dip, dcp, &state);
694 if (rval == NDI_SUCCESS) {
695 if (copyout(&state, dcp->cpyout_buf,
696 sizeof (uint_t)) != 0)
697 rval = NDI_FAULT;
699 break;
701 case DEVCTL_DEVICE_ONLINE:
702 rval = ndi_devctl_device_online(dip, dcp, flags);
703 break;
705 case DEVCTL_DEVICE_OFFLINE:
706 rval = ndi_devctl_device_offline(dip, dcp, flags);
707 break;
709 case DEVCTL_DEVICE_GETSTATE:
710 rval = ndi_devctl_device_getstate(dip, dcp, &state);
711 if (rval == NDI_SUCCESS) {
712 if (copyout(&state, dcp->cpyout_buf,
713 sizeof (uint_t)) != 0)
714 rval = NDI_FAULT;
716 break;
718 case DEVCTL_DEVICE_REMOVE:
719 rval = ndi_devctl_device_remove(dip, dcp, flags);
720 break;
722 case DEVCTL_BUS_DEV_CREATE:
723 rval = ndi_dc_devi_create(dcp, dip, 0, NULL);
724 break;
727 * ioctls for which a generic implementation makes no sense
729 case DEVCTL_BUS_RESET:
730 case DEVCTL_BUS_RESETALL:
731 case DEVCTL_DEVICE_RESET:
732 case DEVCTL_AP_CONNECT:
733 case DEVCTL_AP_DISCONNECT:
734 case DEVCTL_AP_INSERT:
735 case DEVCTL_AP_REMOVE:
736 case DEVCTL_AP_CONFIGURE:
737 case DEVCTL_AP_UNCONFIGURE:
738 case DEVCTL_AP_GETSTATE:
739 case DEVCTL_AP_CONTROL:
740 case DEVCTL_BUS_QUIESCE:
741 case DEVCTL_BUS_UNQUIESCE:
742 rval = ENOTSUP;
743 break;
746 ndi_dc_freehdl(dcp);
747 return (rval);
751 * Copyout the state of the Attachment Point "ap" to the requesting
752 * user process.
755 ndi_dc_return_ap_state(devctl_ap_state_t *ap, struct devctl_iocdata *dcp)
757 if ((ap == NULL) || (dcp == NULL))
758 return (NDI_FAILURE);
761 if (get_udatamodel() == DATAMODEL_NATIVE) {
762 if (copyout(ap, dcp->cpyout_buf,
763 sizeof (devctl_ap_state_t)) != 0)
764 return (NDI_FAULT);
766 #ifdef _SYSCALL32_IMPL
767 else {
768 struct devctl_ap_state32 ap_state32;
770 ap_state32.ap_rstate = ap->ap_rstate;
771 ap_state32.ap_ostate = ap->ap_ostate;
772 ap_state32.ap_condition = ap->ap_condition;
773 ap_state32.ap_error_code = ap->ap_error_code;
774 ap_state32.ap_in_transition = ap->ap_in_transition;
775 ap_state32.ap_last_change = (time32_t)ap->ap_last_change;
776 if (copyout(&ap_state32, dcp->cpyout_buf,
777 sizeof (devctl_ap_state32_t)) != 0)
778 return (NDI_FAULT);
780 #endif
782 return (NDI_SUCCESS);
786 * Copyout the bus state of the bus nexus device "dip" to the requesting
787 * user process.
790 ndi_dc_return_bus_state(dev_info_t *dip, struct devctl_iocdata *dcp)
792 uint_t devstate = 0;
794 if ((dip == NULL) || (dcp == NULL))
795 return (NDI_FAILURE);
797 if (ndi_get_bus_state(dip, &devstate) != NDI_SUCCESS)
798 return (NDI_FAILURE);
800 if (copyout(&devstate, dcp->cpyout_buf, sizeof (uint_t)) != 0)
801 return (NDI_FAULT);
803 return (NDI_SUCCESS);
806 static int
807 i_dc_devi_create(struct devctl_iocdata *, dev_info_t *, dev_info_t **);
810 * create a child device node given the property definitions
811 * supplied by the userland process
814 ndi_dc_devi_create(struct devctl_iocdata *dcp, dev_info_t *pdip, int flags,
815 dev_info_t **rdip)
817 dev_info_t *cdip;
818 int rv, circular = 0;
819 char devnm[MAXNAMELEN];
820 int nmlen;
823 * The child device may have been pre-constructed by an earlier
824 * call to this function with the flag DEVCTL_CONSTRUCT set.
827 if ((cdip = (rdip != NULL) ? *rdip : NULL) == NULL)
828 if ((rv = i_dc_devi_create(dcp, pdip, &cdip)) != 0)
829 return (rv);
831 ASSERT(cdip != NULL);
834 * Return the device node partially constructed if the
835 * DEVCTL_CONSTRUCT flag is set.
837 if (flags & DEVCTL_CONSTRUCT) {
838 if (rdip == NULL) {
839 (void) ndi_devi_free(cdip);
840 return (EINVAL);
842 *rdip = cdip;
843 return (0);
847 * Bring the node up to a named but OFFLINE state. The calling
848 * application will need to manage the node from here on.
850 if (dcp->flags & DEVCTL_OFFLINE) {
852 * In the unlikely event that the dip was somehow attached by
853 * the userland process (and device contracts or LDI opens
854 * were registered against the dip) after it was created by
855 * a previous DEVCTL_CONSTRUCT call, we start notify
856 * proceedings on this dip. Note that we don't need to
857 * return the dip after a failure of the notify since
858 * for a contract or LDI handle to be created the dip was
859 * already available to the user.
861 if (e_ddi_offline_notify(cdip) == DDI_FAILURE) {
862 return (EBUSY);
866 * hand set the OFFLINE flag to prevent any asynchronous
867 * autoconfiguration operations from attaching this node.
869 mutex_enter(&(DEVI(cdip)->devi_lock));
870 DEVI_SET_DEVICE_OFFLINE(cdip);
871 mutex_exit(&(DEVI(cdip)->devi_lock));
873 e_ddi_offline_finalize(cdip, DDI_SUCCESS);
875 rv = ndi_devi_bind_driver(cdip, flags);
876 if (rv != NDI_SUCCESS) {
877 (void) ndi_devi_offline(cdip, NDI_DEVI_REMOVE);
878 return (ENXIO);
882 * remove the dev_info node if it failed to bind to a
883 * driver above.
885 if (i_ddi_node_state(cdip) < DS_BOUND) {
886 (void) ndi_devi_offline(cdip, NDI_DEVI_REMOVE);
887 return (ENXIO);
891 * add the node to the per-driver list and INITCHILD it
892 * to give it a name.
894 ndi_devi_enter(pdip, &circular);
895 if ((rv = ddi_initchild(pdip, cdip)) != DDI_SUCCESS) {
896 (void) ndi_devi_offline(cdip, NDI_DEVI_REMOVE);
897 ndi_devi_exit(pdip, circular);
898 return (EINVAL);
900 ndi_devi_exit(pdip, circular);
902 } else {
904 * Attempt to bring the device ONLINE. If the request to
905 * fails, remove the dev_info node.
907 if (ndi_devi_online(cdip, NDI_ONLINE_ATTACH) != NDI_SUCCESS) {
908 (void) ndi_devi_offline(cdip, NDI_DEVI_REMOVE);
909 return (ENXIO);
913 * if the node was successfully added but there was
914 * no driver available for the device, remove the node
916 if (i_ddi_node_state(cdip) < DS_BOUND) {
917 (void) ndi_devi_offline(cdip, NDI_DEVI_REMOVE);
918 return (ENODEV);
923 * return a handle to the child device
924 * copy out the name of the newly attached child device if
925 * the IOCTL request has provided a copyout buffer.
927 if (rdip != NULL)
928 *rdip = cdip;
930 if (dcp->cpyout_buf == NULL)
931 return (0);
933 ASSERT(ddi_node_name(cdip) != NULL);
934 ASSERT(ddi_get_name_addr(cdip) != NULL);
936 nmlen = snprintf(devnm, MAXNAMELEN, "%s@%s",
937 ddi_node_name(cdip), ddi_get_name_addr(cdip));
939 if (copyout(&devnm, dcp->cpyout_buf, nmlen) != 0) {
940 (void) ndi_devi_offline(cdip, NDI_DEVI_REMOVE);
941 return (EFAULT);
943 return (0);
946 static int
947 i_dc_devi_create(struct devctl_iocdata *dcp, dev_info_t *pdip,
948 dev_info_t **rdip)
951 dev_info_t *cdip;
952 char *cname = NULL;
953 nvlist_t *nvlp = dcp->nvl_user;
954 nvpair_t *npp;
955 char *np;
956 int rv = 0;
958 ASSERT(rdip != NULL && *rdip == NULL);
960 if ((nvlp == NULL) ||
961 (nvlist_lookup_string(nvlp, DC_DEVI_NODENAME, &cname) != 0))
962 return (EINVAL);
965 * construct a new dev_info node with a user-provided nodename
967 ndi_devi_alloc_sleep(pdip, cname, (pnode_t)DEVI_SID_NODEID, &cdip);
970 * create hardware properties for each member in the property
971 * list.
973 for (npp = nvlist_next_nvpair(nvlp, NULL); (npp != NULL && !rv);
974 npp = nvlist_next_nvpair(nvlp, npp)) {
976 np = nvpair_name(npp);
979 * skip the nodename property
981 if (strcmp(np, DC_DEVI_NODENAME) == 0)
982 continue;
984 switch (nvpair_type(npp)) {
986 case DATA_TYPE_INT32: {
987 int32_t prop_val;
989 if ((rv = nvpair_value_int32(npp, &prop_val)) != 0)
990 break;
992 (void) ndi_prop_update_int(DDI_DEV_T_NONE, cdip, np,
993 (int)prop_val);
994 break;
997 case DATA_TYPE_STRING: {
998 char *prop_val;
1000 if ((rv = nvpair_value_string(npp, &prop_val)) != 0)
1001 break;
1003 (void) ndi_prop_update_string(DDI_DEV_T_NONE, cdip,
1004 np, prop_val);
1005 break;
1008 case DATA_TYPE_BYTE_ARRAY: {
1009 uchar_t *val;
1010 uint_t nelms;
1012 if ((rv = nvpair_value_byte_array(npp, &val,
1013 &nelms)) != 0)
1014 break;
1016 (void) ndi_prop_update_byte_array(DDI_DEV_T_NONE,
1017 cdip, np, (uchar_t *)val, nelms);
1018 break;
1021 case DATA_TYPE_INT32_ARRAY: {
1022 int32_t *val;
1023 uint_t nelms;
1025 if ((rv = nvpair_value_int32_array(npp, &val,
1026 &nelms)) != 0)
1027 break;
1029 (void) ndi_prop_update_int_array(DDI_DEV_T_NONE,
1030 cdip, np, val, nelms);
1031 break;
1034 case DATA_TYPE_STRING_ARRAY: {
1035 char **val;
1036 uint_t nelms;
1038 if ((rv = nvpair_value_string_array(npp, &val,
1039 &nelms)) != 0)
1040 break;
1042 (void) ndi_prop_update_string_array(DDI_DEV_T_NONE,
1043 cdip, np, val, nelms);
1044 break;
1048 * unsupported property data type
1050 default:
1051 rv = EINVAL;
1056 * something above failed
1057 * destroy the partially child device and abort the request
1059 if (rv != 0) {
1060 (void) ndi_devi_free(cdip);
1061 return (rv);
1064 *rdip = cdip;
1065 return (0);
1069 * return current soft bus state of bus nexus "dip"
1072 ndi_get_bus_state(dev_info_t *dip, uint_t *rstate)
1074 if (dip == NULL || rstate == NULL)
1075 return (NDI_FAILURE);
1077 if (DEVI(dip)->devi_ops->devo_bus_ops == NULL)
1078 return (NDI_FAILURE);
1080 mutex_enter(&(DEVI(dip)->devi_lock));
1081 if (DEVI_IS_BUS_QUIESCED(dip))
1082 *rstate = BUS_QUIESCED;
1083 else if (DEVI_IS_BUS_DOWN(dip))
1084 *rstate = BUS_SHUTDOWN;
1085 else
1086 *rstate = BUS_ACTIVE;
1087 mutex_exit(&(DEVI(dip)->devi_lock));
1088 return (NDI_SUCCESS);
1092 * Set the soft state of bus nexus "dip"
1095 ndi_set_bus_state(dev_info_t *dip, uint_t state)
1097 int rv = NDI_SUCCESS;
1099 if (dip == NULL)
1100 return (NDI_FAILURE);
1102 mutex_enter(&(DEVI(dip)->devi_lock));
1104 switch (state) {
1105 case BUS_QUIESCED:
1106 DEVI_SET_BUS_QUIESCE(dip);
1107 break;
1109 case BUS_ACTIVE:
1110 DEVI_SET_BUS_ACTIVE(dip);
1111 DEVI_SET_BUS_UP(dip);
1112 break;
1114 case BUS_SHUTDOWN:
1115 DEVI_SET_BUS_DOWN(dip);
1116 break;
1118 default:
1119 rv = NDI_FAILURE;
1122 mutex_exit(&(DEVI(dip)->devi_lock));
1123 return (rv);
1127 * These dummy functions are obsolete and may be removed.
1128 * Retained for existing driver compatibility only.
1129 * Drivers should be fixed not to use these functions.
1130 * Don't write new code using these obsolete interfaces.
1132 /*ARGSUSED*/
1133 void
1134 i_ndi_block_device_tree_changes(uint_t *lkcnt) /* obsolete */
1136 /* obsolete dummy function */
1139 /*ARGSUSED*/
1140 void
1141 i_ndi_allow_device_tree_changes(uint_t lkcnt) /* obsolete */
1143 /* obsolete dummy function */
1147 * Single thread entry into per-driver list
1149 /*ARGSUSED*/
1150 void
1151 e_ddi_enter_driver_list(struct devnames *dnp, int *listcnt) /* obsolete */
1153 /* obsolete dummy function */
1157 * release the per-driver list
1159 /*ARGSUSED*/
1160 void
1161 e_ddi_exit_driver_list(struct devnames *dnp, int listcnt) /* obsolete */
1163 /* obsolete dummy function */
1167 * Attempt to enter driver list
1169 /*ARGSUSED*/
1171 e_ddi_tryenter_driver_list(struct devnames *dnp, int *listcnt) /* obsolete */
1173 return (1); /* obsolete dummy function */
1177 * ndi event handling support functions:
1178 * The NDI event support model is as follows:
1180 * The nexus driver defines a set of events using some static structures (so
1181 * these structures can be shared by all instances of the nexus driver).
1182 * The nexus driver allocates an event handle and binds the event set
1183 * to this handle. The nexus driver's event busop functions can just
1184 * call the appropriate NDI event support function using this handle
1185 * as the first argument.
1187 * The reasoning for tying events to the device tree is that the entity
1188 * generating the callback will typically be one of the device driver's
1189 * ancestors in the tree.
1191 static int ndi_event_debug = 0;
1193 #ifdef DEBUG
1194 #define NDI_EVENT_DEBUG ndi_event_debug
1195 #endif /* DEBUG */
1198 * allocate a new ndi event handle
1201 ndi_event_alloc_hdl(dev_info_t *dip, ddi_iblock_cookie_t cookie,
1202 ndi_event_hdl_t *handle, uint_t flag)
1204 struct ndi_event_hdl *ndi_event_hdl;
1206 ndi_event_hdl = kmem_zalloc(sizeof (struct ndi_event_hdl),
1207 ((flag & NDI_NOSLEEP) ? KM_NOSLEEP : KM_SLEEP));
1209 if (!ndi_event_hdl) {
1210 return (NDI_FAILURE);
1213 ndi_event_hdl->ndi_evthdl_dip = dip;
1214 ndi_event_hdl->ndi_evthdl_iblock_cookie = cookie;
1215 mutex_init(&ndi_event_hdl->ndi_evthdl_mutex, NULL,
1216 MUTEX_DRIVER, (void *)cookie);
1218 mutex_init(&ndi_event_hdl->ndi_evthdl_cb_mutex, NULL,
1219 MUTEX_DRIVER, (void *)cookie);
1221 *handle = (ndi_event_hdl_t)ndi_event_hdl;
1223 return (NDI_SUCCESS);
1227 * free the ndi event handle
1230 ndi_event_free_hdl(ndi_event_hdl_t handle)
1232 struct ndi_event_hdl *ndi_event_hdl = (struct ndi_event_hdl *)handle;
1233 ndi_event_cookie_t *cookie;
1234 ndi_event_cookie_t *free;
1236 ASSERT(handle);
1238 mutex_enter(&ndi_event_hdl->ndi_evthdl_mutex);
1239 mutex_enter(&ndi_event_hdl->ndi_evthdl_cb_mutex);
1241 cookie = ndi_event_hdl->ndi_evthdl_cookie_list;
1243 /* deallocate all defined cookies */
1244 while (cookie != NULL) {
1245 ASSERT(cookie->callback_list == NULL);
1246 free = cookie;
1247 cookie = cookie->next_cookie;
1249 kmem_free(free, sizeof (ndi_event_cookie_t));
1253 mutex_exit(&ndi_event_hdl->ndi_evthdl_cb_mutex);
1254 mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
1256 /* destroy mutexes */
1257 mutex_destroy(&ndi_event_hdl->ndi_evthdl_mutex);
1258 mutex_destroy(&ndi_event_hdl->ndi_evthdl_cb_mutex);
1260 /* free event handle */
1261 kmem_free(ndi_event_hdl, sizeof (struct ndi_event_hdl));
1263 return (NDI_SUCCESS);
1268 * ndi_event_bind_set() adds a set of events to the NDI event
1269 * handle.
1271 * Events generated by high level interrupts should not
1272 * be mixed in the same event set with events generated by
1273 * normal interrupts or kernel events.
1275 * This function can be called multiple times to bind
1276 * additional sets to the event handle.
1277 * However, events generated by high level interrupts cannot
1278 * be bound to a handle that already has bound events generated
1279 * by normal interrupts or from kernel context and vice versa.
1282 ndi_event_bind_set(ndi_event_hdl_t handle,
1283 ndi_event_set_t *ndi_events,
1284 uint_t flag)
1286 struct ndi_event_hdl *ndi_event_hdl;
1287 ndi_event_cookie_t *next, *prev, *new_cookie;
1288 uint_t i, len;
1289 uint_t dup = 0;
1290 uint_t high_plevels, other_plevels;
1291 ndi_event_definition_t *ndi_event_defs;
1293 int km_flag = ((flag & NDI_NOSLEEP) ? KM_NOSLEEP : KM_SLEEP);
1295 ASSERT(handle);
1296 ASSERT(ndi_events);
1299 * binding must be performed during attach/detach
1301 if (!DEVI_IS_ATTACHING(handle->ndi_evthdl_dip) &&
1302 !DEVI_IS_DETACHING(handle->ndi_evthdl_dip)) {
1303 cmn_err(CE_WARN, "ndi_event_bind_set must be called within "
1304 "attach or detach");
1305 return (NDI_FAILURE);
1309 * if it is not the correct version or the event set is
1310 * empty, bail out
1312 if (ndi_events->ndi_events_version != NDI_EVENTS_REV1)
1313 return (NDI_FAILURE);
1315 ndi_event_hdl = (struct ndi_event_hdl *)handle;
1316 ndi_event_defs = ndi_events->ndi_event_defs;
1317 high_plevels = other_plevels = 0;
1319 mutex_enter(&ndi_event_hdl->ndi_evthdl_mutex);
1321 /* check for mixing events at high level with the other types */
1322 for (i = 0; i < ndi_events->ndi_n_events; i++) {
1323 if (ndi_event_defs[i].ndi_event_plevel == EPL_HIGHLEVEL) {
1324 high_plevels++;
1325 } else {
1326 other_plevels++;
1331 * bail out if high level events are mixed with other types in this
1332 * event set or the set is incompatible with the set in the handle
1334 if ((high_plevels && other_plevels) ||
1335 (other_plevels && ndi_event_hdl->ndi_evthdl_high_plevels) ||
1336 (high_plevels && ndi_event_hdl->ndi_evthdl_other_plevels)) {
1337 mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
1339 return (NDI_FAILURE);
1343 * check for duplicate events in both the existing handle
1344 * and the event set, add events if not duplicates
1346 next = ndi_event_hdl->ndi_evthdl_cookie_list;
1347 for (i = 0; i < ndi_events->ndi_n_events; i++) {
1348 while (next != NULL) {
1349 len = strlen(NDI_EVENT_NAME(next)) + 1;
1350 if (strncmp(NDI_EVENT_NAME(next),
1351 ndi_event_defs[i].ndi_event_name, len) == 0) {
1352 dup = 1;
1353 break;
1356 prev = next;
1357 next = next->next_cookie;
1360 if (dup == 0) {
1361 new_cookie = kmem_zalloc(sizeof (ndi_event_cookie_t),
1362 km_flag);
1364 if (!new_cookie)
1365 return (NDI_FAILURE);
1367 if (ndi_event_hdl->ndi_evthdl_n_events == 0) {
1368 ndi_event_hdl->ndi_evthdl_cookie_list =
1369 new_cookie;
1370 } else {
1371 prev->next_cookie = new_cookie;
1374 ndi_event_hdl->ndi_evthdl_n_events++;
1377 * set up new cookie
1379 new_cookie->definition = &ndi_event_defs[i];
1380 new_cookie->ddip = ndi_event_hdl->ndi_evthdl_dip;
1382 } else {
1384 * event not added, must correct plevel numbers
1386 if (ndi_event_defs[i].ndi_event_plevel ==
1387 EPL_HIGHLEVEL) {
1388 high_plevels--;
1389 } else {
1390 other_plevels--;
1394 dup = 0;
1395 next = ndi_event_hdl->ndi_evthdl_cookie_list;
1396 prev = NULL;
1400 ndi_event_hdl->ndi_evthdl_high_plevels += high_plevels;
1401 ndi_event_hdl->ndi_evthdl_other_plevels += other_plevels;
1403 ASSERT((ndi_event_hdl->ndi_evthdl_high_plevels == 0) ||
1404 (ndi_event_hdl->ndi_evthdl_other_plevels == 0));
1406 #ifdef NDI_EVENT_DEBUG
1407 if (ndi_event_debug) {
1408 ndi_event_dump_hdl(ndi_event_hdl, "ndi_event_bind_set");
1410 #endif /* NDI_EVENT_DEBUG */
1412 mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
1414 return (NDI_SUCCESS);
1418 * ndi_event_unbind_set() unbinds a set of events, previously
1419 * bound using ndi_event_bind_set(), from the NDI event
1420 * handle.
1422 * This routine will unbind all events in the event set. If an event,
1423 * specified in the event set, is not found in the handle, this
1424 * routine will proceed onto the next member of the set as if the event
1425 * was never specified.
1427 * The event set may be a subset of the set of events that
1428 * was previously bound to the handle. For example, events
1429 * can be individually unbound.
1431 * An event cannot be unbound if callbacks are still
1432 * registered against the event.
1434 /*ARGSUSED*/
1436 ndi_event_unbind_set(ndi_event_hdl_t handle, ndi_event_set_t *ndi_events,
1437 uint_t flag)
1439 ndi_event_definition_t *ndi_event_defs;
1440 int len;
1441 uint_t i;
1442 int rval;
1443 ndi_event_cookie_t *cookie_list;
1444 ndi_event_cookie_t *prev = NULL;
1446 ASSERT(ndi_events);
1447 ASSERT(handle);
1450 * binding must be performed during attach/detac
1452 if (!DEVI_IS_ATTACHING(handle->ndi_evthdl_dip) &&
1453 !DEVI_IS_DETACHING(handle->ndi_evthdl_dip)) {
1454 cmn_err(CE_WARN, "ndi_event_bind_set must be called within "
1455 "attach or detach");
1456 return (NDI_FAILURE);
1459 /* bail out if ndi_event_set is outdated */
1460 if (ndi_events->ndi_events_version != NDI_EVENTS_REV1) {
1461 return (NDI_FAILURE);
1464 ASSERT(ndi_events->ndi_event_defs);
1466 ndi_event_defs = ndi_events->ndi_event_defs;
1468 mutex_enter(&handle->ndi_evthdl_mutex);
1469 mutex_enter(&handle->ndi_evthdl_cb_mutex);
1472 * Verify that all events in the event set are eligible
1473 * for unbinding(ie. there are no outstanding callbacks).
1474 * If any one of the events are ineligible, fail entire
1475 * operation.
1478 for (i = 0; i < ndi_events->ndi_n_events; i++) {
1479 cookie_list = handle->ndi_evthdl_cookie_list;
1480 while (cookie_list != NULL) {
1481 len = strlen(NDI_EVENT_NAME(cookie_list)) + 1;
1482 if (strncmp(NDI_EVENT_NAME(cookie_list),
1483 ndi_event_defs[i].ndi_event_name, len) == 0) {
1485 ASSERT(cookie_list->callback_list == NULL);
1486 if (cookie_list->callback_list) {
1487 rval = NDI_FAILURE;
1488 goto done;
1490 break;
1491 } else {
1492 cookie_list = cookie_list->next_cookie;
1498 * remove all events found within the handle
1499 * If an event is not found, this function will proceed as if the event
1500 * was never specified.
1503 for (i = 0; i < ndi_events->ndi_n_events; i++) {
1504 cookie_list = handle->ndi_evthdl_cookie_list;
1505 prev = NULL;
1506 while (cookie_list != NULL) {
1507 len = strlen(NDI_EVENT_NAME(cookie_list)) + 1;
1508 if (strncmp(NDI_EVENT_NAME(cookie_list),
1509 ndi_event_defs[i].ndi_event_name, len) == 0) {
1512 * can not unbind an event definition with
1513 * outstanding callbacks
1515 if (cookie_list->callback_list) {
1516 rval = NDI_FAILURE;
1517 goto done;
1520 /* remove this cookie from the list */
1521 if (prev != NULL) {
1522 prev->next_cookie =
1523 cookie_list->next_cookie;
1524 } else {
1525 handle->ndi_evthdl_cookie_list =
1526 cookie_list->next_cookie;
1529 /* adjust plevel counts */
1530 if (NDI_EVENT_PLEVEL(cookie_list) ==
1531 EPL_HIGHLEVEL) {
1532 handle->ndi_evthdl_high_plevels--;
1533 } else {
1534 handle->ndi_evthdl_other_plevels--;
1537 /* adjust cookie count */
1538 handle->ndi_evthdl_n_events--;
1540 /* free the cookie */
1541 kmem_free(cookie_list,
1542 sizeof (ndi_event_cookie_t));
1544 cookie_list = handle->ndi_evthdl_cookie_list;
1545 break;
1547 } else {
1548 prev = cookie_list;
1549 cookie_list = cookie_list->next_cookie;
1556 #ifdef NDI_EVENT_DEBUG
1557 if (ndi_event_debug) {
1558 ndi_event_dump_hdl(handle, "ndi_event_unbind_set");
1560 #endif /* NDI_EVENT_DEBUG */
1562 rval = NDI_SUCCESS;
1564 done:
1565 mutex_exit(&handle->ndi_evthdl_cb_mutex);
1566 mutex_exit(&handle->ndi_evthdl_mutex);
1568 return (rval);
1572 * ndi_event_retrieve_cookie():
1573 * Return an event cookie for eventname if this nexus driver
1574 * has defined the named event. The event cookie returned
1575 * by this function is used to register callback handlers
1576 * for the event.
1578 * ndi_event_retrieve_cookie() is intended to be used in the
1579 * nexus driver's bus_get_eventcookie busop routine.
1581 * If the event is not defined by this bus nexus driver, and flag
1582 * does not include NDI_EVENT_NOPASS, then ndi_event_retrieve_cookie()
1583 * will pass the request up the device tree hierarchy by calling
1584 * ndi_busop_get_eventcookie(9N).
1585 * If the event is not defined by this bus nexus driver, and flag
1586 * does include NDI_EVENT_NOPASS, ndi_event_retrieve_cookie()
1587 * will return NDI_FAILURE. The caller may then determine what further
1588 * action to take, such as using a different handle, passing the
1589 * request up the device tree using ndi_busop_get_eventcookie(9N),
1590 * or returning the failure to the caller, thus blocking the
1591 * progress of the request up the tree.
1594 ndi_event_retrieve_cookie(ndi_event_hdl_t handle,
1595 dev_info_t *rdip,
1596 char *eventname,
1597 ddi_eventcookie_t *cookiep,
1598 uint_t flag)
1600 struct ndi_event_hdl *ndi_event_hdl = (struct ndi_event_hdl *)handle;
1601 int len;
1602 ndi_event_cookie_t *cookie_list;
1604 mutex_enter(&ndi_event_hdl->ndi_evthdl_mutex);
1606 cookie_list = ndi_event_hdl->ndi_evthdl_cookie_list;
1608 * search the cookie list for the event name and return
1609 * cookie if found.
1611 while (cookie_list != NULL) {
1613 len = strlen(NDI_EVENT_NAME(cookie_list)) + 1;
1614 if (strncmp(NDI_EVENT_NAME(cookie_list), eventname,
1615 len) == 0) {
1616 *cookiep = (ddi_eventcookie_t)cookie_list;
1618 mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
1619 return (NDI_SUCCESS);
1622 cookie_list = cookie_list->next_cookie;
1625 mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
1627 * event was not found, pass up or return failure
1629 if ((flag & NDI_EVENT_NOPASS) == 0) {
1630 return (ndi_busop_get_eventcookie(
1631 ndi_event_hdl->ndi_evthdl_dip, rdip, eventname, cookiep));
1632 } else {
1633 return (NDI_FAILURE);
1638 * check whether this nexus defined this event and look up attributes
1640 static int
1641 ndi_event_is_defined(ndi_event_hdl_t handle,
1642 ddi_eventcookie_t cookie, int *attributes)
1645 struct ndi_event_hdl *ndi_event_hdl = (struct ndi_event_hdl *)handle;
1646 ndi_event_cookie_t *cookie_list;
1648 ASSERT(mutex_owned(&handle->ndi_evthdl_mutex));
1650 cookie_list = ndi_event_hdl->ndi_evthdl_cookie_list;
1651 while (cookie_list != NULL) {
1652 if (cookie_list == NDI_EVENT(cookie)) {
1653 if (attributes)
1654 *attributes =
1655 NDI_EVENT_ATTRIBUTES(cookie_list);
1657 return (NDI_SUCCESS);
1660 cookie_list = cookie_list->next_cookie;
1663 return (NDI_FAILURE);
1667 * ndi_event_add_callback(): adds an event callback registration
1668 * to the event cookie defining this event.
1670 * Refer also to bus_add_eventcall(9n) and ndi_busop_add_eventcall(9n).
1672 * ndi_event_add_callback(9n) is intended to be used in
1673 * the nexus driver's bus_add_eventcall(9n) busop function.
1675 * If the event is not defined by this bus nexus driver,
1676 * ndi_event_add_callback() will return NDI_FAILURE.
1679 ndi_event_add_callback(ndi_event_hdl_t handle, dev_info_t *child_dip,
1680 ddi_eventcookie_t cookie,
1681 void (*event_callback)(dev_info_t *,
1682 ddi_eventcookie_t, void *arg, void *impldata),
1683 void *arg,
1684 uint_t flag,
1685 ddi_callback_id_t *cb_id)
1687 struct ndi_event_hdl *ndi_event_hdl = (struct ndi_event_hdl *)handle;
1688 int km_flag = ((flag & NDI_NOSLEEP) ? KM_NOSLEEP : KM_SLEEP);
1689 ndi_event_callbacks_t *cb;
1691 mutex_enter(&ndi_event_hdl->ndi_evthdl_mutex);
1694 * if the event was not bound to this handle, return failure
1696 if (ndi_event_is_defined(handle, cookie, NULL) != NDI_SUCCESS) {
1698 mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
1699 return (NDI_FAILURE);
1703 mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
1706 * allocate space for a callback structure
1708 cb = kmem_zalloc(sizeof (ndi_event_callbacks_t), km_flag);
1709 if (cb == NULL) {
1710 return (NDI_FAILURE);
1713 mutex_enter(&ndi_event_hdl->ndi_evthdl_mutex);
1715 /* initialize callback structure */
1716 cb->ndi_evtcb_dip = child_dip;
1717 cb->ndi_evtcb_callback = event_callback;
1718 cb->ndi_evtcb_arg = arg;
1719 cb->ndi_evtcb_cookie = cookie;
1720 cb->devname = (char *)ddi_driver_name(child_dip);
1722 *cb_id = (ddi_callback_id_t)cb;
1723 mutex_enter(&ndi_event_hdl->ndi_evthdl_cb_mutex);
1725 /* add this callback structure to the list */
1726 if (NDI_EVENT(cookie)->callback_list) {
1727 cb->ndi_evtcb_next = NDI_EVENT(cookie)->callback_list;
1728 NDI_EVENT(cookie)->callback_list->ndi_evtcb_prev = cb;
1729 NDI_EVENT(cookie)->callback_list = cb;
1730 } else {
1731 NDI_EVENT(cookie)->callback_list = cb;
1733 #ifdef NDI_EVENT_DEBUG
1734 if (ndi_event_debug) {
1735 ndi_event_dump_hdl(ndi_event_hdl, "ndi_event_add_callback");
1737 #endif /* NDI_EVENT_DEBUG */
1739 mutex_exit(&ndi_event_hdl->ndi_evthdl_cb_mutex);
1740 mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
1742 return (NDI_SUCCESS);
1746 * ndi_event_remove_callback():
1748 * ndi_event_remove_callback() removes a callback that was
1749 * previously registered using ndi_event_add_callback(9N).
1750 * Refer also to bus_remove_eventcall(9n) and
1751 * ndi_busop_remove_eventcall(9n).
1752 * ndi_event_remove_callback(9n) is intended to be used in
1753 * the nexus driver's bus_remove_eventcall (9n) busop function.
1754 * If the event is not defined by this bus nexus driver,
1755 * ndi_event_remove_callback() will return NDI_FAILURE.
1757 static void do_ndi_event_remove_callback(struct ndi_event_hdl *ndi_event_hdl,
1758 ddi_callback_id_t cb_id);
1761 ndi_event_remove_callback(ndi_event_hdl_t handle, ddi_callback_id_t cb_id)
1763 struct ndi_event_hdl *ndi_event_hdl = (struct ndi_event_hdl *)handle;
1765 ASSERT(cb_id);
1767 mutex_enter(&ndi_event_hdl->ndi_evthdl_mutex);
1768 mutex_enter(&ndi_event_hdl->ndi_evthdl_cb_mutex);
1770 do_ndi_event_remove_callback(ndi_event_hdl, cb_id);
1772 mutex_exit(&ndi_event_hdl->ndi_evthdl_cb_mutex);
1773 mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
1775 return (NDI_SUCCESS);
1778 /*ARGSUSED*/
1779 static void
1780 do_ndi_event_remove_callback(struct ndi_event_hdl *ndi_event_hdl,
1781 ddi_callback_id_t cb_id)
1783 ndi_event_callbacks_t *cb = (ndi_event_callbacks_t *)cb_id;
1784 ASSERT(cb);
1786 ASSERT(mutex_owned(&ndi_event_hdl->ndi_evthdl_mutex));
1787 ASSERT(mutex_owned(&ndi_event_hdl->ndi_evthdl_cb_mutex));
1789 /* remove from callback linked list */
1790 if (cb->ndi_evtcb_prev) {
1791 cb->ndi_evtcb_prev->ndi_evtcb_next = cb->ndi_evtcb_next;
1794 if (cb->ndi_evtcb_next) {
1795 cb->ndi_evtcb_next->ndi_evtcb_prev = cb->ndi_evtcb_prev;
1798 if (NDI_EVENT(cb->ndi_evtcb_cookie)->callback_list == cb) {
1799 NDI_EVENT(cb->ndi_evtcb_cookie)->callback_list =
1800 cb->ndi_evtcb_next;
1803 kmem_free(cb, sizeof (ndi_event_callbacks_t));
1807 * ndi_event_run_callbacks() performs event callbacks for the event
1808 * specified by cookie, if this is among those bound to the
1809 * supplied handle.
1810 * If the event is among those bound to the handle, none,
1811 * some, or all of the handlers registered for the event
1812 * will be called, according to the delivery attributes of
1813 * the event.
1814 * If the event attributes include NDI_EVENT_POST_TO_ALL
1815 * (the default), all the handlers for the event will be
1816 * called in an unspecified order.
1817 * If the event attributes include NDI_EVENT_POST_TO_TGT, only
1818 * the handlers (if any) registered by the driver identified by
1819 * rdip will be called.
1820 * If the event identified by cookie is not bound to the handle,
1821 * NDI_FAILURE will be returned.
1824 ndi_event_run_callbacks(ndi_event_hdl_t handle, dev_info_t *child_dip,
1825 ddi_eventcookie_t cookie, void *bus_impldata)
1827 struct ndi_event_hdl *ndi_event_hdl = (struct ndi_event_hdl *)handle;
1828 ndi_event_callbacks_t *next, *cb;
1829 int attributes;
1831 mutex_enter(&ndi_event_hdl->ndi_evthdl_mutex);
1833 /* if this is not our event, fail */
1834 if (ndi_event_is_defined(handle, cookie, &attributes) !=
1835 NDI_SUCCESS) {
1837 mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
1838 return (NDI_FAILURE);
1841 mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
1843 #ifdef NDI_EVENT_DEBUG
1844 if (ndi_event_debug) {
1845 cmn_err(CE_CONT, "ndi_event_run_callbacks:\n\t"
1846 "producer dip=%p (%s%d): cookie = %p, name = %s\n",
1847 (void *)ndi_event_hdl->ndi_evthdl_dip,
1848 ddi_node_name(ndi_event_hdl->ndi_evthdl_dip),
1849 ddi_get_instance(ndi_event_hdl->ndi_evthdl_dip),
1850 (void *)cookie,
1851 ndi_event_cookie_to_name(handle, cookie));
1853 #endif /* #ifdef NDI_EVENT_DEBUG */
1857 * The callback handlers may call conversion functions. The conversion
1858 * functions may hold the ndi_evthdl_mutex during execution. Thus, to
1859 * avoid a recursive mutex problem, only the ndi_evthdl_cb_mutex is
1860 * held. The ndi_evthdl_mutex is not held when running the callbacks.
1862 mutex_enter(&ndi_event_hdl->ndi_evthdl_cb_mutex);
1864 /* perform callbacks */
1865 next = NDI_EVENT(cookie)->callback_list;
1866 while (next != NULL) {
1868 cb = next;
1869 next = next->ndi_evtcb_next;
1871 ASSERT(cb->ndi_evtcb_cookie == cookie);
1873 if (attributes == NDI_EVENT_POST_TO_TGT &&
1874 child_dip != cb->ndi_evtcb_dip) {
1875 continue;
1878 cb->ndi_evtcb_callback(cb->ndi_evtcb_dip, cb->ndi_evtcb_cookie,
1879 cb->ndi_evtcb_arg, bus_impldata);
1881 #ifdef NDI_EVENT_DEBUG
1882 if (ndi_event_debug) {
1883 cmn_err(CE_CONT,
1884 "\t\tconsumer dip=%p (%s%d)\n",
1885 (void *)cb->ndi_evtcb_dip,
1886 ddi_node_name(cb->ndi_evtcb_dip),
1887 ddi_get_instance(cb->ndi_evtcb_dip));
1889 #endif
1893 mutex_exit(&ndi_event_hdl->ndi_evthdl_cb_mutex);
1895 #ifdef NDI_EVENT_DEBUG
1896 if (ndi_event_debug) {
1897 mutex_enter(&ndi_event_hdl->ndi_evthdl_mutex);
1898 ndi_event_dump_hdl(ndi_event_hdl, "ndi_event_run_callbacks");
1899 mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
1901 #endif /* NDI_EVENT_DEBUG */
1903 return (NDI_SUCCESS);
1908 * perform one callback for a specified cookie and just one target
1911 ndi_event_do_callback(ndi_event_hdl_t handle, dev_info_t *child_dip,
1912 ddi_eventcookie_t cookie, void *bus_impldata)
1914 struct ndi_event_hdl *ndi_event_hdl = (struct ndi_event_hdl *)handle;
1915 ndi_event_callbacks_t *next, *cb;
1916 int attributes;
1918 mutex_enter(&ndi_event_hdl->ndi_evthdl_mutex);
1920 /* if this is not our event, fail */
1921 if (ndi_event_is_defined(handle, cookie, &attributes) !=
1922 NDI_SUCCESS) {
1924 mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
1926 return (NDI_FAILURE);
1929 mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
1931 #ifdef NDI_EVENT_DEBUG
1932 if (ndi_event_debug) {
1933 cmn_err(CE_CONT, "ndi_event_run_callbacks:\n\t"
1934 "producer dip=%p (%s%d): cookie = %p, name = %s\n",
1935 (void *)ndi_event_hdl->ndi_evthdl_dip,
1936 ddi_node_name(ndi_event_hdl->ndi_evthdl_dip),
1937 ddi_get_instance(ndi_event_hdl->ndi_evthdl_dip),
1938 (void *)cookie,
1939 ndi_event_cookie_to_name(handle, cookie));
1941 #endif
1945 * we only grab the cb mutex because the callback handlers
1946 * may call the conversion functions which would cause a recursive
1947 * mutex problem
1949 mutex_enter(&ndi_event_hdl->ndi_evthdl_cb_mutex);
1951 /* perform callbacks */
1952 for (next = NDI_EVENT(cookie)->callback_list; next != NULL; ) {
1953 cb = next;
1954 next = next->ndi_evtcb_next;
1956 if (cb->ndi_evtcb_dip == child_dip) {
1957 cb->ndi_evtcb_callback(cb->ndi_evtcb_dip,
1958 cb->ndi_evtcb_cookie, cb->ndi_evtcb_arg,
1959 bus_impldata);
1961 #ifdef NDI_EVENT_DEBUG
1962 if (ndi_event_debug) {
1963 cmn_err(CE_CONT,
1964 "\t\tconsumer dip=%p (%s%d)\n",
1965 (void *)cb->ndi_evtcb_dip,
1966 ddi_node_name(cb->ndi_evtcb_dip),
1967 ddi_get_instance(cb->ndi_evtcb_dip));
1969 #endif
1970 break;
1974 mutex_exit(&ndi_event_hdl->ndi_evthdl_cb_mutex);
1976 #ifdef NDI_EVENT_DEBUG
1977 if (ndi_event_debug) {
1978 mutex_enter(&ndi_event_hdl->ndi_evthdl_mutex);
1979 ndi_event_dump_hdl(ndi_event_hdl, "ndi_event_run_callbacks");
1980 mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
1982 #endif /* NDI_EVENT_DEBUG */
1984 return (NDI_SUCCESS);
1989 * ndi_event_tag_to_cookie: utility function to find an event cookie
1990 * given an event tag
1992 ddi_eventcookie_t
1993 ndi_event_tag_to_cookie(ndi_event_hdl_t handle, int event_tag)
1995 struct ndi_event_hdl *ndi_event_hdl = (struct ndi_event_hdl *)handle;
1996 ndi_event_cookie_t *list;
1998 mutex_enter(&ndi_event_hdl->ndi_evthdl_mutex);
2000 list = ndi_event_hdl->ndi_evthdl_cookie_list;
2001 while (list != NULL) {
2002 if (NDI_EVENT_TAG(list) == event_tag) {
2003 mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
2004 return ((ddi_eventcookie_t)list);
2007 list = list->next_cookie;
2010 mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
2011 return (NULL);
2015 * ndi_event_cookie_to_tag: utility function to find a event tag
2016 * given an event_cookie
2019 ndi_event_cookie_to_tag(ndi_event_hdl_t handle, ddi_eventcookie_t cookie)
2021 struct ndi_event_hdl *ndi_event_hdl = (struct ndi_event_hdl *)handle;
2022 ndi_event_cookie_t *list;
2024 mutex_enter(&ndi_event_hdl->ndi_evthdl_mutex);
2026 list = ndi_event_hdl->ndi_evthdl_cookie_list;
2028 while (list != NULL) {
2029 if ((ddi_eventcookie_t)list == cookie) {
2030 mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
2031 return (NDI_EVENT_TAG(list));
2034 list = list->next_cookie;
2037 mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
2038 return (NDI_FAILURE);
2043 * ndi_event_cookie_to_name: utility function to find an event name
2044 * given an event_cookie
2046 char *
2047 ndi_event_cookie_to_name(ndi_event_hdl_t handle, ddi_eventcookie_t cookie)
2049 struct ndi_event_hdl *ndi_event_hdl = (struct ndi_event_hdl *)handle;
2050 ndi_event_cookie_t *list;
2052 mutex_enter(&ndi_event_hdl->ndi_evthdl_mutex);
2054 list = ndi_event_hdl->ndi_evthdl_cookie_list;
2056 while (list != NULL) {
2057 if (list == NDI_EVENT(cookie)) {
2058 mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
2059 return (NDI_EVENT_NAME(list));
2062 list = list->next_cookie;
2065 mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
2066 return (NULL);
2070 * ndi_event_tag_to_name: utility function to find an event name
2071 * given an event tag
2073 char *
2074 ndi_event_tag_to_name(ndi_event_hdl_t handle, int event_tag)
2076 struct ndi_event_hdl *ndi_event_hdl = (struct ndi_event_hdl *)handle;
2077 ndi_event_cookie_t *list;
2079 mutex_enter(&ndi_event_hdl->ndi_evthdl_mutex);
2081 list = ndi_event_hdl->ndi_evthdl_cookie_list;
2083 while (list) {
2084 if (NDI_EVENT_TAG(list) == event_tag) {
2085 mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
2086 return (NDI_EVENT_NAME(list));
2089 list = list->next_cookie;
2092 mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
2094 return (NULL);
2097 #ifdef NDI_EVENT_DEBUG
2098 void
2099 ndi_event_dump_hdl(struct ndi_event_hdl *hdl, char *location)
2103 ndi_event_callbacks_t *next;
2104 ndi_event_cookie_t *list;
2106 ASSERT(mutex_owned(&hdl->ndi_evthdl_mutex));
2107 list = hdl->ndi_evthdl_cookie_list;
2109 cmn_err(CE_CONT, "%s: event handle (%p): dip = %p (%s%d)\n",
2110 location, (void *)hdl, (void *)hdl->ndi_evthdl_dip,
2111 ddi_node_name(hdl->ndi_evthdl_dip),
2112 ddi_get_instance(hdl->ndi_evthdl_dip));
2113 cmn_err(CE_CONT, "\thigh=%d other=%d n=%d\n",
2114 hdl->ndi_evthdl_high_plevels, hdl->ndi_evthdl_other_plevels,
2115 hdl->ndi_evthdl_n_events);
2117 cmn_err(CE_CONT, "\tevent cookies:\n");
2118 while (list) {
2119 cmn_err(CE_CONT, "\t\ttag=%d name=%s p=%d a=%x dd=%p\n",
2120 NDI_EVENT_TAG(list), NDI_EVENT_NAME(list),
2121 NDI_EVENT_PLEVEL(list), NDI_EVENT_ATTRIBUTES(list),
2122 (void *)NDI_EVENT_DDIP(list));
2123 cmn_err(CE_CONT, "\t\tcallbacks:\n");
2124 for (next = list->callback_list; next != NULL;
2125 next = next->ndi_evtcb_next) {
2126 cmn_err(CE_CONT,
2127 "\t\t dip=%p (%s%d) cookie=%p arg=%p\n",
2128 (void*)next->ndi_evtcb_dip,
2129 ddi_driver_name(next->ndi_evtcb_dip),
2130 ddi_get_instance(next->ndi_evtcb_dip),
2131 (void *)next->ndi_evtcb_cookie,
2132 next->ndi_evtcb_arg);
2135 list = list->next_cookie;
2138 cmn_err(CE_CONT, "\n");
2140 #endif
2143 ndi_dev_is_prom_node(dev_info_t *dip)
2145 return (DEVI(dip)->devi_node_class == DDI_NC_PROM);
2149 ndi_dev_is_pseudo_node(dev_info_t *dip)
2152 * NOTE: this does NOT mean the pseudo branch of the device tree,
2153 * it means the node was created by software (DEVI_SID_NODEID ||
2154 * DEVI_PSEUDO_NODEID || DEVI_SID_HIDDEN_NODEID) instead of being
2155 * generated from a PROM node.
2157 return (DEVI(dip)->devi_node_class == DDI_NC_PSEUDO);
2161 ndi_dev_is_persistent_node(dev_info_t *dip)
2163 return ((DEVI(dip)->devi_node_attributes & DDI_PERSISTENT) != 0);
2167 ndi_dev_is_hidden_node(dev_info_t *dip)
2169 return ((DEVI(dip)->devi_node_attributes & DDI_HIDDEN_NODE) != 0);
2173 ndi_dev_is_hotplug_node(dev_info_t *dip)
2175 return ((DEVI(dip)->devi_node_attributes & DDI_HOTPLUG_NODE) != 0);
2178 void
2179 ndi_devi_set_hidden(dev_info_t *dip)
2181 DEVI(dip)->devi_node_attributes |= DDI_HIDDEN_NODE;
2184 void
2185 ndi_devi_clr_hidden(dev_info_t *dip)
2187 DEVI(dip)->devi_node_attributes &= ~DDI_HIDDEN_NODE;
2191 i_ndi_dev_is_auto_assigned_node(dev_info_t *dip)
2193 return ((DEVI(dip)->devi_node_attributes &
2194 DDI_AUTO_ASSIGNED_NODEID) != 0);
2197 void
2198 i_ndi_set_node_class(dev_info_t *dip, ddi_node_class_t c)
2200 DEVI(dip)->devi_node_class = c;
2203 ddi_node_class_t
2204 i_ndi_get_node_class(dev_info_t *dip)
2206 return (DEVI(dip)->devi_node_class);
2209 void
2210 i_ndi_set_node_attributes(dev_info_t *dip, int p)
2212 DEVI(dip)->devi_node_attributes = p;
2216 i_ndi_get_node_attributes(dev_info_t *dip)
2218 return (DEVI(dip)->devi_node_attributes);
2221 void
2222 i_ndi_set_nodeid(dev_info_t *dip, int n)
2224 DEVI(dip)->devi_nodeid = n;
2227 void
2228 ndi_set_acc_fault(ddi_acc_handle_t ah)
2230 i_ddi_acc_set_fault(ah);
2233 void
2234 ndi_clr_acc_fault(ddi_acc_handle_t ah)
2236 i_ddi_acc_clr_fault(ah);
2239 void
2240 ndi_set_dma_fault(ddi_dma_handle_t dh)
2242 i_ddi_dma_set_fault(dh);
2245 void
2246 ndi_clr_dma_fault(ddi_dma_handle_t dh)
2248 i_ddi_dma_clr_fault(dh);
2252 * The default fault-handler, called when the event posted by
2253 * ddi_dev_report_fault() reaches rootnex.
2255 static void
2256 i_ddi_fault_handler(dev_info_t *dip, struct ddi_fault_event_data *fedp)
2258 ASSERT(fedp);
2260 mutex_enter(&(DEVI(dip)->devi_lock));
2261 if (!DEVI_IS_DEVICE_OFFLINE(dip)) {
2262 switch (fedp->f_impact) {
2263 case DDI_SERVICE_LOST:
2264 DEVI_SET_DEVICE_DOWN(dip);
2265 break;
2267 case DDI_SERVICE_DEGRADED:
2268 DEVI_SET_DEVICE_DEGRADED(dip);
2269 break;
2271 case DDI_SERVICE_UNAFFECTED:
2272 default:
2273 break;
2275 case DDI_SERVICE_RESTORED:
2276 DEVI_SET_DEVICE_UP(dip);
2277 break;
2280 mutex_exit(&(DEVI(dip)->devi_lock));
2284 * The default fault-logger, called when the event posted by
2285 * ddi_dev_report_fault() reaches rootnex.
2287 /*ARGSUSED*/
2288 static void
2289 i_ddi_fault_logger(dev_info_t *rdip, struct ddi_fault_event_data *fedp)
2291 ddi_devstate_t newstate;
2292 const char *action;
2293 const char *servstate;
2294 const char *location;
2295 int bad;
2296 int changed;
2297 int level;
2298 int still;
2300 ASSERT(fedp);
2302 bad = 0;
2303 switch (fedp->f_location) {
2304 case DDI_DATAPATH_FAULT:
2305 location = "in datapath to";
2306 break;
2307 case DDI_DEVICE_FAULT:
2308 location = "in";
2309 break;
2310 case DDI_EXTERNAL_FAULT:
2311 location = "external to";
2312 break;
2313 default:
2314 location = "somewhere near";
2315 bad = 1;
2316 break;
2319 newstate = ddi_get_devstate(fedp->f_dip);
2320 switch (newstate) {
2321 case DDI_DEVSTATE_OFFLINE:
2322 servstate = "unavailable";
2323 break;
2324 case DDI_DEVSTATE_DOWN:
2325 servstate = "unavailable";
2326 break;
2327 case DDI_DEVSTATE_QUIESCED:
2328 servstate = "suspended";
2329 break;
2330 case DDI_DEVSTATE_DEGRADED:
2331 servstate = "degraded";
2332 break;
2333 default:
2334 servstate = "available";
2335 break;
2338 changed = (newstate != fedp->f_oldstate);
2339 level = (newstate < fedp->f_oldstate) ? CE_WARN : CE_NOTE;
2340 switch (fedp->f_impact) {
2341 case DDI_SERVICE_LOST:
2342 case DDI_SERVICE_DEGRADED:
2343 case DDI_SERVICE_UNAFFECTED:
2344 /* fault detected; service [still] <servstate> */
2345 action = "fault detected";
2346 still = !changed;
2347 break;
2349 case DDI_SERVICE_RESTORED:
2350 if (newstate != DDI_DEVSTATE_UP) {
2351 /* fault cleared; service still <servstate> */
2352 action = "fault cleared";
2353 still = 1;
2354 } else if (changed) {
2355 /* fault cleared; service <servstate> */
2356 action = "fault cleared";
2357 still = 0;
2358 } else {
2359 /* no fault; service <servstate> */
2360 action = "no fault";
2361 still = 0;
2363 break;
2365 default:
2366 bad = 1;
2367 break;
2370 cmn_err(level, "!%s%d: %s %s device; service %s%s"+(bad|changed),
2371 ddi_driver_name(fedp->f_dip), ddi_get_instance(fedp->f_dip),
2372 bad ? "invalid report of fault" : action,
2373 location, still ? "still " : "", servstate);
2375 cmn_err(level, "!%s%d: %s"+(bad|changed),
2376 ddi_driver_name(fedp->f_dip), ddi_get_instance(fedp->f_dip),
2377 fedp->f_message);
2381 * Platform-settable pointers to fault handler and logger functions.
2382 * These are called by the default rootnex event-posting code when
2383 * a fault event reaches rootnex.
2385 void (*plat_fault_handler)(dev_info_t *, struct ddi_fault_event_data *) =
2386 i_ddi_fault_handler;
2387 void (*plat_fault_logger)(dev_info_t *, struct ddi_fault_event_data *) =
2388 i_ddi_fault_logger;
2391 * Rootnex event definitions ...
2393 enum rootnex_event_tags {
2394 ROOTNEX_FAULT_EVENT
2396 static ndi_event_hdl_t rootnex_event_hdl;
2397 static ndi_event_definition_t rootnex_event_set[] = {
2399 ROOTNEX_FAULT_EVENT,
2400 DDI_DEVI_FAULT_EVENT,
2401 EPL_INTERRUPT,
2402 NDI_EVENT_POST_TO_ALL
2405 static ndi_event_set_t rootnex_events = {
2406 NDI_EVENTS_REV1,
2407 sizeof (rootnex_event_set) / sizeof (rootnex_event_set[0]),
2408 rootnex_event_set
2412 * Initialize rootnex event handle
2414 void
2415 i_ddi_rootnex_init_events(dev_info_t *dip)
2417 if (ndi_event_alloc_hdl(dip, (ddi_iblock_cookie_t)(LOCK_LEVEL-1),
2418 &rootnex_event_hdl, NDI_SLEEP) == NDI_SUCCESS) {
2419 if (ndi_event_bind_set(rootnex_event_hdl,
2420 &rootnex_events, NDI_SLEEP) != NDI_SUCCESS) {
2421 (void) ndi_event_free_hdl(rootnex_event_hdl);
2422 rootnex_event_hdl = NULL;
2428 * Event-handling functions for rootnex
2429 * These provide the standard implementation of fault handling
2431 /*ARGSUSED*/
2433 i_ddi_rootnex_get_eventcookie(dev_info_t *dip, dev_info_t *rdip,
2434 char *eventname, ddi_eventcookie_t *cookiep)
2436 if (rootnex_event_hdl == NULL)
2437 return (NDI_FAILURE);
2438 return (ndi_event_retrieve_cookie(rootnex_event_hdl, rdip, eventname,
2439 cookiep, NDI_EVENT_NOPASS));
2442 /*ARGSUSED*/
2444 i_ddi_rootnex_add_eventcall(dev_info_t *dip, dev_info_t *rdip,
2445 ddi_eventcookie_t eventid, void (*handler)(dev_info_t *dip,
2446 ddi_eventcookie_t event, void *arg, void *impl_data), void *arg,
2447 ddi_callback_id_t *cb_id)
2449 if (rootnex_event_hdl == NULL)
2450 return (NDI_FAILURE);
2451 return (ndi_event_add_callback(rootnex_event_hdl, rdip,
2452 eventid, handler, arg, NDI_SLEEP, cb_id));
2455 /*ARGSUSED*/
2457 i_ddi_rootnex_remove_eventcall(dev_info_t *dip, ddi_callback_id_t cb_id)
2459 if (rootnex_event_hdl == NULL)
2460 return (NDI_FAILURE);
2462 return (ndi_event_remove_callback(rootnex_event_hdl, cb_id));
2465 /*ARGSUSED*/
2467 i_ddi_rootnex_post_event(dev_info_t *dip, dev_info_t *rdip,
2468 ddi_eventcookie_t eventid, void *impl_data)
2470 int tag;
2472 if (rootnex_event_hdl == NULL)
2473 return (NDI_FAILURE);
2475 tag = ndi_event_cookie_to_tag(rootnex_event_hdl, eventid);
2476 if (tag == ROOTNEX_FAULT_EVENT) {
2477 (*plat_fault_handler)(rdip, impl_data);
2478 (*plat_fault_logger)(rdip, impl_data);
2480 return (ndi_event_run_callbacks(rootnex_event_hdl, rdip,
2481 eventid, impl_data));
2485 * ndi_set_bus_private/ndi_get_bus_private:
2486 * Get/set device bus private data in devinfo.
2488 void
2489 ndi_set_bus_private(dev_info_t *dip, boolean_t up, uint32_t port_type,
2490 void *data)
2492 if (up) {
2493 DEVI(dip)->devi_bus.port_up.info.port.type = port_type;
2494 DEVI(dip)->devi_bus.port_up.priv_p = data;
2495 } else {
2496 DEVI(dip)->devi_bus.port_down.info.port.type = port_type;
2497 DEVI(dip)->devi_bus.port_down.priv_p = data;
2501 void *
2502 ndi_get_bus_private(dev_info_t *dip, boolean_t up)
2504 if (up)
2505 return (DEVI(dip)->devi_bus.port_up.priv_p);
2506 else
2507 return (DEVI(dip)->devi_bus.port_down.priv_p);
2510 boolean_t
2511 ndi_port_type(dev_info_t *dip, boolean_t up, uint32_t port_type)
2513 if (up) {
2514 return ((DEVI(dip)->devi_bus.port_up.info.port.type) ==
2515 port_type);
2516 } else {
2517 return ((DEVI(dip)->devi_bus.port_down.info.port.type) ==
2518 port_type);
2522 /* Interfaces for 'self' to set/get a child's flavor */
2523 void
2524 ndi_flavor_set(dev_info_t *child, ndi_flavor_t child_flavor)
2526 DEVI(child)->devi_flavor = child_flavor;
2529 ndi_flavor_t
2530 ndi_flavor_get(dev_info_t *child)
2532 return (DEVI(child)->devi_flavor);
2536 * Interfaces to maintain flavor-specific private data of flavored
2537 * children of self.
2539 * The flavor count always includes the default (0) vanilla flavor,
2540 * but storage for the vanilla flavor data pointer is in the same
2541 * place that ddi_[sg]et_driver_private uses, so the flavorv
2542 * storage is just for flavors 1..{nflavors-1}.
2544 void
2545 ndi_flavorv_alloc(dev_info_t *self, int nflavors)
2547 ASSERT(nflavors > 0 && (DEVI(self)->devi_flavorv == NULL ||
2548 nflavors == DEVI(self)->devi_flavorv_n));
2549 if (nflavors <= 1 || (DEVI(self)->devi_flavorv)) {
2550 return;
2552 DEVI(self)->devi_flavorv =
2553 kmem_zalloc((nflavors - 1) * sizeof (void *), KM_SLEEP);
2554 DEVI(self)->devi_flavorv_n = nflavors;
2557 void
2558 ndi_flavorv_set(dev_info_t *self, ndi_flavor_t child_flavor, void *v)
2560 if (child_flavor == NDI_FLAVOR_VANILLA) {
2561 ddi_set_driver_private(self, v);
2562 } else {
2563 ASSERT(child_flavor < DEVI(self)->devi_flavorv_n &&
2564 DEVI(self)->devi_flavorv != NULL);
2565 if (child_flavor > DEVI(self)->devi_flavorv_n ||
2566 DEVI(self)->devi_flavorv == NULL) {
2567 return;
2569 DEVI(self)->devi_flavorv[child_flavor - 1] = v;
2573 void *
2574 ndi_flavorv_get(dev_info_t *self, ndi_flavor_t child_flavor)
2576 if (child_flavor == NDI_FLAVOR_VANILLA) {
2577 return (ddi_get_driver_private(self));
2578 } else {
2579 ASSERT(child_flavor < DEVI(self)->devi_flavorv_n &&
2580 DEVI(self)->devi_flavorv != NULL);
2581 if (child_flavor > DEVI(self)->devi_flavorv_n ||
2582 DEVI(self)->devi_flavorv == NULL) {
2583 return (NULL);
2585 return (DEVI(self)->devi_flavorv[child_flavor - 1]);