xref: /illumos-gate/usr/src/uts/common/os/sunndi.c (revision c6f039c7)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
519174f18Svikram  * Common Development and Distribution License (the "License").
619174f18Svikram  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22255a2d50SMatthew Jacob  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <sys/types.h>
277c478bd9Sstevel@tonic-gate #include <sys/param.h>
287c478bd9Sstevel@tonic-gate #include <sys/systm.h>
297c478bd9Sstevel@tonic-gate #include <sys/buf.h>
307c478bd9Sstevel@tonic-gate #include <sys/uio.h>
317c478bd9Sstevel@tonic-gate #include <sys/cred.h>
327c478bd9Sstevel@tonic-gate #include <sys/poll.h>
337c478bd9Sstevel@tonic-gate #include <sys/mman.h>
347c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
357c478bd9Sstevel@tonic-gate #include <sys/model.h>
367c478bd9Sstevel@tonic-gate #include <sys/file.h>
377c478bd9Sstevel@tonic-gate #include <sys/proc.h>
387c478bd9Sstevel@tonic-gate #include <sys/open.h>
397c478bd9Sstevel@tonic-gate #include <sys/user.h>
407c478bd9Sstevel@tonic-gate #include <sys/t_lock.h>
417c478bd9Sstevel@tonic-gate #include <sys/vm.h>
427c478bd9Sstevel@tonic-gate #include <sys/stat.h>
437c478bd9Sstevel@tonic-gate #include <vm/hat.h>
447c478bd9Sstevel@tonic-gate #include <vm/seg.h>
457c478bd9Sstevel@tonic-gate #include <vm/as.h>
467c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
477c478bd9Sstevel@tonic-gate #include <sys/debug.h>
487c478bd9Sstevel@tonic-gate #include <sys/avintr.h>
497c478bd9Sstevel@tonic-gate #include <sys/autoconf.h>
507c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
517c478bd9Sstevel@tonic-gate #include <sys/esunddi.h>
527c478bd9Sstevel@tonic-gate #include <sys/sunndi.h>
537c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
547c478bd9Sstevel@tonic-gate #include <sys/kstat.h>
557c478bd9Sstevel@tonic-gate #include <sys/conf.h>
567c478bd9Sstevel@tonic-gate #include <sys/ddi_impldefs.h>	/* include implementation structure defs */
577c478bd9Sstevel@tonic-gate #include <sys/ndi_impldefs.h>
587c478bd9Sstevel@tonic-gate #include <sys/hwconf.h>
597c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
607c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
617c478bd9Sstevel@tonic-gate #include <sys/epm.h>
627c478bd9Sstevel@tonic-gate #include <sys/devctl.h>
637c478bd9Sstevel@tonic-gate #include <sys/callb.h>
647c478bd9Sstevel@tonic-gate #include <sys/bootconf.h>
657c478bd9Sstevel@tonic-gate #include <sys/dacf_impl.h>
667c478bd9Sstevel@tonic-gate #include <sys/nvpair.h>
677c478bd9Sstevel@tonic-gate #include <sys/sunmdi.h>
687c478bd9Sstevel@tonic-gate #include <sys/fs/dv_node.h>
6925e8c5aaSvikram #include <sys/sunldi_impl.h>
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate #ifdef __sparc
727c478bd9Sstevel@tonic-gate #include <sys/archsystm.h>	/* getpil/setpil */
737c478bd9Sstevel@tonic-gate #include <sys/membar.h>		/* membar_sync */
747c478bd9Sstevel@tonic-gate #endif
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate /*
777c478bd9Sstevel@tonic-gate  * ndi property handling
787c478bd9Sstevel@tonic-gate  */
797c478bd9Sstevel@tonic-gate int
ndi_prop_update_int(dev_t match_dev,dev_info_t * dip,char * name,int data)807c478bd9Sstevel@tonic-gate ndi_prop_update_int(dev_t match_dev, dev_info_t *dip,
817c478bd9Sstevel@tonic-gate     char *name, int data)
827c478bd9Sstevel@tonic-gate {
837c478bd9Sstevel@tonic-gate 	return (ddi_prop_update_common(match_dev, dip,
847c478bd9Sstevel@tonic-gate 	    DDI_PROP_HW_DEF | DDI_PROP_TYPE_INT | DDI_PROP_DONTSLEEP,
857c478bd9Sstevel@tonic-gate 	    name, &data, 1, ddi_prop_fm_encode_ints));
867c478bd9Sstevel@tonic-gate }
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate int
ndi_prop_update_int64(dev_t match_dev,dev_info_t * dip,char * name,int64_t data)897c478bd9Sstevel@tonic-gate ndi_prop_update_int64(dev_t match_dev, dev_info_t *dip,
907c478bd9Sstevel@tonic-gate     char *name, int64_t data)
917c478bd9Sstevel@tonic-gate {
927c478bd9Sstevel@tonic-gate 	return (ddi_prop_update_common(match_dev, dip,
937c478bd9Sstevel@tonic-gate 	    DDI_PROP_HW_DEF | DDI_PROP_TYPE_INT64 | DDI_PROP_DONTSLEEP,
947c478bd9Sstevel@tonic-gate 	    name, &data, 1, ddi_prop_fm_encode_int64));
957c478bd9Sstevel@tonic-gate }
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate int
ndi_prop_create_boolean(dev_t match_dev,dev_info_t * dip,char * name)987c478bd9Sstevel@tonic-gate ndi_prop_create_boolean(dev_t match_dev, dev_info_t *dip,
997c478bd9Sstevel@tonic-gate     char *name)
1007c478bd9Sstevel@tonic-gate {
1017c478bd9Sstevel@tonic-gate 	return (ddi_prop_update_common(match_dev, dip,
1027c478bd9Sstevel@tonic-gate 	    DDI_PROP_HW_DEF | DDI_PROP_TYPE_ANY | DDI_PROP_DONTSLEEP,
1037c478bd9Sstevel@tonic-gate 	    name, NULL, 0, ddi_prop_fm_encode_bytes));
1047c478bd9Sstevel@tonic-gate }
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate int
ndi_prop_update_int_array(dev_t match_dev,dev_info_t * dip,char * name,int * data,uint_t nelements)1077c478bd9Sstevel@tonic-gate ndi_prop_update_int_array(dev_t match_dev, dev_info_t *dip,
1087c478bd9Sstevel@tonic-gate     char *name, int *data, uint_t nelements)
1097c478bd9Sstevel@tonic-gate {
1107c478bd9Sstevel@tonic-gate 	return (ddi_prop_update_common(match_dev, dip,
1117c478bd9Sstevel@tonic-gate 	    DDI_PROP_HW_DEF | DDI_PROP_TYPE_INT | DDI_PROP_DONTSLEEP,
1127c478bd9Sstevel@tonic-gate 	    name, data, nelements, ddi_prop_fm_encode_ints));
1137c478bd9Sstevel@tonic-gate }
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate int
ndi_prop_update_int64_array(dev_t match_dev,dev_info_t * dip,char * name,int64_t * data,uint_t nelements)1167c478bd9Sstevel@tonic-gate ndi_prop_update_int64_array(dev_t match_dev, dev_info_t *dip,
1177c478bd9Sstevel@tonic-gate     char *name, int64_t *data, uint_t nelements)
1187c478bd9Sstevel@tonic-gate {
1197c478bd9Sstevel@tonic-gate 	return (ddi_prop_update_common(match_dev, dip,
1207c478bd9Sstevel@tonic-gate 	    DDI_PROP_HW_DEF | DDI_PROP_TYPE_INT64 | DDI_PROP_DONTSLEEP,
1217c478bd9Sstevel@tonic-gate 	    name, data, nelements, ddi_prop_fm_encode_int64));
1227c478bd9Sstevel@tonic-gate }
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate int
ndi_prop_update_string(dev_t match_dev,dev_info_t * dip,char * name,char * data)1257c478bd9Sstevel@tonic-gate ndi_prop_update_string(dev_t match_dev, dev_info_t *dip,
1267c478bd9Sstevel@tonic-gate     char *name, char *data)
1277c478bd9Sstevel@tonic-gate {
1287c478bd9Sstevel@tonic-gate 	return (ddi_prop_update_common(match_dev, dip,
1297c478bd9Sstevel@tonic-gate 	    DDI_PROP_HW_DEF | DDI_PROP_TYPE_STRING | DDI_PROP_DONTSLEEP,
1307c478bd9Sstevel@tonic-gate 	    name, &data, 1, ddi_prop_fm_encode_string));
1317c478bd9Sstevel@tonic-gate }
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate int
ndi_prop_update_string_array(dev_t match_dev,dev_info_t * dip,char * name,char ** data,uint_t nelements)1347c478bd9Sstevel@tonic-gate ndi_prop_update_string_array(dev_t match_dev, dev_info_t *dip,
1357c478bd9Sstevel@tonic-gate     char *name, char **data, uint_t nelements)
1367c478bd9Sstevel@tonic-gate {
1377c478bd9Sstevel@tonic-gate 	return (ddi_prop_update_common(match_dev, dip,
1387c478bd9Sstevel@tonic-gate 	    DDI_PROP_HW_DEF | DDI_PROP_TYPE_STRING | DDI_PROP_DONTSLEEP,
1397c478bd9Sstevel@tonic-gate 	    name, data, nelements,
1407c478bd9Sstevel@tonic-gate 	    ddi_prop_fm_encode_strings));
1417c478bd9Sstevel@tonic-gate }
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate int
ndi_prop_update_byte_array(dev_t match_dev,dev_info_t * dip,char * name,uchar_t * data,uint_t nelements)1447c478bd9Sstevel@tonic-gate ndi_prop_update_byte_array(dev_t match_dev, dev_info_t *dip,
1457c478bd9Sstevel@tonic-gate     char *name, uchar_t *data, uint_t nelements)
1467c478bd9Sstevel@tonic-gate {
1477c478bd9Sstevel@tonic-gate 	if (nelements == 0)
1487c478bd9Sstevel@tonic-gate 		return (DDI_PROP_INVAL_ARG);
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 	return (ddi_prop_update_common(match_dev, dip,
1517c478bd9Sstevel@tonic-gate 	    DDI_PROP_HW_DEF | DDI_PROP_TYPE_BYTE | DDI_PROP_DONTSLEEP,
1527c478bd9Sstevel@tonic-gate 	    name, data, nelements, ddi_prop_fm_encode_bytes));
1537c478bd9Sstevel@tonic-gate }
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate int
ndi_prop_remove(dev_t dev,dev_info_t * dip,char * name)1567c478bd9Sstevel@tonic-gate ndi_prop_remove(dev_t dev, dev_info_t *dip, char *name)
1577c478bd9Sstevel@tonic-gate {
1587c478bd9Sstevel@tonic-gate 	return (ddi_prop_remove_common(dev, dip, name, DDI_PROP_HW_DEF));
1597c478bd9Sstevel@tonic-gate }
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate void
ndi_prop_remove_all(dev_info_t * dip)1627c478bd9Sstevel@tonic-gate ndi_prop_remove_all(dev_info_t *dip)
1637c478bd9Sstevel@tonic-gate {
164b9ccdc5aScth 	i_ddi_prop_dyn_parent_set(dip, NULL);
1657c478bd9Sstevel@tonic-gate 	ddi_prop_remove_all_common(dip, (int)DDI_PROP_HW_DEF);
1667c478bd9Sstevel@tonic-gate }
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate /*
1697c478bd9Sstevel@tonic-gate  * Post an event notification to nexus driver responsible for handling
1707c478bd9Sstevel@tonic-gate  * the event.  The responsible nexus is defined in the cookie passed in as
1717c478bd9Sstevel@tonic-gate  * the third parameter.
1727c478bd9Sstevel@tonic-gate  * The dip parameter is an artifact of an older implementation in which all
1737c478bd9Sstevel@tonic-gate  * requests to remove an eventcall would bubble up the tree.  Today, this
1747c478bd9Sstevel@tonic-gate  * parameter is ignored.
1757c478bd9Sstevel@tonic-gate  * Input Parameters:
176027021c7SChris Horne  *	dip	- Ignored.
177027021c7SChris Horne  *	rdip	- device driver posting the event
1787c478bd9Sstevel@tonic-gate  *	cookie	- valid ddi_eventcookie_t, obtained by caller prior to
1797c478bd9Sstevel@tonic-gate  *		  invocation of this routine
1807c478bd9Sstevel@tonic-gate  *	impl_data - used by framework
1817c478bd9Sstevel@tonic-gate  */
1827c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1837c478bd9Sstevel@tonic-gate int
ndi_post_event(dev_info_t * dip,dev_info_t * rdip,ddi_eventcookie_t cookie,void * impl_data)1847c478bd9Sstevel@tonic-gate ndi_post_event(dev_info_t *dip, dev_info_t *rdip,
1857c478bd9Sstevel@tonic-gate 		ddi_eventcookie_t cookie, void *impl_data)
1867c478bd9Sstevel@tonic-gate {
1877c478bd9Sstevel@tonic-gate 	dev_info_t *ddip;
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	ASSERT(cookie);
1907c478bd9Sstevel@tonic-gate 	ddip = NDI_EVENT_DDIP(cookie);
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	/*
1937c478bd9Sstevel@tonic-gate 	 * perform sanity checks.  These conditions should never be true.
1947c478bd9Sstevel@tonic-gate 	 */
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 	ASSERT(DEVI(ddip)->devi_ops->devo_bus_ops != NULL);
1977c478bd9Sstevel@tonic-gate 	ASSERT(DEVI(ddip)->devi_ops->devo_bus_ops->busops_rev >= BUSO_REV_6);
1987c478bd9Sstevel@tonic-gate 	ASSERT(DEVI(ddip)->devi_ops->devo_bus_ops->bus_post_event != NULL);
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 	/*
2017c478bd9Sstevel@tonic-gate 	 * post the event to the responsible ancestor
2027c478bd9Sstevel@tonic-gate 	 */
2037c478bd9Sstevel@tonic-gate 	return ((*(DEVI(ddip)->devi_ops->devo_bus_ops->bus_post_event))
204b9ccdc5aScth 	    (ddip, rdip, cookie, impl_data));
2057c478bd9Sstevel@tonic-gate }
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate /*
2087c478bd9Sstevel@tonic-gate  * Calls the bus nexus driver's implementation of the
2097c478bd9Sstevel@tonic-gate  * (*bus_remove_eventcall)() interface.
2107c478bd9Sstevel@tonic-gate  */
2117c478bd9Sstevel@tonic-gate int
ndi_busop_remove_eventcall(dev_info_t * ddip,ddi_callback_id_t id)2127c478bd9Sstevel@tonic-gate ndi_busop_remove_eventcall(dev_info_t *ddip, ddi_callback_id_t id)
2137c478bd9Sstevel@tonic-gate {
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	ASSERT(id);
2167c478bd9Sstevel@tonic-gate 	/* check for a correct revno before calling up the device tree. */
2177c478bd9Sstevel@tonic-gate 	ASSERT(DEVI(ddip)->devi_ops->devo_bus_ops != NULL);
2187c478bd9Sstevel@tonic-gate 	ASSERT(DEVI(ddip)->devi_ops->devo_bus_ops->busops_rev >= BUSO_REV_6);
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 	if (DEVI(ddip)->devi_ops->devo_bus_ops->bus_remove_eventcall == NULL)
2217c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 	/*
2247c478bd9Sstevel@tonic-gate 	 * request responsible nexus to remove the eventcall
2257c478bd9Sstevel@tonic-gate 	 */
2267c478bd9Sstevel@tonic-gate 	return ((*(DEVI(ddip)->devi_ops->devo_bus_ops->bus_remove_eventcall))
227b9ccdc5aScth 	    (ddip, id));
2287c478bd9Sstevel@tonic-gate }
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate /*
2317c478bd9Sstevel@tonic-gate  * Calls the bus nexus driver's implementation of the
2327c478bd9Sstevel@tonic-gate  * (*bus_add_eventcall)() interface.  The dip parameter is an
2337c478bd9Sstevel@tonic-gate  * artifact of an older implementation in which all requests to
2347c478bd9Sstevel@tonic-gate  * add an eventcall would bubble up the tree.  Today, this parameter is
2357c478bd9Sstevel@tonic-gate  * ignored.
2367c478bd9Sstevel@tonic-gate  */
2377c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2387c478bd9Sstevel@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)2397c478bd9Sstevel@tonic-gate ndi_busop_add_eventcall(dev_info_t *dip, dev_info_t *rdip,
2407c478bd9Sstevel@tonic-gate 		ddi_eventcookie_t cookie, void (*callback)(), void *arg,
2417c478bd9Sstevel@tonic-gate 		ddi_callback_id_t *cb_id)
2427c478bd9Sstevel@tonic-gate {
2437c478bd9Sstevel@tonic-gate 	dev_info_t *ddip = (dev_info_t *)NDI_EVENT_DDIP(cookie);
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	/*
2467c478bd9Sstevel@tonic-gate 	 * check for a correct revno before calling up the device tree.
2477c478bd9Sstevel@tonic-gate 	 */
2487c478bd9Sstevel@tonic-gate 	ASSERT(DEVI(ddip)->devi_ops->devo_bus_ops != NULL);
2497c478bd9Sstevel@tonic-gate 	ASSERT(DEVI(ddip)->devi_ops->devo_bus_ops->busops_rev >= BUSO_REV_6);
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 	if (DEVI(ddip)->devi_ops->devo_bus_ops->bus_add_eventcall == NULL)
2527c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 	/*
2557c478bd9Sstevel@tonic-gate 	 * request responsible ancestor to add the eventcall
2567c478bd9Sstevel@tonic-gate 	 */
2577c478bd9Sstevel@tonic-gate 	return ((*(DEVI(ddip)->devi_ops->devo_bus_ops->bus_add_eventcall))
258b9ccdc5aScth 	    (ddip, rdip, cookie, callback, arg, cb_id));
2597c478bd9Sstevel@tonic-gate }
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate /*
2627c478bd9Sstevel@tonic-gate  * Calls the bus nexus driver's implementation of the
2637c478bd9Sstevel@tonic-gate  * (*bus_get_eventcookie)() interface up the device tree hierarchy.
2647c478bd9Sstevel@tonic-gate  */
2657c478bd9Sstevel@tonic-gate int
ndi_busop_get_eventcookie(dev_info_t * dip,dev_info_t * rdip,char * name,ddi_eventcookie_t * event_cookiep)2667c478bd9Sstevel@tonic-gate ndi_busop_get_eventcookie(dev_info_t *dip, dev_info_t *rdip, char *name,
2677c478bd9Sstevel@tonic-gate 		ddi_eventcookie_t *event_cookiep)
2687c478bd9Sstevel@tonic-gate {
2697c478bd9Sstevel@tonic-gate 	dev_info_t *pdip = (dev_info_t *)DEVI(dip)->devi_parent;
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 	/* Can not be called from rootnex. */
2727c478bd9Sstevel@tonic-gate 	ASSERT(pdip);
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 	/*
2757c478bd9Sstevel@tonic-gate 	 * check for a correct revno before calling up the device tree.
2767c478bd9Sstevel@tonic-gate 	 */
2777c478bd9Sstevel@tonic-gate 	ASSERT(DEVI(pdip)->devi_ops->devo_bus_ops != NULL);
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 	if ((DEVI(pdip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_6) ||
2807c478bd9Sstevel@tonic-gate 	    (DEVI(pdip)->devi_ops->devo_bus_ops->bus_get_eventcookie == NULL)) {
2817c478bd9Sstevel@tonic-gate #ifdef DEBUG
2827c478bd9Sstevel@tonic-gate 		if ((DEVI(pdip)->devi_ops->devo_bus_ops->busops_rev >=
2837c478bd9Sstevel@tonic-gate 		    BUSO_REV_3) &&
2847c478bd9Sstevel@tonic-gate 		    (DEVI(pdip)->devi_ops->devo_bus_ops->bus_get_eventcookie)) {
2857c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN,
2867c478bd9Sstevel@tonic-gate 			    "Warning: %s%d busops_rev=%d no longer supported"
2877c478bd9Sstevel@tonic-gate 			    " by the NDI event framework.\nBUSO_REV_6 or "
2887c478bd9Sstevel@tonic-gate 			    "greater must be used.",
2897c478bd9Sstevel@tonic-gate 			    DEVI(pdip)->devi_binding_name,
2907c478bd9Sstevel@tonic-gate 			    DEVI(pdip)->devi_instance,
2917c478bd9Sstevel@tonic-gate 			    DEVI(pdip)->devi_ops->devo_bus_ops->busops_rev);
2927c478bd9Sstevel@tonic-gate 		}
2937c478bd9Sstevel@tonic-gate #endif /* DEBUG */
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 		return (ndi_busop_get_eventcookie(pdip, rdip, name,
296b9ccdc5aScth 		    event_cookiep));
2977c478bd9Sstevel@tonic-gate 	}
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 	return ((*(DEVI(pdip)->devi_ops->devo_bus_ops->bus_get_eventcookie))
300b9ccdc5aScth 	    (pdip, rdip, name, event_cookiep));
3017c478bd9Sstevel@tonic-gate }
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate /*
3047c478bd9Sstevel@tonic-gate  * Copy in the devctl IOCTL data and return a handle to
3057c478bd9Sstevel@tonic-gate  * the data.
3067c478bd9Sstevel@tonic-gate  */
3077c478bd9Sstevel@tonic-gate int
ndi_dc_allochdl(void * iocarg,struct devctl_iocdata ** rdcp)3087c478bd9Sstevel@tonic-gate ndi_dc_allochdl(void *iocarg, struct devctl_iocdata **rdcp)
3097c478bd9Sstevel@tonic-gate {
3107c478bd9Sstevel@tonic-gate 	struct devctl_iocdata *dcp;
3117c478bd9Sstevel@tonic-gate 	char *cpybuf;
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 	ASSERT(rdcp != NULL);
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 	dcp = kmem_zalloc(sizeof (*dcp), KM_SLEEP);
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 	if (get_udatamodel() == DATAMODEL_NATIVE) {
3187c478bd9Sstevel@tonic-gate 		if (copyin(iocarg, dcp, sizeof (*dcp)) != 0) {
3197c478bd9Sstevel@tonic-gate 			kmem_free(dcp, sizeof (*dcp));
3207c478bd9Sstevel@tonic-gate 			return (NDI_FAULT);
3217c478bd9Sstevel@tonic-gate 		}
3227c478bd9Sstevel@tonic-gate 	}
3237c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
3247c478bd9Sstevel@tonic-gate 	else {
3257c478bd9Sstevel@tonic-gate 		struct devctl_iocdata32 dcp32;
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 		if (copyin(iocarg, &dcp32, sizeof (dcp32)) != 0) {
3287c478bd9Sstevel@tonic-gate 			kmem_free(dcp, sizeof (*dcp));
3297c478bd9Sstevel@tonic-gate 			return (NDI_FAULT);
3307c478bd9Sstevel@tonic-gate 		}
3317c478bd9Sstevel@tonic-gate 		dcp->cmd = (uint_t)dcp32.cmd;
3327c478bd9Sstevel@tonic-gate 		dcp->flags = (uint_t)dcp32.flags;
3337c478bd9Sstevel@tonic-gate 		dcp->cpyout_buf = (uint_t *)(uintptr_t)dcp32.cpyout_buf;
3347c478bd9Sstevel@tonic-gate 		dcp->nvl_user = (nvlist_t *)(uintptr_t)dcp32.nvl_user;
3357c478bd9Sstevel@tonic-gate 		dcp->nvl_usersz = (size_t)dcp32.nvl_usersz;
3367c478bd9Sstevel@tonic-gate 		dcp->c_nodename = (char *)(uintptr_t)dcp32.c_nodename;
3377c478bd9Sstevel@tonic-gate 		dcp->c_unitaddr = (char *)(uintptr_t)dcp32.c_unitaddr;
3387c478bd9Sstevel@tonic-gate 	}
3397c478bd9Sstevel@tonic-gate #endif
3407c478bd9Sstevel@tonic-gate 	if (dcp->c_nodename != NULL) {
3417c478bd9Sstevel@tonic-gate 		cpybuf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
3427c478bd9Sstevel@tonic-gate 		if (copyinstr(dcp->c_nodename, cpybuf, MAXNAMELEN, 0) != 0) {
3437c478bd9Sstevel@tonic-gate 			kmem_free(cpybuf, MAXNAMELEN);
3447c478bd9Sstevel@tonic-gate 			kmem_free(dcp, sizeof (*dcp));
3457c478bd9Sstevel@tonic-gate 			return (NDI_FAULT);
3467c478bd9Sstevel@tonic-gate 		}
3477c478bd9Sstevel@tonic-gate 		cpybuf[MAXNAMELEN - 1] = '\0';
3487c478bd9Sstevel@tonic-gate 		dcp->c_nodename = cpybuf;
3497c478bd9Sstevel@tonic-gate 	}
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 	if (dcp->c_unitaddr != NULL) {
3527c478bd9Sstevel@tonic-gate 		cpybuf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
3537c478bd9Sstevel@tonic-gate 		if (copyinstr(dcp->c_unitaddr, cpybuf, MAXNAMELEN, 0) != 0) {
3547c478bd9Sstevel@tonic-gate 			kmem_free(cpybuf, MAXNAMELEN);
3557c478bd9Sstevel@tonic-gate 			if (dcp->c_nodename != NULL)
3567c478bd9Sstevel@tonic-gate 				kmem_free(dcp->c_nodename, MAXNAMELEN);
3577c478bd9Sstevel@tonic-gate 			kmem_free(dcp, sizeof (*dcp));
3587c478bd9Sstevel@tonic-gate 			return (NDI_FAULT);
3597c478bd9Sstevel@tonic-gate 		}
3607c478bd9Sstevel@tonic-gate 		cpybuf[MAXNAMELEN - 1] = '\0';
3617c478bd9Sstevel@tonic-gate 		dcp->c_unitaddr = cpybuf;
3627c478bd9Sstevel@tonic-gate 	}
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 	/*
3657c478bd9Sstevel@tonic-gate 	 * copyin and unpack a user defined nvlist if one was passed
3667c478bd9Sstevel@tonic-gate 	 */
3677c478bd9Sstevel@tonic-gate 	if (dcp->nvl_user != NULL) {
3685e3f6b1fScth 		if ((dcp->nvl_usersz == 0) ||
3695e3f6b1fScth 		    (dcp->nvl_usersz > DEVCTL_MAX_NVL_USERSZ)) {
37092d20396Scth 			if (dcp->c_nodename != NULL)
37192d20396Scth 				kmem_free(dcp->c_nodename, MAXNAMELEN);
37292d20396Scth 			if (dcp->c_unitaddr != NULL)
37392d20396Scth 				kmem_free(dcp->c_unitaddr, MAXNAMELEN);
37492d20396Scth 			kmem_free(dcp, sizeof (*dcp));
37592d20396Scth 			return (NDI_FAILURE);
37692d20396Scth 		}
3777c478bd9Sstevel@tonic-gate 		cpybuf = kmem_alloc(dcp->nvl_usersz, KM_SLEEP);
3787c478bd9Sstevel@tonic-gate 		if (copyin(dcp->nvl_user, cpybuf, dcp->nvl_usersz) != 0) {
3797c478bd9Sstevel@tonic-gate 			kmem_free(cpybuf, dcp->nvl_usersz);
3807c478bd9Sstevel@tonic-gate 			if (dcp->c_nodename != NULL)
3817c478bd9Sstevel@tonic-gate 				kmem_free(dcp->c_nodename, MAXNAMELEN);
3827c478bd9Sstevel@tonic-gate 			if (dcp->c_unitaddr != NULL)
3837c478bd9Sstevel@tonic-gate 				kmem_free(dcp->c_unitaddr, MAXNAMELEN);
3847c478bd9Sstevel@tonic-gate 			kmem_free(dcp, sizeof (*dcp));
3857c478bd9Sstevel@tonic-gate 			return (NDI_FAULT);
3867c478bd9Sstevel@tonic-gate 		}
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate 		if (nvlist_unpack(cpybuf, dcp->nvl_usersz, &dcp->nvl_user,
3897c478bd9Sstevel@tonic-gate 		    KM_SLEEP)) {
3907c478bd9Sstevel@tonic-gate 			kmem_free(cpybuf, dcp->nvl_usersz);
3917c478bd9Sstevel@tonic-gate 			if (dcp->c_nodename != NULL)
3927c478bd9Sstevel@tonic-gate 				kmem_free(dcp->c_nodename, MAXNAMELEN);
3937c478bd9Sstevel@tonic-gate 			if (dcp->c_unitaddr != NULL)
3947c478bd9Sstevel@tonic-gate 				kmem_free(dcp->c_unitaddr, MAXNAMELEN);
3957c478bd9Sstevel@tonic-gate 			kmem_free(dcp, sizeof (*dcp));
3967c478bd9Sstevel@tonic-gate 			return (NDI_FAULT);
3977c478bd9Sstevel@tonic-gate 		}
3987c478bd9Sstevel@tonic-gate 		/*
3997c478bd9Sstevel@tonic-gate 		 * free the buffer containing the packed nvlist
4007c478bd9Sstevel@tonic-gate 		 */
4017c478bd9Sstevel@tonic-gate 		kmem_free(cpybuf, dcp->nvl_usersz);
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 	}
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 	*rdcp = dcp;
4067c478bd9Sstevel@tonic-gate 	return (NDI_SUCCESS);
4077c478bd9Sstevel@tonic-gate }
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate /*
4107c478bd9Sstevel@tonic-gate  * free all space allocated to a handle.
4117c478bd9Sstevel@tonic-gate  */
4127c478bd9Sstevel@tonic-gate void
ndi_dc_freehdl(struct devctl_iocdata * dcp)4137c478bd9Sstevel@tonic-gate ndi_dc_freehdl(struct devctl_iocdata *dcp)
4147c478bd9Sstevel@tonic-gate {
4157c478bd9Sstevel@tonic-gate 	ASSERT(dcp != NULL);
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 	if (dcp->c_nodename != NULL)
4187c478bd9Sstevel@tonic-gate 		kmem_free(dcp->c_nodename, MAXNAMELEN);
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate 	if (dcp->c_unitaddr != NULL)
4217c478bd9Sstevel@tonic-gate 		kmem_free(dcp->c_unitaddr, MAXNAMELEN);
4227c478bd9Sstevel@tonic-gate 
423aab83bb8SJosef 'Jeff' Sipek 	nvlist_free(dcp->nvl_user);
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 	kmem_free(dcp, sizeof (*dcp));
4267c478bd9Sstevel@tonic-gate }
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate char *
ndi_dc_getname(struct devctl_iocdata * dcp)4297c478bd9Sstevel@tonic-gate ndi_dc_getname(struct devctl_iocdata *dcp)
4307c478bd9Sstevel@tonic-gate {
4317c478bd9Sstevel@tonic-gate 	ASSERT(dcp != NULL);
4327c478bd9Sstevel@tonic-gate 	return (dcp->c_nodename);
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate }
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate char *
ndi_dc_getaddr(struct devctl_iocdata * dcp)4377c478bd9Sstevel@tonic-gate ndi_dc_getaddr(struct devctl_iocdata *dcp)
4387c478bd9Sstevel@tonic-gate {
4397c478bd9Sstevel@tonic-gate 	ASSERT(dcp != NULL);
4407c478bd9Sstevel@tonic-gate 	return (dcp->c_unitaddr);
4417c478bd9Sstevel@tonic-gate }
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate nvlist_t *
ndi_dc_get_ap_data(struct devctl_iocdata * dcp)4447c478bd9Sstevel@tonic-gate ndi_dc_get_ap_data(struct devctl_iocdata *dcp)
4457c478bd9Sstevel@tonic-gate {
4467c478bd9Sstevel@tonic-gate 	ASSERT(dcp != NULL);
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 	return (dcp->nvl_user);
4497c478bd9Sstevel@tonic-gate }
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate /*
4527c478bd9Sstevel@tonic-gate  * Transition the child named by "devname@devaddr" to the online state.
4537c478bd9Sstevel@tonic-gate  * For use by a driver's DEVCTL_DEVICE_ONLINE handler.
4547c478bd9Sstevel@tonic-gate  */
4557c478bd9Sstevel@tonic-gate int
ndi_devctl_device_online(dev_info_t * dip,struct devctl_iocdata * dcp,uint_t flags)4567c478bd9Sstevel@tonic-gate ndi_devctl_device_online(dev_info_t *dip, struct devctl_iocdata *dcp,
4577c478bd9Sstevel@tonic-gate 	uint_t flags)
4587c478bd9Sstevel@tonic-gate {
4597c478bd9Sstevel@tonic-gate 	int	rval;
4607c478bd9Sstevel@tonic-gate 	char	*name;
4617c478bd9Sstevel@tonic-gate 	dev_info_t *rdip;
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 	if (ndi_dc_getname(dcp) == NULL || ndi_dc_getaddr(dcp) == NULL)
4647c478bd9Sstevel@tonic-gate 		return (EINVAL);
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 	name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
4677c478bd9Sstevel@tonic-gate 	(void) snprintf(name, MAXNAMELEN, "%s@%s",
468b9ccdc5aScth 	    ndi_dc_getname(dcp), ndi_dc_getaddr(dcp));
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 	if ((rval = ndi_devi_config_one(dip, name, &rdip,
4717c478bd9Sstevel@tonic-gate 	    flags | NDI_DEVI_ONLINE | NDI_CONFIG)) == NDI_SUCCESS) {
4727c478bd9Sstevel@tonic-gate 		ndi_rele_devi(rdip);
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate 		/*
4757c478bd9Sstevel@tonic-gate 		 * Invalidate devfs cached directory contents. For the checks
4767c478bd9Sstevel@tonic-gate 		 * in the "if" condition see the comment in ndi_devi_online().
4777c478bd9Sstevel@tonic-gate 		 */
478737d277aScth 		if (i_ddi_devi_attached(dip) && !DEVI_BUSY_OWNED(dip))
4797c478bd9Sstevel@tonic-gate 			(void) devfs_clean(dip, NULL, 0);
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 	} else if (rval == NDI_BUSY) {
4827c478bd9Sstevel@tonic-gate 		rval = EBUSY;
4837c478bd9Sstevel@tonic-gate 	} else if (rval == NDI_FAILURE) {
4847c478bd9Sstevel@tonic-gate 		rval = EIO;
4857c478bd9Sstevel@tonic-gate 	}
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate 	NDI_DEBUG(flags, (CE_CONT, "%s%d: online: %s: %s\n",
488b9ccdc5aScth 	    ddi_driver_name(dip), ddi_get_instance(dip), name,
489b9ccdc5aScth 	    ((rval == NDI_SUCCESS) ? "ok" : "failed")));
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate 	kmem_free(name, MAXNAMELEN);
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate 	return (rval);
4947c478bd9Sstevel@tonic-gate }
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate /*
4977c478bd9Sstevel@tonic-gate  * Transition the child named by "devname@devaddr" to the offline state.
4987c478bd9Sstevel@tonic-gate  * For use by a driver's DEVCTL_DEVICE_OFFLINE handler.
4997c478bd9Sstevel@tonic-gate  */
5007c478bd9Sstevel@tonic-gate int
ndi_devctl_device_offline(dev_info_t * dip,struct devctl_iocdata * dcp,uint_t flags)5017c478bd9Sstevel@tonic-gate ndi_devctl_device_offline(dev_info_t *dip, struct devctl_iocdata *dcp,
5027c478bd9Sstevel@tonic-gate 	uint_t flags)
5037c478bd9Sstevel@tonic-gate {
5047c478bd9Sstevel@tonic-gate 	int	rval;
5057c478bd9Sstevel@tonic-gate 	char	*name;
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate 	if (ndi_dc_getname(dcp) == NULL || ndi_dc_getaddr(dcp) == NULL)
5087c478bd9Sstevel@tonic-gate 		return (EINVAL);
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate 	name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
5117c478bd9Sstevel@tonic-gate 	(void) snprintf(name, MAXNAMELEN, "%s@%s",
512b9ccdc5aScth 	    ndi_dc_getname(dcp), ndi_dc_getaddr(dcp));
5137c478bd9Sstevel@tonic-gate 
51419174f18Svikram 	(void) devfs_clean(dip, name, DV_CLEAN_FORCE);
51519174f18Svikram 	rval = ndi_devi_unconfig_one(dip, name, NULL,
51619174f18Svikram 	    flags | NDI_DEVI_OFFLINE);
51719174f18Svikram 
51819174f18Svikram 	if (rval == NDI_BUSY) {
5197c478bd9Sstevel@tonic-gate 		rval = EBUSY;
52019174f18Svikram 	} else if (rval == NDI_FAILURE) {
52119174f18Svikram 		rval = EIO;
5227c478bd9Sstevel@tonic-gate 	}
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate 	NDI_DEBUG(flags, (CE_CONT, "%s%d: offline: %s: %s\n",
525b9ccdc5aScth 	    ddi_driver_name(dip), ddi_get_instance(dip), name,
526b9ccdc5aScth 	    (rval == NDI_SUCCESS) ? "ok" : "failed"));
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate 	kmem_free(name, MAXNAMELEN);
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate 	return (rval);
5317c478bd9Sstevel@tonic-gate }
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate /*
5347c478bd9Sstevel@tonic-gate  * Remove the child named by "devname@devaddr".
5357c478bd9Sstevel@tonic-gate  * For use by a driver's DEVCTL_DEVICE_REMOVE handler.
5367c478bd9Sstevel@tonic-gate  */
5377c478bd9Sstevel@tonic-gate int
ndi_devctl_device_remove(dev_info_t * dip,struct devctl_iocdata * dcp,uint_t flags)5387c478bd9Sstevel@tonic-gate ndi_devctl_device_remove(dev_info_t *dip, struct devctl_iocdata *dcp,
5397c478bd9Sstevel@tonic-gate 	uint_t flags)
5407c478bd9Sstevel@tonic-gate {
5417c478bd9Sstevel@tonic-gate 	int	rval;
5427c478bd9Sstevel@tonic-gate 	char	*name;
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate 	if (ndi_dc_getname(dcp) == NULL || ndi_dc_getaddr(dcp) == NULL)
5457c478bd9Sstevel@tonic-gate 		return (EINVAL);
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 	name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
5487c478bd9Sstevel@tonic-gate 	(void) snprintf(name, MAXNAMELEN, "%s@%s",
549b9ccdc5aScth 	    ndi_dc_getname(dcp), ndi_dc_getaddr(dcp));
5507c478bd9Sstevel@tonic-gate 
5517c478bd9Sstevel@tonic-gate 	(void) devfs_clean(dip, name, DV_CLEAN_FORCE);
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate 	rval = ndi_devi_unconfig_one(dip, name, NULL, flags | NDI_DEVI_REMOVE);
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate 	if (rval == NDI_BUSY) {
5567c478bd9Sstevel@tonic-gate 		rval = EBUSY;
5577c478bd9Sstevel@tonic-gate 	} else if (rval == NDI_FAILURE) {
5587c478bd9Sstevel@tonic-gate 		rval = EIO;
5597c478bd9Sstevel@tonic-gate 	}
5607c478bd9Sstevel@tonic-gate 
5617c478bd9Sstevel@tonic-gate 	NDI_DEBUG(flags, (CE_CONT, "%s%d: remove: %s: %s\n",
562b9ccdc5aScth 	    ddi_driver_name(dip), ddi_get_instance(dip), name,
563b9ccdc5aScth 	    (rval == NDI_SUCCESS) ? "ok" : "failed"));
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate 	kmem_free(name, MAXNAMELEN);
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate 	return (rval);
5687c478bd9Sstevel@tonic-gate }
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate /*
5717c478bd9Sstevel@tonic-gate  * Return devctl state of the child named by "name@addr".
5727c478bd9Sstevel@tonic-gate  * For use by a driver's DEVCTL_DEVICE_GETSTATE handler.
5737c478bd9Sstevel@tonic-gate  */
5747c478bd9Sstevel@tonic-gate int
ndi_devctl_device_getstate(dev_info_t * parent,struct devctl_iocdata * dcp,uint_t * state)5757c478bd9Sstevel@tonic-gate ndi_devctl_device_getstate(dev_info_t *parent, struct devctl_iocdata *dcp,
5767c478bd9Sstevel@tonic-gate 	uint_t *state)
5777c478bd9Sstevel@tonic-gate {
5787c478bd9Sstevel@tonic-gate 	dev_info_t *dip;
5797c478bd9Sstevel@tonic-gate 	char *name, *addr;
5807c478bd9Sstevel@tonic-gate 	char *devname;
5817c478bd9Sstevel@tonic-gate 	int devnamelen;
5827c478bd9Sstevel@tonic-gate 	int circ;
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate 	if (parent == NULL ||
5857c478bd9Sstevel@tonic-gate 	    ((name = ndi_dc_getname(dcp)) == NULL) ||
5867c478bd9Sstevel@tonic-gate 	    ((addr = ndi_dc_getaddr(dcp)) == NULL))
5877c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 	devnamelen = strlen(name) + strlen(addr) + 2;
5907c478bd9Sstevel@tonic-gate 	devname = kmem_alloc(devnamelen, KM_SLEEP);
5917c478bd9Sstevel@tonic-gate 	if (strlen(addr) > 0) {
5927c478bd9Sstevel@tonic-gate 		(void) snprintf(devname, devnamelen, "%s@%s", name, addr);
5937c478bd9Sstevel@tonic-gate 	} else {
5947c478bd9Sstevel@tonic-gate 		(void) snprintf(devname, devnamelen, "%s", name);
5957c478bd9Sstevel@tonic-gate 	}
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate 	ndi_devi_enter(parent, &circ);
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate 	dip = ndi_devi_findchild(parent, devname);
6007c478bd9Sstevel@tonic-gate 	kmem_free(devname, devnamelen);
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate 	if (dip == NULL) {
6037c478bd9Sstevel@tonic-gate 		ndi_devi_exit(parent, circ);
6047c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
6057c478bd9Sstevel@tonic-gate 	}
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate 	mutex_enter(&(DEVI(dip)->devi_lock));
6087c478bd9Sstevel@tonic-gate 	if (DEVI_IS_DEVICE_OFFLINE(dip)) {
6097c478bd9Sstevel@tonic-gate 		*state = DEVICE_OFFLINE;
6107c478bd9Sstevel@tonic-gate 	} else if (DEVI_IS_DEVICE_DOWN(dip)) {
6117c478bd9Sstevel@tonic-gate 		*state = DEVICE_DOWN;
6127c478bd9Sstevel@tonic-gate 	} else {
6137c478bd9Sstevel@tonic-gate 		*state = DEVICE_ONLINE;
6147c478bd9Sstevel@tonic-gate 		if (devi_stillreferenced(dip) == DEVI_REFERENCED)
6157c478bd9Sstevel@tonic-gate 			*state |= DEVICE_BUSY;
6167c478bd9Sstevel@tonic-gate 	}
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate 	mutex_exit(&(DEVI(dip)->devi_lock));
6197c478bd9Sstevel@tonic-gate 	ndi_devi_exit(parent, circ);
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate 	return (NDI_SUCCESS);
6227c478bd9Sstevel@tonic-gate }
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate /*
6257c478bd9Sstevel@tonic-gate  * return the current state of the device "dip"
6267c478bd9Sstevel@tonic-gate  *
6277c478bd9Sstevel@tonic-gate  * recommend using ndi_devctl_ioctl() or
6287c478bd9Sstevel@tonic-gate  * ndi_devctl_device_getstate() instead
6297c478bd9Sstevel@tonic-gate  */
6307c478bd9Sstevel@tonic-gate int
ndi_dc_return_dev_state(dev_info_t * dip,struct devctl_iocdata * dcp)6317c478bd9Sstevel@tonic-gate ndi_dc_return_dev_state(dev_info_t *dip, struct devctl_iocdata *dcp)
6327c478bd9Sstevel@tonic-gate {
6337c478bd9Sstevel@tonic-gate 	dev_info_t *pdip;
6347c478bd9Sstevel@tonic-gate 	uint_t devstate = 0;
6357c478bd9Sstevel@tonic-gate 	int circ;
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate 	if ((dip == NULL) || (dcp == NULL))
6387c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate 	pdip = ddi_get_parent(dip);
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate 	ndi_devi_enter(pdip, &circ);
6437c478bd9Sstevel@tonic-gate 	mutex_enter(&(DEVI(dip)->devi_lock));
6447c478bd9Sstevel@tonic-gate 	if (DEVI_IS_DEVICE_OFFLINE(dip)) {
6457c478bd9Sstevel@tonic-gate 		devstate = DEVICE_OFFLINE;
6467c478bd9Sstevel@tonic-gate 	} else if (DEVI_IS_DEVICE_DOWN(dip)) {
6477c478bd9Sstevel@tonic-gate 		devstate = DEVICE_DOWN;
6487c478bd9Sstevel@tonic-gate 	} else {
6497c478bd9Sstevel@tonic-gate 		devstate = DEVICE_ONLINE;
6507c478bd9Sstevel@tonic-gate 		if (devi_stillreferenced(dip) == DEVI_REFERENCED)
6517c478bd9Sstevel@tonic-gate 			devstate |= DEVICE_BUSY;
6527c478bd9Sstevel@tonic-gate 	}
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate 	mutex_exit(&(DEVI(dip)->devi_lock));
6557c478bd9Sstevel@tonic-gate 	ndi_devi_exit(pdip, circ);
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate 	if (copyout(&devstate, dcp->cpyout_buf, sizeof (uint_t)) != 0)
6587c478bd9Sstevel@tonic-gate 		return (NDI_FAULT);
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 	return (NDI_SUCCESS);
6617c478bd9Sstevel@tonic-gate }
6627c478bd9Sstevel@tonic-gate 
6637c478bd9Sstevel@tonic-gate /*
6647c478bd9Sstevel@tonic-gate  * Return device's bus state
6657c478bd9Sstevel@tonic-gate  * For use by a driver's DEVCTL_BUS_GETSTATE handler.
6667c478bd9Sstevel@tonic-gate  */
6677c478bd9Sstevel@tonic-gate int
ndi_devctl_bus_getstate(dev_info_t * dip,struct devctl_iocdata * dcp,uint_t * state)6687c478bd9Sstevel@tonic-gate ndi_devctl_bus_getstate(dev_info_t *dip, struct devctl_iocdata *dcp,
6697c478bd9Sstevel@tonic-gate 	uint_t *state)
6707c478bd9Sstevel@tonic-gate {
6717c478bd9Sstevel@tonic-gate 	if ((dip == NULL) || (dcp == NULL))
6727c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
6737c478bd9Sstevel@tonic-gate 
6747c478bd9Sstevel@tonic-gate 	return (ndi_get_bus_state(dip, state));
6757c478bd9Sstevel@tonic-gate }
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate /*
6787c478bd9Sstevel@tonic-gate  * Generic devctl ioctl handler
6797c478bd9Sstevel@tonic-gate  */
6807c478bd9Sstevel@tonic-gate int
ndi_devctl_ioctl(dev_info_t * dip,int cmd,intptr_t arg,int mode,uint_t flags)6817c478bd9Sstevel@tonic-gate ndi_devctl_ioctl(dev_info_t *dip, int cmd, intptr_t arg, int mode, uint_t flags)
6827c478bd9Sstevel@tonic-gate {
6837c478bd9Sstevel@tonic-gate 	_NOTE(ARGUNUSED(mode))
6847c478bd9Sstevel@tonic-gate 	struct devctl_iocdata *dcp;
6857c478bd9Sstevel@tonic-gate 	uint_t state;
6867c478bd9Sstevel@tonic-gate 	int rval = ENOTTY;
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate 	/*
6897c478bd9Sstevel@tonic-gate 	 * read devctl ioctl data
6907c478bd9Sstevel@tonic-gate 	 */
6917c478bd9Sstevel@tonic-gate 	if (ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS)
6927c478bd9Sstevel@tonic-gate 		return (EFAULT);
6937c478bd9Sstevel@tonic-gate 
6947c478bd9Sstevel@tonic-gate 	switch (cmd) {
6957c478bd9Sstevel@tonic-gate 
6967c478bd9Sstevel@tonic-gate 	case DEVCTL_BUS_GETSTATE:
6977c478bd9Sstevel@tonic-gate 		rval = ndi_devctl_bus_getstate(dip, dcp, &state);
6987c478bd9Sstevel@tonic-gate 		if (rval == NDI_SUCCESS) {
6997c478bd9Sstevel@tonic-gate 			if (copyout(&state, dcp->cpyout_buf,
7007c478bd9Sstevel@tonic-gate 			    sizeof (uint_t)) != 0)
7017c478bd9Sstevel@tonic-gate 				rval = NDI_FAULT;
7027c478bd9Sstevel@tonic-gate 		}
7037c478bd9Sstevel@tonic-gate 		break;
7047c478bd9Sstevel@tonic-gate 
7057c478bd9Sstevel@tonic-gate 	case DEVCTL_DEVICE_ONLINE:
7067c478bd9Sstevel@tonic-gate 		rval = ndi_devctl_device_online(dip, dcp, flags);
7077c478bd9Sstevel@tonic-gate 		break;
7087c478bd9Sstevel@tonic-gate 
7097c478bd9Sstevel@tonic-gate 	case DEVCTL_DEVICE_OFFLINE:
7107c478bd9Sstevel@tonic-gate 		rval = ndi_devctl_device_offline(dip, dcp, flags);
7117c478bd9Sstevel@tonic-gate 		break;
7127c478bd9Sstevel@tonic-gate 
7137c478bd9Sstevel@tonic-gate 	case DEVCTL_DEVICE_GETSTATE:
7147c478bd9Sstevel@tonic-gate 		rval = ndi_devctl_device_getstate(dip, dcp, &state);
7157c478bd9Sstevel@tonic-gate 		if (rval == NDI_SUCCESS) {
7167c478bd9Sstevel@tonic-gate 			if (copyout(&state, dcp->cpyout_buf,
7177c478bd9Sstevel@tonic-gate 			    sizeof (uint_t)) != 0)
7187c478bd9Sstevel@tonic-gate 				rval = NDI_FAULT;
7197c478bd9Sstevel@tonic-gate 		}
7207c478bd9Sstevel@tonic-gate 		break;
7217c478bd9Sstevel@tonic-gate 
7227c478bd9Sstevel@tonic-gate 	case DEVCTL_DEVICE_REMOVE:
7237c478bd9Sstevel@tonic-gate 		rval = ndi_devctl_device_remove(dip, dcp, flags);
7247c478bd9Sstevel@tonic-gate 		break;
7257c478bd9Sstevel@tonic-gate 
7267c478bd9Sstevel@tonic-gate 	case DEVCTL_BUS_DEV_CREATE:
7277c478bd9Sstevel@tonic-gate 		rval = ndi_dc_devi_create(dcp, dip, 0, NULL);
7287c478bd9Sstevel@tonic-gate 		break;
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate 	/*
7317c478bd9Sstevel@tonic-gate 	 * ioctls for which a generic implementation makes no sense
7327c478bd9Sstevel@tonic-gate 	 */
7337c478bd9Sstevel@tonic-gate 	case DEVCTL_BUS_RESET:
7347c478bd9Sstevel@tonic-gate 	case DEVCTL_BUS_RESETALL:
7357c478bd9Sstevel@tonic-gate 	case DEVCTL_DEVICE_RESET:
7367c478bd9Sstevel@tonic-gate 	case DEVCTL_AP_CONNECT:
7377c478bd9Sstevel@tonic-gate 	case DEVCTL_AP_DISCONNECT:
7387c478bd9Sstevel@tonic-gate 	case DEVCTL_AP_INSERT:
7397c478bd9Sstevel@tonic-gate 	case DEVCTL_AP_REMOVE:
7407c478bd9Sstevel@tonic-gate 	case DEVCTL_AP_CONFIGURE:
7417c478bd9Sstevel@tonic-gate 	case DEVCTL_AP_UNCONFIGURE:
7427c478bd9Sstevel@tonic-gate 	case DEVCTL_AP_GETSTATE:
7437c478bd9Sstevel@tonic-gate 	case DEVCTL_AP_CONTROL:
7447c478bd9Sstevel@tonic-gate 	case DEVCTL_BUS_QUIESCE:
7457c478bd9Sstevel@tonic-gate 	case DEVCTL_BUS_UNQUIESCE:
7467c478bd9Sstevel@tonic-gate 		rval = ENOTSUP;
7477c478bd9Sstevel@tonic-gate 		break;
7487c478bd9Sstevel@tonic-gate 	}
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate 	ndi_dc_freehdl(dcp);
7517c478bd9Sstevel@tonic-gate 	return (rval);
7527c478bd9Sstevel@tonic-gate }
7537c478bd9Sstevel@tonic-gate 
7547c478bd9Sstevel@tonic-gate /*
7557c478bd9Sstevel@tonic-gate  * Copyout the state of the Attachment Point "ap" to the requesting
7567c478bd9Sstevel@tonic-gate  * user process.
7577c478bd9Sstevel@tonic-gate  */
7587c478bd9Sstevel@tonic-gate int
ndi_dc_return_ap_state(devctl_ap_state_t * ap,struct devctl_iocdata * dcp)7597c478bd9Sstevel@tonic-gate ndi_dc_return_ap_state(devctl_ap_state_t *ap, struct devctl_iocdata *dcp)
7607c478bd9Sstevel@tonic-gate {
7617c478bd9Sstevel@tonic-gate 	if ((ap == NULL) || (dcp == NULL))
7627c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
7637c478bd9Sstevel@tonic-gate 
7647c478bd9Sstevel@tonic-gate 
7657c478bd9Sstevel@tonic-gate 	if (get_udatamodel() == DATAMODEL_NATIVE) {
7667c478bd9Sstevel@tonic-gate 		if (copyout(ap, dcp->cpyout_buf,
767b9ccdc5aScth 		    sizeof (devctl_ap_state_t)) != 0)
768b9ccdc5aScth 			return (NDI_FAULT);
7697c478bd9Sstevel@tonic-gate 	}
7707c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
7717c478bd9Sstevel@tonic-gate 	else {
7727c478bd9Sstevel@tonic-gate 		struct devctl_ap_state32 ap_state32;
7737c478bd9Sstevel@tonic-gate 
7747c478bd9Sstevel@tonic-gate 		ap_state32.ap_rstate = ap->ap_rstate;
7757c478bd9Sstevel@tonic-gate 		ap_state32.ap_ostate = ap->ap_ostate;
7767c478bd9Sstevel@tonic-gate 		ap_state32.ap_condition = ap->ap_condition;
7777c478bd9Sstevel@tonic-gate 		ap_state32.ap_error_code = ap->ap_error_code;
7787c478bd9Sstevel@tonic-gate 		ap_state32.ap_in_transition = ap->ap_in_transition;
7797c478bd9Sstevel@tonic-gate 		ap_state32.ap_last_change = (time32_t)ap->ap_last_change;
7807c478bd9Sstevel@tonic-gate 		if (copyout(&ap_state32, dcp->cpyout_buf,
781b9ccdc5aScth 		    sizeof (devctl_ap_state32_t)) != 0)
782b9ccdc5aScth 			return (NDI_FAULT);
7837c478bd9Sstevel@tonic-gate 	}
7847c478bd9Sstevel@tonic-gate #endif
7857c478bd9Sstevel@tonic-gate 
7867c478bd9Sstevel@tonic-gate 	return (NDI_SUCCESS);
7877c478bd9Sstevel@tonic-gate }
7887c478bd9Sstevel@tonic-gate 
7897c478bd9Sstevel@tonic-gate /*
7907c478bd9Sstevel@tonic-gate  * Copyout the bus state of the bus nexus device "dip" to the requesting
7917c478bd9Sstevel@tonic-gate  * user process.
7927c478bd9Sstevel@tonic-gate  */
7937c478bd9Sstevel@tonic-gate int
ndi_dc_return_bus_state(dev_info_t * dip,struct devctl_iocdata * dcp)7947c478bd9Sstevel@tonic-gate ndi_dc_return_bus_state(dev_info_t *dip, struct devctl_iocdata *dcp)
7957c478bd9Sstevel@tonic-gate {
7967c478bd9Sstevel@tonic-gate 	uint_t devstate = 0;
7977c478bd9Sstevel@tonic-gate 
7987c478bd9Sstevel@tonic-gate 	if ((dip == NULL) || (dcp == NULL))
7997c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
8007c478bd9Sstevel@tonic-gate 
8017c478bd9Sstevel@tonic-gate 	if (ndi_get_bus_state(dip, &devstate) != NDI_SUCCESS)
8027c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
8037c478bd9Sstevel@tonic-gate 
8047c478bd9Sstevel@tonic-gate 	if (copyout(&devstate, dcp->cpyout_buf, sizeof (uint_t)) != 0)
8057c478bd9Sstevel@tonic-gate 		return (NDI_FAULT);
8067c478bd9Sstevel@tonic-gate 
8077c478bd9Sstevel@tonic-gate 	return (NDI_SUCCESS);
8087c478bd9Sstevel@tonic-gate }
8097c478bd9Sstevel@tonic-gate 
8107c478bd9Sstevel@tonic-gate static int
8117c478bd9Sstevel@tonic-gate i_dc_devi_create(struct devctl_iocdata *, dev_info_t *, dev_info_t **);
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate /*
8147c478bd9Sstevel@tonic-gate  * create a child device node given the property definitions
8157c478bd9Sstevel@tonic-gate  * supplied by the userland process
8167c478bd9Sstevel@tonic-gate  */
8177c478bd9Sstevel@tonic-gate int
ndi_dc_devi_create(struct devctl_iocdata * dcp,dev_info_t * pdip,int flags,dev_info_t ** rdip)8187c478bd9Sstevel@tonic-gate ndi_dc_devi_create(struct devctl_iocdata *dcp, dev_info_t *pdip, int flags,
8197c478bd9Sstevel@tonic-gate     dev_info_t **rdip)
8207c478bd9Sstevel@tonic-gate {
8217c478bd9Sstevel@tonic-gate 	dev_info_t *cdip;
8227c478bd9Sstevel@tonic-gate 	int rv, circular = 0;
8237c478bd9Sstevel@tonic-gate 	char devnm[MAXNAMELEN];
8247c478bd9Sstevel@tonic-gate 	int nmlen;
8257c478bd9Sstevel@tonic-gate 
8267c478bd9Sstevel@tonic-gate 	/*
8277c478bd9Sstevel@tonic-gate 	 * The child device may have been pre-constructed by an earlier
8287c478bd9Sstevel@tonic-gate 	 * call to this function with the flag DEVCTL_CONSTRUCT set.
8297c478bd9Sstevel@tonic-gate 	 */
8307c478bd9Sstevel@tonic-gate 
8317c478bd9Sstevel@tonic-gate 	if ((cdip = (rdip != NULL) ? *rdip : NULL) == NULL)
8327c478bd9Sstevel@tonic-gate 		if ((rv = i_dc_devi_create(dcp, pdip, &cdip)) != 0)
8337c478bd9Sstevel@tonic-gate 			return (rv);
8347c478bd9Sstevel@tonic-gate 
8357c478bd9Sstevel@tonic-gate 	ASSERT(cdip != NULL);
8367c478bd9Sstevel@tonic-gate 
8377c478bd9Sstevel@tonic-gate 	/*
8387c478bd9Sstevel@tonic-gate 	 * Return the device node partially constructed if the
8397c478bd9Sstevel@tonic-gate 	 * DEVCTL_CONSTRUCT flag is set.
8407c478bd9Sstevel@tonic-gate 	 */
8417c478bd9Sstevel@tonic-gate 	if (flags & DEVCTL_CONSTRUCT) {
8427c478bd9Sstevel@tonic-gate 		if (rdip == NULL) {
8437c478bd9Sstevel@tonic-gate 			(void) ndi_devi_free(cdip);
8447c478bd9Sstevel@tonic-gate 			return (EINVAL);
8457c478bd9Sstevel@tonic-gate 		}
8467c478bd9Sstevel@tonic-gate 		*rdip = cdip;
8477c478bd9Sstevel@tonic-gate 		return (0);
8487c478bd9Sstevel@tonic-gate 	}
8497c478bd9Sstevel@tonic-gate 
8507c478bd9Sstevel@tonic-gate 	/*
8517c478bd9Sstevel@tonic-gate 	 * Bring the node up to a named but OFFLINE state.  The calling
8527c478bd9Sstevel@tonic-gate 	 * application will need to manage the node from here on.
8537c478bd9Sstevel@tonic-gate 	 */
8547c478bd9Sstevel@tonic-gate 	if (dcp->flags & DEVCTL_OFFLINE) {
85525e8c5aaSvikram 		/*
85625e8c5aaSvikram 		 * In the unlikely event that the dip was somehow attached by
85725e8c5aaSvikram 		 * the userland process (and device contracts or LDI opens
85825e8c5aaSvikram 		 * were registered against the dip) after it was created by
85925e8c5aaSvikram 		 * a previous DEVCTL_CONSTRUCT call, we start notify
86025e8c5aaSvikram 		 * proceedings on this dip. Note that we don't need to
86125e8c5aaSvikram 		 * return the dip after a failure of the notify since
86225e8c5aaSvikram 		 * for a contract or LDI handle to be created the dip was
86325e8c5aaSvikram 		 * already available to the user.
86425e8c5aaSvikram 		 */
86525e8c5aaSvikram 		if (e_ddi_offline_notify(cdip) == DDI_FAILURE) {
86625e8c5aaSvikram 			return (EBUSY);
86725e8c5aaSvikram 		}
86825e8c5aaSvikram 
8697c478bd9Sstevel@tonic-gate 		/*
8707c478bd9Sstevel@tonic-gate 		 * hand set the OFFLINE flag to prevent any asynchronous
8717c478bd9Sstevel@tonic-gate 		 * autoconfiguration operations from attaching this node.
8727c478bd9Sstevel@tonic-gate 		 */
87316747f41Scth 		mutex_enter(&(DEVI(cdip)->devi_lock));
87416747f41Scth 		DEVI_SET_DEVICE_OFFLINE(cdip);
87516747f41Scth 		mutex_exit(&(DEVI(cdip)->devi_lock));
87616747f41Scth 
87725e8c5aaSvikram 		e_ddi_offline_finalize(cdip, DDI_SUCCESS);
87825e8c5aaSvikram 
8797c478bd9Sstevel@tonic-gate 		rv = ndi_devi_bind_driver(cdip, flags);
8807c478bd9Sstevel@tonic-gate 		if (rv != NDI_SUCCESS) {
8817c478bd9Sstevel@tonic-gate 			(void) ndi_devi_offline(cdip, NDI_DEVI_REMOVE);
8827c478bd9Sstevel@tonic-gate 			return (ENXIO);
8837c478bd9Sstevel@tonic-gate 		}
8847c478bd9Sstevel@tonic-gate 
8857c478bd9Sstevel@tonic-gate 		/*
8867c478bd9Sstevel@tonic-gate 		 * remove the dev_info node if it failed to bind to a
8877c478bd9Sstevel@tonic-gate 		 * driver above.
8887c478bd9Sstevel@tonic-gate 		 */
8897c478bd9Sstevel@tonic-gate 		if (i_ddi_node_state(cdip) < DS_BOUND) {
8907c478bd9Sstevel@tonic-gate 			(void) ndi_devi_offline(cdip, NDI_DEVI_REMOVE);
8917c478bd9Sstevel@tonic-gate 			return (ENXIO);
8927c478bd9Sstevel@tonic-gate 		}
8937c478bd9Sstevel@tonic-gate 
8947c478bd9Sstevel@tonic-gate 		/*
8957c478bd9Sstevel@tonic-gate 		 * add the node to the per-driver list and INITCHILD it
8967c478bd9Sstevel@tonic-gate 		 * to give it a name.
8977c478bd9Sstevel@tonic-gate 		 */
8987c478bd9Sstevel@tonic-gate 		ndi_devi_enter(pdip, &circular);
8997c478bd9Sstevel@tonic-gate 		if ((rv = ddi_initchild(pdip, cdip)) != DDI_SUCCESS) {
9007c478bd9Sstevel@tonic-gate 			(void) ndi_devi_offline(cdip, NDI_DEVI_REMOVE);
9017c478bd9Sstevel@tonic-gate 			ndi_devi_exit(pdip, circular);
9027c478bd9Sstevel@tonic-gate 			return (EINVAL);
9037c478bd9Sstevel@tonic-gate 		}
9047c478bd9Sstevel@tonic-gate 		ndi_devi_exit(pdip, circular);
9057c478bd9Sstevel@tonic-gate 
9067c478bd9Sstevel@tonic-gate 	} else {
9077c478bd9Sstevel@tonic-gate 		/*
9087c478bd9Sstevel@tonic-gate 		 * Attempt to bring the device ONLINE. If the request to
9097c478bd9Sstevel@tonic-gate 		 * fails, remove the dev_info node.
9107c478bd9Sstevel@tonic-gate 		 */
9117c478bd9Sstevel@tonic-gate 		if (ndi_devi_online(cdip, NDI_ONLINE_ATTACH) != NDI_SUCCESS) {
9127c478bd9Sstevel@tonic-gate 			(void) ndi_devi_offline(cdip, NDI_DEVI_REMOVE);
9137c478bd9Sstevel@tonic-gate 			return (ENXIO);
9147c478bd9Sstevel@tonic-gate 		}
9157c478bd9Sstevel@tonic-gate 
9167c478bd9Sstevel@tonic-gate 		/*
9177c478bd9Sstevel@tonic-gate 		 * if the node was successfully added but there was
9187c478bd9Sstevel@tonic-gate 		 * no driver available for the device, remove the node
9197c478bd9Sstevel@tonic-gate 		 */
9207c478bd9Sstevel@tonic-gate 		if (i_ddi_node_state(cdip) < DS_BOUND) {
9217c478bd9Sstevel@tonic-gate 			(void) ndi_devi_offline(cdip, NDI_DEVI_REMOVE);
9227c478bd9Sstevel@tonic-gate 			return (ENODEV);
9237c478bd9Sstevel@tonic-gate 		}
9247c478bd9Sstevel@tonic-gate 	}
9257c478bd9Sstevel@tonic-gate 
9267c478bd9Sstevel@tonic-gate 	/*
9277c478bd9Sstevel@tonic-gate 	 * return a handle to the child device
9287c478bd9Sstevel@tonic-gate 	 * copy out the name of the newly attached child device if
9297c478bd9Sstevel@tonic-gate 	 * the IOCTL request has provided a copyout buffer.
9307c478bd9Sstevel@tonic-gate 	 */
9317c478bd9Sstevel@tonic-gate 	if (rdip != NULL)
9327c478bd9Sstevel@tonic-gate 		*rdip = cdip;
9337c478bd9Sstevel@tonic-gate 
9347c478bd9Sstevel@tonic-gate 	if (dcp->cpyout_buf == NULL)
9357c478bd9Sstevel@tonic-gate 		return (0);
9367c478bd9Sstevel@tonic-gate 
9377c478bd9Sstevel@tonic-gate 	ASSERT(ddi_node_name(cdip) != NULL);
9387c478bd9Sstevel@tonic-gate 	ASSERT(ddi_get_name_addr(cdip) != NULL);
9397c478bd9Sstevel@tonic-gate 
9407c478bd9Sstevel@tonic-gate 	nmlen = snprintf(devnm, MAXNAMELEN, "%s@%s",
9417c478bd9Sstevel@tonic-gate 	    ddi_node_name(cdip), ddi_get_name_addr(cdip));
9427c478bd9Sstevel@tonic-gate 
9437c478bd9Sstevel@tonic-gate 	if (copyout(&devnm, dcp->cpyout_buf, nmlen) != 0) {
9447c478bd9Sstevel@tonic-gate 		(void) ndi_devi_offline(cdip, NDI_DEVI_REMOVE);
9457c478bd9Sstevel@tonic-gate 		return (EFAULT);
9467c478bd9Sstevel@tonic-gate 	}
9477c478bd9Sstevel@tonic-gate 	return (0);
9487c478bd9Sstevel@tonic-gate }
9497c478bd9Sstevel@tonic-gate 
9507c478bd9Sstevel@tonic-gate static int
i_dc_devi_create(struct devctl_iocdata * dcp,dev_info_t * pdip,dev_info_t ** rdip)9517c478bd9Sstevel@tonic-gate i_dc_devi_create(struct devctl_iocdata *dcp, dev_info_t *pdip,
9527c478bd9Sstevel@tonic-gate     dev_info_t **rdip)
9537c478bd9Sstevel@tonic-gate {
9547c478bd9Sstevel@tonic-gate 
9557c478bd9Sstevel@tonic-gate 	dev_info_t *cdip;
9567c478bd9Sstevel@tonic-gate 	char *cname = NULL;
9577c478bd9Sstevel@tonic-gate 	nvlist_t *nvlp = dcp->nvl_user;
9587c478bd9Sstevel@tonic-gate 	nvpair_t *npp;
9597c478bd9Sstevel@tonic-gate 	char *np;
9607c478bd9Sstevel@tonic-gate 	int rv = 0;
9617c478bd9Sstevel@tonic-gate 
9627c478bd9Sstevel@tonic-gate 	ASSERT(rdip != NULL && *rdip == NULL);
9637c478bd9Sstevel@tonic-gate 
9647c478bd9Sstevel@tonic-gate 	if ((nvlp == NULL) ||
9657c478bd9Sstevel@tonic-gate 	    (nvlist_lookup_string(nvlp, DC_DEVI_NODENAME, &cname) != 0))
9667c478bd9Sstevel@tonic-gate 		return (EINVAL);
9677c478bd9Sstevel@tonic-gate 
9687c478bd9Sstevel@tonic-gate 	/*
9697c478bd9Sstevel@tonic-gate 	 * construct a new dev_info node with a user-provided nodename
9707c478bd9Sstevel@tonic-gate 	 */
971fa9e4066Sahrens 	ndi_devi_alloc_sleep(pdip, cname, (pnode_t)DEVI_SID_NODEID, &cdip);
9727c478bd9Sstevel@tonic-gate 
9737c478bd9Sstevel@tonic-gate 	/*
9747c478bd9Sstevel@tonic-gate 	 * create hardware properties for each member in the property
9757c478bd9Sstevel@tonic-gate 	 * list.
9767c478bd9Sstevel@tonic-gate 	 */
9777c478bd9Sstevel@tonic-gate 	for (npp = nvlist_next_nvpair(nvlp, NULL); (npp != NULL && !rv);
9787c478bd9Sstevel@tonic-gate 	    npp = nvlist_next_nvpair(nvlp, npp)) {
9797c478bd9Sstevel@tonic-gate 
9807c478bd9Sstevel@tonic-gate 		np = nvpair_name(npp);
9817c478bd9Sstevel@tonic-gate 
9827c478bd9Sstevel@tonic-gate 		/*
9837c478bd9Sstevel@tonic-gate 		 * skip the nodename property
9847c478bd9Sstevel@tonic-gate 		 */
9857c478bd9Sstevel@tonic-gate 		if (strcmp(np, DC_DEVI_NODENAME) == 0)
9867c478bd9Sstevel@tonic-gate 			continue;
9877c478bd9Sstevel@tonic-gate 
9887c478bd9Sstevel@tonic-gate 		switch (nvpair_type(npp)) {
9897c478bd9Sstevel@tonic-gate 
9907c478bd9Sstevel@tonic-gate 		case DATA_TYPE_INT32: {
9917c478bd9Sstevel@tonic-gate 			int32_t prop_val;
9927c478bd9Sstevel@tonic-gate 
9937c478bd9Sstevel@tonic-gate 			if ((rv = nvpair_value_int32(npp, &prop_val)) != 0)
9947c478bd9Sstevel@tonic-gate 				break;
9957c478bd9Sstevel@tonic-gate 
9967c478bd9Sstevel@tonic-gate 			(void) ndi_prop_update_int(DDI_DEV_T_NONE, cdip, np,
9977c478bd9Sstevel@tonic-gate 			    (int)prop_val);
9987c478bd9Sstevel@tonic-gate 			break;
9997c478bd9Sstevel@tonic-gate 		}
10007c478bd9Sstevel@tonic-gate 
10017c478bd9Sstevel@tonic-gate 		case DATA_TYPE_STRING: {
10027c478bd9Sstevel@tonic-gate 			char *prop_val;
10037c478bd9Sstevel@tonic-gate 
10047c478bd9Sstevel@tonic-gate 			if ((rv = nvpair_value_string(npp, &prop_val)) != 0)
10057c478bd9Sstevel@tonic-gate 				break;
10067c478bd9Sstevel@tonic-gate 
10077c478bd9Sstevel@tonic-gate 			(void) ndi_prop_update_string(DDI_DEV_T_NONE, cdip,
10087c478bd9Sstevel@tonic-gate 			    np, prop_val);
10097c478bd9Sstevel@tonic-gate 			break;
10107c478bd9Sstevel@tonic-gate 		}
10117c478bd9Sstevel@tonic-gate 
10127c478bd9Sstevel@tonic-gate 		case DATA_TYPE_BYTE_ARRAY: {
10137c478bd9Sstevel@tonic-gate 			uchar_t *val;
10147c478bd9Sstevel@tonic-gate 			uint_t nelms;
10157c478bd9Sstevel@tonic-gate 
10167c478bd9Sstevel@tonic-gate 			if ((rv = nvpair_value_byte_array(npp, &val,
10177c478bd9Sstevel@tonic-gate 			    &nelms)) != 0)
10187c478bd9Sstevel@tonic-gate 				break;
10197c478bd9Sstevel@tonic-gate 
10207c478bd9Sstevel@tonic-gate 			(void) ndi_prop_update_byte_array(DDI_DEV_T_NONE,
10217c478bd9Sstevel@tonic-gate 			    cdip, np, (uchar_t *)val, nelms);
10227c478bd9Sstevel@tonic-gate 			break;
10237c478bd9Sstevel@tonic-gate 		}
10247c478bd9Sstevel@tonic-gate 
10257c478bd9Sstevel@tonic-gate 		case DATA_TYPE_INT32_ARRAY: {
10267c478bd9Sstevel@tonic-gate 			int32_t *val;
10277c478bd9Sstevel@tonic-gate 			uint_t nelms;
10287c478bd9Sstevel@tonic-gate 
10297c478bd9Sstevel@tonic-gate 			if ((rv = nvpair_value_int32_array(npp, &val,
10307c478bd9Sstevel@tonic-gate 			    &nelms)) != 0)
10317c478bd9Sstevel@tonic-gate 				break;
10327c478bd9Sstevel@tonic-gate 
10337c478bd9Sstevel@tonic-gate 			(void) ndi_prop_update_int_array(DDI_DEV_T_NONE,
10347c478bd9Sstevel@tonic-gate 			    cdip, np, val, nelms);
10357c478bd9Sstevel@tonic-gate 			break;
10367c478bd9Sstevel@tonic-gate 		}
10377c478bd9Sstevel@tonic-gate 
10387c478bd9Sstevel@tonic-gate 		case DATA_TYPE_STRING_ARRAY: {
10397c478bd9Sstevel@tonic-gate 			char **val;
10407c478bd9Sstevel@tonic-gate 			uint_t nelms;
10417c478bd9Sstevel@tonic-gate 
10427c478bd9Sstevel@tonic-gate 			if ((rv = nvpair_value_string_array(npp, &val,
10437c478bd9Sstevel@tonic-gate 			    &nelms)) != 0)
10447c478bd9Sstevel@tonic-gate 				break;
10457c478bd9Sstevel@tonic-gate 
10467c478bd9Sstevel@tonic-gate 			(void) ndi_prop_update_string_array(DDI_DEV_T_NONE,
10477c478bd9Sstevel@tonic-gate 			    cdip, np, val, nelms);
10487c478bd9Sstevel@tonic-gate 			break;
10497c478bd9Sstevel@tonic-gate 		}
10507c478bd9Sstevel@tonic-gate 
10517c478bd9Sstevel@tonic-gate 		/*
10527c478bd9Sstevel@tonic-gate 		 * unsupported property data type
10537c478bd9Sstevel@tonic-gate 		 */
10547c478bd9Sstevel@tonic-gate 		default:
10557c478bd9Sstevel@tonic-gate 			rv = EINVAL;
10567c478bd9Sstevel@tonic-gate 		}
10577c478bd9Sstevel@tonic-gate 	}
10587c478bd9Sstevel@tonic-gate 
10597c478bd9Sstevel@tonic-gate 	/*
10607c478bd9Sstevel@tonic-gate 	 * something above failed
10617c478bd9Sstevel@tonic-gate 	 * destroy the partially child device and abort the request
10627c478bd9Sstevel@tonic-gate 	 */
10637c478bd9Sstevel@tonic-gate 	if (rv != 0) {
10647c478bd9Sstevel@tonic-gate 		(void) ndi_devi_free(cdip);
10657c478bd9Sstevel@tonic-gate 		return (rv);
10667c478bd9Sstevel@tonic-gate 	}
10677c478bd9Sstevel@tonic-gate 
10687c478bd9Sstevel@tonic-gate 	*rdip = cdip;
10697c478bd9Sstevel@tonic-gate 	return (0);
10707c478bd9Sstevel@tonic-gate }
10717c478bd9Sstevel@tonic-gate 
10727c478bd9Sstevel@tonic-gate /*
10737c478bd9Sstevel@tonic-gate  * return current soft bus state of bus nexus "dip"
10747c478bd9Sstevel@tonic-gate  */
10757c478bd9Sstevel@tonic-gate int
ndi_get_bus_state(dev_info_t * dip,uint_t * rstate)10767c478bd9Sstevel@tonic-gate ndi_get_bus_state(dev_info_t *dip, uint_t *rstate)
10777c478bd9Sstevel@tonic-gate {
10787c478bd9Sstevel@tonic-gate 	if (dip == NULL || rstate == NULL)
10797c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
10807c478bd9Sstevel@tonic-gate 
10817c478bd9Sstevel@tonic-gate 	if (DEVI(dip)->devi_ops->devo_bus_ops == NULL)
10827c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
10837c478bd9Sstevel@tonic-gate 
10847c478bd9Sstevel@tonic-gate 	mutex_enter(&(DEVI(dip)->devi_lock));
10857c478bd9Sstevel@tonic-gate 	if (DEVI_IS_BUS_QUIESCED(dip))
10867c478bd9Sstevel@tonic-gate 		*rstate = BUS_QUIESCED;
10877c478bd9Sstevel@tonic-gate 	else if (DEVI_IS_BUS_DOWN(dip))
10887c478bd9Sstevel@tonic-gate 		*rstate = BUS_SHUTDOWN;
10897c478bd9Sstevel@tonic-gate 	else
10907c478bd9Sstevel@tonic-gate 		*rstate = BUS_ACTIVE;
10917c478bd9Sstevel@tonic-gate 	mutex_exit(&(DEVI(dip)->devi_lock));
10927c478bd9Sstevel@tonic-gate 	return (NDI_SUCCESS);
10937c478bd9Sstevel@tonic-gate }
10947c478bd9Sstevel@tonic-gate 
10957c478bd9Sstevel@tonic-gate /*
10967c478bd9Sstevel@tonic-gate  * Set the soft state of bus nexus "dip"
10977c478bd9Sstevel@tonic-gate  */
10987c478bd9Sstevel@tonic-gate int
ndi_set_bus_state(dev_info_t * dip,uint_t state)10997c478bd9Sstevel@tonic-gate ndi_set_bus_state(dev_info_t *dip, uint_t state)
11007c478bd9Sstevel@tonic-gate {
11017c478bd9Sstevel@tonic-gate 	int rv = NDI_SUCCESS;
11027c478bd9Sstevel@tonic-gate 
11037c478bd9Sstevel@tonic-gate 	if (dip == NULL)
11047c478bd9Sstevel@tonic-gate 		return (NDI_FAILURE);
11057c478bd9Sstevel@tonic-gate 
11067c478bd9Sstevel@tonic-gate 	mutex_enter(&(DEVI(dip)->devi_lock));
11077c478bd9Sstevel@tonic-gate 
11087c478bd9Sstevel@tonic-gate 	switch (state) {
11097c478bd9Sstevel@tonic-gate 	case BUS_QUIESCED:
11107c478bd9Sstevel@tonic-gate 		DEVI_SET_BUS_QUIESCE(dip);
11117c478bd9Sstevel@tonic-gate 		break;
11127c478bd9Sstevel@tonic-gate 
11137c478bd9Sstevel@tonic-gate 	case BUS_ACTIVE:
11147c478bd9Sstevel@tonic-gate 		DEVI_SET_BUS_ACTIVE(dip);
11157c478bd9Sstevel@tonic-gate 		DEVI_SET_BUS_UP(dip);
11167c478bd9Sstevel@tonic-gate 		break;
11177c478bd9Sstevel@tonic-gate 
11187c478bd9Sstevel@tonic-gate 	case BUS_SHUTDOWN:
11197c478bd9Sstevel@tonic-gate 		DEVI_SET_BUS_DOWN(dip);
11207c478bd9Sstevel@tonic-gate 		break;
11217c478bd9Sstevel@tonic-gate 
11227c478bd9Sstevel@tonic-gate 	default:
11237c478bd9Sstevel@tonic-gate 		rv = NDI_FAILURE;
11247c478bd9Sstevel@tonic-gate 	}
11257c478bd9Sstevel@tonic-gate 
11267c478bd9Sstevel@tonic-gate 	mutex_exit(&(DEVI(dip)->devi_lock));
11277c478bd9Sstevel@tonic-gate 	return (rv);
11287c478bd9Sstevel@tonic-gate }
11297c478bd9Sstevel@tonic-gate 
11307c478bd9Sstevel@tonic-gate /*
11317c478bd9Sstevel@tonic-gate  * These dummy functions are obsolete and may be removed.
11327c478bd9Sstevel@tonic-gate  * Retained for existing driver compatibility only.
11337c478bd9Sstevel@tonic-gate  * Drivers should be fixed not to use these functions.
11347c478bd9Sstevel@tonic-gate  * Don't write new code using these obsolete interfaces.
11357c478bd9