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