xref: /illumos-gate/usr/src/uts/common/os/sunndi.c (revision 25e8c5aa)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
519174f18Svikram  * Common Development and Distribution License (the "License").
619174f18Svikram  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
2219174f18Svikram  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <sys/types.h>
297c478bd9Sstevel@tonic-gate #include <sys/param.h>
307c478bd9Sstevel@tonic-gate #include <sys/systm.h>
317c478bd9Sstevel@tonic-gate #include <sys/buf.h>
327c478bd9Sstevel@tonic-gate #include <sys/uio.h>
337c478bd9Sstevel@tonic-gate #include <sys/cred.h>
347c478bd9Sstevel@tonic-gate #include <sys/poll.h>
357c478bd9Sstevel@tonic-gate #include <sys/mman.h>
367c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
377c478bd9Sstevel@tonic-gate #include <sys/model.h>
387c478bd9Sstevel@tonic-gate #include <sys/file.h>
397c478bd9Sstevel@tonic-gate #include <sys/proc.h>
407c478bd9Sstevel@tonic-gate #include <sys/open.h>
417c478bd9Sstevel@tonic-gate #include <sys/user.h>
427c478bd9Sstevel@tonic-gate #include <sys/t_lock.h>
437c478bd9Sstevel@tonic-gate #include <sys/vm.h>
447c478bd9Sstevel@tonic-gate #include <sys/stat.h>
457c478bd9Sstevel@tonic-gate #include <vm/hat.h>
467c478bd9Sstevel@tonic-gate #include <vm/seg.h>
477c478bd9Sstevel@tonic-gate #include <vm/as.h>
487c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
497c478bd9Sstevel@tonic-gate #include <sys/debug.h>
507c478bd9Sstevel@tonic-gate #include <sys/avintr.h>
517c478bd9Sstevel@tonic-gate #include <sys/autoconf.h>
527c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
537c478bd9Sstevel@tonic-gate #include <sys/esunddi.h>
547c478bd9Sstevel@tonic-gate #include <sys/sunndi.h>
557c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
567c478bd9Sstevel@tonic-gate #include <sys/kstat.h>
577c478bd9Sstevel@tonic-gate #include <sys/conf.h>
587c478bd9Sstevel@tonic-gate #include <sys/ddi_impldefs.h>	/* include implementation structure defs */
597c478bd9Sstevel@tonic-gate #include <sys/ndi_impldefs.h>
607c478bd9Sstevel@tonic-gate #include <sys/hwconf.h>
617c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
627c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
637c478bd9Sstevel@tonic-gate #include <sys/epm.h>
647c478bd9Sstevel@tonic-gate #include <sys/devctl.h>
657c478bd9Sstevel@tonic-gate #include <sys/callb.h>
667c478bd9Sstevel@tonic-gate #include <sys/bootconf.h>
677c478bd9Sstevel@tonic-gate #include <sys/dacf_impl.h>
687c478bd9Sstevel@tonic-gate #include <sys/nvpair.h>
697c478bd9Sstevel@tonic-gate #include <sys/sunmdi.h>
707c478bd9Sstevel@tonic-gate #include <sys/fs/dv_node.h>
71*25e8c5aaSvikram #include <sys/sunldi_impl.h>
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate #ifdef __sparc
747c478bd9Sstevel@tonic-gate #include <sys/archsystm.h>	/* getpil/setpil */
757c478bd9Sstevel@tonic-gate #include <sys/membar.h>		/* membar_sync */
767c478bd9Sstevel@tonic-gate #endif
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate /*
797c478bd9Sstevel@tonic-gate  * ndi property handling
807c478bd9Sstevel@tonic-gate  */
817c478bd9Sstevel@tonic-gate int
827c478bd9Sstevel@tonic-gate ndi_prop_update_int(dev_t match_dev, dev_info_t *dip,
837c478bd9Sstevel@tonic-gate     char *name, int data)
847c478bd9Sstevel@tonic-gate {
857c478bd9Sstevel@tonic-gate 	return (ddi_prop_update_common(match_dev, dip,
867c478bd9Sstevel@tonic-gate 	    DDI_PROP_HW_DEF | DDI_PROP_TYPE_INT | DDI_PROP_DONTSLEEP,
877c478bd9Sstevel@tonic-gate 	    name, &data, 1, ddi_prop_fm_encode_ints));
887c478bd9Sstevel@tonic-gate }
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate int
917c478bd9Sstevel@tonic-gate ndi_prop_update_int64(dev_t match_dev, dev_info_t *dip,
927c478bd9Sstevel@tonic-gate     char *name, int64_t data)
937c478bd9Sstevel@tonic-gate {
947c478bd9Sstevel@tonic-gate 	return (ddi_prop_update_common(match_dev, dip,
957c478bd9Sstevel@tonic-gate 	    DDI_PROP_HW_DEF | DDI_PROP_TYPE_INT64 | DDI_PROP_DONTSLEEP,
967c478bd9Sstevel@tonic-gate 	    name, &data, 1, ddi_prop_fm_encode_int64));
977c478bd9Sstevel@tonic-gate }
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate int
1007c478bd9Sstevel@tonic-gate ndi_prop_create_boolean(dev_t match_dev, dev_info_t *dip,
1017c478bd9Sstevel@tonic-gate     char *name)
1027c478bd9Sstevel@tonic-gate {
1037c478bd9Sstevel@tonic-gate 	return (ddi_prop_update_common(match_dev, dip,
1047c478bd9Sstevel@tonic-gate 	    DDI_PROP_HW_DEF | DDI_PROP_TYPE_ANY | DDI_PROP_DONTSLEEP,
1057c478bd9Sstevel@tonic-gate 	    name, NULL, 0, ddi_prop_fm_encode_bytes));
1067c478bd9Sstevel@tonic-gate }
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate int
1097c478bd9Sstevel@tonic-gate ndi_prop_update_int_array(dev_t match_dev, dev_info_t *dip,
1107c478bd9Sstevel@tonic-gate     char *name, int *data, uint_t nelements)
1117c478bd9Sstevel@tonic-gate {
1127c478bd9Sstevel@tonic-gate 	return (ddi_prop_update_common(match_dev, dip,
1137c478bd9Sstevel@tonic-gate 	    DDI_PROP_HW_DEF | DDI_PROP_TYPE_INT | DDI_PROP_DONTSLEEP,
1147c478bd9Sstevel@tonic-gate 	    name, data, nelements, ddi_prop_fm_encode_ints));
1157c478bd9Sstevel@tonic-gate }
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate int
1187c478bd9Sstevel@tonic-gate ndi_prop_update_int64_array(dev_t match_dev, dev_info_t *dip,
1197c478bd9Sstevel@tonic-gate     char *name, int64_t *data, uint_t nelements)
1207c478bd9Sstevel@tonic-gate {
1217c478bd9Sstevel@tonic-gate 	return (ddi_prop_update_common(match_dev, dip,
1227c478bd9Sstevel@tonic-gate 	    DDI_PROP_HW_DEF | DDI_PROP_TYPE_INT64 | DDI_PROP_DONTSLEEP,
1237c478bd9Sstevel@tonic-gate 	    name, data, nelements, ddi_prop_fm_encode_int64));
1247c478bd9Sstevel@tonic-gate }
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate int
1277c478bd9Sstevel@tonic-gate ndi_prop_update_string(dev_t match_dev, dev_info_t *dip,
1287c478bd9Sstevel@tonic-gate     char *name, char *data)
1297c478bd9Sstevel@tonic-gate {
1307c478bd9Sstevel@tonic-gate 	return (ddi_prop_update_common(match_dev, dip,
1317c478bd9Sstevel@tonic-gate 	    DDI_PROP_HW_DEF | DDI_PROP_TYPE_STRING | DDI_PROP_DONTSLEEP,
1327c478bd9Sstevel@tonic-gate 	    name, &data, 1, ddi_prop_fm_encode_string));
1337c478bd9Sstevel@tonic-gate }
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate int
1367c478bd9Sstevel@tonic-gate ndi_prop_update_string_array(dev_t match_dev, dev_info_t *dip,
1377c478bd9Sstevel@tonic-gate     char *name, char **data, uint_t nelements)
1387c478bd9Sstevel@tonic-gate {
1397c478bd9Sstevel@tonic-gate 	return (ddi_prop_update_common(match_dev, dip,
1407c478bd9Sstevel@tonic-gate 	    DDI_PROP_HW_DEF | DDI_PROP_TYPE_STRING | DDI_PROP_DONTSLEEP,
1417c478bd9Sstevel@tonic-gate 	    name, data, nelements,
1427c478bd9Sstevel@tonic-gate 	    ddi_prop_fm_encode_strings));
1437c478bd9Sstevel@tonic-gate }
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate int
1467c478bd9Sstevel@tonic-gate ndi_prop_update_byte_array(dev_t match_dev, dev_info_t *dip,
1477c478bd9Sstevel@tonic-gate     char *name, uchar_t *data, uint_t nelements)
1487c478bd9Sstevel@tonic-gate {
1497c478bd9Sstevel@tonic-gate 	if (nelements == 0)
1507c478bd9Sstevel@tonic-gate 		return (DDI_PROP_INVAL_ARG);
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	return (ddi_prop_update_common(match_dev, dip,
1537c478bd9Sstevel@tonic-gate 	    DDI_PROP_HW_DEF | DDI_PROP_TYPE_BYTE | DDI_PROP_DONTSLEEP,
1547c478bd9Sstevel@tonic-gate 	    name, data, nelements, ddi_prop_fm_encode_bytes));
1557c478bd9Sstevel@tonic-gate }
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate int
1587c478bd9Sstevel@tonic-gate ndi_prop_remove(dev_t dev, dev_info_t *dip, char *name)
1597c478bd9Sstevel@tonic-gate {
1607c478bd9Sstevel@tonic-gate 	return (ddi_prop_remove_common(dev, dip, name, DDI_PROP_HW_DEF));
1617c478bd9Sstevel@tonic-gate }
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate void
1647c478bd9Sstevel@tonic-gate ndi_prop_remove_all(dev_info_t *dip)
1657c478bd9Sstevel@tonic-gate {
1667c478bd9Sstevel@tonic-gate 	ddi_prop_remove_all_common(dip, (int)DDI_PROP_HW_DEF);
1677c478bd9Sstevel@tonic-gate }
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate /*
1707c478bd9Sstevel@tonic-gate  * Post an event notification to nexus driver responsible for handling
1717c478bd9Sstevel@tonic-gate  * the event.  The responsible nexus is defined in the cookie passed in as
1727c478bd9Sstevel@tonic-gate  * the third parameter.
1737c478bd9Sstevel@tonic-gate  * The dip parameter is an artifact of an older implementation in which all
1747c478bd9Sstevel@tonic-gate  * requests to remove an eventcall would bubble up the tree.  Today, this
1757c478bd9Sstevel@tonic-gate  * parameter is ignored.
1767c478bd9Sstevel@tonic-gate  * Input Parameters:
1777c478bd9Sstevel@tonic-gate  *	dip 	- Ignored.
1787c478bd9Sstevel@tonic-gate  *	rdip 	- device driver posting the event
1797c478bd9Sstevel@tonic-gate  *	cookie	- valid ddi_eventcookie_t, obtained by caller prior to
1807c478bd9Sstevel@tonic-gate  *		  invocation of this routine
1817c478bd9Sstevel@tonic-gate  *	impl_data - used by framework
1827c478bd9Sstevel@tonic-gate  */
1837c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1847c478bd9Sstevel@tonic-gate int
1857c478bd9Sstevel@tonic-gate ndi_post_event(dev_info_t *dip, dev_info_t *rdip,
1867c478bd9Sstevel@tonic-gate 		ddi_eventcookie_t cookie, void *impl_data)
1877c478bd9Sstevel@tonic-gate {
1887c478bd9Sstevel@tonic-gate 	dev_info_t *ddip;
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 	ASSERT(cookie);
1917c478bd9Sstevel@tonic-gate 	ddip = NDI_EVENT_DDIP(cookie);
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 	/*
1947c478bd9Sstevel@tonic-gate 	 * perform sanity checks.  These conditions should never be true.
1957c478bd9Sstevel@tonic-gate 	 */
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	ASSERT(DEVI(ddip)->devi_ops->devo_bus_ops != NULL);
1987c478bd9Sstevel@tonic-gate 	ASSERT(DEVI(ddip)->devi_ops->devo_bus_ops->busops_rev >= BUSO_REV_6);
1997c478bd9Sstevel@tonic-gate 	ASSERT(DEVI(ddip)->devi_ops->devo_bus_ops->bus_post_event != NULL);
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	/*
2027c478bd9Sstevel@tonic-gate 	 * post the event to the responsible ancestor
2037c478bd9Sstevel@tonic-gate 	 */
2047c478bd9Sstevel@tonic-gate 	return ((*(DEVI(ddip)->devi_ops->devo_bus_ops->bus_post_event))
2057c478bd9Sstevel@tonic-gate 		(ddip, rdip, cookie, impl_data));
2067c478bd9Sstevel@tonic-gate }
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate /*
2097c478bd9Sstevel@tonic-gate  * Calls the bus nexus driver's implementation of the
2107c478bd9Sstevel@tonic-gate  * (*bus_remove_eventcall)() interface.
2117c478bd9Sstevel@tonic-gate  */
2127c478bd9Sstevel@tonic-gate int
2137c478bd9Sstevel@tonic-gate ndi_busop_remove_eventcall(dev_info_t *ddip, ddi_callback_id_t id)
2147c478bd9Sstevel@tonic-gate {
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 	ASSERT(id);
2177c478bd9Sstevel@tonic-gate 	/* check for a correct revno before calling up the device tree. */
2187c478bd9Sstevel@tonic-gate 	ASSERT(DEVI(ddip)->devi_ops->devo_bus_ops != NULL);
2197c478bd9Sstevel@tonic-gate 	ASSERT(DEVI(ddip)->devi_ops->devo_bus_ops->busops_rev >= BUSO_REV_6);
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 	if (DEVI(ddip)->devi_ops->devo_bus_ops->bus_remove_eventcall == NULL)
2227c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	/*
2257c478bd9Sstevel@tonic-gate 	 * request responsible nexus to remove the eventcall
2267c478bd9Sstevel@tonic-gate 	 */
2277c478bd9Sstevel@tonic-gate 	return ((*(DEVI(ddip)->devi_ops->devo_bus_ops->bus_remove_eventcall))
2287c478bd9Sstevel@tonic-gate 		(ddip, id));
2297c478bd9Sstevel@tonic-gate }
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate /*
2327c478bd9Sstevel@tonic-gate  * Calls the bus nexus driver's implementation of the
2337c478bd9Sstevel@tonic-gate  * (*bus_add_eventcall)() interface.  The dip parameter is an
2347c478bd9Sstevel@tonic-gate  * artifact of an older implementation in which all requests to
2357c478bd9Sstevel@tonic-gate  * add an eventcall would bubble up the tree.  Today, this parameter is
2367c478bd9Sstevel@tonic-gate  * ignored.
2377c478bd9Sstevel@tonic-gate  */
2387c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2397c478bd9Sstevel@tonic-gate int
2407c478bd9Sstevel@tonic-gate ndi_busop_add_eventcall(dev_info_t *dip, dev_info_t *rdip,
2417c478bd9Sstevel@tonic-gate 		ddi_eventcookie_t cookie, void (*callback)(), void *arg,
2427c478bd9Sstevel@tonic-gate 		ddi_callback_id_t *cb_id)
2437c478bd9Sstevel@tonic-gate {
2447c478bd9Sstevel@tonic-gate 	dev_info_t *ddip = (dev_info_t *)NDI_EVENT_DDIP(cookie);
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 	/*
2477c478bd9Sstevel@tonic-gate 	 * check for a correct revno before calling up the device tree.
2487c478bd9Sstevel@tonic-gate 	 */
2497c478bd9Sstevel@tonic-gate 	ASSERT(DEVI(ddip)->devi_ops->devo_bus_ops != NULL);
2507c478bd9Sstevel@tonic-gate 	ASSERT(DEVI(ddip)->devi_ops->devo_bus_ops->busops_rev >= BUSO_REV_6);
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 	if (DEVI(ddip)->devi_ops->devo_bus_ops->bus_add_eventcall == NULL)
2537c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 	/*
2567c478bd9Sstevel@tonic-gate 	 * request responsible ancestor to add the eventcall
2577c478bd9Sstevel@tonic-gate 	 */
2587c478bd9Sstevel@tonic-gate 	return ((*(DEVI(ddip)->devi_ops->devo_bus_ops->bus_add_eventcall))
2597c478bd9Sstevel@tonic-gate 		(ddip, rdip, cookie, callback, arg, cb_id));
2607c478bd9Sstevel@tonic-gate }
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate /*
2637c478bd9Sstevel@tonic-gate  * Calls the bus nexus driver's implementation of the
2647c478bd9Sstevel@tonic-gate  * (*bus_get_eventcookie)() interface up the device tree hierarchy.
2657c478bd9Sstevel@tonic-gate  */
2667c478bd9Sstevel@tonic-gate int
2677c478bd9Sstevel@tonic-gate ndi_busop_get_eventcookie(dev_info_t *dip, dev_info_t *rdip, char *name,
2687c478bd9Sstevel@tonic-gate 		ddi_eventcookie_t *event_cookiep)
2697c478bd9Sstevel@tonic-gate {
2707c478bd9Sstevel@tonic-gate 	dev_info_t *pdip = (dev_info_t *)DEVI(dip)->devi_parent;
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 	/* Can not be called from rootnex. */
2737c478bd9Sstevel@tonic-gate 	ASSERT(pdip);
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 	/*
2767c478bd9Sstevel@tonic-gate 	 * check for a correct revno before calling up the device tree.
2777c478bd9Sstevel@tonic-gate 	 */
2787c478bd9Sstevel@tonic-gate 	ASSERT(DEVI(pdip)->devi_ops->devo_bus_ops != NULL);
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 	if ((DEVI(pdip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_6) ||
2817c478bd9Sstevel@tonic-gate 	    (DEVI(pdip)->devi_ops->devo_bus_ops->bus_get_eventcookie == NULL)) {
2827c478bd9Sstevel@tonic-gate #ifdef DEBUG
2837c478bd9Sstevel@tonic-gate 		if ((DEVI(pdip)->devi_ops->devo_bus_ops->busops_rev >=
2847c478bd9Sstevel@tonic-gate 		    BUSO_REV_3) &&
2857c478bd9Sstevel@tonic-gate 		    (DEVI(pdip)->devi_ops->devo_bus_ops->bus_get_eventcookie)) {
2867c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN,
2877c478bd9Sstevel@tonic-gate 			    "Warning: %s%d busops_rev=%d no longer supported"
2887c478bd9Sstevel@tonic-gate 			    " by the NDI event framework.\nBUSO_REV_6 or "
2897c478bd9Sstevel@tonic-gate 			    "greater must be used.",
2907c478bd9Sstevel@tonic-gate 			    DEVI(pdip)->devi_binding_name,
2917c478bd9Sstevel@tonic-gate 			    DEVI(pdip)->devi_instance,
2927c478bd9Sstevel@tonic-gate 			    DEVI(pdip)->devi_ops->devo_bus_ops->busops_rev);
2937c478bd9Sstevel@tonic-gate 		}
2947c478bd9Sstevel@tonic-gate #endif /* DEBUG */
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate 		return (ndi_busop_get_eventcookie(pdip, rdip, name,
2977c478bd9Sstevel@tonic-gate 			    event_cookiep));
2987c478bd9Sstevel@tonic-gate 	}
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 	return ((*(DEVI(pdip)->devi_ops->devo_bus_ops->bus_get_eventcookie))
3017c478bd9Sstevel@tonic-gate 		(pdip, rdip, name, event_cookiep));
3027c478bd9Sstevel@tonic-gate }
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate /*
3057c478bd9Sstevel@tonic-gate  * Copy in the devctl IOCTL data and return a handle to
3067c478bd9Sstevel@tonic-gate  * the data.
3077c478bd9Sstevel@tonic-gate  */
3087c478bd9Sstevel@tonic-gate int
3097c478bd9Sstevel@tonic-gate ndi_dc_allochdl(void *iocarg, struct devctl_iocdata **rdcp)
3107c478bd9Sstevel@tonic-gate {
3117c478bd9Sstevel@tonic-gate 	struct devctl_iocdata *dcp;
3127c478bd9Sstevel@tonic-gate 	char *cpybuf;
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 	ASSERT(rdcp != NULL);
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 	dcp = kmem_zalloc(sizeof (*dcp), KM_SLEEP);
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 	if (get_udatamodel() == DATAMODEL_NATIVE) {
3197c478bd9Sstevel@tonic-gate 		if (copyin(iocarg, dcp, sizeof (*dcp)) != 0) {
3207c478bd9Sstevel@tonic-gate 			kmem_free(dcp, sizeof (*dcp));
3217c478bd9Sstevel@tonic-gate 			return (NDI_FAULT);
3227c478bd9Sstevel@tonic-gate 		}
3237c478bd9Sstevel@tonic-gate 	}
3247c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
3257c478bd9Sstevel@tonic-gate 	else {
3267c478bd9Sstevel@tonic-gate 		struct devctl_iocdata32 dcp32;
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 		if (copyin(iocarg, &dcp32, sizeof (dcp32)) != 0) {
3297c478bd9Sstevel@tonic-gate 			kmem_free(dcp, sizeof (*dcp));
3307c478bd9Sstevel@tonic-gate 			return (NDI_FAULT);
3317c478bd9Sstevel@tonic-gate 		}
3327c478bd9Sstevel@tonic-gate 		dcp->cmd = (uint_t)dcp32.cmd;
3337c478bd9Sstevel@tonic-gate 		dcp->flags = (uint_t)dcp32.flags;
3347c478bd9Sstevel@tonic-gate 		dcp->cpyout_buf = (uint_t *)(uintptr_t)dcp32.cpyout_buf;
3357c478bd9Sstevel@tonic-gate 		dcp->nvl_user = (nvlist_t *)(uintptr_t)dcp32.nvl_user;
3367c478bd9Sstevel@tonic-gate 		dcp->nvl_usersz = (size_t)dcp32.nvl_usersz;
3377c478bd9Sstevel@tonic-gate 		dcp->c_nodename = (char *)(uintptr_t)dcp32.c_nodename;
3387c478bd9Sstevel@tonic-gate 		dcp->c_unitaddr = (char *)(uintptr_t)dcp32.c_unitaddr;
3397c478bd9Sstevel@tonic-gate 	}
3407c478bd9Sstevel@tonic-gate #endif
3417c478bd9Sstevel@tonic-gate 	if (dcp->c_nodename != NULL) {
3427c478bd9Sstevel@tonic-gate 		cpybuf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
3437c478bd9Sstevel@tonic-gate 		if (copyinstr(dcp->c_nodename, cpybuf, MAXNAMELEN, 0) != 0) {
3447c478bd9Sstevel@tonic-gate 			kmem_free(cpybuf, MAXNAMELEN);
3457c478bd9Sstevel@tonic-gate 			kmem_free(dcp, sizeof (*dcp));
3467c478bd9Sstevel@tonic-gate 			return (NDI_FAULT);
3477c478bd9Sstevel@tonic-gate 		}
3487c478bd9Sstevel@tonic-gate 		cpybuf[MAXNAMELEN - 1] = '\0';
3497c478bd9Sstevel@tonic-gate 		dcp->c_nodename = cpybuf;
3507c478bd9Sstevel@tonic-gate 	}
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 	if (dcp->c_unitaddr != NULL) {
3537c478bd9Sstevel@tonic-gate 		cpybuf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
3547c478bd9Sstevel@tonic-gate 		if (copyinstr(dcp->c_unitaddr, cpybuf, MAXNAMELEN, 0) != 0) {
3557c478bd9Sstevel@tonic-gate 			kmem_free(cpybuf, MAXNAMELEN);
3567c478bd9Sstevel@tonic-gate 			if (dcp->c_nodename != NULL)
3577c478bd9Sstevel@tonic-gate 				kmem_free(dcp->c_nodename, MAXNAMELEN);
3587c478bd9Sstevel@tonic-gate 			kmem_free(dcp, sizeof (*dcp));
3597c478bd9Sstevel@tonic-gate 			return (NDI_FAULT);
3607c478bd9Sstevel@tonic-gate 		}
3617c478bd9Sstevel@tonic-gate 		cpybuf[MAXNAMELEN - 1] = '\0';
3627c478bd9Sstevel@tonic-gate 		dcp->c_unitaddr = cpybuf;
3637c478bd9Sstevel@tonic-gate 	}
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 	/*
3667c478bd9Sstevel@tonic-gate 	 * copyin and unpack a user defined nvlist if one was passed
3677c478bd9Sstevel@tonic-gate 	 */
3687c478bd9Sstevel@tonic-gate 	if (dcp->nvl_user != NULL) {
36992d20396Scth 		if (dcp->nvl_usersz == 0) {
37092d20396Scth 			if (dcp->c_nodename != NULL)
37192d20396Scth 				kmem_free(dcp->c_nodename, MAXNAMELEN);
37292d20396Scth 			if (dcp->c_unitaddr != NULL)
37392d20396Scth 				kmem_free(dcp->c_unitaddr, MAXNAMELEN);
37492d20396Scth 			kmem_free(dcp, sizeof (*dcp));
37592d20396Scth 			return (NDI_FAILURE);
37692d20396Scth 		}
3777c478bd9Sstevel@tonic-gate 		cpybuf = kmem_alloc(dcp->nvl_usersz, KM_SLEEP);
3787c478bd9Sstevel@tonic-gate 		if (copyin(dcp->nvl_user, cpybuf, dcp->nvl_usersz) != 0) {
3797c478bd9Sstevel@tonic-gate 			kmem_free(cpybuf, dcp->nvl_usersz);
3807c478bd9Sstevel@tonic-gate 			if (dcp->c_nodename != NULL)
3817c478bd9Sstevel@tonic-gate 				kmem_free(dcp->c_nodename, MAXNAMELEN);
3827c478bd9Sstevel@tonic-gate 			if (dcp->c_unitaddr != NULL)
3837c478bd9Sstevel@tonic-gate 				kmem_free(dcp->c_unitaddr, MAXNAMELEN);
3847c478bd9Sstevel@tonic-gate 			kmem_free(dcp, sizeof (*dcp));
3857c478bd9Sstevel@tonic-gate 			return (NDI_FAULT);
3867c478bd9Sstevel@tonic-gate 		}
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate 		if (nvlist_unpack(cpybuf, dcp->nvl_usersz, &dcp->nvl_user,
3897c478bd9Sstevel@tonic-gate 		    KM_SLEEP)) {
3907c478bd9Sstevel@tonic-gate 			kmem_free(cpybuf, dcp->nvl_usersz);
3917c478bd9Sstevel@tonic-gate 			if (dcp->c_nodename != NULL)
3927c478bd9Sstevel@tonic-gate 				kmem_free(dcp->c_nodename, MAXNAMELEN);
3937c478bd9Sstevel@tonic-gate 			if (dcp->c_unitaddr != NULL)
3947c478bd9Sstevel@tonic-gate 				kmem_free(dcp->c_unitaddr, MAXNAMELEN);
3957c478bd9Sstevel@tonic-gate 			kmem_free(dcp, sizeof (*dcp));
3967c478bd9Sstevel@tonic-gate 			return (NDI_FAULT);
3977c478bd9Sstevel@tonic-gate 		}
3987c478bd9Sstevel@tonic-gate 		/*
3997c478bd9Sstevel@tonic-gate 		 * free the buffer containing the packed nvlist
4007c478bd9Sstevel@tonic-gate 		 */
4017c478bd9Sstevel@tonic-gate 		kmem_free(cpybuf, dcp->nvl_usersz);
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 	}
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 	*rdcp = dcp;
4067c478bd9Sstevel@tonic-gate 	return (NDI_SUCCESS);
4077c478bd9Sstevel@tonic-gate }
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate /*
4107c478bd9Sstevel@tonic-gate  * free all space allocated to a handle.
4117c478bd9Sstevel@tonic-gate  */
4127c478bd9Sstevel@tonic-gate void
4137c478bd9Sstevel@tonic-gate ndi_dc_freehdl(struct devctl_iocdata *dcp)
4147c478bd9Sstevel@tonic-gate {
4157c478bd9Sstevel@tonic-gate 	ASSERT(dcp != NULL);
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 	if (dcp->c_nodename != NULL)
4187c478bd9Sstevel@tonic-gate 		kmem_free(dcp->c_nodename, MAXNAMELEN);
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate 	if (dcp->c_unitaddr != NULL)
4217c478bd9Sstevel@tonic-gate 		kmem_free(dcp->c_unitaddr, MAXNAMELEN);
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate 	if (dcp->nvl_user != NULL)
4247c478bd9Sstevel@tonic-gate 		nvlist_free(dcp->nvl_user);
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate 	kmem_free(dcp, sizeof (*dcp));
4277c478bd9Sstevel@tonic-gate }
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate char *
4307c478bd9Sstevel@tonic-gate ndi_dc_getname(struct devctl_iocdata *dcp)
4317c478bd9Sstevel@tonic-gate {
4327c478bd9Sstevel@tonic-gate 	ASSERT(dcp != NULL);
4337c478bd9Sstevel@tonic-gate 	return (dcp->c_nodename);
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate }
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate char *
4387c478bd9Sstevel@tonic-gate ndi_dc_getaddr(struct devctl_iocdata *dcp)
4397c478bd9Sstevel@tonic-gate {
4407c478bd9Sstevel@tonic-gate 	ASSERT(dcp != NULL);
4417c478bd9Sstevel@tonic-gate 	return (dcp->c_unitaddr);
4427c478bd9Sstevel@tonic-gate }
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate nvlist_t *
4457c478bd9Sstevel@tonic-gate ndi_dc_get_ap_data(struct devctl_iocdata *dcp)
4467c478bd9Sstevel@tonic-gate {
4477c478bd9Sstevel@tonic-gate 	ASSERT(dcp != NULL);
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate 	return (dcp->nvl_user);
4507c478bd9Sstevel@tonic-gate }
4517c478bd9Sstevel@tonic-gate 
4527c478bd9Sstevel@tonic-gate /*
4537c478bd9Sstevel@tonic-gate  * Transition the child named by "devname@devaddr" to the online state.
4547c478bd9Sstevel@tonic-gate  * For use by a driver's DEVCTL_DEVICE_ONLINE handler.
4557c478bd9Sstevel@tonic-gate  */
4567c478bd9Sstevel@tonic-gate int
4577c478bd9Sstevel@tonic-gate ndi_devctl_device_online(dev_info_t *dip, struct devctl_iocdata *dcp,
4587c478bd9Sstevel@tonic-gate 	uint_t flags)
4597c478bd9Sstevel@tonic-gate {
4607c478bd9Sstevel@tonic-gate 	int	rval;
4617c478bd9Sstevel@tonic-gate 	char	*name;
4627c478bd9Sstevel@tonic-gate 	dev_info_t *rdip;
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 	if (ndi_dc_getname(dcp) == NULL || ndi_dc_getaddr(dcp) == NULL)
4657c478bd9Sstevel@tonic-gate 		return (EINVAL);
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate 	name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
4687c478bd9Sstevel@tonic-gate 	(void) snprintf(name, MAXNAMELEN, "%s@%s",
4697c478bd9Sstevel@tonic-gate 		ndi_dc_getname(dcp), ndi_dc_getaddr(dcp));
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate 	if ((rval = ndi_devi_config_one(dip, name, &rdip,
4727c478bd9Sstevel@tonic-gate 	    flags | NDI_DEVI_ONLINE | NDI_CONFIG)) == NDI_SUCCESS) {
4737c478bd9Sstevel@tonic-gate 		ndi_rele_devi(rdip);
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate 		/*
4767c478bd9Sstevel@tonic-gate 		 * Invalidate devfs cached directory contents. For the checks
4777c478bd9Sstevel@tonic-gate 		 * in the "if" condition see the comment in ndi_devi_online().
4787c478bd9Sstevel@tonic-gate 		 */
479737d277aScth 		if (i_ddi_devi_attached(dip) && !DEVI_BUSY_OWNED(dip))
4807c478bd9Sstevel@tonic-gate 			(void) devfs_clean(dip, NULL, 0);
4817c478bd9Sstevel@tonic-gate 
4827c478bd9Sstevel@tonic-gate 	} else if (rval == NDI_BUSY) {
4837c478bd9Sstevel@tonic-gate 		rval = EBUSY;
4847c478bd9Sstevel@tonic-gate 	} else if (rval == NDI_FAILURE) {
4857c478bd9Sstevel@tonic-gate 		rval = EIO;
4867c478bd9Sstevel@tonic-gate 	}
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate 	NDI_DEBUG(flags, (CE_CONT, "%s%d: online: %s: %s\n",
4897c478bd9Sstevel@tonic-gate 		ddi_driver_name(dip), ddi_get_instance(dip), name,
4907c478bd9Sstevel@tonic-gate 		((rval == NDI_SUCCESS) ? "ok" : "failed")));
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate 	kmem_free(name, MAXNAMELEN);
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate 	return (rval);
4957c478bd9Sstevel@tonic-gate }
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate /*
4987c478bd9Sstevel@tonic-gate  * Transition the child named by "devname@devaddr" to the offline state.
4997c478bd9Sstevel@tonic-gate  * For use by a driver's DEVCTL_DEVICE_OFFLINE handler.
5007c478bd9Sstevel@tonic-gate  */
5017c478bd9Sstevel@tonic-gate int
5027c478bd9Sstevel@tonic-gate ndi_devctl_device_offline(dev_info_t *dip, struct devctl_iocdata *dcp,
5037c478bd9Sstevel@tonic-gate 	uint_t flags)
5047c478bd9Sstevel@tonic-gate {
5057c478bd9Sstevel@tonic-gate 	int	rval;
5067c478bd9Sstevel@tonic-gate 	char	*name;
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate 	if (ndi_dc_getname(dcp) == NULL || ndi_dc_getaddr(dcp) == NULL)
5097c478bd9Sstevel@tonic-gate 		return (EINVAL);
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate 	name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
5127c478bd9Sstevel@tonic-gate 	(void) snprintf(name, MAXNAMELEN, "%s@%s",
5137c478bd9Sstevel@tonic-gate 		ndi_dc_getname(dcp), ndi_dc_getaddr(dcp));
5147c478bd9Sstevel@tonic-gate 
51519174f18Svikram 	(void) devfs_clean(dip, name, DV_CLEAN_FORCE);
51619174f18Svikram 	rval = ndi_devi_unconfig_one(dip, name, NULL,
51719174f18Svikram 	    flags | NDI_DEVI_OFFLINE);
51819174f18Svikram 
51919174f18Svikram 	if (rval == NDI_BUSY) {
5207c478bd9Sstevel@tonic-gate 		rval = EBUSY;
52119174f18Svikram 	} else if (rval == NDI_FAILURE) {
52219174f18Svikram 		rval = EIO;
5237c478bd9Sstevel@tonic-gate 	}
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 	NDI_DEBUG(flags, (CE_CONT, "%s%d: offline: %s: %s\n",
5267c478bd9Sstevel@tonic-gate 		ddi_driver_name(dip), ddi_get_instance(dip), name,
5277c478bd9Sstevel@tonic-gate 		(rval == NDI_SUCCESS) ? "ok" : "failed"));
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate 	kmem_free(name, MAXNAMELEN);
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate 	return (rval);
5327c478bd9Sstevel@tonic-gate }
5337c478bd9Sstevel@tonic-gate 
5347c478bd9Sstevel@tonic-gate /*
5357c478bd9Sstevel@tonic-gate  * Remove the child named by "devname@devaddr".
5367c478bd9Sstevel@tonic-gate  * For use by a driver's DEVCTL_DEVICE_REMOVE handler.
5377c478bd9Sstevel@tonic-gate  */
5387c478bd9Sstevel@tonic-gate int
5397c478bd9Sstevel@tonic-gate ndi_devctl_device_remove(dev_info_t *dip, struct devctl_iocdata *dcp,
5407c478bd9Sstevel@tonic-gate 	uint_t flags)
5417c478bd9Sstevel@tonic-gate {
5427c478bd9Sstevel@tonic-gate 	int	rval;
5437c478bd9Sstevel@tonic-gate 	char	*name;
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate 	if (ndi_dc_getname(dcp) == NULL || ndi_dc_getaddr(dcp) == NULL)
5467c478bd9Sstevel@tonic-gate 		return (EINVAL);
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate 	name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
5497c478bd9Sstevel@tonic-gate 	(void) snprintf(name, MAXNAMELEN, "%s@%s",
5507c478bd9Sstevel@tonic-gate 		ndi_dc_getname(dcp), ndi_dc_getaddr(dcp));
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate 	(void) devfs_clean(dip, name, DV_CLEAN_FORCE);
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 	rval = ndi_devi_unconfig_one(dip, name, NULL, flags | NDI_DEVI_REMOVE);
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate 	if (rval == NDI_BUSY) {
5577c478bd9Sstevel@tonic-gate 		rval = EBUSY;
5587c478bd9Sstevel@tonic-gate 	} else if (rval == NDI_FAILURE) {
5597c478bd9Sstevel@tonic-gate 		rval = EIO;
5607c478bd9Sstevel@tonic-gate 	}
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate 	NDI_DEBUG(flags, (CE_CONT, "%s%d: remove: %s: %s\n",
5637c478bd9Sstevel@tonic-gate 		ddi_driver_name(dip), ddi_get_instance(dip), name,
5647c478bd9Sstevel@tonic-gate 		(rval == NDI_SUCCESS) ? "ok" : "failed"));
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate 	kmem_free(name, MAXNAMELEN);
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate 	return (rval);
5697c478bd9Sstevel@tonic-gate }
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate /*
5727c478bd9Sstevel@tonic-gate  * Return devctl state of the child named by "name@addr".
5737c478bd9Sstevel@tonic-gate  * For use by a driver's DEVCTL_DEVICE_GETSTATE handler.
5747c478bd9Sstevel@tonic-gate  */
5757c478bd9Sstevel@tonic-gate int
5767c478bd9Sstevel@tonic-gate ndi_devctl_device_getstate(dev_info_t *parent, struct devctl_iocdata *dcp,
5777c478bd9Sstevel@tonic-gate 	uint_t *state)
5787c478bd9Sstevel@tonic-gate {
5797c478bd9Sstevel@tonic-gate 	dev_info_t *dip;
5807c478bd9Sstevel@tonic-gate 	char *name, *addr;
5817c478bd9Sstevel@tonic-gate 	char *devname;
5827c478bd9Sstevel@tonic-gate 	int devnamelen;
5837c478bd9Sstevel@tonic-gate 	int circ;
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate 	if (parent == NULL ||
5867c478bd9Sstevel@tonic-gate 	    ((name = ndi_dc_getname(dcp)) == NULL) ||
5877c478bd9Sstevel@tonic-gate 	    ((addr = ndi_dc_getaddr(dcp)) == NULL))
5887c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate 	devnamelen = strlen(name) + strlen(addr) + 2;
5917c478bd9Sstevel@tonic-gate 	devname = kmem_alloc(devnamelen, KM_SLEEP);
5927c478bd9Sstevel@tonic-gate 	if (strlen(addr) > 0) {
5937c478bd9Sstevel@tonic-gate 		(void) snprintf(devname, devnamelen, "%s@%s", name, addr);
5947c478bd9Sstevel@tonic-gate 	} else {
5957c478bd9Sstevel@tonic-gate 		(void) snprintf(devname, devnamelen, "%s", name);
5967c478bd9Sstevel@tonic-gate 	}
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate 	ndi_devi_enter(parent, &circ);
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate 	dip = ndi_devi_findchild(parent, devname);
6017c478bd9Sstevel@tonic-gate 	kmem_free(devname, devnamelen);
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate 	if (dip == NULL) {
6047c478bd9Sstevel@tonic-gate 		ndi_devi_exit(parent, circ);
6057c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
6067c478bd9Sstevel@tonic-gate 	}
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate 	mutex_enter(&(DEVI(dip)->devi_lock));
6097c478bd9Sstevel@tonic-gate 	if (DEVI_IS_DEVICE_OFFLINE(dip)) {
6107c478bd9Sstevel@tonic-gate 		*state = DEVICE_OFFLINE;
6117c478bd9Sstevel@tonic-gate 	} else if (DEVI_IS_DEVICE_DOWN(dip)) {
6127c478bd9Sstevel@tonic-gate 		*state = DEVICE_DOWN;
6137c478bd9Sstevel@tonic-gate 	} else {
6147c478bd9Sstevel@tonic-gate 		*state = DEVICE_ONLINE;
6157c478bd9Sstevel@tonic-gate 		if (devi_stillreferenced(dip) == DEVI_REFERENCED)
6167c478bd9Sstevel@tonic-gate 			*state |= DEVICE_BUSY;
6177c478bd9Sstevel@tonic-gate 	}
6187c478bd9Sstevel@tonic-gate 
6197c478bd9Sstevel@tonic-gate 	mutex_exit(&(DEVI(dip)->devi_lock));
6207c478bd9Sstevel@tonic-gate 	ndi_devi_exit(parent, circ);
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate 	return (NDI_SUCCESS);
6237c478bd9Sstevel@tonic-gate }
6247c478bd9Sstevel@tonic-gate 
6257c478bd9Sstevel@tonic-gate /*
6267c478bd9Sstevel@tonic-gate  * return the current state of the device "dip"
6277c478bd9Sstevel@tonic-gate  *
6287c478bd9Sstevel@tonic-gate  * recommend using ndi_devctl_ioctl() or
6297c478bd9Sstevel@tonic-gate  * ndi_devctl_device_getstate() instead
6307c478bd9Sstevel@tonic-gate  */
6317c478bd9Sstevel@tonic-gate int
6327c478bd9Sstevel@tonic-gate ndi_dc_return_dev_state(dev_info_t *dip, struct devctl_iocdata *dcp)
6337c478bd9Sstevel@tonic-gate {
6347c478bd9Sstevel@tonic-gate 	dev_info_t *pdip;
6357c478bd9Sstevel@tonic-gate 	uint_t devstate = 0;
6367c478bd9Sstevel@tonic-gate 	int circ;
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate 	if ((dip == NULL) || (dcp == NULL))
6397c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate 	pdip = ddi_get_parent(dip);
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
6447c478bd9Sstevel@tonic-gate 	mutex_enter(&(DEVI(dip)->devi_lock));
6457c478bd9Sstevel@tonic-gate 	if (DEVI_IS_DEVICE_OFFLINE(dip)) {
6467c478bd9Sstevel@tonic-gate 		devstate = DEVICE_OFFLINE;
6477c478bd9Sstevel@tonic-gate 	} else if (DEVI_IS_DEVICE_DOWN(dip)) {
6487c478bd9Sstevel@tonic-gate 		devstate = DEVICE_DOWN;
6497c478bd9Sstevel@tonic-gate 	} else {
6507c478bd9Sstevel@tonic-gate 		devstate = DEVICE_ONLINE;
6517c478bd9Sstevel@tonic-gate 		if (devi_stillreferenced(dip) == DEVI_REFERENCED)
6527c478bd9Sstevel@tonic-gate 			devstate |= DEVICE_BUSY;
6537c478bd9Sstevel@tonic-gate 	}
6547c478bd9Sstevel@tonic-gate 
6557c478bd9Sstevel@tonic-gate 	mutex_exit(&(DEVI(dip)->devi_lock));
6567c478bd9Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
6577c478bd9Sstevel@tonic-gate 
6587c478bd9Sstevel@tonic-gate 	if (copyout(&devstate, dcp->cpyout_buf, sizeof (uint_t)) != 0)
6597c478bd9Sstevel@tonic-gate 		return (NDI_FAULT);
6607c478bd9Sstevel@tonic-gate 
6617c478bd9Sstevel@tonic-gate 	return (NDI_SUCCESS);
6627c478bd9Sstevel@tonic-gate }
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate /*
6657c478bd9Sstevel@tonic-gate  * Return device's bus state
6667c478bd9Sstevel@tonic-gate  * For use by a driver's DEVCTL_BUS_GETSTATE handler.
6677c478bd9Sstevel@tonic-gate  */
6687c478bd9Sstevel@tonic-gate int
6697c478bd9Sstevel@tonic-gate ndi_devctl_bus_getstate(dev_info_t *dip, struct devctl_iocdata *dcp,
6707c478bd9Sstevel@tonic-gate 	uint_t *state)
6717c478bd9Sstevel@tonic-gate {
6727c478bd9Sstevel@tonic-gate 	if ((dip == NULL) || (dcp == NULL))
6737c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
6747c478bd9Sstevel@tonic-gate 
6757c478bd9Sstevel@tonic-gate 	return (ndi_get_bus_state(dip, state));
6767c478bd9Sstevel@tonic-gate }
6777c478bd9Sstevel@tonic-gate 
6787c478bd9Sstevel@tonic-gate /*
6797c478bd9Sstevel@tonic-gate  * Generic devctl ioctl handler
6807c478bd9Sstevel@tonic-gate  */
6817c478bd9Sstevel@tonic-gate int
6827c478bd9Sstevel@tonic-gate ndi_devctl_ioctl(dev_info_t *dip, int cmd, intptr_t arg, int mode, uint_t flags)
6837c478bd9Sstevel@tonic-gate {
6847c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(mode))
6857c478bd9Sstevel@tonic-gate 	struct devctl_iocdata *dcp;
6867c478bd9Sstevel@tonic-gate 	uint_t state;
6877c478bd9Sstevel@tonic-gate 	int rval = ENOTTY;
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate 	/*
6907c478bd9Sstevel@tonic-gate 	 * read devctl ioctl data
6917c478bd9Sstevel@tonic-gate 	 */
6927c478bd9Sstevel@tonic-gate 	if (ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS)
6937c478bd9Sstevel@tonic-gate 		return (EFAULT);
6947c478bd9Sstevel@tonic-gate 
6957c478bd9Sstevel@tonic-gate 	switch (cmd) {
6967c478bd9Sstevel@tonic-gate 
6977c478bd9Sstevel@tonic-gate 	case DEVCTL_BUS_GETSTATE:
6987c478bd9Sstevel@tonic-gate 		rval = ndi_devctl_bus_getstate(dip, dcp, &state);
6997c478bd9Sstevel@tonic-gate 		if (rval == NDI_SUCCESS) {
7007c478bd9Sstevel@tonic-gate 			if (copyout(&state, dcp->cpyout_buf,
7017c478bd9Sstevel@tonic-gate 			    sizeof (uint_t)) != 0)
7027c478bd9Sstevel@tonic-gate 				rval = NDI_FAULT;
7037c478bd9Sstevel@tonic-gate 		}
7047c478bd9Sstevel@tonic-gate 		break;
7057c478bd9Sstevel@tonic-gate 
7067c478bd9Sstevel@tonic-gate 	case DEVCTL_DEVICE_ONLINE:
7077c478bd9Sstevel@tonic-gate 		rval = ndi_devctl_device_online(dip, dcp, flags);
7087c478bd9Sstevel@tonic-gate 		break;
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate 	case DEVCTL_DEVICE_OFFLINE:
7117c478bd9Sstevel@tonic-gate 		rval = ndi_devctl_device_offline(dip, dcp, flags);
7127c478bd9Sstevel@tonic-gate 		break;
7137c478bd9Sstevel@tonic-gate 
7147c478bd9Sstevel@tonic-gate 	case DEVCTL_DEVICE_GETSTATE:
7157c478bd9Sstevel@tonic-gate 		rval = ndi_devctl_device_getstate(dip, dcp, &state);
7167c478bd9Sstevel@tonic-gate 		if (rval == NDI_SUCCESS) {
7177c478bd9Sstevel@tonic-gate 			if (copyout(&state, dcp->cpyout_buf,
7187c478bd9Sstevel@tonic-gate 			    sizeof (uint_t)) != 0)
7197c478bd9Sstevel@tonic-gate 				rval = NDI_FAULT;
7207c478bd9Sstevel@tonic-gate 		}
7217c478bd9Sstevel@tonic-gate 		break;
7227c478bd9Sstevel@tonic-gate 
7237c478bd9Sstevel@tonic-gate 	case DEVCTL_DEVICE_REMOVE:
7247c478bd9Sstevel@tonic-gate 		rval = ndi_devctl_device_remove(dip, dcp, flags);
7257c478bd9Sstevel@tonic-gate 		break;
7267c478bd9Sstevel@tonic-gate 
7277c478bd9Sstevel@tonic-gate 	case DEVCTL_BUS_DEV_CREATE:
7287c478bd9Sstevel@tonic-gate 		rval = ndi_dc_devi_create(dcp, dip, 0, NULL);
7297c478bd9Sstevel@tonic-gate 		break;
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate 	/*
7327c478bd9Sstevel@tonic-gate 	 * ioctls for which a generic implementation makes no sense
7337c478bd9Sstevel@tonic-gate 	 */
7347c478bd9Sstevel@tonic-gate 	case DEVCTL_BUS_RESET:
7357c478bd9Sstevel@tonic-gate 	case DEVCTL_BUS_RESETALL:
7367c478bd9Sstevel@tonic-gate 	case DEVCTL_DEVICE_RESET:
7377c478bd9Sstevel@tonic-gate 	case DEVCTL_AP_CONNECT:
7387c478bd9Sstevel@tonic-gate 	case DEVCTL_AP_DISCONNECT:
7397c478bd9Sstevel@tonic-gate 	case DEVCTL_AP_INSERT:
7407c478bd9Sstevel@tonic-gate 	case DEVCTL_AP_REMOVE:
7417c478bd9Sstevel@tonic-gate 	case DEVCTL_AP_CONFIGURE:
7427c478bd9Sstevel@tonic-gate 	case DEVCTL_AP_UNCONFIGURE:
7437c478bd9Sstevel@tonic-gate 	case DEVCTL_AP_GETSTATE:
7447c478bd9Sstevel@tonic-gate 	case DEVCTL_AP_CONTROL:
7457c478bd9Sstevel@tonic-gate 	case DEVCTL_BUS_QUIESCE:
7467c478bd9Sstevel@tonic-gate 	case DEVCTL_BUS_UNQUIESCE:
7477c478bd9Sstevel@tonic-gate 		rval = ENOTSUP;
7487c478bd9Sstevel@tonic-gate 		break;
7497c478bd9Sstevel@tonic-gate 	}
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate 	ndi_dc_freehdl(dcp);
7527c478bd9Sstevel@tonic-gate 	return (rval);
7537c478bd9Sstevel@tonic-gate }
7547c478bd9Sstevel@tonic-gate 
7557c478bd9Sstevel@tonic-gate /*
7567c478bd9Sstevel@tonic-gate  * Copyout the state of the Attachment Point "ap" to the requesting
7577c478bd9Sstevel@tonic-gate  * user process.
7587c478bd9Sstevel@tonic-gate  */
7597c478bd9Sstevel@tonic-gate int
7607c478bd9Sstevel@tonic-gate ndi_dc_return_ap_state(devctl_ap_state_t *ap, struct devctl_iocdata *dcp)
7617c478bd9Sstevel@tonic-gate {
7627c478bd9Sstevel@tonic-gate 	if ((ap == NULL) || (dcp == NULL))
7637c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
7647c478bd9Sstevel@tonic-gate 
7657c478bd9Sstevel@tonic-gate 
7667c478bd9Sstevel@tonic-gate 	if (get_udatamodel() == DATAMODEL_NATIVE) {
7677c478bd9Sstevel@tonic-gate 		if (copyout(ap, dcp->cpyout_buf,
7687c478bd9Sstevel@tonic-gate 			sizeof (devctl_ap_state_t)) != 0)
7697c478bd9Sstevel@tonic-gate 		    return (NDI_FAULT);
7707c478bd9Sstevel@tonic-gate 	}
7717c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
7727c478bd9Sstevel@tonic-gate 	else {
7737c478bd9Sstevel@tonic-gate 		struct devctl_ap_state32 ap_state32;
7747c478bd9Sstevel@tonic-gate 
7757c478bd9Sstevel@tonic-gate 		ap_state32.ap_rstate = ap->ap_rstate;
7767c478bd9Sstevel@tonic-gate 		ap_state32.ap_ostate = ap->ap_ostate;
7777c478bd9Sstevel@tonic-gate 		ap_state32.ap_condition = ap->ap_condition;
7787c478bd9Sstevel@tonic-gate 		ap_state32.ap_error_code = ap->ap_error_code;
7797c478bd9Sstevel@tonic-gate 		ap_state32.ap_in_transition = ap->ap_in_transition;
7807c478bd9Sstevel@tonic-gate 		ap_state32.ap_last_change = (time32_t)ap->ap_last_change;
7817c478bd9Sstevel@tonic-gate 		if (copyout(&ap_state32, dcp->cpyout_buf,
7827c478bd9Sstevel@tonic-gate 			sizeof (devctl_ap_state32_t)) != 0)
7837c478bd9Sstevel@tonic-gate 		    return (NDI_FAULT);
7847c478bd9Sstevel@tonic-gate 	}
7857c478bd9Sstevel@tonic-gate #endif
7867c478bd9Sstevel@tonic-gate 
7877c478bd9Sstevel@tonic-gate 	return (NDI_SUCCESS);
7887c478bd9Sstevel@tonic-gate }
7897c478bd9Sstevel@tonic-gate 
7907c478bd9Sstevel@tonic-gate /*
7917c478bd9Sstevel@tonic-gate  * Copyout the bus state of the bus nexus device "dip" to the requesting
7927c478bd9Sstevel@tonic-gate  * user process.
7937c478bd9Sstevel@tonic-gate  */
7947c478bd9Sstevel@tonic-gate int
7957c478bd9Sstevel@tonic-gate ndi_dc_return_bus_state(dev_info_t *dip, struct devctl_iocdata *dcp)
7967c478bd9Sstevel@tonic-gate {
7977c478bd9Sstevel@tonic-gate 	uint_t devstate = 0;
7987c478bd9Sstevel@tonic-gate 
7997c478bd9Sstevel@tonic-gate 	if ((dip == NULL) || (dcp == NULL))
8007c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
8017c478bd9Sstevel@tonic-gate 
8027c478bd9Sstevel@tonic-gate 	if (ndi_get_bus_state(dip, &devstate) != NDI_SUCCESS)
8037c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
8047c478bd9Sstevel@tonic-gate 
8057c478bd9Sstevel@tonic-gate 	if (copyout(&devstate, dcp->cpyout_buf, sizeof (uint_t)) != 0)
8067c478bd9Sstevel@tonic-gate 		return (NDI_FAULT);
8077c478bd9Sstevel@tonic-gate 
8087c478bd9Sstevel@tonic-gate 	return (NDI_SUCCESS);
8097c478bd9Sstevel@tonic-gate }
8107c478bd9Sstevel@tonic-gate 
8117c478bd9Sstevel@tonic-gate static int
8127c478bd9Sstevel@tonic-gate i_dc_devi_create(struct devctl_iocdata *, dev_info_t *, dev_info_t **);
8137c478bd9Sstevel@tonic-gate 
8147c478bd9Sstevel@tonic-gate /*
8157c478bd9Sstevel@tonic-gate  * create a child device node given the property definitions
8167c478bd9Sstevel@tonic-gate  * supplied by the userland process
8177c478bd9Sstevel@tonic-gate  */
8187c478bd9Sstevel@tonic-gate int
8197c478bd9Sstevel@tonic-gate ndi_dc_devi_create(struct devctl_iocdata *dcp, dev_info_t *pdip, int flags,
8207c478bd9Sstevel@tonic-gate     dev_info_t **rdip)
8217c478bd9Sstevel@tonic-gate {
8227c478bd9Sstevel@tonic-gate 	dev_info_t *cdip;
8237c478bd9Sstevel@tonic-gate 	int rv, circular = 0;
8247c478bd9Sstevel@tonic-gate 	char devnm[MAXNAMELEN];
8257c478bd9Sstevel@tonic-gate 	int nmlen;
8267c478bd9Sstevel@tonic-gate 
8277c478bd9Sstevel@tonic-gate 	/*
8287c478bd9Sstevel@tonic-gate 	 * The child device may have been pre-constructed by an earlier
8297c478bd9Sstevel@tonic-gate 	 * call to this function with the flag DEVCTL_CONSTRUCT set.
8307c478bd9Sstevel@tonic-gate 	 */
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate 	if ((cdip = (rdip != NULL) ? *rdip : NULL) == NULL)
8337c478bd9Sstevel@tonic-gate 		if ((rv = i_dc_devi_create(dcp, pdip, &cdip)) != 0)
8347c478bd9Sstevel@tonic-gate 			return (rv);
8357c478bd9Sstevel@tonic-gate 
8367c478bd9Sstevel@tonic-gate 	ASSERT(cdip != NULL);
8377c478bd9Sstevel@tonic-gate 
8387c478bd9Sstevel@tonic-gate 	/*
8397c478bd9Sstevel@tonic-gate 	 * Return the device node partially constructed if the
8407c478bd9Sstevel@tonic-gate 	 * DEVCTL_CONSTRUCT flag is set.
8417c478bd9Sstevel@tonic-gate 	 */
8427c478bd9Sstevel@tonic-gate 	if (flags & DEVCTL_CONSTRUCT) {
8437c478bd9Sstevel@tonic-gate 		if (rdip == NULL) {
8447c478bd9Sstevel@tonic-gate 			(void) ndi_devi_free(cdip);
8457c478bd9Sstevel@tonic-gate 			return (EINVAL);
8467c478bd9Sstevel@tonic-gate 		}
8477c478bd9Sstevel@tonic-gate 		*rdip = cdip;
8487c478bd9Sstevel@tonic-gate 		return (0);
8497c478bd9Sstevel@tonic-gate 	}
8507c478bd9Sstevel@tonic-gate 
8517c478bd9Sstevel@tonic-gate 	/*
8527c478bd9Sstevel@tonic-gate 	 * Bring the node up to a named but OFFLINE state.  The calling
8537c478bd9Sstevel@tonic-gate 	 * application will need to manage the node from here on.
8547c478bd9Sstevel@tonic-gate 	 */
8557c478bd9Sstevel@tonic-gate 	if (dcp->flags & DEVCTL_OFFLINE) {
856*25e8c5aaSvikram 		/*
857*25e8c5aaSvikram 		 * In the unlikely event that the dip was somehow attached by
858*25e8c5aaSvikram 		 * the userland process (and device contracts or LDI opens
859*25e8c5aaSvikram 		 * were registered against the dip) after it was created by
860*25e8c5aaSvikram 		 * a previous DEVCTL_CONSTRUCT call, we start notify
861*25e8c5aaSvikram 		 * proceedings on this dip. Note that we don't need to
862*25e8c5aaSvikram 		 * return the dip after a failure of the notify since
863*25e8c5aaSvikram 		 * for a contract or LDI handle to be created the dip was
864*25e8c5aaSvikram 		 * already available to the user.
865*25e8c5aaSvikram 		 */
866*25e8c5aaSvikram 		if (e_ddi_offline_notify(cdip) == DDI_FAILURE) {
867*25e8c5aaSvikram 			return (EBUSY);
868*25e8c5aaSvikram 		}
869*25e8c5aaSvikram 
8707c478bd9Sstevel@tonic-gate 		/*
8717c478bd9Sstevel@tonic-gate 		 * hand set the OFFLINE flag to prevent any asynchronous
8727c478bd9Sstevel@tonic-gate 		 * autoconfiguration operations from attaching this node.
8737c478bd9Sstevel@tonic-gate 		 */
87416747f41Scth 		mutex_enter(&(DEVI(cdip)->devi_lock));
87516747f41Scth 		DEVI_SET_DEVICE_OFFLINE(cdip);
87616747f41Scth 		mutex_exit(&(DEVI(cdip)->devi_lock));
87716747f41Scth 
878*25e8c5aaSvikram 		e_ddi_offline_finalize(cdip, DDI_SUCCESS);
879*25e8c5aaSvikram 
8807c478bd9Sstevel@tonic-gate 		rv = ndi_devi_bind_driver(cdip, flags);
8817c478bd9Sstevel@tonic-gate 		if (rv != NDI_SUCCESS) {
8827c478bd9Sstevel@tonic-gate 			(void) ndi_devi_offline(cdip, NDI_DEVI_REMOVE);
8837c478bd9Sstevel@tonic-gate 			return (ENXIO);
8847c478bd9Sstevel@tonic-gate 		}
8857c478bd9Sstevel@tonic-gate 
8867c478bd9Sstevel@tonic-gate 		/*
8877c478bd9Sstevel@tonic-gate 		 * remove the dev_info node if it failed to bind to a
8887c478bd9Sstevel@tonic-gate 		 * driver above.
8897c478bd9Sstevel@tonic-gate 		 */
8907c478bd9Sstevel@tonic-gate 		if (i_ddi_node_state(cdip) < DS_BOUND) {
8917c478bd9Sstevel@tonic-gate 			(void) ndi_devi_offline(cdip, NDI_DEVI_REMOVE);
8927c478bd9Sstevel@tonic-gate 			return (ENXIO);
8937c478bd9Sstevel@tonic-gate 		}
8947c478bd9Sstevel@tonic-gate 
8957c478bd9Sstevel@tonic-gate 		/*
8967c478bd9Sstevel@tonic-gate 		 * add the node to the per-driver list and INITCHILD it
8977c478bd9Sstevel@tonic-gate 		 * to give it a name.
8987c478bd9Sstevel@tonic-gate 		 */
8997c478bd9Sstevel@tonic-gate 		ndi_devi_enter(pdip, &circular);
9007c478bd9Sstevel@tonic-gate 		if ((rv = ddi_initchild(pdip, cdip)) != DDI_SUCCESS) {
9017c478bd9Sstevel@tonic-gate 			(void) ndi_devi_offline(cdip, NDI_DEVI_REMOVE);
9027c478bd9Sstevel@tonic-gate 			ndi_devi_exit(pdip, circular);
9037c478bd9Sstevel@tonic-gate 			return (EINVAL);
9047c478bd9Sstevel@tonic-gate 		}
9057c478bd9Sstevel@tonic-gate 		ndi_devi_exit(pdip, circular);
9067c478bd9Sstevel@tonic-gate 
9077c478bd9Sstevel@tonic-gate 	} else {
9087c478bd9Sstevel@tonic-gate 		/*
9097c478bd9Sstevel@tonic-gate 		 * Attempt to bring the device ONLINE. If the request to
9107c478bd9Sstevel@tonic-gate 		 * fails, remove the dev_info node.
9117c478bd9Sstevel@tonic-gate 		 */
9127c478bd9Sstevel@tonic-gate 		if (ndi_devi_online(cdip, NDI_ONLINE_ATTACH) != NDI_SUCCESS) {
9137c478bd9Sstevel@tonic-gate 			(void) ndi_devi_offline(cdip, NDI_DEVI_REMOVE);
9147c478bd9Sstevel@tonic-gate 			return (ENXIO);
9157c478bd9Sstevel@tonic-gate 		}
9167c478bd9Sstevel@tonic-gate 
9177c478bd9Sstevel@tonic-gate 		/*
9187c478bd9Sstevel@tonic-gate 		 * if the node was successfully added but there was
9197c478bd9Sstevel@tonic-gate 		 * no driver available for the device, remove the node
9207c478bd9Sstevel@tonic-gate 		 */
9217c478bd9Sstevel@tonic-gate 		if (i_ddi_node_state(cdip) < DS_BOUND) {
9227c478bd9Sstevel@tonic-gate 			(void) ndi_devi_offline(cdip, NDI_DEVI_REMOVE);
9237c478bd9Sstevel@tonic-gate 			return (ENODEV);
9247c478bd9Sstevel@tonic-gate 		}
9257c478bd9Sstevel@tonic-gate 	}
9267c478bd9Sstevel@tonic-gate 
9277c478bd9Sstevel@tonic-gate 	/*
9287c478bd9Sstevel@tonic-gate 	 * return a handle to the child device
9297c478bd9Sstevel@tonic-gate 	 * copy out the name of the newly attached child device if
9307c478bd9Sstevel@tonic-gate 	 * the IOCTL request has provided a copyout buffer.
9317c478bd9Sstevel@tonic-gate 	 */
9327c478bd9Sstevel@tonic-gate 	if (rdip != NULL)
9337c478bd9Sstevel@tonic-gate 		*rdip = cdip;
9347c478bd9Sstevel@tonic-gate 
9357c478bd9Sstevel@tonic-gate 	if (dcp->cpyout_buf == NULL)
9367c478bd9Sstevel@tonic-gate 		return (0);
9377c478bd9Sstevel@tonic-gate 
9387c478bd9Sstevel@tonic-gate 	ASSERT(ddi_node_name(cdip) != NULL);
9397c478bd9Sstevel@tonic-gate 	ASSERT(ddi_get_name_addr(cdip) != NULL);
9407c478bd9Sstevel@tonic-gate 
9417c478bd9Sstevel@tonic-gate 	nmlen = snprintf(devnm, MAXNAMELEN, "%s@%s",
9427c478bd9Sstevel@tonic-gate 	    ddi_node_name(cdip), ddi_get_name_addr(cdip));
9437c478bd9Sstevel@tonic-gate 
9447c478bd9Sstevel@tonic-gate 	if (copyout(&devnm, dcp->cpyout_buf, nmlen) != 0) {
9457c478bd9Sstevel@tonic-gate 		(void) ndi_devi_offline(cdip, NDI_DEVI_REMOVE);
9467c478bd9Sstevel@tonic-gate 		return (EFAULT);
9477c478bd9Sstevel@tonic-gate 	}
9487c478bd9Sstevel@tonic-gate 	return (0);
9497c478bd9Sstevel@tonic-gate }
9507c478bd9Sstevel@tonic-gate 
9517c478bd9Sstevel@tonic-gate static int
9527c478bd9Sstevel@tonic-gate i_dc_devi_create(struct devctl_iocdata *dcp, dev_info_t *pdip,
9537c478bd9Sstevel@tonic-gate     dev_info_t **rdip)
9547c478bd9Sstevel@tonic-gate {
9557c478bd9Sstevel@tonic-gate 
9567c478bd9Sstevel@tonic-gate 	dev_info_t *cdip;
9577c478bd9Sstevel@tonic-gate 	char *cname = NULL;
9587c478bd9Sstevel@tonic-gate 	nvlist_t *nvlp = dcp->nvl_user;
9597c478bd9Sstevel@tonic-gate 	nvpair_t *npp;
9607c478bd9Sstevel@tonic-gate 	char *np;
9617c478bd9Sstevel@tonic-gate 	int rv = 0;
9627c478bd9Sstevel@tonic-gate 
9637c478bd9Sstevel@tonic-gate 	ASSERT(rdip != NULL && *rdip == NULL);
9647c478bd9Sstevel@tonic-gate 
9657c478bd9Sstevel@tonic-gate 	if ((nvlp == NULL) ||
9667c478bd9Sstevel@tonic-gate 	    (nvlist_lookup_string(nvlp, DC_DEVI_NODENAME, &cname) != 0))
9677c478bd9Sstevel@tonic-gate 		return (EINVAL);
9687c478bd9Sstevel@tonic-gate 
9697c478bd9Sstevel@tonic-gate 	/*
9707c478bd9Sstevel@tonic-gate 	 * construct a new dev_info node with a user-provided nodename
9717c478bd9Sstevel@tonic-gate 	 */
972fa9e4066Sahrens 	ndi_devi_alloc_sleep(pdip, cname, (pnode_t)DEVI_SID_NODEID, &cdip);
9737c478bd9Sstevel@tonic-gate 
9747c478bd9Sstevel@tonic-gate 	/*
9757c478bd9Sstevel@tonic-gate 	 * create hardware properties for each member in the property
9767c478bd9Sstevel@tonic-gate 	 * list.
9777c478bd9Sstevel@tonic-gate 	 */
9787c478bd9Sstevel@tonic-gate 	for (npp = nvlist_next_nvpair(nvlp, NULL); (npp != NULL && !rv);
9797c478bd9Sstevel@tonic-gate 	    npp = nvlist_next_nvpair(nvlp, npp)) {
9807c478bd9Sstevel@tonic-gate 
9817c478bd9Sstevel@tonic-gate 		np = nvpair_name(npp);
9827c478bd9Sstevel@tonic-gate 
9837c478bd9Sstevel@tonic-gate 		/*
9847c478bd9Sstevel@tonic-gate 		 * skip the nodename property
9857c478bd9Sstevel@tonic-gate 		 */
9867c478bd9Sstevel@tonic-gate 		if (strcmp(np, DC_DEVI_NODENAME) == 0)
9877c478bd9Sstevel@tonic-gate 			continue;
9887c478bd9Sstevel@tonic-gate 
9897c478bd9Sstevel@tonic-gate 		switch (nvpair_type(npp)) {
9907c478bd9Sstevel@tonic-gate 
9917c478bd9Sstevel@tonic-gate 		case DATA_TYPE_INT32: {
9927c478bd9Sstevel@tonic-gate 			int32_t prop_val;
9937c478bd9Sstevel@tonic-gate 
9947c478bd9Sstevel@tonic-gate 			if ((rv = nvpair_value_int32(npp, &prop_val)) != 0)
9957c478bd9Sstevel@tonic-gate 				break;
9967c478bd9Sstevel@tonic-gate 
9977c478bd9Sstevel@tonic-gate 			(void) ndi_prop_update_int(DDI_DEV_T_NONE, cdip, np,
9987c478bd9Sstevel@tonic-gate 			    (int)prop_val);
9997c478bd9Sstevel@tonic-gate 			break;
10007c478bd9Sstevel@tonic-gate 		}
10017c478bd9Sstevel@tonic-gate 
10027c478bd9Sstevel@tonic-gate 		case DATA_TYPE_STRING: {
10037c478bd9Sstevel@tonic-gate 			char *prop_val;
10047c478bd9Sstevel@tonic-gate 
10057c478bd9Sstevel@tonic-gate 			if ((rv = nvpair_value_string(npp, &prop_val)) != 0)
10067c478bd9Sstevel@tonic-gate 				break;
10077c478bd9Sstevel@tonic-gate 
10087c478bd9Sstevel@tonic-gate 			(void) ndi_prop_update_string(DDI_DEV_T_NONE, cdip,
10097c478bd9Sstevel@tonic-gate 			    np, prop_val);
10107c478bd9Sstevel@tonic-gate 			break;
10117c478bd9Sstevel@tonic-gate 		}
10127c478bd9Sstevel@tonic-gate 
10137c478bd9Sstevel@tonic-gate 		case DATA_TYPE_BYTE_ARRAY: {
10147c478bd9Sstevel@tonic-gate 			uchar_t *val;
10157c478bd9Sstevel@tonic-gate 			uint_t nelms;
10167c478bd9Sstevel@tonic-gate 
10177c478bd9Sstevel@tonic-gate 			if ((rv = nvpair_value_byte_array(npp, &val,
10187c478bd9Sstevel@tonic-gate 			    &nelms)) != 0)
10197c478bd9Sstevel@tonic-gate 				break;
10207c478bd9Sstevel@tonic-gate 
10217c478bd9Sstevel@tonic-gate 			(void) ndi_prop_update_byte_array(DDI_DEV_T_NONE,
10227c478bd9Sstevel@tonic-gate 			    cdip, np, (uchar_t *)val, nelms);
10237c478bd9Sstevel@tonic-gate 			break;
10247c478bd9Sstevel@tonic-gate 		}
10257c478bd9Sstevel@tonic-gate 
10267c478bd9Sstevel@tonic-gate 		case DATA_TYPE_INT32_ARRAY: {
10277c478bd9Sstevel@tonic-gate 			int32_t *val;
10287c478bd9Sstevel@tonic-gate 			uint_t nelms;
10297c478bd9Sstevel@tonic-gate 
10307c478bd9Sstevel@tonic-gate 			if ((rv = nvpair_value_int32_array(npp, &val,
10317c478bd9Sstevel@tonic-gate 			    &nelms)) != 0)
10327c478bd9Sstevel@tonic-gate 				break;
10337c478bd9Sstevel@tonic-gate 
10347c478bd9Sstevel@tonic-gate 			(void) ndi_prop_update_int_array(DDI_DEV_T_NONE,
10357c478bd9Sstevel@tonic-gate 			    cdip, np, val, nelms);
10367c478bd9Sstevel@tonic-gate 			break;
10377c478bd9Sstevel@tonic-gate 		}
10387c478bd9Sstevel@tonic-gate 
10397c478bd9Sstevel@tonic-gate 		case DATA_TYPE_STRING_ARRAY: {
10407c478bd9Sstevel@tonic-gate 			char **val;
10417c478bd9Sstevel@tonic-gate 			uint_t nelms;
10427c478bd9Sstevel@tonic-gate 
10437c478bd9Sstevel@tonic-gate 			if ((rv = nvpair_value_string_array(npp, &val,
10447c478bd9Sstevel@tonic-gate 			    &nelms)) != 0)
10457c478bd9Sstevel@tonic-gate 				break;
10467c478bd9Sstevel@tonic-gate 
10477c478bd9Sstevel@tonic-gate 			(void) ndi_prop_update_string_array(DDI_DEV_T_NONE,
10487c478bd9Sstevel@tonic-gate 			    cdip, np, val, nelms);
10497c478bd9Sstevel@tonic-gate 			break;
10507c478bd9Sstevel@tonic-gate 		}
10517c478bd9Sstevel@tonic-gate 
10527c478bd9Sstevel@tonic-gate 		/*
10537c478bd9Sstevel@tonic-gate 		 * unsupported property data type
10547c478bd9Sstevel@tonic-gate 		 */
10557c478bd9Sstevel@tonic-gate 		default:
10567c478bd9Sstevel@tonic-gate 			rv = EINVAL;
10577c478bd9Sstevel@tonic-gate 		}
10587c478bd9Sstevel@tonic-gate 	}
10597c478bd9Sstevel@tonic-gate 
10607c478bd9Sstevel@tonic-gate 	/*
10617c478bd9Sstevel@tonic-gate 	 * something above failed
10627c478bd9Sstevel@tonic-gate 	 * destroy the partially child device and abort the request
10637c478bd9Sstevel@tonic-gate 	 */
10647c478bd9Sstevel@tonic-gate 	if (rv != 0) {
10657c478bd9Sstevel@tonic-gate 		(void) ndi_devi_free(cdip);
10667c478bd9Sstevel@tonic-gate 		return (rv);
10677c478bd9Sstevel@tonic-gate 	}
10687c478bd9Sstevel@tonic-gate 
10697c478bd9Sstevel@tonic-gate 	*rdip = cdip;
10707c478bd9Sstevel@tonic-gate 	return (0);
10717c478bd9Sstevel@tonic-gate }
10727c478bd9Sstevel@tonic-gate 
10737c478bd9Sstevel@tonic-gate /*
10747c478bd9Sstevel@tonic-gate  * return current soft bus state of bus nexus "dip"
10757c478bd9Sstevel@tonic-gate  */
10767c478bd9Sstevel@tonic-gate int
10777c478bd9Sstevel@tonic-gate ndi_get_bus_state(dev_info_t *dip, uint_t *rstate)
10787c478bd9Sstevel@tonic-gate {
10797c478bd9Sstevel@tonic-gate 	if (dip == NULL || rstate == NULL)
10807c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
10817c478bd9Sstevel@tonic-gate 
10827c478bd9Sstevel@tonic-gate 	if (DEVI(dip)->devi_ops->devo_bus_ops == NULL)
10837c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
10847c478bd9Sstevel@tonic-gate 
10857c478bd9Sstevel@tonic-gate 	mutex_enter(&(DEVI(dip)->devi_lock));
10867c478bd9Sstevel@tonic-gate 	if (DEVI_IS_BUS_QUIESCED(dip))
10877c478bd9Sstevel@tonic-gate 		*rstate = BUS_QUIESCED;
10887c478bd9Sstevel@tonic-gate 	else if (DEVI_IS_BUS_DOWN(dip))
10897c478bd9Sstevel@tonic-gate 		*rstate = BUS_SHUTDOWN;
10907c478bd9Sstevel@tonic-gate 	else
10917c478bd9Sstevel@tonic-gate 		*rstate = BUS_ACTIVE;
10927c478bd9Sstevel@tonic-gate 	mutex_exit(&(DEVI(dip)->devi_lock));
10937c478bd9Sstevel@tonic-gate 	return (NDI_SUCCESS);
10947c478bd9Sstevel@tonic-gate }
10957c478bd9Sstevel@tonic-gate 
10967c478bd9Sstevel@tonic-gate /*
10977c478bd9Sstevel@tonic-gate  * Set the soft state of bus nexus "dip"
10987c478bd9Sstevel@tonic-gate  */
10997c478bd9Sstevel@tonic-gate int
11007c478bd9Sstevel@tonic-gate ndi_set_bus_state(dev_info_t *dip, uint_t state)
11017c478bd9Sstevel@tonic-gate {
11027c478bd9Sstevel@tonic-gate 	int rv = NDI_SUCCESS;
11037c478bd9Sstevel@tonic-gate 
11047c478bd9Sstevel@tonic-gate 	if (dip == NULL)
11057c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
11067c478bd9Sstevel@tonic-gate 
11077c478bd9Sstevel@tonic-gate 	mutex_enter(&(DEVI(dip)->devi_lock));
11087c478bd9Sstevel@tonic-gate 
11097c478bd9Sstevel@tonic-gate 	switch (state) {
11107c478bd9Sstevel@tonic-gate 	case BUS_QUIESCED:
11117c478bd9Sstevel@tonic-gate 		DEVI_SET_BUS_QUIESCE(dip);
11127c478bd9Sstevel@tonic-gate 		break;
11137c478bd9Sstevel@tonic-gate 
11147c478bd9Sstevel@tonic-gate 	case BUS_ACTIVE:
11157c478bd9Sstevel@tonic-gate 		DEVI_SET_BUS_ACTIVE(dip);
11167c478bd9Sstevel@tonic-gate 		DEVI_SET_BUS_UP(dip);
11177c478bd9Sstevel@tonic-gate 		break;
11187c478bd9Sstevel@tonic-gate 
11197c478bd9Sstevel@tonic-gate 	case BUS_SHUTDOWN:
11207c478bd9Sstevel@tonic-gate 		DEVI_SET_BUS_DOWN(dip);
11217c478bd9Sstevel@tonic-gate 		break;
11227c478bd9Sstevel@tonic-gate 
11237c478bd9Sstevel@tonic-gate 	default:
11247c478bd9Sstevel@tonic-gate 		rv = NDI_FAILURE;
11257c478bd9Sstevel@tonic-gate 	}
11267c478bd9Sstevel@tonic-gate 
11277c478bd9Sstevel@tonic-gate 	mutex_exit(&(DEVI(dip)->devi_lock));
11287c478bd9Sstevel@tonic-gate 	return (rv);
11297c478bd9Sstevel@tonic-gate }
11307c478bd9Sstevel@tonic-gate 
11317c478bd9Sstevel@tonic-gate /*
11327c478bd9Sstevel@tonic-gate  * These dummy functions are obsolete and may be removed.
11337c478bd9Sstevel@tonic-gate  * Retained for existing driver compatibility only.
11347c478bd9Sstevel@tonic-gate  * Drivers should be fixed not to use these functions.
11357c478bd9Sstevel@tonic-gate  * Don't write new code using these obsolete interfaces.
11367c478bd9Sstevel@tonic-gate  */
11377c478bd9Sstevel@tonic-gate /*ARGSUSED*/
11387c478bd9Sstevel@tonic-gate void
11397c478bd9Sstevel@tonic-gate i_ndi_block_device_tree_changes(uint_t *lkcnt)	/* obsolete */
11407c478bd9Sstevel@tonic-gate {
11417c478bd9Sstevel@tonic-gate 	/* obsolete dummy function */
11427c478bd9Sstevel@tonic-gate }
11437c478bd9Sstevel@tonic-gate 
11447c478bd9Sstevel@tonic-gate /*ARGSUSED*/
11457c478bd9Sstevel@tonic-gate void
11467c478bd9Sstevel@tonic-gate i_ndi_allow_device_tree_changes(uint_t lkcnt)	/* obsolete */
11477c478bd9Sstevel@tonic-gate {
11487c478bd9Sstevel@tonic-gate 	/* obsolete dummy function */
11497c478bd9Sstevel@tonic-gate }
11507c478bd9Sstevel@tonic-gate 
11517c478bd9Sstevel@tonic-gate /*
11527c478bd9Sstevel@tonic-gate  * Single thread entry into per-driver list
11537c478bd9Sstevel@tonic-gate  */
11547c478bd9Sstevel@tonic-gate /*ARGSUSED*/
11557c478bd9Sstevel@tonic-gate void
11567c478bd9Sstevel@tonic-gate e_ddi_enter_driver_list(struct devnames *dnp, int *listcnt)	/* obsolete */
11577c478bd9Sstevel@tonic-gate {
11587c478bd9Sstevel@tonic-gate 	/* obsolete dummy function */
11597c478bd9Sstevel@tonic-gate }
11607c478bd9Sstevel@tonic-gate 
11617c478bd9Sstevel@tonic-gate /*
11627c478bd9Sstevel@tonic-gate  * release the per-driver list
11637c478bd9Sstevel@tonic-gate  */
11647c478bd9Sstevel@tonic-gate /*ARGSUSED*/
11657c478bd9Sstevel@tonic-gate void
11667c478bd9Sstevel@tonic-gate e_ddi_exit_driver_list(struct devnames *dnp, int listcnt)	/* obsolete */
11677c478bd9Sstevel@tonic-gate {
11687c478bd9Sstevel@tonic-gate 	/* obsolete dummy function */
11697c478bd9Sstevel@tonic-gate }
11707c478bd9Sstevel@tonic-gate 
11717c478bd9Sstevel@tonic-gate /*
11727c478bd9Sstevel@tonic-gate  * Attempt to enter driver list
11737c478bd9Sstevel@tonic-gate  */
11747c478bd9Sstevel@tonic-gate /*ARGSUSED*/
11757c478bd9Sstevel@tonic-gate int
11767c478bd9Sstevel@tonic-gate e_ddi_tryenter_driver_list(struct devnames *dnp, int *listcnt)	/* obsolete */
11777c478bd9Sstevel@tonic-gate {
11787c478bd9Sstevel@tonic-gate 	return (1);	/* obsolete dummy function */
11797c478bd9Sstevel@tonic-gate }
11807c478bd9Sstevel@tonic-gate 
11817c478bd9Sstevel@tonic-gate /*
11827c478bd9Sstevel@tonic-gate  * ndi event handling support functions:
11837c478bd9Sstevel@tonic-gate  * The NDI event support model is as follows:
11847c478bd9Sstevel@tonic-gate  *
11857c478bd9Sstevel@tonic-gate  * The nexus driver defines a set of events using some static structures (so
11867c478bd9Sstevel@tonic-gate  * these structures can be shared by all instances of the nexus driver).
11877c478bd9Sstevel@tonic-gate  * The nexus driver allocates an event handle and binds the event set
11887c478bd9Sstevel@tonic-gate  * to this handle. The nexus driver's event busop functions can just
11897c478bd9Sstevel@tonic-gate  * call the appropriate NDI event support function using this handle
11907c478bd9Sstevel@tonic-gate  * as the first argument.
11917c478bd9Sstevel@tonic-gate  *
11927c478bd9Sstevel@tonic-gate  * The reasoning for tying events to the device tree is that the entity
11937c478bd9Sstevel@tonic-gate  * generating the callback will typically be one of the device driver's
11947c478bd9Sstevel@tonic-gate  * ancestors in the tree.
11957c478bd9Sstevel@tonic-gate  */
11967c478bd9Sstevel@tonic-gate static int ndi_event_debug = 0;
11977c478bd9Sstevel@tonic-gate 
11987c478bd9Sstevel@tonic-gate #ifdef DEBUG
11997c478bd9Sstevel@tonic-gate #define	NDI_EVENT_DEBUG	ndi_event_debug
12007c478bd9Sstevel@tonic-gate #endif /* DEBUG */
12017c478bd9Sstevel@tonic-gate 
12027c478bd9Sstevel@tonic-gate /*
12037c478bd9Sstevel@tonic-gate  * allocate a new ndi event handle
12047c478bd9Sstevel@tonic-gate  */
12057c478bd9Sstevel@tonic-gate int
12067c478bd9Sstevel@tonic-gate ndi_event_alloc_hdl(dev_info_t *dip, ddi_iblock_cookie_t cookie,
12077c478bd9Sstevel@tonic-gate 	ndi_event_hdl_t *handle, uint_t flag)
12087c478bd9Sstevel@tonic-gate {
12097c478bd9Sstevel@tonic-gate 	struct ndi_event_hdl *ndi_event_hdl;
12107c478bd9Sstevel@tonic-gate 
12117c478bd9Sstevel@tonic-gate 	ndi_event_hdl = kmem_zalloc(sizeof (struct ndi_event_hdl),
12127c478bd9Sstevel@tonic-gate 		((flag & NDI_NOSLEEP) ? KM_NOSLEEP : KM_SLEEP));
12137c478bd9Sstevel@tonic-gate 
12147c478bd9Sstevel@tonic-gate 	if (!ndi_event_hdl) {
12157c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
12167c478bd9Sstevel@tonic-gate 	}
12177c478bd9Sstevel@tonic-gate 
12187c478bd9Sstevel@tonic-gate 	ndi_event_hdl->ndi_evthdl_dip = dip;
12197c478bd9Sstevel@tonic-gate 	ndi_event_hdl->ndi_evthdl_iblock_cookie = cookie;
12207c478bd9Sstevel@tonic-gate 	mutex_init(&ndi_event_hdl->ndi_evthdl_mutex, NULL,
12217c478bd9Sstevel@tonic-gate 	    MUTEX_DRIVER, (void *)cookie);
12227c478bd9Sstevel@tonic-gate 
12237c478bd9Sstevel@tonic-gate 	mutex_init(&ndi_event_hdl->ndi_evthdl_cb_mutex, NULL,
12247c478bd9Sstevel@tonic-gate 	    MUTEX_DRIVER, (void *)cookie);
12257c478bd9Sstevel@tonic-gate 
12267c478bd9Sstevel@tonic-gate 	*handle = (ndi_event_hdl_t)ndi_event_hdl;
12277c478bd9Sstevel@tonic-gate 
12287c478bd9Sstevel@tonic-gate 	return (NDI_SUCCESS);
12297c478bd9Sstevel@tonic-gate }
12307c478bd9Sstevel@tonic-gate 
12317c478bd9Sstevel@tonic-gate /*
12327c478bd9Sstevel@tonic-gate  * free the ndi event handle
12337c478bd9Sstevel@tonic-gate  */
12347c478bd9Sstevel@tonic-gate int
12357c478bd9Sstevel@tonic-gate ndi_event_free_hdl(ndi_event_hdl_t handle)
12367c478bd9Sstevel@tonic-gate {
12377c478bd9Sstevel@tonic-gate 	struct ndi_event_hdl *ndi_event_hdl = (struct ndi_event_hdl *)handle;
12387c478bd9Sstevel@tonic-gate 	ndi_event_cookie_t *cookie;
12397c478bd9Sstevel@tonic-gate 	ndi_event_cookie_t *free;
12407c478bd9Sstevel@tonic-gate 
12417c478bd9Sstevel@tonic-gate 	ASSERT(handle);
12427c478bd9Sstevel@tonic-gate 
12437c478bd9Sstevel@tonic-gate 	mutex_enter(&ndi_event_hdl->ndi_evthdl_mutex);
12447c478bd9Sstevel@tonic-gate 	mutex_enter(&ndi_event_hdl->ndi_evthdl_cb_mutex);
12457c478bd9Sstevel@tonic-gate 
12467c478bd9Sstevel@tonic-gate 	cookie = ndi_event_hdl->ndi_evthdl_cookie_list;
12477c478bd9Sstevel@tonic-gate 
12487c478bd9Sstevel@tonic-gate 	/* deallocate all defined cookies */
12497c478bd9Sstevel@tonic-gate 	while (cookie != NULL) {
12507c478bd9Sstevel@tonic-gate 		ASSERT(cookie->callback_list == NULL);
12517c478bd9Sstevel@tonic-gate 		free = cookie;
12527c478bd9Sstevel@tonic-gate 		cookie = cookie->next_cookie;
12537c478bd9Sstevel@tonic-gate 
12547c478bd9Sstevel@tonic-gate 		kmem_free(free, sizeof (ndi_event_cookie_t));
12557c478bd9Sstevel@tonic-gate 	}
12567c478bd9Sstevel@tonic-gate 
12577c478bd9Sstevel@tonic-gate 
12587c478bd9Sstevel@tonic-gate 	mutex_exit(&ndi_event_hdl->ndi_evthdl_cb_mutex);
12597c478bd9Sstevel@tonic-gate 	mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
12607c478bd9Sstevel@tonic-gate 
12617c478bd9Sstevel@tonic-gate 	/* destroy mutexes */
12627c478bd9Sstevel@tonic-gate 	mutex_destroy(&ndi_event_hdl->ndi_evthdl_mutex);
12637c478bd9Sstevel@tonic-gate 	mutex_destroy(&ndi_event_hdl->ndi_evthdl_cb_mutex);
12647c478bd9Sstevel@tonic-gate 
12657c478bd9Sstevel@tonic-gate 	/* free event handle */
12667c478bd9Sstevel@tonic-gate 	kmem_free(ndi_event_hdl, sizeof (struct ndi_event_hdl));
12677c478bd9Sstevel@tonic-gate 
12687c478bd9Sstevel@tonic-gate 	return (NDI_SUCCESS);
12697c478bd9Sstevel@tonic-gate }
12707c478bd9Sstevel@tonic-gate 
12717c478bd9Sstevel@tonic-gate 
12727c478bd9Sstevel@tonic-gate /*
12737c478bd9Sstevel@tonic-gate  * ndi_event_bind_set() adds a set of events to the NDI event
12747c478bd9Sstevel@tonic-gate  * handle.
12757c478bd9Sstevel@tonic-gate  *
12767c478bd9Sstevel@tonic-gate  * Events generated by high level interrupts should not
12777c478bd9Sstevel@tonic-gate  * be mixed in the same event set with events generated by
12787c478bd9Sstevel@tonic-gate  * normal interrupts or kernel events.
12797c478bd9Sstevel@tonic-gate  *
12807c478bd9Sstevel@tonic-gate  * This function can be called multiple times to bind
12817c478bd9Sstevel@tonic-gate  * additional sets to the event handle.
12827c478bd9Sstevel@tonic-gate  * However, events generated by high level interrupts cannot
12837c478bd9Sstevel@tonic-gate  * be bound to a handle that already has bound events generated
12847c478bd9Sstevel@tonic-gate  * by normal interrupts or from kernel context and vice versa.
12857c478bd9Sstevel@tonic-gate  */
12867c478bd9Sstevel@tonic-gate int
12877c478bd9Sstevel@tonic-gate ndi_event_bind_set(ndi_event_hdl_t handle,
12887c478bd9Sstevel@tonic-gate 	ndi_event_set_t		*ndi_events,
12897c478bd9Sstevel@tonic-gate 	uint_t			flag)
12907c478bd9Sstevel@tonic-gate {
12917c478bd9Sstevel@tonic-gate 	struct ndi_event_hdl	*ndi_event_hdl;
12927c478bd9Sstevel@tonic-gate 	ndi_event_cookie_t	*next, *prev, *new_cookie;
12937c478bd9Sstevel@tonic-gate 	uint_t			i, len;
12947c478bd9Sstevel@tonic-gate 	uint_t			dup = 0;
12957c478bd9Sstevel@tonic-gate 	uint_t			high_plevels, other_plevels;
12967c478bd9Sstevel@tonic-gate 	ndi_event_definition_t *ndi_event_defs;
12977c478bd9Sstevel@tonic-gate 
12987c478bd9Sstevel@tonic-gate 	int km_flag = ((flag & NDI_NOSLEEP) ? KM_NOSLEEP : KM_SLEEP);
12997c478bd9Sstevel@tonic-gate 
13007c478bd9Sstevel@tonic-gate 	ASSERT(handle);
13017c478bd9Sstevel@tonic-gate 	ASSERT(ndi_events);
13027c478bd9Sstevel@tonic-gate 
13037c478bd9Sstevel@tonic-gate 	/*
13047c478bd9Sstevel@tonic-gate 	 * binding must be performed during attach/detach
13057c478bd9Sstevel@tonic-gate 	 */
13067c478bd9Sstevel@tonic-gate 	if (!DEVI_IS_ATTACHING(handle->ndi_evthdl_dip) &&
13077c478bd9Sstevel@tonic-gate 	    !DEVI_IS_DETACHING(handle->ndi_evthdl_dip)) {
13087c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "ndi_event_bind_set must be called within "
13097c478bd9Sstevel@tonic-gate 		    "attach or detach");
13107c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
13117c478bd9Sstevel@tonic-gate 	}
13127c478bd9Sstevel@tonic-gate 
13137c478bd9Sstevel@tonic-gate 	/*
13147c478bd9Sstevel@tonic-gate 	 * if it is not the correct version or the event set is
13157c478bd9Sstevel@tonic-gate 	 * empty, bail out
13167c478bd9Sstevel@tonic-gate 	 */
13177c478bd9Sstevel@tonic-gate 	if (ndi_events->ndi_events_version != NDI_EVENTS_REV1)
13187c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
13197c478bd9Sstevel@tonic-gate 
13207c478bd9Sstevel@tonic-gate 	ndi_event_hdl	= (struct ndi_event_hdl *)handle;
13217c478bd9Sstevel@tonic-gate 	ndi_event_defs = ndi_events->ndi_event_defs;
13227c478bd9Sstevel@tonic-gate 	high_plevels	= other_plevels = 0;
13237c478bd9Sstevel@tonic-gate 
13247c478bd9Sstevel@tonic-gate 	mutex_enter(&ndi_event_hdl->ndi_evthdl_mutex);
13257c478bd9Sstevel@tonic-gate 
13267c478bd9Sstevel@tonic-gate 	/* check for mixing events at high level with the other types */
13277c478bd9Sstevel@tonic-gate 	for (i = 0; i < ndi_events->ndi_n_events; i++) {
13287c478bd9Sstevel@tonic-gate 		if (ndi_event_defs[i].ndi_event_plevel == EPL_HIGHLEVEL) {
13297c478bd9Sstevel@tonic-gate 			high_plevels++;
13307c478bd9Sstevel@tonic-gate 		} else {
13317c478bd9Sstevel@tonic-gate 			other_plevels++;
13327c478bd9Sstevel@tonic-gate 		}
13337c478bd9Sstevel@tonic-gate 	}
13347c478bd9Sstevel@tonic-gate 
13357c478bd9Sstevel@tonic-gate 	/*
13367c478bd9Sstevel@tonic-gate 	 * bail out if high level events are mixed with other types in this
13377c478bd9Sstevel@tonic-gate 	 * event set or the set is incompatible with the set in the handle
13387c478bd9Sstevel@tonic-gate 	 */
13397c478bd9Sstevel@tonic-gate 	if ((high_plevels && other_plevels) ||
13407c478bd9Sstevel@tonic-gate 	    (other_plevels && ndi_event_hdl->ndi_evthdl_high_plevels) ||
13417c478bd9Sstevel@tonic-gate 	    (high_plevels && ndi_event_hdl->ndi_evthdl_other_plevels)) {
13427c478bd9Sstevel@tonic-gate 		mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
13437c478bd9Sstevel@tonic-gate 
13447c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
13457c478bd9Sstevel@tonic-gate 	}
13467c478bd9Sstevel@tonic-gate 
13477c478bd9Sstevel@tonic-gate 	/*
13487c478bd9Sstevel@tonic-gate 	 * check for duplicate events in both the existing handle
13497c478bd9Sstevel@tonic-gate 	 * and the event set, add events if not duplicates
13507c478bd9Sstevel@tonic-gate 	 */
13517c478bd9Sstevel@tonic-gate 	next = ndi_event_hdl->ndi_evthdl_cookie_list;
13527c478bd9Sstevel@tonic-gate 	for (i = 0; i < ndi_events->ndi_n_events; i++) {
13537c478bd9Sstevel@tonic-gate 		while (next != NULL) {
13547c478bd9Sstevel@tonic-gate 			len = strlen(NDI_EVENT_NAME(next)) + 1;
13557c478bd9Sstevel@tonic-gate 			if (strncmp(NDI_EVENT_NAME(next),
13567c478bd9Sstevel@tonic-gate 			    ndi_event_defs[i].ndi_event_name, len) == 0) {
13577c478bd9Sstevel@tonic-gate 				dup = 1;
13587c478bd9Sstevel@tonic-gate 				break;
13597c478bd9Sstevel@tonic-gate 			}
13607c478bd9Sstevel@tonic-gate 
13617c478bd9Sstevel@tonic-gate 			prev = next;
13627c478bd9Sstevel@tonic-gate 			next = next->next_cookie;
13637c478bd9Sstevel@tonic-gate 		}
13647c478bd9Sstevel@tonic-gate 
13657c478bd9Sstevel@tonic-gate 		if (dup == 0) {
13667c478bd9Sstevel@tonic-gate 			new_cookie = kmem_zalloc(sizeof (ndi_event_cookie_t),
13677c478bd9Sstevel@tonic-gate 			    km_flag);
13687c478bd9Sstevel@tonic-gate 
13697c478bd9Sstevel@tonic-gate 			if (!new_cookie)
13707c478bd9Sstevel@tonic-gate 				return (NDI_FAILURE);
13717c478bd9Sstevel@tonic-gate 
13727c478bd9Sstevel@tonic-gate 			if (ndi_event_hdl->ndi_evthdl_n_events == 0) {
13737c478bd9Sstevel@tonic-gate 				ndi_event_hdl->ndi_evthdl_cookie_list =
13747c478bd9Sstevel@tonic-gate 				    new_cookie;
13757c478bd9Sstevel@tonic-gate 			} else {
13767c478bd9Sstevel@tonic-gate 				prev->next_cookie = new_cookie;
13777c478bd9Sstevel@tonic-gate 			}
13787c478bd9Sstevel@tonic-gate 
13797c478bd9Sstevel@tonic-gate 			ndi_event_hdl->ndi_evthdl_n_events++;
13807c478bd9Sstevel@tonic-gate 
13817c478bd9Sstevel@tonic-gate 			/*
13827c478bd9Sstevel@tonic-gate 			 * set up new cookie
13837c478bd9Sstevel@tonic-gate 			 */
13847c478bd9Sstevel@tonic-gate 			new_cookie->definition = &ndi_event_defs[i];
13857c478bd9Sstevel@tonic-gate 			new_cookie->ddip = ndi_event_hdl->ndi_evthdl_dip;
13867c478bd9Sstevel@tonic-gate 
13877c478bd9Sstevel@tonic-gate 		} else {
13887c478bd9Sstevel@tonic-gate 			/*
13897c478bd9Sstevel@tonic-gate 			 * event not added, must correct plevel numbers
13907c478bd9Sstevel@tonic-gate 			 */
13917c478bd9Sstevel@tonic-gate 			if (ndi_event_defs[i].ndi_event_plevel ==
13927c478bd9Sstevel@tonic-gate 			    EPL_HIGHLEVEL) {
13937c478bd9Sstevel@tonic-gate 				high_plevels--;
13947c478bd9Sstevel@tonic-gate 			} else {
13957c478bd9Sstevel@tonic-gate 				other_plevels--;
13967c478bd9Sstevel@tonic-gate 			}
13977c478bd9Sstevel@tonic-gate 		}
13987c478bd9Sstevel@tonic-gate 
13997c478bd9Sstevel@tonic-gate 		dup = 0;
14007c478bd9Sstevel@tonic-gate 		next = ndi_event_hdl->ndi_evthdl_cookie_list;
14017c478bd9Sstevel@tonic-gate 		prev = NULL;
14027c478bd9Sstevel@tonic-gate 
14037c478bd9Sstevel@tonic-gate 	}
14047c478bd9Sstevel@tonic-gate 
14057c478bd9Sstevel@tonic-gate 	ndi_event_hdl->ndi_evthdl_high_plevels	+= high_plevels;
14067c478bd9Sstevel@tonic-gate 	ndi_event_hdl->ndi_evthdl_other_plevels += other_plevels;
14077c478bd9Sstevel@tonic-gate 
14087c478bd9Sstevel@tonic-gate 	ASSERT((ndi_event_hdl->ndi_evthdl_high_plevels == 0) ||
14097c478bd9Sstevel@tonic-gate 	    (ndi_event_hdl->ndi_evthdl_other_plevels == 0));
14107c478bd9Sstevel@tonic-gate 
14117c478bd9Sstevel@tonic-gate #ifdef NDI_EVENT_DEBUG
14127c478bd9Sstevel@tonic-gate 	if (ndi_event_debug) {
14137c478bd9Sstevel@tonic-gate 		ndi_event_dump_hdl(ndi_event_hdl, "ndi_event_bind_set");
14147c478bd9Sstevel@tonic-gate 	}
14157c478bd9Sstevel@tonic-gate #endif /* NDI_EVENT_DEBUG */
14167c478bd9Sstevel@tonic-gate 
14177c478bd9Sstevel@tonic-gate 	mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
14187c478bd9Sstevel@tonic-gate 
14197c478bd9Sstevel@tonic-gate 	return (NDI_SUCCESS);
14207c478bd9Sstevel@tonic-gate }
14217c478bd9Sstevel@tonic-gate 
14227c478bd9Sstevel@tonic-gate /*
14237c478bd9Sstevel@tonic-gate  * ndi_event_unbind_set() unbinds a set of events, previously
14247c478bd9Sstevel@tonic-gate  * bound using ndi_event_bind_set(), from the NDI event
14257c478bd9Sstevel@tonic-gate  * handle.
14267c478bd9Sstevel@tonic-gate  *
14277c478bd9Sstevel@tonic-gate  * This routine will unbind all events in the event set.  If an event,
14287c478bd9Sstevel@tonic-gate  * specified in the event set, is not found in the handle, this
14297c478bd9Sstevel@tonic-gate  * routine will proceed onto the next member of the set as if the event
14307c478bd9Sstevel@tonic-gate  * was never specified.
14317c478bd9Sstevel@tonic-gate  *
14327c478bd9Sstevel@tonic-gate  * The event set may be a subset of the set of events that
14337c478bd9Sstevel@tonic-gate  * was previously bound to the handle. For example, events
14347c478bd9Sstevel@tonic-gate  * can be individually unbound.
14357c478bd9Sstevel@tonic-gate  *
14367c478bd9Sstevel@tonic-gate  * An event cannot be unbound if callbacks are still
14377c478bd9Sstevel@tonic-gate  * registered against the event.
14387c478bd9Sstevel@tonic-gate  */
14397c478bd9Sstevel@tonic-gate /*ARGSUSED*/
14407c478bd9Sstevel@tonic-gate int
14417c478bd9Sstevel@tonic-gate ndi_event_unbind_set(ndi_event_hdl_t   handle, ndi_event_set_t	*ndi_events,
14427c478bd9Sstevel@tonic-gate     uint_t flag)
14437c478bd9Sstevel@tonic-gate {
14447c478bd9Sstevel@tonic-gate 	ndi_event_definition_t	*ndi_event_defs;
14457c478bd9Sstevel@tonic-gate 	int			len;
14467c478bd9Sstevel@tonic-gate 	uint_t			i;
14477c478bd9Sstevel@tonic-gate 	int			rval;
14487c478bd9Sstevel@tonic-gate 	ndi_event_cookie_t *cookie_list;
14497c478bd9Sstevel@tonic-gate 	ndi_event_cookie_t *prev = NULL;
14507c478bd9Sstevel@tonic-gate 
14517c478bd9Sstevel@tonic-gate 	ASSERT(ndi_events);
14527c478bd9Sstevel@tonic-gate 	ASSERT(handle);
14537c478bd9Sstevel@tonic-gate 
14547c478bd9Sstevel@tonic-gate 	/*
14557c478bd9Sstevel@tonic-gate 	 * binding must be performed during attach/detac
14567c478bd9Sstevel@tonic-gate 	 */
14577c478bd9Sstevel@tonic-gate 	if (!DEVI_IS_ATTACHING(handle->ndi_evthdl_dip) &&
14587c478bd9Sstevel@tonic-gate 	    !DEVI_IS_DETACHING(handle->ndi_evthdl_dip)) {
14597c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "ndi_event_bind_set must be called within "
14607c478bd9Sstevel@tonic-gate 		    "attach or detach");
14617c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
14627c478bd9Sstevel@tonic-gate 	}
14637c478bd9Sstevel@tonic-gate 
14647c478bd9Sstevel@tonic-gate 	/* bail out if ndi_event_set is outdated */
14657c478bd9Sstevel@tonic-gate 	if (ndi_events->ndi_events_version != NDI_EVENTS_REV1) {
14667c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
14677c478bd9Sstevel@tonic-gate 	}
14687c478bd9Sstevel@tonic-gate 
14697c478bd9Sstevel@tonic-gate 	ASSERT(ndi_events->ndi_event_defs);
14707c478bd9Sstevel@tonic-gate 
14717c478bd9Sstevel@tonic-gate 	ndi_event_defs = ndi_events->ndi_event_defs;
14727c478bd9Sstevel@tonic-gate 
14737c478bd9Sstevel@tonic-gate 	mutex_enter(&handle->ndi_evthdl_mutex);
14747c478bd9Sstevel@tonic-gate 	mutex_enter(&handle->ndi_evthdl_cb_mutex);
14757c478bd9Sstevel@tonic-gate 
14767c478bd9Sstevel@tonic-gate 	/*
14777c478bd9Sstevel@tonic-gate 	 * Verify that all events in the event set are eligible
14787c478bd9Sstevel@tonic-gate 	 * for unbinding(ie. there are no outstanding callbacks).
14797c478bd9Sstevel@tonic-gate 	 * If any one of the events are ineligible, fail entire
14807c478bd9Sstevel@tonic-gate 	 * operation.
14817c478bd9Sstevel@tonic-gate 	 */
14827c478bd9Sstevel@tonic-gate 
14837c478bd9Sstevel@tonic-gate 	for (i = 0; i < ndi_events->ndi_n_events; i++) {
14847c478bd9Sstevel@tonic-gate 		cookie_list = handle->ndi_evthdl_cookie_list;
14857c478bd9Sstevel@tonic-gate 		while (cookie_list != NULL) {
14867c478bd9Sstevel@tonic-gate 			len = strlen(NDI_EVENT_NAME(cookie_list)) + 1;
14877c478bd9Sstevel@tonic-gate 			if (strncmp(NDI_EVENT_NAME(cookie_list),
14887c478bd9Sstevel@tonic-gate 			    ndi_event_defs[i].ndi_event_name, len) == 0) {
14897c478bd9Sstevel@tonic-gate 
14907c478bd9Sstevel@tonic-gate 			    ASSERT(cookie_list->callback_list == NULL);
14917c478bd9Sstevel@tonic-gate 			    if (cookie_list->callback_list) {
14927c478bd9Sstevel@tonic-gate 				    rval = NDI_FAILURE;
14937c478bd9Sstevel@tonic-gate 				    goto done;
14947c478bd9Sstevel@tonic-gate 			    }
14957c478bd9Sstevel@tonic-gate 			    break;
14967c478bd9Sstevel@tonic-gate 			} else {
14977c478bd9Sstevel@tonic-gate 				cookie_list = cookie_list->next_cookie;
14987c478bd9Sstevel@tonic-gate 			}
14997c478bd9Sstevel@tonic-gate 		}
15007c478bd9Sstevel@tonic-gate 	}
15017c478bd9Sstevel@tonic-gate 
15027c478bd9Sstevel@tonic-gate 	/*
15037c478bd9Sstevel@tonic-gate 	 * remove all events found within the handle
15047c478bd9Sstevel@tonic-gate 	 * If an event is not found, this function will proceed as if the event
15057c478bd9Sstevel@tonic-gate 	 * was never specified.
15067c478bd9Sstevel@tonic-gate 	 */
15077c478bd9Sstevel@tonic-gate 
15087c478bd9Sstevel@tonic-gate 	for (i = 0; i < ndi_events->ndi_n_events; i++) {
15097c478bd9Sstevel@tonic-gate 		cookie_list = handle->ndi_evthdl_cookie_list;
15107c478bd9Sstevel@tonic-gate 		prev = NULL;
15117c478bd9Sstevel@tonic-gate 		while (cookie_list != NULL) {
15127c478bd9Sstevel@tonic-gate 			len = strlen(NDI_EVENT_NAME(cookie_list)) + 1;
15137c478bd9Sstevel@tonic-gate 			if (strncmp(NDI_EVENT_NAME(cookie_list),
15147c478bd9Sstevel@tonic-gate 			    ndi_event_defs[i].ndi_event_name, len) == 0) {
15157c478bd9Sstevel@tonic-gate 
15167c478bd9Sstevel@tonic-gate 				/*
15177c478bd9Sstevel@tonic-gate 				 * can not unbind an event definition with
15187c478bd9Sstevel@tonic-gate 				 * outstanding callbacks
15197c478bd9Sstevel@tonic-gate 				 */
15207c478bd9Sstevel@tonic-gate 				if (cookie_list->callback_list) {
15217c478bd9Sstevel@tonic-gate 					rval = NDI_FAILURE;
15227c478bd9Sstevel@tonic-gate 					goto done;
15237c478bd9Sstevel@tonic-gate 				}
15247c478bd9Sstevel@tonic-gate 
15257c478bd9Sstevel@tonic-gate 				/* remove this cookie from the list */
15267c478bd9Sstevel@tonic-gate 				if (prev != NULL) {
15277c478bd9Sstevel@tonic-gate 					prev->next_cookie =
15287c478bd9Sstevel@tonic-gate 					    cookie_list->next_cookie;
15297c478bd9Sstevel@tonic-gate 				} else {
15307c478bd9Sstevel@tonic-gate 					handle->ndi_evthdl_cookie_list =
15317c478bd9Sstevel@tonic-gate 					    cookie_list->next_cookie;
15327c478bd9Sstevel@tonic-gate 				}
15337c478bd9Sstevel@tonic-gate 
15347c478bd9Sstevel@tonic-gate 				/* adjust plevel counts */
15357c478bd9Sstevel@tonic-gate 				if (NDI_EVENT_PLEVEL(cookie_list) ==
15367c478bd9Sstevel@tonic-gate 				    EPL_HIGHLEVEL) {
15377c478bd9Sstevel@tonic-gate 					handle->ndi_evthdl_high_plevels--;
15387c478bd9Sstevel@tonic-gate 				} else {
15397c478bd9Sstevel@tonic-gate 					handle->ndi_evthdl_other_plevels--;
15407c478bd9Sstevel@tonic-gate 				}
15417c478bd9Sstevel@tonic-gate 
15427c478bd9Sstevel@tonic-gate 				/* adjust cookie count */
15437c478bd9Sstevel@tonic-gate 				handle->ndi_evthdl_n_events--;
15447c478bd9Sstevel@tonic-gate 
15457c478bd9Sstevel@tonic-gate 				/* free the cookie */
15467c478bd9Sstevel@tonic-gate 				kmem_free(cookie_list,
15477c478bd9Sstevel@tonic-gate 				    sizeof (ndi_event_cookie_t));
15487c478bd9Sstevel@tonic-gate 
15497c478bd9Sstevel@tonic-gate 				cookie_list = handle->ndi_evthdl_cookie_list;
15507c478bd9Sstevel@tonic-gate 				break;
15517c478bd9Sstevel@tonic-gate 
15527c478bd9Sstevel@tonic-gate 			} else {
15537c478bd9Sstevel@tonic-gate 				prev = cookie_list;
15547c478bd9Sstevel@tonic-gate 				cookie_list = cookie_list->next_cookie;
15557c478bd9Sstevel@tonic-gate 			}
15567c478bd9Sstevel@tonic-gate 
15577c478bd9Sstevel@tonic-gate 		}
15587c478bd9Sstevel@tonic-gate 
15597c478bd9Sstevel@tonic-gate 	}
15607c478bd9Sstevel@tonic-gate 
15617c478bd9Sstevel@tonic-gate #ifdef NDI_EVENT_DEBUG
15627c478bd9Sstevel@tonic-gate 	if (ndi_event_debug) {
15637c478bd9Sstevel@tonic-gate 		ndi_event_dump_hdl(handle, "ndi_event_unbind_set");
15647c478bd9Sstevel@tonic-gate 	}
15657c478bd9Sstevel@tonic-gate #endif /* NDI_EVENT_DEBUG */
15667c478bd9Sstevel@tonic-gate 
15677c478bd9Sstevel@tonic-gate 	rval = NDI_SUCCESS;
15687c478bd9Sstevel@tonic-gate 
15697c478bd9Sstevel@tonic-gate done:
15707c478bd9Sstevel@tonic-gate 	mutex_exit(&handle->ndi_evthdl_cb_mutex);
15717c478bd9Sstevel@tonic-gate 	mutex_exit(&handle->ndi_evthdl_mutex);
15727c478bd9Sstevel@tonic-gate 
15737c478bd9Sstevel@tonic-gate 	return (rval);
15747c478bd9Sstevel@tonic-gate }
15757c478bd9Sstevel@tonic-gate 
15767c478bd9Sstevel@tonic-gate /*
15777c478bd9Sstevel@tonic-gate  * ndi_event_retrieve_cookie():
15787c478bd9Sstevel@tonic-gate  * Return an event cookie for eventname if this nexus driver
15797c478bd9Sstevel@tonic-gate  * has defined the named event. The event cookie returned
15807c478bd9Sstevel@tonic-gate  * by this function is used to register callback handlers
15817c478bd9Sstevel@tonic-gate  * for the event.
15827c478bd9Sstevel@tonic-gate  *
15837c478bd9Sstevel@tonic-gate  * ndi_event_retrieve_cookie() is intended to be used in the
15847c478bd9Sstevel@tonic-gate  * nexus driver's bus_get_eventcookie busop routine.
15857c478bd9Sstevel@tonic-gate  *
15867c478bd9Sstevel@tonic-gate  * If the event is not defined by this bus nexus driver, and flag
15877c478bd9Sstevel@tonic-gate  * does not include NDI_EVENT_NOPASS, then ndi_event_retrieve_cookie()
15887c478bd9Sstevel@tonic-gate  * will pass the request up the device tree hierarchy by calling
15897c478bd9Sstevel@tonic-gate  * ndi_busop_get_eventcookie(9N).
15907c478bd9Sstevel@tonic-gate  * If the event is not defined by this bus nexus driver, and flag
15917c478bd9Sstevel@tonic-gate  * does include NDI_EVENT_NOPASS, ndi_event_retrieve_cookie()
15927c478bd9Sstevel@tonic-gate  * will return NDI_FAILURE.  The caller may then determine what further
15937c478bd9Sstevel@tonic-gate  * action to take, such as using a different handle, passing the
15947c478bd9Sstevel@tonic-gate  * request up the device tree using ndi_busop_get_eventcookie(9N),
15957c478bd9Sstevel@tonic-gate  * or returning the failure to the caller, thus blocking the
15967c478bd9Sstevel@tonic-gate  * progress of the request up the tree.
15977c478bd9Sstevel@tonic-gate  */
15987c478bd9Sstevel@tonic-gate int
15997c478bd9Sstevel@tonic-gate ndi_event_retrieve_cookie(ndi_event_hdl_t handle,
16007c478bd9Sstevel@tonic-gate 	dev_info_t		*rdip,
16017c478bd9Sstevel@tonic-gate 	char			*eventname,
16027c478bd9Sstevel@tonic-gate 	ddi_eventcookie_t	*cookiep,
16037c478bd9Sstevel@tonic-gate 	uint_t			flag)
16047c478bd9Sstevel@tonic-gate {
16057c478bd9Sstevel@tonic-gate 	struct ndi_event_hdl *ndi_event_hdl = (struct ndi_event_hdl *)handle;
16067c478bd9Sstevel@tonic-gate 	int		len;
16077c478bd9Sstevel@tonic-gate 	ndi_event_cookie_t *cookie_list;
16087c478bd9Sstevel@tonic-gate 
16097c478bd9Sstevel@tonic-gate 	mutex_enter(&ndi_event_hdl->ndi_evthdl_mutex);
16107c478bd9Sstevel@tonic-gate 
16117c478bd9Sstevel@tonic-gate 	cookie_list = ndi_event_hdl->ndi_evthdl_cookie_list;
16127c478bd9Sstevel@tonic-gate 	/*
16137c478bd9Sstevel@tonic-gate 	 * search the cookie list for the event name and return
16147c478bd9Sstevel@tonic-gate 	 * cookie if found.
16157c478bd9Sstevel@tonic-gate 	 */
16167c478bd9Sstevel@tonic-gate 	while (cookie_list != NULL) {
16177c478bd9Sstevel@tonic-gate 
16187c478bd9Sstevel@tonic-gate 		len = strlen(NDI_EVENT_NAME(cookie_list)) + 1;
16197c478bd9Sstevel@tonic-gate 		if (strncmp(NDI_EVENT_NAME(cookie_list), eventname,
16207c478bd9Sstevel@tonic-gate 		    len) == 0) {
16217c478bd9Sstevel@tonic-gate 			*cookiep = (ddi_eventcookie_t)cookie_list;
16227c478bd9Sstevel@tonic-gate 
16237c478bd9Sstevel@tonic-gate 			mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
16247c478bd9Sstevel@tonic-gate 			return (NDI_SUCCESS);
16257c478bd9Sstevel@tonic-gate 		}
16267c478bd9Sstevel@tonic-gate 
16277c478bd9Sstevel@tonic-gate 		cookie_list = cookie_list->next_cookie;
16287c478bd9Sstevel@tonic-gate 	}
16297c478bd9Sstevel@tonic-gate 
16307c478bd9Sstevel@tonic-gate 	mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
16317c478bd9Sstevel@tonic-gate 	/*
16327c478bd9Sstevel@tonic-gate 	 * event was not found, pass up or return failure
16337c478bd9Sstevel@tonic-gate 	 */
16347c478bd9Sstevel@tonic-gate 	if ((flag & NDI_EVENT_NOPASS) == 0) {
16357c478bd9Sstevel@tonic-gate 		return (ndi_busop_get_eventcookie(
16367c478bd9Sstevel@tonic-gate 			ndi_event_hdl->ndi_evthdl_dip, rdip,
16377c478bd9Sstevel@tonic-gate 			eventname, cookiep));
16387c478bd9Sstevel@tonic-gate 	} else {
16397c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
16407c478bd9Sstevel@tonic-gate 	}
16417c478bd9Sstevel@tonic-gate }
16427c478bd9Sstevel@tonic-gate 
16437c478bd9Sstevel@tonic-gate /*
16447c478bd9Sstevel@tonic-gate  * check whether this nexus defined this event and look up attributes
16457c478bd9Sstevel@tonic-gate  */
16467c478bd9Sstevel@tonic-gate static int
16477c478bd9Sstevel@tonic-gate ndi_event_is_defined(ndi_event_hdl_t handle,
16487c478bd9Sstevel@tonic-gate 	ddi_eventcookie_t cookie, int *attributes)
16497c478bd9Sstevel@tonic-gate {
16507c478bd9Sstevel@tonic-gate 
16517c478bd9Sstevel@tonic-gate 	struct ndi_event_hdl *ndi_event_hdl = (struct ndi_event_hdl *)handle;
16527c478bd9Sstevel@tonic-gate 	ndi_event_cookie_t *cookie_list;
16537c478bd9Sstevel@tonic-gate 
16547c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&handle->ndi_evthdl_mutex));
16557c478bd9Sstevel@tonic-gate 
16567c478bd9Sstevel@tonic-gate 	cookie_list = ndi_event_hdl->ndi_evthdl_cookie_list;
16577c478bd9Sstevel@tonic-gate 	while (cookie_list != NULL) {
16587c478bd9Sstevel@tonic-gate 		if (cookie_list == NDI_EVENT(cookie)) {
16597c478bd9Sstevel@tonic-gate 			if (attributes)
16607c478bd9Sstevel@tonic-gate 				*attributes =
16617c478bd9Sstevel@tonic-gate 				    NDI_EVENT_ATTRIBUTES(cookie_list);
16627c478bd9Sstevel@tonic-gate 
16637c478bd9Sstevel@tonic-gate 			return (NDI_SUCCESS);
16647c478bd9Sstevel@tonic-gate 		}
16657c478bd9Sstevel@tonic-gate 
16667c478bd9Sstevel@tonic-gate 		cookie_list = cookie_list->next_cookie;
16677c478bd9Sstevel@tonic-gate 	}
16687c478bd9Sstevel@tonic-gate 
16697c478bd9Sstevel@tonic-gate 	return (NDI_FAILURE);
16707c478bd9Sstevel@tonic-gate }
16717c478bd9Sstevel@tonic-gate 
16727c478bd9Sstevel@tonic-gate /*
16737c478bd9Sstevel@tonic-gate  * ndi_event_add_callback(): adds an event callback registration
16747c478bd9Sstevel@tonic-gate  * to the event cookie defining this event.
16757c478bd9Sstevel@tonic-gate  *
16767c478bd9Sstevel@tonic-gate  * Refer also to bus_add_eventcall(9n) and ndi_busop_add_eventcall(9n).
16777c478bd9Sstevel@tonic-gate  *
16787c478bd9Sstevel@tonic-gate  * ndi_event_add_callback(9n) is intended to be used in
16797c478bd9Sstevel@tonic-gate  * the nexus driver's bus_add_eventcall(9n) busop function.
16807c478bd9Sstevel@tonic-gate  *
16817c478bd9Sstevel@tonic-gate  * If the event is not defined by this bus nexus driver,
16827c478bd9Sstevel@tonic-gate  * ndi_event_add_callback() will return NDI_FAILURE.
16837c478bd9Sstevel@tonic-gate  */
16847c478bd9Sstevel@tonic-gate int
16857c478bd9Sstevel@tonic-gate ndi_event_add_callback(ndi_event_hdl_t handle, dev_info_t *child_dip,
16867c478bd9Sstevel@tonic-gate 	ddi_eventcookie_t cookie,
16877c478bd9Sstevel@tonic-gate 	void		(*event_callback)(dev_info_t *,
16887c478bd9Sstevel@tonic-gate 			ddi_eventcookie_t, void *arg, void *impldata),
16897c478bd9Sstevel@tonic-gate 	void		*arg,
16907c478bd9Sstevel@tonic-gate 	uint_t		flag,
16917c478bd9Sstevel@tonic-gate 	ddi_callback_id_t *cb_id)
16927c478bd9Sstevel@tonic-gate {
16937c478bd9Sstevel@tonic-gate 	struct ndi_event_hdl *ndi_event_hdl = (struct ndi_event_hdl *)handle;
16947c478bd9Sstevel@tonic-gate 	int km_flag = ((flag & NDI_NOSLEEP) ? KM_NOSLEEP : KM_SLEEP);
16957c478bd9Sstevel@tonic-gate 	ndi_event_callbacks_t *cb;
16967c478bd9Sstevel@tonic-gate 
16977c478bd9Sstevel@tonic-gate 	mutex_enter(&ndi_event_hdl->ndi_evthdl_mutex);
16987c478bd9Sstevel@tonic-gate 
16997c478bd9Sstevel@tonic-gate 	/*
17007c478bd9Sstevel@tonic-gate 	 * if the event was not bound to this handle, return failure
17017c478bd9Sstevel@tonic-gate 	 */
17027c478bd9Sstevel@tonic-gate 	if (ndi_event_is_defined(handle, cookie, NULL) != NDI_SUCCESS) {
17037c478bd9Sstevel@tonic-gate 
17047c478bd9Sstevel@tonic-gate 		mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
17057c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
17067c478bd9Sstevel@tonic-gate 
17077c478bd9Sstevel@tonic-gate 	}
17087c478bd9Sstevel@tonic-gate 
17097c478bd9Sstevel@tonic-gate 	mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
17107c478bd9Sstevel@tonic-gate 
17117c478bd9Sstevel@tonic-gate 	/*
17127c478bd9Sstevel@tonic-gate 	 * allocate space for a callback structure
17137c478bd9Sstevel@tonic-gate 	 */
17147c478bd9Sstevel@tonic-gate 	cb = kmem_zalloc(sizeof (ndi_event_callbacks_t), km_flag);
17157c478bd9Sstevel@tonic-gate 	if (cb == NULL) {
17167c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
17177c478bd9Sstevel@tonic-gate 	}
17187c478bd9Sstevel@tonic-gate 
17197c478bd9Sstevel@tonic-gate 	mutex_enter(&ndi_event_hdl->ndi_evthdl_mutex);
17207c478bd9Sstevel@tonic-gate 
17217c478bd9Sstevel@tonic-gate 	/* initialize callback structure */
17227c478bd9Sstevel@tonic-gate 	cb->ndi_evtcb_dip	= child_dip;
17237c478bd9Sstevel@tonic-gate 	cb->ndi_evtcb_callback	= event_callback;
17247c478bd9Sstevel@tonic-gate 	cb->ndi_evtcb_arg	= arg;
17257c478bd9Sstevel@tonic-gate 	cb->ndi_evtcb_cookie	= cookie;
17267c478bd9Sstevel@tonic-gate 	cb->devname 		= (char *)ddi_driver_name(child_dip);
17277c478bd9Sstevel@tonic-gate 
17287c478bd9Sstevel@tonic-gate 	*cb_id = (ddi_callback_id_t)cb;
17297c478bd9Sstevel@tonic-gate 	mutex_enter(&ndi_event_hdl->ndi_evthdl_cb_mutex);
17307c478bd9Sstevel@tonic-gate 
17317c478bd9Sstevel@tonic-gate 	/* add this callback structure to the list */
17327c478bd9Sstevel@tonic-gate 	if (NDI_EVENT(cookie)->callback_list) {
17337c478bd9Sstevel@tonic-gate 		cb->ndi_evtcb_next = NDI_EVENT(cookie)->callback_list;
17347c478bd9Sstevel@tonic-gate 		NDI_EVENT(cookie)->callback_list->ndi_evtcb_prev = cb;
17357c478bd9Sstevel@tonic-gate 		NDI_EVENT(cookie)->callback_list = cb;
17367c478bd9Sstevel@tonic-gate 	} else {
17377c478bd9Sstevel@tonic-gate 		NDI_EVENT(cookie)->callback_list = cb;
17387c478bd9Sstevel@tonic-gate 	}
17397c478bd9Sstevel@tonic-gate #ifdef NDI_EVENT_DEBUG
17407c478bd9Sstevel@tonic-gate 	if (ndi_event_debug) {
17417c478bd9Sstevel@tonic-gate 		ndi_event_dump_hdl(ndi_event_hdl, "ndi_event_add_callback");
17427c478bd9Sstevel@tonic-gate 	}
17437c478bd9Sstevel@tonic-gate #endif /* NDI_EVENT_DEBUG */
17447c478bd9Sstevel@tonic-gate 
17457c478bd9Sstevel@tonic-gate 	mutex_exit(&ndi_event_hdl->ndi_evthdl_cb_mutex);
17467c478bd9Sstevel@tonic-gate 	mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
17477c478bd9Sstevel@tonic-gate 
17487c478bd9Sstevel@tonic-gate 	return (NDI_SUCCESS);
17497c478bd9Sstevel@tonic-gate }
17507c478bd9Sstevel@tonic-gate 
17517c478bd9Sstevel@tonic-gate /*
17527c478bd9Sstevel@tonic-gate  * ndi_event_remove_callback():
17537c478bd9Sstevel@tonic-gate  *
17547c478bd9Sstevel@tonic-gate  * ndi_event_remove_callback() removes a callback that was
17557c478bd9Sstevel@tonic-gate  * previously registered using ndi_event_add_callback(9N).
17567c478bd9Sstevel@tonic-gate  * Refer also to bus_remove_eventcall(9n) and
17577c478bd9Sstevel@tonic-gate  * ndi_busop_remove_eventcall(9n).
17587c478bd9Sstevel@tonic-gate  * ndi_event_remove_callback(9n) is intended to be used in
17597c478bd9Sstevel@tonic-gate  * the nexus driver's bus_remove_eventcall (9n) busop function.
17607c478bd9Sstevel@tonic-gate  * If the event is not defined by this bus nexus driver,
17617c478bd9Sstevel@tonic-gate  * ndi_event_remove_callback() will return NDI_FAILURE.
17627c478bd9Sstevel@tonic-gate  */
17637c478bd9Sstevel@tonic-gate static void do_ndi_event_remove_callback(struct ndi_event_hdl *ndi_event_hdl,
17647c478bd9Sstevel@tonic-gate 	ddi_callback_id_t cb_id);
17657c478bd9Sstevel@tonic-gate 
17667c478bd9Sstevel@tonic-gate int
17677c478bd9Sstevel@tonic-gate ndi_event_remove_callback(ndi_event_hdl_t handle, ddi_callback_id_t cb_id)
17687c478bd9Sstevel@tonic-gate {
17697c478bd9Sstevel@tonic-gate 	struct ndi_event_hdl *ndi_event_hdl = (struct ndi_event_hdl *)handle;
17707c478bd9Sstevel@tonic-gate 
17717c478bd9Sstevel@tonic-gate 	ASSERT(cb_id);
17727c478bd9Sstevel@tonic-gate 
17737c478bd9Sstevel@tonic-gate 	mutex_enter(&ndi_event_hdl->ndi_evthdl_mutex);
17747c478bd9Sstevel@tonic-gate 	mutex_enter(&ndi_event_hdl->ndi_evthdl_cb_mutex);
17757c478bd9Sstevel@tonic-gate 
17767c478bd9Sstevel@tonic-gate 	do_ndi_event_remove_callback(ndi_event_hdl, cb_id);
17777c478bd9Sstevel@tonic-gate 
17787c478bd9Sstevel@tonic-gate 	mutex_exit(&ndi_event_hdl->ndi_evthdl_cb_mutex);
17797c478bd9Sstevel@tonic-gate 	mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
17807c478bd9Sstevel@tonic-gate 
17817c478bd9Sstevel@tonic-gate 	return (NDI_SUCCESS);
17827c478bd9Sstevel@tonic-gate }
17837c478bd9Sstevel@tonic-gate 
17847c478bd9Sstevel@tonic-gate /*ARGSUSED*/
17857c478bd9Sstevel@tonic-gate static void
17867c478bd9Sstevel@tonic-gate do_ndi_event_remove_callback(struct ndi_event_hdl *ndi_event_hdl,
17877c478bd9Sstevel@tonic-gate     ddi_callback_id_t cb_id)
17887c478bd9Sstevel@tonic-gate {
17897c478bd9Sstevel@tonic-gate 	ndi_event_callbacks_t *cb = (ndi_event_callbacks_t *)cb_id;
17907c478bd9Sstevel@tonic-gate 	ASSERT(cb);
17917c478bd9Sstevel@tonic-gate 
17927c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&ndi_event_hdl->ndi_evthdl_mutex));
17937c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&ndi_event_hdl->ndi_evthdl_cb_mutex));
17947c478bd9Sstevel@tonic-gate 
17957c478bd9Sstevel@tonic-gate 	/* remove from callback linked list */
17967c478bd9Sstevel@tonic-gate 	if (cb->ndi_evtcb_prev) {
17977c478bd9Sstevel@tonic-gate 		cb->ndi_evtcb_prev->ndi_evtcb_next = cb->ndi_evtcb_next;
17987c478bd9Sstevel@tonic-gate 	}
17997c478bd9Sstevel@tonic-gate 
18007c478bd9Sstevel@tonic-gate 	if (cb->ndi_evtcb_next) {
18017c478bd9Sstevel@tonic-gate 		cb->ndi_evtcb_next->ndi_evtcb_prev = cb->ndi_evtcb_prev;
18027c478bd9Sstevel@tonic-gate 	}
18037c478bd9Sstevel@tonic-gate 
18047c478bd9Sstevel@tonic-gate 	if (NDI_EVENT(cb->ndi_evtcb_cookie)->callback_list == cb) {
18057c478bd9Sstevel@tonic-gate 		NDI_EVENT(cb->ndi_evtcb_cookie)->callback_list =
18067c478bd9Sstevel@tonic-gate 		    cb->ndi_evtcb_next;
18077c478bd9Sstevel@tonic-gate 	}
18087c478bd9Sstevel@tonic-gate 
18097c478bd9Sstevel@tonic-gate 	kmem_free(cb, sizeof (ndi_event_callbacks_t));
18107c478bd9Sstevel@tonic-gate }
18117c478bd9Sstevel@tonic-gate 
18127c478bd9Sstevel@tonic-gate /*
18137c478bd9Sstevel@tonic-gate  * ndi_event_run_callbacks() performs event callbacks for the event
18147c478bd9Sstevel@tonic-gate  * specified by cookie, if this is among those bound to the
18157c478bd9Sstevel@tonic-gate  * supplied handle.
18167c478bd9Sstevel@tonic-gate  * If the event is among those bound to the handle, none,
18177c478bd9Sstevel@tonic-gate  * some, or all of the handlers registered for the event
18187c478bd9Sstevel@tonic-gate  * will be called, according to the delivery attributes of
18197c478bd9Sstevel@tonic-gate  * the event.
18207c478bd9Sstevel@tonic-gate  * If the event attributes include NDI_EVENT_POST_TO_ALL
18217c478bd9Sstevel@tonic-gate  * (the default), all the handlers for the event will be
18227c478bd9Sstevel@tonic-gate  * called in an unspecified order.
18237c478bd9Sstevel@tonic-gate  * If the event attributes include NDI_EVENT_POST_TO_TGT, only
18247c478bd9Sstevel@tonic-gate  * the handlers (if any) registered by the driver identified by
18257c478bd9Sstevel@tonic-gate  * rdip will be called.
18267c478bd9Sstevel@tonic-gate  * If the event identified by cookie is not bound to the handle,
18277c478bd9Sstevel@tonic-gate  * NDI_FAILURE will be returned.
18287c478bd9Sstevel@tonic-gate  */
18297c478bd9Sstevel@tonic-gate int
18307c478bd9Sstevel@tonic-gate ndi_event_run_callbacks(ndi_event_hdl_t handle, dev_info_t *child_dip,
18317c478bd9Sstevel@tonic-gate 	ddi_eventcookie_t cookie, void *bus_impldata)
18327c478bd9Sstevel@tonic-gate {
18337c478bd9Sstevel@tonic-gate 	struct ndi_event_hdl *ndi_event_hdl = (struct ndi_event_hdl *)handle;
18347c478bd9Sstevel@tonic-gate 	ndi_event_callbacks_t *next, *cb;
18357c478bd9Sstevel@tonic-gate 	int attributes;
18367c478bd9Sstevel@tonic-gate 
18377c478bd9Sstevel@tonic-gate 	mutex_enter(&ndi_event_hdl->ndi_evthdl_mutex);
18387c478bd9Sstevel@tonic-gate 
18397c478bd9Sstevel@tonic-gate 	/* if this is not our event, fail */
18407c478bd9Sstevel@tonic-gate 	if (ndi_event_is_defined(handle, cookie, &attributes) !=
18417c478bd9Sstevel@tonic-gate 	    NDI_SUCCESS) {
18427c478bd9Sstevel@tonic-gate 
18437c478bd9Sstevel@tonic-gate 		mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
18447c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
18457c478bd9Sstevel@tonic-gate 	}
18467c478bd9Sstevel@tonic-gate 
18477c478bd9Sstevel@tonic-gate 	mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
18487c478bd9Sstevel@tonic-gate 
18497c478bd9Sstevel@tonic-gate #ifdef NDI_EVENT_DEBUG
18507c478bd9Sstevel@tonic-gate 	if (ndi_event_debug) {
18517c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "ndi_event_run_callbacks:\n\t"
18527c478bd9Sstevel@tonic-gate 		    "producer dip=%p (%s%d): cookie = %p, name = %s\n",
18537c478bd9Sstevel@tonic-gate 		    (void *)ndi_event_hdl->ndi_evthdl_dip,
18547c478bd9Sstevel@tonic-gate 		    ddi_node_name(ndi_event_hdl->ndi_evthdl_dip),
18557c478bd9Sstevel@tonic-gate 		    ddi_get_instance(ndi_event_hdl->ndi_evthdl_dip),
18567c478bd9Sstevel@tonic-gate 		    (void *)cookie,
18577c478bd9Sstevel@tonic-gate 		    ndi_event_cookie_to_name(handle, cookie));
18587c478bd9Sstevel@tonic-gate 	}
18597c478bd9Sstevel@tonic-gate #endif /* #ifdef NDI_EVENT_DEBUG */
18607c478bd9Sstevel@tonic-gate 
18617c478bd9Sstevel@tonic-gate 
18627c478bd9Sstevel@tonic-gate 	/*
18637c478bd9Sstevel@tonic-gate 	 * The callback handlers may call conversion functions.  The conversion
18647c478bd9Sstevel@tonic-gate 	 * functions may hold the ndi_evthdl_mutex during execution.  Thus, to
18657c478bd9Sstevel@tonic-gate 	 * avoid a recursive mutex problem, only the ndi_evthdl_cb_mutex is
18667c478bd9Sstevel@tonic-gate 	 * held.  The ndi_evthdl_mutex is not held when running the callbacks.
18677c478bd9Sstevel@tonic-gate 	 */
18687c478bd9Sstevel@tonic-gate 	mutex_enter(&ndi_event_hdl->ndi_evthdl_cb_mutex);
18697c478bd9Sstevel@tonic-gate 
18707c478bd9Sstevel@tonic-gate 	/* perform callbacks */
18717c478bd9Sstevel@tonic-gate 	next = NDI_EVENT(cookie)->callback_list;
18727c478bd9Sstevel@tonic-gate 	while (next != NULL) {
18737c478bd9Sstevel@tonic-gate 
18747c478bd9Sstevel@tonic-gate 		cb = next;
18757c478bd9Sstevel@tonic-gate 		next = next->ndi_evtcb_next;
18767c478bd9Sstevel@tonic-gate 
18777c478bd9Sstevel@tonic-gate 		ASSERT(cb->ndi_evtcb_cookie == cookie);
18787c478bd9Sstevel@tonic-gate 
18797c478bd9Sstevel@tonic-gate 		if (attributes == NDI_EVENT_POST_TO_TGT &&
18807c478bd9Sstevel@tonic-gate 		    child_dip != cb->ndi_evtcb_dip) {
18817c478bd9Sstevel@tonic-gate 			continue;
18827c478bd9Sstevel@tonic-gate 		}
18837c478bd9Sstevel@tonic-gate 
18847c478bd9Sstevel@tonic-gate 		cb->ndi_evtcb_callback(cb->ndi_evtcb_dip, cb->ndi_evtcb_cookie,
18857c478bd9Sstevel@tonic-gate 		    cb->ndi_evtcb_arg, bus_impldata);
18867c478bd9Sstevel@tonic-gate 
18877c478bd9Sstevel@tonic-gate #ifdef NDI_EVENT_DEBUG
18887c478bd9Sstevel@tonic-gate 		if (ndi_event_debug) {
18897c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT,
18907c478bd9Sstevel@tonic-gate 			    "\t\tconsumer dip=%p (%s%d)\n",
18917c478bd9Sstevel@tonic-gate 			    (void *)cb->ndi_evtcb_dip,
18927c478bd9Sstevel@tonic-gate 			    ddi_node_name(cb->ndi_evtcb_dip),
18937c478bd9Sstevel@tonic-gate 			    ddi_get_instance(cb->ndi_evtcb_dip));
18947c478bd9Sstevel@tonic-gate 		}
18957c478bd9Sstevel@tonic-gate #endif
18967c478bd9Sstevel@tonic-gate 
18977c478bd9Sstevel@tonic-gate 	}
18987c478bd9Sstevel@tonic-gate 
18997c478bd9Sstevel@tonic-gate 	mutex_exit(&ndi_event_hdl->ndi_evthdl_cb_mutex);
19007c478bd9Sstevel@tonic-gate 
19017c478bd9Sstevel@tonic-gate #ifdef NDI_EVENT_DEBUG
19027c478bd9Sstevel@tonic-gate 	if (ndi_event_debug) {
19037c478bd9Sstevel@tonic-gate 		mutex_enter(&ndi_event_hdl->ndi_evthdl_mutex);
19047c478bd9Sstevel@tonic-gate 		ndi_event_dump_hdl(ndi_event_hdl, "ndi_event_run_callbacks");
19057c478bd9Sstevel@tonic-gate 		mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
19067c478bd9Sstevel@tonic-gate 	}
19077c478bd9Sstevel@tonic-gate #endif /* NDI_EVENT_DEBUG */
19087c478bd9Sstevel@tonic-gate 
19097c478bd9Sstevel@tonic-gate 	return (NDI_SUCCESS);
19107c478bd9Sstevel@tonic-gate }
19117c478bd9Sstevel@tonic-gate 
19127c478bd9Sstevel@tonic-gate 
19137c478bd9Sstevel@tonic-gate /*
19147c478bd9Sstevel@tonic-gate  * perform one callback for a specified cookie and just one target
19157c478bd9Sstevel@tonic-gate  */
19167c478bd9Sstevel@tonic-gate int
19177c478bd9Sstevel@tonic-gate ndi_event_do_callback(ndi_event_hdl_t handle, dev_info_t *child_dip,
19187c478bd9Sstevel@tonic-gate 	ddi_eventcookie_t cookie, void *bus_impldata)
19197c478bd9Sstevel@tonic-gate {
19207c478bd9Sstevel@tonic-gate 	struct ndi_event_hdl *ndi_event_hdl = (struct ndi_event_hdl *)handle;
19217c478bd9Sstevel@tonic-gate 	ndi_event_callbacks_t *next, *cb;
19227c478bd9Sstevel@tonic-gate 	int attributes;
19237c478bd9Sstevel@tonic-gate 
19247c478bd9Sstevel@tonic-gate 	mutex_enter(&ndi_event_hdl->ndi_evthdl_mutex);
19257c478bd9Sstevel@tonic-gate 
19267c478bd9Sstevel@tonic-gate 	/* if this is not our event, fail */
19277c478bd9Sstevel@tonic-gate 	if (ndi_event_is_defined(handle, cookie, &attributes) !=
19287c478bd9Sstevel@tonic-gate 	    NDI_SUCCESS) {
19297c478bd9Sstevel@tonic-gate 
19307c478bd9Sstevel@tonic-gate 		mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
19317c478bd9Sstevel@tonic-gate 
19327c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
19337c478bd9Sstevel@tonic-gate 	}
19347c478bd9Sstevel@tonic-gate 
19357c478bd9Sstevel@tonic-gate 	mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
19367c478bd9Sstevel@tonic-gate 
19377c478bd9Sstevel@tonic-gate #ifdef NDI_EVENT_DEBUG
19387c478bd9Sstevel@tonic-gate 	if (ndi_event_debug) {
19397c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "ndi_event_run_callbacks:\n\t"
19407c478bd9Sstevel@tonic-gate 		    "producer dip=%p (%s%d): cookie = %p, name = %s\n",
19417c478bd9Sstevel@tonic-gate 		    (void *)ndi_event_hdl->ndi_evthdl_dip,
19427c478bd9Sstevel@tonic-gate 		    ddi_node_name(ndi_event_hdl->ndi_evthdl_dip),
19437c478bd9Sstevel@tonic-gate 		    ddi_get_instance(ndi_event_hdl->ndi_evthdl_dip),
19447c478bd9Sstevel@tonic-gate 		    (void *)cookie,
19457c478bd9Sstevel@tonic-gate 		    ndi_event_cookie_to_name(handle, cookie));
19467c478bd9Sstevel@tonic-gate 	}
19477c478bd9Sstevel@tonic-gate #endif
19487c478bd9Sstevel@tonic-gate 
19497c478bd9Sstevel@tonic-gate 
19507c478bd9Sstevel@tonic-gate 	/*
19517c478bd9Sstevel@tonic-gate 	 * we only grab the cb mutex because the callback handlers
19527c478bd9Sstevel@tonic-gate 	 * may call the conversion functions which would cause a recursive
19537c478bd9Sstevel@tonic-gate 	 * mutex problem
19547c478bd9Sstevel@tonic-gate 	 */
19557c478bd9Sstevel@tonic-gate 	mutex_enter(&ndi_event_hdl->ndi_evthdl_cb_mutex);
19567c478bd9Sstevel@tonic-gate 
19577c478bd9Sstevel@tonic-gate 	/* perform callbacks */
19587c478bd9Sstevel@tonic-gate 	for (next = NDI_EVENT(cookie)->callback_list; next != NULL; ) {
19597c478bd9Sstevel@tonic-gate 		cb = next;
19607c478bd9Sstevel@tonic-gate 		next = next->ndi_evtcb_next;
19617c478bd9Sstevel@tonic-gate 
19627c478bd9Sstevel@tonic-gate 		if (cb->ndi_evtcb_dip == child_dip) {
19637c478bd9Sstevel@tonic-gate 			cb->ndi_evtcb_callback(cb->ndi_evtcb_dip,
19647c478bd9Sstevel@tonic-gate 			    cb->ndi_evtcb_cookie, cb->ndi_evtcb_arg,
19657c478bd9Sstevel@tonic-gate 			    bus_impldata);
19667c478bd9Sstevel@tonic-gate 
19677c478bd9Sstevel@tonic-gate #ifdef NDI_EVENT_DEBUG
19687c478bd9Sstevel@tonic-gate 			if (ndi_event_debug) {
19697c478bd9Sstevel@tonic-gate 				cmn_err(CE_CONT,
19707c478bd9Sstevel@tonic-gate 				    "\t\tconsumer dip=%p (%s%d)\n",
19717c478bd9Sstevel@tonic-gate 				    (void *)cb->ndi_evtcb_dip,
19727c478bd9Sstevel@tonic-gate 				    ddi_node_name(cb->ndi_evtcb_dip),
19737c478bd9Sstevel@tonic-gate 				    ddi_get_instance(cb->ndi_evtcb_dip));
19747c478bd9Sstevel@tonic-gate 			}
19757c478bd9Sstevel@tonic-gate #endif
19767c478bd9Sstevel@tonic-gate 			break;
19777c478bd9Sstevel@tonic-gate 		}
19787c478bd9Sstevel@tonic-gate 	}
19797c478bd9Sstevel@tonic-gate 
19807c478bd9Sstevel@tonic-gate 	mutex_exit(&ndi_event_hdl->ndi_evthdl_cb_mutex);
19817c478bd9Sstevel@tonic-gate 
19827c478bd9Sstevel@tonic-gate #ifdef NDI_EVENT_DEBUG
19837c478bd9Sstevel@tonic-gate 	if (ndi_event_debug) {
19847c478bd9Sstevel@tonic-gate 		mutex_enter(&ndi_event_hdl->ndi_evthdl_mutex);
19857c478bd9Sstevel@tonic-gate 		ndi_event_dump_hdl(ndi_event_hdl, "ndi_event_run_callbacks");
19867c478bd9Sstevel@tonic-gate 		mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
19877c478bd9Sstevel@tonic-gate 	}
19887c478bd9Sstevel@tonic-gate #endif /* NDI_EVENT_DEBUG */
19897c478bd9Sstevel@tonic-gate 
19907c478bd9Sstevel@tonic-gate 	return (NDI_SUCCESS);
19917c478bd9Sstevel@tonic-gate }
19927c478bd9Sstevel@tonic-gate 
19937c478bd9Sstevel@tonic-gate 
19947c478bd9Sstevel@tonic-gate /*
19957c478bd9Sstevel@tonic-gate  * ndi_event_tag_to_cookie: utility function to find an event cookie
19967c478bd9Sstevel@tonic-gate  * given an event tag
19977c478bd9Sstevel@tonic-gate  */
19987c478bd9Sstevel@tonic-gate ddi_eventcookie_t
19997c478bd9Sstevel@tonic-gate ndi_event_tag_to_cookie(ndi_event_hdl_t handle, int event_tag)
20007c478bd9Sstevel@tonic-gate {
20017c478bd9Sstevel@tonic-gate 	struct ndi_event_hdl *ndi_event_hdl = (struct ndi_event_hdl *)handle;
20027c478bd9Sstevel@tonic-gate 	ndi_event_cookie_t *list;
20037c478bd9Sstevel@tonic-gate 
20047c478bd9Sstevel@tonic-gate 	mutex_enter(&ndi_event_hdl->ndi_evthdl_mutex);
20057c478bd9Sstevel@tonic-gate 
20067c478bd9Sstevel@tonic-gate 	list = ndi_event_hdl->ndi_evthdl_cookie_list;
20077c478bd9Sstevel@tonic-gate 	while (list != NULL) {
20087c478bd9Sstevel@tonic-gate 		if (NDI_EVENT_TAG(list) == event_tag) {
20097c478bd9Sstevel@tonic-gate 			mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
20107c478bd9Sstevel@tonic-gate 			return ((ddi_eventcookie_t)list);
20117c478bd9Sstevel@tonic-gate 		}
20127c478bd9Sstevel@tonic-gate 
20137c478bd9Sstevel@tonic-gate 		list = list->next_cookie;
20147c478bd9Sstevel@tonic-gate 	}
20157c478bd9Sstevel@tonic-gate 
20167c478bd9Sstevel@tonic-gate 	mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
20177c478bd9Sstevel@tonic-gate 	return (NULL);
20187c478bd9Sstevel@tonic-gate }
20197c478bd9Sstevel@tonic-gate 
20207c478bd9Sstevel@tonic-gate /*
20217c478bd9Sstevel@tonic-gate  * ndi_event_cookie_to_tag: utility function to find a event tag
20227c478bd9Sstevel@tonic-gate  * given an event_cookie
20237c478bd9Sstevel@tonic-gate  */
20247c478bd9Sstevel@tonic-gate int
20257c478bd9Sstevel@tonic-gate ndi_event_cookie_to_tag(ndi_event_hdl_t handle, ddi_eventcookie_t cookie)
20267c478bd9Sstevel@tonic-gate {
20277c478bd9Sstevel@tonic-gate 	struct ndi_event_hdl *ndi_event_hdl = (struct ndi_event_hdl *)handle;
20287c478bd9Sstevel@tonic-gate 	ndi_event_cookie_t *list;
20297c478bd9Sstevel@tonic-gate 
20307c478bd9Sstevel@tonic-gate 	mutex_enter(&ndi_event_hdl->ndi_evthdl_mutex);
20317c478bd9Sstevel@tonic-gate 
20327c478bd9Sstevel@tonic-gate 	list = ndi_event_hdl->ndi_evthdl_cookie_list;
20337c478bd9Sstevel@tonic-gate 
20347c478bd9Sstevel@tonic-gate 	while (list != NULL) {
20357c478bd9Sstevel@tonic-gate 		if ((ddi_eventcookie_t)list == cookie) {
20367c478bd9Sstevel@tonic-gate 			mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
20377c478bd9Sstevel@tonic-gate 			return (NDI_EVENT_TAG(list));
20387c478bd9Sstevel@tonic-gate 		}
20397c478bd9Sstevel@tonic-gate 
20407c478bd9Sstevel@tonic-gate 		list = list->next_cookie;
20417c478bd9Sstevel@tonic-gate 	}
20427c478bd9Sstevel@tonic-gate 
20437c478bd9Sstevel@tonic-gate 	mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
20447c478bd9Sstevel@tonic-gate 	return (NDI_FAILURE);
20457c478bd9Sstevel@tonic-gate 
20467c478bd9Sstevel@tonic-gate }
20477c478bd9Sstevel@tonic-gate 
20487c478bd9Sstevel@tonic-gate /*
20497c478bd9Sstevel@tonic-gate  * ndi_event_cookie_to_name: utility function to find an event name
20507c478bd9Sstevel@tonic-gate  * given an event_cookie
20517c478bd9Sstevel@tonic-gate  */
20527c478bd9Sstevel@tonic-gate char *
20537c478bd9Sstevel@tonic-gate ndi_event_cookie_to_name(ndi_event_hdl_t handle, ddi_eventcookie_t cookie)
20547c478bd9Sstevel@tonic-gate {
20557c478bd9Sstevel@tonic-gate 	struct ndi_event_hdl *ndi_event_hdl = (struct ndi_event_hdl *)handle;
20567c478bd9Sstevel@tonic-gate 	ndi_event_cookie_t *list;
20577c478bd9Sstevel@tonic-gate 
20587c478bd9Sstevel@tonic-gate 	mutex_enter(&ndi_event_hdl->ndi_evthdl_mutex);
20597c478bd9Sstevel@tonic-gate 
20607c478bd9Sstevel@tonic-gate 	list = ndi_event_hdl->ndi_evthdl_cookie_list;
20617c478bd9Sstevel@tonic-gate 
20627c478bd9Sstevel@tonic-gate 	while (list != NULL) {
20637c478bd9Sstevel@tonic-gate 		if (list == NDI_EVENT(cookie)) {
20647c478bd9Sstevel@tonic-gate 			mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
20657c478bd9Sstevel@tonic-gate 			return (NDI_EVENT_NAME(list));
20667c478bd9Sstevel@tonic-gate 		}
20677c478bd9Sstevel@tonic-gate 
20687c478bd9Sstevel@tonic-gate 		list = list->next_cookie;
20697c478bd9Sstevel@tonic-gate 	}
20707c478bd9Sstevel@tonic-gate 
20717c478bd9Sstevel@tonic-gate 	mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
20727c478bd9Sstevel@tonic-gate 	return (NULL);
20737c478bd9Sstevel@tonic-gate }
20747c478bd9Sstevel@tonic-gate 
20757c478bd9Sstevel@tonic-gate /*
20767c478bd9Sstevel@tonic-gate  * ndi_event_tag_to_name: utility function to find an event name
20777c478bd9Sstevel@tonic-gate  * given an event tag
20787c478bd9Sstevel@tonic-gate  */
20797c478bd9Sstevel@tonic-gate char *
20807c478bd9Sstevel@tonic-gate ndi_event_tag_to_name(ndi_event_hdl_t handle, int event_tag)
20817c478bd9Sstevel@tonic-gate {
20827c478bd9Sstevel@tonic-gate 	struct ndi_event_hdl *ndi_event_hdl = (struct ndi_event_hdl *)handle;
20837c478bd9Sstevel@tonic-gate 	ndi_event_cookie_t *list;
20847c478bd9Sstevel@tonic-gate 
20857c478bd9Sstevel@tonic-gate 	mutex_enter(&ndi_event_hdl->ndi_evthdl_mutex);
20867c478bd9Sstevel@tonic-gate 
20877c478bd9Sstevel@tonic-gate 	list = ndi_event_hdl->ndi_evthdl_cookie_list;
20887c478bd9Sstevel@tonic-gate 
20897c478bd9Sstevel@tonic-gate 	while (list) {
20907c478bd9Sstevel@tonic-gate 		if (NDI_EVENT_TAG(list) == event_tag) {
20917c478bd9Sstevel@tonic-gate 			mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
20927c478bd9Sstevel@tonic-gate 			return (NDI_EVENT_NAME(list));
20937c478bd9Sstevel@tonic-gate 		}
20947c478bd9Sstevel@tonic-gate 
20957c478bd9Sstevel@tonic-gate 		list = list->next_cookie;
20967c478bd9Sstevel@tonic-gate 	}
20977c478bd9Sstevel@tonic-gate 
20987c478bd9Sstevel@tonic-gate 	mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
20997c478bd9Sstevel@tonic-gate 
21007c478bd9Sstevel@tonic-gate 	return (NULL);
21017c478bd9Sstevel@tonic-gate }
21027c478bd9Sstevel@tonic-gate 
21037c478bd9Sstevel@tonic-gate #ifdef NDI_EVENT_DEBUG
21047c478bd9Sstevel@tonic-gate void
21057c478bd9Sstevel@tonic-gate ndi_event_dump_hdl(struct ndi_event_hdl *hdl, char *location)
21067c478bd9Sstevel@tonic-gate {
21077c478bd9Sstevel@tonic-gate 
21087c478bd9Sstevel@tonic-gate 
21097c478bd9Sstevel@tonic-gate 	ndi_event_callbacks_t *next;
21107c478bd9Sstevel@tonic-gate 	ndi_event_cookie_t *list;
21117c478bd9Sstevel@tonic-gate 
21127c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&hdl->ndi_evthdl_mutex));
21137c478bd9Sstevel@tonic-gate 	list = hdl->ndi_evthdl_cookie_list;
21147c478bd9Sstevel@tonic-gate 
21157c478bd9Sstevel@tonic-gate 	cmn_err(CE_CONT, "%s: event handle (%p): dip = %p (%s%d)\n",
21167c478bd9Sstevel@tonic-gate 		location, (void *)hdl,
21177c478bd9Sstevel@tonic-gate 		(void *)hdl->ndi_evthdl_dip,
21187c478bd9Sstevel@tonic-gate 		ddi_node_name(hdl->ndi_evthdl_dip),
21197c478bd9Sstevel@tonic-gate 		ddi_get_instance(hdl->ndi_evthdl_dip));
21207c478bd9Sstevel@tonic-gate 	cmn_err(CE_CONT, "\thigh=%d other=%d n=%d\n",
21217c478bd9Sstevel@tonic-gate 		hdl->ndi_evthdl_high_plevels,
21227c478bd9Sstevel@tonic-gate 		hdl->ndi_evthdl_other_plevels,
21237c478bd9Sstevel@tonic-gate 		hdl->ndi_evthdl_n_events);
21247c478bd9Sstevel@tonic-gate 
21257c478bd9Sstevel@tonic-gate 
21267c478bd9Sstevel@tonic-gate 	cmn_err(CE_CONT, "\tevent cookies:\n");
21277c478bd9Sstevel@tonic-gate 	while (list) {
21287c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT,
21297c478bd9Sstevel@tonic-gate 			"\t\ttag=%d name=%s p=%d a=%x dd=%p\n",
21307c478bd9Sstevel@tonic-gate 			NDI_EVENT_TAG(list),
21317c478bd9Sstevel@tonic-gate 			NDI_EVENT_NAME(list),
21327c478bd9Sstevel@tonic-gate 			NDI_EVENT_PLEVEL(list),
21337c478bd9Sstevel@tonic-gate 			NDI_EVENT_ATTRIBUTES(list),
21347c478bd9Sstevel@tonic-gate 			(void *)NDI_EVENT_DDIP(list));
21357c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "\t\tcallbacks:\n");
21367c478bd9Sstevel@tonic-gate 		for (next = list->callback_list; next != NULL;
21377c478bd9Sstevel@tonic-gate 		    next = next->ndi_evtcb_next) {
21387c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT,
21397c478bd9Sstevel@tonic-gate 			    "\t\t  dip=%p (%s%d) cookie=%p arg=%p\n",
21407c478bd9Sstevel@tonic-gate 			    (void*)next->ndi_evtcb_dip,
21417c478bd9Sstevel@tonic-gate 			    ddi_driver_name(next->ndi_evtcb_dip),
21427c478bd9Sstevel@tonic-gate 			    ddi_get_instance(next->ndi_evtcb_dip),
21437c478bd9Sstevel@tonic-gate 			    (void *)next->ndi_evtcb_cookie,
21447c478bd9Sstevel@tonic-gate 			    next->ndi_evtcb_arg);
21457c478bd9Sstevel@tonic-gate 		}
21467c478bd9Sstevel@tonic-gate 
21477c478bd9Sstevel@tonic-gate 		list = list->next_cookie;
21487c478bd9Sstevel@tonic-gate 	}
21497c478bd9Sstevel@tonic-gate 
21507c478bd9Sstevel@tonic-gate 	cmn_err(CE_CONT, "\n");
21517c478bd9Sstevel@tonic-gate }
21527c478bd9Sstevel@tonic-gate #endif
21537c478bd9Sstevel@tonic-gate 
21547c478bd9Sstevel@tonic-gate int
21557c478bd9Sstevel@tonic-gate ndi_dev_is_prom_node(dev_info_t *dip)
21567c478bd9Sstevel@tonic-gate {
21577c478bd9Sstevel@tonic-gate 	return (DEVI(dip)->devi_node_class == DDI_NC_PROM);
21587c478bd9Sstevel@tonic-gate }
21597c478bd9Sstevel@tonic-gate 
21607c478bd9Sstevel@tonic-gate int
21617c478bd9Sstevel@tonic-gate ndi_dev_is_pseudo_node(dev_info_t *dip)
21627c478bd9Sstevel@tonic-gate {
21637c478bd9Sstevel@tonic-gate 	/*
21647c478bd9Sstevel@tonic-gate 	 * NOTE: this does NOT mean the pseudo branch of the device tree,
21657c478bd9Sstevel@tonic-gate 	 * it means the node was created by software (DEVI_SID_NODEID |
21667c478bd9Sstevel@tonic-gate 	 * DEVI_PSEUDO_NODEID) instead of being generated from a PROM node.
21677c478bd9Sstevel@tonic-gate 	 */
21687c478bd9Sstevel@tonic-gate 	return (DEVI(dip)->devi_node_class == DDI_NC_PSEUDO);
21697c478bd9Sstevel@tonic-gate }
21707c478bd9Sstevel@tonic-gate 
21717c478bd9Sstevel@tonic-gate int
21727c478bd9Sstevel@tonic-gate ndi_dev_is_persistent_node(dev_info_t *dip)
21737c478bd9Sstevel@tonic-gate {
21747c478bd9Sstevel@tonic-gate 	return ((DEVI(dip)->devi_node_attributes & DDI_PERSISTENT) != 0);
21757c478bd9Sstevel@tonic-gate }
21767c478bd9Sstevel@tonic-gate 
21777c478bd9Sstevel@tonic-gate int
21787c478bd9Sstevel@tonic-gate i_ndi_dev_is_auto_assigned_node(dev_info_t *dip)
21797c478bd9Sstevel@tonic-gate {
21807c478bd9Sstevel@tonic-gate 	return ((DEVI(dip)->devi_node_attributes &
21817c478bd9Sstevel@tonic-gate 	    DDI_AUTO_ASSIGNED_NODEID) != 0);
21827c478bd9Sstevel@tonic-gate }
21837c478bd9Sstevel@tonic-gate 
21847c478bd9Sstevel@tonic-gate void
21857c478bd9Sstevel@tonic-gate i_ndi_set_node_class(dev_info_t *dip, ddi_node_class_t c)
21867c478bd9Sstevel@tonic-gate {
21877c478bd9Sstevel@tonic-gate 	DEVI(dip)->devi_node_class = c;
21887c478bd9Sstevel@tonic-gate }
21897c478bd9Sstevel@tonic-gate 
21907c478bd9Sstevel@tonic-gate ddi_node_class_t
21917c478bd9Sstevel@tonic-gate i_ndi_get_node_class(dev_info_t *dip)
21927c478bd9Sstevel@tonic-gate {
21937c478bd9Sstevel@tonic-gate 	return (DEVI(dip)->devi_node_class);
21947c478bd9Sstevel@tonic-gate }
21957c478bd9Sstevel@tonic-gate 
21967c478bd9Sstevel@tonic-gate void
21977c478bd9Sstevel@tonic-gate i_ndi_set_node_attributes(dev_info_t *dip, int p)
21987c478bd9Sstevel@tonic-gate {
21997c478bd9Sstevel@tonic-gate 	DEVI(dip)->devi_node_attributes = p;
22007c478bd9Sstevel@tonic-gate }
22017c478bd9Sstevel@tonic-gate 
22027c478bd9Sstevel@tonic-gate int
22037c478bd9Sstevel@tonic-gate i_ndi_get_node_attributes(dev_info_t *dip)
22047c478bd9Sstevel@tonic-gate {
22057c478bd9Sstevel@tonic-gate 	return (DEVI(dip)->devi_node_attributes);
22067c478bd9Sstevel@tonic-gate }
22077c478bd9Sstevel@tonic-gate 
22087c478bd9Sstevel@tonic-gate void
22097c478bd9Sstevel@tonic-gate i_ndi_set_nodeid(dev_info_t *dip, int n)
22107c478bd9Sstevel@tonic-gate {
22117c478bd9Sstevel@tonic-gate 	DEVI(dip)->devi_nodeid = n;
22127c478bd9Sstevel@tonic-gate }
22137c478bd9Sstevel@tonic-gate 
22147c478bd9Sstevel@tonic-gate void
22157c478bd9Sstevel@tonic-gate ndi_set_acc_fault(ddi_acc_handle_t ah)
22167c478bd9Sstevel@tonic-gate {
22177c478bd9Sstevel@tonic-gate 	i_ddi_acc_set_fault(ah);
22187c478bd9Sstevel@tonic-gate }
22197c478bd9Sstevel@tonic-gate 
22207c478bd9Sstevel@tonic-gate void
22217c478bd9Sstevel@tonic-gate ndi_clr_acc_fault(ddi_acc_handle_t ah)
22227c478bd9Sstevel@tonic-gate {
22237c478bd9Sstevel@tonic-gate 	i_ddi_acc_clr_fault(ah);
22247c478bd9Sstevel@tonic-gate }
22257c478bd9Sstevel@tonic-gate 
22267c478bd9Sstevel@tonic-gate void
22277c478bd9Sstevel@tonic-gate ndi_set_dma_fault(ddi_dma_handle_t dh)
22287c478bd9Sstevel@tonic-gate {
22297c478bd9Sstevel@tonic-gate 	i_ddi_dma_set_fault(dh);
22307c478bd9Sstevel@tonic-gate }
22317c478bd9Sstevel@tonic-gate 
22327c478bd9Sstevel@tonic-gate void
22337c478bd9Sstevel@tonic-gate ndi_clr_dma_fault(ddi_dma_handle_t dh)
22347c478bd9Sstevel@tonic-gate {
22357c478bd9Sstevel@tonic-gate 	i_ddi_dma_clr_fault(dh);
22367c478bd9Sstevel@tonic-gate }
22377c478bd9Sstevel@tonic-gate 
22387c478bd9Sstevel@tonic-gate /*
22397c478bd9Sstevel@tonic-gate  *  The default fault-handler, called when the event posted by
22407c478bd9Sstevel@tonic-gate  *  ddi_dev_report_fault() reaches rootnex.
22417c478bd9Sstevel@tonic-gate  */
22427c478bd9Sstevel@tonic-gate static void
22437c478bd9Sstevel@tonic-gate i_ddi_fault_handler(dev_info_t *dip, struct ddi_fault_event_data *fedp)
22447c478bd9Sstevel@tonic-gate {
22457c478bd9Sstevel@tonic-gate 	ASSERT(fedp);
22467c478bd9Sstevel@tonic-gate 
22477c478bd9Sstevel@tonic-gate 	mutex_enter(&(DEVI(dip)->devi_lock));
22487c478bd9Sstevel@tonic-gate 	if (!DEVI_IS_DEVICE_OFFLINE(dip)) {
22497c478bd9Sstevel@tonic-gate 		switch (fedp->f_impact) {
22507c478bd9Sstevel@tonic-gate 		case DDI_SERVICE_LOST:
22517c478bd9Sstevel@tonic-gate 			DEVI_SET_DEVICE_DOWN(dip);
22527c478bd9Sstevel@tonic-gate 			break;
22537c478bd9Sstevel@tonic-gate 
22547c478bd9Sstevel@tonic-gate 		case DDI_SERVICE_DEGRADED:
22557c478bd9Sstevel@tonic-gate 			DEVI_SET_DEVICE_DEGRADED(dip);
22567c478bd9Sstevel@tonic-gate 			break;
22577c478bd9Sstevel@tonic-gate 
22587c478bd9Sstevel@tonic-gate 		case DDI_SERVICE_UNAFFECTED:
22597c478bd9Sstevel@tonic-gate 		default:
22607c478bd9Sstevel@tonic-gate 			break;
22617c478bd9Sstevel@tonic-gate 
22627c478bd9Sstevel@tonic-gate 		case DDI_SERVICE_RESTORED:
22637c478bd9Sstevel@tonic-gate 			DEVI_SET_DEVICE_UP(dip);
22647c478bd9Sstevel@tonic-gate 			break;
22657c478bd9Sstevel@tonic-gate 		}
22667c478bd9Sstevel@tonic-gate 	}
22677c478bd9Sstevel@tonic-gate 	mutex_exit(&(DEVI(dip)->devi_lock));
22687c478bd9Sstevel@tonic-gate }
22697c478bd9Sstevel@tonic-gate 
22707c478bd9Sstevel@tonic-gate /*
22717c478bd9Sstevel@tonic-gate  * The default fault-logger, called when the event posted by
22727c478bd9Sstevel@tonic-gate  * ddi_dev_report_fault() reaches rootnex.
22737c478bd9Sstevel@tonic-gate  */
22747c478bd9Sstevel@tonic-gate /*ARGSUSED*/
22757c478bd9Sstevel@tonic-gate static void
22767c478bd9Sstevel@tonic-gate i_ddi_fault_logger(dev_info_t *rdip, struct ddi_fault_event_data *fedp)
22777c478bd9Sstevel@tonic-gate {
22787c478bd9Sstevel@tonic-gate 	ddi_devstate_t newstate;
22797c478bd9Sstevel@tonic-gate 	const char *action;
22807c478bd9Sstevel@tonic-gate 	const char *servstate;
22817c478bd9Sstevel@tonic-gate 	const char *location;
22827c478bd9Sstevel@tonic-gate 	int bad;
22837c478bd9Sstevel@tonic-gate 	int changed;
22847c478bd9Sstevel@tonic-gate 	int level;
22857c478bd9Sstevel@tonic-gate 	int still;
22867c478bd9Sstevel@tonic-gate 
22877c478bd9Sstevel@tonic-gate 	ASSERT(fedp);
22887c478bd9Sstevel@tonic-gate 
22897c478bd9Sstevel@tonic-gate 	bad = 0;
22907c478bd9Sstevel@tonic-gate 	switch (fedp->f_location) {
22917c478bd9Sstevel@tonic-gate 	case DDI_DATAPATH_FAULT:
22927c478bd9Sstevel@tonic-gate 		location = "in datapath to";
22937c478bd9Sstevel@tonic-gate 		break;
22947c478bd9Sstevel@tonic-gate 	case DDI_DEVICE_FAULT:
22957c478bd9Sstevel@tonic-gate 		location = "in";
22967c478bd9Sstevel@tonic-gate 		break;
22977c478bd9Sstevel@tonic-gate 	case DDI_EXTERNAL_FAULT:
22987c478bd9Sstevel@tonic-gate 		location = "external to";
22997c478bd9Sstevel@tonic-gate 		break;
23007c478bd9Sstevel@tonic-gate 	default:
23017c478bd9Sstevel@tonic-gate 		location = "somewhere near";
23027c478bd9Sstevel@tonic-gate 		bad = 1;
23037c478bd9Sstevel@tonic-gate 		break;
23047c478bd9Sstevel@tonic-gate 	}
23057c478bd9Sstevel@tonic-gate 
23067c478bd9Sstevel@tonic-gate 	newstate = ddi_get_devstate(fedp->f_dip);
23077c478bd9Sstevel@tonic-gate 	switch (newstate) {
23087c478bd9Sstevel@tonic-gate 	case DDI_DEVSTATE_OFFLINE:
23097c478bd9Sstevel@tonic-gate 		servstate = "unavailable";
23107c478bd9Sstevel@tonic-gate 		break;
23117c478bd9Sstevel@tonic-gate 	case DDI_DEVSTATE_DOWN:
23127c478bd9Sstevel@tonic-gate 		servstate = "unavailable";
23137c478bd9Sstevel@tonic-gate 		break;
23147c478bd9Sstevel@tonic-gate 	case DDI_DEVSTATE_QUIESCED:
23157c478bd9Sstevel@tonic-gate 		servstate = "suspended";
23167c478bd9Sstevel@tonic-gate 		break;
23177c478bd9Sstevel@tonic-gate 	case DDI_DEVSTATE_DEGRADED:
23187c478bd9Sstevel@tonic-gate 		servstate = "degraded";
23197c478bd9Sstevel@tonic-gate 		break;
23207c478bd9Sstevel@tonic-gate 	default:
23217c478bd9Sstevel@tonic-gate 		servstate = "available";
23227c478bd9Sstevel@tonic-gate 		break;
23237c478bd9Sstevel@tonic-gate 	}
23247c478bd9Sstevel@tonic-gate 
23257c478bd9Sstevel@tonic-gate 	changed = (newstate != fedp->f_oldstate);
23267c478bd9Sstevel@tonic-gate 	level = (newstate < fedp->f_oldstate) ? CE_WARN : CE_NOTE;
23277c478bd9Sstevel@tonic-gate 	switch (fedp->f_impact) {
23287c478bd9Sstevel@tonic-gate 	case DDI_SERVICE_LOST:
23297c478bd9Sstevel@tonic-gate 	case DDI_SERVICE_DEGRADED:
23307c478bd9Sstevel@tonic-gate 	case DDI_SERVICE_UNAFFECTED:
23317c478bd9Sstevel@tonic-gate 		/* fault detected; service [still] <servstate> */
23327c478bd9Sstevel@tonic-gate 		action = "fault detected";
23337c478bd9Sstevel@tonic-gate 		still = !changed;
23347c478bd9Sstevel@tonic-gate 		break;
23357c478bd9Sstevel@tonic-gate 
23367c478bd9Sstevel@tonic-gate 	case DDI_SERVICE_RESTORED:
23377c478bd9Sstevel@tonic-gate 		if (newstate != DDI_DEVSTATE_UP) {
23387c478bd9Sstevel@tonic-gate 			/* fault cleared; service still <servstate> */
23397c478bd9Sstevel@tonic-gate 			action = "fault cleared";
23407c478bd9Sstevel@tonic-gate 			still = 1;
23417c478bd9Sstevel@tonic-gate 		} else if (changed) {
23427c478bd9Sstevel@tonic-gate 			/* fault cleared; service <servstate> */
23437c478bd9Sstevel@tonic-gate 			action = "fault cleared";
23447c478bd9Sstevel@tonic-gate 			still = 0;
23457c478bd9Sstevel@tonic-gate 		} else {
23467c478bd9Sstevel@tonic-gate 			/* no fault; service <servstate> */
23477c478bd9Sstevel@tonic-gate 			action = "no fault";
23487c478bd9Sstevel@tonic-gate 			still = 0;
23497c478bd9Sstevel@tonic-gate 		}
23507c478bd9Sstevel@tonic-gate 		break;
23517c478bd9Sstevel@tonic-gate 
23527c478bd9Sstevel@tonic-gate 	default:
23537c478bd9Sstevel@tonic-gate 		bad = 1;
23547c478bd9Sstevel@tonic-gate 		break;
23557c478bd9Sstevel@tonic-gate 	}
23567c478bd9Sstevel@tonic-gate 
23577c478bd9Sstevel@tonic-gate 	cmn_err(level, "!%s%d: %s %s device; service %s%s"+(bad|changed),
23587c478bd9Sstevel@tonic-gate 		ddi_driver_name(fedp->f_dip),
23597c478bd9Sstevel@tonic-gate 		ddi_get_instance(fedp->f_dip),
23607c478bd9Sstevel@tonic-gate 		bad ? "invalid report of fault" : action,
23617c478bd9Sstevel@tonic-gate 		location, still ? "still " : "", servstate);
23627c478bd9Sstevel@tonic-gate 
23637c478bd9Sstevel@tonic-gate 	cmn_err(level, "!%s%d: %s"+(bad|changed),
23647c478bd9Sstevel@tonic-gate 		ddi_driver_name(fedp->f_dip),
23657c478bd9Sstevel@tonic-gate 		ddi_get_instance(fedp->f_dip),
23667c478bd9Sstevel@tonic-gate 		fedp->f_message);
23677c478bd9Sstevel@tonic-gate }
23687c478bd9Sstevel@tonic-gate 
23697c478bd9Sstevel@tonic-gate /*
23707c478bd9Sstevel@tonic-gate  * Platform-settable pointers to fault handler and logger functions.
23717c478bd9Sstevel@tonic-gate  * These are called by the default rootnex event-posting code when
23727c478bd9Sstevel@tonic-gate  * a fault event reaches rootnex.
23737c478bd9Sstevel@tonic-gate  */
23747c478bd9Sstevel@tonic-gate void (*plat_fault_handler)(dev_info_t *, struct ddi_fault_event_data *) =
23757c478bd9Sstevel@tonic-gate 	i_ddi_fault_handler;
23767c478bd9Sstevel@tonic-gate void (*plat_fault_logger)(dev_info_t *, struct ddi_fault_event_data *) =
23777c478bd9Sstevel@tonic-gate 	i_ddi_fault_logger;
23787c478bd9Sstevel@tonic-gate 
23797c478bd9Sstevel@tonic-gate /*
23807c478bd9Sstevel@tonic-gate  * Rootnex event definitions ...
23817c478bd9Sstevel@tonic-gate  */
23827c478bd9Sstevel@tonic-gate enum rootnex_event_tags {
23837c478bd9Sstevel@tonic-gate 	ROOTNEX_FAULT_EVENT
23847c478bd9Sstevel@tonic-gate };
23857c478bd9Sstevel@tonic-gate static ndi_event_hdl_t rootnex_event_hdl;
23867c478bd9Sstevel@tonic-gate static ndi_event_definition_t rootnex_event_set[] = {
23877c478bd9Sstevel@tonic-gate 	{
23887c478bd9Sstevel@tonic-gate 		ROOTNEX_FAULT_EVENT,
23897c478bd9Sstevel@tonic-gate 		DDI_DEVI_FAULT_EVENT,
23907c478bd9Sstevel@tonic-gate 		EPL_INTERRUPT,
23917c478bd9Sstevel@tonic-gate 		NDI_EVENT_POST_TO_ALL
23927c478bd9Sstevel@tonic-gate 	}
23937c478bd9Sstevel@tonic-gate };
23947c478bd9Sstevel@tonic-gate static ndi_event_set_t rootnex_events = {
23957c478bd9Sstevel@tonic-gate 	NDI_EVENTS_REV1,
23967c478bd9Sstevel@tonic-gate 	sizeof (rootnex_event_set) / sizeof (rootnex_event_set[0]),
23977c478bd9Sstevel@tonic-gate 	rootnex_event_set
23987c478bd9Sstevel@tonic-gate };
23997c478bd9Sstevel@tonic-gate 
24007c478bd9Sstevel@tonic-gate /*
24017c478bd9Sstevel@tonic-gate  * Initialize rootnex event handle
24027c478bd9Sstevel@tonic-gate  */
24037c478bd9Sstevel@tonic-gate void
24047c478bd9Sstevel@tonic-gate i_ddi_rootnex_init_events(dev_info_t *dip)
24057c478bd9Sstevel@tonic-gate {
24067c478bd9Sstevel@tonic-gate 	if (ndi_event_alloc_hdl(dip, (ddi_iblock_cookie_t)(LOCK_LEVEL-1),
24077c478bd9Sstevel@tonic-gate 	    &rootnex_event_hdl, NDI_SLEEP) == NDI_SUCCESS) {
24087c478bd9Sstevel@tonic-gate 		if (ndi_event_bind_set(rootnex_event_hdl,
24097c478bd9Sstevel@tonic-gate 		    &rootnex_events, NDI_SLEEP) != NDI_SUCCESS) {
24107c478bd9Sstevel@tonic-gate 			(void) ndi_event_free_hdl(rootnex_event_hdl);
24117c478bd9Sstevel@tonic-gate 			rootnex_event_hdl = NULL;
24127c478bd9Sstevel@tonic-gate 		}
24137c478bd9Sstevel@tonic-gate 	}
24147c478bd9Sstevel@tonic-gate }
24157c478bd9Sstevel@tonic-gate 
24167c478bd9Sstevel@tonic-gate /*
24177c478bd9Sstevel@tonic-gate  *      Event-handling functions for rootnex
24187c478bd9Sstevel@tonic-gate  *      These provide the standard implementation of fault handling
24197c478bd9Sstevel@tonic-gate  */
24207c478bd9Sstevel@tonic-gate /*ARGSUSED*/
24217c478bd9Sstevel@tonic-gate int
24227c478bd9Sstevel@tonic-gate i_ddi_rootnex_get_eventcookie(dev_info_t *dip, dev_info_t *rdip,
24237c478bd9Sstevel@tonic-gate 	char *eventname, ddi_eventcookie_t *cookiep)
24247c478bd9Sstevel@tonic-gate {
24257c478bd9Sstevel@tonic-gate 	if (rootnex_event_hdl == NULL)
24267c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
24277c478bd9Sstevel@tonic-gate 	return (ndi_event_retrieve_cookie(rootnex_event_hdl, rdip, eventname,
24287c478bd9Sstevel@tonic-gate 		cookiep, NDI_EVENT_NOPASS));
24297c478bd9Sstevel@tonic-gate }
24307c478bd9Sstevel@tonic-gate 
24317c478bd9Sstevel@tonic-gate /*ARGSUSED*/
24327c478bd9Sstevel@tonic-gate int
24337c478bd9Sstevel@tonic-gate i_ddi_rootnex_add_eventcall(dev_info_t *dip, dev_info_t *rdip,
24347c478bd9Sstevel@tonic-gate 	ddi_eventcookie_t eventid, void (*handler)(dev_info_t *dip,
24357c478bd9Sstevel@tonic-gate 	ddi_eventcookie_t event, void *arg, void *impl_data), void *arg,
24367c478bd9Sstevel@tonic-gate 	ddi_callback_id_t *cb_id)
24377c478bd9Sstevel@tonic-gate {
24387c478bd9Sstevel@tonic-gate 	if (rootnex_event_hdl == NULL)
24397c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
24407c478bd9Sstevel@tonic-gate 	return (ndi_event_add_callback(rootnex_event_hdl, rdip,
24417c478bd9Sstevel@tonic-gate 		eventid, handler, arg, NDI_SLEEP, cb_id));
24427c478bd9Sstevel@tonic-gate }
24437c478bd9Sstevel@tonic-gate 
24447c478bd9Sstevel@tonic-gate /*ARGSUSED*/
24457c478bd9Sstevel@tonic-gate int
24467c478bd9Sstevel@tonic-gate i_ddi_rootnex_remove_eventcall(dev_info_t *dip, ddi_callback_id_t cb_id)
24477c478bd9Sstevel@tonic-gate {
24487c478bd9Sstevel@tonic-gate 	if (rootnex_event_hdl == NULL)
24497c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
24507c478bd9Sstevel@tonic-gate 
24517c478bd9Sstevel@tonic-gate 	return (ndi_event_remove_callback(rootnex_event_hdl, cb_id));
24527c478bd9Sstevel@tonic-gate }
24537c478bd9Sstevel@tonic-gate 
24547c478bd9Sstevel@tonic-gate /*ARGSUSED*/
24557c478bd9Sstevel@tonic-gate int
24567c478bd9Sstevel@tonic-gate i_ddi_rootnex_post_event(dev_info_t *dip, dev_info_t *rdip,
24577c478bd9Sstevel@tonic-gate 	ddi_eventcookie_t eventid, void *impl_data)
24587c478bd9Sstevel@tonic-gate {
24597c478bd9Sstevel@tonic-gate 	int tag;
24607c478bd9Sstevel@tonic-gate 
24617c478bd9Sstevel@tonic-gate 	if (rootnex_event_hdl == NULL)
24627c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
24637c478bd9Sstevel@tonic-gate 
24647c478bd9Sstevel@tonic-gate 	tag = ndi_event_cookie_to_tag(rootnex_event_hdl, eventid);
24657c478bd9Sstevel@tonic-gate 	if (tag == ROOTNEX_FAULT_EVENT) {
24667c478bd9Sstevel@tonic-gate 		(*plat_fault_handler)(rdip, impl_data);
24677c478bd9Sstevel@tonic-gate 		(*plat_fault_logger)(rdip, impl_data);
24687c478bd9Sstevel@tonic-gate 	}
24697c478bd9Sstevel@tonic-gate 	return (ndi_event_run_callbacks(rootnex_event_hdl, rdip,
24707c478bd9Sstevel@tonic-gate 		eventid, impl_data));
24717c478bd9Sstevel@tonic-gate }
2472