xref: /onnv-gate/usr/src/uts/common/os/sunndi.c (revision 10696:cd0f390dd9e2)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
54411Svikram  * Common Development and Distribution License (the "License").
64411Svikram  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
228860SMatthew.Jacob@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include <sys/types.h>
270Sstevel@tonic-gate #include <sys/param.h>
280Sstevel@tonic-gate #include <sys/systm.h>
290Sstevel@tonic-gate #include <sys/buf.h>
300Sstevel@tonic-gate #include <sys/uio.h>
310Sstevel@tonic-gate #include <sys/cred.h>
320Sstevel@tonic-gate #include <sys/poll.h>
330Sstevel@tonic-gate #include <sys/mman.h>
340Sstevel@tonic-gate #include <sys/kmem.h>
350Sstevel@tonic-gate #include <sys/model.h>
360Sstevel@tonic-gate #include <sys/file.h>
370Sstevel@tonic-gate #include <sys/proc.h>
380Sstevel@tonic-gate #include <sys/open.h>
390Sstevel@tonic-gate #include <sys/user.h>
400Sstevel@tonic-gate #include <sys/t_lock.h>
410Sstevel@tonic-gate #include <sys/vm.h>
420Sstevel@tonic-gate #include <sys/stat.h>
430Sstevel@tonic-gate #include <vm/hat.h>
440Sstevel@tonic-gate #include <vm/seg.h>
450Sstevel@tonic-gate #include <vm/as.h>
460Sstevel@tonic-gate #include <sys/cmn_err.h>
470Sstevel@tonic-gate #include <sys/debug.h>
480Sstevel@tonic-gate #include <sys/avintr.h>
490Sstevel@tonic-gate #include <sys/autoconf.h>
500Sstevel@tonic-gate #include <sys/sunddi.h>
510Sstevel@tonic-gate #include <sys/esunddi.h>
520Sstevel@tonic-gate #include <sys/sunndi.h>
530Sstevel@tonic-gate #include <sys/ddi.h>
540Sstevel@tonic-gate #include <sys/kstat.h>
550Sstevel@tonic-gate #include <sys/conf.h>
560Sstevel@tonic-gate #include <sys/ddi_impldefs.h>	/* include implementation structure defs */
570Sstevel@tonic-gate #include <sys/ndi_impldefs.h>
580Sstevel@tonic-gate #include <sys/hwconf.h>
590Sstevel@tonic-gate #include <sys/pathname.h>
600Sstevel@tonic-gate #include <sys/modctl.h>
610Sstevel@tonic-gate #include <sys/epm.h>
620Sstevel@tonic-gate #include <sys/devctl.h>
630Sstevel@tonic-gate #include <sys/callb.h>
640Sstevel@tonic-gate #include <sys/bootconf.h>
650Sstevel@tonic-gate #include <sys/dacf_impl.h>
660Sstevel@tonic-gate #include <sys/nvpair.h>
670Sstevel@tonic-gate #include <sys/sunmdi.h>
680Sstevel@tonic-gate #include <sys/fs/dv_node.h>
694845Svikram #include <sys/sunldi_impl.h>
700Sstevel@tonic-gate 
710Sstevel@tonic-gate #ifdef __sparc
720Sstevel@tonic-gate #include <sys/archsystm.h>	/* getpil/setpil */
730Sstevel@tonic-gate #include <sys/membar.h>		/* membar_sync */
740Sstevel@tonic-gate #endif
750Sstevel@tonic-gate 
760Sstevel@tonic-gate /*
770Sstevel@tonic-gate  * ndi property handling
780Sstevel@tonic-gate  */
790Sstevel@tonic-gate int
ndi_prop_update_int(dev_t match_dev,dev_info_t * dip,char * name,int data)800Sstevel@tonic-gate ndi_prop_update_int(dev_t match_dev, dev_info_t *dip,
810Sstevel@tonic-gate     char *name, int data)
820Sstevel@tonic-gate {
830Sstevel@tonic-gate 	return (ddi_prop_update_common(match_dev, dip,
840Sstevel@tonic-gate 	    DDI_PROP_HW_DEF | DDI_PROP_TYPE_INT | DDI_PROP_DONTSLEEP,
850Sstevel@tonic-gate 	    name, &data, 1, ddi_prop_fm_encode_ints));
860Sstevel@tonic-gate }
870Sstevel@tonic-gate 
880Sstevel@tonic-gate int
ndi_prop_update_int64(dev_t match_dev,dev_info_t * dip,char * name,int64_t data)890Sstevel@tonic-gate ndi_prop_update_int64(dev_t match_dev, dev_info_t *dip,
900Sstevel@tonic-gate     char *name, int64_t data)
910Sstevel@tonic-gate {
920Sstevel@tonic-gate 	return (ddi_prop_update_common(match_dev, dip,
930Sstevel@tonic-gate 	    DDI_PROP_HW_DEF | DDI_PROP_TYPE_INT64 | DDI_PROP_DONTSLEEP,
940Sstevel@tonic-gate 	    name, &data, 1, ddi_prop_fm_encode_int64));
950Sstevel@tonic-gate }
960Sstevel@tonic-gate 
970Sstevel@tonic-gate int
ndi_prop_create_boolean(dev_t match_dev,dev_info_t * dip,char * name)980Sstevel@tonic-gate ndi_prop_create_boolean(dev_t match_dev, dev_info_t *dip,
990Sstevel@tonic-gate     char *name)
1000Sstevel@tonic-gate {
1010Sstevel@tonic-gate 	return (ddi_prop_update_common(match_dev, dip,
1020Sstevel@tonic-gate 	    DDI_PROP_HW_DEF | DDI_PROP_TYPE_ANY | DDI_PROP_DONTSLEEP,
1030Sstevel@tonic-gate 	    name, NULL, 0, ddi_prop_fm_encode_bytes));
1040Sstevel@tonic-gate }
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate int
ndi_prop_update_int_array(dev_t match_dev,dev_info_t * dip,char * name,int * data,uint_t nelements)1070Sstevel@tonic-gate ndi_prop_update_int_array(dev_t match_dev, dev_info_t *dip,
1080Sstevel@tonic-gate     char *name, int *data, uint_t nelements)
1090Sstevel@tonic-gate {
1100Sstevel@tonic-gate 	return (ddi_prop_update_common(match_dev, dip,
1110Sstevel@tonic-gate 	    DDI_PROP_HW_DEF | DDI_PROP_TYPE_INT | DDI_PROP_DONTSLEEP,
1120Sstevel@tonic-gate 	    name, data, nelements, ddi_prop_fm_encode_ints));
1130Sstevel@tonic-gate }
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate int
ndi_prop_update_int64_array(dev_t match_dev,dev_info_t * dip,char * name,int64_t * data,uint_t nelements)1160Sstevel@tonic-gate ndi_prop_update_int64_array(dev_t match_dev, dev_info_t *dip,
1170Sstevel@tonic-gate     char *name, int64_t *data, uint_t nelements)
1180Sstevel@tonic-gate {
1190Sstevel@tonic-gate 	return (ddi_prop_update_common(match_dev, dip,
1200Sstevel@tonic-gate 	    DDI_PROP_HW_DEF | DDI_PROP_TYPE_INT64 | DDI_PROP_DONTSLEEP,
1210Sstevel@tonic-gate 	    name, data, nelements, ddi_prop_fm_encode_int64));
1220Sstevel@tonic-gate }
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate int
ndi_prop_update_string(dev_t match_dev,dev_info_t * dip,char * name,char * data)1250Sstevel@tonic-gate ndi_prop_update_string(dev_t match_dev, dev_info_t *dip,
1260Sstevel@tonic-gate     char *name, char *data)
1270Sstevel@tonic-gate {
1280Sstevel@tonic-gate 	return (ddi_prop_update_common(match_dev, dip,
1290Sstevel@tonic-gate 	    DDI_PROP_HW_DEF | DDI_PROP_TYPE_STRING | DDI_PROP_DONTSLEEP,
1300Sstevel@tonic-gate 	    name, &data, 1, ddi_prop_fm_encode_string));
1310Sstevel@tonic-gate }
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate int
ndi_prop_update_string_array(dev_t match_dev,dev_info_t * dip,char * name,char ** data,uint_t nelements)1340Sstevel@tonic-gate ndi_prop_update_string_array(dev_t match_dev, dev_info_t *dip,
1350Sstevel@tonic-gate     char *name, char **data, uint_t nelements)
1360Sstevel@tonic-gate {
1370Sstevel@tonic-gate 	return (ddi_prop_update_common(match_dev, dip,
1380Sstevel@tonic-gate 	    DDI_PROP_HW_DEF | DDI_PROP_TYPE_STRING | DDI_PROP_DONTSLEEP,
1390Sstevel@tonic-gate 	    name, data, nelements,
1400Sstevel@tonic-gate 	    ddi_prop_fm_encode_strings));
1410Sstevel@tonic-gate }
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate int
ndi_prop_update_byte_array(dev_t match_dev,dev_info_t * dip,char * name,uchar_t * data,uint_t nelements)1440Sstevel@tonic-gate ndi_prop_update_byte_array(dev_t match_dev, dev_info_t *dip,
1450Sstevel@tonic-gate     char *name, uchar_t *data, uint_t nelements)
1460Sstevel@tonic-gate {
1470Sstevel@tonic-gate 	if (nelements == 0)
1480Sstevel@tonic-gate 		return (DDI_PROP_INVAL_ARG);
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate 	return (ddi_prop_update_common(match_dev, dip,
1510Sstevel@tonic-gate 	    DDI_PROP_HW_DEF | DDI_PROP_TYPE_BYTE | DDI_PROP_DONTSLEEP,
1520Sstevel@tonic-gate 	    name, data, nelements, ddi_prop_fm_encode_bytes));
1530Sstevel@tonic-gate }
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate int
ndi_prop_remove(dev_t dev,dev_info_t * dip,char * name)1560Sstevel@tonic-gate ndi_prop_remove(dev_t dev, dev_info_t *dip, char *name)
1570Sstevel@tonic-gate {
1580Sstevel@tonic-gate 	return (ddi_prop_remove_common(dev, dip, name, DDI_PROP_HW_DEF));
1590Sstevel@tonic-gate }
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate void
ndi_prop_remove_all(dev_info_t * dip)1620Sstevel@tonic-gate ndi_prop_remove_all(dev_info_t *dip)
1630Sstevel@tonic-gate {
1647224Scth 	i_ddi_prop_dyn_parent_set(dip, NULL);
1650Sstevel@tonic-gate 	ddi_prop_remove_all_common(dip, (int)DDI_PROP_HW_DEF);
1660Sstevel@tonic-gate }
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate /*
1690Sstevel@tonic-gate  * Post an event notification to nexus driver responsible for handling
1700Sstevel@tonic-gate  * the event.  The responsible nexus is defined in the cookie passed in as
1710Sstevel@tonic-gate  * the third parameter.
1720Sstevel@tonic-gate  * The dip parameter is an artifact of an older implementation in which all
1730Sstevel@tonic-gate  * requests to remove an eventcall would bubble up the tree.  Today, this
1740Sstevel@tonic-gate  * parameter is ignored.
1750Sstevel@tonic-gate  * Input Parameters:
1768912SChris.Horne@Sun.COM  *	dip	- Ignored.
1778912SChris.Horne@Sun.COM  *	rdip	- device driver posting the event
1780Sstevel@tonic-gate  *	cookie	- valid ddi_eventcookie_t, obtained by caller prior to
1790Sstevel@tonic-gate  *		  invocation of this routine
1800Sstevel@tonic-gate  *	impl_data - used by framework
1810Sstevel@tonic-gate  */
1820Sstevel@tonic-gate /*ARGSUSED*/
1830Sstevel@tonic-gate int
ndi_post_event(dev_info_t * dip,dev_info_t * rdip,ddi_eventcookie_t cookie,void * impl_data)1840Sstevel@tonic-gate ndi_post_event(dev_info_t *dip, dev_info_t *rdip,
1850Sstevel@tonic-gate 		ddi_eventcookie_t cookie, void *impl_data)
1860Sstevel@tonic-gate {
1870Sstevel@tonic-gate 	dev_info_t *ddip;
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 	ASSERT(cookie);
1900Sstevel@tonic-gate 	ddip = NDI_EVENT_DDIP(cookie);
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate 	/*
1930Sstevel@tonic-gate 	 * perform sanity checks.  These conditions should never be true.
1940Sstevel@tonic-gate 	 */
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate 	ASSERT(DEVI(ddip)->devi_ops->devo_bus_ops != NULL);
1970Sstevel@tonic-gate 	ASSERT(DEVI(ddip)->devi_ops->devo_bus_ops->busops_rev >= BUSO_REV_6);
1980Sstevel@tonic-gate 	ASSERT(DEVI(ddip)->devi_ops->devo_bus_ops->bus_post_event != NULL);
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate 	/*
2010Sstevel@tonic-gate 	 * post the event to the responsible ancestor
2020Sstevel@tonic-gate 	 */
2030Sstevel@tonic-gate 	return ((*(DEVI(ddip)->devi_ops->devo_bus_ops->bus_post_event))
2047224Scth 	    (ddip, rdip, cookie, impl_data));
2050Sstevel@tonic-gate }
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate /*
2080Sstevel@tonic-gate  * Calls the bus nexus driver's implementation of the
2090Sstevel@tonic-gate  * (*bus_remove_eventcall)() interface.
2100Sstevel@tonic-gate  */
2110Sstevel@tonic-gate int
ndi_busop_remove_eventcall(dev_info_t * ddip,ddi_callback_id_t id)2120Sstevel@tonic-gate ndi_busop_remove_eventcall(dev_info_t *ddip, ddi_callback_id_t id)
2130Sstevel@tonic-gate {
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate 	ASSERT(id);
2160Sstevel@tonic-gate 	/* check for a correct revno before calling up the device tree. */
2170Sstevel@tonic-gate 	ASSERT(DEVI(ddip)->devi_ops->devo_bus_ops != NULL);
2180Sstevel@tonic-gate 	ASSERT(DEVI(ddip)->devi_ops->devo_bus_ops->busops_rev >= BUSO_REV_6);
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate 	if (DEVI(ddip)->devi_ops->devo_bus_ops->bus_remove_eventcall == NULL)
2210Sstevel@tonic-gate 		return (DDI_FAILURE);
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate 	/*
2240Sstevel@tonic-gate 	 * request responsible nexus to remove the eventcall
2250Sstevel@tonic-gate 	 */
2260Sstevel@tonic-gate 	return ((*(DEVI(ddip)->devi_ops->devo_bus_ops->bus_remove_eventcall))
2277224Scth 	    (ddip, id));
2280Sstevel@tonic-gate }
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate /*
2310Sstevel@tonic-gate  * Calls the bus nexus driver's implementation of the
2320Sstevel@tonic-gate  * (*bus_add_eventcall)() interface.  The dip parameter is an
2330Sstevel@tonic-gate  * artifact of an older implementation in which all requests to
2340Sstevel@tonic-gate  * add an eventcall would bubble up the tree.  Today, this parameter is
2350Sstevel@tonic-gate  * ignored.
2360Sstevel@tonic-gate  */
2370Sstevel@tonic-gate /*ARGSUSED*/
2380Sstevel@tonic-gate int
ndi_busop_add_eventcall(dev_info_t * dip,dev_info_t * rdip,ddi_eventcookie_t cookie,void (* callback)(),void * arg,ddi_callback_id_t * cb_id)2390Sstevel@tonic-gate ndi_busop_add_eventcall(dev_info_t *dip, dev_info_t *rdip,
2400Sstevel@tonic-gate 		ddi_eventcookie_t cookie, void (*callback)(), void *arg,
2410Sstevel@tonic-gate 		ddi_callback_id_t *cb_id)
2420Sstevel@tonic-gate {
2430Sstevel@tonic-gate 	dev_info_t *ddip = (dev_info_t *)NDI_EVENT_DDIP(cookie);
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate 	/*
2460Sstevel@tonic-gate 	 * check for a correct revno before calling up the device tree.
2470Sstevel@tonic-gate 	 */
2480Sstevel@tonic-gate 	ASSERT(DEVI(ddip)->devi_ops->devo_bus_ops != NULL);
2490Sstevel@tonic-gate 	ASSERT(DEVI(ddip)->devi_ops->devo_bus_ops->busops_rev >= BUSO_REV_6);
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate 	if (DEVI(ddip)->devi_ops->devo_bus_ops->bus_add_eventcall == NULL)
2520Sstevel@tonic-gate 		return (DDI_FAILURE);
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate 	/*
2550Sstevel@tonic-gate 	 * request responsible ancestor to add the eventcall
2560Sstevel@tonic-gate 	 */
2570Sstevel@tonic-gate 	return ((*(DEVI(ddip)->devi_ops->devo_bus_ops->bus_add_eventcall))
2587224Scth 	    (ddip, rdip, cookie, callback, arg, cb_id));
2590Sstevel@tonic-gate }
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate /*
2620Sstevel@tonic-gate  * Calls the bus nexus driver's implementation of the
2630Sstevel@tonic-gate  * (*bus_get_eventcookie)() interface up the device tree hierarchy.
2640Sstevel@tonic-gate  */
2650Sstevel@tonic-gate int
ndi_busop_get_eventcookie(dev_info_t * dip,dev_info_t * rdip,char * name,ddi_eventcookie_t * event_cookiep)2660Sstevel@tonic-gate ndi_busop_get_eventcookie(dev_info_t *dip, dev_info_t *rdip, char *name,
2670Sstevel@tonic-gate 		ddi_eventcookie_t *event_cookiep)
2680Sstevel@tonic-gate {
2690Sstevel@tonic-gate 	dev_info_t *pdip = (dev_info_t *)DEVI(dip)->devi_parent;
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate 	/* Can not be called from rootnex. */
2720Sstevel@tonic-gate 	ASSERT(pdip);
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate 	/*
2750Sstevel@tonic-gate 	 * check for a correct revno before calling up the device tree.
2760Sstevel@tonic-gate 	 */
2770Sstevel@tonic-gate 	ASSERT(DEVI(pdip)->devi_ops->devo_bus_ops != NULL);
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate 	if ((DEVI(pdip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_6) ||
2800Sstevel@tonic-gate 	    (DEVI(pdip)->devi_ops->devo_bus_ops->bus_get_eventcookie == NULL)) {
2810Sstevel@tonic-gate #ifdef DEBUG
2820Sstevel@tonic-gate 		if ((DEVI(pdip)->devi_ops->devo_bus_ops->busops_rev >=
2830Sstevel@tonic-gate 		    BUSO_REV_3) &&
2840Sstevel@tonic-gate 		    (DEVI(pdip)->devi_ops->devo_bus_ops->bus_get_eventcookie)) {
2850Sstevel@tonic-gate 			cmn_err(CE_WARN,
2860Sstevel@tonic-gate 			    "Warning: %s%d busops_rev=%d no longer supported"
2870Sstevel@tonic-gate 			    " by the NDI event framework.\nBUSO_REV_6 or "
2880Sstevel@tonic-gate 			    "greater must be used.",
2890Sstevel@tonic-gate 			    DEVI(pdip)->devi_binding_name,
2900Sstevel@tonic-gate 			    DEVI(pdip)->devi_instance,
2910Sstevel@tonic-gate 			    DEVI(pdip)->devi_ops->devo_bus_ops->busops_rev);
2920Sstevel@tonic-gate 		}
2930Sstevel@tonic-gate #endif /* DEBUG */
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate 		return (ndi_busop_get_eventcookie(pdip, rdip, name,
2967224Scth 		    event_cookiep));
2970Sstevel@tonic-gate 	}
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate 	return ((*(DEVI(pdip)->devi_ops->devo_bus_ops->bus_get_eventcookie))
3007224Scth 	    (pdip, rdip, name, event_cookiep));
3010Sstevel@tonic-gate }
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate /*
3040Sstevel@tonic-gate  * Copy in the devctl IOCTL data and return a handle to
3050Sstevel@tonic-gate  * the data.
3060Sstevel@tonic-gate  */
3070Sstevel@tonic-gate int
ndi_dc_allochdl(void * iocarg,struct devctl_iocdata ** rdcp)3080Sstevel@tonic-gate ndi_dc_allochdl(void *iocarg, struct devctl_iocdata **rdcp)
3090Sstevel@tonic-gate {
3100Sstevel@tonic-gate 	struct devctl_iocdata *dcp;
3110Sstevel@tonic-gate 	char *cpybuf;
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate 	ASSERT(rdcp != NULL);
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate 	dcp = kmem_zalloc(sizeof (*dcp), KM_SLEEP);
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate 	if (get_udatamodel() == DATAMODEL_NATIVE) {
3180Sstevel@tonic-gate 		if (copyin(iocarg, dcp, sizeof (*dcp)) != 0) {
3190Sstevel@tonic-gate 			kmem_free(dcp, sizeof (*dcp));
3200Sstevel@tonic-gate 			return (NDI_FAULT);
3210Sstevel@tonic-gate 		}
3220Sstevel@tonic-gate 	}
3230Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
3240Sstevel@tonic-gate 	else {
3250Sstevel@tonic-gate 		struct devctl_iocdata32 dcp32;
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate 		if (copyin(iocarg, &dcp32, sizeof (dcp32)) != 0) {
3280Sstevel@tonic-gate 			kmem_free(dcp, sizeof (*dcp));
3290Sstevel@tonic-gate 			return (NDI_FAULT);
3300Sstevel@tonic-gate 		}
3310Sstevel@tonic-gate 		dcp->cmd = (uint_t)dcp32.cmd;
3320Sstevel@tonic-gate 		dcp->flags = (uint_t)dcp32.flags;
3330Sstevel@tonic-gate 		dcp->cpyout_buf = (uint_t *)(uintptr_t)dcp32.cpyout_buf;
3340Sstevel@tonic-gate 		dcp->nvl_user = (nvlist_t *)(uintptr_t)dcp32.nvl_user;
3350Sstevel@tonic-gate 		dcp->nvl_usersz = (size_t)dcp32.nvl_usersz;
3360Sstevel@tonic-gate 		dcp->c_nodename = (char *)(uintptr_t)dcp32.c_nodename;
3370Sstevel@tonic-gate 		dcp->c_unitaddr = (char *)(uintptr_t)dcp32.c_unitaddr;
3380Sstevel@tonic-gate 	}
3390Sstevel@tonic-gate #endif
3400Sstevel@tonic-gate 	if (dcp->c_nodename != NULL) {
3410Sstevel@tonic-gate 		cpybuf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
3420Sstevel@tonic-gate 		if (copyinstr(dcp->c_nodename, cpybuf, MAXNAMELEN, 0) != 0) {
3430Sstevel@tonic-gate 			kmem_free(cpybuf, MAXNAMELEN);
3440Sstevel@tonic-gate 			kmem_free(dcp, sizeof (*dcp));
3450Sstevel@tonic-gate 			return (NDI_FAULT);
3460Sstevel@tonic-gate 		}
3470Sstevel@tonic-gate 		cpybuf[MAXNAMELEN - 1] = '\0';
3480Sstevel@tonic-gate 		dcp->c_nodename = cpybuf;
3490Sstevel@tonic-gate 	}
3500Sstevel@tonic-gate 
3510Sstevel@tonic-gate 	if (dcp->c_unitaddr != NULL) {
3520Sstevel@tonic-gate 		cpybuf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
3530Sstevel@tonic-gate 		if (copyinstr(dcp->c_unitaddr, cpybuf, MAXNAMELEN, 0) != 0) {
3540Sstevel@tonic-gate 			kmem_free(cpybuf, MAXNAMELEN);
3550Sstevel@tonic-gate 			if (dcp->c_nodename != NULL)
3560Sstevel@tonic-gate 				kmem_free(dcp->c_nodename, MAXNAMELEN);
3570Sstevel@tonic-gate 			kmem_free(dcp, sizeof (*dcp));
3580Sstevel@tonic-gate 			return (NDI_FAULT);
3590Sstevel@tonic-gate 		}
3600Sstevel@tonic-gate 		cpybuf[MAXNAMELEN - 1] = '\0';
3610Sstevel@tonic-gate 		dcp->c_unitaddr = cpybuf;
3620Sstevel@tonic-gate 	}
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate 	/*
3650Sstevel@tonic-gate 	 * copyin and unpack a user defined nvlist if one was passed
3660Sstevel@tonic-gate 	 */
3670Sstevel@tonic-gate 	if (dcp->nvl_user != NULL) {
3687263Scth 		if ((dcp->nvl_usersz == 0) ||
3697263Scth 		    (dcp->nvl_usersz > DEVCTL_MAX_NVL_USERSZ)) {
370257Scth 			if (dcp->c_nodename != NULL)
371257Scth 				kmem_free(dcp->c_nodename, MAXNAMELEN);
372257Scth 			if (dcp->c_unitaddr != NULL)
373257Scth 				kmem_free(dcp->c_unitaddr, MAXNAMELEN);
374257Scth 			kmem_free(dcp, sizeof (*dcp));
375257Scth 			return (NDI_FAILURE);
376257Scth 		}
3770Sstevel@tonic-gate 		cpybuf = kmem_alloc(dcp->nvl_usersz, KM_SLEEP);
3780Sstevel@tonic-gate 		if (copyin(dcp->nvl_user, cpybuf, dcp->nvl_usersz) != 0) {
3790Sstevel@tonic-gate 			kmem_free(cpybuf, dcp->nvl_usersz);
3800Sstevel@tonic-gate 			if (dcp->c_nodename != NULL)
3810Sstevel@tonic-gate 				kmem_free(dcp->c_nodename, MAXNAMELEN);
3820Sstevel@tonic-gate 			if (dcp->c_unitaddr != NULL)
3830Sstevel@tonic-gate 				kmem_free(dcp->c_unitaddr, MAXNAMELEN);
3840Sstevel@tonic-gate 			kmem_free(dcp, sizeof (*dcp));
3850Sstevel@tonic-gate 			return (NDI_FAULT);
3860Sstevel@tonic-gate 		}
3870Sstevel@tonic-gate 
3880Sstevel@tonic-gate 		if (nvlist_unpack(cpybuf, dcp->nvl_usersz, &dcp->nvl_user,
3890Sstevel@tonic-gate 		    KM_SLEEP)) {
3900Sstevel@tonic-gate 			kmem_free(cpybuf, dcp->nvl_usersz);
3910Sstevel@tonic-gate 			if (dcp->c_nodename != NULL)
3920Sstevel@tonic-gate 				kmem_free(dcp->c_nodename, MAXNAMELEN);
3930Sstevel@tonic-gate 			if (dcp->c_unitaddr != NULL)
3940Sstevel@tonic-gate 				kmem_free(dcp->c_unitaddr, MAXNAMELEN);
3950Sstevel@tonic-gate 			kmem_free(dcp, sizeof (*dcp));
3960Sstevel@tonic-gate 			return (NDI_FAULT);
3970Sstevel@tonic-gate 		}
3980Sstevel@tonic-gate 		/*
3990Sstevel@tonic-gate 		 * free the buffer containing the packed nvlist
4000Sstevel@tonic-gate 		 */
4010Sstevel@tonic-gate 		kmem_free(cpybuf, dcp->nvl_usersz);
4020Sstevel@tonic-gate 
4030Sstevel@tonic-gate 	}
4040Sstevel@tonic-gate 
4050Sstevel@tonic-gate 	*rdcp = dcp;
4060Sstevel@tonic-gate 	return (NDI_SUCCESS);
4070Sstevel@tonic-gate }
4080Sstevel@tonic-gate 
4090Sstevel@tonic-gate /*
4100Sstevel@tonic-gate  * free all space allocated to a handle.
4110Sstevel@tonic-gate  */
4120Sstevel@tonic-gate void
ndi_dc_freehdl(struct devctl_iocdata * dcp)4130Sstevel@tonic-gate ndi_dc_freehdl(struct devctl_iocdata *dcp)
4140Sstevel@tonic-gate {
4150Sstevel@tonic-gate 	ASSERT(dcp != NULL);
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate 	if (dcp->c_nodename != NULL)
4180Sstevel@tonic-gate 		kmem_free(dcp->c_nodename, MAXNAMELEN);
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate 	if (dcp->c_unitaddr != NULL)
4210Sstevel@tonic-gate 		kmem_free(dcp->c_unitaddr, MAXNAMELEN);
4220Sstevel@tonic-gate 
4230Sstevel@tonic-gate 	if (dcp->nvl_user != NULL)
4240Sstevel@tonic-gate 		nvlist_free(dcp->nvl_user);
4250Sstevel@tonic-gate 
4260Sstevel@tonic-gate 	kmem_free(dcp, sizeof (*dcp));
4270Sstevel@tonic-gate }
4280Sstevel@tonic-gate 
4290Sstevel@tonic-gate char *
ndi_dc_getname(struct devctl_iocdata * dcp)4300Sstevel@tonic-gate ndi_dc_getname(struct devctl_iocdata *dcp)
4310Sstevel@tonic-gate {
4320Sstevel@tonic-gate 	ASSERT(dcp != NULL);
4330Sstevel@tonic-gate 	return (dcp->c_nodename);
4340Sstevel@tonic-gate 
4350Sstevel@tonic-gate }
4360Sstevel@tonic-gate 
4370Sstevel@tonic-gate char *
ndi_dc_getaddr(struct devctl_iocdata * dcp)4380Sstevel@tonic-gate ndi_dc_getaddr(struct devctl_iocdata *dcp)
4390Sstevel@tonic-gate {
4400Sstevel@tonic-gate 	ASSERT(dcp != NULL);
4410Sstevel@tonic-gate 	return (dcp->c_unitaddr);
4420Sstevel@tonic-gate }
4430Sstevel@tonic-gate 
4440Sstevel@tonic-gate nvlist_t *
ndi_dc_get_ap_data(struct devctl_iocdata * dcp)4450Sstevel@tonic-gate ndi_dc_get_ap_data(struct devctl_iocdata *dcp)
4460Sstevel@tonic-gate {
4470Sstevel@tonic-gate 	ASSERT(dcp != NULL);
4480Sstevel@tonic-gate 
4490Sstevel@tonic-gate 	return (dcp->nvl_user);
4500Sstevel@tonic-gate }
4510Sstevel@tonic-gate 
4520Sstevel@tonic-gate /*
4530Sstevel@tonic-gate  * Transition the child named by "devname@devaddr" to the online state.
4540Sstevel@tonic-gate  * For use by a driver's DEVCTL_DEVICE_ONLINE handler.
4550Sstevel@tonic-gate  */
4560Sstevel@tonic-gate int
ndi_devctl_device_online(dev_info_t * dip,struct devctl_iocdata * dcp,uint_t flags)4570Sstevel@tonic-gate ndi_devctl_device_online(dev_info_t *dip, struct devctl_iocdata *dcp,
4580Sstevel@tonic-gate 	uint_t flags)
4590Sstevel@tonic-gate {
4600Sstevel@tonic-gate 	int	rval;
4610Sstevel@tonic-gate 	char	*name;
4620Sstevel@tonic-gate 	dev_info_t *rdip;
4630Sstevel@tonic-gate 
4640Sstevel@tonic-gate 	if (ndi_dc_getname(dcp) == NULL || ndi_dc_getaddr(dcp) == NULL)
4650Sstevel@tonic-gate 		return (EINVAL);
4660Sstevel@tonic-gate 
4670Sstevel@tonic-gate 	name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
4680Sstevel@tonic-gate 	(void) snprintf(name, MAXNAMELEN, "%s@%s",
4697224Scth 	    ndi_dc_getname(dcp), ndi_dc_getaddr(dcp));
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate 	if ((rval = ndi_devi_config_one(dip, name, &rdip,
4720Sstevel@tonic-gate 	    flags | NDI_DEVI_ONLINE | NDI_CONFIG)) == NDI_SUCCESS) {
4730Sstevel@tonic-gate 		ndi_rele_devi(rdip);
4740Sstevel@tonic-gate 
4750Sstevel@tonic-gate 		/*
4760Sstevel@tonic-gate 		 * Invalidate devfs cached directory contents. For the checks
4770Sstevel@tonic-gate 		 * in the "if" condition see the comment in ndi_devi_online().
4780Sstevel@tonic-gate 		 */
4791333Scth 		if (i_ddi_devi_attached(dip) && !DEVI_BUSY_OWNED(dip))
4800Sstevel@tonic-gate 			(void) devfs_clean(dip, NULL, 0);
4810Sstevel@tonic-gate 
4820Sstevel@tonic-gate 	} else if (rval == NDI_BUSY) {
4830Sstevel@tonic-gate 		rval = EBUSY;
4840Sstevel@tonic-gate 	} else if (rval == NDI_FAILURE) {
4850Sstevel@tonic-gate 		rval = EIO;
4860Sstevel@tonic-gate 	}
4870Sstevel@tonic-gate 
4880Sstevel@tonic-gate 	NDI_DEBUG(flags, (CE_CONT, "%s%d: online: %s: %s\n",
4897224Scth 	    ddi_driver_name(dip), ddi_get_instance(dip), name,
4907224Scth 	    ((rval == NDI_SUCCESS) ? "ok" : "failed")));
4910Sstevel@tonic-gate 
4920Sstevel@tonic-gate 	kmem_free(name, MAXNAMELEN);
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate 	return (rval);
4950Sstevel@tonic-gate }
4960Sstevel@tonic-gate 
4970Sstevel@tonic-gate /*
4980Sstevel@tonic-gate  * Transition the child named by "devname@devaddr" to the offline state.
4990Sstevel@tonic-gate  * For use by a driver's DEVCTL_DEVICE_OFFLINE handler.
5000Sstevel@tonic-gate  */
5010Sstevel@tonic-gate int
ndi_devctl_device_offline(dev_info_t * dip,struct devctl_iocdata * dcp,uint_t flags)5020Sstevel@tonic-gate ndi_devctl_device_offline(dev_info_t *dip, struct devctl_iocdata *dcp,
5030Sstevel@tonic-gate 	uint_t flags)
5040Sstevel@tonic-gate {
5050Sstevel@tonic-gate 	int	rval;
5060Sstevel@tonic-gate 	char	*name;
5070Sstevel@tonic-gate 
5080Sstevel@tonic-gate 	if (ndi_dc_getname(dcp) == NULL || ndi_dc_getaddr(dcp) == NULL)
5090Sstevel@tonic-gate 		return (EINVAL);
5100Sstevel@tonic-gate 
5110Sstevel@tonic-gate 	name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
5120Sstevel@tonic-gate 	(void) snprintf(name, MAXNAMELEN, "%s@%s",
5137224Scth 	    ndi_dc_getname(dcp), ndi_dc_getaddr(dcp));
5140Sstevel@tonic-gate 
5154411Svikram 	(void) devfs_clean(dip, name, DV_CLEAN_FORCE);
5164411Svikram 	rval = ndi_devi_unconfig_one(dip, name, NULL,
5174411Svikram 	    flags | NDI_DEVI_OFFLINE);
5184411Svikram 
5194411Svikram 	if (rval == NDI_BUSY) {
5200Sstevel@tonic-gate 		rval = EBUSY;
5214411Svikram 	} else if (rval == NDI_FAILURE) {
5224411Svikram 		rval = EIO;
5230Sstevel@tonic-gate 	}
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate 	NDI_DEBUG(flags, (CE_CONT, "%s%d: offline: %s: %s\n",
5267224Scth 	    ddi_driver_name(dip), ddi_get_instance(dip), name,
5277224Scth 	    (rval == NDI_SUCCESS) ? "ok" : "failed"));
5280Sstevel@tonic-gate 
5290Sstevel@tonic-gate 	kmem_free(name, MAXNAMELEN);
5300Sstevel@tonic-gate 
5310Sstevel@tonic-gate 	return (rval);
5320Sstevel@tonic-gate }
5330Sstevel@tonic-gate 
5340Sstevel@tonic-gate /*
5350Sstevel@tonic-gate  * Remove the child named by "devname@devaddr".
5360Sstevel@tonic-gate  * For use by a driver's DEVCTL_DEVICE_REMOVE handler.
5370Sstevel@tonic-gate  */
5380Sstevel@tonic-gate int
ndi_devctl_device_remove(dev_info_t * dip,struct devctl_iocdata * dcp,uint_t flags)5390Sstevel@tonic-gate ndi_devctl_device_remove(dev_info_t *dip, struct devctl_iocdata *dcp,
5400Sstevel@tonic-gate 	uint_t flags)
5410Sstevel@tonic-gate {
5420Sstevel@tonic-gate 	int	rval;
5430Sstevel@tonic-gate 	char	*name;
5440Sstevel@tonic-gate 
5450Sstevel@tonic-gate 	if (ndi_dc_getname(dcp) == NULL || ndi_dc_getaddr(dcp) == NULL)
5460Sstevel@tonic-gate 		return (EINVAL);
5470Sstevel@tonic-gate 
5480Sstevel@tonic-gate 	name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
5490Sstevel@tonic-gate 	(void) snprintf(name, MAXNAMELEN, "%s@%s",
5507224Scth 	    ndi_dc_getname(dcp), ndi_dc_getaddr(dcp));
5510Sstevel@tonic-gate 
5520Sstevel@tonic-gate 	(void) devfs_clean(dip, name, DV_CLEAN_FORCE);
5530Sstevel@tonic-gate 
5540Sstevel@tonic-gate 	rval = ndi_devi_unconfig_one(dip, name, NULL, flags | NDI_DEVI_REMOVE);
5550Sstevel@tonic-gate 
5560Sstevel@tonic-gate 	if (rval == NDI_BUSY) {
5570Sstevel@tonic-gate 		rval = EBUSY;
5580Sstevel@tonic-gate 	} else if (rval == NDI_FAILURE) {
5590Sstevel@tonic-gate 		rval = EIO;
5600Sstevel@tonic-gate 	}
5610Sstevel@tonic-gate 
5620Sstevel@tonic-gate 	NDI_DEBUG(flags, (CE_CONT, "%s%d: remove: %s: %s\n",
5637224Scth 	    ddi_driver_name(dip), ddi_get_instance(dip), name,
5647224Scth 	    (rval == NDI_SUCCESS) ? "ok" : "failed"));
5650Sstevel@tonic-gate 
5660Sstevel@tonic-gate 	kmem_free(name, MAXNAMELEN);
5670Sstevel@tonic-gate 
5680Sstevel@tonic-gate 	return (rval);
5690Sstevel@tonic-gate }
5700Sstevel@tonic-gate 
5710Sstevel@tonic-gate /*
5720Sstevel@tonic-gate  * Return devctl state of the child named by "name@addr".
5730Sstevel@tonic-gate  * For use by a driver's DEVCTL_DEVICE_GETSTATE handler.
5740Sstevel@tonic-gate  */
5750Sstevel@tonic-gate int
ndi_devctl_device_getstate(dev_info_t * parent,struct devctl_iocdata * dcp,uint_t * state)5760Sstevel@tonic-gate ndi_devctl_device_getstate(dev_info_t *parent, struct devctl_iocdata *dcp,
5770Sstevel@tonic-gate 	uint_t *state)
5780Sstevel@tonic-gate {
5790Sstevel@tonic-gate 	dev_info_t *dip;
5800Sstevel@tonic-gate 	char *name, *addr;
5810Sstevel@tonic-gate 	char *devname;
5820Sstevel@tonic-gate 	int devnamelen;
5830Sstevel@tonic-gate 	int circ;
5840Sstevel@tonic-gate 
5850Sstevel@tonic-gate 	if (parent == NULL ||
5860Sstevel@tonic-gate 	    ((name = ndi_dc_getname(dcp)) == NULL) ||
5870Sstevel@tonic-gate 	    ((addr = ndi_dc_getaddr(dcp)) == NULL))
5880Sstevel@tonic-gate 		return (NDI_FAILURE);
5890Sstevel@tonic-gate 
5900Sstevel@tonic-gate 	devnamelen = strlen(name) + strlen(addr) + 2;
5910Sstevel@tonic-gate 	devname = kmem_alloc(devnamelen, KM_SLEEP);
5920Sstevel@tonic-gate 	if (strlen(addr) > 0) {
5930Sstevel@tonic-gate 		(void) snprintf(devname, devnamelen, "%s@%s", name, addr);
5940Sstevel@tonic-gate 	} else {
5950Sstevel@tonic-gate 		(void) snprintf(devname, devnamelen, "%s", name);
5960Sstevel@tonic-gate 	}
5970Sstevel@tonic-gate 
5980Sstevel@tonic-gate 	ndi_devi_enter(parent, &circ);
5990Sstevel@tonic-gate 
6000Sstevel@tonic-gate 	dip = ndi_devi_findchild(parent, devname);
6010Sstevel@tonic-gate 	kmem_free(devname, devnamelen);
6020Sstevel@tonic-gate 
6030Sstevel@tonic-gate 	if (dip == NULL) {
6040Sstevel@tonic-gate 		ndi_devi_exit(parent, circ);
6050Sstevel@tonic-gate 		return (NDI_FAILURE);
6060Sstevel@tonic-gate 	}
6070Sstevel@tonic-gate 
6080Sstevel@tonic-gate 	mutex_enter(&(DEVI(dip)->devi_lock));
6090Sstevel@tonic-gate 	if (DEVI_IS_DEVICE_OFFLINE(dip)) {
6100Sstevel@tonic-gate 		*state = DEVICE_OFFLINE;
6110Sstevel@tonic-gate 	} else if (DEVI_IS_DEVICE_DOWN(dip)) {
6120Sstevel@tonic-gate 		*state = DEVICE_DOWN;
6130Sstevel@tonic-gate 	} else {
6140Sstevel@tonic-gate 		*state = DEVICE_ONLINE;
6150Sstevel@tonic-gate 		if (devi_stillreferenced(dip) == DEVI_REFERENCED)
6160Sstevel@tonic-gate 			*state |= DEVICE_BUSY;
6170Sstevel@tonic-gate 	}
6180Sstevel@tonic-gate 
6190Sstevel@tonic-gate 	mutex_exit(&(DEVI(dip)->devi_lock));
6200Sstevel@tonic-gate 	ndi_devi_exit(parent, circ);
6210Sstevel@tonic-gate 
6220Sstevel@tonic-gate 	return (NDI_SUCCESS);
6230Sstevel@tonic-gate }
6240Sstevel@tonic-gate 
6250Sstevel@tonic-gate /*
6260Sstevel@tonic-gate  * return the current state of the device "dip"
6270Sstevel@tonic-gate  *
6280Sstevel@tonic-gate  * recommend using ndi_devctl_ioctl() or
6290Sstevel@tonic-gate  * ndi_devctl_device_getstate() instead
6300Sstevel@tonic-gate  */
6310Sstevel@tonic-gate int
ndi_dc_return_dev_state(dev_info_t * dip,struct devctl_iocdata * dcp)6320Sstevel@tonic-gate ndi_dc_return_dev_state(dev_info_t *dip, struct devctl_iocdata *dcp)
6330Sstevel@tonic-gate {
6340Sstevel@tonic-gate 	dev_info_t *pdip;
6350Sstevel@tonic-gate 	uint_t devstate = 0;
6360Sstevel@tonic-gate 	int circ;
6370Sstevel@tonic-gate 
6380Sstevel@tonic-gate 	if ((dip == NULL) || (dcp == NULL))
6390Sstevel@tonic-gate 		return (NDI_FAILURE);
6400Sstevel@tonic-gate 
6410Sstevel@tonic-gate 	pdip = ddi_get_parent(dip);
6420Sstevel@tonic-gate 
6430Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
6440Sstevel@tonic-gate 	mutex_enter(&(DEVI(dip)->devi_lock));
6450Sstevel@tonic-gate 	if (DEVI_IS_DEVICE_OFFLINE(dip)) {
6460Sstevel@tonic-gate 		devstate = DEVICE_OFFLINE;
6470Sstevel@tonic-gate 	} else if (DEVI_IS_DEVICE_DOWN(dip)) {
6480Sstevel@tonic-gate 		devstate = DEVICE_DOWN;
6490Sstevel@tonic-gate 	} else {
6500Sstevel@tonic-gate 		devstate = DEVICE_ONLINE;
6510Sstevel@tonic-gate 		if (devi_stillreferenced(dip) == DEVI_REFERENCED)
6520Sstevel@tonic-gate 			devstate |= DEVICE_BUSY;
6530Sstevel@tonic-gate 	}
6540Sstevel@tonic-gate 
6550Sstevel@tonic-gate 	mutex_exit(&(DEVI(dip)->devi_lock));
6560Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
6570Sstevel@tonic-gate 
6580Sstevel@tonic-gate 	if (copyout(&devstate, dcp->cpyout_buf, sizeof (uint_t)) != 0)
6590Sstevel@tonic-gate 		return (NDI_FAULT);
6600Sstevel@tonic-gate 
6610Sstevel@tonic-gate 	return (NDI_SUCCESS);
6620Sstevel@tonic-gate }
6630Sstevel@tonic-gate 
6640Sstevel@tonic-gate /*
6650Sstevel@tonic-gate  * Return device's bus state
6660Sstevel@tonic-gate  * For use by a driver's DEVCTL_BUS_GETSTATE handler.
6670Sstevel@tonic-gate  */
6680Sstevel@tonic-gate int
ndi_devctl_bus_getstate(dev_info_t * dip,struct devctl_iocdata * dcp,uint_t * state)6690Sstevel@tonic-gate ndi_devctl_bus_getstate(dev_info_t *dip, struct devctl_iocdata *dcp,
6700Sstevel@tonic-gate 	uint_t *state)
6710Sstevel@tonic-gate {
6720Sstevel@tonic-gate 	if ((dip == NULL) || (dcp == NULL))
6730Sstevel@tonic-gate 		return (NDI_FAILURE);
6740Sstevel@tonic-gate 
6750Sstevel@tonic-gate 	return (ndi_get_bus_state(dip, state));
6760Sstevel@tonic-gate }
6770Sstevel@tonic-gate 
6780Sstevel@tonic-gate /*
6790Sstevel@tonic-gate  * Generic devctl ioctl handler
6800Sstevel@tonic-gate  */
6810Sstevel@tonic-gate int
ndi_devctl_ioctl(dev_info_t * dip,int cmd,intptr_t arg,int mode,uint_t flags)6820Sstevel@tonic-gate ndi_devctl_ioctl(dev_info_t *dip, int cmd, intptr_t arg, int mode, uint_t flags)
6830Sstevel@tonic-gate {
6840Sstevel@tonic-gate 	_NOTE(ARGUNUSED(mode))
6850Sstevel@tonic-gate 	struct devctl_iocdata *dcp;
6860Sstevel@tonic-gate 	uint_t state;
6870Sstevel@tonic-gate 	int rval = ENOTTY;
6880Sstevel@tonic-gate 
6890Sstevel@tonic-gate 	/*
6900Sstevel@tonic-gate 	 * read devctl ioctl data
6910Sstevel@tonic-gate 	 */
6920Sstevel@tonic-gate 	if (ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS)
6930Sstevel@tonic-gate 		return (EFAULT);
6940Sstevel@tonic-gate 
6950Sstevel@tonic-gate 	switch (cmd) {
6960Sstevel@tonic-gate 
6970Sstevel@tonic-gate 	case DEVCTL_BUS_GETSTATE:
6980Sstevel@tonic-gate 		rval = ndi_devctl_bus_getstate(dip, dcp, &state);
6990Sstevel@tonic-gate 		if (rval == NDI_SUCCESS) {
7000Sstevel@tonic-gate 			if (copyout(&state, dcp->cpyout_buf,
7010Sstevel@tonic-gate 			    sizeof (uint_t)) != 0)
7020Sstevel@tonic-gate 				rval = NDI_FAULT;
7030Sstevel@tonic-gate 		}
7040Sstevel@tonic-gate 		break;
7050Sstevel@tonic-gate 
7060Sstevel@tonic-gate 	case DEVCTL_DEVICE_ONLINE:
7070Sstevel@tonic-gate 		rval = ndi_devctl_device_online(dip, dcp, flags);
7080Sstevel@tonic-gate 		break;
7090Sstevel@tonic-gate 
7100Sstevel@tonic-gate 	case DEVCTL_DEVICE_OFFLINE:
7110Sstevel@tonic-gate 		rval = ndi_devctl_device_offline(dip, dcp, flags);
7120Sstevel@tonic-gate 		break;
7130Sstevel@tonic-gate 
7140Sstevel@tonic-gate 	case DEVCTL_DEVICE_GETSTATE:
7150Sstevel@tonic-gate 		rval = ndi_devctl_device_getstate(dip, dcp, &state);
7160Sstevel@tonic-gate 		if (rval == NDI_SUCCESS) {
7170Sstevel@tonic-gate 			if (copyout(&state, dcp->cpyout_buf,
7180Sstevel@tonic-gate 			    sizeof (uint_t)) != 0)
7190Sstevel@tonic-gate 				rval = NDI_FAULT;
7200Sstevel@tonic-gate 		}
7210Sstevel@tonic-gate 		break;
7220Sstevel@tonic-gate 
7230Sstevel@tonic-gate 	case DEVCTL_DEVICE_REMOVE:
7240Sstevel@tonic-gate 		rval = ndi_devctl_device_remove(dip, dcp, flags);
7250Sstevel@tonic-gate 		break;
7260Sstevel@tonic-gate 
7270Sstevel@tonic-gate 	case DEVCTL_BUS_DEV_CREATE:
7280Sstevel@tonic-gate 		rval = ndi_dc_devi_create(dcp, dip, 0, NULL);
7290Sstevel@tonic-gate 		break;
7300Sstevel@tonic-gate 
7310Sstevel@tonic-gate 	/*
7320Sstevel@tonic-gate 	 * ioctls for which a generic implementation makes no sense
7330Sstevel@tonic-gate 	 */
7340Sstevel@tonic-gate 	case DEVCTL_BUS_RESET:
7350Sstevel@tonic-gate 	case DEVCTL_BUS_RESETALL:
7360Sstevel@tonic-gate 	case DEVCTL_DEVICE_RESET:
7370Sstevel@tonic-gate 	case DEVCTL_AP_CONNECT:
7380Sstevel@tonic-gate 	case DEVCTL_AP_DISCONNECT:
7390Sstevel@tonic-gate 	case DEVCTL_AP_INSERT:
7400Sstevel@tonic-gate 	case DEVCTL_AP_REMOVE:
7410Sstevel@tonic-gate 	case DEVCTL_AP_CONFIGURE:
7420Sstevel@tonic-gate 	case DEVCTL_AP_UNCONFIGURE:
7430Sstevel@tonic-gate 	case DEVCTL_AP_GETSTATE:
7440Sstevel@tonic-gate 	case DEVCTL_AP_CONTROL:
7450Sstevel@tonic-gate 	case DEVCTL_BUS_QUIESCE:
7460Sstevel@tonic-gate 	case DEVCTL_BUS_UNQUIESCE:
7470Sstevel@tonic-gate 		rval = ENOTSUP;
7480Sstevel@tonic-gate 		break;
7490Sstevel@tonic-gate 	}
7500Sstevel@tonic-gate 
7510Sstevel@tonic-gate 	ndi_dc_freehdl(dcp);
7520Sstevel@tonic-gate 	return (rval);
7530Sstevel@tonic-gate }
7540Sstevel@tonic-gate 
7550Sstevel@tonic-gate /*
7560Sstevel@tonic-gate  * Copyout the state of the Attachment Point "ap" to the requesting
7570Sstevel@tonic-gate  * user process.
7580Sstevel@tonic-gate  */
7590Sstevel@tonic-gate int
ndi_dc_return_ap_state(devctl_ap_state_t * ap,struct devctl_iocdata * dcp)7600Sstevel@tonic-gate ndi_dc_return_ap_state(devctl_ap_state_t *ap, struct devctl_iocdata *dcp)
7610Sstevel@tonic-gate {
7620Sstevel@tonic-gate 	if ((ap == NULL) || (dcp == NULL))
7630Sstevel@tonic-gate 		return (NDI_FAILURE);
7640Sstevel@tonic-gate 
7650Sstevel@tonic-gate 
7660Sstevel@tonic-gate 	if (get_udatamodel() == DATAMODEL_NATIVE) {
7670Sstevel@tonic-gate 		if (copyout(ap, dcp->cpyout_buf,
7687224Scth 		    sizeof (devctl_ap_state_t)) != 0)
7697224Scth 			return (NDI_FAULT);
7700Sstevel@tonic-gate 	}
7710Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
7720Sstevel@tonic-gate 	else {
7730Sstevel@tonic-gate 		struct devctl_ap_state32 ap_state32;
7740Sstevel@tonic-gate 
7750Sstevel@tonic-gate 		ap_state32.ap_rstate = ap->ap_rstate;
7760Sstevel@tonic-gate 		ap_state32.ap_ostate = ap->ap_ostate;
7770Sstevel@tonic-gate 		ap_state32.ap_condition = ap->ap_condition;
7780Sstevel@tonic-gate 		ap_state32.ap_error_code = ap->ap_error_code;
7790Sstevel@tonic-gate 		ap_state32.ap_in_transition = ap->ap_in_transition;
7800Sstevel@tonic-gate 		ap_state32.ap_last_change = (time32_t)ap->ap_last_change;
7810Sstevel@tonic-gate 		if (copyout(&ap_state32, dcp->cpyout_buf,
7827224Scth 		    sizeof (devctl_ap_state32_t)) != 0)
7837224Scth 			return (NDI_FAULT);
7840Sstevel@tonic-gate 	}
7850Sstevel@tonic-gate #endif
7860Sstevel@tonic-gate 
7870Sstevel@tonic-gate 	return (NDI_SUCCESS);
7880Sstevel@tonic-gate }
7890Sstevel@tonic-gate 
7900Sstevel@tonic-gate /*
7910Sstevel@tonic-gate  * Copyout the bus state of the bus nexus device "dip" to the requesting
7920Sstevel@tonic-gate  * user process.
7930Sstevel@tonic-gate  */
7940Sstevel@tonic-gate int
ndi_dc_return_bus_state(dev_info_t * dip,struct devctl_iocdata * dcp)7950Sstevel@tonic-gate ndi_dc_return_bus_state(dev_info_t *dip, struct devctl_iocdata *dcp)
7960Sstevel@tonic-gate {
7970Sstevel@tonic-gate 	uint_t devstate = 0;
7980Sstevel@tonic-gate 
7990Sstevel@tonic-gate 	if ((dip == NULL) || (dcp == NULL))
8000Sstevel@tonic-gate 		return (NDI_FAILURE);
8010Sstevel@tonic-gate 
8020Sstevel@tonic-gate 	if (ndi_get_bus_state(dip, &devstate) != NDI_SUCCESS)
8030Sstevel@tonic-gate 		return (NDI_FAILURE);
8040Sstevel@tonic-gate 
8050Sstevel@tonic-gate 	if (copyout(&devstate, dcp->cpyout_buf, sizeof (uint_t)) != 0)
8060Sstevel@tonic-gate 		return (NDI_FAULT);
8070Sstevel@tonic-gate 
8080Sstevel@tonic-gate 	return (NDI_SUCCESS);
8090Sstevel@tonic-gate }
8100Sstevel@tonic-gate 
8110Sstevel@tonic-gate static int
8120Sstevel@tonic-gate i_dc_devi_create(struct devctl_iocdata *, dev_info_t *, dev_info_t **);
8130Sstevel@tonic-gate 
8140Sstevel@tonic-gate /*
8150Sstevel@tonic-gate  * create a child device node given the property definitions
8160Sstevel@tonic-gate  * supplied by the userland process
8170Sstevel@tonic-gate  */
8180Sstevel@tonic-gate int
ndi_dc_devi_create(struct devctl_iocdata * dcp,dev_info_t * pdip,int flags,dev_info_t ** rdip)8190Sstevel@tonic-gate ndi_dc_devi_create(struct devctl_iocdata *dcp, dev_info_t *pdip, int flags,
8200Sstevel@tonic-gate     dev_info_t **rdip)
8210Sstevel@tonic-gate {
8220Sstevel@tonic-gate 	dev_info_t *cdip;
8230Sstevel@tonic-gate 	int rv, circular = 0;
8240Sstevel@tonic-gate 	char devnm[MAXNAMELEN];
8250Sstevel@tonic-gate 	int nmlen;
8260Sstevel@tonic-gate 
8270Sstevel@tonic-gate 	/*
8280Sstevel@tonic-gate 	 * The child device may have been pre-constructed by an earlier
8290Sstevel@tonic-gate 	 * call to this function with the flag DEVCTL_CONSTRUCT set.
8300Sstevel@tonic-gate 	 */
8310Sstevel@tonic-gate 
8320Sstevel@tonic-gate 	if ((cdip = (rdip != NULL) ? *rdip : NULL) == NULL)
8330Sstevel@tonic-gate 		if ((rv = i_dc_devi_create(dcp, pdip, &cdip)) != 0)
8340Sstevel@tonic-gate 			return (rv);
8350Sstevel@tonic-gate 
8360Sstevel@tonic-gate 	ASSERT(cdip != NULL);
8370Sstevel@tonic-gate 
8380Sstevel@tonic-gate 	/*
8390Sstevel@tonic-gate 	 * Return the device node partially constructed if the
8400Sstevel@tonic-gate 	 * DEVCTL_CONSTRUCT flag is set.
8410Sstevel@tonic-gate 	 */
8420Sstevel@tonic-gate 	if (flags & DEVCTL_CONSTRUCT) {
8430Sstevel@tonic-gate 		if (rdip == NULL) {
8440Sstevel@tonic-gate 			(void) ndi_devi_free(cdip);
8450Sstevel@tonic-gate 			return (EINVAL);
8460Sstevel@tonic-gate 		}
8470Sstevel@tonic-gate 		*rdip = cdip;
8480Sstevel@tonic-gate 		return (0);
8490Sstevel@tonic-gate 	}
8500Sstevel@tonic-gate 
8510Sstevel@tonic-gate 	/*
8520Sstevel@tonic-gate 	 * Bring the node up to a named but OFFLINE state.  The calling
8530Sstevel@tonic-gate 	 * application will need to manage the node from here on.
8540Sstevel@tonic-gate 	 */
8550Sstevel@tonic-gate 	if (dcp->flags & DEVCTL_OFFLINE) {
8560Sstevel@tonic-gate 		/*
8574845Svikram 		 * In the unlikely event that the dip was somehow attached by
8584845Svikram 		 * the userland process (and device contracts or LDI opens
8594845Svikram 		 * were registered against the dip) after it was created by
8604845Svikram 		 * a previous DEVCTL_CONSTRUCT call, we start notify
8614845Svikram 		 * proceedings on this dip. Note that we don't need to
8624845Svikram 		 * return the dip after a failure of the notify since
8634845Svikram 		 * for a contract or LDI handle to be created the dip was
8644845Svikram 		 * already available to the user.
8654845Svikram 		 */
8664845Svikram 		if (e_ddi_offline_notify(cdip) == DDI_FAILURE) {
8674845Svikram 			return (EBUSY);
8684845Svikram 		}
8694845Svikram 
8704845Svikram 		/*
8710Sstevel@tonic-gate 		 * hand set the OFFLINE flag to prevent any asynchronous
8720Sstevel@tonic-gate 		 * autoconfiguration operations from attaching this node.
8730Sstevel@tonic-gate 		 */
874495Scth 		mutex_enter(&(DEVI(cdip)->devi_lock));
875495Scth 		DEVI_SET_DEVICE_OFFLINE(cdip);
876495Scth 		mutex_exit(&(DEVI(cdip)->devi_lock));
877495Scth 
8784845Svikram 		e_ddi_offline_finalize(cdip, DDI_SUCCESS);
8794845Svikram 
8800Sstevel@tonic-gate 		rv = ndi_devi_bind_driver(cdip, flags);
8810Sstevel@tonic-gate 		if (rv != NDI_SUCCESS) {
8820Sstevel@tonic-gate 			(void) ndi_devi_offline(cdip, NDI_DEVI_REMOVE);
8830Sstevel@tonic-gate 			return (ENXIO);
8840Sstevel@tonic-gate 		}
8850Sstevel@tonic-gate 
8860Sstevel@tonic-gate 		/*
8870Sstevel@tonic-gate 		 * remove the dev_info node if it failed to bind to a
8880Sstevel@tonic-gate 		 * driver above.
8890Sstevel@tonic-gate 		 */
8900Sstevel@tonic-gate 		if (i_ddi_node_state(cdip) < DS_BOUND) {
8910Sstevel@tonic-gate 			(void) ndi_devi_offline(cdip, NDI_DEVI_REMOVE);
8920Sstevel@tonic-gate 			return (ENXIO);
8930Sstevel@tonic-gate 		}
8940Sstevel@tonic-gate 
8950Sstevel@tonic-gate 		/*
8960Sstevel@tonic-gate 		 * add the node to the per-driver list and INITCHILD it
8970Sstevel@tonic-gate 		 * to give it a name.
8980Sstevel@tonic-gate 		 */
8990Sstevel@tonic-gate 		ndi_devi_enter(pdip, &circular);
9000Sstevel@tonic-gate 		if ((rv = ddi_initchild(pdip, cdip)) != DDI_SUCCESS) {
9010Sstevel@tonic-gate 			(void) ndi_devi_offline(cdip, NDI_DEVI_REMOVE);
9020Sstevel@tonic-gate 			ndi_devi_exit(pdip, circular);
9030Sstevel@tonic-gate 			return (EINVAL);
9040Sstevel@tonic-gate 		}
9050Sstevel@tonic-gate 		ndi_devi_exit(pdip, circular);
9060Sstevel@tonic-gate 
9070Sstevel@tonic-gate 	} else {
9080Sstevel@tonic-gate 		/*
9090Sstevel@tonic-gate 		 * Attempt to bring the device ONLINE. If the request to
9100Sstevel@tonic-gate 		 * fails, remove the dev_info node.
9110Sstevel@tonic-gate 		 */
9120Sstevel@tonic-gate 		if (ndi_devi_online(cdip, NDI_ONLINE_ATTACH) != NDI_SUCCESS) {
9130Sstevel@tonic-gate 			(void) ndi_devi_offline(cdip, NDI_DEVI_REMOVE);
9140Sstevel@tonic-gate 			return (ENXIO);
9150Sstevel@tonic-gate 		}
9160Sstevel@tonic-gate 
9170Sstevel@tonic-gate 		/*
9180Sstevel@tonic-gate 		 * if the node was successfully added but there was
9190Sstevel@tonic-gate 		 * no driver available for the device, remove the node
9200Sstevel@tonic-gate 		 */
9210Sstevel@tonic-gate 		if (i_ddi_node_state(cdip) < DS_BOUND) {
9220Sstevel@tonic-gate 			(void) ndi_devi_offline(cdip, NDI_DEVI_REMOVE);
9230Sstevel@tonic-gate 			return (ENODEV);
9240Sstevel@tonic-gate 		}
9250Sstevel@tonic-gate 	}
9260Sstevel@tonic-gate 
9270Sstevel@tonic-gate 	/*
9280Sstevel@tonic-gate 	 * return a handle to the child device
9290Sstevel@tonic-gate 	 * copy out the name of the newly attached child device if
9300Sstevel@tonic-gate 	 * the IOCTL request has provided a copyout buffer.
9310Sstevel@tonic-gate 	 */
9320Sstevel@tonic-gate 	if (rdip != NULL)
9330Sstevel@tonic-gate 		*rdip = cdip;
9340Sstevel@tonic-gate 
9350Sstevel@tonic-gate 	if (dcp->cpyout_buf == NULL)
9360Sstevel@tonic-gate 		return (0);
9370Sstevel@tonic-gate 
9380Sstevel@tonic-gate 	ASSERT(ddi_node_name(cdip) != NULL);
9390Sstevel@tonic-gate 	ASSERT(ddi_get_name_addr(cdip) != NULL);
9400Sstevel@tonic-gate 
9410Sstevel@tonic-gate 	nmlen = snprintf(devnm, MAXNAMELEN, "%s@%s",
9420Sstevel@tonic-gate 	    ddi_node_name(cdip), ddi_get_name_addr(cdip));
9430Sstevel@tonic-gate 
9440Sstevel@tonic-gate 	if (copyout(&devnm, dcp->cpyout_buf, nmlen) != 0) {
9450Sstevel@tonic-gate 		(void) ndi_devi_offline(cdip, NDI_DEVI_REMOVE);
9460Sstevel@tonic-gate 		return (EFAULT);
9470Sstevel@tonic-gate 	}
9480Sstevel@tonic-gate 	return (0);
9490Sstevel@tonic-gate }
9500Sstevel@tonic-gate 
9510Sstevel@tonic-gate static int
i_dc_devi_create(struct devctl_iocdata * dcp,dev_info_t * pdip,dev_info_t ** rdip)9520Sstevel@tonic-gate i_dc_devi_create(struct devctl_iocdata *dcp, dev_info_t *pdip,
9530Sstevel@tonic-gate     dev_info_t **rdip)
9540Sstevel@tonic-gate {
9550Sstevel@tonic-gate 
9560Sstevel@tonic-gate 	dev_info_t *cdip;
9570Sstevel@tonic-gate 	char *cname = NULL;
9580Sstevel@tonic-gate 	nvlist_t *nvlp = dcp->nvl_user;
9590Sstevel@tonic-gate 	nvpair_t *npp;
9600Sstevel@tonic-gate 	char *np;
9610Sstevel@tonic-gate 	int rv = 0;
9620Sstevel@tonic-gate 
9630Sstevel@tonic-gate 	ASSERT(rdip != NULL && *rdip == NULL);
9640Sstevel@tonic-gate 
9650Sstevel@tonic-gate 	if ((nvlp == NULL) ||
9660Sstevel@tonic-gate 	    (nvlist_lookup_string(nvlp, DC_DEVI_NODENAME, &cname) != 0))
9670Sstevel@tonic-gate 		return (EINVAL);
9680Sstevel@tonic-gate 
9690Sstevel@tonic-gate 	/*
9700Sstevel@tonic-gate 	 * construct a new dev_info node with a user-provided nodename
9710Sstevel@tonic-gate 	 */
972789Sahrens 	ndi_devi_alloc_sleep(pdip, cname, (pnode_t)DEVI_SID_NODEID, &cdip);
9730Sstevel@tonic-gate 
9740Sstevel@tonic-gate 	/*
9750Sstevel@tonic-gate 	 * create hardware properties for each member in the property
9760Sstevel@tonic-gate 	 * list.
9770Sstevel@tonic-gate 	 */
9780Sstevel@tonic-gate 	for (npp = nvlist_next_nvpair(nvlp, NULL); (npp != NULL && !rv);
9790Sstevel@tonic-gate 	    npp = nvlist_next_nvpair(nvlp, npp)) {
9800Sstevel@tonic-gate 
9810Sstevel@tonic-gate 		np = nvpair_name(npp);
9820Sstevel@tonic-gate 
9830Sstevel@tonic-gate 		/*
9840Sstevel@tonic-gate 		 * skip the nodename property
9850Sstevel@tonic-gate 		 */
9860Sstevel@tonic-gate 		if (strcmp(np, DC_DEVI_NODENAME) == 0)
9870Sstevel@tonic-gate 			continue;
9880Sstevel@tonic-gate 
9890Sstevel@tonic-gate 		switch (nvpair_type(npp)) {
9900Sstevel@tonic-gate 
9910Sstevel@tonic-gate 		case DATA_TYPE_INT32: {
9920Sstevel@tonic-gate 			int32_t prop_val;
9930Sstevel@tonic-gate 
9940Sstevel@tonic-gate 			if ((rv = nvpair_value_int32(npp, &prop_val)) != 0)
9950Sstevel@tonic-gate 				break;
9960Sstevel@tonic-gate 
9970Sstevel@tonic-gate 			(void) ndi_prop_update_int(DDI_DEV_T_NONE, cdip, np,
9980Sstevel@tonic-gate 			    (int)prop_val);
9990Sstevel@tonic-gate 			break;
10000Sstevel@tonic-gate 		}
10010Sstevel@tonic-gate 
10020Sstevel@tonic-gate 		case DATA_TYPE_STRING: {
10030Sstevel@tonic-gate 			char *prop_val;
10040Sstevel@tonic-gate 
10050Sstevel@tonic-gate 			if ((rv = nvpair_value_string(npp, &prop_val)) != 0)
10060Sstevel@tonic-gate 				break;
10070Sstevel@tonic-gate 
10080Sstevel@tonic-gate 			(void) ndi_prop_update_string(DDI_DEV_T_NONE, cdip,
10090Sstevel@tonic-gate 			    np, prop_val);
10100Sstevel@tonic-gate 			break;
10110Sstevel@tonic-gate 		}
10120Sstevel@tonic-gate 
10130Sstevel@tonic-gate 		case DATA_TYPE_BYTE_ARRAY: {
10140Sstevel@tonic-gate 			uchar_t *val;
10150Sstevel@tonic-gate 			uint_t nelms;
10160Sstevel@tonic-gate 
10170Sstevel@tonic-gate 			if ((rv = nvpair_value_byte_array(npp, &val,
10180Sstevel@tonic-gate 			    &nelms)) != 0)
10190Sstevel@tonic-gate 				break;
10200Sstevel@tonic-gate 
10210Sstevel@tonic-gate 			(void) ndi_prop_update_byte_array(DDI_DEV_T_NONE,
10220Sstevel@tonic-gate 			    cdip, np, (uchar_t *)val, nelms);
10230Sstevel@tonic-gate 			break;
10240Sstevel@tonic-gate 		}
10250Sstevel@tonic-gate 
10260Sstevel@tonic-gate 		case DATA_TYPE_INT32_ARRAY: {
10270Sstevel@tonic-gate 			int32_t *val;
10280Sstevel@tonic-gate 			uint_t nelms;
10290Sstevel@tonic-gate 
10300Sstevel@tonic-gate 			if ((rv = nvpair_value_int32_array(npp, &val,
10310Sstevel@tonic-gate 			    &nelms)) != 0)
10320Sstevel@tonic-gate 				break;
10330Sstevel@tonic-gate 
10340Sstevel@tonic-gate 			(void) ndi_prop_update_int_array(DDI_DEV_T_NONE,
10350Sstevel@tonic-gate 			    cdip, np, val, nelms);
10360Sstevel@tonic-gate 			break;
10370Sstevel@tonic-gate 		}
10380Sstevel@tonic-gate 
10390Sstevel@tonic-gate 		case DATA_TYPE_STRING_ARRAY: {
10400Sstevel@tonic-gate 			char **val;
10410Sstevel@tonic-gate 			uint_t nelms;
10420Sstevel@tonic-gate 
10430Sstevel@tonic-gate 			if ((rv = nvpair_value_string_array(npp, &val,
10440Sstevel@tonic-gate 			    &nelms)) != 0)
10450Sstevel@tonic-gate 				break;
10460Sstevel@tonic-gate 
10470Sstevel@tonic-gate 			(void) ndi_prop_update_string_array(DDI_DEV_T_NONE,
10480Sstevel@tonic-gate 			    cdip, np, val, nelms);
10490Sstevel@tonic-gate 			break;
10500Sstevel@tonic-gate 		}
10510Sstevel@tonic-gate 
10520Sstevel@tonic-gate 		/*
10530Sstevel@tonic-gate 		 * unsupported property data type
10540Sstevel@tonic-gate 		 */
10550Sstevel@tonic-gate 		default:
10560Sstevel@tonic-gate 			rv = EINVAL;
10570Sstevel@tonic-gate 		}
10580Sstevel@tonic-gate 	}
10590Sstevel@tonic-gate 
10600Sstevel@tonic-gate 	/*
10610Sstevel@tonic-gate 	 * something above failed
10620Sstevel@tonic-gate 	 * destroy the partially child device and abort the request
10630Sstevel@tonic-gate 	 */
10640Sstevel@tonic-gate 	if (rv != 0) {
10650Sstevel@tonic-gate 		(void) ndi_devi_free(cdip);
10660Sstevel@tonic-gate 		return (rv);
10670Sstevel@tonic-gate 	}
10680Sstevel@tonic-gate 
10690Sstevel@tonic-gate 	*rdip = cdip;
10700Sstevel@tonic-gate 	return (0);
10710Sstevel@tonic-gate }
10720Sstevel@tonic-gate 
10730Sstevel@tonic-gate /*
10740Sstevel@tonic-gate  * return current soft bus state of bus nexus "dip"
10750Sstevel@tonic-gate  */
10760Sstevel@tonic-gate int
ndi_get_bus_state(dev_info_t * dip,uint_t * rstate)10770Sstevel@tonic-gate ndi_get_bus_state(dev_info_t *dip, uint_t *rstate)
10780Sstevel@tonic-gate {
10790Sstevel@tonic-gate 	if (dip == NULL || rstate == NULL)
10800Sstevel@tonic-gate 		return (NDI_FAILURE);
10810Sstevel@tonic-gate 
10820Sstevel@tonic-gate 	if (DEVI(dip)->devi_ops->devo_bus_ops == NULL)
10830Sstevel@tonic-gate 		return (NDI_FAILURE);
10840Sstevel@tonic-gate 
10850Sstevel@tonic-gate 	mutex_enter(&(DEVI(dip)->devi_lock));
10860Sstevel@tonic-gate 	if (DEVI_IS_BUS_QUIESCED(dip))
10870Sstevel@tonic-gate 		*rstate = BUS_QUIESCED;
10880Sstevel@tonic-gate 	else if (DEVI_IS_BUS_DOWN(dip))
10890Sstevel@tonic-gate 		*rstate = BUS_SHUTDOWN;
10900Sstevel@tonic-gate 	else
10910Sstevel@tonic-gate 		*rstate = BUS_ACTIVE;
10920Sstevel@tonic-gate 	mutex_exit(&(DEVI(dip)->devi_lock));
10930Sstevel@tonic-gate 	return (NDI_SUCCESS);
10940Sstevel@tonic-gate }
10950Sstevel@tonic-gate 
10960Sstevel@tonic-gate /*
10970Sstevel@tonic-gate  * Set the soft state of bus nexus "dip"
10980Sstevel@tonic-gate  */
10990Sstevel@tonic-gate int
ndi_set_bus_state(dev_info_t * dip,uint_t state)11000Sstevel@tonic-gate ndi_set_bus_state(dev_info_t *dip, uint_t state)
11010Sstevel@tonic-gate {
11020Sstevel@tonic-gate 	int rv = NDI_SUCCESS;
11030Sstevel@tonic-gate 
11040Sstevel@tonic-gate 	if (dip == NULL)
11050Sstevel@tonic-gate 		return (NDI_FAILURE);
11060Sstevel@tonic-gate 
11070Sstevel@tonic-gate 	mutex_enter(&(DEVI(dip)->devi_lock));
11080Sstevel@tonic-gate 
11090Sstevel@tonic-gate 	switch (state) {
11100Sstevel@tonic-gate 	case BUS_QUIESCED:
11110Sstevel@tonic-gate 		DEVI_SET_BUS_QUIESCE(dip);
11120Sstevel@tonic-gate 		break;
11130Sstevel@tonic-gate 
11140Sstevel@tonic-gate 	case BUS_ACTIVE:
11150Sstevel@tonic-gate 		DEVI_SET_BUS_ACTIVE(dip);
11160Sstevel@tonic-gate 		DEVI_SET_BUS_UP(dip);
11170Sstevel@tonic-gate 		break;
11180Sstevel@tonic-gate 
11190Sstevel@tonic-gate 	case BUS_SHUTDOWN:
11200Sstevel@tonic-gate 		DEVI_SET_BUS_DOWN(dip);
11210Sstevel@tonic-gate 		break;
11220Sstevel@tonic-gate 
11230Sstevel@tonic-gate 	default:
11240Sstevel@tonic-gate 		rv = NDI_FAILURE;
11250Sstevel@tonic-gate 	}
11260Sstevel@tonic-gate 
11270Sstevel@tonic-gate 	mutex_exit(&(DEVI(dip)->devi_lock));
11280Sstevel@tonic-gate 	return (rv);
11290Sstevel@tonic-gate }
11300Sstevel@tonic-gate 
11310Sstevel@tonic-gate /*
11320Sstevel@tonic-gate  * These dummy functions are obsolete and may be removed.
11330Sstevel@tonic-gate  * Retained for existing driver compatibility only.
11340Sstevel@tonic-gate  * Drivers should be fixed not to use these functions.
11350Sstevel@tonic-gate  * Don't write new code using these obsolete interfaces.
11360Sstevel@tonic-gate  */
11370Sstevel@tonic-gate /*ARGSUSED*/
11380Sstevel@tonic-gate void
i_ndi_block_device_tree_changes(uint_t * lkcnt)11390Sstevel@tonic-gate i_ndi_block_device_tree_changes(uint_t *lkcnt)	/* obsolete */
11400Sstevel@tonic-gate {
11410Sstevel@tonic-gate 	/* obsolete dummy function */
11420Sstevel@tonic-gate }
11430Sstevel@tonic-gate 
11440Sstevel@tonic-gate /*ARGSUSED*/
11450Sstevel@tonic-gate void
i_ndi_allow_device_tree_changes(uint_t lkcnt)11460Sstevel@tonic-gate i_ndi_allow_device_tree_changes(uint_t lkcnt)	/* obsolete */
11470Sstevel@tonic-gate {
11480Sstevel@tonic-gate 	/* obsolete dummy function */
11490Sstevel@tonic-gate }
11500Sstevel@tonic-gate 
11510Sstevel@tonic-gate /*
11520Sstevel@tonic-gate  * Single thread entry into per-driver list
11530Sstevel@tonic-gate  */
11540Sstevel@tonic-gate /*ARGSUSED*/
11550Sstevel@tonic-gate void
e_ddi_enter_driver_list(struct devnames * dnp,int * listcnt)11560Sstevel@tonic-gate e_ddi_enter_driver_list(struct devnames *dnp, int *listcnt)	/* obsolete */
11570Sstevel@tonic-gate {
11580Sstevel@tonic-gate 	/* obsolete dummy function */
11590Sstevel@tonic-gate }
11600Sstevel@tonic-gate 
11610Sstevel@tonic-gate /*
11620Sstevel@tonic-gate  * release the per-driver list
11630Sstevel@tonic-gate  */
11640Sstevel@tonic-gate /*ARGSUSED*/
11650Sstevel@tonic-gate void
e_ddi_exit_driver_list(struct devnames * dnp,int listcnt)11660Sstevel@tonic-gate e_ddi_exit_driver_list(struct devnames *dnp, int listcnt)	/* obsolete */
11670Sstevel@tonic-gate {
11680Sstevel@tonic-gate 	/* obsolete dummy function */
11690Sstevel@tonic-gate }
11700Sstevel@tonic-gate 
11710Sstevel@tonic-gate /*
11720Sstevel@tonic-gate  * Attempt to enter driver list
11730Sstevel@tonic-gate  */
11740Sstevel@tonic-gate /*ARGSUSED*/
11750Sstevel@tonic-gate int
e_ddi_tryenter_driver_list(struct devnames * dnp,int * listcnt)11760Sstevel@tonic-gate e_ddi_tryenter_driver_list(struct devnames *dnp, int *listcnt)	/* obsolete */
11770Sstevel@tonic-gate {
11780Sstevel@tonic-gate 	return (1);	/* obsolete dummy function */
11790Sstevel@tonic-gate }
11800Sstevel@tonic-gate 
11810Sstevel@tonic-gate /*
11820Sstevel@tonic-gate  * ndi event handling support functions:
11830Sstevel@tonic-gate  * The NDI event support model is as follows:
11840Sstevel@tonic-gate  *
11850Sstevel@tonic-gate  * The nexus driver defines a set of events using some static structures (so
11860Sstevel@tonic-gate  * these structures can be shared by all instances of the nexus driver).
11870Sstevel@tonic-gate  * The nexus driver allocates an event handle and binds the event set
11880Sstevel@tonic-gate  * to this handle. The nexus driver's event busop functions can just
11890Sstevel@tonic-gate  * call the appropriate NDI event support function using this handle
11900Sstevel@tonic-gate  * as the first argument.
11910Sstevel@tonic-gate  *
11920Sstevel@tonic-gate  * The reasoning for tying events to the device tree is that the entity
11930Sstevel@tonic-gate  * generating the callback will typically be one of the device driver's
11940Sstevel@tonic-gate  * ancestors in the tree.
11950Sstevel@tonic-gate  */
11960Sstevel@tonic-gate static int ndi_event_debug = 0;
11970Sstevel@tonic-gate 
11980Sstevel@tonic-gate #ifdef DEBUG
11990Sstevel@tonic-gate #define	NDI_EVENT_DEBUG	ndi_event_debug
12000Sstevel@tonic-gate #endif /* DEBUG */
12010Sstevel@tonic-gate 
12020Sstevel@tonic-gate /*
12030Sstevel@tonic-gate  * allocate a new ndi event handle
12040Sstevel@tonic-gate  */
12050Sstevel@tonic-gate int
ndi_event_alloc_hdl(dev_info_t * dip,ddi_iblock_cookie_t cookie,ndi_event_hdl_t * handle,uint_t flag)12060Sstevel@tonic-gate ndi_event_alloc_hdl(dev_info_t *dip, ddi_iblock_cookie_t cookie,
12070Sstevel@tonic-gate 	ndi_event_hdl_t *handle, uint_t flag)
12080Sstevel@tonic-gate {
12090Sstevel@tonic-gate 	struct ndi_event_hdl *ndi_event_hdl;
12100Sstevel@tonic-gate 
12110Sstevel@tonic-gate 	ndi_event_hdl = kmem_zalloc(sizeof (struct ndi_event_hdl),
12127224Scth 	    ((flag & NDI_NOSLEEP) ? KM_NOSLEEP : KM_SLEEP));
12130Sstevel@tonic-gate 
12140Sstevel@tonic-gate 	if (!ndi_event_hdl) {
12150Sstevel@tonic-gate 		return (NDI_FAILURE);
12160Sstevel@tonic-gate 	}
12170Sstevel@tonic-gate 
12180Sstevel@tonic-gate 	ndi_event_hdl->ndi_evthdl_dip = dip;
12190Sstevel@tonic-gate 	ndi_event_hdl->ndi_evthdl_iblock_cookie = cookie;
12200Sstevel@tonic-gate 	mutex_init(&ndi_event_hdl->ndi_evthdl_mutex, NULL,
12210Sstevel@tonic-gate 	    MUTEX_DRIVER, (void *)cookie);
12220Sstevel@tonic-gate 
12230Sstevel@tonic-gate 	mutex_init(&ndi_event_hdl->ndi_evthdl_cb_mutex, NULL,
12240Sstevel@tonic-gate 	    MUTEX_DRIVER, (void *)cookie);
12250Sstevel@tonic-gate 
12260Sstevel@tonic-gate 	*handle = (ndi_event_hdl_t)ndi_event_hdl;
12270Sstevel@tonic-gate 
12280Sstevel@tonic-gate 	return (NDI_SUCCESS);
12290Sstevel@tonic-gate }
12300Sstevel@tonic-gate 
12310Sstevel@tonic-gate /*
12320Sstevel@tonic-gate  * free the ndi event handle
12330Sstevel@tonic-gate  */
12340Sstevel@tonic-gate int
ndi_event_free_hdl(ndi_event_hdl_t handle)12350Sstevel@tonic-gate ndi_event_free_hdl(ndi_event_hdl_t handle)
12360Sstevel@tonic-gate {
12370Sstevel@tonic-gate 	struct ndi_event_hdl *ndi_event_hdl = (struct ndi_event_hdl *)handle;
12380Sstevel@tonic-gate 	ndi_event_cookie_t *cookie;
12390Sstevel@tonic-gate 	ndi_event_cookie_t *free;
12400Sstevel@tonic-gate 
12410Sstevel@tonic-gate 	ASSERT(handle);
12420Sstevel@tonic-gate 
12430Sstevel@tonic-gate 	mutex_enter(&ndi_event_hdl->ndi_evthdl_mutex);
12440Sstevel@tonic-gate 	mutex_enter(&ndi_event_hdl->ndi_evthdl_cb_mutex);
12450Sstevel@tonic-gate 
12460Sstevel@tonic-gate 	cookie = ndi_event_hdl->ndi_evthdl_cookie_list;
12470Sstevel@tonic-gate 
12480Sstevel@tonic-gate 	/* deallocate all defined cookies */
12490Sstevel@tonic-gate 	while (cookie != NULL) {
12500Sstevel@tonic-gate 		ASSERT(cookie->callback_list == NULL);
12510Sstevel@tonic-gate 		free = cookie;
12520Sstevel@tonic-gate 		cookie = cookie->next_cookie;
12530Sstevel@tonic-gate 
12540Sstevel@tonic-gate 		kmem_free(free, sizeof (ndi_event_cookie_t));
12550Sstevel@tonic-gate 	}
12560Sstevel@tonic-gate 
12570Sstevel@tonic-gate 
12580Sstevel@tonic-gate 	mutex_exit(&ndi_event_hdl->ndi_evthdl_cb_mutex);
12590Sstevel@tonic-gate 	mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
12600Sstevel@tonic-gate 
12610Sstevel@tonic-gate 	/* destroy mutexes */
12620Sstevel@tonic-gate 	mutex_destroy(&ndi_event_hdl->ndi_evthdl_mutex);
12630Sstevel@tonic-gate 	mutex_destroy(&ndi_event_hdl->ndi_evthdl_cb_mutex);
12640Sstevel@tonic-gate 
12650Sstevel@tonic-gate 	/* free event handle */
12660Sstevel@tonic-gate 	kmem_free(ndi_event_hdl, sizeof (struct ndi_event_hdl));
12670Sstevel@tonic-gate 
12680Sstevel@tonic-gate 	return (NDI_SUCCESS);
12690Sstevel@tonic-gate }
12700Sstevel@tonic-gate 
12710Sstevel@tonic-gate 
12720Sstevel@tonic-gate /*
12730Sstevel@tonic-gate  * ndi_event_bind_set() adds a set of events to the NDI event
12740Sstevel@tonic-gate  * handle.
12750Sstevel@tonic-gate  *
12760Sstevel@tonic-gate  * Events generated by high level interrupts should not
12770Sstevel@tonic-gate  * be mixed in the same event set with events generated by
12780Sstevel@tonic-gate  * normal interrupts or kernel events.
12790Sstevel@tonic-gate  *
12800Sstevel@tonic-gate  * This function can be called multiple times to bind
12810Sstevel@tonic-gate  * additional sets to the event handle.
12820Sstevel@tonic-gate  * However, events generated by high level interrupts cannot
12830Sstevel@tonic-gate  * be bound to a handle that already has bound events generated
12840Sstevel@tonic-gate  * by normal interrupts or from kernel context and vice versa.
12850Sstevel@tonic-gate  */
12860Sstevel@tonic-gate int
ndi_event_bind_set(ndi_event_hdl_t handle,ndi_event_set_t * ndi_events,uint_t flag)12870Sstevel@tonic-gate ndi_event_bind_set(ndi_event_hdl_t handle,
12880Sstevel@tonic-gate 	ndi_event_set_t		*ndi_events,
12890Sstevel@tonic-gate 	uint_t			flag)
12900Sstevel@tonic-gate {
12910Sstevel@tonic-gate 	struct ndi_event_hdl	*ndi_event_hdl;
12920Sstevel@tonic-gate 	ndi_event_cookie_t	*next, *prev, *new_cookie;
12930Sstevel@tonic-gate 	uint_t			i, len;
12940Sstevel@tonic-gate 	uint_t			dup = 0;
12950Sstevel@tonic-gate 	uint_t			high_plevels, other_plevels;
12960Sstevel@tonic-gate 	ndi_event_definition_t *ndi_event_defs;
12970Sstevel@tonic-gate 
12980Sstevel@tonic-gate 	int km_flag = ((flag & NDI_NOSLEEP) ? KM_NOSLEEP : KM_SLEEP);
12990Sstevel@tonic-gate 
13000Sstevel@tonic-gate 	ASSERT(handle);
13010Sstevel@tonic-gate 	ASSERT(ndi_events);
13020Sstevel@tonic-gate 
13030Sstevel@tonic-gate 	/*
13040Sstevel@tonic-gate 	 * binding must be performed during attach/detach
13050Sstevel@tonic-gate 	 */
13060Sstevel@tonic-gate 	if (!DEVI_IS_ATTACHING(handle->ndi_evthdl_dip) &&
13070Sstevel@tonic-gate 	    !DEVI_IS_DETACHING(handle->ndi_evthdl_dip)) {
13080Sstevel@tonic-gate 		cmn_err(CE_WARN, "ndi_event_bind_set must be called within "
13090Sstevel@tonic-gate 		    "attach or detach");
13100Sstevel@tonic-gate 		return (NDI_FAILURE);
13110Sstevel@tonic-gate 	}
13120Sstevel@tonic-gate 
13130Sstevel@tonic-gate 	/*
13140Sstevel@tonic-gate 	 * if it is not the correct version or the event set is
13150Sstevel@tonic-gate 	 * empty, bail out
13160Sstevel@tonic-gate 	 */
13170Sstevel@tonic-gate 	if (ndi_events->ndi_events_version != NDI_EVENTS_REV1)
13180Sstevel@tonic-gate 		return (NDI_FAILURE);
13190Sstevel@tonic-gate 
13200Sstevel@tonic-gate 	ndi_event_hdl	= (struct ndi_event_hdl *)handle;
13210Sstevel@tonic-gate 	ndi_event_defs = ndi_events->ndi_event_defs;
13220Sstevel@tonic-gate 	high_plevels	= other_plevels = 0;
13230Sstevel@tonic-gate 
13240Sstevel@tonic-gate 	mutex_enter(&ndi_event_hdl->ndi_evthdl_mutex);
13250Sstevel@tonic-gate 
13260Sstevel@tonic-gate 	/* check for mixing events at high level with the other types */
13270Sstevel@tonic-gate 	for (i = 0; i < ndi_events->ndi_n_events; i++) {
13280Sstevel@tonic-gate 		if (ndi_event_defs[i].ndi_event_plevel == EPL_HIGHLEVEL) {
13290Sstevel@tonic-gate 			high_plevels++;
13300Sstevel@tonic-gate 		} else {
13310Sstevel@tonic-gate 			other_plevels++;
13320Sstevel@tonic-gate 		}
13330Sstevel@tonic-gate 	}
13340Sstevel@tonic-gate 
13350Sstevel@tonic-gate 	/*
13360Sstevel@tonic-gate 	 * bail out if high level events are mixed with other types in this
13370Sstevel@tonic-gate 	 * event set or the set is incompatible with the set in the handle
13380Sstevel@tonic-gate 	 */
13390Sstevel@tonic-gate 	if ((high_plevels && other_plevels) ||
13400Sstevel@tonic-gate 	    (other_plevels && ndi_event_hdl->ndi_evthdl_high_plevels) ||
13410Sstevel@tonic-gate 	    (high_plevels && ndi_event_hdl->ndi_evthdl_other_plevels)) {
13420Sstevel@tonic-gate 		mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
13430Sstevel@tonic-gate 
13440Sstevel@tonic-gate 		return (NDI_FAILURE);
13450Sstevel@tonic-gate 	}
13460Sstevel@tonic-gate 
13470Sstevel@tonic-gate 	/*
13480Sstevel@tonic-gate 	 * check for duplicate events in both the existing handle
13490Sstevel@tonic-gate 	 * and the event set, add events if not duplicates
13500Sstevel@tonic-gate 	 */
13510Sstevel@tonic-gate 	next = ndi_event_hdl->ndi_evthdl_cookie_list;
13520Sstevel@tonic-gate 	for (i = 0; i < ndi_events->ndi_n_events; i++) {
13530Sstevel@tonic-gate 		while (next != NULL) {
13540Sstevel@tonic-gate 			len = strlen(NDI_EVENT_NAME(next)) + 1;
13550Sstevel@tonic-gate 			if (strncmp(NDI_EVENT_NAME(next),
13560Sstevel@tonic-gate 			    ndi_event_defs[i].ndi_event_name, len) == 0) {
13570Sstevel@tonic-gate 				dup = 1;
13580Sstevel@tonic-gate 				break;
13590Sstevel@tonic-gate 			}
13600Sstevel@tonic-gate 
13610Sstevel@tonic-gate 			prev = next;
13620Sstevel@tonic-gate 			next = next->next_cookie;
13630Sstevel@tonic-gate 		}
13640Sstevel@tonic-gate 
13650Sstevel@tonic-gate 		if (dup == 0) {
13660Sstevel@tonic-gate 			new_cookie = kmem_zalloc(sizeof (ndi_event_cookie_t),
13670Sstevel@tonic-gate 			    km_flag);
13680Sstevel@tonic-gate 
13690Sstevel@tonic-gate 			if (!new_cookie)
13700Sstevel@tonic-gate 				return (NDI_FAILURE);
13710Sstevel@tonic-gate 
13720Sstevel@tonic-gate 			if (ndi_event_hdl->ndi_evthdl_n_events == 0) {
13730Sstevel@tonic-gate 				ndi_event_hdl->ndi_evthdl_cookie_list =
13740Sstevel@tonic-gate 				    new_cookie;
13750Sstevel@tonic-gate 			} else {
13760Sstevel@tonic-gate 				prev->next_cookie = new_cookie;
13770Sstevel@tonic-gate 			}
13780Sstevel@tonic-gate 
13790Sstevel@tonic-gate 			ndi_event_hdl->ndi_evthdl_n_events++;
13800Sstevel@tonic-gate 
13810Sstevel@tonic-gate 			/*
13820Sstevel@tonic-gate 			 * set up new cookie
13830Sstevel@tonic-gate 			 */
13840Sstevel@tonic-gate 			new_cookie->definition = &ndi_event_defs[i];
13850Sstevel@tonic-gate 			new_cookie->ddip = ndi_event_hdl->ndi_evthdl_dip;
13860Sstevel@tonic-gate 
13870Sstevel@tonic-gate 		} else {
13880Sstevel@tonic-gate 			/*
13890Sstevel@tonic-gate 			 * event not added, must correct plevel numbers
13900Sstevel@tonic-gate 			 */
13910Sstevel@tonic-gate 			if (ndi_event_defs[i].ndi_event_plevel ==
13920Sstevel@tonic-gate 			    EPL_HIGHLEVEL) {
13930Sstevel@tonic-gate 				high_plevels--;
13940Sstevel@tonic-gate 			} else {
13950Sstevel@tonic-gate 				other_plevels--;
13960Sstevel@tonic-gate 			}
13970Sstevel@tonic-gate 		}
13980Sstevel@tonic-gate 
13990Sstevel@tonic-gate 		dup = 0;
14000Sstevel@tonic-gate 		next = ndi_event_hdl->ndi_evthdl_cookie_list;
14010Sstevel@tonic-gate 		prev = NULL;
14020Sstevel@tonic-gate 
14030Sstevel@tonic-gate 	}
14040Sstevel@tonic-gate 
14050Sstevel@tonic-gate 	ndi_event_hdl->ndi_evthdl_high_plevels	+= high_plevels;
14060Sstevel@tonic-gate 	ndi_event_hdl->ndi_evthdl_other_plevels += other_plevels;
14070Sstevel@tonic-gate 
14080Sstevel@tonic-gate 	ASSERT((ndi_event_hdl->ndi_evthdl_high_plevels == 0) ||
14090Sstevel@tonic-gate 	    (ndi_event_hdl->ndi_evthdl_other_plevels == 0));
14100Sstevel@tonic-gate 
14110Sstevel@tonic-gate #ifdef NDI_EVENT_DEBUG
14120Sstevel@tonic-gate 	if (ndi_event_debug) {
14130Sstevel@tonic-gate 		ndi_event_dump_hdl(ndi_event_hdl, "ndi_event_bind_set");
14140Sstevel@tonic-gate 	}
14150Sstevel@tonic-gate #endif /* NDI_EVENT_DEBUG */
14160Sstevel@tonic-gate 
14170Sstevel@tonic-gate 	mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
14180Sstevel@tonic-gate 
14190Sstevel@tonic-gate 	return (NDI_SUCCESS);
14200Sstevel@tonic-gate }
14210Sstevel@tonic-gate 
14220Sstevel@tonic-gate /*
14230Sstevel@tonic-gate  * ndi_event_unbind_set() unbinds a set of events, previously
14240Sstevel@tonic-gate  * bound using ndi_event_bind_set(), from the NDI event
14250Sstevel@tonic-gate  * handle.
14260Sstevel@tonic-gate  *
14270Sstevel@tonic-gate  * This routine will unbind all events in the event set.  If an event,
14280Sstevel@tonic-gate  * specified in the event set, is not found in the handle, this
14290Sstevel@tonic-gate  * routine will proceed onto the next member of the set as if the event
14300Sstevel@tonic-gate  * was never specified.
14310Sstevel@tonic-gate  *
14320Sstevel@tonic-gate  * The event set may be a subset of the set of events that
14330Sstevel@tonic-gate  * was previously bound to the handle. For example, events
14340Sstevel@tonic-gate  * can be individually unbound.
14350Sstevel@tonic-gate  *
14360Sstevel@tonic-gate  * An event cannot be unbound if callbacks are still
14370Sstevel@tonic-gate  * registered against the event.
14380Sstevel@tonic-gate  */
14390Sstevel@tonic-gate /*ARGSUSED*/
14400Sstevel@tonic-gate int
ndi_event_unbind_set(ndi_event_hdl_t handle,ndi_event_set_t * ndi_events,uint_t flag)14410Sstevel@tonic-gate ndi_event_unbind_set(ndi_event_hdl_t   handle, ndi_event_set_t	*ndi_events,
14420Sstevel@tonic-gate     uint_t flag)
14430Sstevel@tonic-gate {
14440Sstevel@tonic-gate 	ndi_event_definition_t	*ndi_event_defs;
14450Sstevel@tonic-gate 	int			len;
14460Sstevel@tonic-gate 	uint_t			i;
14470Sstevel@tonic-gate 	int			rval;
14480Sstevel@tonic-gate 	ndi_event_cookie_t *cookie_list;
14490Sstevel@tonic-gate 	ndi_event_cookie_t *prev = NULL;
14500Sstevel@tonic-gate 
14510Sstevel@tonic-gate 	ASSERT(ndi_events);
14520Sstevel@tonic-gate 	ASSERT(handle);
14530Sstevel@tonic-gate 
14540Sstevel@tonic-gate 	/*
14550Sstevel@tonic-gate 	 * binding must be performed during attach/detac
14560Sstevel@tonic-gate 	 */
14570Sstevel@tonic-gate 	if (!DEVI_IS_ATTACHING(handle->ndi_evthdl_dip) &&
14580Sstevel@tonic-gate 	    !DEVI_IS_DETACHING(handle->ndi_evthdl_dip)) {
14590Sstevel@tonic-gate 		cmn_err(CE_WARN, "ndi_event_bind_set must be called within "
14600Sstevel@tonic-gate 		    "attach or detach");
14610Sstevel@tonic-gate 		return (NDI_FAILURE);
14620Sstevel@tonic-gate 	}
14630Sstevel@tonic-gate 
14640Sstevel@tonic-gate 	/* bail out if ndi_event_set is outdated */
14650Sstevel@tonic-gate 	if (ndi_events->ndi_events_version != NDI_EVENTS_REV1) {
14660Sstevel@tonic-gate 		return (NDI_FAILURE);
14670Sstevel@tonic-gate 	}
14680Sstevel@tonic-gate 
14690Sstevel@tonic-gate 	ASSERT(ndi_events->ndi_event_defs);
14700Sstevel@tonic-gate 
14710Sstevel@tonic-gate 	ndi_event_defs = ndi_events->ndi_event_defs;
14720Sstevel@tonic-gate 
14730Sstevel@tonic-gate 	mutex_enter(&handle->ndi_evthdl_mutex);
14740Sstevel@tonic-gate 	mutex_enter(&handle->ndi_evthdl_cb_mutex);
14750Sstevel@tonic-gate 
14760Sstevel@tonic-gate 	/*
14770Sstevel@tonic-gate 	 * Verify that all events in the event set are eligible
14780Sstevel@tonic-gate 	 * for unbinding(ie. there are no outstanding callbacks).
14790Sstevel@tonic-gate 	 * If any one of the events are ineligible, fail entire
14800Sstevel@tonic-gate 	 * operation.
14810Sstevel@tonic-gate 	 */
14820Sstevel@tonic-gate 
14830Sstevel@tonic-gate 	for (i = 0; i < ndi_events->ndi_n_events; i++) {
14840Sstevel@tonic-gate 		cookie_list = handle->ndi_evthdl_cookie_list;
14850Sstevel@tonic-gate 		while (cookie_list != NULL) {
14860Sstevel@tonic-gate 			len = strlen(NDI_EVENT_NAME(cookie_list)) + 1;
14870Sstevel@tonic-gate 			if (strncmp(NDI_EVENT_NAME(cookie_list),
14880Sstevel@tonic-gate 			    ndi_event_defs[i].ndi_event_name, len) == 0) {
14890Sstevel@tonic-gate 
14907224Scth 				ASSERT(cookie_list->callback_list == NULL);
14917224Scth 				if (cookie_list->callback_list) {
14927224Scth 					rval = NDI_FAILURE;
14937224Scth 					goto done;
14947224Scth 				}
14957224Scth 				break;
14960Sstevel@tonic-gate 			} else {
14970Sstevel@tonic-gate 				cookie_list = cookie_list->next_cookie;
14980Sstevel@tonic-gate 			}
14990Sstevel@tonic-gate 		}
15000Sstevel@tonic-gate 	}
15010Sstevel@tonic-gate 
15020Sstevel@tonic-gate 	/*
15030Sstevel@tonic-gate 	 * remove all events found within the handle
15040Sstevel@tonic-gate 	 * If an event is not found, this function will proceed as if the event
15050Sstevel@tonic-gate 	 * was never specified.
15060Sstevel@tonic-gate 	 */
15070Sstevel@tonic-gate 
15080Sstevel@tonic-gate 	for (i = 0; i < ndi_events->ndi_n_events; i++) {
15090Sstevel@tonic-gate 		cookie_list = handle->ndi_evthdl_cookie_list;
15100Sstevel@tonic-gate 		prev = NULL;
15110Sstevel@tonic-gate 		while (cookie_list != NULL) {
15120Sstevel@tonic-gate 			len = strlen(NDI_EVENT_NAME(cookie_list)) + 1;
15130Sstevel@tonic-gate 			if (strncmp(NDI_EVENT_NAME(cookie_list),
15140Sstevel@tonic-gate 			    ndi_event_defs[i].ndi_event_name, len) == 0) {
15150Sstevel@tonic-gate 
15160Sstevel@tonic-gate 				/*
15170Sstevel@tonic-gate 				 * can not unbind an event definition with
15180Sstevel@tonic-gate 				 * outstanding callbacks
15190Sstevel@tonic-gate 				 */
15200Sstevel@tonic-gate 				if (cookie_list->callback_list) {
15210Sstevel@tonic-gate 					rval = NDI_FAILURE;
15220Sstevel@tonic-gate 					goto done;
15230Sstevel@tonic-gate 				}
15240Sstevel@tonic-gate 
15250Sstevel@tonic-gate 				/* remove this cookie from the list */
15260Sstevel@tonic-gate 				if (prev != NULL) {
15270Sstevel@tonic-gate 					prev->next_cookie =
15280Sstevel@tonic-gate 					    cookie_list->next_cookie;
15290Sstevel@tonic-gate 				} else {
15300Sstevel@tonic-gate 					handle->ndi_evthdl_cookie_list =
15310Sstevel@tonic-gate 					    cookie_list->next_cookie;
15320Sstevel@tonic-gate 				}
15330Sstevel@tonic-gate 
15340Sstevel@tonic-gate 				/* adjust plevel counts */
15350Sstevel@tonic-gate 				if (NDI_EVENT_PLEVEL(cookie_list) ==
15360Sstevel@tonic-gate 				    EPL_HIGHLEVEL) {
15370Sstevel@tonic-gate 					handle->ndi_evthdl_high_plevels--;
15380Sstevel@tonic-gate 				} else {
15390Sstevel@tonic-gate 					handle->ndi_evthdl_other_plevels--;
15400Sstevel@tonic-gate 				}
15410Sstevel@tonic-gate 
15420Sstevel@tonic-gate 				/* adjust cookie count */
15430Sstevel@tonic-gate 				handle->ndi_evthdl_n_events--;
15440Sstevel@tonic-gate 
15450Sstevel@tonic-gate 				/* free the cookie */
15460Sstevel@tonic-gate 				kmem_free(cookie_list,
15470Sstevel@tonic-gate 				    sizeof (ndi_event_cookie_t));
15480Sstevel@tonic-gate 
15490Sstevel@tonic-gate 				cookie_list = handle->ndi_evthdl_cookie_list;
15500Sstevel@tonic-gate 				break;
15510Sstevel@tonic-gate 
15520Sstevel@tonic-gate 			} else {
15530Sstevel@tonic-gate 				prev = cookie_list;
15540Sstevel@tonic-gate 				cookie_list = cookie_list->next_cookie;
15550Sstevel@tonic-gate 			}
15560Sstevel@tonic-gate 
15570Sstevel@tonic-gate 		}
15580Sstevel@tonic-gate 
15590Sstevel@tonic-gate 	}
15600Sstevel@tonic-gate 
15610Sstevel@tonic-gate #ifdef NDI_EVENT_DEBUG
15620Sstevel@tonic-gate 	if (ndi_event_debug) {
15630Sstevel@tonic-gate 		ndi_event_dump_hdl(handle, "ndi_event_unbind_set");
15640Sstevel@tonic-gate 	}
15650Sstevel@tonic-gate #endif /* NDI_EVENT_DEBUG */
15660Sstevel@tonic-gate 
15670Sstevel@tonic-gate 	rval = NDI_SUCCESS;
15680Sstevel@tonic-gate 
15690Sstevel@tonic-gate done:
15700Sstevel@tonic-gate 	mutex_exit(&handle->ndi_evthdl_cb_mutex);
15710Sstevel@tonic-gate 	mutex_exit(&handle->ndi_evthdl_mutex);
15720Sstevel@tonic-gate 
15730Sstevel@tonic-gate 	return (rval);
15740Sstevel@tonic-gate }
15750Sstevel@tonic-gate 
15760Sstevel@tonic-gate /*
15770Sstevel@tonic-gate  * ndi_event_retrieve_cookie():
15780Sstevel@tonic-gate  * Return an event cookie for eventname if this nexus driver
15790Sstevel@tonic-gate  * has defined the named event. The event cookie returned
15800Sstevel@tonic-gate  * by this function is used to register callback handlers
15810Sstevel@tonic-gate  * for the event.
15820Sstevel@tonic-gate  *
15830Sstevel@tonic-gate  * ndi_event_retrieve_cookie() is intended to be used in the
15840Sstevel@tonic-gate  * nexus driver's bus_get_eventcookie busop routine.
15850Sstevel@tonic-gate  *
15860Sstevel@tonic-gate  * If the event is not defined by this bus nexus driver, and flag
15870Sstevel@tonic-gate  * does not include NDI_EVENT_NOPASS, then ndi_event_retrieve_cookie()
15880Sstevel@tonic-gate  * will pass the request up the device tree hierarchy by calling
15890Sstevel@tonic-gate  * ndi_busop_get_eventcookie(9N).
15900Sstevel@tonic-gate  * If the event is not defined by this bus nexus driver, and flag
15910Sstevel@tonic-gate  * does include NDI_EVENT_NOPASS, ndi_event_retrieve_cookie()
15920Sstevel@tonic-gate  * will return NDI_FAILURE.  The caller may then determine what further
15930Sstevel@tonic-gate  * action to take, such as using a different handle, passing the
15940Sstevel@tonic-gate  * request up the device tree using ndi_busop_get_eventcookie(9N),
15950Sstevel@tonic-gate  * or returning the failure to the caller, thus blocking the
15960Sstevel@tonic-gate  * progress of the request up the tree.
15970Sstevel@tonic-gate  */
15980Sstevel@tonic-gate int
ndi_event_retrieve_cookie(ndi_event_hdl_t handle,dev_info_t * rdip,char * eventname,ddi_eventcookie_t * cookiep,uint_t flag)15990Sstevel@tonic-gate ndi_event_retrieve_cookie(ndi_event_hdl_t handle,
16000Sstevel@tonic-gate 	dev_info_t		*rdip,
16010Sstevel@tonic-gate 	char			*eventname,
16020Sstevel@tonic-gate 	ddi_eventcookie_t	*cookiep,
16030Sstevel@tonic-gate 	uint_t			flag)
16040Sstevel@tonic-gate {
16050Sstevel@tonic-gate 	struct ndi_event_hdl *ndi_event_hdl = (struct ndi_event_hdl *)handle;
16060Sstevel@tonic-gate 	int		len;
16070Sstevel@tonic-gate 	ndi_event_cookie_t *cookie_list;
16080Sstevel@tonic-gate 
16090Sstevel@tonic-gate 	mutex_enter(&ndi_event_hdl->ndi_evthdl_mutex);
16100Sstevel@tonic-gate 
16110Sstevel@tonic-gate 	cookie_list = ndi_event_hdl->ndi_evthdl_cookie_list;
16120Sstevel@tonic-gate 	/*
16130Sstevel@tonic-gate 	 * search the cookie list for the event name and return
16140Sstevel@tonic-gate 	 * cookie if found.
16150Sstevel@tonic-gate 	 */
16160Sstevel@tonic-gate 	while (cookie_list != NULL) {
16170Sstevel@tonic-gate 
16180Sstevel@tonic-gate 		len = strlen(NDI_EVENT_NAME(cookie_list)) + 1;
16190Sstevel@tonic-gate 		if (strncmp(NDI_EVENT_NAME(cookie_list), eventname,
16200Sstevel@tonic-gate 		    len) == 0) {
16210Sstevel@tonic-gate 			*cookiep = (ddi_eventcookie_t)cookie_list;
16220Sstevel@tonic-gate 
16230Sstevel@tonic-gate 			mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
16240Sstevel@tonic-gate 			return (NDI_SUCCESS);
16250Sstevel@tonic-gate 		}
16260Sstevel@tonic-gate 
16270Sstevel@tonic-gate 		cookie_list = cookie_list->next_cookie;
16280Sstevel@tonic-gate 	}
16290Sstevel@tonic-gate 
16300Sstevel@tonic-gate 	mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
16310Sstevel@tonic-gate 	/*
16320Sstevel@tonic-gate 	 * event was not found, pass up or return failure
16330Sstevel@tonic-gate 	 */
16340Sstevel@tonic-gate 	if ((flag & NDI_EVENT_NOPASS) == 0) {
16350Sstevel@tonic-gate 		return (ndi_busop_get_eventcookie(
16367224Scth 		    ndi_event_hdl->ndi_evthdl_dip, rdip, eventname, cookiep));
16370Sstevel@tonic-gate 	} else {
16380Sstevel@tonic-gate 		return (NDI_FAILURE);
16390Sstevel@tonic-gate 	}
16400Sstevel@tonic-gate }
16410Sstevel@tonic-gate 
16420Sstevel@tonic-gate /*
16430Sstevel@tonic-gate  * check whether this nexus defined this event and look up attributes
16440Sstevel@tonic-gate  */
16450Sstevel@tonic-gate static int
ndi_event_is_defined(ndi_event_hdl_t handle,ddi_eventcookie_t cookie,int * attributes)16460Sstevel@tonic-gate ndi_event_is_defined(ndi_event_hdl_t handle,
16470Sstevel@tonic-gate 	ddi_eventcookie_t cookie, int *attributes)
16480Sstevel@tonic-gate {
16490Sstevel@tonic-gate 
16500Sstevel@tonic-gate 	struct ndi_event_hdl *ndi_event_hdl = (struct ndi_event_hdl *)handle;
16510Sstevel@tonic-gate 	ndi_event_cookie_t *cookie_list;
16520Sstevel@tonic-gate 
16530Sstevel@tonic-gate 	ASSERT(mutex_owned(&handle->ndi_evthdl_mutex));
16540Sstevel@tonic-gate 
16550Sstevel@tonic-gate 	cookie_list = ndi_event_hdl->ndi_evthdl_cookie_list;
16560Sstevel@tonic-gate 	while (cookie_list != NULL) {
16570Sstevel@tonic-gate 		if (cookie_list == NDI_EVENT(cookie)) {
16580Sstevel@tonic-gate 			if (attributes)
16590Sstevel@tonic-gate 				*attributes =
16600Sstevel@tonic-gate 				    NDI_EVENT_ATTRIBUTES(cookie_list);
16610Sstevel@tonic-gate 
16620Sstevel@tonic-gate 			return (NDI_SUCCESS);
16630Sstevel@tonic-gate 		}
16640Sstevel@tonic-gate 
16650Sstevel@tonic-gate 		cookie_list = cookie_list->next_cookie;
16660Sstevel@tonic-gate 	}
16670Sstevel@tonic-gate 
16680Sstevel@tonic-gate 	return (NDI_FAILURE);
16690Sstevel@tonic-gate }
16700Sstevel@tonic-gate 
16710Sstevel@tonic-gate /*
16720Sstevel@tonic-gate  * ndi_event_add_callback(): adds an event callback registration
16730Sstevel@tonic-gate  * to the event cookie defining this event.
16740Sstevel@tonic-gate  *
16750Sstevel@tonic-gate  * Refer also to bus_add_eventcall(9n) and ndi_busop_add_eventcall(9n).
16760Sstevel@tonic-gate  *
16770Sstevel@tonic-gate  * ndi_event_add_callback(9n) is intended to be used in
16780Sstevel@tonic-gate  * the nexus driver's bus_add_eventcall(9n) busop function.
16790Sstevel@tonic-gate  *
16800Sstevel@tonic-gate  * If the event is not defined by this bus nexus driver,
16810Sstevel@tonic-gate  * ndi_event_add_callback() will return NDI_FAILURE.
16820Sstevel@tonic-gate  */
16830Sstevel@tonic-gate int
ndi_event_add_callback(ndi_event_hdl_t handle,dev_info_t * child_dip,ddi_eventcookie_t cookie,void (* event_callback)(dev_info_t *,ddi_eventcookie_t,void * arg,void * impldata),void * arg,uint_t flag,ddi_callback_id_t * cb_id)16840Sstevel@tonic-gate ndi_event_add_callback(ndi_event_hdl_t handle, dev_info_t *child_dip,
16850Sstevel@tonic-gate 	ddi_eventcookie_t cookie,
16860Sstevel@tonic-gate 	void		(*event_callback)(dev_info_t *,
16870Sstevel@tonic-gate 			ddi_eventcookie_t, void *arg, void *impldata),
16880Sstevel@tonic-gate 	void		*arg,
16890Sstevel@tonic-gate 	uint_t		flag,
16900Sstevel@tonic-gate 	ddi_callback_id_t *cb_id)
16910Sstevel@tonic-gate {
16920Sstevel@tonic-gate 	struct ndi_event_hdl *ndi_event_hdl = (struct ndi_event_hdl *)handle;
16930Sstevel@tonic-gate 	int km_flag = ((flag & NDI_NOSLEEP) ? KM_NOSLEEP : KM_SLEEP);
16940Sstevel@tonic-gate 	ndi_event_callbacks_t *cb;
16950Sstevel@tonic-gate 
16960Sstevel@tonic-gate 	mutex_enter(&ndi_event_hdl->ndi_evthdl_mutex);
16970Sstevel@tonic-gate 
16980Sstevel@tonic-gate 	/*
16990Sstevel@tonic-gate 	 * if the event was not bound to this handle, return failure
17000Sstevel@tonic-gate 	 */
17010Sstevel@tonic-gate 	if (ndi_event_is_defined(handle, cookie, NULL) != NDI_SUCCESS) {
17020Sstevel@tonic-gate 
17030Sstevel@tonic-gate 		mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
17040Sstevel@tonic-gate 		return (NDI_FAILURE);
17050Sstevel@tonic-gate 
17060Sstevel@tonic-gate 	}
17070Sstevel@tonic-gate 
17080Sstevel@tonic-gate 	mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
17090Sstevel@tonic-gate 
17100Sstevel@tonic-gate 	/*
17110Sstevel@tonic-gate 	 * allocate space for a callback structure
17120Sstevel@tonic-gate 	 */
17130Sstevel@tonic-gate 	cb = kmem_zalloc(sizeof (ndi_event_callbacks_t), km_flag);
17140Sstevel@tonic-gate 	if (cb == NULL) {
17150Sstevel@tonic-gate 		return (NDI_FAILURE);
17160Sstevel@tonic-gate 	}
17170Sstevel@tonic-gate 
17180Sstevel@tonic-gate 	mutex_enter(&ndi_event_hdl->ndi_evthdl_mutex);
17190Sstevel@tonic-gate 
17200Sstevel@tonic-gate 	/* initialize callback structure */
17210Sstevel@tonic-gate 	cb->ndi_evtcb_dip	= child_dip;
17220Sstevel@tonic-gate 	cb->ndi_evtcb_callback	= event_callback;
17230Sstevel@tonic-gate 	cb->ndi_evtcb_arg	= arg;
17240Sstevel@tonic-gate 	cb->ndi_evtcb_cookie	= cookie;
17258912SChris.Horne@Sun.COM 	cb->devname		= (char *)ddi_driver_name(child_dip);
17260Sstevel@tonic-gate 
17270Sstevel@tonic-gate 	*cb_id = (ddi_callback_id_t)cb;
17280Sstevel@tonic-gate 	mutex_enter(&ndi_event_hdl->ndi_evthdl_cb_mutex);
17290Sstevel@tonic-gate 
17300Sstevel@tonic-gate 	/* add this callback structure to the list */
17310Sstevel@tonic-gate 	if (NDI_EVENT(cookie)->callback_list) {
17320Sstevel@tonic-gate 		cb->ndi_evtcb_next = NDI_EVENT(cookie)->callback_list;
17330Sstevel@tonic-gate 		NDI_EVENT(cookie)->callback_list->ndi_evtcb_prev = cb;
17340Sstevel@tonic-gate 		NDI_EVENT(cookie)->callback_list = cb;
17350Sstevel@tonic-gate 	} else {
17360Sstevel@tonic-gate 		NDI_EVENT(cookie)->callback_list = cb;
17370Sstevel@tonic-gate 	}
17380Sstevel@tonic-gate #ifdef NDI_EVENT_DEBUG
17390Sstevel@tonic-gate 	if (ndi_event_debug) {
17400Sstevel@tonic-gate 		ndi_event_dump_hdl(ndi_event_hdl, "ndi_event_add_callback");
17410Sstevel@tonic-gate 	}
17420Sstevel@tonic-gate #endif /* NDI_EVENT_DEBUG */
17430Sstevel@tonic-gate 
17440Sstevel@tonic-gate 	mutex_exit(&ndi_event_hdl->ndi_evthdl_cb_mutex);
17450Sstevel@tonic-gate 	mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
17460Sstevel@tonic-gate 
17470Sstevel@tonic-gate 	return (NDI_SUCCESS);
17480Sstevel@tonic-gate }
17490Sstevel@tonic-gate 
17500Sstevel@tonic-gate /*
17510Sstevel@tonic-gate  * ndi_event_remove_callback():
17520Sstevel@tonic-gate  *
17530Sstevel@tonic-gate  * ndi_event_remove_callback() removes a callback that was
17540Sstevel@tonic-gate  * previously registered using ndi_event_add_callback(9N).
17550Sstevel@tonic-gate  * Refer also to bus_remove_eventcall(9n) and
17560Sstevel@tonic-gate  * ndi_busop_remove_eventcall(9n).
17570Sstevel@tonic-gate  * ndi_event_remove_callback(9n) is intended to be used in
17580Sstevel@tonic-gate  * the nexus driver's bus_remove_eventcall (9n) busop function.
17590Sstevel@tonic-gate  * If the event is not defined by this bus nexus driver,
17600Sstevel@tonic-gate  * ndi_event_remove_callback() will return NDI_FAILURE.
17610Sstevel@tonic-gate  */
17620Sstevel@tonic-gate static void do_ndi_event_remove_callback(struct ndi_event_hdl *ndi_event_hdl,
17630Sstevel@tonic-gate 	ddi_callback_id_t cb_id);
17640Sstevel@tonic-gate 
17650Sstevel@tonic-gate int
ndi_event_remove_callback(ndi_event_hdl_t handle,ddi_callback_id_t cb_id)17660Sstevel@tonic-gate ndi_event_remove_callback(ndi_event_hdl_t handle, ddi_callback_id_t cb_id)
17670Sstevel@tonic-gate {
17680Sstevel@tonic-gate 	struct ndi_event_hdl *ndi_event_hdl = (struct ndi_event_hdl *)handle;
17690Sstevel@tonic-gate 
17700Sstevel@tonic-gate 	ASSERT(cb_id);
17710Sstevel@tonic-gate 
17720Sstevel@tonic-gate 	mutex_enter(&ndi_event_hdl->ndi_evthdl_mutex);
17730Sstevel@tonic-gate 	mutex_enter(&ndi_event_hdl->ndi_evthdl_cb_mutex);
17740Sstevel@tonic-gate 
17750Sstevel@tonic-gate 	do_ndi_event_remove_callback(ndi_event_hdl, cb_id);
17760Sstevel@tonic-gate 
17770Sstevel@tonic-gate 	mutex_exit(&ndi_event_hdl->ndi_evthdl_cb_mutex);
17780Sstevel@tonic-gate 	mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
17790Sstevel@tonic-gate 
17800Sstevel@tonic-gate 	return (NDI_SUCCESS);
17810Sstevel@tonic-gate }
17820Sstevel@tonic-gate 
17830Sstevel@tonic-gate /*ARGSUSED*/
17840Sstevel@tonic-gate static void
do_ndi_event_remove_callback(struct ndi_event_hdl * ndi_event_hdl,ddi_callback_id_t cb_id)17850Sstevel@tonic-gate do_ndi_event_remove_callback(struct ndi_event_hdl *ndi_event_hdl,
17860Sstevel@tonic-gate     ddi_callback_id_t cb_id)
17870Sstevel@tonic-gate {
17880Sstevel@tonic-gate 	ndi_event_callbacks_t *cb = (ndi_event_callbacks_t *)cb_id;
17890Sstevel@tonic-gate 	ASSERT(cb);
17900Sstevel@tonic-gate 
17910Sstevel@tonic-gate 	ASSERT(mutex_owned(&ndi_event_hdl->ndi_evthdl_mutex));
17920Sstevel@tonic-gate 	ASSERT(mutex_owned(&ndi_event_hdl->ndi_evthdl_cb_mutex));
17930Sstevel@tonic-gate 
17940Sstevel@tonic-gate 	/* remove from callback linked list */
17950Sstevel@tonic-gate 	if (cb->ndi_evtcb_prev) {
17960Sstevel@tonic-gate 		cb->ndi_evtcb_prev->ndi_evtcb_next = cb->ndi_evtcb_next;
17970Sstevel@tonic-gate 	}
17980Sstevel@tonic-gate 
17990Sstevel@tonic-gate 	if (cb->ndi_evtcb_next) {
18000Sstevel@tonic-gate 		cb->ndi_evtcb_next->ndi_evtcb_prev = cb->ndi_evtcb_prev;
18010Sstevel@tonic-gate 	}
18020Sstevel@tonic-gate 
18030Sstevel@tonic-gate 	if (NDI_EVENT(cb->ndi_evtcb_cookie)->callback_list == cb) {
18040Sstevel@tonic-gate 		NDI_EVENT(cb->ndi_evtcb_cookie)->callback_list =
18050Sstevel@tonic-gate 		    cb->ndi_evtcb_next;
18060Sstevel@tonic-gate 	}
18070Sstevel@tonic-gate 
18080Sstevel@tonic-gate 	kmem_free(cb, sizeof (ndi_event_callbacks_t));
18090Sstevel@tonic-gate }
18100Sstevel@tonic-gate 
18110Sstevel@tonic-gate /*
18120Sstevel@tonic-gate  * ndi_event_run_callbacks() performs event callbacks for the event
18130Sstevel@tonic-gate  * specified by cookie, if this is among those bound to the
18140Sstevel@tonic-gate  * supplied handle.
18150Sstevel@tonic-gate  * If the event is among those bound to the handle, none,
18160Sstevel@tonic-gate  * some, or all of the handlers registered for the event
18170Sstevel@tonic-gate  * will be called, according to the delivery attributes of
18180Sstevel@tonic-gate  * the event.
18190Sstevel@tonic-gate  * If the event attributes include NDI_EVENT_POST_TO_ALL
18200Sstevel@tonic-gate  * (the default), all the handlers for the event will be
18210Sstevel@tonic-gate  * called in an unspecified order.
18220Sstevel@tonic-gate  * If the event attributes include NDI_EVENT_POST_TO_TGT, only
18230Sstevel@tonic-gate  * the handlers (if any) registered by the driver identified by
18240Sstevel@tonic-gate  * rdip will be called.
18250Sstevel@tonic-gate  * If the event identified by cookie is not bound to the handle,
18260Sstevel@tonic-gate  * NDI_FAILURE will be returned.
18270Sstevel@tonic-gate  */
18280Sstevel@tonic-gate int
ndi_event_run_callbacks(ndi_event_hdl_t handle,dev_info_t * child_dip,ddi_eventcookie_t cookie,void * bus_impldata)18290Sstevel@tonic-gate ndi_event_run_callbacks(ndi_event_hdl_t handle, dev_info_t *child_dip,
18300Sstevel@tonic-gate 	ddi_eventcookie_t cookie, void *bus_impldata)
18310Sstevel@tonic-gate {
18320Sstevel@tonic-gate 	struct ndi_event_hdl *ndi_event_hdl = (struct ndi_event_hdl *)handle;
18330Sstevel@tonic-gate 	ndi_event_callbacks_t *next, *cb;
18340Sstevel@tonic-gate 	int attributes;
18350Sstevel@tonic-gate 
18360Sstevel@tonic-gate 	mutex_enter(&ndi_event_hdl->ndi_evthdl_mutex);
18370Sstevel@tonic-gate 
18380Sstevel@tonic-gate 	/* if this is not our event, fail */
18390Sstevel@tonic-gate 	if (ndi_event_is_defined(handle, cookie, &attributes) !=
18400Sstevel@tonic-gate 	    NDI_SUCCESS) {
18410Sstevel@tonic-gate 
18420Sstevel@tonic-gate 		mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
18430Sstevel@tonic-gate 		return (NDI_FAILURE);
18440Sstevel@tonic-gate 	}
18450Sstevel@tonic-gate 
18460Sstevel@tonic-gate 	mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
18470Sstevel@tonic-gate 
18480Sstevel@tonic-gate #ifdef NDI_EVENT_DEBUG
18490Sstevel@tonic-gate 	if (ndi_event_debug) {
18500Sstevel@tonic-gate 		cmn_err(CE_CONT, "ndi_event_run_callbacks:\n\t"
18510Sstevel@tonic-gate 		    "producer dip=%p (%s%d): cookie = %p, name = %s\n",
18520Sstevel@tonic-gate 		    (void *)ndi_event_hdl->ndi_evthdl_dip,
18530Sstevel@tonic-gate 		    ddi_node_name(ndi_event_hdl->ndi_evthdl_dip),
18540Sstevel@tonic-gate 		    ddi_get_instance(ndi_event_hdl->ndi_evthdl_dip),
18550Sstevel@tonic-gate 		    (void *)cookie,
18560Sstevel@tonic-gate 		    ndi_event_cookie_to_name(handle, cookie));
18570Sstevel@tonic-gate 	}
18580Sstevel@tonic-gate #endif /* #ifdef NDI_EVENT_DEBUG */
18590Sstevel@tonic-gate 
18600Sstevel@tonic-gate 
18610Sstevel@tonic-gate 	/*
18620Sstevel@tonic-gate 	 * The callback handlers may call conversion functions.  The conversion
18630Sstevel@tonic-gate 	 * functions may hold the ndi_evthdl_mutex during execution.  Thus, to
18640Sstevel@tonic-gate 	 * avoid a recursive mutex problem, only the ndi_evthdl_cb_mutex is
18650Sstevel@tonic-gate 	 * held.  The ndi_evthdl_mutex is not held when running the callbacks.
18660Sstevel@tonic-gate 	 */
18670Sstevel@tonic-gate 	mutex_enter(&ndi_event_hdl->ndi_evthdl_cb_mutex);
18680Sstevel@tonic-gate 
18690Sstevel@tonic-gate 	/* perform callbacks */
18700Sstevel@tonic-gate 	next = NDI_EVENT(cookie)->callback_list;
18710Sstevel@tonic-gate 	while (next != NULL) {
18720Sstevel@tonic-gate 
18730Sstevel@tonic-gate 		cb = next;
18740Sstevel@tonic-gate 		next = next->ndi_evtcb_next;
18750Sstevel@tonic-gate 
18760Sstevel@tonic-gate 		ASSERT(cb->ndi_evtcb_cookie == cookie);
18770Sstevel@tonic-gate 
18780Sstevel@tonic-gate 		if (attributes == NDI_EVENT_POST_TO_TGT &&
18790Sstevel@tonic-gate 		    child_dip != cb->ndi_evtcb_dip) {
18800Sstevel@tonic-gate 			continue;
18810Sstevel@tonic-gate 		}
18820Sstevel@tonic-gate 
18830Sstevel@tonic-gate 		cb->ndi_evtcb_callback(cb->ndi_evtcb_dip, cb->ndi_evtcb_cookie,
18840Sstevel@tonic-gate 		    cb->ndi_evtcb_arg, bus_impldata);
18850Sstevel@tonic-gate 
18860Sstevel@tonic-gate #ifdef NDI_EVENT_DEBUG
18870Sstevel@tonic-gate 		if (ndi_event_debug) {
18880Sstevel@tonic-gate 			cmn_err(CE_CONT,
18890Sstevel@tonic-gate 			    "\t\tconsumer dip=%p (%s%d)\n",
18900Sstevel@tonic-gate 			    (void *)cb->ndi_evtcb_dip,
18910Sstevel@tonic-gate 			    ddi_node_name(cb->ndi_evtcb_dip),
18920Sstevel@tonic-gate 			    ddi_get_instance(cb->ndi_evtcb_dip));
18930Sstevel@tonic-gate 		}
18940Sstevel@tonic-gate #endif
18950Sstevel@tonic-gate 
18960Sstevel@tonic-gate 	}
18970Sstevel@tonic-gate 
18980Sstevel@tonic-gate 	mutex_exit(&ndi_event_hdl->ndi_evthdl_cb_mutex);
18990Sstevel@tonic-gate 
19000Sstevel@tonic-gate #ifdef NDI_EVENT_DEBUG
19010Sstevel@tonic-gate 	if (ndi_event_debug) {
19020Sstevel@tonic-gate 		mutex_enter(&ndi_event_hdl->ndi_evthdl_mutex);
19030Sstevel@tonic-gate 		ndi_event_dump_hdl(ndi_event_hdl, "ndi_event_run_callbacks");
19040Sstevel@tonic-gate 		mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
19050Sstevel@tonic-gate 	}
19060Sstevel@tonic-gate #endif /* NDI_EVENT_DEBUG */
19070Sstevel@tonic-gate 
19080Sstevel@tonic-gate 	return (NDI_SUCCESS);
19090Sstevel@tonic-gate }
19100Sstevel@tonic-gate 
19110Sstevel@tonic-gate 
19120Sstevel@tonic-gate /*
19130Sstevel@tonic-gate  * perform one callback for a specified cookie and just one target
19140Sstevel@tonic-gate  */
19150Sstevel@tonic-gate int
ndi_event_do_callback(ndi_event_hdl_t handle,dev_info_t * child_dip,ddi_eventcookie_t cookie,void * bus_impldata)19160Sstevel@tonic-gate ndi_event_do_callback(ndi_event_hdl_t handle, dev_info_t *child_dip,
19170Sstevel@tonic-gate 	ddi_eventcookie_t cookie, void *bus_impldata)
19180Sstevel@tonic-gate {
19190Sstevel@tonic-gate 	struct ndi_event_hdl *ndi_event_hdl = (struct ndi_event_hdl *)handle;
19200Sstevel@tonic-gate 	ndi_event_callbacks_t *next, *cb;
19210Sstevel@tonic-gate 	int attributes;
19220Sstevel@tonic-gate 
19230Sstevel@tonic-gate 	mutex_enter(&ndi_event_hdl->ndi_evthdl_mutex);
19240Sstevel@tonic-gate 
19250Sstevel@tonic-gate 	/* if this is not our event, fail */
19260Sstevel@tonic-gate 	if (ndi_event_is_defined(handle, cookie, &attributes) !=
19270Sstevel@tonic-gate 	    NDI_SUCCESS) {
19280Sstevel@tonic-gate 
19290Sstevel@tonic-gate 		mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
19300Sstevel@tonic-gate 
19310Sstevel@tonic-gate 		return (NDI_FAILURE);
19320Sstevel@tonic-gate 	}
19330Sstevel@tonic-gate 
19340Sstevel@tonic-gate 	mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
19350Sstevel@tonic-gate 
19360Sstevel@tonic-gate #ifdef NDI_EVENT_DEBUG
19370Sstevel@tonic-gate 	if (ndi_event_debug) {
19380Sstevel@tonic-gate 		cmn_err(CE_CONT, "ndi_event_run_callbacks:\n\t"
19390Sstevel@tonic-gate 		    "producer dip=%p (%s%d): cookie = %p, name = %s\n",
19400Sstevel@tonic-gate 		    (void *)ndi_event_hdl->ndi_evthdl_dip,
19410Sstevel@tonic-gate 		    ddi_node_name(ndi_event_hdl->ndi_evthdl_dip),
19420Sstevel@tonic-gate 		    ddi_get_instance(ndi_event_hdl->ndi_evthdl_dip),
19430Sstevel@tonic-gate 		    (void *)cookie,
19440Sstevel@tonic-gate 		    ndi_event_cookie_to_name(handle, cookie));
19450Sstevel@tonic-gate 	}
19460Sstevel@tonic-gate #endif
19470Sstevel@tonic-gate 
19480Sstevel@tonic-gate 
19490Sstevel@tonic-gate 	/*
19500Sstevel@tonic-gate 	 * we only grab the cb mutex because the callback handlers
19510Sstevel@tonic-gate 	 * may call the conversion functions which would cause a recursive
19520Sstevel@tonic-gate 	 * mutex problem
19530Sstevel@tonic-gate 	 */
19540Sstevel@tonic-gate 	mutex_enter(&ndi_event_hdl->ndi_evthdl_cb_mutex);
19550Sstevel@tonic-gate 
19560Sstevel@tonic-gate 	/* perform callbacks */
19570Sstevel@tonic-gate 	for (next = NDI_EVENT(cookie)->callback_list; next != NULL; ) {
19580Sstevel@tonic-gate 		cb = next;
19590Sstevel@tonic-gate 		next = next->ndi_evtcb_next;
19600Sstevel@tonic-gate 
19610Sstevel@tonic-gate 		if (cb->ndi_evtcb_dip == child_dip) {
19620Sstevel@tonic-gate 			cb->ndi_evtcb_callback(cb->ndi_evtcb_dip,
19630Sstevel@tonic-gate 			    cb->ndi_evtcb_cookie, cb->ndi_evtcb_arg,
19640Sstevel@tonic-gate 			    bus_impldata);
19650Sstevel@tonic-gate 
19660Sstevel@tonic-gate #ifdef NDI_EVENT_DEBUG
19670Sstevel@tonic-gate 			if (ndi_event_debug) {
19680Sstevel@tonic-gate 				cmn_err(CE_CONT,
19690Sstevel@tonic-gate 				    "\t\tconsumer dip=%p (%s%d)\n",
19700Sstevel@tonic-gate 				    (void *)cb->ndi_evtcb_dip,
19710Sstevel@tonic-gate 				    ddi_node_name(cb->ndi_evtcb_dip),
19720Sstevel@tonic-gate 				    ddi_get_instance(cb->ndi_evtcb_dip));
19730Sstevel@tonic-gate 			}
19740Sstevel@tonic-gate #endif
19750Sstevel@tonic-gate 			break;
19760Sstevel@tonic-gate 		}
19770Sstevel@tonic-gate 	}
19780Sstevel@tonic-gate 
19790Sstevel@tonic-gate 	mutex_exit(&ndi_event_hdl->ndi_evthdl_cb_mutex);
19800Sstevel@tonic-gate 
19810Sstevel@tonic-gate #ifdef NDI_EVENT_DEBUG
19820Sstevel@tonic-gate 	if (ndi_event_debug) {
19830Sstevel@tonic-gate 		mutex_enter(&ndi_event_hdl->ndi_evthdl_mutex);
19840Sstevel@tonic-gate 		ndi_event_dump_hdl(ndi_event_hdl, "ndi_event_run_callbacks");
19850Sstevel@tonic-gate 		mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
19860Sstevel@tonic-gate 	}
19870Sstevel@tonic-gate #endif /* NDI_EVENT_DEBUG */
19880Sstevel@tonic-gate 
19890Sstevel@tonic-gate 	return (NDI_SUCCESS);
19900Sstevel@tonic-gate }
19910Sstevel@tonic-gate 
19920Sstevel@tonic-gate 
19930Sstevel@tonic-gate /*
19940Sstevel@tonic-gate  * ndi_event_tag_to_cookie: utility function to find an event cookie
19950Sstevel@tonic-gate  * given an event tag
19960Sstevel@tonic-gate  */
19970Sstevel@tonic-gate ddi_eventcookie_t
ndi_event_tag_to_cookie(ndi_event_hdl_t handle,int event_tag)19980Sstevel@tonic-gate ndi_event_tag_to_cookie(ndi_event_hdl_t handle, int event_tag)
19990Sstevel@tonic-gate {
20000Sstevel@tonic-gate 	struct ndi_event_hdl *ndi_event_hdl = (struct ndi_event_hdl *)handle;
20010Sstevel@tonic-gate 	ndi_event_cookie_t *list;
20020Sstevel@tonic-gate 
20030Sstevel@tonic-gate 	mutex_enter(&ndi_event_hdl->ndi_evthdl_mutex);
20040Sstevel@tonic-gate 
20050Sstevel@tonic-gate 	list = ndi_event_hdl->ndi_evthdl_cookie_list;
20060Sstevel@tonic-gate 	while (list != NULL) {
20070Sstevel@tonic-gate 		if (NDI_EVENT_TAG(list) == event_tag) {
20080Sstevel@tonic-gate 			mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
20090Sstevel@tonic-gate 			return ((ddi_eventcookie_t)list);
20100Sstevel@tonic-gate 		}
20110Sstevel@tonic-gate 
20120Sstevel@tonic-gate 		list = list->next_cookie;
20130Sstevel@tonic-gate 	}
20140Sstevel@tonic-gate 
20150Sstevel@tonic-gate 	mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
20160Sstevel@tonic-gate 	return (NULL);
20170Sstevel@tonic-gate }
20180Sstevel@tonic-gate 
20190Sstevel@tonic-gate /*
20200Sstevel@tonic-gate  * ndi_event_cookie_to_tag: utility function to find a event tag
20210Sstevel@tonic-gate  * given an event_cookie
20220Sstevel@tonic-gate  */
20230Sstevel@tonic-gate int
ndi_event_cookie_to_tag(ndi_event_hdl_t handle,ddi_eventcookie_t cookie)20240Sstevel@tonic-gate ndi_event_cookie_to_tag(ndi_event_hdl_t handle, ddi_eventcookie_t cookie)
20250Sstevel@tonic-gate {
20260Sstevel@tonic-gate 	struct ndi_event_hdl *ndi_event_hdl = (struct ndi_event_hdl *)handle;
20270Sstevel@tonic-gate 	ndi_event_cookie_t *list;
20280Sstevel@tonic-gate 
20290Sstevel@tonic-gate 	mutex_enter(&ndi_event_hdl->ndi_evthdl_mutex);
20300Sstevel@tonic-gate 
20310Sstevel@tonic-gate 	list = ndi_event_hdl->ndi_evthdl_cookie_list;
20320Sstevel@tonic-gate 
20330Sstevel@tonic-gate 	while (list != NULL) {
20340Sstevel@tonic-gate 		if ((ddi_eventcookie_t)list == cookie) {
20350Sstevel@tonic-gate 			mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
20360Sstevel@tonic-gate 			return (NDI_EVENT_TAG(list));
20370Sstevel@tonic-gate 		}
20380Sstevel@tonic-gate 
20390Sstevel@tonic-gate 		list = list->next_cookie;
20400Sstevel@tonic-gate 	}
20410Sstevel@tonic-gate 
20420Sstevel@tonic-gate 	mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
20430Sstevel@tonic-gate 	return (NDI_FAILURE);
20440Sstevel@tonic-gate 
20450Sstevel@tonic-gate }
20460Sstevel@tonic-gate 
20470Sstevel@tonic-gate /*
20480Sstevel@tonic-gate  * ndi_event_cookie_to_name: utility function to find an event name
20490Sstevel@tonic-gate  * given an event_cookie
20500Sstevel@tonic-gate  */
20510Sstevel@tonic-gate char *
ndi_event_cookie_to_name(ndi_event_hdl_t handle,ddi_eventcookie_t cookie)20520Sstevel@tonic-gate ndi_event_cookie_to_name(ndi_event_hdl_t handle, ddi_eventcookie_t cookie)
20530Sstevel@tonic-gate {
20540Sstevel@tonic-gate 	struct ndi_event_hdl *ndi_event_hdl = (struct ndi_event_hdl *)handle;
20550Sstevel@tonic-gate 	ndi_event_cookie_t *list;
20560Sstevel@tonic-gate 
20570Sstevel@tonic-gate 	mutex_enter(&ndi_event_hdl->ndi_evthdl_mutex);
20580Sstevel@tonic-gate 
20590Sstevel@tonic-gate 	list = ndi_event_hdl->ndi_evthdl_cookie_list;
20600Sstevel@tonic-gate 
20610Sstevel@tonic-gate 	while (list != NULL) {
20620Sstevel@tonic-gate 		if (list == NDI_EVENT(cookie)) {
20630Sstevel@tonic-gate 			mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
20640Sstevel@tonic-gate 			return (NDI_EVENT_NAME(list));
20650Sstevel@tonic-gate 		}
20660Sstevel@tonic-gate 
20670Sstevel@tonic-gate 		list = list->next_cookie;
20680Sstevel@tonic-gate 	}
20690Sstevel@tonic-gate 
20700Sstevel@tonic-gate 	mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
20710Sstevel@tonic-gate 	return (NULL);
20720Sstevel@tonic-gate }
20730Sstevel@tonic-gate 
20740Sstevel@tonic-gate /*
20750Sstevel@tonic-gate  * ndi_event_tag_to_name: utility function to find an event name
20760Sstevel@tonic-gate  * given an event tag
20770Sstevel@tonic-gate  */
20780Sstevel@tonic-gate char *
ndi_event_tag_to_name(ndi_event_hdl_t handle,int event_tag)20790Sstevel@tonic-gate ndi_event_tag_to_name(ndi_event_hdl_t handle, int event_tag)
20800Sstevel@tonic-gate {
20810Sstevel@tonic-gate 	struct ndi_event_hdl *ndi_event_hdl = (struct ndi_event_hdl *)handle;
20820Sstevel@tonic-gate 	ndi_event_cookie_t *list;
20830Sstevel@tonic-gate 
20840Sstevel@tonic-gate 	mutex_enter(&ndi_event_hdl->ndi_evthdl_mutex);
20850Sstevel@tonic-gate 
20860Sstevel@tonic-gate 	list = ndi_event_hdl->ndi_evthdl_cookie_list;
20870Sstevel@tonic-gate 
20880Sstevel@tonic-gate 	while (list) {
20890Sstevel@tonic-gate 		if (NDI_EVENT_TAG(list) == event_tag) {
20900Sstevel@tonic-gate 			mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
20910Sstevel@tonic-gate 			return (NDI_EVENT_NAME(list));
20920Sstevel@tonic-gate 		}
20930Sstevel@tonic-gate 
20940Sstevel@tonic-gate 		list = list->next_cookie;
20950Sstevel@tonic-gate 	}
20960Sstevel@tonic-gate 
20970Sstevel@tonic-gate 	mutex_exit(&ndi_event_hdl->ndi_evthdl_mutex);
20980Sstevel@tonic-gate 
20990Sstevel@tonic-gate 	return (NULL);
21000Sstevel@tonic-gate }
21010Sstevel@tonic-gate 
21020Sstevel@tonic-gate #ifdef NDI_EVENT_DEBUG
21030Sstevel@tonic-gate void
ndi_event_dump_hdl(struct ndi_event_hdl * hdl,char * location)21040Sstevel@tonic-gate ndi_event_dump_hdl(struct ndi_event_hdl *hdl, char *location)
21050Sstevel@tonic-gate {
21060Sstevel@tonic-gate 
21070Sstevel@tonic-gate 
21080Sstevel@tonic-gate 	ndi_event_callbacks_t *next;
21090Sstevel@tonic-gate 	ndi_event_cookie_t *list;
21100Sstevel@tonic-gate 
21110Sstevel@tonic-gate 	ASSERT(mutex_owned(&hdl->ndi_evthdl_mutex));
21120Sstevel@tonic-gate 	list = hdl->ndi_evthdl_cookie_list;
21130Sstevel@tonic-gate 
21140Sstevel@tonic-gate 	cmn_err(CE_CONT, "%s: event handle (%p): dip = %p (%s%d)\n",
21157224Scth 	    location, (void *)hdl, (void *)hdl->ndi_evthdl_dip,
21167224Scth 	    ddi_node_name(hdl->ndi_evthdl_dip),
21177224Scth 	    ddi_get_instance(hdl->ndi_evthdl_dip));
21180Sstevel@tonic-gate 	cmn_err(CE_CONT, "\thigh=%d other=%d n=%d\n",
21197224Scth 	    hdl->ndi_evthdl_high_plevels, hdl->ndi_evthdl_other_plevels,
21207224Scth 	    hdl->ndi_evthdl_n_events);
21210Sstevel@tonic-gate 
21220Sstevel@tonic-gate 	cmn_err(CE_CONT, "\tevent cookies:\n");
21230Sstevel@tonic-gate 	while (list) {
21247224Scth 		cmn_err(CE_CONT, "\t\ttag=%d name=%s p=%d a=%x dd=%p\n",
21257224Scth 		    NDI_EVENT_TAG(list), NDI_EVENT_NAME(list),
21267224Scth 		    NDI_EVENT_PLEVEL(list), NDI_EVENT_ATTRIBUTES(list),
21277224Scth 		    (void *)NDI_EVENT_DDIP(list));
21280Sstevel@tonic-gate 		cmn_err(CE_CONT, "\t\tcallbacks:\n");
21290Sstevel@tonic-gate 		for (next = list->callback_list; next != NULL;
21300Sstevel@tonic-gate 		    next = next->ndi_evtcb_next) {
21310Sstevel@tonic-gate 			cmn_err(CE_CONT,
21320Sstevel@tonic-gate 			    "\t\t  dip=%p (%s%d) cookie=%p arg=%p\n",
21330Sstevel@tonic-gate 			    (void*)next->ndi_evtcb_dip,
21340Sstevel@tonic-gate 			    ddi_driver_name(next->ndi_evtcb_dip),
21350Sstevel@tonic-gate 			    ddi_get_instance(next->ndi_evtcb_dip),
21360Sstevel@tonic-gate 			    (void *)next->ndi_evtcb_cookie,
21370Sstevel@tonic-gate 			    next->ndi_evtcb_arg);
21380Sstevel@tonic-gate 		}
21390Sstevel@tonic-gate 
21400Sstevel@tonic-gate 		list = list->next_cookie;
21410Sstevel@tonic-gate 	}
21420Sstevel@tonic-gate 
21430Sstevel@tonic-gate 	cmn_err(CE_CONT, "\n");
21440Sstevel@tonic-gate }
21450Sstevel@tonic-gate #endif
21460Sstevel@tonic-gate 
21470Sstevel@tonic-gate int
ndi_dev_is_prom_node(dev_info_t * dip)21480Sstevel@tonic-gate ndi_dev_is_prom_node(dev_info_t *dip)
21490Sstevel@tonic-gate {
21500Sstevel@tonic-gate 	return (DEVI(dip)->devi_node_class == DDI_NC_PROM);
21510Sstevel@tonic-gate }
21520Sstevel@tonic-gate 
21530Sstevel@tonic-gate int
ndi_dev_is_pseudo_node(dev_info_t * dip)21540Sstevel@tonic-gate ndi_dev_is_pseudo_node(dev_info_t *dip)
21550Sstevel@tonic-gate {
21560Sstevel@tonic-gate 	/*
21570Sstevel@tonic-gate 	 * NOTE: this does NOT mean the pseudo branch of the device tree,
21588912SChris.Horne@Sun.COM 	 * it means the node was created by software (DEVI_SID_NODEID ||
21598912SChris.Horne@Sun.COM 	 * DEVI_PSEUDO_NODEID || DEVI_SID_HIDDEN_NODEID) instead of being
21608912SChris.Horne@Sun.COM 	 * generated from a PROM node.
21610Sstevel@tonic-gate 	 */
21620Sstevel@tonic-gate 	return (DEVI(dip)->devi_node_class == DDI_NC_PSEUDO);
21630Sstevel@tonic-gate }
21640Sstevel@tonic-gate 
21650Sstevel@tonic-gate int
ndi_dev_is_persistent_node(dev_info_t * dip)21660Sstevel@tonic-gate ndi_dev_is_persistent_node(dev_info_t *dip)
21670Sstevel@tonic-gate {
21680Sstevel@tonic-gate 	return ((DEVI(dip)->devi_node_attributes & DDI_PERSISTENT) != 0);
21690Sstevel@tonic-gate }
21700Sstevel@tonic-gate 
21710Sstevel@tonic-gate int
ndi_dev_is_hidden_node(dev_info_t * dip)21728912SChris.Horne@Sun.COM ndi_dev_is_hidden_node(dev_info_t *dip)
21738912SChris.Horne@Sun.COM {
21748912SChris.Horne@Sun.COM 	return ((DEVI(dip)->devi_node_attributes & DDI_HIDDEN_NODE) != 0);
21758912SChris.Horne@Sun.COM }
21768912SChris.Horne@Sun.COM 
2177*10696SDavid.Hollister@Sun.COM int
ndi_dev_is_hotplug_node(dev_info_t * dip)2178*10696SDavid.Hollister@Sun.COM ndi_dev_is_hotplug_node(dev_info_t *dip)
2179*10696SDavid.Hollister@Sun.COM {
2180*10696SDavid.Hollister@Sun.COM 	return ((DEVI(dip)->devi_node_attributes & DDI_HOTPLUG_NODE) != 0);
2181*10696SDavid.Hollister@Sun.COM }
2182*10696SDavid.Hollister@Sun.COM 
21838912SChris.Horne@Sun.COM void
ndi_devi_set_hidden(dev_info_t * dip)21848912SChris.Horne@Sun.COM ndi_devi_set_hidden(dev_info_t *dip)
21858912SChris.Horne@Sun.COM {
21868912SChris.Horne@Sun.COM 	DEVI(dip)->devi_node_attributes |= DDI_HIDDEN_NODE;
21878912SChris.Horne@Sun.COM }
21888912SChris.Horne@Sun.COM 
21898912SChris.Horne@Sun.COM void
ndi_devi_clr_hidden(dev_info_t * dip)21908912SChris.Horne@Sun.COM ndi_devi_clr_hidden(dev_info_t *dip)
21918912SChris.Horne@Sun.COM {
21928912SChris.Horne@Sun.COM 	DEVI(dip)->devi_node_attributes &= ~DDI_HIDDEN_NODE;
21938912SChris.Horne@Sun.COM }
21948912SChris.Horne@Sun.COM 
21958912SChris.Horne@Sun.COM int
i_ndi_dev_is_auto_assigned_node(dev_info_t * dip)21960Sstevel@tonic-gate i_ndi_dev_is_auto_assigned_node(dev_info_t *dip)
21970Sstevel@tonic-gate {
21980Sstevel@tonic-gate 	return ((DEVI(dip)->devi_node_attributes &
21990Sstevel@tonic-gate 	    DDI_AUTO_ASSIGNED_NODEID) != 0);
22000Sstevel@tonic-gate }
22010Sstevel@tonic-gate 
22020Sstevel@tonic-gate void
i_ndi_set_node_class(dev_info_t * dip,ddi_node_class_t c)22030Sstevel@tonic-gate i_ndi_set_node_class(dev_info_t *dip, ddi_node_class_t c)
22040Sstevel@tonic-gate {
22050Sstevel@tonic-gate 	DEVI(dip)->devi_node_class = c;
22060Sstevel@tonic-gate }
22070Sstevel@tonic-gate 
22080Sstevel@tonic-gate ddi_node_class_t
i_ndi_get_node_class(dev_info_t * dip)22090Sstevel@tonic-gate i_ndi_get_node_class(dev_info_t *dip)
22100Sstevel@tonic-gate {
22110Sstevel@tonic-gate 	return (DEVI(dip)->devi_node_class);
22120Sstevel@tonic-gate }
22130Sstevel@tonic-gate 
22140Sstevel@tonic-gate void
i_ndi_set_node_attributes(dev_info_t * dip,int p)22150Sstevel@tonic-gate i_ndi_set_node_attributes(dev_info_t *dip, int p)
22160Sstevel@tonic-gate {
22170Sstevel@tonic-gate 	DEVI(dip)->devi_node_attributes = p;
22180Sstevel@tonic-gate }
22190Sstevel@tonic-gate 
22200Sstevel@tonic-gate int
i_ndi_get_node_attributes(dev_info_t * dip)22210Sstevel@tonic-gate i_ndi_get_node_attributes(dev_info_t *dip)
22220Sstevel@tonic-gate {
22230Sstevel@tonic-gate 	return (DEVI(dip)->devi_node_attributes);
22240Sstevel@tonic-gate }
22250Sstevel@tonic-gate 
22260Sstevel@tonic-gate void
i_ndi_set_nodeid(dev_info_t * dip,int n)22270Sstevel@tonic-gate i_ndi_set_nodeid(dev_info_t *dip, int n)
22280Sstevel@tonic-gate {
22290Sstevel@tonic-gate 	DEVI(dip)->devi_nodeid = n;
22300Sstevel@tonic-gate }
22310Sstevel@tonic-gate 
22320Sstevel@tonic-gate void
ndi_set_acc_fault(ddi_acc_handle_t ah)22330Sstevel@tonic-gate ndi_set_acc_fault(ddi_acc_handle_t ah)
22340Sstevel@tonic-gate {
22350Sstevel@tonic-gate 	i_ddi_acc_set_fault(ah);
22360Sstevel@tonic-gate }
22370Sstevel@tonic-gate 
22380Sstevel@tonic-gate void
ndi_clr_acc_fault(ddi_acc_handle_t ah)22390Sstevel@tonic-gate ndi_clr_acc_fault(ddi_acc_handle_t ah)
22400Sstevel@tonic-gate {
22410Sstevel@tonic-gate 	i_ddi_acc_clr_fault(ah);
22420Sstevel@tonic-gate }
22430Sstevel@tonic-gate 
22440Sstevel@tonic-gate void
ndi_set_dma_fault(ddi_dma_handle_t dh)22450Sstevel@tonic-gate ndi_set_dma_fault(ddi_dma_handle_t dh)
22460Sstevel@tonic-gate {
22470Sstevel@tonic-gate 	i_ddi_dma_set_fault(dh);
22480Sstevel@tonic-gate }
22490Sstevel@tonic-gate 
22500Sstevel@tonic-gate void
ndi_clr_dma_fault(ddi_dma_handle_t dh)22510Sstevel@tonic-gate ndi_clr_dma_fault(ddi_dma_handle_t dh)
22520Sstevel@tonic-gate {
22530Sstevel@tonic-gate 	i_ddi_dma_clr_fault(dh);
22540Sstevel@tonic-gate }
22550Sstevel@tonic-gate 
22560Sstevel@tonic-gate /*
22570Sstevel@tonic-gate  *  The default fault-handler, called when the event posted by
22580Sstevel@tonic-gate  *  ddi_dev_report_fault() reaches rootnex.
22590Sstevel@tonic-gate  */
22600Sstevel@tonic-gate static void
i_ddi_fault_handler(dev_info_t * dip,struct ddi_fault_event_data * fedp)22610Sstevel@tonic-gate i_ddi_fault_handler(dev_info_t *dip, struct ddi_fault_event_data *fedp)
22620Sstevel@tonic-gate {
22630Sstevel@tonic-gate 	ASSERT(fedp);
22640Sstevel@tonic-gate 
22650Sstevel@tonic-gate 	mutex_enter(&(DEVI(dip)->devi_lock));
22660Sstevel@tonic-gate 	if (!DEVI_IS_DEVICE_OFFLINE(dip)) {
22670Sstevel@tonic-gate 		switch (fedp->f_impact) {
22680Sstevel@tonic-gate 		case DDI_SERVICE_LOST:
22690Sstevel@tonic-gate 			DEVI_SET_DEVICE_DOWN(dip);
22700Sstevel@tonic-gate 			break;
22710Sstevel@tonic-gate 
22720Sstevel@tonic-gate 		case DDI_SERVICE_DEGRADED:
22730Sstevel@tonic-gate 			DEVI_SET_DEVICE_DEGRADED(dip);
22740Sstevel@tonic-gate 			break;
22750Sstevel@tonic-gate 
22760Sstevel@tonic-gate 		case DDI_SERVICE_UNAFFECTED:
22770Sstevel@tonic-gate 		default:
22780Sstevel@tonic-gate 			break;
22790Sstevel@tonic-gate 
22800Sstevel@tonic-gate 		case DDI_SERVICE_RESTORED:
22810Sstevel@tonic-gate 			DEVI_SET_DEVICE_UP(dip);
22820Sstevel@tonic-gate 			break;
22830Sstevel@tonic-gate 		}
22840Sstevel@tonic-gate 	}
22850Sstevel@tonic-gate 	mutex_exit(&(DEVI(dip)->devi_lock));
22860Sstevel@tonic-gate }
22870Sstevel@tonic-gate 
22880Sstevel@tonic-gate /*
22890Sstevel@tonic-gate  * The default fault-logger, called when the event posted by
22900Sstevel@tonic-gate  * ddi_dev_report_fault() reaches rootnex.
22910Sstevel@tonic-gate  */
22920Sstevel@tonic-gate /*ARGSUSED*/
22930Sstevel@tonic-gate static void
i_ddi_fault_logger(dev_info_t * rdip,struct ddi_fault_event_data * fedp)22940Sstevel@tonic-gate i_ddi_fault_logger(dev_info_t *rdip, struct ddi_fault_event_data *fedp)
22950Sstevel@tonic-gate {
22960Sstevel@tonic-gate 	ddi_devstate_t newstate;
22970Sstevel@tonic-gate 	const char *action;
22980Sstevel@tonic-gate 	const char *servstate;
22990Sstevel@tonic-gate 	const char *location;
23000Sstevel@tonic-gate 	int bad;
23010Sstevel@tonic-gate 	int changed;
23020Sstevel@tonic-gate 	int level;
23030Sstevel@tonic-gate 	int still;
23040Sstevel@tonic-gate 
23050Sstevel@tonic-gate 	ASSERT(fedp);
23060Sstevel@tonic-gate 
23070Sstevel@tonic-gate 	bad = 0;
23080Sstevel@tonic-gate 	switch (fedp->f_location) {
23090Sstevel@tonic-gate 	case DDI_DATAPATH_FAULT:
23100Sstevel@tonic-gate 		location = "in datapath to";
23110Sstevel@tonic-gate 		break;
23120Sstevel@tonic-gate 	case DDI_DEVICE_FAULT:
23130Sstevel@tonic-gate 		location = "in";
23140Sstevel@tonic-gate 		break;
23150Sstevel@tonic-gate 	case DDI_EXTERNAL_FAULT:
23160Sstevel@tonic-gate 		location = "external to";
23170Sstevel@tonic-gate 		break;
23180Sstevel@tonic-gate 	default:
23190Sstevel@tonic-gate 		location = "somewhere near";
23200Sstevel@tonic-gate 		bad = 1;
23210Sstevel@tonic-gate 		break;
23220Sstevel@tonic-gate 	}
23230Sstevel@tonic-gate 
23240Sstevel@tonic-gate 	newstate = ddi_get_devstate(fedp->f_dip);
23250Sstevel@tonic-gate 	switch (newstate) {
23260Sstevel@tonic-gate 	case DDI_DEVSTATE_OFFLINE:
23270Sstevel@tonic-gate 		servstate = "unavailable";
23280Sstevel@tonic-gate 		break;
23290Sstevel@tonic-gate 	case DDI_DEVSTATE_DOWN:
23300Sstevel@tonic-gate 		servstate = "unavailable";
23310Sstevel@tonic-gate 		break;
23320Sstevel@tonic-gate 	case DDI_DEVSTATE_QUIESCED:
23330Sstevel@tonic-gate 		servstate = "suspended";
23340Sstevel@tonic-gate 		break;
23350Sstevel@tonic-gate 	case DDI_DEVSTATE_DEGRADED:
23360Sstevel@tonic-gate 		servstate = "degraded";
23370Sstevel@tonic-gate 		break;
23380Sstevel@tonic-gate 	default:
23390Sstevel@tonic-gate 		servstate = "available";
23400Sstevel@tonic-gate 		break;
23410Sstevel@tonic-gate 	}
23420Sstevel@tonic-gate 
23430Sstevel@tonic-gate 	changed = (newstate != fedp->f_oldstate);
23440Sstevel@tonic-gate 	level = (newstate < fedp->f_oldstate) ? CE_WARN : CE_NOTE;
23450Sstevel@tonic-gate 	switch (fedp->f_impact) {
23460Sstevel@tonic-gate 	case DDI_SERVICE_LOST:
23470Sstevel@tonic-gate 	case DDI_SERVICE_DEGRADED:
23480Sstevel@tonic-gate 	case DDI_SERVICE_UNAFFECTED:
23490Sstevel@tonic-gate 		/* fault detected; service [still] <servstate> */
23500Sstevel@tonic-gate 		action = "fault detected";
23510Sstevel@tonic-gate 		still = !changed;
23520Sstevel@tonic-gate 		break;
23530Sstevel@tonic-gate 
23540Sstevel@tonic-gate 	case DDI_SERVICE_RESTORED:
23550Sstevel@tonic-gate 		if (newstate != DDI_DEVSTATE_UP) {
23560Sstevel@tonic-gate 			/* fault cleared; service still <servstate> */
23570Sstevel@tonic-gate 			action = "fault cleared";
23580Sstevel@tonic-gate 			still = 1;
23590Sstevel@tonic-gate 		} else if (changed) {
23600Sstevel@tonic-gate 			/* fault cleared; service <servstate> */
23610Sstevel@tonic-gate 			action = "fault cleared";
23620Sstevel@tonic-gate 			still = 0;
23630Sstevel@tonic-gate 		} else {
23640Sstevel@tonic-gate 			/* no fault; service <servstate> */
23650Sstevel@tonic-gate 			action = "no fault";
23660Sstevel@tonic-gate 			still = 0;
23670Sstevel@tonic-gate 		}
23680Sstevel@tonic-gate 		break;
23690Sstevel@tonic-gate 
23700Sstevel@tonic-gate 	default:
23710Sstevel@tonic-gate 		bad = 1;
23720Sstevel@tonic-gate 		break;
23730Sstevel@tonic-gate 	}
23740Sstevel@tonic-gate 
23750Sstevel@tonic-gate 	cmn_err(level, "!%s%d: %s %s device; service %s%s"+(bad|changed),
23767224Scth 	    ddi_driver_name(fedp->f_dip), ddi_get_instance(fedp->f_dip),
23777224Scth 	    bad ? "invalid report of fault" : action,
23787224Scth 	    location, still ? "still " : "", servstate);
23790Sstevel@tonic-gate 
23800Sstevel@tonic-gate 	cmn_err(level, "!%s%d: %s"+(bad|changed),
23817224Scth 	    ddi_driver_name(fedp->f_dip), ddi_get_instance(fedp->f_dip),
23827224Scth 	    fedp->f_message);
23830Sstevel@tonic-gate }
23840Sstevel@tonic-gate 
23850Sstevel@tonic-gate /*
23860Sstevel@tonic-gate  * Platform-settable pointers to fault handler and logger functions.
23870Sstevel@tonic-gate  * These are called by the default rootnex event-posting code when
23880Sstevel@tonic-gate  * a fault event reaches rootnex.
23890Sstevel@tonic-gate  */
23900Sstevel@tonic-gate void (*plat_fault_handler)(dev_info_t *, struct ddi_fault_event_data *) =
23910Sstevel@tonic-gate 	i_ddi_fault_handler;
23920Sstevel@tonic-gate void (*plat_fault_logger)(dev_info_t *, struct ddi_fault_event_data *) =
23930Sstevel@tonic-gate 	i_ddi_fault_logger;
23940Sstevel@tonic-gate 
23950Sstevel@tonic-gate /*
23960Sstevel@tonic-gate  * Rootnex event definitions ...
23970Sstevel@tonic-gate  */
23980Sstevel@tonic-gate enum rootnex_event_tags {
23990Sstevel@tonic-gate 	ROOTNEX_FAULT_EVENT
24000Sstevel@tonic-gate };
24010Sstevel@tonic-gate static ndi_event_hdl_t rootnex_event_hdl;
24020Sstevel@tonic-gate static ndi_event_definition_t rootnex_event_set[] = {
24030Sstevel@tonic-gate 	{
24040Sstevel@tonic-gate 		ROOTNEX_FAULT_EVENT,
24050Sstevel@tonic-gate 		DDI_DEVI_FAULT_EVENT,
24060Sstevel@tonic-gate 		EPL_INTERRUPT,
24070Sstevel@tonic-gate 		NDI_EVENT_POST_TO_ALL
24080Sstevel@tonic-gate 	}
24090Sstevel@tonic-gate };
24100Sstevel@tonic-gate static ndi_event_set_t rootnex_events = {
24110Sstevel@tonic-gate 	NDI_EVENTS_REV1,
24120Sstevel@tonic-gate 	sizeof (rootnex_event_set) / sizeof (rootnex_event_set[0]),
24130Sstevel@tonic-gate 	rootnex_event_set
24140Sstevel@tonic-gate };
24150Sstevel@tonic-gate 
24160Sstevel@tonic-gate /*
24170Sstevel@tonic-gate  * Initialize rootnex event handle
24180Sstevel@tonic-gate  */
24190Sstevel@tonic-gate void
i_ddi_rootnex_init_events(dev_info_t * dip)24200Sstevel@tonic-gate i_ddi_rootnex_init_events(dev_info_t *dip)
24210Sstevel@tonic-gate {
24220Sstevel@tonic-gate 	if (ndi_event_alloc_hdl(dip, (ddi_iblock_cookie_t)(LOCK_LEVEL-1),
24230Sstevel@tonic-gate 	    &rootnex_event_hdl, NDI_SLEEP) == NDI_SUCCESS) {
24240Sstevel@tonic-gate 		if (ndi_event_bind_set(rootnex_event_hdl,
24250Sstevel@tonic-gate 		    &rootnex_events, NDI_SLEEP) != NDI_SUCCESS) {
24260Sstevel@tonic-gate 			(void) ndi_event_free_hdl(rootnex_event_hdl);
24270Sstevel@tonic-gate 			rootnex_event_hdl = NULL;
24280Sstevel@tonic-gate 		}
24290Sstevel@tonic-gate 	}
24300Sstevel@tonic-gate }
24310Sstevel@tonic-gate 
24320Sstevel@tonic-gate /*
24330Sstevel@tonic-gate  *      Event-handling functions for rootnex
24340Sstevel@tonic-gate  *      These provide the standard implementation of fault handling
24350Sstevel@tonic-gate  */
24360Sstevel@tonic-gate /*ARGSUSED*/
24370Sstevel@tonic-gate int
i_ddi_rootnex_get_eventcookie(dev_info_t * dip,dev_info_t * rdip,char * eventname,ddi_eventcookie_t * cookiep)24380Sstevel@tonic-gate i_ddi_rootnex_get_eventcookie(dev_info_t *dip, dev_info_t *rdip,
24390Sstevel@tonic-gate 	char *eventname, ddi_eventcookie_t *cookiep)
24400Sstevel@tonic-gate {
24410Sstevel@tonic-gate 	if (rootnex_event_hdl == NULL)
24420Sstevel@tonic-gate 		return (NDI_FAILURE);
24430Sstevel@tonic-gate 	return (ndi_event_retrieve_cookie(rootnex_event_hdl, rdip, eventname,
24447224Scth 	    cookiep, NDI_EVENT_NOPASS));
24450Sstevel@tonic-gate }
24460Sstevel@tonic-gate 
24470Sstevel@tonic-gate /*ARGSUSED*/
24480Sstevel@tonic-gate int
i_ddi_rootnex_add_eventcall(dev_info_t * dip,dev_info_t * rdip,ddi_eventcookie_t eventid,void (* handler)(dev_info_t * dip,ddi_eventcookie_t event,void * arg,void * impl_data),void * arg,ddi_callback_id_t * cb_id)24490Sstevel@tonic-gate i_ddi_rootnex_add_eventcall(dev_info_t *dip, dev_info_t *rdip,
24500Sstevel@tonic-gate 	ddi_eventcookie_t eventid, void (*handler)(dev_info_t *dip,
24510Sstevel@tonic-gate 	ddi_eventcookie_t event, void *arg, void *impl_data), void *arg,
24520Sstevel@tonic-gate 	ddi_callback_id_t *cb_id)
24530Sstevel@tonic-gate {
24540Sstevel@tonic-gate 	if (rootnex_event_hdl == NULL)
24550Sstevel@tonic-gate 		return (NDI_FAILURE);
24560Sstevel@tonic-gate 	return (ndi_event_add_callback(rootnex_event_hdl, rdip,
24577224Scth 	    eventid, handler, arg, NDI_SLEEP, cb_id));
24580Sstevel@tonic-gate }
24590Sstevel@tonic-gate 
24600Sstevel@tonic-gate /*ARGSUSED*/
24610Sstevel@tonic-gate int
i_ddi_rootnex_remove_eventcall(dev_info_t * dip,ddi_callback_id_t cb_id)24620Sstevel@tonic-gate i_ddi_rootnex_remove_eventcall(dev_info_t *dip, ddi_callback_id_t cb_id)
24630Sstevel@tonic-gate {
24640Sstevel@tonic-gate 	if (rootnex_event_hdl == NULL)
24650Sstevel@tonic-gate 		return (NDI_FAILURE);
24660Sstevel@tonic-gate 
24670Sstevel@tonic-gate 	return (ndi_event_remove_callback(rootnex_event_hdl, cb_id));
24680Sstevel@tonic-gate }
24690Sstevel@tonic-gate 
24700Sstevel@tonic-gate /*ARGSUSED*/
24710Sstevel@tonic-gate int
i_ddi_rootnex_post_event(dev_info_t * dip,dev_info_t * rdip,ddi_eventcookie_t eventid,void * impl_data)24720Sstevel@tonic-gate i_ddi_rootnex_post_event(dev_info_t *dip, dev_info_t *rdip,
24730Sstevel@tonic-gate 	ddi_eventcookie_t eventid, void *impl_data)
24740Sstevel@tonic-gate {
24750Sstevel@tonic-gate 	int tag;
24760Sstevel@tonic-gate 
24770Sstevel@tonic-gate 	if (rootnex_event_hdl == NULL)
24780Sstevel@tonic-gate 		return (NDI_FAILURE);
24790Sstevel@tonic-gate 
24800Sstevel@tonic-gate 	tag = ndi_event_cookie_to_tag(rootnex_event_hdl, eventid);
24810Sstevel@tonic-gate 	if (tag == ROOTNEX_FAULT_EVENT) {
24820Sstevel@tonic-gate 		(*plat_fault_handler)(rdip, impl_data);
24830Sstevel@tonic-gate 		(*plat_fault_logger)(rdip, impl_data);
24840Sstevel@tonic-gate 	}
24850Sstevel@tonic-gate 	return (ndi_event_run_callbacks(rootnex_event_hdl, rdip,
24867224Scth 	    eventid, impl_data));
24870Sstevel@tonic-gate }
24886313Skrishnae 
24896313Skrishnae /*
24906313Skrishnae  * ndi_set_bus_private/ndi_get_bus_private:
24916313Skrishnae  * Get/set device bus private data in devinfo.
24926313Skrishnae  */
24936313Skrishnae void
ndi_set_bus_private(dev_info_t * dip,boolean_t up,uint32_t port_type,void * data)24946313Skrishnae ndi_set_bus_private(dev_info_t *dip, boolean_t up, uint32_t port_type,
24956313Skrishnae     void *data)
24966313Skrishnae {
24976313Skrishnae 	if (up) {
24986313Skrishnae 		DEVI(dip)->devi_bus.port_up.info.port.type = port_type;
24996313Skrishnae 		DEVI(dip)->devi_bus.port_up.priv_p = data;
25006313Skrishnae 	} else {
25016313Skrishnae 		DEVI(dip)->devi_bus.port_down.info.port.type = port_type;
25026313Skrishnae 		DEVI(dip)->devi_bus.port_down.priv_p = data;
25036313Skrishnae 	}
25046313Skrishnae }
25056313Skrishnae 
25066313Skrishnae void *
ndi_get_bus_private(dev_info_t * dip,boolean_t up)25076313Skrishnae ndi_get_bus_private(dev_info_t *dip, boolean_t up)
25086313Skrishnae {
25096313Skrishnae 	if (up)
25106313Skrishnae 		return (DEVI(dip)->devi_bus.port_up.priv_p);
25116313Skrishnae 	else
25126313Skrishnae 		return (DEVI(dip)->devi_bus.port_down.priv_p);
25136313Skrishnae }
25146313Skrishnae 
25156313Skrishnae boolean_t
ndi_port_type(dev_info_t * dip,boolean_t up,uint32_t port_type)25166313Skrishnae ndi_port_type(dev_info_t *dip, boolean_t up, uint32_t port_type)
25176313Skrishnae {
25186313Skrishnae 	if (up) {
25196313Skrishnae 		return ((DEVI(dip)->devi_bus.port_up.info.port.type) ==
25206313Skrishnae 		    port_type);
25216313Skrishnae 	} else {
25226313Skrishnae 		return ((DEVI(dip)->devi_bus.port_down.info.port.type) ==
25236313Skrishnae 		    port_type);
25246313Skrishnae 	}
25256313Skrishnae }
25268860SMatthew.Jacob@Sun.COM 
25278860SMatthew.Jacob@Sun.COM /* Interfaces for 'self' to set/get a child's flavor */
25288860SMatthew.Jacob@Sun.COM void
ndi_flavor_set(dev_info_t * child,ndi_flavor_t child_flavor)25298860SMatthew.Jacob@Sun.COM ndi_flavor_set(dev_info_t *child, ndi_flavor_t child_flavor)
25308860SMatthew.Jacob@Sun.COM {
25318860SMatthew.Jacob@Sun.COM 	DEVI(child)->devi_flavor = child_flavor;
25328860SMatthew.Jacob@Sun.COM }
25338860SMatthew.Jacob@Sun.COM 
25348860SMatthew.Jacob@Sun.COM ndi_flavor_t
ndi_flavor_get(dev_info_t * child)25358860SMatthew.Jacob@Sun.COM ndi_flavor_get(dev_info_t *child)
25368860SMatthew.Jacob@Sun.COM {
25378860SMatthew.Jacob@Sun.COM 	return (DEVI(child)->devi_flavor);
25388860SMatthew.Jacob@Sun.COM }
25398860SMatthew.Jacob@Sun.COM 
25408860SMatthew.Jacob@Sun.COM /*
25418860SMatthew.Jacob@Sun.COM  * Interfaces to maintain flavor-specific private data of flavored
25428860SMatthew.Jacob@Sun.COM  * children of self.
25438860SMatthew.Jacob@Sun.COM  *
25448860SMatthew.Jacob@Sun.COM  * The flavor count always includes the default (0) vanilla flavor,
25458860SMatthew.Jacob@Sun.COM  * but storage for the vanilla flavor data pointer is in the same
25468860SMatthew.Jacob@Sun.COM  * place that ddi_[sg]et_driver_private uses, so the flavorv
25478860SMatthew.Jacob@Sun.COM  * storage is just for flavors 1..{nflavors-1}.
25488860SMatthew.Jacob@Sun.COM  */
25498860SMatthew.Jacob@Sun.COM void
ndi_flavorv_alloc(dev_info_t * self,int nflavors)25508860SMatthew.Jacob@Sun.COM ndi_flavorv_alloc(dev_info_t *self, int nflavors)
25518860SMatthew.Jacob@Sun.COM {
25528860SMatthew.Jacob@Sun.COM 	ASSERT(nflavors > 0 && (DEVI(self)->devi_flavorv == NULL ||
25538860SMatthew.Jacob@Sun.COM 	    nflavors == DEVI(self)->devi_flavorv_n));
25548860SMatthew.Jacob@Sun.COM 	if (nflavors <= 1 || (DEVI(self)->devi_flavorv)) {
25558860SMatthew.Jacob@Sun.COM 		return;
25568860SMatthew.Jacob@Sun.COM 	}
25578860SMatthew.Jacob@Sun.COM 	DEVI(self)->devi_flavorv =
25588860SMatthew.Jacob@Sun.COM 	    kmem_zalloc((nflavors - 1) * sizeof (void *), KM_SLEEP);
25598860SMatthew.Jacob@Sun.COM 	DEVI(self)->devi_flavorv_n = nflavors;
25608860SMatthew.Jacob@Sun.COM }
25618860SMatthew.Jacob@Sun.COM 
25628860SMatthew.Jacob@Sun.COM void
ndi_flavorv_set(dev_info_t * self,ndi_flavor_t child_flavor,void * v)25638860SMatthew.Jacob@Sun.COM ndi_flavorv_set(dev_info_t *self, ndi_flavor_t child_flavor, void *v)
25648860SMatthew.Jacob@Sun.COM {
25658860SMatthew.Jacob@Sun.COM 	if (child_flavor == NDI_FLAVOR_VANILLA) {
25668860SMatthew.Jacob@Sun.COM 		ddi_set_driver_private(self, v);
25678860SMatthew.Jacob@Sun.COM 	} else {
2568*10696SDavid.Hollister@Sun.COM 		ASSERT(child_flavor < DEVI(self)->devi_flavorv_n &&
2569*10696SDavid.Hollister@Sun.COM 		    DEVI(self)->devi_flavorv != NULL);
2570*10696SDavid.Hollister@Sun.COM 		if (child_flavor > DEVI(self)->devi_flavorv_n ||
2571*10696SDavid.Hollister@Sun.COM 		    DEVI(self)->devi_flavorv == NULL) {
2572*10696SDavid.Hollister@Sun.COM 			return;
2573*10696SDavid.Hollister@Sun.COM 		}
25748860SMatthew.Jacob@Sun.COM 		DEVI(self)->devi_flavorv[child_flavor - 1] = v;
25758860SMatthew.Jacob@Sun.COM 	}
25768860SMatthew.Jacob@Sun.COM }
25778860SMatthew.Jacob@Sun.COM 
25788860SMatthew.Jacob@Sun.COM void	*
ndi_flavorv_get(dev_info_t * self,ndi_flavor_t child_flavor)25798860SMatthew.Jacob@Sun.COM ndi_flavorv_get(dev_info_t *self, ndi_flavor_t child_flavor)
25808860SMatthew.Jacob@Sun.COM {
25818860SMatthew.Jacob@Sun.COM 	if (child_flavor == NDI_FLAVOR_VANILLA) {
25828860SMatthew.Jacob@Sun.COM 		return (ddi_get_driver_private(self));
25838860SMatthew.Jacob@Sun.COM 	} else {
2584*10696SDavid.Hollister@Sun.COM 		ASSERT(child_flavor < DEVI(self)->devi_flavorv_n &&
2585*10696SDavid.Hollister@Sun.COM 		    DEVI(self)->devi_flavorv != NULL);
2586*10696SDavid.Hollister@Sun.COM 		if (child_flavor > DEVI(self)->devi_flavorv_n ||
2587*10696SDavid.Hollister@Sun.COM 		    DEVI(self)->devi_flavorv == NULL) {
2588*10696SDavid.Hollister@Sun.COM 			return (NULL);
2589*10696SDavid.Hollister@Sun.COM 		}
25908860SMatthew.Jacob@Sun.COM 		return (DEVI(self)->devi_flavorv[child_flavor - 1]);
25918860SMatthew.Jacob@Sun.COM 	}
25928860SMatthew.Jacob@Sun.COM }
2593