xref: /onnv-gate/usr/src/uts/sun4/io/ebus.c (revision 3920)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*3920Srameshc  * Common Development and Distribution License (the "License").
6*3920Srameshc  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*3920Srameshc  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <sys/types.h>
290Sstevel@tonic-gate #include <sys/conf.h>
300Sstevel@tonic-gate #include <sys/ddi.h>
310Sstevel@tonic-gate #include <sys/sunddi.h>
320Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
330Sstevel@tonic-gate #include <sys/ddi_subrdefs.h>
340Sstevel@tonic-gate #include <sys/pci.h>
350Sstevel@tonic-gate #include <sys/autoconf.h>
360Sstevel@tonic-gate #include <sys/cmn_err.h>
370Sstevel@tonic-gate #include <sys/errno.h>
380Sstevel@tonic-gate #include <sys/kmem.h>
390Sstevel@tonic-gate #include <sys/debug.h>
400Sstevel@tonic-gate #include <sys/sysmacros.h>
410Sstevel@tonic-gate #include <sys/ebus.h>
420Sstevel@tonic-gate #include <sys/open.h>
430Sstevel@tonic-gate #include <sys/stat.h>
440Sstevel@tonic-gate #include <sys/file.h>
450Sstevel@tonic-gate #include <sys/sunndi.h>
460Sstevel@tonic-gate 
470Sstevel@tonic-gate #ifdef DEBUG
480Sstevel@tonic-gate uint64_t ebus_debug_flags = 0;
490Sstevel@tonic-gate #endif
500Sstevel@tonic-gate 
510Sstevel@tonic-gate /*
520Sstevel@tonic-gate  * The values of the following variables are used to initialize
530Sstevel@tonic-gate  * the cache line size and latency timer registers in the ebus
540Sstevel@tonic-gate  * configuration header.  Variables are used instead of constants
550Sstevel@tonic-gate  * to allow tuning from the /etc/system file.
560Sstevel@tonic-gate  */
570Sstevel@tonic-gate static uint8_t ebus_cache_line_size = 0x10;	/* 64 bytes */
580Sstevel@tonic-gate static uint8_t ebus_latency_timer = 0x40;	/* 64 PCI cycles */
590Sstevel@tonic-gate 
600Sstevel@tonic-gate /*
610Sstevel@tonic-gate  * function prototypes for bus ops routines:
620Sstevel@tonic-gate  */
630Sstevel@tonic-gate static int
640Sstevel@tonic-gate ebus_map(dev_info_t *dip, dev_info_t *rdip, ddi_map_req_t *mp,
650Sstevel@tonic-gate 	off_t offset, off_t len, caddr_t *addrp);
660Sstevel@tonic-gate static int
670Sstevel@tonic-gate ebus_ctlops(dev_info_t *dip, dev_info_t *rdip,
680Sstevel@tonic-gate 	ddi_ctl_enum_t op, void *arg, void *result);
690Sstevel@tonic-gate static int
700Sstevel@tonic-gate ebus_intr_ops(dev_info_t *dip, dev_info_t *rdip, ddi_intr_op_t intr_op,
710Sstevel@tonic-gate     ddi_intr_handle_impl_t *hdlp, void *result);
720Sstevel@tonic-gate 
730Sstevel@tonic-gate /*
740Sstevel@tonic-gate  * function prototypes for dev ops routines:
750Sstevel@tonic-gate  */
760Sstevel@tonic-gate static int ebus_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
770Sstevel@tonic-gate static int ebus_detach(dev_info_t *dip, ddi_detach_cmd_t cmd);
780Sstevel@tonic-gate static int ebus_info(dev_info_t *dip, ddi_info_cmd_t infocmd,
790Sstevel@tonic-gate 	void *arg, void **result);
800Sstevel@tonic-gate 
810Sstevel@tonic-gate /*
820Sstevel@tonic-gate  * general function prototypes:
830Sstevel@tonic-gate  */
840Sstevel@tonic-gate static int ebus_config(ebus_devstate_t *ebus_p);
850Sstevel@tonic-gate static int ebus_apply_range(ebus_devstate_t *ebus_p, dev_info_t *rdip,
86558Sjasonwu     ebus_regspec_t *ebus_rp, vregspec_t *rp);
87558Sjasonwu int ebus_get_ranges_prop(ebus_devstate_t *ebus_p);
88558Sjasonwu static void ebus_get_cells_prop(ebus_devstate_t *ebus_p);
89558Sjasonwu static void ebus_vreg_dump(ebus_devstate_t *ebus_p, vregspec_t *rp);
900Sstevel@tonic-gate 
910Sstevel@tonic-gate #define	getprop(dip, name, addr, intp)		\
92506Scth 		ddi_getlongprop(DDI_DEV_T_ANY, (dip), DDI_PROP_DONTPASS, \
930Sstevel@tonic-gate 				(name), (caddr_t)(addr), (intp))
940Sstevel@tonic-gate 
950Sstevel@tonic-gate static int ebus_open(dev_t *devp, int flags, int otyp, cred_t *credp);
960Sstevel@tonic-gate static int ebus_close(dev_t dev, int flags, int otyp, cred_t *credp);
970Sstevel@tonic-gate static int ebus_ioctl(dev_t dev, int cmd, intptr_t arg, int mode,
980Sstevel@tonic-gate 						cred_t *credp, int *rvalp);
990Sstevel@tonic-gate struct cb_ops ebus_cb_ops = {
1000Sstevel@tonic-gate 	ebus_open,			/* open */
1010Sstevel@tonic-gate 	ebus_close,			/* close */
1020Sstevel@tonic-gate 	nodev,				/* strategy */
1030Sstevel@tonic-gate 	nodev,				/* print */
1040Sstevel@tonic-gate 	nodev,				/* dump */
1050Sstevel@tonic-gate 	nodev,				/* read */
1060Sstevel@tonic-gate 	nodev,				/* write */
1070Sstevel@tonic-gate 	ebus_ioctl,			/* ioctl */
1080Sstevel@tonic-gate 	nodev,				/* devmap */
1090Sstevel@tonic-gate 	nodev,				/* mmap */
1100Sstevel@tonic-gate 	nodev,				/* segmap */
1110Sstevel@tonic-gate 	nochpoll,			/* poll */
1120Sstevel@tonic-gate 	ddi_prop_op,			/* cb_prop_op */
1130Sstevel@tonic-gate 	NULL,				/* streamtab */
1140Sstevel@tonic-gate 	D_NEW | D_MP | D_HOTPLUG,	/* Driver compatibility flag */
1150Sstevel@tonic-gate 	CB_REV,				/* rev */
1160Sstevel@tonic-gate 	nodev,				/* int (*cb_aread)() */
1170Sstevel@tonic-gate 	nodev				/* int (*cb_awrite)() */
1180Sstevel@tonic-gate };
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate /*
1210Sstevel@tonic-gate  * bus ops and dev ops structures:
1220Sstevel@tonic-gate  */
1230Sstevel@tonic-gate static struct bus_ops ebus_bus_ops = {
1240Sstevel@tonic-gate 	BUSO_REV,
1250Sstevel@tonic-gate 	ebus_map,
1260Sstevel@tonic-gate 	NULL,
1270Sstevel@tonic-gate 	NULL,
1280Sstevel@tonic-gate 	NULL,
1290Sstevel@tonic-gate 	i_ddi_map_fault,
1300Sstevel@tonic-gate 	ddi_dma_map,
1310Sstevel@tonic-gate 	ddi_dma_allochdl,
1320Sstevel@tonic-gate 	ddi_dma_freehdl,
1330Sstevel@tonic-gate 	ddi_dma_bindhdl,
1340Sstevel@tonic-gate 	ddi_dma_unbindhdl,
1350Sstevel@tonic-gate 	ddi_dma_flush,
1360Sstevel@tonic-gate 	ddi_dma_win,
1370Sstevel@tonic-gate 	ddi_dma_mctl,
1380Sstevel@tonic-gate 	ebus_ctlops,
1390Sstevel@tonic-gate 	ddi_bus_prop_op,
1400Sstevel@tonic-gate 	ndi_busop_get_eventcookie,
1410Sstevel@tonic-gate 	ndi_busop_add_eventcall,
1420Sstevel@tonic-gate 	ndi_busop_remove_eventcall,
1430Sstevel@tonic-gate 	ndi_post_event,
1440Sstevel@tonic-gate 	0,
1450Sstevel@tonic-gate 	0,
1460Sstevel@tonic-gate 	0,
1470Sstevel@tonic-gate 	0,
1480Sstevel@tonic-gate 	0,
1490Sstevel@tonic-gate 	0,
1500Sstevel@tonic-gate 	0,
1510Sstevel@tonic-gate 	0,
1520Sstevel@tonic-gate 	ebus_intr_ops
1530Sstevel@tonic-gate };
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate static struct dev_ops ebus_ops = {
1560Sstevel@tonic-gate 	DEVO_REV,
1570Sstevel@tonic-gate 	0,
1580Sstevel@tonic-gate 	ebus_info,
1590Sstevel@tonic-gate 	nulldev,
1600Sstevel@tonic-gate 	nulldev,
1610Sstevel@tonic-gate 	ebus_attach,
1620Sstevel@tonic-gate 	ebus_detach,
1630Sstevel@tonic-gate 	nodev,
1640Sstevel@tonic-gate 	&ebus_cb_ops,
1650Sstevel@tonic-gate 	&ebus_bus_ops
1660Sstevel@tonic-gate };
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate /*
1690Sstevel@tonic-gate  * module definitions:
1700Sstevel@tonic-gate  */
1710Sstevel@tonic-gate #include <sys/modctl.h>
1720Sstevel@tonic-gate extern struct mod_ops mod_driverops;
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate static struct modldrv modldrv = {
1750Sstevel@tonic-gate 	&mod_driverops, 	/* Type of module.  This one is a driver */
1760Sstevel@tonic-gate 	"ebus nexus driver %I%", /* Name of module. */
1770Sstevel@tonic-gate 	&ebus_ops,		/* driver ops */
1780Sstevel@tonic-gate };
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate static struct modlinkage modlinkage = {
1810Sstevel@tonic-gate 	MODREV_1, (void *)&modldrv, NULL
1820Sstevel@tonic-gate };
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate /*
1850Sstevel@tonic-gate  * driver global data:
1860Sstevel@tonic-gate  */
1870Sstevel@tonic-gate static void *per_ebus_state;		/* per-ebus soft state pointer */
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate int
1910Sstevel@tonic-gate _init(void)
1920Sstevel@tonic-gate {
1930Sstevel@tonic-gate 	int e;
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 	/*
1960Sstevel@tonic-gate 	 * Initialize per-ebus soft state pointer.
1970Sstevel@tonic-gate 	 */
1980Sstevel@tonic-gate 	e = ddi_soft_state_init(&per_ebus_state, sizeof (ebus_devstate_t), 1);
1990Sstevel@tonic-gate 	if (e != 0)
2000Sstevel@tonic-gate 		return (e);
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate 	/*
2030Sstevel@tonic-gate 	 * Install the module.
2040Sstevel@tonic-gate 	 */
2050Sstevel@tonic-gate 	e = mod_install(&modlinkage);
2060Sstevel@tonic-gate 	if (e != 0)
2070Sstevel@tonic-gate 		ddi_soft_state_fini(&per_ebus_state);
2080Sstevel@tonic-gate 	return (e);
2090Sstevel@tonic-gate }
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate int
2120Sstevel@tonic-gate _fini(void)
2130Sstevel@tonic-gate {
2140Sstevel@tonic-gate 	int e;
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate 	/*
2170Sstevel@tonic-gate 	 * Remove the module.
2180Sstevel@tonic-gate 	 */
2190Sstevel@tonic-gate 	e = mod_remove(&modlinkage);
2200Sstevel@tonic-gate 	if (e != 0)
2210Sstevel@tonic-gate 		return (e);
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate 	/*
2240Sstevel@tonic-gate 	 * Free the soft state info.
2250Sstevel@tonic-gate 	 */
2260Sstevel@tonic-gate 	ddi_soft_state_fini(&per_ebus_state);
2270Sstevel@tonic-gate 	return (e);
2280Sstevel@tonic-gate }
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate int
2310Sstevel@tonic-gate _info(struct modinfo *modinfop)
2320Sstevel@tonic-gate {
2330Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
2340Sstevel@tonic-gate }
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate /* device driver entry points */
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate /*ARGSUSED*/
2390Sstevel@tonic-gate static int
2400Sstevel@tonic-gate ebus_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
2410Sstevel@tonic-gate {
2420Sstevel@tonic-gate 	ebus_devstate_t *ebus_p;	/* per ebus state pointer */
2430Sstevel@tonic-gate 	int instance;
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate 	instance = getminor((dev_t)arg);
2460Sstevel@tonic-gate 	ebus_p = get_ebus_soft_state(instance);
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate 	switch (infocmd) {
2490Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
250796Smathue 		*result = (void *)(uintptr_t)instance;
251558Sjasonwu 		break;
2520Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
2530Sstevel@tonic-gate 		if (ebus_p == NULL)
2540Sstevel@tonic-gate 			return (DDI_FAILURE);
2550Sstevel@tonic-gate 		*result = (void *)ebus_p->dip;
256558Sjasonwu 		break;
257558Sjasonwu 	default:
258558Sjasonwu 		return (DDI_FAILURE);
2590Sstevel@tonic-gate 	}
260558Sjasonwu 
261558Sjasonwu 	return (DDI_SUCCESS);
2620Sstevel@tonic-gate }
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate /*
2650Sstevel@tonic-gate  * attach entry point:
2660Sstevel@tonic-gate  *
2670Sstevel@tonic-gate  * normal attach:
2680Sstevel@tonic-gate  *
2690Sstevel@tonic-gate  *	create soft state structure (dip, reg, nreg and state fields)
2700Sstevel@tonic-gate  *	map in configuration header
2710Sstevel@tonic-gate  *	make sure device is properly configured
2720Sstevel@tonic-gate  *	report device
2730Sstevel@tonic-gate  */
2740Sstevel@tonic-gate static int
2750Sstevel@tonic-gate ebus_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
2760Sstevel@tonic-gate {
2770Sstevel@tonic-gate 	ebus_devstate_t *ebus_p;	/* per ebus state pointer */
2780Sstevel@tonic-gate 	int instance;
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate 	DBG1(D_ATTACH, NULL, "dip=%p\n", dip);
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate 	switch (cmd) {
2830Sstevel@tonic-gate 	case DDI_ATTACH:
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate 		/*
2860Sstevel@tonic-gate 		 * Allocate soft state for this instance.
2870Sstevel@tonic-gate 		 */
2880Sstevel@tonic-gate 		instance = ddi_get_instance(dip);
2890Sstevel@tonic-gate 		if (ddi_soft_state_zalloc(per_ebus_state, instance)
290558Sjasonwu 		    != DDI_SUCCESS) {
2910Sstevel@tonic-gate 			DBG(D_ATTACH, NULL, "failed to alloc soft state\n");
2920Sstevel@tonic-gate 			return (DDI_FAILURE);
2930Sstevel@tonic-gate 		}
2940Sstevel@tonic-gate 		ebus_p = get_ebus_soft_state(instance);
2950Sstevel@tonic-gate 		ebus_p->dip = dip;
2960Sstevel@tonic-gate 		mutex_init(&ebus_p->ebus_mutex, NULL, MUTEX_DRIVER, NULL);
2970Sstevel@tonic-gate 		ebus_p->ebus_soft_state = EBUS_SOFT_STATE_CLOSED;
2980Sstevel@tonic-gate 
299558Sjasonwu 		ebus_get_cells_prop(ebus_p);
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate 		(void) ddi_prop_create(DDI_DEV_T_NONE, dip,
302558Sjasonwu 		    DDI_PROP_CANSLEEP, "no-dma-interrupt-sync", NULL, 0);
3030Sstevel@tonic-gate 		/* Get our ranges property for mapping child registers. */
304558Sjasonwu 		if (ebus_get_ranges_prop(ebus_p) != DDI_SUCCESS) {
305558Sjasonwu 			goto attach_fail;
3060Sstevel@tonic-gate 		}
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate 		/*
3090Sstevel@tonic-gate 		 * create minor node for devctl interfaces
3100Sstevel@tonic-gate 		 */
3110Sstevel@tonic-gate 		if (ddi_create_minor_node(dip, "devctl", S_IFCHR, instance,
312558Sjasonwu 			DDI_NT_NEXUS, 0) != DDI_SUCCESS) {
313558Sjasonwu 			goto attach_fail;
3140Sstevel@tonic-gate 		}
315558Sjasonwu 
316558Sjasonwu 		if (ebus_config(ebus_p) != DDI_SUCCESS) {
317558Sjasonwu 			ddi_remove_minor_node(dip, "devctl");
318558Sjasonwu 			goto attach_fail;
3190Sstevel@tonic-gate 		}
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate 		/*
3220Sstevel@tonic-gate 		 * Make the pci_report_pmcap() call only for RIO
3230Sstevel@tonic-gate 		 * implementations.
3240Sstevel@tonic-gate 		 */
3250Sstevel@tonic-gate 		if (IS_RIO(dip)) {
3260Sstevel@tonic-gate 			(void) pci_report_pmcap(dip, PCI_PM_IDLESPEED,
3270Sstevel@tonic-gate 			    (void *)EBUS_4MHZ);
3280Sstevel@tonic-gate 		}
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate 		/*
3310Sstevel@tonic-gate 		 * Make the state as attached and report the device.
3320Sstevel@tonic-gate 		 */
3330Sstevel@tonic-gate 		ebus_p->state = ATTACHED;
3340Sstevel@tonic-gate 		ddi_report_dev(dip);
3350Sstevel@tonic-gate 		DBG(D_ATTACH, ebus_p, "returning\n");
336558Sjasonwu 		break;
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate 	case DDI_RESUME:
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate 		instance = ddi_get_instance(dip);
3410Sstevel@tonic-gate 		ebus_p = get_ebus_soft_state(instance);
3420Sstevel@tonic-gate 
343558Sjasonwu 		(void) ebus_config(ebus_p);
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate 		ebus_p->state = RESUMED;
346558Sjasonwu 		break;
3470Sstevel@tonic-gate 	}
348558Sjasonwu 
349558Sjasonwu 	return (DDI_SUCCESS);
350558Sjasonwu 
351558Sjasonwu attach_fail:
352558Sjasonwu 	mutex_destroy(&ebus_p->ebus_mutex);
353558Sjasonwu 	free_ebus_soft_state(instance);
3540Sstevel@tonic-gate 	return (DDI_FAILURE);
3550Sstevel@tonic-gate }
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate /*
3580Sstevel@tonic-gate  * detach entry point:
3590Sstevel@tonic-gate  */
3600Sstevel@tonic-gate static int
3610Sstevel@tonic-gate ebus_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
3620Sstevel@tonic-gate {
3630Sstevel@tonic-gate 	int instance = ddi_get_instance(dip);
3640Sstevel@tonic-gate 	ebus_devstate_t *ebus_p = get_ebus_soft_state(instance);
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate 	switch (cmd) {
3670Sstevel@tonic-gate 	case DDI_DETACH:
3680Sstevel@tonic-gate 		DBG1(D_DETACH, ebus_p, "DDI_DETACH dip=%p\n", dip);
3690Sstevel@tonic-gate 
370558Sjasonwu 		kmem_free(ebus_p->vrangep, ebus_p->vrange_len);
3710Sstevel@tonic-gate 
3720Sstevel@tonic-gate 		ddi_remove_minor_node(dip, "devctl");
3730Sstevel@tonic-gate 		mutex_destroy(&ebus_p->ebus_mutex);
3740Sstevel@tonic-gate 		free_ebus_soft_state(instance);
375558Sjasonwu 		break;
3760Sstevel@tonic-gate 	case DDI_SUSPEND:
3770Sstevel@tonic-gate 		DBG1(D_DETACH, ebus_p, "DDI_SUSPEND dip=%p\n", dip);
3780Sstevel@tonic-gate 		ebus_p->state = SUSPENDED;
379558Sjasonwu 		break;
380558Sjasonwu 	default:
381558Sjasonwu 		DBG(D_ATTACH, NULL,
382558Sjasonwu 		    "failed to recognize ebus detach command\n");
383558Sjasonwu 		return (DDI_FAILURE);
3840Sstevel@tonic-gate 	}
385558Sjasonwu 	return (DDI_SUCCESS);
3860Sstevel@tonic-gate }
3870Sstevel@tonic-gate 
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate int
390558Sjasonwu ebus_get_ranges_prop(ebus_devstate_t *ebus_p)
3910Sstevel@tonic-gate {
392558Sjasonwu 	if (ddi_getlongprop(DDI_DEV_T_ANY, ebus_p->dip, DDI_PROP_DONTPASS,
393558Sjasonwu 		"ranges", (caddr_t)&ebus_p->vrangep, &ebus_p->vrange_len)
394558Sjasonwu 	    != DDI_SUCCESS) {
395558Sjasonwu 		cmn_err(CE_WARN, "Can't get %s ranges property",
396558Sjasonwu 		    ddi_get_name(ebus_p->dip));
397558Sjasonwu 		return (DDI_ME_REGSPEC_RANGE);
398558Sjasonwu 	}
3990Sstevel@tonic-gate 
400558Sjasonwu 	ebus_p->vrange_cnt = ebus_p->vrange_len /
401558Sjasonwu 	    (ebus_p->ebus_paddr_cells + ebus_p->ebus_addr_cells +
402558Sjasonwu 		ebus_p->ebus_psz_cells);
4030Sstevel@tonic-gate 
404558Sjasonwu 	if (ebus_p->vrange_cnt == 0) {
405558Sjasonwu 		kmem_free(ebus_p->vrangep, ebus_p->vrange_len);
406558Sjasonwu 		DBG(D_ATTACH, NULL, "range is equal to zero\n");
4070Sstevel@tonic-gate 		return (DDI_FAILURE);
4080Sstevel@tonic-gate 	}
409558Sjasonwu 
410558Sjasonwu 	return (DDI_SUCCESS);
411558Sjasonwu }
412558Sjasonwu 
413558Sjasonwu static void
414558Sjasonwu ebus_get_cells_prop(ebus_devstate_t *ebus_p)
415558Sjasonwu {
416558Sjasonwu 	dev_info_t *dip = ebus_p->dip;
417558Sjasonwu 	dev_info_t *pdip;
418558Sjasonwu 
419558Sjasonwu 	ebus_p->ebus_addr_cells = ddi_getprop(DDI_DEV_T_ANY,
420558Sjasonwu 	    dip, DDI_PROP_DONTPASS, "#address-cells", 2);
421558Sjasonwu 
422558Sjasonwu 	pdip = ddi_get_parent(dip);
423558Sjasonwu 	ebus_p->ebus_paddr_cells = ddi_getprop(DDI_DEV_T_ANY,
424558Sjasonwu 	    pdip, DDI_PROP_DONTPASS, "#address-cells", 2);
425558Sjasonwu 
426558Sjasonwu 	ASSERT((ebus_p->ebus_paddr_cells == 3) ||
427558Sjasonwu 	    (ebus_p->ebus_paddr_cells == 2));
428558Sjasonwu 
429558Sjasonwu 	ebus_p->ebus_sz_cells = ddi_getprop(DDI_DEV_T_ANY,
430558Sjasonwu 	    dip, DDI_PROP_DONTPASS, "#size-cells", 2);
431558Sjasonwu 	ebus_p->ebus_psz_cells = ddi_getprop(DDI_DEV_T_ANY,
432558Sjasonwu 	    pdip, DDI_PROP_DONTPASS, "#size-cells", 1);
433558Sjasonwu 
434558Sjasonwu 	/* XXX rootnex assumes 1 cell and does not respect #size-cells */
435558Sjasonwu 	if (ddi_root_node() == pdip)
436558Sjasonwu 		ebus_p->ebus_psz_cells = 1;
437558Sjasonwu 
438558Sjasonwu 	ASSERT((ebus_p->ebus_psz_cells == 2) ||
439558Sjasonwu 	    (ebus_p->ebus_psz_cells == 1));
440558Sjasonwu 
4410Sstevel@tonic-gate }
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate /* bus driver entry points */
4440Sstevel@tonic-gate 
4450Sstevel@tonic-gate /*
4460Sstevel@tonic-gate  * bus map entry point:
4470Sstevel@tonic-gate  *
4480Sstevel@tonic-gate  * 	if map request is for an rnumber
4490Sstevel@tonic-gate  *		get the corresponding regspec from device node
4500Sstevel@tonic-gate  * 	build a new regspec in our parent's format
4510Sstevel@tonic-gate  *	build a new map_req with the new regspec
4520Sstevel@tonic-gate  *	call up the tree to complete the mapping
4530Sstevel@tonic-gate  */
4540Sstevel@tonic-gate static int
4550Sstevel@tonic-gate ebus_map(dev_info_t *dip, dev_info_t *rdip, ddi_map_req_t *mp,
4560Sstevel@tonic-gate 	off_t off, off_t len, caddr_t *addrp)
4570Sstevel@tonic-gate {
4580Sstevel@tonic-gate 	ebus_devstate_t *ebus_p = get_ebus_soft_state(ddi_get_instance(dip));
4590Sstevel@tonic-gate 	ebus_regspec_t *ebus_rp, *ebus_regs;
460558Sjasonwu 	vregspec_t vreg;
4610Sstevel@tonic-gate 	ddi_map_req_t p_map_request;
4620Sstevel@tonic-gate 	int rnumber, i, n;
4630Sstevel@tonic-gate 	int rval = DDI_SUCCESS;
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate 	/*
4660Sstevel@tonic-gate 	 * Handle the mapping according to its type.
4670Sstevel@tonic-gate 	 */
4680Sstevel@tonic-gate 	DBG4(D_MAP, ebus_p, "rdip=%s%d: off=%x len=%x\n",
4690Sstevel@tonic-gate 	    ddi_get_name(rdip), ddi_get_instance(rdip), off, len);
4700Sstevel@tonic-gate 	switch (mp->map_type) {
4710Sstevel@tonic-gate 	case DDI_MT_REGSPEC:
4720Sstevel@tonic-gate 
4730Sstevel@tonic-gate 		/*
4740Sstevel@tonic-gate 		 * We assume the register specification is in ebus format.
4750Sstevel@tonic-gate 		 * We must convert it into a PCI format regspec and pass
4760Sstevel@tonic-gate 		 * the request to our parent.
4770Sstevel@tonic-gate 		 */
4780Sstevel@tonic-gate 		DBG3(D_MAP, ebus_p, "rdip=%s%d: REGSPEC - handlep=%p\n",
4790Sstevel@tonic-gate 			ddi_get_name(rdip), ddi_get_instance(rdip),
4800Sstevel@tonic-gate 			mp->map_handlep);
4810Sstevel@tonic-gate 		ebus_rp = (ebus_regspec_t *)mp->map_obj.rp;
4820Sstevel@tonic-gate 		break;
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate 	case DDI_MT_RNUMBER:
4850Sstevel@tonic-gate 
4860Sstevel@tonic-gate 		/*
4870Sstevel@tonic-gate 		 * Get the "reg" property from the device node and convert
4880Sstevel@tonic-gate 		 * it to our parent's format.
4890Sstevel@tonic-gate 		 */
4900Sstevel@tonic-gate 		rnumber = mp->map_obj.rnumber;
4910Sstevel@tonic-gate 		DBG4(D_MAP, ebus_p, "rdip=%s%d: rnumber=%x handlep=%p\n",
4920Sstevel@tonic-gate 			ddi_get_name(rdip), ddi_get_instance(rdip),
4930Sstevel@tonic-gate 			rnumber, mp->map_handlep);
4940Sstevel@tonic-gate 
4950Sstevel@tonic-gate 		if (getprop(rdip, "reg", &ebus_regs, &i) != DDI_SUCCESS) {
4960Sstevel@tonic-gate 			DBG(D_MAP, ebus_p, "can't get reg property\n");
4970Sstevel@tonic-gate 			return (DDI_ME_RNUMBER_RANGE);
4980Sstevel@tonic-gate 		}
499558Sjasonwu 
5000Sstevel@tonic-gate 		n = i / sizeof (ebus_regspec_t);
5010Sstevel@tonic-gate 
5020Sstevel@tonic-gate 		if (rnumber < 0 || rnumber >= n) {
5030Sstevel@tonic-gate 			DBG(D_MAP, ebus_p, "rnumber out of range\n");
5040Sstevel@tonic-gate 			return (DDI_ME_RNUMBER_RANGE);
5050Sstevel@tonic-gate 		}
5060Sstevel@tonic-gate 		ebus_rp = &ebus_regs[rnumber];
5070Sstevel@tonic-gate 		break;
5080Sstevel@tonic-gate 
5090Sstevel@tonic-gate 	default:
5100Sstevel@tonic-gate 		return (DDI_ME_INVAL);
5110Sstevel@tonic-gate 
5120Sstevel@tonic-gate 	}
5130Sstevel@tonic-gate 
5140Sstevel@tonic-gate 	/* Adjust our reg property with offset and length */
5150Sstevel@tonic-gate 	ebus_rp->addr_low += off;
5160Sstevel@tonic-gate 	if (len)
5170Sstevel@tonic-gate 		ebus_rp->size = len;
5180Sstevel@tonic-gate 
519558Sjasonwu 	rval = ebus_apply_range(ebus_p, rdip, ebus_rp, &vreg);
5200Sstevel@tonic-gate 
5210Sstevel@tonic-gate 	if (mp->map_type == DDI_MT_RNUMBER)
5220Sstevel@tonic-gate 		kmem_free(ebus_regs, i);
5230Sstevel@tonic-gate 
5240Sstevel@tonic-gate 	if (rval != DDI_SUCCESS)
5250Sstevel@tonic-gate 		return (rval);
5260Sstevel@tonic-gate 
5270Sstevel@tonic-gate 	p_map_request = *mp;
5280Sstevel@tonic-gate 	p_map_request.map_type = DDI_MT_REGSPEC;
5290Sstevel@tonic-gate 
530558Sjasonwu 	p_map_request.map_obj.rp = (struct regspec *)&vreg;
5310Sstevel@tonic-gate 
5320Sstevel@tonic-gate 	rval = ddi_map(dip, &p_map_request, 0, 0, addrp);
5330Sstevel@tonic-gate 	DBG1(D_MAP, ebus_p, "parent returned %x\n", rval);
5340Sstevel@tonic-gate 	return (rval);
5350Sstevel@tonic-gate }
5360Sstevel@tonic-gate 
537558Sjasonwu /*
538558Sjasonwu  * ebus_apply_range generically relocates child's regspec to
539558Sjasonwu  * parent's format according to ebus' range spec
540558Sjasonwu  *
541558Sjasonwu  * Assumptions:
542558Sjasonwu  * - rng_caddr_hi is the space type
543558Sjasonwu  * - rng_caddr_low is the base address
544558Sjasonwu  * - ebus address is 32 bit and ebus entirely lives between 0-4G of
545558Sjasonwu  *   parent space, so maths on preg/rng_cell_p[ebus_p->ebus_paddr_cells - 1],
546558Sjasonwu  *   preg_cell_p[i], rng_caddr_low and ebus_rp->size are sufficient.
547558Sjasonwu  */
5480Sstevel@tonic-gate static int
5490Sstevel@tonic-gate ebus_apply_range(ebus_devstate_t *ebus_p, dev_info_t *rdip,
550558Sjasonwu     ebus_regspec_t *ebus_rp, vregspec_t *rp) {
551558Sjasonwu 	int b, i;
552558Sjasonwu 	int nrange = ebus_p->vrange_cnt;
553558Sjasonwu 	uint32_t addr_offset, rng_caddr_hi, rng_caddr_low, rng_sz;
554558Sjasonwu 	uint32_t req_addr = ebus_rp->addr_low;
555558Sjasonwu 
556558Sjasonwu 	uint32_t *rng_cell_p = (uint32_t *)ebus_p->vrangep;
557558Sjasonwu 	int rng_rec_sz = ebus_p->ebus_paddr_cells + ebus_p->ebus_addr_cells +
558558Sjasonwu 	    ebus_p->ebus_sz_cells;
559558Sjasonwu 	uint32_t *preg_cell_p = (uint32_t *)rp;
560558Sjasonwu 	int preg_rec_sz = ebus_p->ebus_paddr_cells + ebus_p->ebus_psz_cells;
561558Sjasonwu 
5620Sstevel@tonic-gate 	static char out_of_range[] =
5630Sstevel@tonic-gate 	    "Out of range register specification from device node <%s>";
5640Sstevel@tonic-gate 
5650Sstevel@tonic-gate 	DBG3(D_MAP, ebus_p, "Range Matching Addr 0x%x.%x size 0x%x\n",
566558Sjasonwu 	    ebus_rp->addr_hi, req_addr, ebus_rp->size);
567558Sjasonwu 
568558Sjasonwu 	for (b = 0; b < nrange; b++, rng_cell_p += rng_rec_sz) {
569558Sjasonwu 
570558Sjasonwu 		rng_caddr_hi = rng_cell_p[0];
571558Sjasonwu 		rng_caddr_low = rng_cell_p[1];
572558Sjasonwu 		rng_sz = rng_cell_p[rng_rec_sz-1];
5730Sstevel@tonic-gate 
574558Sjasonwu 		/* Check for correct space */
575558Sjasonwu 		if (ebus_rp->addr_hi != rng_caddr_hi)
576558Sjasonwu 			continue;
577558Sjasonwu 
578558Sjasonwu 		/* Detect whether request entirely fits within a range */
579558Sjasonwu 		if (req_addr < rng_caddr_low)
580558Sjasonwu 			continue;
581558Sjasonwu 
582558Sjasonwu 		if ((req_addr + ebus_rp->size - 1)
583558Sjasonwu 		    > (rng_caddr_low + rng_sz - 1))
584558Sjasonwu 			continue;
5850Sstevel@tonic-gate 
586558Sjasonwu 		addr_offset = req_addr - rng_caddr_low;
587558Sjasonwu 
588558Sjasonwu 		/* parent addr = child addr + offset from ranges */
589558Sjasonwu 		for (i = 0; i < preg_rec_sz; i++)
590558Sjasonwu 			preg_cell_p[i] = 0;
591558Sjasonwu 
592558Sjasonwu 		/* Copy the physical address */
593558Sjasonwu 		for (i = 0; i < ebus_p->ebus_paddr_cells; i++)
594558Sjasonwu 			preg_cell_p[i] = rng_cell_p[ebus_p->ebus_addr_cells+i];
5950Sstevel@tonic-gate 
596558Sjasonwu 		preg_cell_p[ebus_p->ebus_paddr_cells-1] += addr_offset;
597558Sjasonwu 
598558Sjasonwu 		/* Copy the size */
599558Sjasonwu 		preg_cell_p[preg_rec_sz-1] = min(ebus_rp->size,
600558Sjasonwu 		    rng_sz - addr_offset);
6010Sstevel@tonic-gate 
602558Sjasonwu #ifdef DEBUG
603558Sjasonwu 		ebus_vreg_dump(ebus_p, (vregspec_t *)preg_cell_p);
604558Sjasonwu #endif /* DEBUG */
605558Sjasonwu 
606558Sjasonwu 		break;
6070Sstevel@tonic-gate 	}
6080Sstevel@tonic-gate 
6090Sstevel@tonic-gate 	if (b == nrange)  {
6100Sstevel@tonic-gate 		cmn_err(CE_WARN, out_of_range, ddi_get_name(rdip));
6110Sstevel@tonic-gate 		return (DDI_ME_REGSPEC_RANGE);
6120Sstevel@tonic-gate 	}
6130Sstevel@tonic-gate 
614558Sjasonwu 	return (DDI_SUCCESS);
6150Sstevel@tonic-gate }
6160Sstevel@tonic-gate 
6170Sstevel@tonic-gate static int
6180Sstevel@tonic-gate ebus_name_child(dev_info_t *child, char *name, int namelen)
6190Sstevel@tonic-gate {
6200Sstevel@tonic-gate 	ebus_regspec_t *ebus_rp;
6210Sstevel@tonic-gate 	int reglen;
6220Sstevel@tonic-gate 
6230Sstevel@tonic-gate 	/*
6240Sstevel@tonic-gate 	 * Get the address portion of the node name based on the
6250Sstevel@tonic-gate 	 * address/offset.
6260Sstevel@tonic-gate 	 */
627506Scth 	if (ddi_getlongprop(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS,
6280Sstevel@tonic-gate 	    "reg", (caddr_t)&ebus_rp, &reglen) != DDI_SUCCESS) {
6290Sstevel@tonic-gate 		return (DDI_FAILURE);
6300Sstevel@tonic-gate 	}
6310Sstevel@tonic-gate 
6320Sstevel@tonic-gate 	(void) snprintf(name, namelen, "%x,%x", ebus_rp->addr_hi,
6330Sstevel@tonic-gate 	    ebus_rp->addr_low);
6340Sstevel@tonic-gate 	kmem_free(ebus_rp, reglen);
6350Sstevel@tonic-gate 
6360Sstevel@tonic-gate 	return (DDI_SUCCESS);
6370Sstevel@tonic-gate }
6380Sstevel@tonic-gate 
6390Sstevel@tonic-gate /*
6400Sstevel@tonic-gate  * control ops entry point:
6410Sstevel@tonic-gate  *
6420Sstevel@tonic-gate  * Requests handled completely:
6430Sstevel@tonic-gate  *	DDI_CTLOPS_INITCHILD
6440Sstevel@tonic-gate  *	DDI_CTLOPS_UNINITCHILD
6450Sstevel@tonic-gate  *	DDI_CTLOPS_REPORTDEV
6460Sstevel@tonic-gate  *	DDI_CTLOPS_REGSIZE
6470Sstevel@tonic-gate  *	DDI_CTLOPS_NREGS
6480Sstevel@tonic-gate  *
6490Sstevel@tonic-gate  * All others passed to parent.
6500Sstevel@tonic-gate  */
6510Sstevel@tonic-gate static int
6520Sstevel@tonic-gate ebus_ctlops(dev_info_t *dip, dev_info_t *rdip,
6530Sstevel@tonic-gate 	ddi_ctl_enum_t op, void *arg, void *result)
6540Sstevel@tonic-gate {
6550Sstevel@tonic-gate #ifdef DEBUG
6560Sstevel@tonic-gate 	ebus_devstate_t *ebus_p = get_ebus_soft_state(ddi_get_instance(dip));
6570Sstevel@tonic-gate #endif
6580Sstevel@tonic-gate 	ebus_regspec_t *ebus_rp;
6590Sstevel@tonic-gate 	int i, n;
6600Sstevel@tonic-gate 	char name[10];
6610Sstevel@tonic-gate 
6620Sstevel@tonic-gate 	switch (op) {
6630Sstevel@tonic-gate 	case DDI_CTLOPS_INITCHILD: {
6640Sstevel@tonic-gate 		dev_info_t *child = (dev_info_t *)arg;
6650Sstevel@tonic-gate 		/*
6660Sstevel@tonic-gate 		 * Set the address portion of the node name based on the
6670Sstevel@tonic-gate 		 * address/offset.
6680Sstevel@tonic-gate 		 */
6690Sstevel@tonic-gate 		DBG2(D_CTLOPS, ebus_p, "DDI_CTLOPS_INITCHILD: rdip=%s%d\n",
6700Sstevel@tonic-gate 		    ddi_get_name(child), ddi_get_instance(child));
6710Sstevel@tonic-gate 
6720Sstevel@tonic-gate 		if (ebus_name_child(child, name, 10) != DDI_SUCCESS) {
6730Sstevel@tonic-gate 			DBG(D_CTLOPS, ebus_p, "can't name child\n");
6740Sstevel@tonic-gate 			return (DDI_FAILURE);
6750Sstevel@tonic-gate 		}
6760Sstevel@tonic-gate 
6770Sstevel@tonic-gate 		ddi_set_name_addr(child, name);
6780Sstevel@tonic-gate 		ddi_set_parent_data(child, NULL);
6790Sstevel@tonic-gate 		return (DDI_SUCCESS);
6800Sstevel@tonic-gate 	}
6810Sstevel@tonic-gate 
6820Sstevel@tonic-gate 	case DDI_CTLOPS_UNINITCHILD:
6830Sstevel@tonic-gate 		DBG2(D_CTLOPS, ebus_p, "DDI_CTLOPS_UNINITCHILD: rdip=%s%d\n",
6840Sstevel@tonic-gate 			ddi_get_name((dev_info_t *)arg),
6850Sstevel@tonic-gate 			ddi_get_instance((dev_info_t *)arg));
6860Sstevel@tonic-gate 		ddi_set_name_addr((dev_info_t *)arg, NULL);
6870Sstevel@tonic-gate 		ddi_remove_minor_node((dev_info_t *)arg, NULL);
6880Sstevel@tonic-gate 		impl_rem_dev_props((dev_info_t *)arg);
6890Sstevel@tonic-gate 		return (DDI_SUCCESS);
6900Sstevel@tonic-gate 
6910Sstevel@tonic-gate 	case DDI_CTLOPS_REPORTDEV:
6920Sstevel@tonic-gate 
6930Sstevel@tonic-gate 		DBG2(D_CTLOPS, ebus_p, "DDI_CTLOPS_REPORTDEV: rdip=%s%d\n",
6940Sstevel@tonic-gate 			ddi_get_name(rdip), ddi_get_instance(rdip));
6950Sstevel@tonic-gate 		cmn_err(CE_CONT, "?%s%d at %s%d: offset %s\n",
6960Sstevel@tonic-gate 			ddi_driver_name(rdip), ddi_get_instance(rdip),
6970Sstevel@tonic-gate 			ddi_driver_name(dip), ddi_get_instance(dip),
6980Sstevel@tonic-gate 			ddi_get_name_addr(rdip));
6990Sstevel@tonic-gate 		return (DDI_SUCCESS);
7000Sstevel@tonic-gate 
7010Sstevel@tonic-gate 	case DDI_CTLOPS_REGSIZE:
7020Sstevel@tonic-gate 
7030Sstevel@tonic-gate 		DBG2(D_CTLOPS, ebus_p, "DDI_CTLOPS_REGSIZE: rdip=%s%d\n",
7040Sstevel@tonic-gate 			ddi_get_name(rdip), ddi_get_instance(rdip));
7050Sstevel@tonic-gate 		if (getprop(rdip, "reg", &ebus_rp, &i) != DDI_SUCCESS) {
7060Sstevel@tonic-gate 			DBG(D_CTLOPS, ebus_p, "can't get reg property\n");
7070Sstevel@tonic-gate 			return (DDI_FAILURE);
7080Sstevel@tonic-gate 		}
7090Sstevel@tonic-gate 		n = i / sizeof (ebus_regspec_t);
7100Sstevel@tonic-gate 		if (*(int *)arg < 0 || *(int *)arg >= n) {
7110Sstevel@tonic-gate 			DBG(D_MAP, ebus_p, "rnumber out of range\n");
7120Sstevel@tonic-gate 			kmem_free(ebus_rp, i);
7130Sstevel@tonic-gate 			return (DDI_FAILURE);
7140Sstevel@tonic-gate 		}
7150Sstevel@tonic-gate 		*((off_t *)result) = ebus_rp[*(int *)arg].size;
7160Sstevel@tonic-gate 		kmem_free(ebus_rp, i);
7170Sstevel@tonic-gate 		return (DDI_SUCCESS);
7180Sstevel@tonic-gate 
7190Sstevel@tonic-gate 	case DDI_CTLOPS_NREGS:
7200Sstevel@tonic-gate 
7210Sstevel@tonic-gate 		DBG2(D_CTLOPS, ebus_p, "DDI_CTLOPS_NREGS: rdip=%s%d\n",
7220Sstevel@tonic-gate 			ddi_get_name(rdip), ddi_get_instance(rdip));
7230Sstevel@tonic-gate 		if (getprop(rdip, "reg", &ebus_rp, &i) != DDI_SUCCESS) {
7240Sstevel@tonic-gate 			DBG(D_CTLOPS, ebus_p, "can't get reg property\n");
7250Sstevel@tonic-gate 			return (DDI_FAILURE);
7260Sstevel@tonic-gate 		}
7270Sstevel@tonic-gate 		*((uint_t *)result) = i / sizeof (ebus_regspec_t);
7280Sstevel@tonic-gate 		kmem_free(ebus_rp, i);
7290Sstevel@tonic-gate 		return (DDI_SUCCESS);
7300Sstevel@tonic-gate 	}
7310Sstevel@tonic-gate 
7320Sstevel@tonic-gate 	/*
7330Sstevel@tonic-gate 	 * Now pass the request up to our parent.
7340Sstevel@tonic-gate 	 */
7350Sstevel@tonic-gate 	DBG2(D_CTLOPS, ebus_p, "passing request to parent: rdip=%s%d\n",
7360Sstevel@tonic-gate 		ddi_get_name(rdip), ddi_get_instance(rdip));
7370Sstevel@tonic-gate 	return (ddi_ctlops(dip, rdip, op, arg, result));
7380Sstevel@tonic-gate }
7390Sstevel@tonic-gate 
7400Sstevel@tonic-gate struct ebus_string_to_pil {
7410Sstevel@tonic-gate 	int8_t *string;
7420Sstevel@tonic-gate 	uint32_t pil;
7430Sstevel@tonic-gate };
7440Sstevel@tonic-gate 
7450Sstevel@tonic-gate static struct ebus_string_to_pil ebus_name_to_pil[] = {{"SUNW,CS4231", 9},
7460Sstevel@tonic-gate 							{"audio", 9},
7470Sstevel@tonic-gate 							{"fdthree", 8},
7480Sstevel@tonic-gate 							{"floppy", 8},
7490Sstevel@tonic-gate 							{"ecpp", 3},
7500Sstevel@tonic-gate 							{"parallel", 3},
7510Sstevel@tonic-gate 							{"su", 12},
7520Sstevel@tonic-gate 							{"se", 12},
7530Sstevel@tonic-gate 							{"serial", 12},
7540Sstevel@tonic-gate 							{"power", 14}};
7550Sstevel@tonic-gate 
7560Sstevel@tonic-gate static struct ebus_string_to_pil ebus_device_type_to_pil[] = {{"serial", 12},
7570Sstevel@tonic-gate 								{"block", 8}};
7580Sstevel@tonic-gate 
7590Sstevel@tonic-gate static int
7600Sstevel@tonic-gate ebus_intr_ops(dev_info_t *dip, dev_info_t *rdip, ddi_intr_op_t intr_op,
7610Sstevel@tonic-gate     ddi_intr_handle_impl_t *hdlp, void *result)
7620Sstevel@tonic-gate {
7630Sstevel@tonic-gate #ifdef DEBUG
7640Sstevel@tonic-gate 	ebus_devstate_t *ebus_p = get_ebus_soft_state(ddi_get_instance(dip));
7650Sstevel@tonic-gate #endif
766693Sgovinda 	int32_t		i, max_children, max_device_types, len;
767693Sgovinda 	char		*name_p, *device_type_p;
7680Sstevel@tonic-gate 
769693Sgovinda 	DBG1(D_INTR, ebus_p, "ebus_p 0x%p\n", ebus_p);
7700Sstevel@tonic-gate 
7710Sstevel@tonic-gate 	/*
7720Sstevel@tonic-gate 	 * NOTE: These ops below will never be supported in this nexus
7730Sstevel@tonic-gate 	 * driver, hence they always return immediately.
7740Sstevel@tonic-gate 	 */
7750Sstevel@tonic-gate 	switch (intr_op) {
7760Sstevel@tonic-gate 	case DDI_INTROP_GETCAP:
777693Sgovinda 		*(int *)result = DDI_INTR_FLAG_LEVEL;
7780Sstevel@tonic-gate 		return (DDI_SUCCESS);
779*3920Srameshc 	case DDI_INTROP_SUPPORTED_TYPES:
780*3920Srameshc 		*(int *)result = i_ddi_get_intx_nintrs(rdip) ?
781*3920Srameshc 		    DDI_INTR_TYPE_FIXED : 0;
782*3920Srameshc 		return (DDI_SUCCESS);
7830Sstevel@tonic-gate 	case DDI_INTROP_SETCAP:
7840Sstevel@tonic-gate 	case DDI_INTROP_SETMASK:
7850Sstevel@tonic-gate 	case DDI_INTROP_CLRMASK:
7860Sstevel@tonic-gate 	case DDI_INTROP_GETPENDING:
7870Sstevel@tonic-gate 		return (DDI_ENOTSUP);
7880Sstevel@tonic-gate 	default:
7890Sstevel@tonic-gate 		break;
7900Sstevel@tonic-gate 	}
7910Sstevel@tonic-gate 
792*3920Srameshc 	if (hdlp->ih_pri)
7930Sstevel@tonic-gate 		goto done;
7940Sstevel@tonic-gate 
7950Sstevel@tonic-gate 	/*
7960Sstevel@tonic-gate 	 * This is a hack to set the PIL for the devices under ebus.
7970Sstevel@tonic-gate 	 * We first look up a device by it's specific name, if we can't
7980Sstevel@tonic-gate 	 * match the name, we try and match it's device_type property.
7990Sstevel@tonic-gate 	 * Lastly we default a PIL level of 1.
8000Sstevel@tonic-gate 	 */
8010Sstevel@tonic-gate 	name_p = ddi_node_name(rdip);
8020Sstevel@tonic-gate 	max_children = sizeof (ebus_name_to_pil) /
8030Sstevel@tonic-gate 	    sizeof (struct ebus_string_to_pil);
8040Sstevel@tonic-gate 
8050Sstevel@tonic-gate 	for (i = 0; i < max_children; i++) {
8060Sstevel@tonic-gate 		if (strcmp(ebus_name_to_pil[i].string, name_p) == 0) {
8070Sstevel@tonic-gate 			DBG2(D_INTR, ebus_p, "child name %s; match PIL %d\n",
8080Sstevel@tonic-gate 			    ebus_name_to_pil[i].string,
8090Sstevel@tonic-gate 			    ebus_name_to_pil[i].pil);
8100Sstevel@tonic-gate 
811693Sgovinda 			hdlp->ih_pri = ebus_name_to_pil[i].pil;
8120Sstevel@tonic-gate 			goto done;
8130Sstevel@tonic-gate 		}
8140Sstevel@tonic-gate 	}
8150Sstevel@tonic-gate 
816506Scth 	if (ddi_getlongprop(DDI_DEV_T_ANY, rdip, DDI_PROP_DONTPASS,
8170Sstevel@tonic-gate 	    "device_type", (caddr_t)&device_type_p, &len) == DDI_SUCCESS) {
8180Sstevel@tonic-gate 
8190Sstevel@tonic-gate 		max_device_types = sizeof (ebus_device_type_to_pil) /
8200Sstevel@tonic-gate 		    sizeof (struct ebus_string_to_pil);
8210Sstevel@tonic-gate 
8220Sstevel@tonic-gate 		for (i = 0; i < max_device_types; i++) {
8230Sstevel@tonic-gate 			if (strcmp(ebus_device_type_to_pil[i].string,
8240Sstevel@tonic-gate 			    device_type_p) == 0) {
8250Sstevel@tonic-gate 				DBG2(D_INTR, ebus_p, "Device type %s; match "
8260Sstevel@tonic-gate 				    "PIL %d\n", ebus_device_type_to_pil[i].
8270Sstevel@tonic-gate 				    string, ebus_device_type_to_pil[i].pil);
8280Sstevel@tonic-gate 
829693Sgovinda 				hdlp->ih_pri = ebus_device_type_to_pil[i].pil;
8300Sstevel@tonic-gate 				break;
8310Sstevel@tonic-gate 			}
8320Sstevel@tonic-gate 		}
8330Sstevel@tonic-gate 
8340Sstevel@tonic-gate 		kmem_free(device_type_p, len);
8350Sstevel@tonic-gate 	}
8360Sstevel@tonic-gate 
8370Sstevel@tonic-gate 	/*
8380Sstevel@tonic-gate 	 * If we get here, we need to set a default value
8390Sstevel@tonic-gate 	 * for the PIL.
8400Sstevel@tonic-gate 	 */
841693Sgovinda 	if (hdlp->ih_pri == 0) {
842693Sgovinda 		hdlp->ih_pri = 1;
8430Sstevel@tonic-gate 
8440Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d assigning default interrupt level %d "
845693Sgovinda 		    "for device %s%d", ddi_driver_name(dip),
846693Sgovinda 		    ddi_get_instance(dip), hdlp->ih_pri, ddi_driver_name(rdip),
847693Sgovinda 		    ddi_get_instance(rdip));
8480Sstevel@tonic-gate 	}
8490Sstevel@tonic-gate 
8500Sstevel@tonic-gate done:
8510Sstevel@tonic-gate 	/* Pass up the request to our parent. */
8520Sstevel@tonic-gate 	return (i_ddi_intr_ops(dip, rdip, intr_op, hdlp, result));
8530Sstevel@tonic-gate }
8540Sstevel@tonic-gate 
855558Sjasonwu /*
856558Sjasonwu  * ebus_config: setup pci config space registers:
857558Sjasonwu  *     enable bus mastering, memory access and error reporting
858558Sjasonwu  */
8590Sstevel@tonic-gate static int
8600Sstevel@tonic-gate ebus_config(ebus_devstate_t *ebus_p)
8610Sstevel@tonic-gate {
8620Sstevel@tonic-gate 	ddi_acc_handle_t conf_handle;
8630Sstevel@tonic-gate 	uint16_t comm;
864558Sjasonwu 	dev_info_t *dip = ebus_p->dip;
865558Sjasonwu 	char *devtype_str;
866558Sjasonwu 	int devtype_len;
867558Sjasonwu 
868558Sjasonwu 	if (ddi_getlongprop(DDI_DEV_T_ANY, ddi_get_parent(dip),
869558Sjasonwu 		DDI_PROP_DONTPASS, "device_type", (caddr_t)&devtype_str,
870558Sjasonwu 		&devtype_len) != DDI_SUCCESS) {
871558Sjasonwu 		cmn_err(CE_WARN, "Can't get %s device_type property",
872558Sjasonwu 		    ddi_get_name(ddi_get_parent(dip)));
873558Sjasonwu 
874558Sjasonwu 		return (DDI_FAILURE);
875558Sjasonwu 	}
876558Sjasonwu 
877558Sjasonwu 	comm = strcmp(devtype_str, "pci");
878558Sjasonwu 	kmem_free(devtype_str, devtype_len);
879558Sjasonwu 
880558Sjasonwu 	if (comm)
881558Sjasonwu 		return (DDI_SUCCESS);
8820Sstevel@tonic-gate 
8830Sstevel@tonic-gate 	/*
8840Sstevel@tonic-gate 	 * Make sure the master enable and memory access enable
8850Sstevel@tonic-gate 	 * bits are set in the config command register.
8860Sstevel@tonic-gate 	 */
8870Sstevel@tonic-gate 	if (pci_config_setup(ebus_p->dip, &conf_handle) != DDI_SUCCESS)
888558Sjasonwu 		return (DDI_FAILURE);
8890Sstevel@tonic-gate 
8900Sstevel@tonic-gate 	comm = pci_config_get16(conf_handle, PCI_CONF_COMM),
8910Sstevel@tonic-gate #ifdef DEBUG
8920Sstevel@tonic-gate 	    DBG1(D_MAP, ebus_p, "command register was 0x%x\n", comm);
8930Sstevel@tonic-gate #endif
8940Sstevel@tonic-gate 	comm |= (PCI_COMM_ME|PCI_COMM_MAE|PCI_COMM_SERR_ENABLE|
8950Sstevel@tonic-gate 	    PCI_COMM_PARITY_DETECT);
8960Sstevel@tonic-gate 	pci_config_put16(conf_handle, PCI_CONF_COMM, comm),
8970Sstevel@tonic-gate #ifdef DEBUG
8980Sstevel@tonic-gate 	    DBG1(D_MAP, ebus_p, "command register is now 0x%x\n", comm);
8990Sstevel@tonic-gate #endif
9000Sstevel@tonic-gate 	pci_config_put8(conf_handle, PCI_CONF_CACHE_LINESZ,
9010Sstevel@tonic-gate 	    (uchar_t)ebus_cache_line_size);
9020Sstevel@tonic-gate 	pci_config_put8(conf_handle, PCI_CONF_LATENCY_TIMER,
9030Sstevel@tonic-gate 	    (uchar_t)ebus_latency_timer);
9040Sstevel@tonic-gate 	pci_config_teardown(&conf_handle);
905558Sjasonwu 	return (DDI_SUCCESS);
9060Sstevel@tonic-gate }
9070Sstevel@tonic-gate 
9080Sstevel@tonic-gate #ifdef DEBUG
9090Sstevel@tonic-gate extern void prom_printf(const char *, ...);
9100Sstevel@tonic-gate 
9110Sstevel@tonic-gate static void
9120Sstevel@tonic-gate ebus_debug(uint_t flag, ebus_devstate_t *ebus_p, char *fmt,
9130Sstevel@tonic-gate 	uintptr_t a1, uintptr_t a2, uintptr_t a3, uintptr_t a4, uintptr_t a5)
9140Sstevel@tonic-gate {
9150Sstevel@tonic-gate 	char *s;
9160Sstevel@tonic-gate 
9170Sstevel@tonic-gate 	if (ebus_debug_flags & flag) {
9180Sstevel@tonic-gate 		switch (flag) {
9190Sstevel@tonic-gate 		case D_ATTACH:
9200Sstevel@tonic-gate 			s = "attach"; break;
9210Sstevel@tonic-gate 		case D_DETACH:
9220Sstevel@tonic-gate 			s = "detach"; break;
9230Sstevel@tonic-gate 		case D_MAP:
9240Sstevel@tonic-gate 			s = "map"; break;
9250Sstevel@tonic-gate 		case D_CTLOPS:
9260Sstevel@tonic-gate 			s = "ctlops"; break;
9270Sstevel@tonic-gate 		case D_INTR:
9280Sstevel@tonic-gate 			s = "intr"; break;
9290Sstevel@tonic-gate 		}
9300Sstevel@tonic-gate 		if (ebus_p)
9310Sstevel@tonic-gate 			cmn_err(CE_CONT, "%s%d: %s: ",
9320Sstevel@tonic-gate 				ddi_get_name(ebus_p->dip),
9330Sstevel@tonic-gate 				ddi_get_instance(ebus_p->dip), s);
9340Sstevel@tonic-gate 		else
9350Sstevel@tonic-gate 			cmn_err(CE_CONT, "ebus: ");
9360Sstevel@tonic-gate 		cmn_err(CE_CONT, fmt, a1, a2, a3, a4, a5);
9370Sstevel@tonic-gate 	}
9380Sstevel@tonic-gate }
939558Sjasonwu 
940558Sjasonwu static void
941558Sjasonwu ebus_vreg_dump(ebus_devstate_t *ebus_p, vregspec_t *rp)
942558Sjasonwu {
943558Sjasonwu 	if (ebus_p->ebus_paddr_cells == 3) {
944558Sjasonwu 		DBG5(D_MAP, ebus_p, "(%x,%x,%x)(%x,%x)\n",
945558Sjasonwu 		    rp->pci_regspec.pci_phys_hi,
946558Sjasonwu 		    rp->pci_regspec.pci_phys_mid,
947558Sjasonwu 		    rp->pci_regspec.pci_phys_low,
948558Sjasonwu 		    rp->pci_regspec.pci_size_hi,
949558Sjasonwu 		    rp->pci_regspec.pci_size_low);
950558Sjasonwu 	} else if (ebus_p->ebus_paddr_cells == 2) {
951558Sjasonwu 		DBG3(D_MAP, ebus_p, "%x,%x,%x\n",
952558Sjasonwu 		    rp->jbus_regspec.regspec_bustype,
953558Sjasonwu 		    rp->jbus_regspec.regspec_addr,
954558Sjasonwu 		    rp->jbus_regspec.regspec_size);
955558Sjasonwu 	}
956558Sjasonwu }
957558Sjasonwu #endif /* DEBUG */
9580Sstevel@tonic-gate 
9590Sstevel@tonic-gate /* ARGSUSED3 */
9600Sstevel@tonic-gate static int
9610Sstevel@tonic-gate ebus_open(dev_t *devp, int flags, int otyp, cred_t *credp)
9620Sstevel@tonic-gate {
9630Sstevel@tonic-gate 	ebus_devstate_t *ebus_p;
9640Sstevel@tonic-gate 
9650Sstevel@tonic-gate 	/*
9660Sstevel@tonic-gate 	 * Make sure the open is for the right file type.
9670Sstevel@tonic-gate 	 */
9680Sstevel@tonic-gate 	if (otyp != OTYP_CHR)
9690Sstevel@tonic-gate 		return (EINVAL);
9700Sstevel@tonic-gate 
9710Sstevel@tonic-gate 	/*
9720Sstevel@tonic-gate 	 * Get the soft state structure for the device.
9730Sstevel@tonic-gate 	 */
9740Sstevel@tonic-gate 	ebus_p = get_ebus_soft_state(getminor(*devp));
9750Sstevel@tonic-gate 	if (ebus_p == NULL)
9760Sstevel@tonic-gate 		return (ENXIO);
9770Sstevel@tonic-gate 
9780Sstevel@tonic-gate 	/*
9790Sstevel@tonic-gate 	 * Handle the open by tracking the device state.
9800Sstevel@tonic-gate 	 */
9810Sstevel@tonic-gate 	mutex_enter(&ebus_p->ebus_mutex);
9820Sstevel@tonic-gate 	if (flags & FEXCL) {
9830Sstevel@tonic-gate 		if (ebus_p->ebus_soft_state != EBUS_SOFT_STATE_CLOSED) {
9840Sstevel@tonic-gate 			mutex_exit(&ebus_p->ebus_mutex);
9850Sstevel@tonic-gate 			return (EBUSY);
9860Sstevel@tonic-gate 		}
9870Sstevel@tonic-gate 		ebus_p->ebus_soft_state = EBUS_SOFT_STATE_OPEN_EXCL;
9880Sstevel@tonic-gate 	} else {
9890Sstevel@tonic-gate 		if (ebus_p->ebus_soft_state == EBUS_SOFT_STATE_OPEN_EXCL) {
9900Sstevel@tonic-gate 			mutex_exit(&ebus_p->ebus_mutex);
9910Sstevel@tonic-gate 			return (EBUSY);
9920Sstevel@tonic-gate 		}
9930Sstevel@tonic-gate 		ebus_p->ebus_soft_state = EBUS_SOFT_STATE_OPEN;
9940Sstevel@tonic-gate 	}
9950Sstevel@tonic-gate 	mutex_exit(&ebus_p->ebus_mutex);
9960Sstevel@tonic-gate 	return (0);
9970Sstevel@tonic-gate }
9980Sstevel@tonic-gate 
9990Sstevel@tonic-gate 
10000Sstevel@tonic-gate /* ARGSUSED */
10010Sstevel@tonic-gate static int
10020Sstevel@tonic-gate ebus_close(dev_t dev, int flags, int otyp, cred_t *credp)
10030Sstevel@tonic-gate {
10040Sstevel@tonic-gate 	ebus_devstate_t *ebus_p;
10050Sstevel@tonic-gate 
10060Sstevel@tonic-gate 	if (otyp != OTYP_CHR)
10070Sstevel@tonic-gate 		return (EINVAL);
10080Sstevel@tonic-gate 
10090Sstevel@tonic-gate 	ebus_p = get_ebus_soft_state(getminor(dev));
10100Sstevel@tonic-gate 	if (ebus_p == NULL)
10110Sstevel@tonic-gate 		return (ENXIO);
10120Sstevel@tonic-gate 
10130Sstevel@tonic-gate 	mutex_enter(&ebus_p->ebus_mutex);
10140Sstevel@tonic-gate 	ebus_p->ebus_soft_state = EBUS_SOFT_STATE_CLOSED;
10150Sstevel@tonic-gate 	mutex_exit(&ebus_p->ebus_mutex);
10160Sstevel@tonic-gate 	return (0);
10170Sstevel@tonic-gate }
10180Sstevel@tonic-gate 
10190Sstevel@tonic-gate 
10200Sstevel@tonic-gate /*
10210Sstevel@tonic-gate  * ebus_ioctl: devctl hotplug controls
10220Sstevel@tonic-gate  */
10230Sstevel@tonic-gate /* ARGSUSED */
10240Sstevel@tonic-gate static int
10250Sstevel@tonic-gate ebus_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp,
10260Sstevel@tonic-gate 	int *rvalp)
10270Sstevel@tonic-gate {
10280Sstevel@tonic-gate 	ebus_devstate_t *ebus_p;
10290Sstevel@tonic-gate 	dev_info_t *self;
10300Sstevel@tonic-gate 	struct devctl_iocdata *dcp;
10310Sstevel@tonic-gate 	uint_t bus_state;
10320Sstevel@tonic-gate 	int rv = 0;
10330Sstevel@tonic-gate 
10340Sstevel@tonic-gate 	ebus_p = get_ebus_soft_state(getminor(dev));
10350Sstevel@tonic-gate 	if (ebus_p == NULL)
10360Sstevel@tonic-gate 		return (ENXIO);
10370Sstevel@tonic-gate 
10380Sstevel@tonic-gate 	self = ebus_p->dip;
10390Sstevel@tonic-gate 
10400Sstevel@tonic-gate 	/*
10410Sstevel@tonic-gate 	 * We can use the generic implementation for these ioctls
10420Sstevel@tonic-gate 	 */
10430Sstevel@tonic-gate 	switch (cmd) {
10440Sstevel@tonic-gate 	case DEVCTL_DEVICE_GETSTATE:
10450Sstevel@tonic-gate 	case DEVCTL_DEVICE_ONLINE:
10460Sstevel@tonic-gate 	case DEVCTL_DEVICE_OFFLINE:
10470Sstevel@tonic-gate 	case DEVCTL_BUS_GETSTATE:
10480Sstevel@tonic-gate 		return (ndi_devctl_ioctl(self, cmd, arg, mode, 0));
10490Sstevel@tonic-gate 	}
10500Sstevel@tonic-gate 
10510Sstevel@tonic-gate 	/*
10520Sstevel@tonic-gate 	 * read devctl ioctl data
10530Sstevel@tonic-gate 	 */
10540Sstevel@tonic-gate 	if (ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS)
10550Sstevel@tonic-gate 		return (EFAULT);
10560Sstevel@tonic-gate 
10570Sstevel@tonic-gate 	switch (cmd) {
10580Sstevel@tonic-gate 
10590Sstevel@tonic-gate 	case DEVCTL_DEVICE_RESET:
10600Sstevel@tonic-gate 		rv = ENOTSUP;
10610Sstevel@tonic-gate 		break;
10620Sstevel@tonic-gate 
10630Sstevel@tonic-gate 	case DEVCTL_BUS_QUIESCE:
10640Sstevel@tonic-gate 		if (ndi_get_bus_state(self, &bus_state) == NDI_SUCCESS)
10650Sstevel@tonic-gate 			if (bus_state == BUS_QUIESCED)
10660Sstevel@tonic-gate 				break;
10670Sstevel@tonic-gate 		(void) ndi_set_bus_state(self, BUS_QUIESCED);
10680Sstevel@tonic-gate 		break;
10690Sstevel@tonic-gate 
10700Sstevel@tonic-gate 	case DEVCTL_BUS_UNQUIESCE:
10710Sstevel@tonic-gate 		if (ndi_get_bus_state(self, &bus_state) == NDI_SUCCESS)
10720Sstevel@tonic-gate 			if (bus_state == BUS_ACTIVE)
10730Sstevel@tonic-gate 				break;
10740Sstevel@tonic-gate 		(void) ndi_set_bus_state(self, BUS_ACTIVE);
10750Sstevel@tonic-gate 		break;
10760Sstevel@tonic-gate 
10770Sstevel@tonic-gate 	case DEVCTL_BUS_RESET:
10780Sstevel@tonic-gate 		rv = ENOTSUP;
10790Sstevel@tonic-gate 		break;
10800Sstevel@tonic-gate 
10810Sstevel@tonic-gate 	case DEVCTL_BUS_RESETALL:
10820Sstevel@tonic-gate 		rv = ENOTSUP;
10830Sstevel@tonic-gate 		break;
10840Sstevel@tonic-gate 
10850Sstevel@tonic-gate 	default:
10860Sstevel@tonic-gate 		rv = ENOTTY;
10870Sstevel@tonic-gate 	}
10880Sstevel@tonic-gate 
10890Sstevel@tonic-gate 	ndi_dc_freehdl(dcp);
10900Sstevel@tonic-gate 	return (rv);
10910Sstevel@tonic-gate }
1092