xref: /titanic_51/usr/src/uts/sun4/io/ebus.c (revision 193974072f41a843678abf5f61979c748687e66b)
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
5fd715b00Srameshc  * Common Development and Distribution License (the "License").
6fd715b00Srameshc  * 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 /*
22*19397407SSherry Moore  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <sys/types.h>
287c478bd9Sstevel@tonic-gate #include <sys/conf.h>
297c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
307c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
317c478bd9Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
327c478bd9Sstevel@tonic-gate #include <sys/ddi_subrdefs.h>
337c478bd9Sstevel@tonic-gate #include <sys/pci.h>
347c478bd9Sstevel@tonic-gate #include <sys/autoconf.h>
357c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
367c478bd9Sstevel@tonic-gate #include <sys/errno.h>
377c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
387c478bd9Sstevel@tonic-gate #include <sys/debug.h>
397c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
407c478bd9Sstevel@tonic-gate #include <sys/ebus.h>
417c478bd9Sstevel@tonic-gate #include <sys/open.h>
427c478bd9Sstevel@tonic-gate #include <sys/stat.h>
437c478bd9Sstevel@tonic-gate #include <sys/file.h>
447c478bd9Sstevel@tonic-gate #include <sys/sunndi.h>
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate #ifdef DEBUG
477c478bd9Sstevel@tonic-gate uint64_t ebus_debug_flags = 0;
487c478bd9Sstevel@tonic-gate #endif
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate /*
517c478bd9Sstevel@tonic-gate  * The values of the following variables are used to initialize
527c478bd9Sstevel@tonic-gate  * the cache line size and latency timer registers in the ebus
537c478bd9Sstevel@tonic-gate  * configuration header.  Variables are used instead of constants
547c478bd9Sstevel@tonic-gate  * to allow tuning from the /etc/system file.
557c478bd9Sstevel@tonic-gate  */
567c478bd9Sstevel@tonic-gate static uint8_t ebus_cache_line_size = 0x10;	/* 64 bytes */
577c478bd9Sstevel@tonic-gate static uint8_t ebus_latency_timer = 0x40;	/* 64 PCI cycles */
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate /*
607c478bd9Sstevel@tonic-gate  * function prototypes for bus ops routines:
617c478bd9Sstevel@tonic-gate  */
627c478bd9Sstevel@tonic-gate static int
637c478bd9Sstevel@tonic-gate ebus_map(dev_info_t *dip, dev_info_t *rdip, ddi_map_req_t *mp,
647c478bd9Sstevel@tonic-gate 	off_t offset, off_t len, caddr_t *addrp);
657c478bd9Sstevel@tonic-gate static int
667c478bd9Sstevel@tonic-gate ebus_ctlops(dev_info_t *dip, dev_info_t *rdip,
677c478bd9Sstevel@tonic-gate 	ddi_ctl_enum_t op, void *arg, void *result);
687c478bd9Sstevel@tonic-gate static int
697c478bd9Sstevel@tonic-gate ebus_intr_ops(dev_info_t *dip, dev_info_t *rdip, ddi_intr_op_t intr_op,
707c478bd9Sstevel@tonic-gate     ddi_intr_handle_impl_t *hdlp, void *result);
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate /*
737c478bd9Sstevel@tonic-gate  * function prototypes for dev ops routines:
747c478bd9Sstevel@tonic-gate  */
757c478bd9Sstevel@tonic-gate static int ebus_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
767c478bd9Sstevel@tonic-gate static int ebus_detach(dev_info_t *dip, ddi_detach_cmd_t cmd);
777c478bd9Sstevel@tonic-gate static int ebus_info(dev_info_t *dip, ddi_info_cmd_t infocmd,
787c478bd9Sstevel@tonic-gate 	void *arg, void **result);
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate /*
817c478bd9Sstevel@tonic-gate  * general function prototypes:
827c478bd9Sstevel@tonic-gate  */
837c478bd9Sstevel@tonic-gate static int ebus_config(ebus_devstate_t *ebus_p);
847c478bd9Sstevel@tonic-gate static int ebus_apply_range(ebus_devstate_t *ebus_p, dev_info_t *rdip,
85c4485e78Sjasonwu     ebus_regspec_t *ebus_rp, vregspec_t *rp);
86c4485e78Sjasonwu int ebus_get_ranges_prop(ebus_devstate_t *ebus_p);
87c4485e78Sjasonwu static void ebus_get_cells_prop(ebus_devstate_t *ebus_p);
88c4485e78Sjasonwu static void ebus_vreg_dump(ebus_devstate_t *ebus_p, vregspec_t *rp);
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate #define	getprop(dip, name, addr, intp)		\
91a3282898Scth 		ddi_getlongprop(DDI_DEV_T_ANY, (dip), DDI_PROP_DONTPASS, \
927c478bd9Sstevel@tonic-gate 				(name), (caddr_t)(addr), (intp))
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate static int ebus_open(dev_t *devp, int flags, int otyp, cred_t *credp);
957c478bd9Sstevel@tonic-gate static int ebus_close(dev_t dev, int flags, int otyp, cred_t *credp);
967c478bd9Sstevel@tonic-gate static int ebus_ioctl(dev_t dev, int cmd, intptr_t arg, int mode,
977c478bd9Sstevel@tonic-gate 						cred_t *credp, int *rvalp);
987c478bd9Sstevel@tonic-gate struct cb_ops ebus_cb_ops = {
997c478bd9Sstevel@tonic-gate 	ebus_open,			/* open */
1007c478bd9Sstevel@tonic-gate 	ebus_close,			/* close */
1017c478bd9Sstevel@tonic-gate 	nodev,				/* strategy */
1027c478bd9Sstevel@tonic-gate 	nodev,				/* print */
1037c478bd9Sstevel@tonic-gate 	nodev,				/* dump */
1047c478bd9Sstevel@tonic-gate 	nodev,				/* read */
1057c478bd9Sstevel@tonic-gate 	nodev,				/* write */
1067c478bd9Sstevel@tonic-gate 	ebus_ioctl,			/* ioctl */
1077c478bd9Sstevel@tonic-gate 	nodev,				/* devmap */
1087c478bd9Sstevel@tonic-gate 	nodev,				/* mmap */
1097c478bd9Sstevel@tonic-gate 	nodev,				/* segmap */
1107c478bd9Sstevel@tonic-gate 	nochpoll,			/* poll */
1117c478bd9Sstevel@tonic-gate 	ddi_prop_op,			/* cb_prop_op */
1127c478bd9Sstevel@tonic-gate 	NULL,				/* streamtab */
1137c478bd9Sstevel@tonic-gate 	D_NEW | D_MP | D_HOTPLUG,	/* Driver compatibility flag */
1147c478bd9Sstevel@tonic-gate 	CB_REV,				/* rev */
1157c478bd9Sstevel@tonic-gate 	nodev,				/* int (*cb_aread)() */
1167c478bd9Sstevel@tonic-gate 	nodev				/* int (*cb_awrite)() */
1177c478bd9Sstevel@tonic-gate };
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate /*
1207c478bd9Sstevel@tonic-gate  * bus ops and dev ops structures:
1217c478bd9Sstevel@tonic-gate  */
1227c478bd9Sstevel@tonic-gate static struct bus_ops ebus_bus_ops = {
1237c478bd9Sstevel@tonic-gate 	BUSO_REV,
1247c478bd9Sstevel@tonic-gate 	ebus_map,
1257c478bd9Sstevel@tonic-gate 	NULL,
1267c478bd9Sstevel@tonic-gate 	NULL,
1277c478bd9Sstevel@tonic-gate 	NULL,
1287c478bd9Sstevel@tonic-gate 	i_ddi_map_fault,
1297c478bd9Sstevel@tonic-gate 	ddi_dma_map,
1307c478bd9Sstevel@tonic-gate 	ddi_dma_allochdl,
1317c478bd9Sstevel@tonic-gate 	ddi_dma_freehdl,
1327c478bd9Sstevel@tonic-gate 	ddi_dma_bindhdl,
1337c478bd9Sstevel@tonic-gate 	ddi_dma_unbindhdl,
1347c478bd9Sstevel@tonic-gate 	ddi_dma_flush,
1357c478bd9Sstevel@tonic-gate 	ddi_dma_win,
1367c478bd9Sstevel@tonic-gate 	ddi_dma_mctl,
1377c478bd9Sstevel@tonic-gate 	ebus_ctlops,
1387c478bd9Sstevel@tonic-gate 	ddi_bus_prop_op,
1397c478bd9Sstevel@tonic-gate 	ndi_busop_get_eventcookie,
1407c478bd9Sstevel@tonic-gate 	ndi_busop_add_eventcall,
1417c478bd9Sstevel@tonic-gate 	ndi_busop_remove_eventcall,
1427c478bd9Sstevel@tonic-gate 	ndi_post_event,
1437c478bd9Sstevel@tonic-gate 	0,
1447c478bd9Sstevel@tonic-gate 	0,
1457c478bd9Sstevel@tonic-gate 	0,
1467c478bd9Sstevel@tonic-gate 	0,
1477c478bd9Sstevel@tonic-gate 	0,
1487c478bd9Sstevel@tonic-gate 	0,
1497c478bd9Sstevel@tonic-gate 	0,
1507c478bd9Sstevel@tonic-gate 	0,
1517c478bd9Sstevel@tonic-gate 	ebus_intr_ops
1527c478bd9Sstevel@tonic-gate };
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate static struct dev_ops ebus_ops = {
1557c478bd9Sstevel@tonic-gate 	DEVO_REV,
1567c478bd9Sstevel@tonic-gate 	0,
1577c478bd9Sstevel@tonic-gate 	ebus_info,
1587c478bd9Sstevel@tonic-gate 	nulldev,
1597c478bd9Sstevel@tonic-gate 	nulldev,
1607c478bd9Sstevel@tonic-gate 	ebus_attach,
1617c478bd9Sstevel@tonic-gate 	ebus_detach,
1627c478bd9Sstevel@tonic-gate 	nodev,
1637c478bd9Sstevel@tonic-gate 	&ebus_cb_ops,
164*19397407SSherry Moore 	&ebus_bus_ops,
165*19397407SSherry Moore 	NULL,
166*19397407SSherry Moore 	ddi_quiesce_not_supported,	/* devo_quiesce */
1677c478bd9Sstevel@tonic-gate };
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate /*
1707c478bd9Sstevel@tonic-gate  * module definitions:
1717c478bd9Sstevel@tonic-gate  */
1727c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
1737c478bd9Sstevel@tonic-gate extern struct mod_ops mod_driverops;
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate static struct modldrv modldrv = {
1767c478bd9Sstevel@tonic-gate 	&mod_driverops, 	/* Type of module.  This one is a driver */
177*19397407SSherry Moore 	"ebus nexus driver", /* Name of module. */
1787c478bd9Sstevel@tonic-gate 	&ebus_ops,		/* driver ops */
1797c478bd9Sstevel@tonic-gate };
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
1827c478bd9Sstevel@tonic-gate 	MODREV_1, (void *)&modldrv, NULL
1837c478bd9Sstevel@tonic-gate };
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate /*
1867c478bd9Sstevel@tonic-gate  * driver global data:
1877c478bd9Sstevel@tonic-gate  */
1887c478bd9Sstevel@tonic-gate static void *per_ebus_state;		/* per-ebus soft state pointer */
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate int
1927c478bd9Sstevel@tonic-gate _init(void)
1937c478bd9Sstevel@tonic-gate {
1947c478bd9Sstevel@tonic-gate 	int e;
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 	/*
1977c478bd9Sstevel@tonic-gate 	 * Initialize per-ebus soft state pointer.
1987c478bd9Sstevel@tonic-gate 	 */
1997c478bd9Sstevel@tonic-gate 	e = ddi_soft_state_init(&per_ebus_state, sizeof (ebus_devstate_t), 1);
2007c478bd9Sstevel@tonic-gate 	if (e != 0)
2017c478bd9Sstevel@tonic-gate 		return (e);
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	/*
2047c478bd9Sstevel@tonic-gate 	 * Install the module.
2057c478bd9Sstevel@tonic-gate 	 */
2067c478bd9Sstevel@tonic-gate 	e = mod_install(&modlinkage);
2077c478bd9Sstevel@tonic-gate 	if (e != 0)
2087c478bd9Sstevel@tonic-gate 		ddi_soft_state_fini(&per_ebus_state);
2097c478bd9Sstevel@tonic-gate 	return (e);
2107c478bd9Sstevel@tonic-gate }
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate int
2137c478bd9Sstevel@tonic-gate _fini(void)
2147c478bd9Sstevel@tonic-gate {
2157c478bd9Sstevel@tonic-gate 	int e;
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	/*
2187c478bd9Sstevel@tonic-gate 	 * Remove the module.
2197c478bd9Sstevel@tonic-gate 	 */
2207c478bd9Sstevel@tonic-gate 	e = mod_remove(&modlinkage);
2217c478bd9Sstevel@tonic-gate 	if (e != 0)
2227c478bd9Sstevel@tonic-gate 		return (e);
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	/*
2257c478bd9Sstevel@tonic-gate 	 * Free the soft state info.
2267c478bd9Sstevel@tonic-gate 	 */
2277c478bd9Sstevel@tonic-gate 	ddi_soft_state_fini(&per_ebus_state);
2287c478bd9Sstevel@tonic-gate 	return (e);
2297c478bd9Sstevel@tonic-gate }
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate int
2327c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
2337c478bd9Sstevel@tonic-gate {
2347c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
2357c478bd9Sstevel@tonic-gate }
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate /* device driver entry points */
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2407c478bd9Sstevel@tonic-gate static int
2417c478bd9Sstevel@tonic-gate ebus_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
2427c478bd9Sstevel@tonic-gate {
2437c478bd9Sstevel@tonic-gate 	ebus_devstate_t *ebus_p;	/* per ebus state pointer */
2447c478bd9Sstevel@tonic-gate 	int instance;
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 	instance = getminor((dev_t)arg);
2477c478bd9Sstevel@tonic-gate 	ebus_p = get_ebus_soft_state(instance);
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 	switch (infocmd) {
2507c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
251360e6f5eSmathue 		*result = (void *)(uintptr_t)instance;
252c4485e78Sjasonwu 		break;
2537c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
2547c478bd9Sstevel@tonic-gate 		if (ebus_p == NULL)
2557c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
2567c478bd9Sstevel@tonic-gate 		*result = (void *)ebus_p->dip;
257c4485e78Sjasonwu 		break;
258c4485e78Sjasonwu 	default:
259c4485e78Sjasonwu 		return (DDI_FAILURE);
2607c478bd9Sstevel@tonic-gate 	}
261c4485e78Sjasonwu 
262c4485e78Sjasonwu 	return (DDI_SUCCESS);
2637c478bd9Sstevel@tonic-gate }
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate /*
2667c478bd9Sstevel@tonic-gate  * attach entry point:
2677c478bd9Sstevel@tonic-gate  *
2687c478bd9Sstevel@tonic-gate  * normal attach:
2697c478bd9Sstevel@tonic-gate  *
2707c478bd9Sstevel@tonic-gate  *	create soft state structure (dip, reg, nreg and state fields)
2717c478bd9Sstevel@tonic-gate  *	map in configuration header
2727c478bd9Sstevel@tonic-gate  *	make sure device is properly configured
2737c478bd9Sstevel@tonic-gate  *	report device
2747c478bd9Sstevel@tonic-gate  */
2757c478bd9Sstevel@tonic-gate static int
2767c478bd9Sstevel@tonic-gate ebus_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
2777c478bd9Sstevel@tonic-gate {
2787c478bd9Sstevel@tonic-gate 	ebus_devstate_t *ebus_p;	/* per ebus state pointer */
2797c478bd9Sstevel@tonic-gate 	int instance;
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 	DBG1(D_ATTACH, NULL, "dip=%p\n", dip);
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 	switch (cmd) {
2847c478bd9Sstevel@tonic-gate 	case DDI_ATTACH:
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 		/*
2877c478bd9Sstevel@tonic-gate 		 * Allocate soft state for this instance.
2887c478bd9Sstevel@tonic-gate 		 */
2897c478bd9Sstevel@tonic-gate 		instance = ddi_get_instance(dip);
2907c478bd9Sstevel@tonic-gate 		if (ddi_soft_state_zalloc(per_ebus_state, instance)
2917c478bd9Sstevel@tonic-gate 		    != DDI_SUCCESS) {
2927c478bd9Sstevel@tonic-gate 			DBG(D_ATTACH, NULL, "failed to alloc soft state\n");
2937c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
2947c478bd9Sstevel@tonic-gate 		}
2957c478bd9Sstevel@tonic-gate 		ebus_p = get_ebus_soft_state(instance);
2967c478bd9Sstevel@tonic-gate 		ebus_p->dip = dip;
2977c478bd9Sstevel@tonic-gate 		mutex_init(&ebus_p->ebus_mutex, NULL, MUTEX_DRIVER, NULL);
2987c478bd9Sstevel@tonic-gate 		ebus_p->ebus_soft_state = EBUS_SOFT_STATE_CLOSED;
2997c478bd9Sstevel@tonic-gate 
300c4485e78Sjasonwu 		ebus_get_cells_prop(ebus_p);
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 		(void) ddi_prop_create(DDI_DEV_T_NONE, dip,
3037c478bd9Sstevel@tonic-gate 		    DDI_PROP_CANSLEEP, "no-dma-interrupt-sync", NULL, 0);
3047c478bd9Sstevel@tonic-gate 		/* Get our ranges property for mapping child registers. */
305c4485e78Sjasonwu 		if (ebus_get_ranges_prop(ebus_p) != DDI_SUCCESS) {
306c4485e78Sjasonwu 			goto attach_fail;
3077c478bd9Sstevel@tonic-gate 		}
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 		/*
3107c478bd9Sstevel@tonic-gate 		 * create minor node for devctl interfaces
3117c478bd9Sstevel@tonic-gate 		 */
3127c478bd9Sstevel@tonic-gate 		if (ddi_create_minor_node(dip, "devctl", S_IFCHR, instance,
3137c478bd9Sstevel@tonic-gate 		    DDI_NT_NEXUS, 0) != DDI_SUCCESS) {
314c4485e78Sjasonwu 			goto attach_fail;
3157c478bd9Sstevel@tonic-gate 		}
316c4485e78Sjasonwu 
317c4485e78Sjasonwu 		if (ebus_config(ebus_p) != DDI_SUCCESS) {
3187c478bd9Sstevel@tonic-gate 			ddi_remove_minor_node(dip, "devctl");
319c4485e78Sjasonwu 			goto attach_fail;
3207c478bd9Sstevel@tonic-gate 		}
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 		/*
3237c478bd9Sstevel@tonic-gate 		 * Make the pci_report_pmcap() call only for RIO
3247c478bd9Sstevel@tonic-gate 		 * implementations.
3257c478bd9Sstevel@tonic-gate 		 */
3267c478bd9Sstevel@tonic-gate 		if (IS_RIO(dip)) {
3277c478bd9Sstevel@tonic-gate 			(void) pci_report_pmcap(dip, PCI_PM_IDLESPEED,
3287c478bd9Sstevel@tonic-gate 			    (void *)EBUS_4MHZ);
3297c478bd9Sstevel@tonic-gate 		}
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 		/*
3327c478bd9Sstevel@tonic-gate 		 * Make the state as attached and report the device.
3337c478bd9Sstevel@tonic-gate 		 */
3347c478bd9Sstevel@tonic-gate 		ebus_p->state = ATTACHED;
3357c478bd9Sstevel@tonic-gate 		ddi_report_dev(dip);
3367c478bd9Sstevel@tonic-gate 		DBG(D_ATTACH, ebus_p, "returning\n");
337c4485e78Sjasonwu 		break;
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 	case DDI_RESUME:
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 		instance = ddi_get_instance(dip);
3427c478bd9Sstevel@tonic-gate 		ebus_p = get_ebus_soft_state(instance);
3437c478bd9Sstevel@tonic-gate 
344c4485e78Sjasonwu 		(void) ebus_config(ebus_p);
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate 		ebus_p->state = RESUMED;
347c4485e78Sjasonwu 		break;
3487c478bd9Sstevel@tonic-gate 	}
349c4485e78Sjasonwu 
350c4485e78Sjasonwu 	return (DDI_SUCCESS);
351c4485e78Sjasonwu 
352c4485e78Sjasonwu attach_fail:
353c4485e78Sjasonwu 	mutex_destroy(&ebus_p->ebus_mutex);
354c4485e78Sjasonwu 	free_ebus_soft_state(instance);
3557c478bd9Sstevel@tonic-gate 	return (DDI_FAILURE);
3567c478bd9Sstevel@tonic-gate }
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate /*
3597c478bd9Sstevel@tonic-gate  * detach entry point:
3607c478bd9Sstevel@tonic-gate  */
3617c478bd9Sstevel@tonic-gate static int
3627c478bd9Sstevel@tonic-gate ebus_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
3637c478bd9Sstevel@tonic-gate {
3647c478bd9Sstevel@tonic-gate 	int instance = ddi_get_instance(dip);
3657c478bd9Sstevel@tonic-gate 	ebus_devstate_t *ebus_p = get_ebus_soft_state(instance);
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 	switch (cmd) {
3687c478bd9Sstevel@tonic-gate 	case DDI_DETACH:
3697c478bd9Sstevel@tonic-gate 		DBG1(D_DETACH, ebus_p, "DDI_DETACH dip=%p\n", dip);
3707c478bd9Sstevel@tonic-gate 
371c4485e78Sjasonwu 		kmem_free(ebus_p->vrangep, ebus_p->vrange_len);
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 		ddi_remove_minor_node(dip, "devctl");
3747c478bd9Sstevel@tonic-gate 		mutex_destroy(&ebus_p->ebus_mutex);
3757c478bd9Sstevel@tonic-gate 		free_ebus_soft_state(instance);
376c4485e78Sjasonwu 		break;
3777c478bd9Sstevel@tonic-gate 	case DDI_SUSPEND:
3787c478bd9Sstevel@tonic-gate 		DBG1(D_DETACH, ebus_p, "DDI_SUSPEND dip=%p\n", dip);
3797c478bd9Sstevel@tonic-gate 		ebus_p->state = SUSPENDED;
380c4485e78Sjasonwu 		break;
381c4485e78Sjasonwu 	default:
382c4485e78Sjasonwu 		DBG(D_ATTACH, NULL,
383c4485e78Sjasonwu 		    "failed to recognize ebus detach command\n");
3847c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
3857c478bd9Sstevel@tonic-gate 	}
386c4485e78Sjasonwu 	return (DDI_SUCCESS);
387c4485e78Sjasonwu }
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate int
391c4485e78Sjasonwu ebus_get_ranges_prop(ebus_devstate_t *ebus_p)
3927c478bd9Sstevel@tonic-gate {
393c4485e78Sjasonwu 	if (ddi_getlongprop(DDI_DEV_T_ANY, ebus_p->dip, DDI_PROP_DONTPASS,
394c4485e78Sjasonwu 	    "ranges", (caddr_t)&ebus_p->vrangep, &ebus_p->vrange_len)
395c4485e78Sjasonwu 	    != DDI_SUCCESS) {
3967c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "Can't get %s ranges property",
3977c478bd9Sstevel@tonic-gate 		    ddi_get_name(ebus_p->dip));
3987c478bd9Sstevel@tonic-gate 		return (DDI_ME_REGSPEC_RANGE);
3997c478bd9Sstevel@tonic-gate 	}
4007c478bd9Sstevel@tonic-gate 
401c4485e78Sjasonwu 	ebus_p->vrange_cnt = ebus_p->vrange_len /
402c4485e78Sjasonwu 	    (ebus_p->ebus_paddr_cells + ebus_p->ebus_addr_cells +
403c4485e78Sjasonwu 	    ebus_p->ebus_psz_cells);
4047c478bd9Sstevel@tonic-gate 
405c4485e78Sjasonwu 	if (ebus_p->vrange_cnt == 0) {
406c4485e78Sjasonwu 		kmem_free(ebus_p->vrangep, ebus_p->vrange_len);
4077c478bd9Sstevel@tonic-gate 		DBG(D_ATTACH, NULL, "range is equal to zero\n");
4087c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
4097c478bd9Sstevel@tonic-gate 	}
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
4127c478bd9Sstevel@tonic-gate }
4137c478bd9Sstevel@tonic-gate 
414c4485e78Sjasonwu static void
415c4485e78Sjasonwu ebus_get_cells_prop(ebus_devstate_t *ebus_p)
4167c478bd9Sstevel@tonic-gate {
417c4485e78Sjasonwu 	dev_info_t *dip = ebus_p->dip;
418c4485e78Sjasonwu 	dev_info_t *pdip;
4197c478bd9Sstevel@tonic-gate 
420c4485e78Sjasonwu 	ebus_p->ebus_addr_cells = ddi_getprop(DDI_DEV_T_ANY,
421c4485e78Sjasonwu 	    dip, DDI_PROP_DONTPASS, "#address-cells", 2);
4227c478bd9Sstevel@tonic-gate 
423c4485e78Sjasonwu 	pdip = ddi_get_parent(dip);
424c4485e78Sjasonwu 	ebus_p->ebus_paddr_cells = ddi_getprop(DDI_DEV_T_ANY,
425c4485e78Sjasonwu 	    pdip, DDI_PROP_DONTPASS, "#address-cells", 2);
426c4485e78Sjasonwu 
427c4485e78Sjasonwu 	ASSERT((ebus_p->ebus_paddr_cells == 3) ||
428c4485e78Sjasonwu 	    (ebus_p->ebus_paddr_cells == 2));
429c4485e78Sjasonwu 
430c4485e78Sjasonwu 	ebus_p->ebus_sz_cells = ddi_getprop(DDI_DEV_T_ANY,
431c4485e78Sjasonwu 	    dip, DDI_PROP_DONTPASS, "#size-cells", 2);
432c4485e78Sjasonwu 	ebus_p->ebus_psz_cells = ddi_getprop(DDI_DEV_T_ANY,
433c4485e78Sjasonwu 	    pdip, DDI_PROP_DONTPASS, "#size-cells", 1);
434c4485e78Sjasonwu 
435c4485e78Sjasonwu 	/* XXX rootnex assumes 1 cell and does not respect #size-cells */
436c4485e78Sjasonwu 	if (ddi_root_node() == pdip)
437c4485e78Sjasonwu 		ebus_p->ebus_psz_cells = 1;
438c4485e78Sjasonwu 
439c4485e78Sjasonwu 	ASSERT((ebus_p->ebus_psz_cells == 2) ||
440c4485e78Sjasonwu 	    (ebus_p->ebus_psz_cells == 1));
441c4485e78Sjasonwu 
4427c478bd9Sstevel@tonic-gate }
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate /* bus driver entry points */
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate /*
4477c478bd9Sstevel@tonic-gate  * bus map entry point:
4487c478bd9Sstevel@tonic-gate  *
4497c478bd9Sstevel@tonic-gate  * 	if map request is for an rnumber
4507c478bd9Sstevel@tonic-gate  *		get the corresponding regspec from device node
4517c478bd9Sstevel@tonic-gate  * 	build a new regspec in our parent's format
4527c478bd9Sstevel@tonic-gate  *	build a new map_req with the new regspec
4537c478bd9Sstevel@tonic-gate  *	call up the tree to complete the mapping
4547c478bd9Sstevel@tonic-gate  */
4557c478bd9Sstevel@tonic-gate static int
4567c478bd9Sstevel@tonic-gate ebus_map(dev_info_t *dip, dev_info_t *rdip, ddi_map_req_t *mp,
4577c478bd9Sstevel@tonic-gate 	off_t off, off_t len, caddr_t *addrp)
4587c478bd9Sstevel@tonic-gate {
4597c478bd9Sstevel@tonic-gate 	ebus_devstate_t *ebus_p = get_ebus_soft_state(ddi_get_instance(dip));
4607c478bd9Sstevel@tonic-gate 	ebus_regspec_t *ebus_rp, *ebus_regs;
461c4485e78Sjasonwu 	vregspec_t vreg;
4627c478bd9Sstevel@tonic-gate 	ddi_map_req_t p_map_request;
4637c478bd9Sstevel@tonic-gate 	int rnumber, i, n;
4647c478bd9Sstevel@tonic-gate 	int rval = DDI_SUCCESS;
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 	/*
4677c478bd9Sstevel@tonic-gate 	 * Handle the mapping according to its type.
4687c478bd9Sstevel@tonic-gate 	 */
4697c478bd9Sstevel@tonic-gate 	DBG4(D_MAP, ebus_p, "rdip=%s%d: off=%x len=%x\n",
4707c478bd9Sstevel@tonic-gate 	    ddi_get_name(rdip), ddi_get_instance(rdip), off, len);
4717c478bd9Sstevel@tonic-gate 	switch (mp->map_type) {
4727c478bd9Sstevel@tonic-gate 	case DDI_MT_REGSPEC:
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate 		/*
4757c478bd9Sstevel@tonic-gate 		 * We assume the register specification is in ebus format.
4767c478bd9Sstevel@tonic-gate 		 * We must convert it into a PCI format regspec and pass
4777c478bd9Sstevel@tonic-gate 		 * the request to our parent.
4787c478bd9Sstevel@tonic-gate 		 */
4797c478bd9Sstevel@tonic-gate 		DBG3(D_MAP, ebus_p, "rdip=%s%d: REGSPEC - handlep=%p\n",
4807c478bd9Sstevel@tonic-gate 		    ddi_get_name(rdip), ddi_get_instance(rdip),
4817c478bd9Sstevel@tonic-gate 		    mp->map_handlep);
4827c478bd9Sstevel@tonic-gate 		ebus_rp = (ebus_regspec_t *)mp->map_obj.rp;
4837c478bd9Sstevel@tonic-gate 		break;
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate 	case DDI_MT_RNUMBER:
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate 		/*
4887c478bd9Sstevel@tonic-gate 		 * Get the "reg" property from the device node and convert
4897c478bd9Sstevel@tonic-gate 		 * it to our parent's format.
4907c478bd9Sstevel@tonic-gate 		 */
4917c478bd9Sstevel@tonic-gate 		rnumber = mp->map_obj.rnumber;
4927c478bd9Sstevel@tonic-gate 		DBG4(D_MAP, ebus_p, "rdip=%s%d: rnumber=%x handlep=%p\n",
4937c478bd9Sstevel@tonic-gate 		    ddi_get_name(rdip), ddi_get_instance(rdip),
4947c478bd9Sstevel@tonic-gate 		    rnumber, mp->map_handlep);
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate 		if (getprop(rdip, "reg", &ebus_regs, &i) != DDI_SUCCESS) {
4977c478bd9Sstevel@tonic-gate 			DBG(D_MAP, ebus_p, "can't get reg property\n");
4987c478bd9Sstevel@tonic-gate 			return (DDI_ME_RNUMBER_RANGE);
4997c478bd9Sstevel@tonic-gate 		}
500c4485e78Sjasonwu 
5017c478bd9Sstevel@tonic-gate 		n = i / sizeof (ebus_regspec_t);
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate 		if (rnumber < 0 || rnumber >= n) {
5047c478bd9Sstevel@tonic-gate 			DBG(D_MAP, ebus_p, "rnumber out of range\n");
5057c478bd9Sstevel@tonic-gate 			return (DDI_ME_RNUMBER_RANGE);
5067c478bd9Sstevel@tonic-gate 		}
5077c478bd9Sstevel@tonic-gate 		ebus_rp = &ebus_regs[rnumber];
5087c478bd9Sstevel@tonic-gate 		break;
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate 	default:
5117c478bd9Sstevel@tonic-gate 		return (DDI_ME_INVAL);
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate 	}
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate 	/* Adjust our reg property with offset and length */
5167c478bd9Sstevel@tonic-gate 	ebus_rp->addr_low += off;
5177c478bd9Sstevel@tonic-gate 	if (len)
5187c478bd9Sstevel@tonic-gate 		ebus_rp->size = len;
5197c478bd9Sstevel@tonic-gate 
520c4485e78Sjasonwu 	rval = ebus_apply_range(ebus_p, rdip, ebus_rp, &vreg);
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate 	if (mp->map_type == DDI_MT_RNUMBER)
5237c478bd9Sstevel@tonic-gate 		kmem_free(ebus_regs, i);
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 	if (rval != DDI_SUCCESS)
5267c478bd9Sstevel@tonic-gate 		return (rval);
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate 	p_map_request = *mp;
5297c478bd9Sstevel@tonic-gate 	p_map_request.map_type = DDI_MT_REGSPEC;
5307c478bd9Sstevel@tonic-gate 
531c4485e78Sjasonwu 	p_map_request.map_obj.rp = (struct regspec *)&vreg;
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate 	rval = ddi_map(dip, &p_map_request, 0, 0, addrp);
5347c478bd9Sstevel@tonic-gate 	DBG1(D_MAP, ebus_p, "parent returned %x\n", rval);
5357c478bd9Sstevel@tonic-gate 	return (rval);
5367c478bd9Sstevel@tonic-gate }
5377c478bd9Sstevel@tonic-gate 
538c4485e78Sjasonwu /*
539c4485e78Sjasonwu  * ebus_apply_range generically relocates child's regspec to
540c4485e78Sjasonwu  * parent's format according to ebus' range spec
541c4485e78Sjasonwu  *
542c4485e78Sjasonwu  * Assumptions:
543c4485e78Sjasonwu  * - rng_caddr_hi is the space type
544c4485e78Sjasonwu  * - rng_caddr_low is the base address
545c4485e78Sjasonwu  * - ebus address is 32 bit and ebus entirely lives between 0-4G of
546c4485e78Sjasonwu  *   parent space, so maths on preg/rng_cell_p[ebus_p->ebus_paddr_cells - 1],
547c4485e78Sjasonwu  *   preg_cell_p[i], rng_caddr_low and ebus_rp->size are sufficient.
548c4485e78Sjasonwu  */
5497c478bd9Sstevel@tonic-gate static int
5507c478bd9Sstevel@tonic-gate ebus_apply_range(ebus_devstate_t *ebus_p, dev_info_t *rdip,
551c4485e78Sjasonwu     ebus_regspec_t *ebus_rp, vregspec_t *rp) {
552c4485e78Sjasonwu 	int b, i;
553c4485e78Sjasonwu 	int nrange = ebus_p->vrange_cnt;
554c4485e78Sjasonwu 	uint32_t addr_offset, rng_caddr_hi, rng_caddr_low, rng_sz;
555c4485e78Sjasonwu 	uint32_t req_addr = ebus_rp->addr_low;
556c4485e78Sjasonwu 
557c4485e78Sjasonwu 	uint32_t *rng_cell_p = (uint32_t *)ebus_p->vrangep;
558c4485e78Sjasonwu 	int rng_rec_sz = ebus_p->ebus_paddr_cells + ebus_p->ebus_addr_cells +
559c4485e78Sjasonwu 	    ebus_p->ebus_sz_cells;
560c4485e78Sjasonwu 	uint32_t *preg_cell_p = (uint32_t *)rp;
561c4485e78Sjasonwu 	int preg_rec_sz = ebus_p->ebus_paddr_cells + ebus_p->ebus_psz_cells;
562c4485e78Sjasonwu 
5637c478bd9Sstevel@tonic-gate 	static char out_of_range[] =
5647c478bd9Sstevel@tonic-gate 	    "Out of range register specification from device node <%s>";
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate 	DBG3(D_MAP, ebus_p, "Range Matching Addr 0x%x.%x size 0x%x\n",
567c4485e78Sjasonwu 	    ebus_rp->addr_hi, req_addr, ebus_rp->size);
5687c478bd9Sstevel@tonic-gate 
569c4485e78Sjasonwu 	for (b = 0; b < nrange; b++, rng_cell_p += rng_rec_sz) {
5707c478bd9Sstevel@tonic-gate 
571c4485e78Sjasonwu 		rng_caddr_hi = rng_cell_p[0];
572c4485e78Sjasonwu 		rng_caddr_low = rng_cell_p[1];
573c4485e78Sjasonwu 		rng_sz = rng_cell_p[rng_rec_sz-1];
5747c478bd9Sstevel@tonic-gate 
575c4485e78Sjasonwu 		/* Check for correct space */
576c4485e78Sjasonwu 		if (ebus_rp->addr_hi != rng_caddr_hi)
577c4485e78Sjasonwu 			continue;
578c4485e78Sjasonwu 
579c4485e78Sjasonwu 		/* Detect whether request entirely fits within a range */
580c4485e78Sjasonwu 		if (req_addr < rng_caddr_low)
581c4485e78Sjasonwu 			continue;
582c4485e78Sjasonwu 
583c4485e78Sjasonwu 		if ((req_addr + ebus_rp->size - 1)
584c4485e78Sjasonwu 		    > (rng_caddr_low + rng_sz - 1))
585c4485e78Sjasonwu 			continue;
586c4485e78Sjasonwu 
587c4485e78Sjasonwu 		addr_offset = req_addr - rng_caddr_low;
588c4485e78Sjasonwu 
589c4485e78Sjasonwu 		/* parent addr = child addr + offset from ranges */
590c4485e78Sjasonwu 		for (i = 0; i < preg_rec_sz; i++)
591c4485e78Sjasonwu 			preg_cell_p[i] = 0;
592c4485e78Sjasonwu 
593c4485e78Sjasonwu 		/* Copy the physical address */
594c4485e78Sjasonwu 		for (i = 0; i < ebus_p->ebus_paddr_cells; i++)
595c4485e78Sjasonwu 			preg_cell_p[i] = rng_cell_p[ebus_p->ebus_addr_cells+i];
596c4485e78Sjasonwu 
597c4485e78Sjasonwu 		preg_cell_p[ebus_p->ebus_paddr_cells-1] += addr_offset;
598c4485e78Sjasonwu 
599c4485e78Sjasonwu 		/* Copy the size */
600c4485e78Sjasonwu 		preg_cell_p[preg_rec_sz-1] = min(ebus_rp->size,
601c4485e78Sjasonwu 		    rng_sz - addr_offset);
602c4485e78Sjasonwu 
603c4485e78Sjasonwu #ifdef DEBUG
604c4485e78Sjasonwu 		ebus_vreg_dump(ebus_p, (vregspec_t *)preg_cell_p);
605c4485e78Sjasonwu #endif /* DEBUG */
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate 		break;
6087c478bd9Sstevel@tonic-gate 	}
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate 	if (b == nrange)  {
6117c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, out_of_range, ddi_get_name(rdip));
6127c478bd9Sstevel@tonic-gate 		return (DDI_ME_REGSPEC_RANGE);
6137c478bd9Sstevel@tonic-gate 	}
6147c478bd9Sstevel@tonic-gate 
615c4485e78Sjasonwu 	return (DDI_SUCCESS);
6167c478bd9Sstevel@tonic-gate }
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate static int
6197c478bd9Sstevel@tonic-gate ebus_name_child(dev_info_t *child, char *name, int namelen)
6207c478bd9Sstevel@tonic-gate {
6217c478bd9Sstevel@tonic-gate 	ebus_regspec_t *ebus_rp;
6227c478bd9Sstevel@tonic-gate 	int reglen;
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate 	/*
6257c478bd9Sstevel@tonic-gate 	 * Get the address portion of the node name based on the
6267c478bd9Sstevel@tonic-gate 	 * address/offset.
6277c478bd9Sstevel@tonic-gate 	 */
628a3282898Scth 	if (ddi_getlongprop(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS,
6297c478bd9Sstevel@tonic-gate 	    "reg", (caddr_t)&ebus_rp, &reglen) != DDI_SUCCESS) {
6307c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
6317c478bd9Sstevel@tonic-gate 	}
6327c478bd9Sstevel@tonic-gate 
6337c478bd9Sstevel@tonic-gate 	(void) snprintf(name, namelen, "%x,%x", ebus_rp->addr_hi,
6347c478bd9Sstevel@tonic-gate 	    ebus_rp->addr_low);
6357c478bd9Sstevel@tonic-gate 	kmem_free(ebus_rp, reglen);
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
6387c478bd9Sstevel@tonic-gate }
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate /*
6417c478bd9Sstevel@tonic-gate  * control ops entry point:
6427c478bd9Sstevel@tonic-gate  *
6437c478bd9Sstevel@tonic-gate  * Requests handled completely:
6447c478bd9Sstevel@tonic-gate  *	DDI_CTLOPS_INITCHILD
6457c478bd9Sstevel@tonic-gate  *	DDI_CTLOPS_UNINITCHILD
6467c478bd9Sstevel@tonic-gate  *	DDI_CTLOPS_REPORTDEV
6477c478bd9Sstevel@tonic-gate  *	DDI_CTLOPS_REGSIZE
6487c478bd9Sstevel@tonic-gate  *	DDI_CTLOPS_NREGS
6497c478bd9Sstevel@tonic-gate  *
6507c478bd9Sstevel@tonic-gate  * All others passed to parent.
6517c478bd9Sstevel@tonic-gate  */
6527c478bd9Sstevel@tonic-gate static int
6537c478bd9Sstevel@tonic-gate ebus_ctlops(dev_info_t *dip, dev_info_t *rdip,
6547c478bd9Sstevel@tonic-gate 	ddi_ctl_enum_t op, void *arg, void *result)
6557c478bd9Sstevel@tonic-gate {
6567c478bd9Sstevel@tonic-gate #ifdef DEBUG
6577c478bd9Sstevel@tonic-gate 	ebus_devstate_t *ebus_p = get_ebus_soft_state(ddi_get_instance(dip));
6587c478bd9Sstevel@tonic-gate #endif
6597c478bd9Sstevel@tonic-gate 	ebus_regspec_t *ebus_rp;
6607c478bd9Sstevel@tonic-gate 	int i, n;
6617c478bd9Sstevel@tonic-gate 	char name[10];
6627c478bd9Sstevel@tonic-gate 
6637c478bd9Sstevel@tonic-gate 	switch (op) {
6647c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_INITCHILD: {
6657c478bd9Sstevel@tonic-gate 		dev_info_t *child = (dev_info_t *)arg;
6667c478bd9Sstevel@tonic-gate 		/*
6677c478bd9Sstevel@tonic-gate 		 * Set the address portion of the node name based on the
6687c478bd9Sstevel@tonic-gate 		 * address/offset.
6697c478bd9Sstevel@tonic-gate 		 */
6707c478bd9Sstevel@tonic-gate 		DBG2(D_CTLOPS, ebus_p, "DDI_CTLOPS_INITCHILD: rdip=%s%d\n",
6717c478bd9Sstevel@tonic-gate 		    ddi_get_name(child), ddi_get_instance(child));
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate 		if (ebus_name_child(child, name, 10) != DDI_SUCCESS) {
6747c478bd9Sstevel@tonic-gate 			DBG(D_CTLOPS, ebus_p, "can't name child\n");
6757c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
6767c478bd9Sstevel@tonic-gate 		}
6777c478bd9Sstevel@tonic-gate 
6787c478bd9Sstevel@tonic-gate 		ddi_set_name_addr(child, name);
6797c478bd9Sstevel@tonic-gate 		ddi_set_parent_data(child, NULL);
6807c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
6817c478bd9Sstevel@tonic-gate 	}
6827c478bd9Sstevel@tonic-gate 
6837c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_UNINITCHILD:
6847c478bd9Sstevel@tonic-gate 		DBG2(D_CTLOPS, ebus_p, "DDI_CTLOPS_UNINITCHILD: rdip=%s%d\n",
6857c478bd9Sstevel@tonic-gate 		    ddi_get_name((dev_info_t *)arg),
6867c478bd9Sstevel@tonic-gate 		    ddi_get_instance((dev_info_t *)arg));
6877c478bd9Sstevel@tonic-gate 		ddi_set_name_addr((dev_info_t *)arg, NULL);
6887c478bd9Sstevel@tonic-gate 		ddi_remove_minor_node((dev_info_t *)arg, NULL);
6897c478bd9Sstevel@tonic-gate 		impl_rem_dev_props((dev_info_t *)arg);
6907c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_REPORTDEV:
6937c478bd9Sstevel@tonic-gate 
6947c478bd9Sstevel@tonic-gate 		DBG2(D_CTLOPS, ebus_p, "DDI_CTLOPS_REPORTDEV: rdip=%s%d\n",
6957c478bd9Sstevel@tonic-gate 		    ddi_get_name(rdip), ddi_get_instance(rdip));
6967c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "?%s%d at %s%d: offset %s\n",
6977c478bd9Sstevel@tonic-gate 		    ddi_driver_name(rdip), ddi_get_instance(rdip),
6987c478bd9Sstevel@tonic-gate 		    ddi_driver_name(dip), ddi_get_instance(dip),
6997c478bd9Sstevel@tonic-gate 		    ddi_get_name_addr(rdip));
7007c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_REGSIZE:
7037c478bd9Sstevel@tonic-gate 
7047c478bd9Sstevel@tonic-gate 		DBG2(D_CTLOPS, ebus_p, "DDI_CTLOPS_REGSIZE: rdip=%s%d\n",
7057c478bd9Sstevel@tonic-gate 		    ddi_get_name(rdip), ddi_get_instance(rdip));
7067c478bd9Sstevel@tonic-gate 		if (getprop(rdip, "reg", &ebus_rp, &i) != DDI_SUCCESS) {
7077c478bd9Sstevel@tonic-gate 			DBG(D_CTLOPS, ebus_p, "can't get reg property\n");
7087c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
7097c478bd9Sstevel@tonic-gate 		}
7107c478bd9Sstevel@tonic-gate 		n = i / sizeof (ebus_regspec_t);
7117c478bd9Sstevel@tonic-gate 		if (*(int *)arg < 0 || *(int *)arg >= n) {
7127c478bd9Sstevel@tonic-gate 			DBG(D_MAP, ebus_p, "rnumber out of range\n");
7137c478bd9Sstevel@tonic-gate 			kmem_free(ebus_rp, i);
7147c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
7157c478bd9Sstevel@tonic-gate 		}
7167c478bd9Sstevel@tonic-gate 		*((off_t *)result) = ebus_rp[*(int *)arg].size;
7177c478bd9Sstevel@tonic-gate 		kmem_free(ebus_rp, i);
7187c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
7197c478bd9Sstevel@tonic-gate 
7207c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_NREGS:
7217c478bd9Sstevel@tonic-gate 
7227c478bd9Sstevel@tonic-gate 		DBG2(D_CTLOPS, ebus_p, "DDI_CTLOPS_NREGS: rdip=%s%d\n",
7237c478bd9Sstevel@tonic-gate 		    ddi_get_name(rdip), ddi_get_instance(rdip));
7247c478bd9Sstevel@tonic-gate 		if (getprop(rdip, "reg", &ebus_rp, &i) != DDI_SUCCESS) {
7257c478bd9Sstevel@tonic-gate 			DBG(D_CTLOPS, ebus_p, "can't get reg property\n");
7267c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
7277c478bd9Sstevel@tonic-gate 		}
7287c478bd9Sstevel@tonic-gate 		*((uint_t *)result) = i / sizeof (ebus_regspec_t);
7297c478bd9Sstevel@tonic-gate 		kmem_free(ebus_rp, i);
7307c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
7317c478bd9Sstevel@tonic-gate 	}
7327c478bd9Sstevel@tonic-gate 
7337c478bd9Sstevel@tonic-gate 	/*
7347c478bd9Sstevel@tonic-gate 	 * Now pass the request up to our parent.
7357c478bd9Sstevel@tonic-gate 	 */
7367c478bd9Sstevel@tonic-gate 	DBG2(D_CTLOPS, ebus_p, "passing request to parent: rdip=%s%d\n",
7377c478bd9Sstevel@tonic-gate 	    ddi_get_name(rdip), ddi_get_instance(rdip));
7387c478bd9Sstevel@tonic-gate 	return (ddi_ctlops(dip, rdip, op, arg, result));
7397c478bd9Sstevel@tonic-gate }
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate struct ebus_string_to_pil {
7427c478bd9Sstevel@tonic-gate 	int8_t *string;
7437c478bd9Sstevel@tonic-gate 	uint32_t pil;
7447c478bd9Sstevel@tonic-gate };
7457c478bd9Sstevel@tonic-gate 
7467c478bd9Sstevel@tonic-gate static struct ebus_string_to_pil ebus_name_to_pil[] = {{"SUNW,CS4231", 9},
7477c478bd9Sstevel@tonic-gate 							{"audio", 9},
7487c478bd9Sstevel@tonic-gate 							{"fdthree", 8},
7497c478bd9Sstevel@tonic-gate 							{"floppy", 8},
7507c478bd9Sstevel@tonic-gate 							{"ecpp", 3},
7517c478bd9Sstevel@tonic-gate 							{"parallel", 3},
7527c478bd9Sstevel@tonic-gate 							{"su", 12},
7537c478bd9Sstevel@tonic-gate 							{"se", 12},
7547c478bd9Sstevel@tonic-gate 							{"serial", 12},
7557c478bd9Sstevel@tonic-gate 							{"power", 14}};
7567c478bd9Sstevel@tonic-gate 
7577c478bd9Sstevel@tonic-gate static struct ebus_string_to_pil ebus_device_type_to_pil[] = {{"serial", 12},
7587c478bd9Sstevel@tonic-gate 								{"block", 8}};
7597c478bd9Sstevel@tonic-gate 
7607c478bd9Sstevel@tonic-gate static int
7617c478bd9Sstevel@tonic-gate ebus_intr_ops(dev_info_t *dip, dev_info_t *rdip, ddi_intr_op_t intr_op,
7627c478bd9Sstevel@tonic-gate     ddi_intr_handle_impl_t *hdlp, void *result)
7637c478bd9Sstevel@tonic-gate {
7647c478bd9Sstevel@tonic-gate #ifdef DEBUG
7657c478bd9Sstevel@tonic-gate 	ebus_devstate_t *ebus_p = get_ebus_soft_state(ddi_get_instance(dip));
7667c478bd9Sstevel@tonic-gate #endif
7677c478bd9Sstevel@tonic-gate 	int32_t		i, max_children, max_device_types, len;
7687c478bd9Sstevel@tonic-gate 	char		*name_p, *device_type_p;
7697c478bd9Sstevel@tonic-gate 
770a195726fSgovinda 	DBG1(D_INTR, ebus_p, "ebus_p 0x%p\n", ebus_p);
7717c478bd9Sstevel@tonic-gate 
7727c478bd9Sstevel@tonic-gate 	/*
7737c478bd9Sstevel@tonic-gate 	 * NOTE: These ops below will never be supported in this nexus
7747c478bd9Sstevel@tonic-gate 	 * driver, hence they always return immediately.
7757c478bd9Sstevel@tonic-gate 	 */
7767c478bd9Sstevel@tonic-gate 	switch (intr_op) {
7777c478bd9Sstevel@tonic-gate 	case DDI_INTROP_GETCAP:
778a195726fSgovinda 		*(int *)result = DDI_INTR_FLAG_LEVEL;
7797c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
780fd715b00Srameshc 	case DDI_INTROP_SUPPORTED_TYPES:
781fd715b00Srameshc 		*(int *)result = i_ddi_get_intx_nintrs(rdip) ?
782fd715b00Srameshc 		    DDI_INTR_TYPE_FIXED : 0;
783fd715b00Srameshc 		return (DDI_SUCCESS);
7847c478bd9Sstevel@tonic-gate 	case DDI_INTROP_SETCAP:
7857c478bd9Sstevel@tonic-gate 	case DDI_INTROP_SETMASK:
7867c478bd9Sstevel@tonic-gate 	case DDI_INTROP_CLRMASK:
7877c478bd9Sstevel@tonic-gate 	case DDI_INTROP_GETPENDING:
7887c478bd9Sstevel@tonic-gate 		return (DDI_ENOTSUP);
7897c478bd9Sstevel@tonic-gate 	default:
7907c478bd9Sstevel@tonic-gate 		break;
7917c478bd9Sstevel@tonic-gate 	}
7927c478bd9Sstevel@tonic-gate 
793fd715b00Srameshc 	if (hdlp->ih_pri)
7947c478bd9Sstevel@tonic-gate 		goto done;
7957c478bd9Sstevel@tonic-gate 
7967c478bd9Sstevel@tonic-gate 	/*
7977c478bd9Sstevel@tonic-gate 	 * This is a hack to set the PIL for the devices under ebus.
7987c478bd9Sstevel@tonic-gate 	 * We first look up a device by it's specific name, if we can't
7997c478bd9Sstevel@tonic-gate 	 * match the name, we try and match it's device_type property.
8007c478bd9Sstevel@tonic-gate 	 * Lastly we default a PIL level of 1.
8017c478bd9Sstevel@tonic-gate 	 */
8027c478bd9Sstevel@tonic-gate 	name_p = ddi_node_name(rdip);
8037c478bd9Sstevel@tonic-gate 	max_children = sizeof (ebus_name_to_pil) /
8047c478bd9Sstevel@tonic-gate 	    sizeof (struct ebus_string_to_pil);
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate 	for (i = 0; i < max_children; i++) {
8077c478bd9Sstevel@tonic-gate 		if (strcmp(ebus_name_to_pil[i].string, name_p) == 0) {
8087c478bd9Sstevel@tonic-gate 			DBG2(D_INTR, ebus_p, "child name %s; match PIL %d\n",
8097c478bd9Sstevel@tonic-gate 			    ebus_name_to_pil[i].string,
8107c478bd9Sstevel@tonic-gate 			    ebus_name_to_pil[i].pil);
8117c478bd9Sstevel@tonic-gate 
812a195726fSgovinda 			hdlp->ih_pri = ebus_name_to_pil[i].pil;
8137c478bd9Sstevel@tonic-gate 			goto done;
8147c478bd9Sstevel@tonic-gate 		}
8157c478bd9Sstevel@tonic-gate 	}
8167c478bd9Sstevel@tonic-gate 
817a3282898Scth 	if (ddi_getlongprop(DDI_DEV_T_ANY, rdip, DDI_PROP_DONTPASS,
8187c478bd9Sstevel@tonic-gate 	    "device_type", (caddr_t)&device_type_p, &len) == DDI_SUCCESS) {
8197c478bd9Sstevel@tonic-gate 
8207c478bd9Sstevel@tonic-gate 		max_device_types = sizeof (ebus_device_type_to_pil) /
8217c478bd9Sstevel@tonic-gate 		    sizeof (struct ebus_string_to_pil);
8227c478bd9Sstevel@tonic-gate 
8237c478bd9Sstevel@tonic-gate 		for (i = 0; i < max_device_types; i++) {
8247c478bd9Sstevel@tonic-gate 			if (strcmp(ebus_device_type_to_pil[i].string,
8257c478bd9Sstevel@tonic-gate 			    device_type_p) == 0) {
8267c478bd9Sstevel@tonic-gate 				DBG2(D_INTR, ebus_p, "Device type %s; match "
8277c478bd9Sstevel@tonic-gate 				    "PIL %d\n", ebus_device_type_to_pil[i].
8287c478bd9Sstevel@tonic-gate 				    string, ebus_device_type_to_pil[i].pil);
8297c478bd9Sstevel@tonic-gate 
830a195726fSgovinda 				hdlp->ih_pri = ebus_device_type_to_pil[i].pil;
8317c478bd9Sstevel@tonic-gate 				break;
8327c478bd9Sstevel@tonic-gate 			}
8337c478bd9Sstevel@tonic-gate 		}
8347c478bd9Sstevel@tonic-gate 
8357c478bd9Sstevel@tonic-gate 		kmem_free(device_type_p, len);
8367c478bd9Sstevel@tonic-gate 	}
8377c478bd9Sstevel@tonic-gate 
8387c478bd9Sstevel@tonic-gate 	/*
8397c478bd9Sstevel@tonic-gate 	 * If we get here, we need to set a default value
8407c478bd9Sstevel@tonic-gate 	 * for the PIL.
8417c478bd9Sstevel@tonic-gate 	 */
842a195726fSgovinda 	if (hdlp->ih_pri == 0) {
843a195726fSgovinda 		hdlp->ih_pri = 1;
8447c478bd9Sstevel@tonic-gate 
8457c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d assigning default interrupt level %d "
846a195726fSgovinda 		    "for device %s%d", ddi_driver_name(dip),
847a195726fSgovinda 		    ddi_get_instance(dip), hdlp->ih_pri, ddi_driver_name(rdip),
848a195726fSgovinda 		    ddi_get_instance(rdip));
8497c478bd9Sstevel@tonic-gate 	}
8507c478bd9Sstevel@tonic-gate 
8517c478bd9Sstevel@tonic-gate done:
8527c478bd9Sstevel@tonic-gate 	/* Pass up the request to our parent. */
8537c478bd9Sstevel@tonic-gate 	return (i_ddi_intr_ops(dip, rdip, intr_op, hdlp, result));
8547c478bd9Sstevel@tonic-gate }
8557c478bd9Sstevel@tonic-gate 
856c4485e78Sjasonwu /*
857c4485e78Sjasonwu  * ebus_config: setup pci config space registers:
858c4485e78Sjasonwu  *     enable bus mastering, memory access and error reporting
859c4485e78Sjasonwu  */
8607c478bd9Sstevel@tonic-gate static int
8617c478bd9Sstevel@tonic-gate ebus_config(ebus_devstate_t *ebus_p)
8627c478bd9Sstevel@tonic-gate {
8637c478bd9Sstevel@tonic-gate 	ddi_acc_handle_t conf_handle;
8647c478bd9Sstevel@tonic-gate 	uint16_t comm;
865c4485e78Sjasonwu 	dev_info_t *dip = ebus_p->dip;
866c4485e78Sjasonwu 	char *devtype_str;
867c4485e78Sjasonwu 	int devtype_len;
868c4485e78Sjasonwu 
869c4485e78Sjasonwu 	if (ddi_getlongprop(DDI_DEV_T_ANY, ddi_get_parent(dip),
870c4485e78Sjasonwu 	    DDI_PROP_DONTPASS, "device_type", (caddr_t)&devtype_str,
871c4485e78Sjasonwu 	    &devtype_len) != DDI_SUCCESS) {
872c4485e78Sjasonwu 		cmn_err(CE_WARN, "Can't get %s device_type property",
873c4485e78Sjasonwu 		    ddi_get_name(ddi_get_parent(dip)));
874c4485e78Sjasonwu 
875c4485e78Sjasonwu 		return (DDI_FAILURE);
876c4485e78Sjasonwu 	}
877c4485e78Sjasonwu 
878c4485e78Sjasonwu 	comm = strcmp(devtype_str, "pci");
879c4485e78Sjasonwu 	kmem_free(devtype_str, devtype_len);
880c4485e78Sjasonwu 
881c4485e78Sjasonwu 	if (comm)
882c4485e78Sjasonwu 		return (DDI_SUCCESS);
8837c478bd9Sstevel@tonic-gate 
8847c478bd9Sstevel@tonic-gate 	/*
8857c478bd9Sstevel@tonic-gate 	 * Make sure the master enable and memory access enable
8867c478bd9Sstevel@tonic-gate 	 * bits are set in the config command register.
8877c478bd9Sstevel@tonic-gate 	 */
8887c478bd9Sstevel@tonic-gate 	if (pci_config_setup(ebus_p->dip, &conf_handle) != DDI_SUCCESS)
889c4485e78Sjasonwu 		return (DDI_FAILURE);
8907c478bd9Sstevel@tonic-gate 
8917c478bd9Sstevel@tonic-gate 	comm = pci_config_get16(conf_handle, PCI_CONF_COMM),
8927c478bd9Sstevel@tonic-gate #ifdef DEBUG
8937c478bd9Sstevel@tonic-gate 	    DBG1(D_MAP, ebus_p, "command register was 0x%x\n", comm);
8947c478bd9Sstevel@tonic-gate #endif
8957c478bd9Sstevel@tonic-gate 	comm |= (PCI_COMM_ME|PCI_COMM_MAE|PCI_COMM_SERR_ENABLE|
8967c478bd9Sstevel@tonic-gate 	    PCI_COMM_PARITY_DETECT);
8977c478bd9Sstevel@tonic-gate 	pci_config_put16(conf_handle, PCI_CONF_COMM, comm),
8987c478bd9Sstevel@tonic-gate #ifdef DEBUG
8997c478bd9Sstevel@tonic-gate 	    DBG1(D_MAP, ebus_p, "command register is now 0x%x\n", comm);
9007c478bd9Sstevel@tonic-gate #endif
9017c478bd9Sstevel@tonic-gate 	pci_config_put8(conf_handle, PCI_CONF_CACHE_LINESZ,
9027c478bd9Sstevel@tonic-gate 	    (uchar_t)ebus_cache_line_size);
9037c478bd9Sstevel@tonic-gate 	pci_config_put8(conf_handle, PCI_CONF_LATENCY_TIMER,
9047c478bd9Sstevel@tonic-gate 	    (uchar_t)ebus_latency_timer);
9057c478bd9Sstevel@tonic-gate 	pci_config_teardown(&conf_handle);
906c4485e78Sjasonwu 	return (DDI_SUCCESS);
9077c478bd9Sstevel@tonic-gate }
9087c478bd9Sstevel@tonic-gate 
9097c478bd9Sstevel@tonic-gate #ifdef DEBUG
9107c478bd9Sstevel@tonic-gate extern void prom_printf(const char *, ...);
9117c478bd9Sstevel@tonic-gate 
9127c478bd9Sstevel@tonic-gate static void
9137c478bd9Sstevel@tonic-gate ebus_debug(uint_t flag, ebus_devstate_t *ebus_p, char *fmt,
9147c478bd9Sstevel@tonic-gate 	uintptr_t a1, uintptr_t a2, uintptr_t a3, uintptr_t a4, uintptr_t a5)
9157c478bd9Sstevel@tonic-gate {
9167c478bd9Sstevel@tonic-gate 	char *s;
9177c478bd9Sstevel@tonic-gate 
9187c478bd9Sstevel@tonic-gate 	if (ebus_debug_flags & flag) {
9197c478bd9Sstevel@tonic-gate 		switch (flag) {
9207c478bd9Sstevel@tonic-gate 		case D_ATTACH:
9217c478bd9Sstevel@tonic-gate 			s = "attach"; break;
9227c478bd9Sstevel@tonic-gate 		case D_DETACH:
9237c478bd9Sstevel@tonic-gate 			s = "detach"; break;
9247c478bd9Sstevel@tonic-gate 		case D_MAP:
9257c478bd9Sstevel@tonic-gate 			s = "map"; break;
9267c478bd9Sstevel@tonic-gate 		case D_CTLOPS:
9277c478bd9Sstevel@tonic-gate 			s = "ctlops"; break;
9287c478bd9Sstevel@tonic-gate 		case D_INTR:
9297c478bd9Sstevel@tonic-gate 			s = "intr"; break;
9307c478bd9Sstevel@tonic-gate 		}
9317c478bd9Sstevel@tonic-gate 		if (ebus_p)
9327c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT, "%s%d: %s: ",
9337c478bd9Sstevel@tonic-gate 			    ddi_get_name(ebus_p->dip),
9347c478bd9Sstevel@tonic-gate 			    ddi_get_instance(ebus_p->dip), s);
9357c478bd9Sstevel@tonic-gate 		else
9367c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT, "ebus: ");
9377c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, fmt, a1, a2, a3, a4, a5);
9387c478bd9Sstevel@tonic-gate 	}
9397c478bd9Sstevel@tonic-gate }
940c4485e78Sjasonwu 
941c4485e78Sjasonwu static void
942c4485e78Sjasonwu ebus_vreg_dump(ebus_devstate_t *ebus_p, vregspec_t *rp)
943c4485e78Sjasonwu {
944c4485e78Sjasonwu 	if (ebus_p->ebus_paddr_cells == 3) {
945c4485e78Sjasonwu 		DBG5(D_MAP, ebus_p, "(%x,%x,%x)(%x,%x)\n",
946c4485e78Sjasonwu 		    rp->pci_regspec.pci_phys_hi,
947c4485e78Sjasonwu 		    rp->pci_regspec.pci_phys_mid,
948c4485e78Sjasonwu 		    rp->pci_regspec.pci_phys_low,
949c4485e78Sjasonwu 		    rp->pci_regspec.pci_size_hi,
950c4485e78Sjasonwu 		    rp->pci_regspec.pci_size_low);
951c4485e78Sjasonwu 	} else if (ebus_p->ebus_paddr_cells == 2) {
952c4485e78Sjasonwu 		DBG3(D_MAP, ebus_p, "%x,%x,%x\n",
953c4485e78Sjasonwu 		    rp->jbus_regspec.regspec_bustype,
954c4485e78Sjasonwu 		    rp->jbus_regspec.regspec_addr,
955c4485e78Sjasonwu 		    rp->jbus_regspec.regspec_size);
956c4485e78Sjasonwu 	}
957c4485e78Sjasonwu }
958c4485e78Sjasonwu #endif /* DEBUG */
9597c478bd9Sstevel@tonic-gate 
9607c478bd9Sstevel@tonic-gate /* ARGSUSED3 */
9617c478bd9Sstevel@tonic-gate static int
9627c478bd9Sstevel@tonic-gate ebus_open(dev_t *devp, int flags, int otyp, cred_t *credp)
9637c478bd9Sstevel@tonic-gate {
9647c478bd9Sstevel@tonic-gate 	ebus_devstate_t *ebus_p;
9657c478bd9Sstevel@tonic-gate 
9667c478bd9Sstevel@tonic-gate 	/*
9677c478bd9Sstevel@tonic-gate 	 * Make sure the open is for the right file type.
9687c478bd9Sstevel@tonic-gate 	 */
9697c478bd9Sstevel@tonic-gate 	if (otyp != OTYP_CHR)
9707c478bd9Sstevel@tonic-gate 		return (EINVAL);
9717c478bd9Sstevel@tonic-gate 
9727c478bd9Sstevel@tonic-gate 	/*
9737c478bd9Sstevel@tonic-gate 	 * Get the soft state structure for the device.
9747c478bd9Sstevel@tonic-gate 	 */
9757c478bd9Sstevel@tonic-gate 	ebus_p = get_ebus_soft_state(getminor(*devp));
9767c478bd9Sstevel@tonic-gate 	if (ebus_p == NULL)
9777c478bd9Sstevel@tonic-gate 		return (ENXIO);
9787c478bd9Sstevel@tonic-gate 
9797c478bd9Sstevel@tonic-gate 	/*
9807c478bd9Sstevel@tonic-gate 	 * Handle the open by tracking the device state.
9817c478bd9Sstevel@tonic-gate 	 */
9827c478bd9Sstevel@tonic-gate 	mutex_enter(&ebus_p->ebus_mutex);
9837c478bd9Sstevel@tonic-gate 	if (flags & FEXCL) {
9847c478bd9Sstevel@tonic-gate 		if (ebus_p->ebus_soft_state != EBUS_SOFT_STATE_CLOSED) {
9857c478bd9Sstevel@tonic-gate 			mutex_exit(&ebus_p->ebus_mutex);
9867c478bd9Sstevel@tonic-gate 			return (EBUSY);
9877c478bd9Sstevel@tonic-gate 		}
9887c478bd9Sstevel@tonic-gate 		ebus_p->ebus_soft_state = EBUS_SOFT_STATE_OPEN_EXCL;
9897c478bd9Sstevel@tonic-gate 	} else {
9907c478bd9Sstevel@tonic-gate 		if (ebus_p->ebus_soft_state == EBUS_SOFT_STATE_OPEN_EXCL) {
9917c478bd9Sstevel@tonic-gate 			mutex_exit(&ebus_p->ebus_mutex);
9927c478bd9Sstevel@tonic-gate 			return (EBUSY);
9937c478bd9Sstevel@tonic-gate 		}
9947c478bd9Sstevel@tonic-gate 		ebus_p->ebus_soft_state = EBUS_SOFT_STATE_OPEN;
9957c478bd9Sstevel@tonic-gate 	}
9967c478bd9Sstevel@tonic-gate 	mutex_exit(&ebus_p->ebus_mutex);
9977c478bd9Sstevel@tonic-gate 	return (0);
9987c478bd9Sstevel@tonic-gate }
9997c478bd9Sstevel@tonic-gate 
10007c478bd9Sstevel@tonic-gate 
10017c478bd9Sstevel@tonic-gate /* ARGSUSED */
10027c478bd9Sstevel@tonic-gate static int
10037c478bd9Sstevel@tonic-gate ebus_close(dev_t dev, int flags, int otyp, cred_t *credp)
10047c478bd9Sstevel@tonic-gate {
10057c478bd9Sstevel@tonic-gate 	ebus_devstate_t *ebus_p;
10067c478bd9Sstevel@tonic-gate 
10077c478bd9Sstevel@tonic-gate 	if (otyp != OTYP_CHR)
10087c478bd9Sstevel@tonic-gate 		return (EINVAL);
10097c478bd9Sstevel@tonic-gate 
10107c478bd9Sstevel@tonic-gate 	ebus_p = get_ebus_soft_state(getminor(dev));
10117c478bd9Sstevel@tonic-gate 	if (ebus_p == NULL)
10127c478bd9Sstevel@tonic-gate 		return (ENXIO);
10137c478bd9Sstevel@tonic-gate 
10147c478bd9Sstevel@tonic-gate 	mutex_enter(&ebus_p->ebus_mutex);
10157c478bd9Sstevel@tonic-gate 	ebus_p->ebus_soft_state = EBUS_SOFT_STATE_CLOSED;
10167c478bd9Sstevel@tonic-gate 	mutex_exit(&ebus_p->ebus_mutex);
10177c478bd9Sstevel@tonic-gate 	return (0);
10187c478bd9Sstevel@tonic-gate }
10197c478bd9Sstevel@tonic-gate 
10207c478bd9Sstevel@tonic-gate 
10217c478bd9Sstevel@tonic-gate /*
10227c478bd9Sstevel@tonic-gate  * ebus_ioctl: devctl hotplug controls
10237c478bd9Sstevel@tonic-gate  */
10247c478bd9Sstevel@tonic-gate /* ARGSUSED */
10257c478bd9Sstevel@tonic-gate static int
10267c478bd9Sstevel@tonic-gate ebus_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp,
10277c478bd9Sstevel@tonic-gate 	int *rvalp)
10287c478bd9Sstevel@tonic-gate {
10297c478bd9Sstevel@tonic-gate 	ebus_devstate_t *ebus_p;
10307c478bd9Sstevel@tonic-gate 	dev_info_t *self;
10317c478bd9Sstevel@tonic-gate 	struct devctl_iocdata *dcp;
10327c478bd9Sstevel@tonic-gate 	uint_t bus_state;
10337c478bd9Sstevel@tonic-gate 	int rv = 0;
10347c478bd9Sstevel@tonic-gate 
10357c478bd9Sstevel@tonic-gate 	ebus_p = get_ebus_soft_state(getminor(dev));
10367c478bd9Sstevel@tonic-gate 	if (ebus_p == NULL)
10377c478bd9Sstevel@tonic-gate 		return (ENXIO);
10387c478bd9Sstevel@tonic-gate 
10397c478bd9Sstevel@tonic-gate 	self = ebus_p->dip;
10407c478bd9Sstevel@tonic-gate 
10417c478bd9Sstevel@tonic-gate 	/*
10427c478bd9Sstevel@tonic-gate 	 * We can use the generic implementation for these ioctls
10437c478bd9Sstevel@tonic-gate 	 */
10447c478bd9Sstevel@tonic-gate 	switch (cmd) {
10457c478bd9Sstevel@tonic-gate 	case DEVCTL_DEVICE_GETSTATE:
10467c478bd9Sstevel@tonic-gate 	case DEVCTL_DEVICE_ONLINE:
10477c478bd9Sstevel@tonic-gate 	case DEVCTL_DEVICE_OFFLINE:
10487c478bd9Sstevel@tonic-gate 	case DEVCTL_BUS_GETSTATE:
10497c478bd9Sstevel@tonic-gate 		return (ndi_devctl_ioctl(self, cmd, arg, mode, 0));
10507c478bd9Sstevel@tonic-gate 	}
10517c478bd9Sstevel@tonic-gate 
10527c478bd9Sstevel@tonic-gate 	/*
10537c478bd9Sstevel@tonic-gate 	 * read devctl ioctl data
10547c478bd9Sstevel@tonic-gate 	 */
10557c478bd9Sstevel@tonic-gate 	if (ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS)
10567c478bd9Sstevel@tonic-gate 		return (EFAULT);
10577c478bd9Sstevel@tonic-gate 
10587c478bd9Sstevel@tonic-gate 	switch (cmd) {
10597c478bd9Sstevel@tonic-gate 
10607c478bd9Sstevel@tonic-gate 	case DEVCTL_DEVICE_RESET:
10617c478bd9Sstevel@tonic-gate 		rv = ENOTSUP;
10627c478bd9Sstevel@tonic-gate 		break;
10637c478bd9Sstevel@tonic-gate 
10647c478bd9Sstevel@tonic-gate 	case DEVCTL_BUS_QUIESCE:
10657c478bd9Sstevel@tonic-gate 		if (ndi_get_bus_state(self, &bus_state) == NDI_SUCCESS)
10667c478bd9Sstevel@tonic-gate 			if (bus_state == BUS_QUIESCED)
10677c478bd9Sstevel@tonic-gate 				break;
10687c478bd9Sstevel@tonic-gate 		(void) ndi_set_bus_state(self, BUS_QUIESCED);
10697c478bd9Sstevel@tonic-gate 		break;
10707c478bd9Sstevel@tonic-gate 
10717c478bd9Sstevel@tonic-gate 	case DEVCTL_BUS_UNQUIESCE:
10727c478bd9Sstevel@tonic-gate 		if (ndi_get_bus_state(self, &bus_state) == NDI_SUCCESS)
10737c478bd9Sstevel@tonic-gate 			if (bus_state == BUS_ACTIVE)
10747c478bd9Sstevel@tonic-gate 				break;
10757c478bd9Sstevel@tonic-gate 		(void) ndi_set_bus_state(self, BUS_ACTIVE);
10767c478bd9Sstevel@tonic-gate 		break;
10777c478bd9Sstevel@tonic-gate 
10787c478bd9Sstevel@tonic-gate 	case DEVCTL_BUS_RESET:
10797c478bd9Sstevel@tonic-gate 		rv = ENOTSUP;
10807c478bd9Sstevel@tonic-gate 		break;
10817c478bd9Sstevel@tonic-gate 
10827c478bd9Sstevel@tonic-gate 	case DEVCTL_BUS_RESETALL:
10837c478bd9Sstevel@tonic-gate 		rv = ENOTSUP;
10847c478bd9Sstevel@tonic-gate 		break;
10857c478bd9Sstevel@tonic-gate 
10867c478bd9Sstevel@tonic-gate 	default:
10877c478bd9Sstevel@tonic-gate 		rv = ENOTTY;
10887c478bd9Sstevel@tonic-gate 	}
10897c478bd9Sstevel@tonic-gate 
10907c478bd9Sstevel@tonic-gate 	ndi_dc_freehdl(dcp);
10917c478bd9Sstevel@tonic-gate 	return (rv);
10927c478bd9Sstevel@tonic-gate }
1093