xref: /onnv-gate/usr/src/uts/sun4u/io/pci/db21554.c (revision 946:5fb89afc576e)
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
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
230Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  *	Intel 21554 PCI to PCI bus bridge nexus driver for sun4u platforms.
310Sstevel@tonic-gate  *	Please note that 21554 is not a transparent bridge.
320Sstevel@tonic-gate  *	This driver can be used when the 21554 bridge is used like a
330Sstevel@tonic-gate  *	transparent bridge. The host OBP or the OS PCI Resource Allocator
340Sstevel@tonic-gate  *	(during a hotplug/hotswap operation) must represent this device
350Sstevel@tonic-gate  *	as a nexus and do the device tree representation of the child
360Sstevel@tonic-gate  *	nodes underneath.
370Sstevel@tonic-gate  *	Interrupt routing of the children must be done as per the PCI
380Sstevel@tonic-gate  *	specifications recommendation similar to that of a transparent
390Sstevel@tonic-gate  *	bridge.
400Sstevel@tonic-gate  *	Address translations from secondary across primary can be 1:1
410Sstevel@tonic-gate  *	or non 1:1. Currently only 1:1 translations are supported.
420Sstevel@tonic-gate  *	Configuration cycles are indirect. Memory and IO cycles are direct.
430Sstevel@tonic-gate  */
440Sstevel@tonic-gate 
450Sstevel@tonic-gate /*
460Sstevel@tonic-gate  * INCLUDES
470Sstevel@tonic-gate  */
480Sstevel@tonic-gate #include <sys/stat.h>
490Sstevel@tonic-gate #include <sys/conf.h>
500Sstevel@tonic-gate #include <sys/kmem.h>
510Sstevel@tonic-gate #include <sys/debug.h>
520Sstevel@tonic-gate #include <sys/modctl.h>
530Sstevel@tonic-gate #include <sys/autoconf.h>
540Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
550Sstevel@tonic-gate #include <sys/ddi_subrdefs.h>
560Sstevel@tonic-gate #include <sys/pci.h>
570Sstevel@tonic-gate #include <sys/pci/pci_nexus.h>
580Sstevel@tonic-gate #include <sys/pci/pci_regs.h>
590Sstevel@tonic-gate #include <sys/pci/db21554_config.h> /* 21554 configuration space registers */
600Sstevel@tonic-gate #include <sys/pci/db21554_csr.h> /* 21554 control status register layout */
610Sstevel@tonic-gate #include <sys/pci/db21554_ctrl.h> /* driver private control structure	*/
620Sstevel@tonic-gate #include <sys/pci/db21554_debug.h> /* driver debug declarations		*/
630Sstevel@tonic-gate #include <sys/ddi.h>
640Sstevel@tonic-gate #include <sys/sunddi.h>
650Sstevel@tonic-gate #include <sys/sunndi.h>
660Sstevel@tonic-gate #include <sys/fm/protocol.h>
670Sstevel@tonic-gate #include <sys/ddifm.h>
680Sstevel@tonic-gate #include <sys/promif.h>
690Sstevel@tonic-gate #include <sys/file.h>
700Sstevel@tonic-gate #include <sys/hotplug/pci/pcihp.h>
710Sstevel@tonic-gate 
720Sstevel@tonic-gate /*
730Sstevel@tonic-gate  * DEFINES.
740Sstevel@tonic-gate  */
750Sstevel@tonic-gate #define	DB_DEBUG
760Sstevel@tonic-gate #define	DB_MODINFO_DESCRIPTION	"Intel/21554 pci-pci nexus:v%I%"
770Sstevel@tonic-gate #define	DB_DVMA_START		0xc0000000
780Sstevel@tonic-gate #define	DB_DVMA_LEN		0x20000000
790Sstevel@tonic-gate 
800Sstevel@tonic-gate #ifdef	DB_DEBUG
810Sstevel@tonic-gate /* ioctl definitions */
820Sstevel@tonic-gate #define	DB_PCI_READ_CONF_HEADER		1
830Sstevel@tonic-gate #define	DEF_INVALID_REG_VAL		-1
840Sstevel@tonic-gate 
850Sstevel@tonic-gate /* Default values for secondary cache line and latency timer */
860Sstevel@tonic-gate #define	DB_SEC_LATENCY_TIMER_VAL	0x40
870Sstevel@tonic-gate #define	DB_SEC_CACHELN_SIZE_VAL		0x10
880Sstevel@tonic-gate 
890Sstevel@tonic-gate /* complete chip status information */
900Sstevel@tonic-gate typedef struct db_pci_data {
910Sstevel@tonic-gate 	char		name[256];
920Sstevel@tonic-gate 	uint32_t	instance;
930Sstevel@tonic-gate 	db_pci_header_t pri_hdr;
940Sstevel@tonic-gate 	db_pci_header_t sec_hdr;
950Sstevel@tonic-gate 	db_conf_regs_t	conf_regs;
960Sstevel@tonic-gate } db_pci_data_t;
970Sstevel@tonic-gate #endif
980Sstevel@tonic-gate 
990Sstevel@tonic-gate /*
1000Sstevel@tonic-gate  * LOCALS
1010Sstevel@tonic-gate  */
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate /*
1040Sstevel@tonic-gate  * The next set of variables are control parameters for debug purposes only.
1050Sstevel@tonic-gate  * Changing the default values as assigned below are not recommended.
1060Sstevel@tonic-gate  * In some cases, the non-default values are mostly application specific and
1070Sstevel@tonic-gate  * hence may not have been tested yet.
1080Sstevel@tonic-gate  *
1090Sstevel@tonic-gate  *	db_conf_map_mode : specifies the access method used for generating
1100Sstevel@tonic-gate  *			   configuration cycles. Default value indicates
1110Sstevel@tonic-gate  *			   the indirect configuration method.
1120Sstevel@tonic-gate  *	db_io_map_mode	 : specifies the access method used for generating
1130Sstevel@tonic-gate  *			   IO cycles. Default value indicates the direct
1140Sstevel@tonic-gate  *			   method.
1150Sstevel@tonic-gate  *	db_pci_own_wait	 : For indirect cycles, indicates the wait period
1160Sstevel@tonic-gate  *			   for acquiring the bus, when the bus is busy.
1170Sstevel@tonic-gate  *	db_pci_release_wait:For indirect cycles, indicates the wait period
1180Sstevel@tonic-gate  *			    for releasing the bus when the bus is busy.
1190Sstevel@tonic-gate  *	db_pci_max_wait  : max. wait time when bus is busy for indirect cycles
1200Sstevel@tonic-gate  *	db_set_latency_timer_register :
1210Sstevel@tonic-gate  *			   when 1, the driver overwrites the OBP assigned
1220Sstevel@tonic-gate  *			   latency timer register setting for every child
1230Sstevel@tonic-gate  *			   device during child initialization.
1240Sstevel@tonic-gate  *	db_set_cache_line_size_register :
1250Sstevel@tonic-gate  *			   when 1, the driver overwrites the OBP assigned
1260Sstevel@tonic-gate  *			   cache line register setting for every child
1270Sstevel@tonic-gate  *			   device during child initialization.
1280Sstevel@tonic-gate  *	db_use_config_own_bit:
1290Sstevel@tonic-gate  *			   when 1, the driver will use the "config own bit"
1300Sstevel@tonic-gate  *			   for accessing the configuration address and data
1310Sstevel@tonic-gate  *			   registers.
1320Sstevel@tonic-gate  */
1330Sstevel@tonic-gate static uint32_t	db_pci_own_wait = DB_PCI_WAIT_MS;
1340Sstevel@tonic-gate static uint32_t	db_pci_release_wait = DB_PCI_WAIT_MS;
1350Sstevel@tonic-gate static uint32_t	db_pci_max_wait = DB_PCI_TIMEOUT;
1360Sstevel@tonic-gate static uint32_t	db_conf_map_mode = DB_CONF_MAP_INDIRECT_CONF;
1370Sstevel@tonic-gate static uint32_t	db_io_map_mode = DB_IO_MAP_DIRECT;
1380Sstevel@tonic-gate static uint32_t	db_set_latency_timer_register = 1;
1390Sstevel@tonic-gate static uint32_t	db_set_cache_line_size_register = 1;
1400Sstevel@tonic-gate static uint32_t	db_use_config_own_bit = 0;
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate /*
1430Sstevel@tonic-gate  * Properties that can be set via .conf files.
1440Sstevel@tonic-gate  */
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate /*
1470Sstevel@tonic-gate  * By default, we forward SERR# from secondary to primary. This behavior
1480Sstevel@tonic-gate  * can be controlled via a property "serr-fwd-enable", type integer.
1490Sstevel@tonic-gate  * Values are 0 or 1.
1500Sstevel@tonic-gate  * 0 means 'do not forward SERR#'.
1510Sstevel@tonic-gate  * 1 means forwards SERR# to the host. Should be the default.
1520Sstevel@tonic-gate  */
1530Sstevel@tonic-gate static uint32_t	db_serr_fwd_enable = 1;
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate /*
1560Sstevel@tonic-gate  * The next set of parameters are performance tuning parameters.
1570Sstevel@tonic-gate  * These are in the form of properties settable through a .conf file.
1580Sstevel@tonic-gate  * In case if the properties are absent the following defaults are assumed.
1590Sstevel@tonic-gate  * These initial default values can be overwritten via /etc/system also.
1600Sstevel@tonic-gate  *
1610Sstevel@tonic-gate  * -1 means no setting is done ie. we either get OBP assigned value
1620Sstevel@tonic-gate  * or reset values (at hotplug time for example).
1630Sstevel@tonic-gate  */
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate /* primary latency timer: property "p-latency-timer" : type integer */
1660Sstevel@tonic-gate static int8_t	p_latency_timer = DEF_INVALID_REG_VAL;
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate /* secondary latency timer: property "s-latency-timer": type integer */
1690Sstevel@tonic-gate /*
1700Sstevel@tonic-gate  * Currently on the secondary side the latency timer  is not
1710Sstevel@tonic-gate  * set by the serial PROM which causes performance degradation.
1720Sstevel@tonic-gate  * Set the secondary latency timer register.
1730Sstevel@tonic-gate  */
1740Sstevel@tonic-gate static int8_t	s_latency_timer = DB_SEC_LATENCY_TIMER_VAL;
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate /* primary cache line size: property "p-cache-line-size" : type integer */
1770Sstevel@tonic-gate static int8_t	p_cache_line_size = DEF_INVALID_REG_VAL;
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate /* secondary cache line size: property "s-cache-line-size" : type integer */
1800Sstevel@tonic-gate /*
1810Sstevel@tonic-gate  * Currently on the secondary side the cache line size is not
1820Sstevel@tonic-gate  * set by the serial PROM which causes performance degradation.
1830Sstevel@tonic-gate  * Set the secondary cache line size register.
1840Sstevel@tonic-gate  */
1850Sstevel@tonic-gate static int8_t	s_cache_line_size = DB_SEC_CACHELN_SIZE_VAL;
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate /*
1880Sstevel@tonic-gate  * control primary posted write queue threshold limit:
1890Sstevel@tonic-gate  * property "p-pwrite-threshold" : type integer : values are 0 or 1.
1900Sstevel@tonic-gate  * 1 enables control. 0 does not, and is the default reset value.
1910Sstevel@tonic-gate  */
1920Sstevel@tonic-gate static int8_t	p_pwrite_threshold = DEF_INVALID_REG_VAL;
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate /*
1950Sstevel@tonic-gate  * control secondary posted write queue threshold limit:
1960Sstevel@tonic-gate  * property "s-pwrite-threshold" : type integer : values are 0 or 1.
1970Sstevel@tonic-gate  * 1 enables control. 0 does not, and is the default reset value.
1980Sstevel@tonic-gate  */
1990Sstevel@tonic-gate static int8_t	s_pwrite_threshold = DEF_INVALID_REG_VAL;
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate /*
2020Sstevel@tonic-gate  * control read queue threshold for initiating delayed read transaction
2030Sstevel@tonic-gate  * on primary bus.
2040Sstevel@tonic-gate  * property "p-dread-threshold" : type integer: values are
2050Sstevel@tonic-gate  *
2060Sstevel@tonic-gate  * 0 : reset value, default behavior: at least 8DWords free for all MR
2070Sstevel@tonic-gate  * 1 : reserved
2080Sstevel@tonic-gate  * 2 : at least one cache line free for MRL and MRM, 8 DWords free for MR
2090Sstevel@tonic-gate  * 3 : at least one cache line free for all MR
2100Sstevel@tonic-gate  */
2110Sstevel@tonic-gate static int8_t	p_dread_threshold = DEF_INVALID_REG_VAL;
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate /*
2140Sstevel@tonic-gate  * control read queue threshold for initiating delayed read transaction
2150Sstevel@tonic-gate  * on secondary bus.
2160Sstevel@tonic-gate  * property "s-dread-threshold" : type integer: values are
2170Sstevel@tonic-gate  *
2180Sstevel@tonic-gate  * 0 : reset value, default behavior: at least 8DWords free for all MR
2190Sstevel@tonic-gate  * 1 : reserved
2200Sstevel@tonic-gate  * 2 : at least one cache line free for MRL and MRM, 8 DWords free for MR
2210Sstevel@tonic-gate  * 3 : at least one cache line free for all MR
2220Sstevel@tonic-gate  */
2230Sstevel@tonic-gate static int8_t	s_dread_threshold = DEF_INVALID_REG_VAL;
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate /*
2260Sstevel@tonic-gate  * control how 21554 issues delayed transactions on the target bus.
2270Sstevel@tonic-gate  * property "delayed-trans-order" : type integer: values are 0 or 1.
2280Sstevel@tonic-gate  * 1 means repeat transaction on same target on target retries.
2290Sstevel@tonic-gate  * 0 is the reset/default value, and means enable round robin based
2300Sstevel@tonic-gate  * reads on  other targets in read queue on any target retries.
2310Sstevel@tonic-gate  */
2320Sstevel@tonic-gate static int8_t	delayed_trans_order = DEF_INVALID_REG_VAL;
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate /*
2350Sstevel@tonic-gate  * In case if the system DVMA information is not available, as it is
2360Sstevel@tonic-gate  * prior to s28q1, the system dvma range can be set via these parameters.
2370Sstevel@tonic-gate  */
2380Sstevel@tonic-gate static uint32_t	db_dvma_start = DB_DVMA_START;
2390Sstevel@tonic-gate static uint32_t	db_dvma_len = DB_DVMA_LEN;
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate /*
2420Sstevel@tonic-gate  * Default command register settings for all PCI nodes this nexus initializes.
2430Sstevel@tonic-gate  */
2440Sstevel@tonic-gate static uint16_t	db_command_default =
2450Sstevel@tonic-gate 			PCI_COMM_SERR_ENABLE |
2460Sstevel@tonic-gate 			PCI_COMM_PARITY_DETECT |
2470Sstevel@tonic-gate 			PCI_COMM_ME |
2480Sstevel@tonic-gate 			PCI_COMM_MAE |
2490Sstevel@tonic-gate 			PCI_COMM_IO |
2500Sstevel@tonic-gate 			PCI_COMM_BACK2BACK_ENAB |
2510Sstevel@tonic-gate 			PCI_COMM_MEMWR_INVAL;
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate static int	db_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
2540Sstevel@tonic-gate static int	db_detach(dev_info_t *dip, ddi_detach_cmd_t cmd);
2550Sstevel@tonic-gate static void	db_get_perf_parameters(db_ctrl_t *dbp);
2560Sstevel@tonic-gate static void	db_set_perf_parameters(db_ctrl_t *dbp);
2570Sstevel@tonic-gate static void	db_enable_io(db_ctrl_t *dbp);
2580Sstevel@tonic-gate static void	db_orientation(db_ctrl_t *dbp);
2590Sstevel@tonic-gate static void	db_set_dvma_range(db_ctrl_t *dbp);
2600Sstevel@tonic-gate static int	db_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
2610Sstevel@tonic-gate 			void **result);
2620Sstevel@tonic-gate static int	db_pci_map(dev_info_t *, dev_info_t *, ddi_map_req_t *,
2630Sstevel@tonic-gate 			off_t, off_t, caddr_t *);
2640Sstevel@tonic-gate static int	db_ctlops(dev_info_t *, dev_info_t *, ddi_ctl_enum_t,
2650Sstevel@tonic-gate 			void *, void *);
2660Sstevel@tonic-gate static int	db_intr_ops(dev_info_t *dip, dev_info_t *rdip,
2670Sstevel@tonic-gate 			ddi_intr_op_t intr_op, ddi_intr_handle_impl_t *hdlp,
2680Sstevel@tonic-gate 			void *result);
2690Sstevel@tonic-gate static dev_info_t *db_get_my_childs_dip(dev_info_t *dip, dev_info_t *rdip);
2700Sstevel@tonic-gate static int db_fm_init_child(dev_info_t *dip, dev_info_t *tdip, int cap,
2710Sstevel@tonic-gate 		ddi_iblock_cookie_t *ibc);
2720Sstevel@tonic-gate static void db_bus_enter(dev_info_t *dip, ddi_acc_handle_t handle);
2730Sstevel@tonic-gate static void db_bus_exit(dev_info_t *dip, ddi_acc_handle_t handle);
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate struct bus_ops db_bus_ops = {
2760Sstevel@tonic-gate 	BUSO_REV,
2770Sstevel@tonic-gate 	db_pci_map,
2780Sstevel@tonic-gate 	0,
2790Sstevel@tonic-gate 	0,
2800Sstevel@tonic-gate 	0,
2810Sstevel@tonic-gate 	i_ddi_map_fault,
2820Sstevel@tonic-gate 	ddi_dma_map,
2830Sstevel@tonic-gate 	ddi_dma_allochdl,
2840Sstevel@tonic-gate 	ddi_dma_freehdl,
2850Sstevel@tonic-gate 	ddi_dma_bindhdl,
2860Sstevel@tonic-gate 	ddi_dma_unbindhdl,
2870Sstevel@tonic-gate 	ddi_dma_flush,
2880Sstevel@tonic-gate 	ddi_dma_win,
2890Sstevel@tonic-gate 	ddi_dma_mctl,
2900Sstevel@tonic-gate 	db_ctlops,
2910Sstevel@tonic-gate 	ddi_bus_prop_op,
2920Sstevel@tonic-gate 	ndi_busop_get_eventcookie,
2930Sstevel@tonic-gate 	ndi_busop_add_eventcall,
2940Sstevel@tonic-gate 	ndi_busop_remove_eventcall,
2950Sstevel@tonic-gate 	ndi_post_event,
2960Sstevel@tonic-gate 	0,
2970Sstevel@tonic-gate 	0,
2980Sstevel@tonic-gate 	0,
2990Sstevel@tonic-gate 	db_fm_init_child,
3000Sstevel@tonic-gate 	NULL,
3010Sstevel@tonic-gate 	db_bus_enter,
3020Sstevel@tonic-gate 	db_bus_exit,
3030Sstevel@tonic-gate 	0,
3040Sstevel@tonic-gate 	db_intr_ops
3050Sstevel@tonic-gate };
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate static int	db_open(dev_t *dev_p, int flag, int otyp, cred_t *cred_p);
3080Sstevel@tonic-gate static int	db_close(dev_t dev, int flag, int otyp, cred_t *cred_p);
3090Sstevel@tonic-gate static int	db_ioctl(dev_t dev, int cmd, intptr_t arg, int flag,
3100Sstevel@tonic-gate 			cred_t *cred_p, int *rval_p);
3110Sstevel@tonic-gate #ifdef	DB_DEBUG
3120Sstevel@tonic-gate static dev_info_t *db_lookup_child_name(db_ctrl_t *dbp, char *name,
3130Sstevel@tonic-gate 			int instance);
3140Sstevel@tonic-gate static void	db_pci_get_header(ddi_acc_handle_t config_handle,
3150Sstevel@tonic-gate 			db_pci_header_t *ph, off_t hdr_off);
3160Sstevel@tonic-gate static void	db_pci_get_conf_regs(ddi_acc_handle_t config_handle,
3170Sstevel@tonic-gate 			db_conf_regs_t *cr);
3180Sstevel@tonic-gate #endif	/* DB_DEBUG */
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate #ifdef DEBUG
3210Sstevel@tonic-gate static void
3220Sstevel@tonic-gate db_debug(uint64_t func_id, dev_info_t *dip, char *fmt,
3230Sstevel@tonic-gate 	uintptr_t a1, uintptr_t a2, uintptr_t a3, uintptr_t a4, uintptr_t a5);
3240Sstevel@tonic-gate #endif
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate static int db_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op,
3270Sstevel@tonic-gate     int flags, char *name, caddr_t valuep, int *lengthp);
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate static struct cb_ops db_cb_ops = {
3300Sstevel@tonic-gate 	db_open,			/* open */
3310Sstevel@tonic-gate 	db_close,			/* close */
3320Sstevel@tonic-gate 	nulldev,			/* strategy */
3330Sstevel@tonic-gate 	nulldev,			/* print */
3340Sstevel@tonic-gate 	nulldev,			/* dump */
3350Sstevel@tonic-gate 	nulldev,			/* read */
3360Sstevel@tonic-gate 	nulldev,			/* write */
3370Sstevel@tonic-gate 	db_ioctl,			/* ioctl */
3380Sstevel@tonic-gate 	nodev,				/* devmap */
3390Sstevel@tonic-gate 	nodev,				/* mmap */
3400Sstevel@tonic-gate 	nodev,				/* segmap */
3410Sstevel@tonic-gate 	nochpoll,			/* poll */
3420Sstevel@tonic-gate 	db_prop_op,			/* cb_prop_op */
3430Sstevel@tonic-gate 	NULL,				/* streamtab */
3440Sstevel@tonic-gate 	D_NEW | D_MP | D_HOTPLUG,	/* Driver compatibility flag */
3450Sstevel@tonic-gate 	CB_REV,				/* rev */
3460Sstevel@tonic-gate 	nodev,				/* int (*cb_aread)() */
3470Sstevel@tonic-gate 	nodev				/* int (*cb_awrite)() */
3480Sstevel@tonic-gate };
3490Sstevel@tonic-gate 
3500Sstevel@tonic-gate static uint8_t 	db_ddi_get8(ddi_acc_impl_t *handle, uint8_t *addr);
3510Sstevel@tonic-gate static uint16_t db_ddi_get16(ddi_acc_impl_t *handle, uint16_t *addr);
3520Sstevel@tonic-gate static uint32_t db_ddi_get32(ddi_acc_impl_t *handle, uint32_t *addr);
3530Sstevel@tonic-gate static uint64_t db_ddi_get64(ddi_acc_impl_t *handle, uint64_t *addr);
3540Sstevel@tonic-gate static void 	db_ddi_put8(ddi_acc_impl_t *handle, uint8_t *addr,
3550Sstevel@tonic-gate 			uint8_t data);
3560Sstevel@tonic-gate static void 	db_ddi_put16(ddi_acc_impl_t *handle, uint16_t *addr,
3570Sstevel@tonic-gate 			uint16_t data);
3580Sstevel@tonic-gate static void 	db_ddi_put32(ddi_acc_impl_t *handle, uint32_t *addr,
3590Sstevel@tonic-gate 			uint32_t data);
3600Sstevel@tonic-gate static void 	db_ddi_put64(ddi_acc_impl_t *handle, uint64_t *addr,
3610Sstevel@tonic-gate 			uint64_t data);
3620Sstevel@tonic-gate static void 	db_ddi_rep_get8(ddi_acc_impl_t *handle, uint8_t *host_addr,
3630Sstevel@tonic-gate 			uint8_t *dev_addr, size_t repcount, uint_t flags);
3640Sstevel@tonic-gate static void 	db_ddi_rep_get16(ddi_acc_impl_t *handle, uint16_t *host_addr,
3650Sstevel@tonic-gate 			uint16_t *dev_addr, size_t repcount, uint_t flags);
3660Sstevel@tonic-gate static void 	db_ddi_rep_get32(ddi_acc_impl_t *handle, uint32_t *host_addr,
3670Sstevel@tonic-gate 			uint32_t *dev_addr, size_t repcount, uint_t flags);
3680Sstevel@tonic-gate static void 	db_ddi_rep_get64(ddi_acc_impl_t *handle, uint64_t *host_addr,
3690Sstevel@tonic-gate 			uint64_t *dev_addr, size_t repcount, uint_t flags);
3700Sstevel@tonic-gate static void 	db_ddi_rep_put8(ddi_acc_impl_t *handle, uint8_t *host_addr,
3710Sstevel@tonic-gate 			uint8_t *dev_addr, size_t repcount, uint_t flags);
3720Sstevel@tonic-gate static void 	db_ddi_rep_put16(ddi_acc_impl_t *handle, uint16_t *host_addr,
3730Sstevel@tonic-gate 			uint16_t *dev_addr, size_t repcount, uint_t flags);
3740Sstevel@tonic-gate static void 	db_ddi_rep_put32(ddi_acc_impl_t *handle, uint32_t *host_addr,
3750Sstevel@tonic-gate 			uint32_t *dev_addr, size_t repcount, uint_t flags);
3760Sstevel@tonic-gate static void 	db_ddi_rep_put64(ddi_acc_impl_t *handle, uint64_t *host_addr,
3770Sstevel@tonic-gate 			uint64_t *dev_addr, size_t repcount, uint_t flags);
3780Sstevel@tonic-gate 
3790Sstevel@tonic-gate static struct dev_ops db_dev_ops = {
3800Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev */
3810Sstevel@tonic-gate 	0,			/* refcnt  */
3820Sstevel@tonic-gate 	db_getinfo,		/* info */
3830Sstevel@tonic-gate 	nulldev,		/* identify */
3840Sstevel@tonic-gate 	nulldev,		/* probe */
3850Sstevel@tonic-gate 	db_attach,		/* attach */
3860Sstevel@tonic-gate 	db_detach,		/* detach */
3870Sstevel@tonic-gate 	nulldev,		/* reset */
3880Sstevel@tonic-gate 	&db_cb_ops,		/* driver operations */
3890Sstevel@tonic-gate 	&db_bus_ops,		/* bus operations */
3900Sstevel@tonic-gate 	ddi_power
3910Sstevel@tonic-gate };
3920Sstevel@tonic-gate 
3930Sstevel@tonic-gate 
3940Sstevel@tonic-gate /*
3950Sstevel@tonic-gate  * Module linkage information for the kernel.
3960Sstevel@tonic-gate  */
3970Sstevel@tonic-gate 
3980Sstevel@tonic-gate static struct modldrv modldrv = {
3990Sstevel@tonic-gate 	&mod_driverops, /* Type of module */
4000Sstevel@tonic-gate 	DB_MODINFO_DESCRIPTION,
4010Sstevel@tonic-gate 	&db_dev_ops	/* driver ops */
4020Sstevel@tonic-gate };
4030Sstevel@tonic-gate 
4040Sstevel@tonic-gate static struct modlinkage modlinkage = {
4050Sstevel@tonic-gate 	MODREV_1,
4060Sstevel@tonic-gate 	(void *)&modldrv,
4070Sstevel@tonic-gate 	NULL
4080Sstevel@tonic-gate };
4090Sstevel@tonic-gate 
4100Sstevel@tonic-gate /* soft state pointer and structure template. */
4110Sstevel@tonic-gate static void 	*db_state;
4120Sstevel@tonic-gate 
4130Sstevel@tonic-gate /*
4140Sstevel@tonic-gate  * forward function declarations:
4150Sstevel@tonic-gate  */
4160Sstevel@tonic-gate static void	db_uninitchild(dev_info_t *);
4170Sstevel@tonic-gate static int 	db_initchild(dev_info_t *child);
4180Sstevel@tonic-gate static int 	db_create_pci_prop(dev_info_t *child);
4190Sstevel@tonic-gate static int 	db_save_config_regs(db_ctrl_t *dbp);
4200Sstevel@tonic-gate static int 	db_restore_config_regs(db_ctrl_t *dbp);
4210Sstevel@tonic-gate 
4220Sstevel@tonic-gate /*
4230Sstevel@tonic-gate  * FMA error callback
4240Sstevel@tonic-gate  * Register error handling callback with our parent. We will just call
4250Sstevel@tonic-gate  * our children's error callbacks and return their status.
4260Sstevel@tonic-gate  */
4270Sstevel@tonic-gate static int db_err_callback(dev_info_t *dip, ddi_fm_error_t *derr,
4280Sstevel@tonic-gate 		const void *impl_data);
4290Sstevel@tonic-gate 
4300Sstevel@tonic-gate /*
4310Sstevel@tonic-gate  * init/fini routines to alloc/dealloc fm structures and
4320Sstevel@tonic-gate  * register/unregister our callback.
4330Sstevel@tonic-gate  */
4340Sstevel@tonic-gate static void db_fm_init(db_ctrl_t *db_p);
4350Sstevel@tonic-gate static void db_fm_fini(db_ctrl_t *db_p);
4360Sstevel@tonic-gate 
4370Sstevel@tonic-gate int
4380Sstevel@tonic-gate _init(void)
4390Sstevel@tonic-gate {
4400Sstevel@tonic-gate 	int rc;
4410Sstevel@tonic-gate 
4420Sstevel@tonic-gate 	DB_DEBUG0(DB_INIT|DB_DONT_DISPLAY_DIP, NULL, "enter\n");
4430Sstevel@tonic-gate 	if (((rc = ddi_soft_state_init(&db_state,
4440Sstevel@tonic-gate 			sizeof (db_ctrl_t), 1)) == 0) &&
4450Sstevel@tonic-gate 			((rc = mod_install(&modlinkage)) != 0))
4460Sstevel@tonic-gate 		ddi_soft_state_fini(&db_state);
4470Sstevel@tonic-gate 	DB_DEBUG1(DB_INIT|DB_DONT_DISPLAY_DIP, NULL, "exit rc=%d\n", rc);
4480Sstevel@tonic-gate 	return (rc);
4490Sstevel@tonic-gate }
4500Sstevel@tonic-gate 
4510Sstevel@tonic-gate 
4520Sstevel@tonic-gate int
4530Sstevel@tonic-gate _fini(void)
4540Sstevel@tonic-gate {
4550Sstevel@tonic-gate 	int rc;
4560Sstevel@tonic-gate 
4570Sstevel@tonic-gate 	DB_DEBUG0(DB_FINI|DB_DONT_DISPLAY_DIP, NULL, "enter\n");
4580Sstevel@tonic-gate 	if ((rc = mod_remove(&modlinkage)) == 0)
4590Sstevel@tonic-gate 		ddi_soft_state_fini(&db_state);
4600Sstevel@tonic-gate 	DB_DEBUG1(DB_FINI|DB_DONT_DISPLAY_DIP, NULL, "exit rc=%d\n", rc);
4610Sstevel@tonic-gate 	return (rc);
4620Sstevel@tonic-gate }
4630Sstevel@tonic-gate 
4640Sstevel@tonic-gate int
4650Sstevel@tonic-gate _info(struct modinfo *modinfop)
4660Sstevel@tonic-gate {
4670Sstevel@tonic-gate 	int rc;
4680Sstevel@tonic-gate 	rc = mod_info(&modlinkage, modinfop);
4690Sstevel@tonic-gate 	DB_DEBUG1(DB_INFO|DB_DONT_DISPLAY_DIP, NULL, "exit rc=%d\n", rc);
4700Sstevel@tonic-gate 	return (rc);
4710Sstevel@tonic-gate }
4720Sstevel@tonic-gate 
4730Sstevel@tonic-gate /*ARGSUSED*/
4740Sstevel@tonic-gate static int
4750Sstevel@tonic-gate db_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
4760Sstevel@tonic-gate {
4770Sstevel@tonic-gate 	db_ctrl_t *dbp;
4780Sstevel@tonic-gate 	int rc = DDI_FAILURE;
4790Sstevel@tonic-gate 	minor_t		minor = getminor((dev_t)arg);
4800Sstevel@tonic-gate 	int		instance = PCIHP_AP_MINOR_NUM_TO_INSTANCE(minor);
4810Sstevel@tonic-gate 
4820Sstevel@tonic-gate 	DB_DEBUG1(DB_GETINFO|DB_DONT_DISPLAY_DIP, dip, "enter:cmd=%d\n",
4830Sstevel@tonic-gate 		infocmd);
4840Sstevel@tonic-gate 
4850Sstevel@tonic-gate 	switch (infocmd) {
4860Sstevel@tonic-gate 		case DDI_INFO_DEVT2DEVINFO:
4870Sstevel@tonic-gate 
4880Sstevel@tonic-gate 			if ((dbp = ddi_get_soft_state(db_state,
4890Sstevel@tonic-gate 			    instance)) != NULL) {
4900Sstevel@tonic-gate 				*result = dbp->dip;
4910Sstevel@tonic-gate 				rc = DDI_SUCCESS;
4920Sstevel@tonic-gate 			} else
4930Sstevel@tonic-gate 				*result = NULL;
4940Sstevel@tonic-gate 			break;
4950Sstevel@tonic-gate 
4960Sstevel@tonic-gate 		case DDI_INFO_DEVT2INSTANCE:
497*946Smathue 			*result = (void *)(uintptr_t)instance;
4980Sstevel@tonic-gate 			rc = DDI_SUCCESS;
4990Sstevel@tonic-gate 			break;
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate 		default:
5020Sstevel@tonic-gate 			break;
5030Sstevel@tonic-gate 	}
5040Sstevel@tonic-gate 	DB_DEBUG2(DB_GETINFO|DB_DONT_DISPLAY_DIP, dip,
5050Sstevel@tonic-gate 		"exit: result=%x, rc=%d\n", *result, rc);
5060Sstevel@tonic-gate 
5070Sstevel@tonic-gate 	return (rc);
5080Sstevel@tonic-gate }
5090Sstevel@tonic-gate 
5100Sstevel@tonic-gate static int
5110Sstevel@tonic-gate db_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
5120Sstevel@tonic-gate {
5130Sstevel@tonic-gate 	int instance = ddi_get_instance(dip);
5140Sstevel@tonic-gate 	db_ctrl_t	*dbp;
5150Sstevel@tonic-gate 	int		rc = DDI_SUCCESS;
5160Sstevel@tonic-gate 	ddi_device_acc_attr_t db_csr_attr = {	/* CSR map attributes */
5170Sstevel@tonic-gate 		DDI_DEVICE_ATTR_V0,
5180Sstevel@tonic-gate 		DDI_STRUCTURE_LE_ACC,
5190Sstevel@tonic-gate 		DDI_STRICTORDER_ACC
5200Sstevel@tonic-gate 	};
5210Sstevel@tonic-gate 	off_t bar_size;
5220Sstevel@tonic-gate 	int range_size;
5230Sstevel@tonic-gate 	char name[32];
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate 	DB_DEBUG1(DB_ATTACH, dip, "enter: cmd=%d\n", cmd);
5260Sstevel@tonic-gate 	switch (cmd) {
5270Sstevel@tonic-gate 
5280Sstevel@tonic-gate 	case DDI_ATTACH:
5290Sstevel@tonic-gate 		if (ddi_soft_state_zalloc(db_state, instance) != DDI_SUCCESS) {
5300Sstevel@tonic-gate 			rc = DDI_FAILURE;
5310Sstevel@tonic-gate 			break;
5320Sstevel@tonic-gate 		}
5330Sstevel@tonic-gate 
5340Sstevel@tonic-gate 		dbp = (db_ctrl_t *)ddi_get_soft_state(db_state, instance);
5350Sstevel@tonic-gate 
5360Sstevel@tonic-gate 		dbp->dip = dip;
5370Sstevel@tonic-gate 		mutex_init(&dbp->db_mutex, NULL, MUTEX_DRIVER, NULL);
5380Sstevel@tonic-gate 		dbp->db_soft_state = DB_SOFT_STATE_CLOSED;
5390Sstevel@tonic-gate 
5400Sstevel@tonic-gate 		/*
5410Sstevel@tonic-gate 		 * Cannot use pci_config_setup here as we'd need
5420Sstevel@tonic-gate 		 * to get a pointer to the address map to be able
5430Sstevel@tonic-gate 		 * to set the bus private handle during child map
5440Sstevel@tonic-gate 		 * operation.
5450Sstevel@tonic-gate 		 */
5460Sstevel@tonic-gate 		if ((rc = ddi_regs_map_setup(dip, DB_PCI_CONF_RNUMBER,
5470Sstevel@tonic-gate 			(caddr_t *)&dbp->conf_io, DB_PCI_CONF_OFFSET,
5480Sstevel@tonic-gate 			PCI_CONF_HDR_SIZE, &db_csr_attr, &dbp->conf_handle))
5490Sstevel@tonic-gate 							!= DDI_SUCCESS) {
5500Sstevel@tonic-gate 
5510Sstevel@tonic-gate 			cmn_err(CE_WARN,
5520Sstevel@tonic-gate 				"%s#%d: cannot map configuration space",
5530Sstevel@tonic-gate 				ddi_driver_name(dip), ddi_get_instance(dip));
5540Sstevel@tonic-gate 			mutex_destroy(&dbp->db_mutex);
5550Sstevel@tonic-gate 			ddi_soft_state_free(db_state, instance);
5560Sstevel@tonic-gate 			rc = DDI_FAILURE;
5570Sstevel@tonic-gate 			break;
5580Sstevel@tonic-gate 		}
5590Sstevel@tonic-gate 
5600Sstevel@tonic-gate 		db_get_perf_parameters(dbp);
5610Sstevel@tonic-gate 
5620Sstevel@tonic-gate 		if (ddi_dev_regsize(dip, DB_CSR_MEMBAR_RNUMBER, &bar_size)
5630Sstevel@tonic-gate 				!= DDI_SUCCESS) {
5640Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s#%d: cannot get memory CSR size",
5650Sstevel@tonic-gate 				ddi_driver_name(dbp->dip),
5660Sstevel@tonic-gate 				ddi_get_instance(dbp->dip));
5670Sstevel@tonic-gate 			ddi_regs_map_free(&dbp->conf_handle);
5680Sstevel@tonic-gate 			mutex_destroy(&dbp->db_mutex);
5690Sstevel@tonic-gate 			ddi_soft_state_free(db_state, instance);
5700Sstevel@tonic-gate 			rc = DDI_FAILURE;
5710Sstevel@tonic-gate 			break;
5720Sstevel@tonic-gate 		}
5730Sstevel@tonic-gate 
5740Sstevel@tonic-gate 		/* map memory CSR space */
5750Sstevel@tonic-gate 		if (ddi_regs_map_setup(dip, DB_CSR_MEMBAR_RNUMBER,
5760Sstevel@tonic-gate 			(caddr_t *)&dbp->csr_mem, DB_CSR_MEM_OFFSET, bar_size,
5770Sstevel@tonic-gate 			&db_csr_attr, &dbp->csr_mem_handle) != DDI_SUCCESS) {
5780Sstevel@tonic-gate 
5790Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s#%d: cannot map memory CSR space",
5800Sstevel@tonic-gate 				ddi_driver_name(dbp->dip),
5810Sstevel@tonic-gate 				ddi_get_instance(dbp->dip));
5820Sstevel@tonic-gate 			ddi_regs_map_free(&dbp->conf_handle);
5830Sstevel@tonic-gate 			mutex_destroy(&dbp->db_mutex);
5840Sstevel@tonic-gate 			ddi_soft_state_free(db_state, instance);
5850Sstevel@tonic-gate 			rc = DDI_FAILURE;
5860Sstevel@tonic-gate 			break;
5870Sstevel@tonic-gate 		}
5880Sstevel@tonic-gate 
5890Sstevel@tonic-gate 		if (ddi_dev_regsize(dip, DB_CSR_IOBAR_RNUMBER, &bar_size)
5900Sstevel@tonic-gate 				!= DDI_SUCCESS) {
5910Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s#%d: cannot get IO CSR size",
5920Sstevel@tonic-gate 				ddi_driver_name(dbp->dip),
5930Sstevel@tonic-gate 				ddi_get_instance(dbp->dip));
5940Sstevel@tonic-gate 			ddi_regs_map_free(&dbp->csr_mem_handle);
5950Sstevel@tonic-gate 			ddi_regs_map_free(&dbp->conf_handle);
5960Sstevel@tonic-gate 			mutex_destroy(&dbp->db_mutex);
5970Sstevel@tonic-gate 			ddi_soft_state_free(db_state, instance);
5980Sstevel@tonic-gate 			rc = DDI_FAILURE;
5990Sstevel@tonic-gate 			break;
6000Sstevel@tonic-gate 		}
6010Sstevel@tonic-gate 
6020Sstevel@tonic-gate 		/*
6030Sstevel@tonic-gate 		 * map IO CSR space. We need this map to initiate
6040Sstevel@tonic-gate 		 * indirect configuration transactions as this is a better
6050Sstevel@tonic-gate 		 * option than doing through configuration space map.
6060Sstevel@tonic-gate 		 */
6070Sstevel@tonic-gate 		if (ddi_regs_map_setup(dip, DB_CSR_IOBAR_RNUMBER,
6080Sstevel@tonic-gate 			(caddr_t *)&dbp->csr_io, DB_CSR_IO_OFFSET, bar_size,
6090Sstevel@tonic-gate 			&db_csr_attr, &dbp->csr_io_handle) != DDI_SUCCESS) {
6100Sstevel@tonic-gate 
6110Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s#%d: cannot map IO CSR space",
6120Sstevel@tonic-gate 				ddi_driver_name(dbp->dip),
6130Sstevel@tonic-gate 				ddi_get_instance(dbp->dip));
6140Sstevel@tonic-gate 			ddi_regs_map_free(&dbp->csr_mem_handle);
6150Sstevel@tonic-gate 			ddi_regs_map_free(&dbp->conf_handle);
6160Sstevel@tonic-gate 			mutex_destroy(&dbp->db_mutex);
6170Sstevel@tonic-gate 			ddi_soft_state_free(db_state, instance);
6180Sstevel@tonic-gate 			rc = DDI_FAILURE;
6190Sstevel@tonic-gate 			break;
6200Sstevel@tonic-gate 		}
6210Sstevel@tonic-gate 
6220Sstevel@tonic-gate 		db_orientation(dbp);
6230Sstevel@tonic-gate 
6240Sstevel@tonic-gate 		if (dbp->dev_state & DB_SECONDARY_NEXUS) {
6250Sstevel@tonic-gate 			if (pcihp_init(dip) != DDI_SUCCESS)
6260Sstevel@tonic-gate 				cmn_err(CE_WARN,
6270Sstevel@tonic-gate 				    "%s#%d: could not register with hotplug",
6280Sstevel@tonic-gate 				    ddi_driver_name(dbp->dip),
6290Sstevel@tonic-gate 				    ddi_get_instance(dbp->dip));
6300Sstevel@tonic-gate 		} else {
6310Sstevel@tonic-gate 			/*
6320Sstevel@tonic-gate 			 * create minor node for devctl interfaces
6330Sstevel@tonic-gate 			 */
6340Sstevel@tonic-gate 			if (ddi_create_minor_node(dip, "devctl", S_IFCHR,
6350Sstevel@tonic-gate 			    PCIHP_AP_MINOR_NUM(instance, PCIHP_DEVCTL_MINOR),
6360Sstevel@tonic-gate 			    DDI_NT_NEXUS, 0) != DDI_SUCCESS) {
6370Sstevel@tonic-gate 				ddi_regs_map_free(&dbp->csr_io_handle);
6380Sstevel@tonic-gate 				ddi_regs_map_free(&dbp->csr_mem_handle);
6390Sstevel@tonic-gate 				ddi_regs_map_free(&dbp->conf_handle);
6400Sstevel@tonic-gate 				mutex_destroy(&dbp->db_mutex);
6410Sstevel@tonic-gate 				ddi_soft_state_free(db_state, instance);
6420Sstevel@tonic-gate 				rc = DDI_FAILURE;
6430Sstevel@tonic-gate 				break;
6440Sstevel@tonic-gate 			}
6450Sstevel@tonic-gate 		}
6460Sstevel@tonic-gate 
6470Sstevel@tonic-gate 		db_enable_io(dbp);
6480Sstevel@tonic-gate 
6490Sstevel@tonic-gate 		range_size = sizeof (dbp->range);
650506Scth 		if (ddi_getlongprop_buf(DDI_DEV_T_ANY, dip,
6510Sstevel@tonic-gate 			DDI_PROP_DONTPASS, "bus-range", (caddr_t)&dbp->range,
6520Sstevel@tonic-gate 				&range_size) != DDI_SUCCESS) {
6530Sstevel@tonic-gate 
6540Sstevel@tonic-gate 			cmn_err(CE_WARN,
6550Sstevel@tonic-gate 				"%s#%d: cannot get bus-range property",
6560Sstevel@tonic-gate 				ddi_driver_name(dip), ddi_get_instance(dip));
6570Sstevel@tonic-gate 
6580Sstevel@tonic-gate 			if (dbp->dev_state & DB_SECONDARY_NEXUS)
6590Sstevel@tonic-gate 				(void) pcihp_uninit(dip);
6600Sstevel@tonic-gate 			else
6610Sstevel@tonic-gate 				ddi_remove_minor_node(dip, "devctl");
6620Sstevel@tonic-gate 
6630Sstevel@tonic-gate 			ddi_regs_map_free(&dbp->csr_mem_handle);
6640Sstevel@tonic-gate 			ddi_regs_map_free(&dbp->csr_io_handle);
6650Sstevel@tonic-gate 			ddi_regs_map_free(&dbp->conf_handle);
6660Sstevel@tonic-gate 			mutex_destroy(&dbp->db_mutex);
6670Sstevel@tonic-gate 			ddi_soft_state_free(db_state, instance);
6680Sstevel@tonic-gate 			rc = DDI_FAILURE;
6690Sstevel@tonic-gate 			break;
6700Sstevel@tonic-gate 		}
6710Sstevel@tonic-gate 
6720Sstevel@tonic-gate 		(void) sprintf(name, "%d", instance);
6730Sstevel@tonic-gate 
6740Sstevel@tonic-gate 		if (ddi_create_minor_node(dip, name, S_IFCHR,
6750Sstevel@tonic-gate 		    PCIHP_AP_MINOR_NUM(instance, PCIHP_DEBUG_MINOR),
6760Sstevel@tonic-gate 		    NULL, NULL) == DDI_FAILURE) {
6770Sstevel@tonic-gate 			cmn_err(CE_NOTE, "%s#%d: node creation failure",
6780Sstevel@tonic-gate 					ddi_driver_name(dbp->dip), instance);
6790Sstevel@tonic-gate 		}
6800Sstevel@tonic-gate 
6810Sstevel@tonic-gate 		mutex_init(&dbp->db_busown, NULL, MUTEX_DRIVER, NULL);
6820Sstevel@tonic-gate 
6830Sstevel@tonic-gate 		db_fm_init(dbp);
6840Sstevel@tonic-gate 		ddi_report_dev(dip);
6850Sstevel@tonic-gate 		dbp->dev_state |= DB_ATTACHED;
6860Sstevel@tonic-gate 
6870Sstevel@tonic-gate 		break;
6880Sstevel@tonic-gate 
6890Sstevel@tonic-gate 	case DDI_RESUME:
6900Sstevel@tonic-gate 
6910Sstevel@tonic-gate 		/*
6920Sstevel@tonic-gate 		 * Get the soft state structure for the bridge.
6930Sstevel@tonic-gate 		 */
6940Sstevel@tonic-gate 		dbp = (db_ctrl_t *)ddi_get_soft_state(db_state, instance);
6950Sstevel@tonic-gate 		db_enable_io(dbp);
6960Sstevel@tonic-gate 		(void) db_restore_config_regs(dbp);
6970Sstevel@tonic-gate 		dbp->dev_state &= ~DB_SUSPENDED;
6980Sstevel@tonic-gate 		break;
6990Sstevel@tonic-gate 
7000Sstevel@tonic-gate 	default:
7010Sstevel@tonic-gate 		rc = DDI_FAILURE;	/* not supported yet */
7020Sstevel@tonic-gate 		break;
7030Sstevel@tonic-gate 	}
7040Sstevel@tonic-gate 
7050Sstevel@tonic-gate 	DB_DEBUG1(DB_ATTACH, dip, "exit: rc=%d\n", rc);
7060Sstevel@tonic-gate 	return (rc);
7070Sstevel@tonic-gate }
7080Sstevel@tonic-gate 
7090Sstevel@tonic-gate static int
7100Sstevel@tonic-gate db_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
7110Sstevel@tonic-gate {
7120Sstevel@tonic-gate 	int instance = ddi_get_instance(dip);
7130Sstevel@tonic-gate 	db_ctrl_t	*dbp;
7140Sstevel@tonic-gate 	int		rc = DDI_SUCCESS;
7150Sstevel@tonic-gate 	char		name[32];
7160Sstevel@tonic-gate 
7170Sstevel@tonic-gate 	dbp = (db_ctrl_t *)ddi_get_soft_state(db_state, instance);
7180Sstevel@tonic-gate 
7190Sstevel@tonic-gate 	DB_DEBUG1(DB_DETACH, dip, "enter: cmd=%d\n", cmd);
7200Sstevel@tonic-gate 
7210Sstevel@tonic-gate 	switch (cmd) {
7220Sstevel@tonic-gate 
7230Sstevel@tonic-gate 	case DDI_DETACH :
7240Sstevel@tonic-gate 		db_fm_fini(dbp);
7250Sstevel@tonic-gate 		if (dbp->dev_state & DB_SECONDARY_NEXUS)
7260Sstevel@tonic-gate 			if (pcihp_uninit(dip) == DDI_FAILURE)
7270Sstevel@tonic-gate 				return (DDI_FAILURE);
7280Sstevel@tonic-gate 		else
7290Sstevel@tonic-gate 			ddi_remove_minor_node(dip, "devctl");
7300Sstevel@tonic-gate 
7310Sstevel@tonic-gate 		mutex_destroy(&dbp->db_busown);
7320Sstevel@tonic-gate 		ddi_regs_map_free(&dbp->csr_mem_handle);
7330Sstevel@tonic-gate 		ddi_regs_map_free(&dbp->csr_io_handle);
7340Sstevel@tonic-gate 
7350Sstevel@tonic-gate 		ddi_regs_map_free(&dbp->conf_handle);
7360Sstevel@tonic-gate 		dbp->dev_state &= ~DB_ATTACHED;
7370Sstevel@tonic-gate 		(void) sprintf(name, "%d", instance);
7380Sstevel@tonic-gate 		ddi_remove_minor_node(dip, name);
7390Sstevel@tonic-gate 		mutex_destroy(&dbp->db_mutex);
7400Sstevel@tonic-gate 		ddi_soft_state_free(db_state, instance);
7410Sstevel@tonic-gate 		break;
7420Sstevel@tonic-gate 
7430Sstevel@tonic-gate 	case DDI_SUSPEND :
7440Sstevel@tonic-gate 		if (db_save_config_regs(dbp) != DDI_SUCCESS) {
7450Sstevel@tonic-gate 			cmn_err(CE_WARN,
7460Sstevel@tonic-gate 				"%s#%d: Ignoring Child state Suspend Error",
7470Sstevel@tonic-gate 				ddi_driver_name(dbp->dip),
7480Sstevel@tonic-gate 				ddi_get_instance(dbp->dip));
7490Sstevel@tonic-gate 		}
7500Sstevel@tonic-gate 		dbp->dev_state |= DB_SUSPENDED;
7510Sstevel@tonic-gate 		break;
7520Sstevel@tonic-gate 
7530Sstevel@tonic-gate 	default :
7540Sstevel@tonic-gate 		rc = DDI_FAILURE;
7550Sstevel@tonic-gate 		break;
7560Sstevel@tonic-gate 	}
7570Sstevel@tonic-gate 
7580Sstevel@tonic-gate 	DB_DEBUG1(DB_DETACH, dip, "exit: rc=%d\n", rc);
7590Sstevel@tonic-gate 	return (rc);
7600Sstevel@tonic-gate }
7610Sstevel@tonic-gate 
7620Sstevel@tonic-gate static void
7630Sstevel@tonic-gate db_get_perf_parameters(db_ctrl_t *dbp)
7640Sstevel@tonic-gate {
7650Sstevel@tonic-gate 	dbp->p_latency_timer = (int8_t)ddi_prop_get_int(DDI_DEV_T_ANY,
7660Sstevel@tonic-gate 		dbp->dip, 0, "p-latency-timer", p_latency_timer);
7670Sstevel@tonic-gate 	dbp->s_latency_timer = (int8_t)ddi_prop_get_int(DDI_DEV_T_ANY,
7680Sstevel@tonic-gate 		dbp->dip, 0, "s-latency-timer", s_latency_timer);
7690Sstevel@tonic-gate 	dbp->p_cache_line_size = (int8_t)ddi_prop_get_int(DDI_DEV_T_ANY,
7700Sstevel@tonic-gate 		dbp->dip, 0, "p-cache-line-size", p_cache_line_size);
7710Sstevel@tonic-gate 	dbp->s_cache_line_size = (int8_t)ddi_prop_get_int(DDI_DEV_T_ANY,
7720Sstevel@tonic-gate 		dbp->dip, 0, "s-cache-line-size", s_cache_line_size);
7730Sstevel@tonic-gate 	dbp->p_pwrite_threshold = (int8_t)ddi_prop_get_int(DDI_DEV_T_ANY,
7740Sstevel@tonic-gate 		dbp->dip, 0, "p-pwrite-threshold", p_pwrite_threshold);
7750Sstevel@tonic-gate 	dbp->s_pwrite_threshold = (int8_t)ddi_prop_get_int(DDI_DEV_T_ANY,
7760Sstevel@tonic-gate 		dbp->dip, 0, "s-pwrite-threshold", s_pwrite_threshold);
7770Sstevel@tonic-gate 	dbp->p_dread_threshold = (int8_t)ddi_prop_get_int(DDI_DEV_T_ANY,
7780Sstevel@tonic-gate 		dbp->dip, 0, "p-dread-threshold", p_dread_threshold);
7790Sstevel@tonic-gate 	dbp->s_dread_threshold = (int8_t)ddi_prop_get_int(DDI_DEV_T_ANY,
7800Sstevel@tonic-gate 		dbp->dip, 0, "s-dread-threshold", s_dread_threshold);
7810Sstevel@tonic-gate 	dbp->delayed_trans_order = (int8_t)ddi_prop_get_int(DDI_DEV_T_ANY,
7820Sstevel@tonic-gate 		dbp->dip, 0, "delayed-trans-order", delayed_trans_order);
7830Sstevel@tonic-gate }
7840Sstevel@tonic-gate 
7850Sstevel@tonic-gate static void
7860Sstevel@tonic-gate db_set_perf_parameters(db_ctrl_t *dbp)
7870Sstevel@tonic-gate {
7880Sstevel@tonic-gate 	uint_t	poffset = 0, soffset = 0;
7890Sstevel@tonic-gate 
7900Sstevel@tonic-gate 	if (dbp->dev_state & DB_SECONDARY_NEXUS)
7910Sstevel@tonic-gate 		poffset = DB_SCONF_PRI_HDR_OFF;
7920Sstevel@tonic-gate 	else
7930Sstevel@tonic-gate 		soffset = DB_PCONF_SEC_HDR_OFF;
7940Sstevel@tonic-gate 
7950Sstevel@tonic-gate 	if ((dbp->p_latency_timer != (int8_t)DEF_INVALID_REG_VAL) &&
7960Sstevel@tonic-gate 						(dbp->p_latency_timer != -1))
7970Sstevel@tonic-gate 		ddi_put8(dbp->conf_handle,
7980Sstevel@tonic-gate 			(uint8_t *)dbp->conf_io+poffset+PCI_CONF_LATENCY_TIMER,
7990Sstevel@tonic-gate 			dbp->p_latency_timer);
8000Sstevel@tonic-gate 	if ((dbp->s_latency_timer != (int8_t)DEF_INVALID_REG_VAL) &&
8010Sstevel@tonic-gate 						(dbp->s_latency_timer != -1))
8020Sstevel@tonic-gate 		ddi_put8(dbp->conf_handle,
8030Sstevel@tonic-gate 			(uint8_t *)dbp->conf_io+soffset+PCI_CONF_LATENCY_TIMER,
8040Sstevel@tonic-gate 			dbp->s_latency_timer);
8050Sstevel@tonic-gate 	if ((dbp->p_cache_line_size != (int8_t)DEF_INVALID_REG_VAL) &&
8060Sstevel@tonic-gate 						(dbp->p_cache_line_size != -1))
8070Sstevel@tonic-gate 		ddi_put8(dbp->conf_handle,
8080Sstevel@tonic-gate 			(uint8_t *)dbp->conf_io+poffset+PCI_CONF_CACHE_LINESZ,
8090Sstevel@tonic-gate 			dbp->p_cache_line_size);
8100Sstevel@tonic-gate 	if ((dbp->s_cache_line_size != (int8_t)DEF_INVALID_REG_VAL) &&
8110Sstevel@tonic-gate 						(dbp->s_cache_line_size != -1))
8120Sstevel@tonic-gate 		ddi_put8(dbp->conf_handle,
8130Sstevel@tonic-gate 			(uint8_t *)dbp->conf_io+soffset+PCI_CONF_CACHE_LINESZ,
8140Sstevel@tonic-gate 			dbp->s_cache_line_size);
8150Sstevel@tonic-gate 	if ((dbp->p_pwrite_threshold != (int8_t)DEF_INVALID_REG_VAL) &&
8160Sstevel@tonic-gate 					(dbp->p_pwrite_threshold != -1))
8170Sstevel@tonic-gate 		ddi_put16(dbp->conf_handle, (uint16_t *)
8180Sstevel@tonic-gate 			((uchar_t *)dbp->conf_io+DB_CONF_CHIP_CTRL1),
8190Sstevel@tonic-gate 			(ddi_get16(dbp->conf_handle, (uint16_t *)
8200Sstevel@tonic-gate 			((uchar_t *)dbp->conf_io+DB_CONF_CHIP_CTRL1)) &
8210Sstevel@tonic-gate 				~P_PW_THRESHOLD) |
8220Sstevel@tonic-gate 				(dbp->p_pwrite_threshold?P_PW_THRESHOLD:0));
8230Sstevel@tonic-gate 	if ((dbp->s_pwrite_threshold != (int8_t)DEF_INVALID_REG_VAL) &&
8240Sstevel@tonic-gate 					(dbp->s_pwrite_threshold != -1))
8250Sstevel@tonic-gate 		ddi_put16(dbp->conf_handle, (uint16_t *)
8260Sstevel@tonic-gate 			((uchar_t *)dbp->conf_io+DB_CONF_CHIP_CTRL1),
8270Sstevel@tonic-gate 			(ddi_get16(dbp->conf_handle, (uint16_t *)
8280Sstevel@tonic-gate 				((uchar_t *)dbp->conf_io+DB_CONF_CHIP_CTRL1)) &
8290Sstevel@tonic-gate 				~S_PW_THRESHOLD) |
8300Sstevel@tonic-gate 				(dbp->s_pwrite_threshold?S_PW_THRESHOLD:0));
8310Sstevel@tonic-gate 	/* primary delayed read threshold. 0x01 is reserved ?. */
8320Sstevel@tonic-gate 	if ((dbp->p_dread_threshold != (int8_t)DEF_INVALID_REG_VAL) &&
8330Sstevel@tonic-gate 					(dbp->p_dread_threshold != -1))
8340Sstevel@tonic-gate 		ddi_put16(dbp->conf_handle, (uint16_t *)
8350Sstevel@tonic-gate 			((uchar_t *)dbp->conf_io+DB_CONF_CHIP_CTRL1),
8360Sstevel@tonic-gate 			((ddi_get16(dbp->conf_handle, (uint16_t *)
8370Sstevel@tonic-gate 				((uchar_t *)dbp->conf_io+DB_CONF_CHIP_CTRL1)) &
8380Sstevel@tonic-gate 				~P_DREAD_THRESHOLD_MASK) |
8390Sstevel@tonic-gate 				((dbp->p_dread_threshold &
8400Sstevel@tonic-gate 				DREAD_THRESHOLD_VALBITS)<<2)));
8410Sstevel@tonic-gate 	/* secondary delayed read threshold. 0x01 is reserved ?. */
8420Sstevel@tonic-gate 	if ((dbp->s_dread_threshold != (int8_t)DEF_INVALID_REG_VAL) &&
8430Sstevel@tonic-gate 					(dbp->s_dread_threshold != -1))
8440Sstevel@tonic-gate 		ddi_put16(dbp->conf_handle, (uint16_t *)
8450Sstevel@tonic-gate 			((uchar_t *)dbp->conf_io+DB_CONF_CHIP_CTRL1),
8460Sstevel@tonic-gate 			((ddi_get16(dbp->conf_handle, (uint16_t *)
8470Sstevel@tonic-gate 			((uchar_t *)dbp->conf_io+DB_CONF_CHIP_CTRL1)) &
8480Sstevel@tonic-gate 				~S_DREAD_THRESHOLD_MASK) |
8490Sstevel@tonic-gate 				((dbp->s_dread_threshold &
8500Sstevel@tonic-gate 				DREAD_THRESHOLD_VALBITS)<<4)));
8510Sstevel@tonic-gate 	if ((dbp->delayed_trans_order != (int8_t)DEF_INVALID_REG_VAL) &&
8520Sstevel@tonic-gate 					(dbp->delayed_trans_order != -1))
8530Sstevel@tonic-gate 		ddi_put16(dbp->conf_handle, (uint16_t *)
8540Sstevel@tonic-gate 			((uchar_t *)dbp->conf_io+DB_CONF_CHIP_CTRL0),
8550Sstevel@tonic-gate 			(ddi_get16(dbp->conf_handle, (uint16_t *)
8560Sstevel@tonic-gate 			((uchar_t *)dbp->conf_io+DB_CONF_CHIP_CTRL0)) &
8570Sstevel@tonic-gate 			~DELAYED_TRANS_ORDER) |
8580Sstevel@tonic-gate 			(dbp->delayed_trans_order?DELAYED_TRANS_ORDER:0));
8590Sstevel@tonic-gate }
8600Sstevel@tonic-gate 
8610Sstevel@tonic-gate static void
8620Sstevel@tonic-gate db_orientation(db_ctrl_t *dbp)
8630Sstevel@tonic-gate {
8640Sstevel@tonic-gate 	dev_info_t	*dip = dbp->dip;
8650Sstevel@tonic-gate 	uint8_t		pif;
8660Sstevel@tonic-gate 	uint32_t	mem1;
8670Sstevel@tonic-gate 	uint32_t	newval;
8680Sstevel@tonic-gate 
8690Sstevel@tonic-gate 	/*
8700Sstevel@tonic-gate 	 * determine orientation of drawbridge and enable
8710Sstevel@tonic-gate 	 * Upstream or Downstream path.
8720Sstevel@tonic-gate 	 */
8730Sstevel@tonic-gate 
8740Sstevel@tonic-gate 	/*
8750Sstevel@tonic-gate 	 * if PIF is set correctly, use it to determine orientation
8760Sstevel@tonic-gate 	 */
8770Sstevel@tonic-gate 	pif = ddi_get8(dbp->conf_handle, (uchar_t *)dbp->conf_io +
8780Sstevel@tonic-gate 				PCI_CONF_PROGCLASS);
8790Sstevel@tonic-gate 	if (pif & 0xff) {
8800Sstevel@tonic-gate 		if (pif & DB_PIF_SECONDARY_TO_HOST) {
8810Sstevel@tonic-gate 			dbp->dev_state = DB_SECONDARY_NEXUS;
8820Sstevel@tonic-gate 			DB_DEBUG0(DB_ATTACH, dip,
8830Sstevel@tonic-gate 				"db_orientation: pif secondary\n");
8840Sstevel@tonic-gate 			return;
8850Sstevel@tonic-gate 		}
8860Sstevel@tonic-gate 		if (pif & DB_PIF_PRIMARY_TO_HOST) {
8870Sstevel@tonic-gate 			dbp->dev_state = DB_PRIMARY_NEXUS;
8880Sstevel@tonic-gate 			DB_DEBUG0(DB_ATTACH, dip,
8890Sstevel@tonic-gate 				"db_orientation: pif primary\n");
8900Sstevel@tonic-gate 			return;
8910Sstevel@tonic-gate 		}
8920Sstevel@tonic-gate 		/* otherwise, fall through */
8930Sstevel@tonic-gate 	}
8940Sstevel@tonic-gate 
8950Sstevel@tonic-gate 	/*
8960Sstevel@tonic-gate 	 * otherwise, test the chip directly by trying to write
8970Sstevel@tonic-gate 	 * downstream mem1 setup register, only writeable from
8980Sstevel@tonic-gate 	 * secondary.
8990Sstevel@tonic-gate 	 */
9000Sstevel@tonic-gate 	mem1 = ddi_get32(dbp->conf_handle,
9010Sstevel@tonic-gate 			(uint32_t *)((uchar_t *)dbp->conf_io +
9020Sstevel@tonic-gate 			DB_CONF_DS_IO_MEM1_SETUP));
9030Sstevel@tonic-gate 
9040Sstevel@tonic-gate 	ddi_put32(dbp->conf_handle,
9050Sstevel@tonic-gate 			(uint32_t *)((uchar_t *)(dbp->conf_io +
9060Sstevel@tonic-gate 			DB_CONF_DS_IO_MEM1_SETUP)), ~mem1);
9070Sstevel@tonic-gate 
9080Sstevel@tonic-gate 	newval = ddi_get32(dbp->conf_handle,
9090Sstevel@tonic-gate 			(uint32_t *)((uchar_t *)dbp->conf_io +
9100Sstevel@tonic-gate 			DB_CONF_DS_IO_MEM1_SETUP));
9110Sstevel@tonic-gate 
9120Sstevel@tonic-gate 	if (newval == mem1)
9130Sstevel@tonic-gate 		/* we couldn't write it, orientation is primary */
9140Sstevel@tonic-gate 		dbp->dev_state =  DB_PRIMARY_NEXUS;
9150Sstevel@tonic-gate 	else {
9160Sstevel@tonic-gate 		/*
9170Sstevel@tonic-gate 		 * we could write it, therefore orientation secondary.
9180Sstevel@tonic-gate 		 * restore mem1 value.
9190Sstevel@tonic-gate 		 */
9200Sstevel@tonic-gate 		dbp->dev_state =  DB_SECONDARY_NEXUS;
9210Sstevel@tonic-gate 		ddi_put32(dbp->conf_handle,
9220Sstevel@tonic-gate 			(uint32_t *)((uchar_t *)(dbp->conf_io +
9230Sstevel@tonic-gate 			DB_CONF_DS_IO_MEM1_SETUP)), mem1);
9240Sstevel@tonic-gate 	}
9250Sstevel@tonic-gate 
9260Sstevel@tonic-gate 
9270Sstevel@tonic-gate 	if (dbp->dev_state & DB_PRIMARY_NEXUS) {
9280Sstevel@tonic-gate 		DB_DEBUG0(DB_ATTACH, dip, "db_orientation: chip primary\n");
9290Sstevel@tonic-gate 	} else  {
9300Sstevel@tonic-gate 		DB_DEBUG0(DB_ATTACH, dip, "db_orientation: chip secondary\n");
9310Sstevel@tonic-gate 	}
9320Sstevel@tonic-gate }
9330Sstevel@tonic-gate 
9340Sstevel@tonic-gate static void
9350Sstevel@tonic-gate db_enable_io(db_ctrl_t *dbp)
9360Sstevel@tonic-gate {
9370Sstevel@tonic-gate 	dev_info_t	*dip = dbp->dip;
9380Sstevel@tonic-gate 	pci_regspec_t	*reg;
9390Sstevel@tonic-gate 	int		rcount, length, i;
9400Sstevel@tonic-gate 	uint32_t	offset;
9410Sstevel@tonic-gate 	uint32_t	p_offset, s_offset;
9420Sstevel@tonic-gate 	uint16_t	regval;
9430Sstevel@tonic-gate 	uint16_t	enable;
9440Sstevel@tonic-gate 
9450Sstevel@tonic-gate 	/*
9460Sstevel@tonic-gate 	 * Step 0:
9470Sstevel@tonic-gate 	 *	setup the primary and secondary offset and enable
9480Sstevel@tonic-gate 	 *	values based on the orientation of 21554.
9490Sstevel@tonic-gate 	 */
9500Sstevel@tonic-gate 	if (dbp->dev_state & DB_PRIMARY_NEXUS) {
9510Sstevel@tonic-gate 		DB_DEBUG0(DB_ATTACH, dip, "db_enable_io: primary\n");
9520Sstevel@tonic-gate 		p_offset = 0;
9530Sstevel@tonic-gate 		s_offset = DB_SCONF_HDR_OFF;
9540Sstevel@tonic-gate 		enable = DS_ENABLE;
9550Sstevel@tonic-gate 	} else {
9560Sstevel@tonic-gate 		DB_DEBUG0(DB_ATTACH, dip, "db_enable_io: secondary\n");
9570Sstevel@tonic-gate 		p_offset = DB_SCONF_HDR_OFF;
9580Sstevel@tonic-gate 		s_offset = 0;
9590Sstevel@tonic-gate 		enable = US_ENABLE;
9600Sstevel@tonic-gate 	}
9610Sstevel@tonic-gate 
9620Sstevel@tonic-gate 	db_set_perf_parameters(dbp);
9630Sstevel@tonic-gate 	db_set_dvma_range(dbp);
9640Sstevel@tonic-gate 
9650Sstevel@tonic-gate 	/*
9660Sstevel@tonic-gate 	 * Step 1:
9670Sstevel@tonic-gate 	 *	setup latency timer and cache line size parameters
9680Sstevel@tonic-gate 	 *	which are used for child initialization.
9690Sstevel@tonic-gate 	 */
9700Sstevel@tonic-gate 	dbp->latency_timer = ddi_get8(dbp->conf_handle, (uint8_t *)
9710Sstevel@tonic-gate 			((caddr_t)dbp->conf_io+PCI_CONF_LATENCY_TIMER));
9720Sstevel@tonic-gate 
9730Sstevel@tonic-gate 	dbp->cache_line_size = ddi_get8(dbp->conf_handle, (uint8_t *)
9740Sstevel@tonic-gate 			((caddr_t)dbp->conf_io+PCI_CONF_CACHE_LINESZ));
9750Sstevel@tonic-gate 
9760Sstevel@tonic-gate 	DB_DEBUG2(DB_ATTACH, dip,
9770Sstevel@tonic-gate 		"db_enable_io: latency %d, cache line size %d\n",
9780Sstevel@tonic-gate 		dbp->latency_timer, dbp->cache_line_size);
9790Sstevel@tonic-gate 
9800Sstevel@tonic-gate 	/*
9810Sstevel@tonic-gate 	 * Step 2: program command reg on both primary and secondary
9820Sstevel@tonic-gate 	 *	   interfaces.
9830Sstevel@tonic-gate 	 */
9840Sstevel@tonic-gate 	ddi_put16(dbp->conf_handle, (uint16_t *)((caddr_t)dbp->conf_io +
9850Sstevel@tonic-gate 			(off_t)(p_offset + PCI_CONF_COMM)), db_command_default);
9860Sstevel@tonic-gate 
9870Sstevel@tonic-gate 	ddi_put16(dbp->conf_handle, (uint16_t *)((caddr_t)dbp->conf_io +
9880Sstevel@tonic-gate 			(off_t)(s_offset + PCI_CONF_COMM)), db_command_default);
9890Sstevel@tonic-gate 
9900Sstevel@tonic-gate 	/*
9910Sstevel@tonic-gate 	 * Step 3:
9920Sstevel@tonic-gate 	 *	set up translated base registers, using the primary/
9930Sstevel@tonic-gate 	 *  secondary interface pci configuration Base Address
9940Sstevel@tonic-gate 	 *  Registers (BAR's).
9950Sstevel@tonic-gate 	 */
9960Sstevel@tonic-gate 
9970Sstevel@tonic-gate 	/* mem0 translated base is setup for primary orientation only. */
9980Sstevel@tonic-gate 	if (dbp->dev_state & DB_PRIMARY_NEXUS) {
9990Sstevel@tonic-gate 		/*
10000Sstevel@tonic-gate 		 * And only if the 21554 device node property indicates
10010Sstevel@tonic-gate 		 * the size of base0 register to be larger than csr map
10020Sstevel@tonic-gate 		 * space, DB_CSR_SIZE=4K.
10030Sstevel@tonic-gate 		 *
10040Sstevel@tonic-gate 		 * Note : Setting up 1:1 translations only (for now:), i.e.
10050Sstevel@tonic-gate 		 *	  no look up table.
10060Sstevel@tonic-gate 		 */
10070Sstevel@tonic-gate 		if (ddi_getlongprop(DDI_DEV_T_ANY, dip,
10080Sstevel@tonic-gate 				DDI_PROP_DONTPASS, "reg", (caddr_t)&reg,
10090Sstevel@tonic-gate 				&length) != DDI_PROP_SUCCESS) {
10100Sstevel@tonic-gate 			DB_DEBUG0(DB_ATTACH, dip,
10110Sstevel@tonic-gate 				"Failed to read reg property\n");
10120Sstevel@tonic-gate 			return;
10130Sstevel@tonic-gate 		}
10140Sstevel@tonic-gate 
10150Sstevel@tonic-gate 		/* Find device node's base0 reg property and check its size */
10160Sstevel@tonic-gate 		rcount = length / sizeof (pci_regspec_t);
10170Sstevel@tonic-gate 		for (i = 0; i < rcount; i++) {
10180Sstevel@tonic-gate 			offset = PCI_REG_REG_G(reg[i].pci_phys_hi);
10190Sstevel@tonic-gate 			if ((offset == PCI_CONF_BASE0) &&
10200Sstevel@tonic-gate 				(reg[i].pci_size_low > DB_CSR_SIZE))
10210Sstevel@tonic-gate 					break;
10220Sstevel@tonic-gate 		}
10230Sstevel@tonic-gate 
10240Sstevel@tonic-gate 		/*
10250Sstevel@tonic-gate 		 * set up mem0 translated base, if base0 register was
10260Sstevel@tonic-gate 		 * found and its size was larger than csr map space.
10270Sstevel@tonic-gate 		 */
10280Sstevel@tonic-gate 		if (i != rcount) {
10290Sstevel@tonic-gate 			DB_DEBUG0(DB_ATTACH, dip,
10300Sstevel@tonic-gate 				"db_enable_io: setting up MEM0_TR_BASE\n");
10310Sstevel@tonic-gate 			DB_DEBUG1(DB_ATTACH, dip, "BASE0 register = %x\n",
10320Sstevel@tonic-gate 				pci_config_get32(dbp->conf_handle,
10330Sstevel@tonic-gate 					(off_t)(p_offset + PCI_CONF_BASE0)));
10340Sstevel@tonic-gate 
10350Sstevel@tonic-gate 			pci_config_put32(dbp->conf_handle,
10360Sstevel@tonic-gate 				(off_t)DB_CONF_DS_MEM0_TR_BASE,
10370Sstevel@tonic-gate 				pci_config_get32(dbp->conf_handle,
10380Sstevel@tonic-gate 					(off_t)(p_offset + PCI_CONF_BASE0)));
10390Sstevel@tonic-gate 
10400Sstevel@tonic-gate 			DB_DEBUG1(DB_ATTACH, dip,
10410Sstevel@tonic-gate 				"db_enable_io: MEM0_TR_BASE set value = %x\n",
10420Sstevel@tonic-gate 				pci_config_get32(dbp->conf_handle,
10430Sstevel@tonic-gate 					(off_t)DB_CONF_DS_MEM0_TR_BASE));
10440Sstevel@tonic-gate 		}
10450Sstevel@tonic-gate 		kmem_free(reg, length);
10460Sstevel@tonic-gate 	}
10470Sstevel@tonic-gate 
10480Sstevel@tonic-gate 	pci_config_put32(dbp->conf_handle, (off_t)DB_CONF_DS_IO_MEM1_TR_BASE,
10490Sstevel@tonic-gate 			((pci_config_get32(dbp->conf_handle,
10500Sstevel@tonic-gate 			(off_t)(p_offset + PCI_CONF_BASE2))) & ~DB_IO_BIT));
10510Sstevel@tonic-gate 
10520Sstevel@tonic-gate 	pci_config_put32(dbp->conf_handle, (off_t)DB_CONF_DS_MEM2_TR_BASE,
10530Sstevel@tonic-gate 			((pci_config_get32(dbp->conf_handle,
10540Sstevel@tonic-gate 			(off_t)(p_offset + PCI_CONF_BASE3))) & ~DB_IO_BIT));
10550Sstevel@tonic-gate 
10560Sstevel@tonic-gate 	pci_config_put32(dbp->conf_handle, (off_t)DB_CONF_DS_MEM3_TR_BASE,
10570Sstevel@tonic-gate 			((pci_config_get32(dbp->conf_handle,
10580Sstevel@tonic-gate 			(off_t)(p_offset + PCI_CONF_BASE4))) & ~DB_IO_BIT));
10590Sstevel@tonic-gate 
10600Sstevel@tonic-gate 	pci_config_put32(dbp->conf_handle, (off_t)DB_CONF_US_IO_MEM0_TR_BASE,
10610Sstevel@tonic-gate 			((pci_config_get32(dbp->conf_handle,
10620Sstevel@tonic-gate 			(off_t)(s_offset + PCI_CONF_BASE2))) & ~DB_IO_BIT));
10630Sstevel@tonic-gate 
10640Sstevel@tonic-gate 	pci_config_put32(dbp->conf_handle, (off_t)DB_CONF_US_MEM1_TR_BASE,
10650Sstevel@tonic-gate 			((pci_config_get32(dbp->conf_handle,
10660Sstevel@tonic-gate 			(off_t)(s_offset + PCI_CONF_BASE3))) & ~DB_IO_BIT));
10670Sstevel@tonic-gate 
10680Sstevel@tonic-gate 	/*
10690Sstevel@tonic-gate 	 * Step 4: enable downstream (for primary orientation) or upstream
10700Sstevel@tonic-gate 	 *	   (for secondary orientation) bits in Configuration Control
10710Sstevel@tonic-gate 	 *	   and Status register, if not already enabled.
10720Sstevel@tonic-gate 	 */
10730Sstevel@tonic-gate 	regval = pci_config_get16(dbp->conf_handle, (off_t)DB_CONF_CONF_CSR);
10740Sstevel@tonic-gate 
10750Sstevel@tonic-gate 	DB_DEBUG1(DB_ATTACH, dip, "db_enable_io: CSR value before: %x\n",
10760Sstevel@tonic-gate 		regval);
10770Sstevel@tonic-gate 
10780Sstevel@tonic-gate 	if (!(regval & enable)) {
10790Sstevel@tonic-gate 		/* enable down/upstream configuration transactions */
10800Sstevel@tonic-gate 		regval |= enable;
10810Sstevel@tonic-gate 		pci_config_put16(dbp->conf_handle, (off_t)DB_CONF_CONF_CSR,
10820Sstevel@tonic-gate 				regval);
10830Sstevel@tonic-gate 		regval = pci_config_get16(dbp->conf_handle,
10840Sstevel@tonic-gate 				(off_t)DB_CONF_CONF_CSR);
10850Sstevel@tonic-gate 	}
10860Sstevel@tonic-gate 	DB_DEBUG1(DB_ATTACH, dip, "db_enable_io: CSR value after: %x\n",
10870Sstevel@tonic-gate 		regval);
10880Sstevel@tonic-gate 
10890Sstevel@tonic-gate 	/*
10900Sstevel@tonic-gate 	 * Step 5: enable downstream/upstream I/O (through CSR space)
10910Sstevel@tonic-gate 	 */
10920Sstevel@tonic-gate 	regval = ddi_get16(dbp->csr_mem_handle,
10930Sstevel@tonic-gate 			(uint16_t *)((uchar_t *)dbp->csr_mem + DB_CSR_IO_CSR));
10940Sstevel@tonic-gate 
10950Sstevel@tonic-gate 	DB_DEBUG1(DB_ATTACH, dip, "db_enable_io: IO_CSR value before: %x\n",
10960Sstevel@tonic-gate 		regval);
10970Sstevel@tonic-gate 	if (!(regval & enable)) {
10980Sstevel@tonic-gate 		regval |= enable;
10990Sstevel@tonic-gate 		ddi_put16(dbp->csr_mem_handle,
11000Sstevel@tonic-gate 			(uint16_t *)((uchar_t *)dbp->csr_mem +
11010Sstevel@tonic-gate 			DB_CSR_IO_CSR), regval);
11020Sstevel@tonic-gate 
11030Sstevel@tonic-gate 		regval = ddi_get16(dbp->csr_mem_handle,
11040Sstevel@tonic-gate 			(uint16_t *)((uchar_t *)dbp->csr_mem + DB_CSR_IO_CSR));
11050Sstevel@tonic-gate 	}
11060Sstevel@tonic-gate 	DB_DEBUG1(DB_ATTACH, dip, "db_enable_io: IO_CSR value after: %x\n",
11070Sstevel@tonic-gate 		regval);
11080Sstevel@tonic-gate 
11090Sstevel@tonic-gate 	/*
11100Sstevel@tonic-gate 	 * Step 6: if 21554 orientation is primary to host,
11110Sstevel@tonic-gate 	 *	   forward SERR# to host.
11120Sstevel@tonic-gate 	 */
11130Sstevel@tonic-gate 	if (dbp->dev_state & DB_PRIMARY_NEXUS) {
11140Sstevel@tonic-gate 		dbp->serr_fwd_enable = ddi_prop_get_int(DDI_DEV_T_ANY,
11150Sstevel@tonic-gate 			dbp->dip, 0, "serr-fwd-enable", db_serr_fwd_enable);
11160Sstevel@tonic-gate 
11170Sstevel@tonic-gate 		regval = ddi_get16(dbp->conf_handle,
11180Sstevel@tonic-gate 					(uint16_t *)((uchar_t *)dbp->conf_io +
11190Sstevel@tonic-gate 					DB_CONF_CHIP_CTRL0));
11200Sstevel@tonic-gate 
11210Sstevel@tonic-gate 		DB_DEBUG1(DB_ATTACH, dip,
11220Sstevel@tonic-gate 			"db_enable_io: CHIP_CTRL0 value before: %x\n", regval);
11230Sstevel@tonic-gate 
11240Sstevel@tonic-gate 		ddi_put16(dbp->conf_handle,
11250Sstevel@tonic-gate 			(uint16_t *)((uchar_t *)dbp->conf_io +
11260Sstevel@tonic-gate 			DB_CONF_CHIP_CTRL0),
11270Sstevel@tonic-gate 			(regval & ~SERR_FWD) |
11280Sstevel@tonic-gate 			(dbp->serr_fwd_enable?SERR_FWD:0));
11290Sstevel@tonic-gate 
11300Sstevel@tonic-gate 		regval = ddi_get16(dbp->conf_handle,
11310Sstevel@tonic-gate 					(uint16_t *)((uchar_t *)dbp->conf_io +
11320Sstevel@tonic-gate 					DB_CONF_CHIP_CTRL0));
11330Sstevel@tonic-gate 
11340Sstevel@tonic-gate 		DB_DEBUG1(DB_ATTACH, dip,
11350Sstevel@tonic-gate 			"db_enable_io: CHIP_CTRL0 value after: %x\n", regval);
11360Sstevel@tonic-gate 	}
11370Sstevel@tonic-gate 
11380Sstevel@tonic-gate 	/*
11390Sstevel@tonic-gate 	 * Step 7: if orientation is secondary, make sure primary lockout
11400Sstevel@tonic-gate 	 *	   disable is reset.
11410Sstevel@tonic-gate 	 */
11420Sstevel@tonic-gate 
11430Sstevel@tonic-gate 	if (dbp->dev_state & DB_SECONDARY_NEXUS) {
11440Sstevel@tonic-gate 		regval = pci_config_get16(dbp->conf_handle,
11450Sstevel@tonic-gate 					(off_t)DB_CONF_CHIP_CTRL0);
11460Sstevel@tonic-gate 		DB_DEBUG1(DB_ATTACH, dip,
11470Sstevel@tonic-gate 			"db_enable_io: chip ctrl (0x%x) before\n", regval);
11480Sstevel@tonic-gate 		if (regval & PLOCKOUT)
11490Sstevel@tonic-gate 			pci_config_put16(dbp->conf_handle,
11500Sstevel@tonic-gate 					(off_t)DB_CONF_CHIP_CTRL0,
11510Sstevel@tonic-gate 					(regval & ~PLOCKOUT));
11520Sstevel@tonic-gate 		regval = pci_config_get16(dbp->conf_handle,
11530Sstevel@tonic-gate 					(off_t)DB_CONF_CHIP_CTRL0);
11540Sstevel@tonic-gate 		DB_DEBUG1(DB_ATTACH, dip,
11550Sstevel@tonic-gate 			"db_enable_io: chip ctrl (0x%x) after\n", regval);
11560Sstevel@tonic-gate 	}
11570Sstevel@tonic-gate }
11580Sstevel@tonic-gate 
11590Sstevel@tonic-gate /*
11600Sstevel@tonic-gate  * Set DVMA Address Range.
11610Sstevel@tonic-gate  * This code is common to both orientations of the nexus driver.
11620Sstevel@tonic-gate  */
11630Sstevel@tonic-gate static void
11640Sstevel@tonic-gate db_set_dvma_range(db_ctrl_t *dbp)
11650Sstevel@tonic-gate {
11660Sstevel@tonic-gate 	uint32_t	dvma_start = 0;
11670Sstevel@tonic-gate 	uint32_t	dvma_len = 0;
11680Sstevel@tonic-gate 	uint64_t	db_allocd = 0;
11690Sstevel@tonic-gate 	uint32_t	*dvma_prop;
11700Sstevel@tonic-gate 	uint32_t	dvma_size[2];	/* dvma size may span over 2 BARs */
11710Sstevel@tonic-gate 	uint32_t	dvma_bar[2];	/* dvma range may span over 2 BARs */
11720Sstevel@tonic-gate 	int		dvma_prop_len;
11730Sstevel@tonic-gate 	uint64_t	new_dvma_start, new_dvma_len, new_dvma_end;
11740Sstevel@tonic-gate 
11750Sstevel@tonic-gate 	/*
11760Sstevel@tonic-gate 	 * Need to traverse up the tree looking for a
11770Sstevel@tonic-gate 	 * "virtual-dma" property that specifies the
11780Sstevel@tonic-gate 	 * HPB DVMA range.
11790Sstevel@tonic-gate 	 */
11800Sstevel@tonic-gate 	if (ddi_getlongprop(DDI_DEV_T_ANY, ddi_get_parent(dbp->dip), 0,
11810Sstevel@tonic-gate 		"virtual-dma", (caddr_t)&dvma_prop, &dvma_prop_len)
11820Sstevel@tonic-gate 						== DDI_SUCCESS) {
11830Sstevel@tonic-gate 		dvma_start = dvma_prop[0];
11840Sstevel@tonic-gate 		dvma_len = dvma_prop[1];
11850Sstevel@tonic-gate 		kmem_free((caddr_t)dvma_prop, dvma_prop_len);
11860Sstevel@tonic-gate 	} else {
11870Sstevel@tonic-gate 		/*
11880Sstevel@tonic-gate 		 * For initial implementation, lets avoid a warning since this
11890Sstevel@tonic-gate 		 * change has not been implemented in the host-pci nexus
11900Sstevel@tonic-gate 		 * driver.
11910Sstevel@tonic-gate 		 */
11920Sstevel@tonic-gate 		cmn_err(CE_WARN,
11930Sstevel@tonic-gate 			"%s#%d: Could not get \"virtual-dma\" property",
11940Sstevel@tonic-gate 			ddi_driver_name(dbp->dip),
11950Sstevel@tonic-gate 			ddi_get_instance(dbp->dip));
11960Sstevel@tonic-gate 		dvma_start = db_dvma_start;
11970Sstevel@tonic-gate 		dvma_len = db_dvma_len;
11980Sstevel@tonic-gate 	}
11990Sstevel@tonic-gate 
12000Sstevel@tonic-gate 	DB_DEBUG2(DB_DVMA, dbp->dip,
12010Sstevel@tonic-gate 		"DVMA Range is %lx,%lx\n", dvma_start, dvma_len);
12020Sstevel@tonic-gate 
12030Sstevel@tonic-gate 	dvma_size[0] = dvma_size[1] = 0;
12040Sstevel@tonic-gate 	/* Validate DVMA size programming and system requirements. */
12050Sstevel@tonic-gate 	if (dbp->dev_state & DB_SECONDARY_NEXUS) {
12060Sstevel@tonic-gate 		dvma_size[0] = pci_config_get32(dbp->conf_handle,
12070Sstevel@tonic-gate 				DB_CONF_DS_IO_MEM1_SETUP);
12080Sstevel@tonic-gate 		if (!(dvma_size[0] & 1)) /* make sure it is not a IO BAR */
12090Sstevel@tonic-gate 			dvma_size[0] = ((~dvma_size[0]) + 1) & 0xfffff000;
12100Sstevel@tonic-gate 		else
12110Sstevel@tonic-gate 			dvma_size[0] = 0;
12120Sstevel@tonic-gate 		dvma_size[1] = db_dvma_len;
12130Sstevel@tonic-gate 	} else {
12140Sstevel@tonic-gate 		dvma_size[0] = pci_config_get32(dbp->conf_handle,
12150Sstevel@tonic-gate 				DB_CONF_US_IO_MEM0_SETUP);
12160Sstevel@tonic-gate 		if (!(dvma_size[0] & 1)) /* make sure it is not a IO BAR */
12170Sstevel@tonic-gate 			dvma_size[0] = ((~dvma_size[0]) + 1) & 0xfffff000;
12180Sstevel@tonic-gate 		else
12190Sstevel@tonic-gate 			dvma_size[0] = 0;
12200Sstevel@tonic-gate 		dvma_size[1] = ((~(pci_config_get32(dbp->conf_handle,
12210Sstevel@tonic-gate 				DB_CONF_US_MEM1_SETUP))) + 1) & 0xfffff000;
12220Sstevel@tonic-gate 	}
12230Sstevel@tonic-gate 	DB_DEBUG2(DB_DVMA, dbp->dip, "DVMA size register pair %lx, %lx\n",
12240Sstevel@tonic-gate 		dvma_size[0], dvma_size[1]);
12250Sstevel@tonic-gate 
12260Sstevel@tonic-gate #ifdef	DEBUG
12270Sstevel@tonic-gate 	if ((dvma_size[0] + dvma_size[1]) < dvma_len)
12280Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s#%d: DVMA window (%u) does not coincide"
12290Sstevel@tonic-gate 		    " with system requirements",
12300Sstevel@tonic-gate 		ddi_driver_name(dbp->dip), ddi_get_instance(dbp->dip),
12310Sstevel@tonic-gate 		(dvma_size[0] + dvma_size[1]));
12320Sstevel@tonic-gate #endif
12330Sstevel@tonic-gate 	dvma_bar[0] = dvma_bar[1] = 0xFFFFFFFF;
12340Sstevel@tonic-gate 	db_allocd = 0;
12350Sstevel@tonic-gate 	new_dvma_start = dvma_start;
12360Sstevel@tonic-gate 	new_dvma_len = dvma_len;
12370Sstevel@tonic-gate 
12380Sstevel@tonic-gate 	/* now, program the correct DVMA range over the 2 BARs. Max 4GB */
12390Sstevel@tonic-gate 	if (dvma_size[0]) {
12400Sstevel@tonic-gate 		dvma_bar[0] = (uint32_t)(dvma_start & (~(dvma_size[0] - 1)));
12410Sstevel@tonic-gate 		new_dvma_end =  (uint64_t)((uint64_t)dvma_bar[0] +
12420Sstevel@tonic-gate 						(uint64_t)dvma_size[0]);
12430Sstevel@tonic-gate 		if (new_dvma_end > (new_dvma_start + new_dvma_len))
12440Sstevel@tonic-gate 			new_dvma_end = new_dvma_start + new_dvma_len;
12450Sstevel@tonic-gate 		db_allocd += (new_dvma_end - new_dvma_start);
12460Sstevel@tonic-gate 		new_dvma_start = new_dvma_end;
12470Sstevel@tonic-gate 		new_dvma_len = dvma_len - db_allocd;
12480Sstevel@tonic-gate 	}
12490Sstevel@tonic-gate 	/*
12500Sstevel@tonic-gate 	 * It does not serve any purpose to set the other DVMA register
12510Sstevel@tonic-gate 	 * when we have already met the memory requirements so leave it
12520Sstevel@tonic-gate 	 * disabled.
12530Sstevel@tonic-gate 	 */
12540Sstevel@tonic-gate 	if ((db_allocd != dvma_len) && dvma_size[1]) {
12550Sstevel@tonic-gate 		dvma_bar[1] = (uint32_t)((dvma_start + db_allocd) &
12560Sstevel@tonic-gate 							(~(dvma_size[1] - 1)));
12570Sstevel@tonic-gate 		new_dvma_end =  (uint64_t)((uint64_t)dvma_bar[1] +
12580Sstevel@tonic-gate 						(uint64_t)dvma_size[1]);
12590Sstevel@tonic-gate 		if (new_dvma_end > (new_dvma_start + new_dvma_len))
12600Sstevel@tonic-gate 			new_dvma_end = new_dvma_start + new_dvma_len;
12610Sstevel@tonic-gate 		db_allocd += (new_dvma_end - new_dvma_start);
12620Sstevel@tonic-gate 	}
12630Sstevel@tonic-gate 
12640Sstevel@tonic-gate 	/* In case of secondary orientation, DVMA BAR0 is 0. */
12650Sstevel@tonic-gate 	if (dbp->dev_state & DB_SECONDARY_NEXUS)
12660Sstevel@tonic-gate 		dvma_bar[0] = 0;
12670Sstevel@tonic-gate 
12680Sstevel@tonic-gate 	if (db_allocd != dvma_len) {
12690Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s#%d: dvma range error!",
12700Sstevel@tonic-gate 			ddi_driver_name(dbp->dip), ddi_get_instance(dbp->dip));
12710Sstevel@tonic-gate 	}
12720Sstevel@tonic-gate 
12730Sstevel@tonic-gate 	DB_DEBUG2(DB_DVMA, dbp->dip, "DVMA BARs set as %x, %x\n",
12740Sstevel@tonic-gate 		dvma_bar[0], dvma_bar[1]);
12750Sstevel@tonic-gate 
12760Sstevel@tonic-gate 	/* configure the setup register and DVMA BARs. */
12770Sstevel@tonic-gate 	if (dbp->dev_state & DB_SECONDARY_NEXUS) {
12780Sstevel@tonic-gate 		if (dvma_bar[0] != 0xFFFFFFFF) {
12790Sstevel@tonic-gate #ifdef	DB_SEC_SETUP_WRITE
12800Sstevel@tonic-gate 			/*
12810Sstevel@tonic-gate 			 * No need to program the setup register
12820Sstevel@tonic-gate 			 * as the PROM would have done it.
12830Sstevel@tonic-gate 			 */
12840Sstevel@tonic-gate 			pci_config_put32(dbp->conf_handle,
12850Sstevel@tonic-gate 				DB_CONF_DS_MEM1_SETUP,
12860Sstevel@tonic-gate 				(uint32_t)(((~(dvma_size[0] - 1)) |
12870Sstevel@tonic-gate 				(pci_config_get32(dbp->conf_handle,
12880Sstevel@tonic-gate 				DB_CONF_DS_MEM1_SETUP) & 0xF)) | 0x80000000));
12890Sstevel@tonic-gate #endif
12900Sstevel@tonic-gate 			/*
12910Sstevel@tonic-gate 			 * when translations are to be provided, this will
12920Sstevel@tonic-gate 			 * change.
12930Sstevel@tonic-gate 			 */
12940Sstevel@tonic-gate 			pci_config_put32(dbp->conf_handle,
12950Sstevel@tonic-gate 				DB_CONF_DS_IO_MEM1_TR_BASE,
12960Sstevel@tonic-gate 				(uint32_t)dvma_bar[0]);
12970Sstevel@tonic-gate 			pci_config_put32(dbp->conf_handle,
12980Sstevel@tonic-gate 				DB_SCONF_DS_IO_MEM1, dvma_bar[0]);
12990Sstevel@tonic-gate 		}
13000Sstevel@tonic-gate 		if (dvma_bar[1] != 0xFFFFFFFF) {
13010Sstevel@tonic-gate #ifdef	DB_SEC_SETUP_WRITE
13020Sstevel@tonic-gate 			/*
13030Sstevel@tonic-gate 			 * No need to program the setup register
13040Sstevel@tonic-gate 			 * as the PROM would have done it.
13050Sstevel@tonic-gate 			 */
13060Sstevel@tonic-gate 			pci_config_put32(dbp->conf_handle,
13070Sstevel@tonic-gate 				DB_CONF_DS_MEM2_SETUP,
13080Sstevel@tonic-gate 				(uint32_t)(((~(dvma_size[1] - 1)) |
13090Sstevel@tonic-gate 				(pci_config_get32(dbp->conf_handle,
13100Sstevel@tonic-gate 				DB_CONF_DS_MEM2_SETUP) & 0xF)) | 0x80000000));
13110Sstevel@tonic-gate #endif
13120Sstevel@tonic-gate 			/*
13130Sstevel@tonic-gate 			 * when translations are to be provided, this will
13140Sstevel@tonic-gate 			 * change.
13150Sstevel@tonic-gate 			 */
13160Sstevel@tonic-gate 			pci_config_put32(dbp->conf_handle,
13170Sstevel@tonic-gate 				DB_CONF_DS_MEM2_TR_BASE, (uint32_t)dvma_bar[1]);
13180Sstevel@tonic-gate 			pci_config_put32(dbp->conf_handle,
13190Sstevel@tonic-gate 				DB_SCONF_DS_MEM2, dvma_bar[1]);
13200Sstevel@tonic-gate 		}
13210Sstevel@tonic-gate 
13220Sstevel@tonic-gate 	} else {
13230Sstevel@tonic-gate 		if (dvma_bar[0] != 0xFFFFFFFF) {
13240Sstevel@tonic-gate #ifdef DB_CONF_P2S_WRITE_ENABLED	/* primary to secondary write enabled */
13250Sstevel@tonic-gate 			/*
13260Sstevel@tonic-gate 			 * We have a problem with this setup, because the
13270Sstevel@tonic-gate 			 * US_MEM1 setup register cannot be written from the
13280Sstevel@tonic-gate 			 * primary interface...!!! Hence in this configuration,
13290Sstevel@tonic-gate 			 * we cannot dynamically program the DVMA range!
13300Sstevel@tonic-gate 			 */
13310Sstevel@tonic-gate 			pci_config_put32(dbp->conf_handle,
13320Sstevel@tonic-gate 				DB_CONF_US_IO_MEM0_SETUP,
13330Sstevel@tonic-gate 				(uint32_t)(((~(dvma_size[0] - 1)) |
13340Sstevel@tonic-gate 				(pci_config_get32(dbp->conf_handle,
13350Sstevel@tonic-gate 				DB_CONF_US_IO_MEM0_SETUP) & 0xF)) |
13360Sstevel@tonic-gate 								0x80000000));
13370Sstevel@tonic-gate #endif
13380Sstevel@tonic-gate 			/*
13390Sstevel@tonic-gate 			 * when translations are to be provided, this will
13400Sstevel@tonic-gate 			 * change.
13410Sstevel@tonic-gate 			 */
13420Sstevel@tonic-gate 			pci_config_put32(dbp->conf_handle,
13430Sstevel@tonic-gate 				DB_CONF_US_IO_MEM0_TR_BASE,
13440Sstevel@tonic-gate 				(uint32_t)dvma_bar[0]);
13450Sstevel@tonic-gate 			pci_config_put32(dbp->conf_handle,
13460Sstevel@tonic-gate 				DB_PCONF_US_IO_MEM0, dvma_bar[0]);
13470Sstevel@tonic-gate 		}
13480Sstevel@tonic-gate 		if (dvma_bar[1] != 0xFFFFFFFF) {
13490Sstevel@tonic-gate #ifdef DB_CONF_P2S_WRITE_ENABLED	/* primary to secondary write enabled */
13500Sstevel@tonic-gate 			/*
13510Sstevel@tonic-gate 			 * We have a problem with this setup, because the
13520Sstevel@tonic-gate 			 * US_MEM1 setup register cannot be written from the
13530Sstevel@tonic-gate 			 * primary interface...!!! Hence in this configuration,
13540Sstevel@tonic-gate 			 * we cannot dynamically program the DVMA range!
13550Sstevel@tonic-gate 			 */
13560Sstevel@tonic-gate 			pci_config_put32(dbp->conf_handle,
13570Sstevel@tonic-gate 				DB_CONF_US_MEM1_SETUP,
13580Sstevel@tonic-gate 				(uint32_t)(((~(dvma_size[1] - 1)) |
13590Sstevel@tonic-gate 				(pci_config_get32(dbp->conf_handle,
13600Sstevel@tonic-gate 				DB_CONF_US_MEM1_SETUP) & 0xF)) | 0x80000000));
13610Sstevel@tonic-gate #endif
13620Sstevel@tonic-gate 			/*
13630Sstevel@tonic-gate 			 * when translations are to be provided, this will
13640Sstevel@tonic-gate 			 * change.
13650Sstevel@tonic-gate 			 */
13660Sstevel@tonic-gate 			pci_config_put32(dbp->conf_handle,
13670Sstevel@tonic-gate 				DB_CONF_US_MEM1_TR_BASE, (uint32_t)dvma_bar[1]);
13680Sstevel@tonic-gate 			pci_config_put32(dbp->conf_handle,
13690Sstevel@tonic-gate 				DB_PCONF_US_MEM1, dvma_bar[1]);
13700Sstevel@tonic-gate 		}
13710Sstevel@tonic-gate 	}
13720Sstevel@tonic-gate }
13730Sstevel@tonic-gate 
13740Sstevel@tonic-gate /*ARGSUSED*/
13750Sstevel@tonic-gate static int
13760Sstevel@tonic-gate db_open(dev_t *dev_p, int flag, int otyp, cred_t *cred_p)
13770Sstevel@tonic-gate {
13780Sstevel@tonic-gate 	minor_t		minor = getminor(*dev_p);
13790Sstevel@tonic-gate 	int		instance = PCIHP_AP_MINOR_NUM_TO_INSTANCE(minor);
13800Sstevel@tonic-gate 	db_ctrl_t *dbp = (db_ctrl_t *)ddi_get_soft_state(db_state, instance);
13810Sstevel@tonic-gate 
13820Sstevel@tonic-gate 	if (dbp == (db_ctrl_t *)NULL)
13830Sstevel@tonic-gate 		return (ENXIO);
13840Sstevel@tonic-gate 
13850Sstevel@tonic-gate 	/*
13860Sstevel@tonic-gate 	 * check for debug node
13870Sstevel@tonic-gate 	 */
13880Sstevel@tonic-gate 	if ((minor & 0xff) == 0xfe)
13890Sstevel@tonic-gate 		return (0);
13900Sstevel@tonic-gate 
13910Sstevel@tonic-gate 	if (dbp->dev_state & DB_SECONDARY_NEXUS)
13920Sstevel@tonic-gate 		return ((pcihp_get_cb_ops())->cb_open(dev_p, flag,
13930Sstevel@tonic-gate 		    otyp, cred_p));
13940Sstevel@tonic-gate 	/*
13950Sstevel@tonic-gate 	 * Handle the open by tracking the device state.
13960Sstevel@tonic-gate 	 */
13970Sstevel@tonic-gate 	mutex_enter(&dbp->db_mutex);
13980Sstevel@tonic-gate 	if (flag & FEXCL) {
13990Sstevel@tonic-gate 		if (dbp->db_soft_state != DB_SOFT_STATE_CLOSED) {
14000Sstevel@tonic-gate 			mutex_exit(&dbp->db_mutex);
14010Sstevel@tonic-gate 			return (EBUSY);
14020Sstevel@tonic-gate 		}
14030Sstevel@tonic-gate 		dbp->db_soft_state = DB_SOFT_STATE_OPEN_EXCL;
14040Sstevel@tonic-gate 	} else {
14050Sstevel@tonic-gate 		if (dbp->db_soft_state == DB_SOFT_STATE_OPEN_EXCL) {
14060Sstevel@tonic-gate 			mutex_exit(&dbp->db_mutex);
14070Sstevel@tonic-gate 			return (EBUSY);
14080Sstevel@tonic-gate 		}
14090Sstevel@tonic-gate 		dbp->db_soft_state = DB_SOFT_STATE_OPEN;
14100Sstevel@tonic-gate 	}
14110Sstevel@tonic-gate 	mutex_exit(&dbp->db_mutex);
14120Sstevel@tonic-gate 	return (0);
14130Sstevel@tonic-gate }
14140Sstevel@tonic-gate 
14150Sstevel@tonic-gate /*ARGSUSED*/
14160Sstevel@tonic-gate static int
14170Sstevel@tonic-gate db_close(dev_t dev, int flag, int otyp, cred_t *cred_p)
14180Sstevel@tonic-gate {
14190Sstevel@tonic-gate 	minor_t		minor = getminor(dev);
14200Sstevel@tonic-gate 	int		instance = PCIHP_AP_MINOR_NUM_TO_INSTANCE(minor);
14210Sstevel@tonic-gate 	db_ctrl_t *dbp = (db_ctrl_t *)ddi_get_soft_state(db_state, instance);
14220Sstevel@tonic-gate 
14230Sstevel@tonic-gate 	if (dbp == (db_ctrl_t *)NULL)
14240Sstevel@tonic-gate 		return (ENXIO);
14250Sstevel@tonic-gate 
14260Sstevel@tonic-gate 	/*
14270Sstevel@tonic-gate 	 * check for debug node
14280Sstevel@tonic-gate 	 */
14290Sstevel@tonic-gate 	if ((minor & 0xff) == 0xfe)
14300Sstevel@tonic-gate 		return (0);
14310Sstevel@tonic-gate 
14320Sstevel@tonic-gate 	if (dbp->dev_state & DB_SECONDARY_NEXUS)
14330Sstevel@tonic-gate 		return ((pcihp_get_cb_ops())->cb_close(dev, flag,
14340Sstevel@tonic-gate 		    otyp, cred_p));
14350Sstevel@tonic-gate 	mutex_enter(&dbp->db_mutex);
14360Sstevel@tonic-gate 	dbp->db_soft_state = DB_SOFT_STATE_CLOSED;
14370Sstevel@tonic-gate 	mutex_exit(&dbp->db_mutex);
14380Sstevel@tonic-gate 	return (0);
14390Sstevel@tonic-gate }
14400Sstevel@tonic-gate 
14410Sstevel@tonic-gate /*ARGSUSED*/
14420Sstevel@tonic-gate static int
14430Sstevel@tonic-gate db_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *cred_p,
14440Sstevel@tonic-gate 		int *rval_p)
14450Sstevel@tonic-gate {
14460Sstevel@tonic-gate 	int		rc = DDI_SUCCESS;
14470Sstevel@tonic-gate #ifdef	DB_DEBUG
14480Sstevel@tonic-gate 	ddi_acc_handle_t	config_handle;
14490Sstevel@tonic-gate 	db_pci_data_t	pci_data;
14500Sstevel@tonic-gate 	dev_info_t	*child_dip;
14510Sstevel@tonic-gate #endif
14520Sstevel@tonic-gate 	dev_info_t	*self;
14530Sstevel@tonic-gate 	minor_t		minor = getminor(dev);
14540Sstevel@tonic-gate 	int		instance = PCIHP_AP_MINOR_NUM_TO_INSTANCE(minor);
14550Sstevel@tonic-gate 	struct devctl_iocdata *dcp;
14560Sstevel@tonic-gate 	uint_t		bus_state;
14570Sstevel@tonic-gate 	db_ctrl_t *dbp = (db_ctrl_t *)ddi_get_soft_state(db_state, instance);
14580Sstevel@tonic-gate 
14590Sstevel@tonic-gate #ifdef	DB_DEBUG
14600Sstevel@tonic-gate 	/*
14610Sstevel@tonic-gate 	 * try this first whether were SECONDARY_NEXUS or not
14620Sstevel@tonic-gate 	 */
14630Sstevel@tonic-gate 	if (cmd == DB_PCI_READ_CONF_HEADER) {
14640Sstevel@tonic-gate 		if (ddi_copyin((caddr_t)arg, (caddr_t)&pci_data,
14650Sstevel@tonic-gate 				sizeof (db_pci_data_t), mode)) {
14660Sstevel@tonic-gate 			rc = EFAULT;
14670Sstevel@tonic-gate 			return (rc);
14680Sstevel@tonic-gate 		}
14690Sstevel@tonic-gate 
14700Sstevel@tonic-gate 		if (strcmp(pci_data.name, "") == 0) {
14710Sstevel@tonic-gate 			child_dip = dbp->dip;
14720Sstevel@tonic-gate 			(void) strcpy(pci_data.name,
14730Sstevel@tonic-gate 					ddi_get_name(dbp->dip));
14740Sstevel@tonic-gate 		} else {
14750Sstevel@tonic-gate 
14760Sstevel@tonic-gate 			if ((child_dip = db_lookup_child_name(dbp,
14770Sstevel@tonic-gate 				pci_data.name, pci_data.instance))
14780Sstevel@tonic-gate 					== (dev_info_t *)NULL) {
14790Sstevel@tonic-gate 				rc = ENXIO;
14800Sstevel@tonic-gate 				return (rc);
14810Sstevel@tonic-gate 			} else {
14820Sstevel@tonic-gate 				if (ddi_getprop(DDI_DEV_T_ANY,
14830Sstevel@tonic-gate 					child_dip, DDI_PROP_DONTPASS,
14840Sstevel@tonic-gate 					"vendor-id", DB_INVAL_VEND)
14850Sstevel@tonic-gate 						== DB_INVAL_VEND) {
14860Sstevel@tonic-gate 					/* non PCI device */
14870Sstevel@tonic-gate 					rc = EINVAL;
14880Sstevel@tonic-gate 					return (rc);
14890Sstevel@tonic-gate 				}
14900Sstevel@tonic-gate 			}
14910Sstevel@tonic-gate 		}
14920Sstevel@tonic-gate 		pci_data.instance = ddi_get_instance(child_dip);
14930Sstevel@tonic-gate 		(void) pci_config_setup(child_dip, &config_handle);
14940Sstevel@tonic-gate 		db_pci_get_header(config_handle, &pci_data.pri_hdr, 0);
14950Sstevel@tonic-gate 
14960Sstevel@tonic-gate 		/* if it is the drawbridge itself, read sec header */
14970Sstevel@tonic-gate 		if (child_dip == dbp->dip) {
14980Sstevel@tonic-gate 			db_pci_get_header(config_handle,
14990Sstevel@tonic-gate 			    &pci_data.sec_hdr, DB_PCONF_SEC_HDR_OFF);
15000Sstevel@tonic-gate 			db_pci_get_conf_regs(config_handle,
15010Sstevel@tonic-gate 			    &pci_data.conf_regs);
15020Sstevel@tonic-gate 		}
15030Sstevel@tonic-gate 		pci_config_teardown(&config_handle);
15040Sstevel@tonic-gate 
15050Sstevel@tonic-gate 		if (ddi_copyout((caddr_t)&pci_data, (caddr_t)arg,
15060Sstevel@tonic-gate 				sizeof (db_pci_data_t), mode)) {
15070Sstevel@tonic-gate 			rc = EFAULT;
15080Sstevel@tonic-gate 			return (rc);
15090Sstevel@tonic-gate 		}
15100Sstevel@tonic-gate 
15110Sstevel@tonic-gate 		return (rc);
15120Sstevel@tonic-gate 	}
15130Sstevel@tonic-gate #endif	/* DB_DEBUG */
15140Sstevel@tonic-gate 
15150Sstevel@tonic-gate 	/*
15160Sstevel@tonic-gate 	 * if secondary nexus (hotplug), then use pcihp_ioctl to do everything
15170Sstevel@tonic-gate 	 */
15180Sstevel@tonic-gate 	if (dbp->dev_state & DB_SECONDARY_NEXUS)
15190Sstevel@tonic-gate 		return ((pcihp_get_cb_ops())->cb_ioctl(dev, cmd,
15200Sstevel@tonic-gate 		    arg, mode, cred_p, rval_p));
15210Sstevel@tonic-gate 
15220Sstevel@tonic-gate 	/*
15230Sstevel@tonic-gate 	 * if not secondary nexus, we do DEVCTL_DEVICE and DEVCTL_BUS ourselves
15240Sstevel@tonic-gate 	 */
15250Sstevel@tonic-gate 	self = dbp->dip;
15260Sstevel@tonic-gate 
15270Sstevel@tonic-gate 	/*
15280Sstevel@tonic-gate 	 * We can use the generic implementation for these ioctls
15290Sstevel@tonic-gate 	 */
15300Sstevel@tonic-gate 	switch (cmd) {
15310Sstevel@tonic-gate 	case DEVCTL_DEVICE_GETSTATE:
15320Sstevel@tonic-gate 	case DEVCTL_DEVICE_ONLINE:
15330Sstevel@tonic-gate 	case DEVCTL_DEVICE_OFFLINE:
15340Sstevel@tonic-gate 	case DEVCTL_BUS_GETSTATE:
15350Sstevel@tonic-gate 		return (ndi_devctl_ioctl(self, cmd, arg, mode, 0));
15360Sstevel@tonic-gate 	}
15370Sstevel@tonic-gate 
15380Sstevel@tonic-gate 	/*
15390Sstevel@tonic-gate 	 * read devctl ioctl data
15400Sstevel@tonic-gate 	 */
15410Sstevel@tonic-gate 	if (ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS)
15420Sstevel@tonic-gate 		return (EFAULT);
15430Sstevel@tonic-gate 
15440Sstevel@tonic-gate 	switch (cmd) {
15450Sstevel@tonic-gate 
15460Sstevel@tonic-gate 	case DEVCTL_DEVICE_RESET:
15470Sstevel@tonic-gate 		rc = ENOTSUP;
15480Sstevel@tonic-gate 		break;
15490Sstevel@tonic-gate 
15500Sstevel@tonic-gate 
15510Sstevel@tonic-gate 	case DEVCTL_BUS_QUIESCE:
15520Sstevel@tonic-gate 		if (ndi_get_bus_state(self, &bus_state) == NDI_SUCCESS)
15530Sstevel@tonic-gate 			if (bus_state == BUS_QUIESCED)
15540Sstevel@tonic-gate 				break;
15550Sstevel@tonic-gate 		(void) ndi_set_bus_state(self, BUS_QUIESCED);
15560Sstevel@tonic-gate 		break;
15570Sstevel@tonic-gate 
15580Sstevel@tonic-gate 	case DEVCTL_BUS_UNQUIESCE:
15590Sstevel@tonic-gate 		if (ndi_get_bus_state(self, &bus_state) == NDI_SUCCESS)
15600Sstevel@tonic-gate 			if (bus_state == BUS_ACTIVE)
15610Sstevel@tonic-gate 				break;
15620Sstevel@tonic-gate 		(void) ndi_set_bus_state(self, BUS_ACTIVE);
15630Sstevel@tonic-gate 		break;
15640Sstevel@tonic-gate 
15650Sstevel@tonic-gate 	case DEVCTL_BUS_RESET:
15660Sstevel@tonic-gate 		rc = ENOTSUP;
15670Sstevel@tonic-gate 		break;
15680Sstevel@tonic-gate 
15690Sstevel@tonic-gate 	case DEVCTL_BUS_RESETALL:
15700Sstevel@tonic-gate 		rc = ENOTSUP;
15710Sstevel@tonic-gate 		break;
15720Sstevel@tonic-gate 
15730Sstevel@tonic-gate 	default:
15740Sstevel@tonic-gate 		rc = ENOTTY;
15750Sstevel@tonic-gate 	}
15760Sstevel@tonic-gate 
15770Sstevel@tonic-gate 	ndi_dc_freehdl(dcp);
15780Sstevel@tonic-gate 	return (rc);
15790Sstevel@tonic-gate }
15800Sstevel@tonic-gate 
15810Sstevel@tonic-gate #ifdef	DB_DEBUG
15820Sstevel@tonic-gate static dev_info_t *
15830Sstevel@tonic-gate db_lookup_child_name(db_ctrl_t *dbp, char *name, int instance)
15840Sstevel@tonic-gate {
15850Sstevel@tonic-gate 	dev_info_t *cdip, *pdip = dbp->dip;
15860Sstevel@tonic-gate 
15870Sstevel@tonic-gate 	for (cdip = ddi_get_child(pdip); cdip;
15880Sstevel@tonic-gate 				cdip = ddi_get_next_sibling(pdip)) {
15890Sstevel@tonic-gate 
15900Sstevel@tonic-gate 		do {
15910Sstevel@tonic-gate 			if (strcmp(ddi_node_name(cdip), name) == 0) {
15920Sstevel@tonic-gate 				if (instance != -1) {
15930Sstevel@tonic-gate 					if (ddi_get_instance(cdip) == instance)
15940Sstevel@tonic-gate 						return (cdip);
15950Sstevel@tonic-gate 				} else
15960Sstevel@tonic-gate 					return (cdip);
15970Sstevel@tonic-gate 			}
15980Sstevel@tonic-gate 			pdip = cdip;
15990Sstevel@tonic-gate 		} while ((cdip = ddi_get_child(pdip)));
16000Sstevel@tonic-gate 		cdip = ddi_get_next_sibling(pdip);
16010Sstevel@tonic-gate 		if (cdip == NULL) {
16020Sstevel@tonic-gate 			pdip = ddi_get_parent(pdip);
16030Sstevel@tonic-gate 			if (pdip == dbp->dip)
16040Sstevel@tonic-gate 				break;
16050Sstevel@tonic-gate 		}
16060Sstevel@tonic-gate 	}
16070Sstevel@tonic-gate 	return (NULL);
16080Sstevel@tonic-gate }
16090Sstevel@tonic-gate 
16100Sstevel@tonic-gate static void
16110Sstevel@tonic-gate db_pci_get_header(ddi_acc_handle_t config_handle, db_pci_header_t *ph,
16120Sstevel@tonic-gate     off_t hdr_off)
16130Sstevel@tonic-gate {
16140Sstevel@tonic-gate 	ph->venid = pci_config_get16(config_handle, hdr_off + PCI_CONF_VENID);
16150Sstevel@tonic-gate 	ph->devid = pci_config_get16(config_handle, hdr_off + PCI_CONF_DEVID);
16160Sstevel@tonic-gate 	ph->command = pci_config_get16(config_handle, hdr_off + PCI_CONF_COMM);
16170Sstevel@tonic-gate 	ph->status = pci_config_get16(config_handle, hdr_off + PCI_CONF_STAT);
16180Sstevel@tonic-gate 	ph->revid = pci_config_get8(config_handle, hdr_off + PCI_CONF_REVID);
16190Sstevel@tonic-gate 	ph->pif = pci_config_get8(config_handle, hdr_off + PCI_CONF_PROGCLASS);
16200Sstevel@tonic-gate 	ph->subclass = pci_config_get8(config_handle,
16210Sstevel@tonic-gate 	    hdr_off + PCI_CONF_SUBCLASS);
16220Sstevel@tonic-gate 	ph->class = pci_config_get8(config_handle,
16230Sstevel@tonic-gate 	    hdr_off + PCI_CONF_BASCLASS);
16240Sstevel@tonic-gate 	ph->cacheline = pci_config_get8(config_handle,
16250Sstevel@tonic-gate 	    hdr_off + PCI_CONF_CACHE_LINESZ);
16260Sstevel@tonic-gate 	ph->lat = pci_config_get8(config_handle,
16270Sstevel@tonic-gate 	    hdr_off + PCI_CONF_LATENCY_TIMER);
16280Sstevel@tonic-gate 	ph->hdr_type = pci_config_get8(config_handle,
16290Sstevel@tonic-gate 	    hdr_off + PCI_CONF_HEADER);
16300Sstevel@tonic-gate 	ph->bist = pci_config_get8(config_handle, hdr_off + PCI_CONF_BIST);
16310Sstevel@tonic-gate 	ph->bar0 = pci_config_get32(config_handle, hdr_off + PCI_CONF_BASE0);
16320Sstevel@tonic-gate 	ph->bar1 = pci_config_get32(config_handle, hdr_off + PCI_CONF_BASE1);
16330Sstevel@tonic-gate 	ph->bar2 = pci_config_get32(config_handle, hdr_off + PCI_CONF_BASE2);
16340Sstevel@tonic-gate 	ph->bar3 = pci_config_get32(config_handle, hdr_off + PCI_CONF_BASE3);
16350Sstevel@tonic-gate 	ph->bar4 = pci_config_get32(config_handle, hdr_off + PCI_CONF_BASE4);
16360Sstevel@tonic-gate 	ph->bar5 = pci_config_get32(config_handle, hdr_off + PCI_CONF_BASE5);
16370Sstevel@tonic-gate 	ph->cardbus_cisp = pci_config_get32(config_handle,
16380Sstevel@tonic-gate 	    hdr_off + PCI_CONF_CIS);
16390Sstevel@tonic-gate 	ph->sub_venid = pci_config_get16(config_handle,
16400Sstevel@tonic-gate 	    hdr_off + PCI_CONF_SUBVENID);
16410Sstevel@tonic-gate 	ph->sub_devid = pci_config_get16(config_handle,
16420Sstevel@tonic-gate 	    hdr_off + PCI_CONF_SUBSYSID);
16430Sstevel@tonic-gate 	ph->exprom_bar = pci_config_get32(config_handle,
16440Sstevel@tonic-gate 	    hdr_off + PCI_CONF_ROM);
16450Sstevel@tonic-gate 	ph->int_line = pci_config_get8(config_handle, hdr_off + PCI_CONF_ILINE);
16460Sstevel@tonic-gate 	ph->int_pin = pci_config_get8(config_handle, hdr_off + PCI_CONF_IPIN);
16470Sstevel@tonic-gate 	ph->min_gnt = pci_config_get8(config_handle, hdr_off + PCI_CONF_MIN_G);
16480Sstevel@tonic-gate 	ph->max_lat = pci_config_get8(config_handle, hdr_off + PCI_CONF_MAX_L);
16490Sstevel@tonic-gate }
16500Sstevel@tonic-gate 
16510Sstevel@tonic-gate static void
16520Sstevel@tonic-gate db_pci_get_conf_regs(ddi_acc_handle_t config_handle, db_conf_regs_t *cr)
16530Sstevel@tonic-gate {
16540Sstevel@tonic-gate 	cr->ds_mem0_tr_base = pci_config_get32(config_handle,
16550Sstevel@tonic-gate 	    DB_CONF_DS_MEM0_TR_BASE);
16560Sstevel@tonic-gate 	cr->ds_io_mem1_tr_base = pci_config_get32(config_handle,
16570Sstevel@tonic-gate 	    DB_CONF_DS_IO_MEM1_TR_BASE);
16580Sstevel@tonic-gate 	cr->ds_mem2_tr_base = pci_config_get32(config_handle,
16590Sstevel@tonic-gate 	    DB_CONF_DS_MEM2_TR_BASE);
16600Sstevel@tonic-gate 	cr->ds_mem3_tr_base = pci_config_get32(config_handle,
16610Sstevel@tonic-gate 	    DB_CONF_DS_MEM3_TR_BASE);
16620Sstevel@tonic-gate 	cr->us_io_mem0_tr_base = pci_config_get32(config_handle,
16630Sstevel@tonic-gate 	    DB_CONF_US_IO_MEM0_TR_BASE);
16640Sstevel@tonic-gate 	cr->us_mem1_tr_base = pci_config_get32(config_handle,
16650Sstevel@tonic-gate 	    DB_CONF_US_MEM1_TR_BASE);
16660Sstevel@tonic-gate 	cr->ds_mem0_setup_reg = pci_config_get32(config_handle,
16670Sstevel@tonic-gate 	    DB_CONF_DS_MEM0_SETUP);
16680Sstevel@tonic-gate 	cr->ds_io_mem1_setup_reg = pci_config_get32(config_handle,
16690Sstevel@tonic-gate 	    DB_CONF_DS_IO_MEM1_SETUP);
16700Sstevel@tonic-gate 	cr->ds_mem2_setup_reg = pci_config_get32(config_handle,
16710Sstevel@tonic-gate 	    DB_CONF_DS_MEM2_SETUP);
16720Sstevel@tonic-gate 	cr->ds_mem3_setup_reg = pci_config_get64(config_handle,
16730Sstevel@tonic-gate 	    DB_CONF_DS_MEM3_SETUP);
16740Sstevel@tonic-gate 	cr->p_exp_rom_setup = pci_config_get32(config_handle,
16750Sstevel@tonic-gate 	    DB_CONF_PRIM_EXP_ROM_SETUP);
16760Sstevel@tonic-gate 	cr->us_io_mem0_setup_reg = pci_config_get32(config_handle,
16770Sstevel@tonic-gate 	    DB_CONF_US_IO_MEM0_SETUP);
16780Sstevel@tonic-gate 	cr->us_mem1_setup_reg = pci_config_get32(config_handle,
16790Sstevel@tonic-gate 	    DB_CONF_US_MEM1_SETUP);
16800Sstevel@tonic-gate 	cr->chip_control0 = pci_config_get16(config_handle, DB_CONF_CHIP_CTRL0);
16810Sstevel@tonic-gate 	cr->chip_control1 = pci_config_get16(config_handle, DB_CONF_CHIP_CTRL1);
16820Sstevel@tonic-gate 	cr->chip_status = pci_config_get16(config_handle, DB_CONF_STATUS);
16830Sstevel@tonic-gate 	cr->arb_control = pci_config_get16(config_handle, DB_CONF_ARBITER_CTRL);
16840Sstevel@tonic-gate 	cr->p_serr_disables = pci_config_get8(config_handle,
16850Sstevel@tonic-gate 	    DB_CONF_PRIM_SERR_DISABLES);
16860Sstevel@tonic-gate 	cr->s_serr_disables = pci_config_get8(config_handle,
16870Sstevel@tonic-gate 	    DB_CONF_PRIM_SERR_DISABLES);
16880Sstevel@tonic-gate 	cr->config_csr = pci_config_get16(config_handle, DB_CONF_CONF_CSR);
16890Sstevel@tonic-gate 	cr->reset_control = pci_config_get32(config_handle, DB_CONF_RESET_CTRL);
16900Sstevel@tonic-gate 	cr->pm_cap = pci_config_get16(config_handle, DB_CONF_PM_CAP);
16910Sstevel@tonic-gate 	cr->pm_csr = pci_config_get16(config_handle, DB_CONF_PM_CSR);
16920Sstevel@tonic-gate 	cr->hs_csr = pci_config_get8(config_handle, DB_CONF_HS_CSR);
16930Sstevel@tonic-gate }
16940Sstevel@tonic-gate #endif	/* DB_DEBUG */
16950Sstevel@tonic-gate 
16960Sstevel@tonic-gate /*
16970Sstevel@tonic-gate  * Function: db_pci_map
16980Sstevel@tonic-gate  *
16990Sstevel@tonic-gate  * Note:  	Only memory accesses are direct. IO could be direct
17000Sstevel@tonic-gate  * 		or indirect. Config accesses are always indirect.
17010Sstevel@tonic-gate  * 		The question here is, does the "assigned-addresses"
17020Sstevel@tonic-gate  * 		property entry represents the addresses in the
17030Sstevel@tonic-gate  * 		local domain or the host domain itself.
17040Sstevel@tonic-gate  * 		Strictly speaking, the assumption should be that
17050Sstevel@tonic-gate  * 		it is in the local domain, as the transactions
17060Sstevel@tonic-gate  * 		upstream or downstream are automatically
17070Sstevel@tonic-gate  * 		translated by the bridge chip anyway.
17080Sstevel@tonic-gate  *
17090Sstevel@tonic-gate  * Return values:
17100Sstevel@tonic-gate  *		DDI_SUCCESS: map call by child device success
17110Sstevel@tonic-gate  *		DDI_FAILURE: map operation failed.
17120Sstevel@tonic-gate  */
17130Sstevel@tonic-gate 
17140Sstevel@tonic-gate static int
17150Sstevel@tonic-gate db_pci_map(dev_info_t *dip, dev_info_t *rdip, ddi_map_req_t *mp,
17160Sstevel@tonic-gate 				off_t offset, off_t len, caddr_t *addrp)
17170Sstevel@tonic-gate {
17180Sstevel@tonic-gate 	register dev_info_t *pdip;
17190Sstevel@tonic-gate 	int reg_proplen, num_regs, rnumber;
17200Sstevel@tonic-gate 	uint_t	addr_space_type;
17210Sstevel@tonic-gate 	pci_regspec_t *pci_regsetp, pci_reg;
17220Sstevel@tonic-gate 	db_ctrl_t *dbp;
17230Sstevel@tonic-gate 	db_acc_pvt_t	*db_pvt;
17240Sstevel@tonic-gate 	ddi_acc_impl_t *ap;
17250Sstevel@tonic-gate 	ddi_acc_hdl_t *hp;
17260Sstevel@tonic-gate 	db_acc_cfg_addr_t *pci_addr;
17270Sstevel@tonic-gate 	int instance = ddi_get_instance(dip);
17280Sstevel@tonic-gate 
17290Sstevel@tonic-gate 	DB_DEBUG0(DB_PCI_MAP, dip, "enter\n");
17300Sstevel@tonic-gate 
17310Sstevel@tonic-gate 	/* get map type. check for config space */
17320Sstevel@tonic-gate 	switch (mp->map_type) {
17330Sstevel@tonic-gate 
17340Sstevel@tonic-gate 		case DDI_MT_RNUMBER :
17350Sstevel@tonic-gate 			/* get the reg number */
17360Sstevel@tonic-gate 			rnumber = mp->map_obj.rnumber;
17370Sstevel@tonic-gate 
17380Sstevel@tonic-gate 			if (ddi_getlongprop(DDI_DEV_T_ANY, rdip,
17390Sstevel@tonic-gate 				DDI_PROP_DONTPASS, "reg",
17400Sstevel@tonic-gate 				(caddr_t)&pci_regsetp, &reg_proplen)
17410Sstevel@tonic-gate 					!= DDI_SUCCESS)
17420Sstevel@tonic-gate 				    return (DDI_FAILURE);
17430Sstevel@tonic-gate 
17440Sstevel@tonic-gate 			num_regs = reg_proplen / (int)sizeof (pci_regspec_t);
17450Sstevel@tonic-gate 			if (rnumber >= num_regs) {
17460Sstevel@tonic-gate 				/* this is a DDI_ME_RNUMBER_RANGE error */
17470Sstevel@tonic-gate 				kmem_free(pci_regsetp, reg_proplen);
17480Sstevel@tonic-gate 				return (DDI_FAILURE);
17490Sstevel@tonic-gate 			}
17500Sstevel@tonic-gate 
17510Sstevel@tonic-gate 			pci_reg = pci_regsetp[rnumber];
17520Sstevel@tonic-gate 			kmem_free(pci_regsetp, reg_proplen);
17530Sstevel@tonic-gate 			/* FALLTHROUGH */
17540Sstevel@tonic-gate 		case DDI_MT_REGSPEC :
17550Sstevel@tonic-gate 			if (mp->map_type == DDI_MT_REGSPEC)
17560Sstevel@tonic-gate 				pci_reg = *(pci_regspec_t *)mp->map_obj.rp;
17570Sstevel@tonic-gate 
17580Sstevel@tonic-gate 			/*
17590Sstevel@tonic-gate 			 * Intercept config space accesses only. All other
17600Sstevel@tonic-gate 			 * requests go to the parent.
17610Sstevel@tonic-gate 			 */
17620Sstevel@tonic-gate 			addr_space_type = pci_reg.pci_phys_hi & PCI_ADDR_MASK;
17630Sstevel@tonic-gate 
17640Sstevel@tonic-gate 			DB_DEBUG3(DB_PCI_MAP, dip, "rdip=%lx, rnum=%d(%d)\n",
17650Sstevel@tonic-gate 					rdip, rnumber, num_regs);
17660Sstevel@tonic-gate 
17670Sstevel@tonic-gate 			/* if we do direct map IO, then lets break here */
17680Sstevel@tonic-gate 			if ((db_io_map_mode & DB_IO_MAP_DIRECT) &&
17690Sstevel@tonic-gate 				(addr_space_type == PCI_ADDR_IO))
17700Sstevel@tonic-gate 					break;
17710Sstevel@tonic-gate 
17720Sstevel@tonic-gate 			if ((addr_space_type != PCI_ADDR_CONFIG) &&
17730Sstevel@tonic-gate 					(addr_space_type != PCI_ADDR_IO))
17740Sstevel@tonic-gate 				break;
17750Sstevel@tonic-gate 
17760Sstevel@tonic-gate 			/*
17770Sstevel@tonic-gate 			 * User mapping requests not legal for indirect
17780Sstevel@tonic-gate 			 * IO/Config Space
17790Sstevel@tonic-gate 			 */
17800Sstevel@tonic-gate 			if (mp->map_op == DDI_MO_MAP_HANDLE)
17810Sstevel@tonic-gate 				return (DDI_FAILURE);
17820Sstevel@tonic-gate 
17830Sstevel@tonic-gate 			dbp = (db_ctrl_t *)ddi_get_soft_state(db_state,
17840Sstevel@tonic-gate 								instance);
17850Sstevel@tonic-gate 			/* get our common access handle */
17860Sstevel@tonic-gate 			hp = (ddi_acc_hdl_t *)mp->map_handlep;
17870Sstevel@tonic-gate 
17880Sstevel@tonic-gate 			/* Check for unmap operation */
17890Sstevel@tonic-gate 			if ((mp->map_op == DDI_MO_UNMAP) ||
17900Sstevel@tonic-gate 			    (mp->map_op == DDI_MO_UNLOCK)) {
17910Sstevel@tonic-gate 					/*
17920Sstevel@tonic-gate 					 * free up memory allocated for our
17930Sstevel@tonic-gate 					 * private access handle.
17940Sstevel@tonic-gate 					 */
17950Sstevel@tonic-gate 					db_pvt = (db_acc_pvt_t *)
17960Sstevel@tonic-gate 							hp->ah_bus_private;
17970Sstevel@tonic-gate 					DB_DEBUG1(DB_PCI_MAP, dip,
17980Sstevel@tonic-gate 						"unmap rdip=%lx\n", rdip);
17990Sstevel@tonic-gate 					kmem_free((void *)db_pvt,
18000Sstevel@tonic-gate 							sizeof (db_acc_pvt_t));
18010Sstevel@tonic-gate 
18020Sstevel@tonic-gate 					/*
18030Sstevel@tonic-gate 					 * unmap operation of PCI IO/config
18040Sstevel@tonic-gate 					 * space.
18050Sstevel@tonic-gate 					 */
18060Sstevel@tonic-gate 					return (DDI_SUCCESS);
18070Sstevel@tonic-gate 			}
18080Sstevel@tonic-gate 
18090Sstevel@tonic-gate 			if (addr_space_type == PCI_ADDR_CONFIG) {
18100Sstevel@tonic-gate 				/* Config space access range check */
18110Sstevel@tonic-gate 				if ((offset >= PCI_CONF_HDR_SIZE) ||
18120Sstevel@tonic-gate 				    (len > PCI_CONF_HDR_SIZE) ||
18130Sstevel@tonic-gate 				    (offset + len > PCI_CONF_HDR_SIZE)) {
18140Sstevel@tonic-gate 
18150Sstevel@tonic-gate 					return (DDI_FAILURE);
18160Sstevel@tonic-gate 				}
18170Sstevel@tonic-gate 			}
18180Sstevel@tonic-gate 
18190Sstevel@tonic-gate 			/* define the complete access handle */
18200Sstevel@tonic-gate 			hp = (ddi_acc_hdl_t *)mp->map_handlep;
18210Sstevel@tonic-gate 
18220Sstevel@tonic-gate 			ap = (ddi_acc_impl_t *)hp->ah_platform_private;
18230Sstevel@tonic-gate 
18240Sstevel@tonic-gate 			ap->ahi_get8 = db_ddi_get8;
18250Sstevel@tonic-gate 			ap->ahi_get16 = db_ddi_get16;
18260Sstevel@tonic-gate 			ap->ahi_get32 = db_ddi_get32;
18270Sstevel@tonic-gate 			ap->ahi_get64 = db_ddi_get64;
18280Sstevel@tonic-gate 			ap->ahi_put8 = db_ddi_put8;
18290Sstevel@tonic-gate 			ap->ahi_put16 = db_ddi_put16;
18300Sstevel@tonic-gate 			ap->ahi_put32 = db_ddi_put32;
18310Sstevel@tonic-gate 			ap->ahi_put64 = db_ddi_put64;
18320Sstevel@tonic-gate 			ap->ahi_rep_get8 = db_ddi_rep_get8;
18330Sstevel@tonic-gate 			ap->ahi_rep_get16 = db_ddi_rep_get16;
18340Sstevel@tonic-gate 			ap->ahi_rep_get32 = db_ddi_rep_get32;
18350Sstevel@tonic-gate 			ap->ahi_rep_get64 = db_ddi_rep_get64;
18360Sstevel@tonic-gate 			ap->ahi_rep_put8 = db_ddi_rep_put8;
18370Sstevel@tonic-gate 			ap->ahi_rep_put16 = db_ddi_rep_put16;
18380Sstevel@tonic-gate 			ap->ahi_rep_put32 = db_ddi_rep_put32;
18390Sstevel@tonic-gate 			ap->ahi_rep_put64 = db_ddi_rep_put64;
18400Sstevel@tonic-gate 
18410Sstevel@tonic-gate 			/* Initialize to default check/notify functions */
18420Sstevel@tonic-gate 			ap->ahi_fault = 0;
18430Sstevel@tonic-gate 			ap->ahi_fault_check = i_ddi_acc_fault_check;
18440Sstevel@tonic-gate 			ap->ahi_fault_notify = i_ddi_acc_fault_notify;
18450Sstevel@tonic-gate 
18460Sstevel@tonic-gate 			/* allocate memory for our private handle */
18470Sstevel@tonic-gate 			db_pvt = kmem_zalloc(sizeof (db_acc_pvt_t), KM_SLEEP);
18480Sstevel@tonic-gate 			hp->ah_bus_private = (void *)db_pvt;
18490Sstevel@tonic-gate 			db_pvt->dbp = dbp;
18500Sstevel@tonic-gate 
18510Sstevel@tonic-gate 			/* record the device address for future use */
18520Sstevel@tonic-gate 			pci_addr = &db_pvt->dev_addr;
18530Sstevel@tonic-gate 			pci_addr->c_busnum =
18540Sstevel@tonic-gate 					PCI_REG_BUS_G(pci_reg.pci_phys_hi);
18550Sstevel@tonic-gate 			pci_addr->c_devnum =
18560Sstevel@tonic-gate 					PCI_REG_DEV_G(pci_reg.pci_phys_hi);
18570Sstevel@tonic-gate 			pci_addr->c_funcnum =
18580Sstevel@tonic-gate 					PCI_REG_FUNC_G(pci_reg.pci_phys_hi);
18590Sstevel@tonic-gate 			/*
18600Sstevel@tonic-gate 			 * We should keep the upstream or
18610Sstevel@tonic-gate 			 * downstream info in our own ah_bus_private
18620Sstevel@tonic-gate 			 * structure, so that we do not waste our
18630Sstevel@tonic-gate 			 * time in the actual IO routines, figuring out
18640Sstevel@tonic-gate 			 * if we should use upstream or downstream
18650Sstevel@tonic-gate 			 * configuration addr/data register.
18660Sstevel@tonic-gate 			 * So, check orientation and setup registers
18670Sstevel@tonic-gate 			 * right now.
18680Sstevel@tonic-gate 			 */
18690Sstevel@tonic-gate 			switch (addr_space_type) {
18700Sstevel@tonic-gate 
18710Sstevel@tonic-gate 			case PCI_ADDR_CONFIG :
18720Sstevel@tonic-gate 				if (dbp->dev_state & DB_PRIMARY_NEXUS) {
18730Sstevel@tonic-gate 					DB_DEBUG0(DB_PCI_MAP, dip, "primary\n");
18740Sstevel@tonic-gate 					db_pvt->mask = DS8_CONF_OWN;
18750Sstevel@tonic-gate 					if (db_conf_map_mode &
18760Sstevel@tonic-gate 						DB_CONF_MAP_INDIRECT_IO) {
18770Sstevel@tonic-gate 						DB_DEBUG0(DB_PCI_MAP, dip,
18780Sstevel@tonic-gate 							"INDIRECT_CONF\n");
18790Sstevel@tonic-gate 
18800Sstevel@tonic-gate 						db_pvt->handle =
18810Sstevel@tonic-gate 							dbp->csr_io_handle;
18820Sstevel@tonic-gate 						db_pvt->addr =
18830Sstevel@tonic-gate 							(uint32_t *)
18840Sstevel@tonic-gate 							((uchar_t *)dbp->csr_io
18850Sstevel@tonic-gate 							+ DB_CSR_DS_CONF_ADDR);
18860Sstevel@tonic-gate 						db_pvt->data =
18870Sstevel@tonic-gate 							(uint32_t *)
18880Sstevel@tonic-gate 							((uchar_t *)dbp->csr_io
18890Sstevel@tonic-gate 							+ DB_CSR_DS_CONF_DATA);
18900Sstevel@tonic-gate 						db_pvt->bus_own =
18910Sstevel@tonic-gate 							(uint8_t *)
18920Sstevel@tonic-gate 							((uchar_t *)dbp->csr_io
18930Sstevel@tonic-gate 							+ DB_CSR8_DS_CONF_OWN);
18940Sstevel@tonic-gate 						db_pvt->bus_release =
18950Sstevel@tonic-gate 							(uint8_t *)
18960Sstevel@tonic-gate 							((uchar_t *)dbp->csr_io
18970Sstevel@tonic-gate 							+ DB_CSR8_DS_CONF_CSR);
18980Sstevel@tonic-gate 					} else {
18990Sstevel@tonic-gate 						DB_DEBUG0(DB_PCI_MAP, dip,
19000Sstevel@tonic-gate 							"DIRECT_CONF\n");
19010Sstevel@tonic-gate 
19020Sstevel@tonic-gate 						db_pvt->handle =
19030Sstevel@tonic-gate 							dbp->conf_handle;
19040Sstevel@tonic-gate 						db_pvt->addr =
19050Sstevel@tonic-gate 							(uint32_t *)
19060Sstevel@tonic-gate 							((uchar_t *)dbp->conf_io
19070Sstevel@tonic-gate 							+ DB_CONF_DS_CONF_ADDR);
19080Sstevel@tonic-gate 						db_pvt->data = (uint32_t *)
19090Sstevel@tonic-gate 							((uchar_t *)dbp->conf_io
19100Sstevel@tonic-gate 							+ DB_CONF_DS_CONF_DATA);
19110Sstevel@tonic-gate 						db_pvt->bus_own =
19120Sstevel@tonic-gate 							(uint8_t *)
19130Sstevel@tonic-gate 							((uchar_t *)dbp->conf_io
19140Sstevel@tonic-gate 							+ DB_CONF8_DS_CONF_OWN);
19150Sstevel@tonic-gate 						db_pvt->bus_release =
19160Sstevel@tonic-gate 							(uint8_t *)
19170Sstevel@tonic-gate 							((uchar_t *)dbp->conf_io
19180Sstevel@tonic-gate 							+ DB_CONF8_DS_CONF_CSR);
19190Sstevel@tonic-gate 					}
19200Sstevel@tonic-gate 				} else {
19210Sstevel@tonic-gate 					DB_DEBUG0(DB_PCI_MAP, dip,
19220Sstevel@tonic-gate 						"secondary\n");
19230Sstevel@tonic-gate 					db_pvt->mask = US8_CONF_OWN;
19240Sstevel@tonic-gate 					if (db_conf_map_mode &
19250Sstevel@tonic-gate 						DB_CONF_MAP_INDIRECT_IO) {
19260Sstevel@tonic-gate 						DB_DEBUG0(DB_PCI_MAP, dip,
19270Sstevel@tonic-gate 							"INDIRECT_CONF\n");
19280Sstevel@tonic-gate 
19290Sstevel@tonic-gate 						db_pvt->handle =
19300Sstevel@tonic-gate 							dbp->csr_io_handle;
19310Sstevel@tonic-gate 						db_pvt->addr =
19320Sstevel@tonic-gate 							(uint32_t *)
19330Sstevel@tonic-gate 							((uchar_t *)dbp->csr_io
19340Sstevel@tonic-gate 							+ DB_CSR_US_CONF_ADDR);
19350Sstevel@tonic-gate 						db_pvt->data =
19360Sstevel@tonic-gate 							(uint32_t *)
19370Sstevel@tonic-gate 							((uchar_t *)dbp->csr_io
19380Sstevel@tonic-gate 							+ DB_CSR_US_CONF_DATA);
19390Sstevel@tonic-gate 						db_pvt->bus_own =
19400Sstevel@tonic-gate 							(uint8_t *)
19410Sstevel@tonic-gate 							((uchar_t *)dbp->csr_io
19420Sstevel@tonic-gate 							+ DB_CSR8_US_CONF_OWN);
19430Sstevel@tonic-gate 						db_pvt->bus_release =
19440Sstevel@tonic-gate 							(uint8_t *)
19450Sstevel@tonic-gate 							((uchar_t *)dbp->csr_io
19460Sstevel@tonic-gate 							+ DB_CSR8_US_CONF_CSR);
19470Sstevel@tonic-gate 					} else {
19480Sstevel@tonic-gate 						DB_DEBUG0(DB_PCI_MAP, dip,
19490Sstevel@tonic-gate 							"DIRECT_CONF\n");
19500Sstevel@tonic-gate 
19510Sstevel@tonic-gate 						db_pvt->handle =
19520Sstevel@tonic-gate 							dbp->conf_handle;
19530Sstevel@tonic-gate 						db_pvt->addr =
19540Sstevel@tonic-gate 							(uint32_t *)
19550Sstevel@tonic-gate 							((uchar_t *)dbp->conf_io
19560Sstevel@tonic-gate 							+ DB_CONF_US_CONF_ADDR);
19570Sstevel@tonic-gate 						db_pvt->data =
19580Sstevel@tonic-gate 							(uint32_t *)
19590Sstevel@tonic-gate 							((uchar_t *)dbp->conf_io
19600Sstevel@tonic-gate 							+ DB_CONF_US_CONF_DATA);
19610Sstevel@tonic-gate 						db_pvt->bus_own =
19620Sstevel@tonic-gate 							(uint8_t *)
19630Sstevel@tonic-gate 							((uchar_t *)dbp->conf_io
19640Sstevel@tonic-gate 							+ DB_CONF8_US_CONF_OWN);
19650Sstevel@tonic-gate 						db_pvt->bus_release =
19660Sstevel@tonic-gate 							(uint8_t *)
19670Sstevel@tonic-gate 							((uchar_t *)dbp->conf_io
19680Sstevel@tonic-gate 							+ DB_CONF8_US_CONF_CSR);
19690Sstevel@tonic-gate 					}
19700Sstevel@tonic-gate 				}
19710Sstevel@tonic-gate 				break;
19720Sstevel@tonic-gate 
19730Sstevel@tonic-gate 			case PCI_ADDR_IO :
19740Sstevel@tonic-gate 				DB_DEBUG0(DB_PCI_MAP, dip, "PCI_ADDR_IO\n");
19750Sstevel@tonic-gate 
19760Sstevel@tonic-gate 				/* ap->ahi_acc_attr |= DDI_ACCATTR_IO_SPACE; */
19770Sstevel@tonic-gate 				db_pvt->handle = dbp->csr_io_handle;
19780Sstevel@tonic-gate 				if (dbp->dev_state & DB_PRIMARY_NEXUS) {
19790Sstevel@tonic-gate 					DB_DEBUG0(DB_PCI_MAP, dip, "primary\n");
19800Sstevel@tonic-gate 					db_pvt->addr = (uint32_t *)
19810Sstevel@tonic-gate 							((uchar_t *)dbp->csr_io
19820Sstevel@tonic-gate 							+ DB_CSR_DS_IO_ADDR);
19830Sstevel@tonic-gate 					db_pvt->data = (uint32_t *)
19840Sstevel@tonic-gate 							((uchar_t *)dbp->csr_io
19850Sstevel@tonic-gate 							+ DB_CSR_DS_IO_DATA);
19860Sstevel@tonic-gate 					db_pvt->bus_own = (uint8_t *)
19870Sstevel@tonic-gate 							((uchar_t *)dbp->csr_io
19880Sstevel@tonic-gate 							+ DB_CSR8_DS_IO_OWN);
19890Sstevel@tonic-gate 					db_pvt->bus_release = (uint8_t *)
19900Sstevel@tonic-gate 							((uchar_t *)dbp->csr_io
19910Sstevel@tonic-gate 							+ DB_CSR8_DS_IO_CSR);
19920Sstevel@tonic-gate 					db_pvt->mask = DS8_IO_OWN;
19930Sstevel@tonic-gate 				} else {
19940Sstevel@tonic-gate 					DB_DEBUG0(DB_PCI_MAP, dip,
19950Sstevel@tonic-gate 						"secondary\n");
19960Sstevel@tonic-gate 					db_pvt->addr = (uint32_t *)
19970Sstevel@tonic-gate 							((uchar_t *)dbp->csr_io
19980Sstevel@tonic-gate 							+ DB_CSR_US_IO_ADDR);
19990Sstevel@tonic-gate 					db_pvt->data = (uint32_t *)
20000Sstevel@tonic-gate 							((uchar_t *)dbp->csr_io
20010Sstevel@tonic-gate 							+ DB_CSR_US_IO_DATA);
20020Sstevel@tonic-gate 					db_pvt->bus_own = (uint8_t *)
20030Sstevel@tonic-gate 							((uchar_t *)dbp->csr_io
20040Sstevel@tonic-gate 							+ DB_CSR8_US_IO_OWN);
20050Sstevel@tonic-gate 					db_pvt->bus_release = (uint8_t *)
20060Sstevel@tonic-gate 							((uchar_t *)dbp->csr_io
20070Sstevel@tonic-gate 							+ DB_CSR8_US_IO_CSR);
20080Sstevel@tonic-gate 					db_pvt->mask = US8_IO_OWN;
20090Sstevel@tonic-gate 				}
20100Sstevel@tonic-gate 				break;
20110Sstevel@tonic-gate 
20120Sstevel@tonic-gate 			default :
20130Sstevel@tonic-gate 				DB_DEBUG0(DB_PCI_MAP, dip,
20140Sstevel@tonic-gate 					"PCI_ADDR unknown\n");
20150Sstevel@tonic-gate 				break;
20160Sstevel@tonic-gate 			}
20170Sstevel@tonic-gate 
20180Sstevel@tonic-gate 			/* make and store a type 0/1 address in the *addrp */
20190Sstevel@tonic-gate 			if (pci_addr->c_busnum == dbp->range.lo) {
20200Sstevel@tonic-gate 				*addrp = (caddr_t)DB_PCI_REG_ADDR_TYPE0(
20210Sstevel@tonic-gate 						pci_addr->c_busnum,
20220Sstevel@tonic-gate 						pci_addr->c_devnum,
20230Sstevel@tonic-gate 						pci_addr->c_funcnum,
20240Sstevel@tonic-gate 						offset);
20250Sstevel@tonic-gate 				db_pvt->access_mode |= DB_PCI_CONF_CYCLE_TYPE0;
20260Sstevel@tonic-gate 				DB_DEBUG0(DB_PCI_MAP, dip,
20270Sstevel@tonic-gate 					"access mode type 0\n");
20280Sstevel@tonic-gate 			} else {
20290Sstevel@tonic-gate 				*addrp = (caddr_t)DB_PCI_REG_ADDR_TYPE1(
20300Sstevel@tonic-gate 						pci_addr->c_busnum,
20310Sstevel@tonic-gate 						pci_addr->c_devnum,
20320Sstevel@tonic-gate 						pci_addr->c_funcnum,
20330Sstevel@tonic-gate 						offset);
20340Sstevel@tonic-gate 				db_pvt->access_mode |= DB_PCI_CONF_CYCLE_TYPE1;
20350Sstevel@tonic-gate 				DB_DEBUG0(DB_PCI_MAP, dip,
20360Sstevel@tonic-gate 					"access mode type 1\n");
20370Sstevel@tonic-gate 			}
20380Sstevel@tonic-gate 			DB_DEBUG4(DB_PCI_MAP, dip, "addrp<%x,%x,%x> = %lx\n",
20390Sstevel@tonic-gate 					pci_addr->c_busnum, pci_addr->c_devnum,
20400Sstevel@tonic-gate 					pci_addr->c_funcnum, *addrp);
20410Sstevel@tonic-gate 
20420Sstevel@tonic-gate 			return (DDI_SUCCESS);
20430Sstevel@tonic-gate 
20440Sstevel@tonic-gate 		default :
20450Sstevel@tonic-gate 				DB_DEBUG1(DB_PCI_MAP, dip, "DDI other %x\n",
20460Sstevel@tonic-gate 					mp->map_type);
20470Sstevel@tonic-gate 				break;
20480Sstevel@tonic-gate 	}
20490Sstevel@tonic-gate 	DB_DEBUG0(DB_PCI_MAP, dip, "exit\n");
20500Sstevel@tonic-gate 
20510Sstevel@tonic-gate 	pdip = (dev_info_t *)DEVI(dip)->devi_parent;
20520Sstevel@tonic-gate 	return ((DEVI(pdip)->devi_ops->devo_bus_ops->bus_map)
20530Sstevel@tonic-gate 		(pdip, rdip, mp, offset, len, addrp));
20540Sstevel@tonic-gate }
20550Sstevel@tonic-gate 
20560Sstevel@tonic-gate #ifdef DB_DEBUG
20570Sstevel@tonic-gate char *db_ctlop_name[] = {
20580Sstevel@tonic-gate 	"DDI_CTLOPS_DMAPMAPC",
20590Sstevel@tonic-gate 	"DDI_CTLOPS_INITCHILD",
20600Sstevel@tonic-gate 	"DDI_CTLOPS_UNINITCHILD",
20610Sstevel@tonic-gate 	"DDI_CTLOPS_REPORTDEV",
20620Sstevel@tonic-gate 	"DDI_CTLOPS_REPORTINT",
20630Sstevel@tonic-gate 	"DDI_CTLOPS_REGSIZE",
20640Sstevel@tonic-gate 	"DDI_CTLOPS_NREGS",
2065693Sgovinda 	"DDI_CTLOPS_RESERVED0",
20660Sstevel@tonic-gate 	"DDI_CTLOPS_SIDDEV",
20670Sstevel@tonic-gate 	"DDI_CTLOPS_SLAVEONLY",
20680Sstevel@tonic-gate 	"DDI_CTLOPS_AFFINITY",
20690Sstevel@tonic-gate 	"DDI_CTLOPS_IOMIN",
20700Sstevel@tonic-gate 	"DDI_CTLOPS_PTOB",
20710Sstevel@tonic-gate 	"DDI_CTLOPS_BTOP",
20720Sstevel@tonic-gate 	"DDI_CTLOPS_BTOPR",
20730Sstevel@tonic-gate 	"DDI_CTLOPS_RESERVED1",
20740Sstevel@tonic-gate 	"DDI_CTLOPS_RESERVED2",
20750Sstevel@tonic-gate 	"DDI_CTLOPS_RESERVED3",
2076693Sgovinda 	"DDI_CTLOPS_RESERVED4",
2077693Sgovinda 	"DDI_CTLOPS_RESERVED5",
20780Sstevel@tonic-gate 	"DDI_CTLOPS_DVMAPAGESIZE",
20790Sstevel@tonic-gate 	"DDI_CTLOPS_POWER",
20800Sstevel@tonic-gate 	"DDI_CTLOPS_ATTACH",
20810Sstevel@tonic-gate 	"DDI_CTLOPS_DETACH",
20820Sstevel@tonic-gate 	"DDI_CTLOPS_POKE",
20830Sstevel@tonic-gate 	"DDI_CTLOPS_PEEK"
20840Sstevel@tonic-gate };
20850Sstevel@tonic-gate #endif
20860Sstevel@tonic-gate 
20870Sstevel@tonic-gate static int
20880Sstevel@tonic-gate db_ctlops(dev_info_t *dip, dev_info_t *rdip,
20890Sstevel@tonic-gate 	ddi_ctl_enum_t ctlop, void *arg, void *result)
20900Sstevel@tonic-gate {
20910Sstevel@tonic-gate 
20920Sstevel@tonic-gate 	if ((ctlop >= DDI_CTLOPS_DMAPMAPC) &&
20930Sstevel@tonic-gate 			(ctlop <= DDI_CTLOPS_DETACH)) {
20940Sstevel@tonic-gate 		DB_DEBUG1(DB_CTLOPS, dip, "ctlop=%s\n", db_ctlop_name[ctlop]);
20950Sstevel@tonic-gate 	} else {
20960Sstevel@tonic-gate 		DB_DEBUG1(DB_CTLOPS, dip, "ctlop=%d\n", ctlop);
20970Sstevel@tonic-gate 	}
20980Sstevel@tonic-gate 
20990Sstevel@tonic-gate 	switch (ctlop) {
21000Sstevel@tonic-gate 	case DDI_CTLOPS_REPORTDEV :
21010Sstevel@tonic-gate 		if (rdip == (dev_info_t *)0)
21020Sstevel@tonic-gate 			return (DDI_FAILURE);
21030Sstevel@tonic-gate 		cmn_err(CE_CONT, "?PCI-device: %s@%s, %s#%d\n",
21040Sstevel@tonic-gate 		    ddi_node_name(rdip), ddi_get_name_addr(rdip),
21050Sstevel@tonic-gate 		    ddi_driver_name(rdip),
21060Sstevel@tonic-gate 		    ddi_get_instance(rdip));
21070Sstevel@tonic-gate 		return (DDI_SUCCESS);
21080Sstevel@tonic-gate 
21090Sstevel@tonic-gate 	case DDI_CTLOPS_INITCHILD :
21100Sstevel@tonic-gate 		return (db_initchild((dev_info_t *)arg));
21110Sstevel@tonic-gate 
21120Sstevel@tonic-gate 	case DDI_CTLOPS_UNINITCHILD :
21130Sstevel@tonic-gate 		db_uninitchild((dev_info_t *)arg);
21140Sstevel@tonic-gate 		return (DDI_SUCCESS);
21150Sstevel@tonic-gate 
21160Sstevel@tonic-gate 	case DDI_CTLOPS_SIDDEV :
21170Sstevel@tonic-gate 		return (DDI_SUCCESS);
21180Sstevel@tonic-gate 
21190Sstevel@tonic-gate 	case DDI_CTLOPS_REGSIZE :
21200Sstevel@tonic-gate 	case DDI_CTLOPS_NREGS :
21210Sstevel@tonic-gate 		if (rdip == (dev_info_t *)0)
21220Sstevel@tonic-gate 			return (DDI_FAILURE);
21230Sstevel@tonic-gate 		/* fall through */
21240Sstevel@tonic-gate 
21250Sstevel@tonic-gate 	default :
21260Sstevel@tonic-gate 		return (ddi_ctlops(dip, rdip, ctlop, arg, result));
21270Sstevel@tonic-gate 	}
21280Sstevel@tonic-gate 
21290Sstevel@tonic-gate }
21300Sstevel@tonic-gate 
21310Sstevel@tonic-gate static dev_info_t *
21320Sstevel@tonic-gate db_get_my_childs_dip(dev_info_t *dip, dev_info_t *rdip)
21330Sstevel@tonic-gate {
21340Sstevel@tonic-gate 	dev_info_t *cdip = rdip;
21350Sstevel@tonic-gate 
21360Sstevel@tonic-gate 	for (; ddi_get_parent(cdip) != dip; cdip = ddi_get_parent(cdip))
21370Sstevel@tonic-gate 		;
21380Sstevel@tonic-gate 
21390Sstevel@tonic-gate 	return (cdip);
21400Sstevel@tonic-gate }
21410Sstevel@tonic-gate 
21420Sstevel@tonic-gate static int
21430Sstevel@tonic-gate db_intr_ops(dev_info_t *dip, dev_info_t *rdip, ddi_intr_op_t intr_op,
21440Sstevel@tonic-gate     ddi_intr_handle_impl_t *hdlp, void *result)
21450Sstevel@tonic-gate {
21460Sstevel@tonic-gate 	dev_info_t	*cdip = rdip;
21470Sstevel@tonic-gate 	pci_regspec_t	*pci_rp;
21480Sstevel@tonic-gate 	int		reglen, len;
21490Sstevel@tonic-gate 	uint32_t	d, intr;
21500Sstevel@tonic-gate 
21510Sstevel@tonic-gate 	DB_DEBUG1(DB_INTR_OPS, dip, "intr_op=%d\n",  intr_op);
21520Sstevel@tonic-gate 
21530Sstevel@tonic-gate 	if (hdlp->ih_type != DDI_INTR_TYPE_FIXED)
21540Sstevel@tonic-gate 		goto done;
21550Sstevel@tonic-gate 
21560Sstevel@tonic-gate 	/*
21570Sstevel@tonic-gate 	 * If the interrupt-map property is defined at this
21580Sstevel@tonic-gate 	 * node, it will have performed the interrupt
21590Sstevel@tonic-gate 	 * translation as part of the property, so no
21600Sstevel@tonic-gate 	 * rotation needs to be done.
21610Sstevel@tonic-gate 	 */
21620Sstevel@tonic-gate 
21630Sstevel@tonic-gate 	if (ddi_getproplen(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
21640Sstevel@tonic-gate 	    "interrupt-map", &len) == DDI_PROP_SUCCESS)
21650Sstevel@tonic-gate 		goto done;
21660Sstevel@tonic-gate 
21670Sstevel@tonic-gate 	cdip = db_get_my_childs_dip(dip, rdip);
21680Sstevel@tonic-gate 
21690Sstevel@tonic-gate 	/*
21700Sstevel@tonic-gate 	 * Use the devices reg property to determine it's
21710Sstevel@tonic-gate 	 * PCI bus number and device number.
21720Sstevel@tonic-gate 	 */
2173506Scth 	if (ddi_getlongprop(DDI_DEV_T_ANY, cdip, DDI_PROP_DONTPASS,
21740Sstevel@tonic-gate 	    "reg", (caddr_t)&pci_rp, &reglen) != DDI_SUCCESS)
21750Sstevel@tonic-gate 		return (DDI_FAILURE);
21760Sstevel@tonic-gate 
2177693Sgovinda 	intr = hdlp->ih_vector;
21780Sstevel@tonic-gate 
21790Sstevel@tonic-gate 	/* Spin the interrupt */
21800Sstevel@tonic-gate 	d = PCI_REG_DEV_G(pci_rp[0].pci_phys_hi);
21810Sstevel@tonic-gate 
21820Sstevel@tonic-gate 	if ((intr >= PCI_INTA) && (intr <= PCI_INTD))
2183693Sgovinda 		hdlp->ih_vector = ((intr - 1 + (d % 4)) % 4 + 1);
21840Sstevel@tonic-gate 	else
21850Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s#%d: %s: PCI intr=%x out of range",
21860Sstevel@tonic-gate 		    ddi_driver_name(rdip), ddi_get_instance(rdip),
21870Sstevel@tonic-gate 		    ddi_driver_name(dip), intr);
21880Sstevel@tonic-gate 
21890Sstevel@tonic-gate 	DB_DEBUG3(DB_INTR_OPS, dip, "intr=%d, d=%d, is_intr=%d\n",
2190693Sgovinda 	    intr, d, hdlp->ih_vector);
21910Sstevel@tonic-gate 
21920Sstevel@tonic-gate 	kmem_free(pci_rp, reglen);
21930Sstevel@tonic-gate 
21940Sstevel@tonic-gate done:
21950Sstevel@tonic-gate 	/* Pass up the request to our parent. */
21960Sstevel@tonic-gate 	return (i_ddi_intr_ops(dip, rdip, intr_op, hdlp, result));
21970Sstevel@tonic-gate }
21980Sstevel@tonic-gate 
21990Sstevel@tonic-gate static int
22000Sstevel@tonic-gate db_name_child(dev_info_t *child, char *name, int namelen)
22010Sstevel@tonic-gate {
22020Sstevel@tonic-gate 	uint_t n, slot, func;
22030Sstevel@tonic-gate 	pci_regspec_t *pci_rp;
22040Sstevel@tonic-gate 
22050Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node(child) == 0) {
22060Sstevel@tonic-gate 		char **unit_addr;
22070Sstevel@tonic-gate 
22080Sstevel@tonic-gate 		/* name .conf nodes by "unit-address" property" */
22090Sstevel@tonic-gate 		if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, child,
22100Sstevel@tonic-gate 		    DDI_PROP_DONTPASS, "unit-address", &unit_addr, &n) !=
22110Sstevel@tonic-gate 		    DDI_PROP_SUCCESS) {
22120Sstevel@tonic-gate 			cmn_err(CE_WARN, "cannot name node from %s.conf",
22130Sstevel@tonic-gate 			    ddi_driver_name(child));
22140Sstevel@tonic-gate 			return (DDI_FAILURE);
22150Sstevel@tonic-gate 		}
22160Sstevel@tonic-gate 		if (n != 1 || *unit_addr == NULL || **unit_addr == 0) {
22170Sstevel@tonic-gate 			cmn_err(CE_WARN, "unit-address property in %s.conf"
22180Sstevel@tonic-gate 			    " not well-formed", ddi_driver_name(child));
22190Sstevel@tonic-gate 			ddi_prop_free(unit_addr);
22200Sstevel@tonic-gate 			return (DDI_FAILURE);
22210Sstevel@tonic-gate 		}
22220Sstevel@tonic-gate 
22230Sstevel@tonic-gate 		(void) snprintf(name, namelen, "%s", *unit_addr);
22240Sstevel@tonic-gate 		ddi_prop_free(unit_addr);
22250Sstevel@tonic-gate 		return (DDI_SUCCESS);
22260Sstevel@tonic-gate 	}
22270Sstevel@tonic-gate 
22280Sstevel@tonic-gate 	/* name hardware nodes by "reg" property */
22290Sstevel@tonic-gate 	if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, child, 0, "reg",
22300Sstevel@tonic-gate 	    (int **)&pci_rp, &n) != DDI_SUCCESS)
22310Sstevel@tonic-gate 		return (DDI_FAILURE);
22320Sstevel@tonic-gate 
22330Sstevel@tonic-gate 	/* get the device identifications */
22340Sstevel@tonic-gate 	slot = PCI_REG_DEV_G(pci_rp->pci_phys_hi);
22350Sstevel@tonic-gate 	func = PCI_REG_FUNC_G(pci_rp->pci_phys_hi);
22360Sstevel@tonic-gate 
22370Sstevel@tonic-gate 	if (func != 0)
22380Sstevel@tonic-gate 		(void) snprintf(name, namelen, "%x,%x", slot, func);
22390Sstevel@tonic-gate 	else
22400Sstevel@tonic-gate 		(void) snprintf(name, namelen, "%x", slot);
22410Sstevel@tonic-gate 
22420Sstevel@tonic-gate 	ddi_prop_free(pci_rp);
22430Sstevel@tonic-gate 	return (DDI_SUCCESS);
22440Sstevel@tonic-gate }
22450Sstevel@tonic-gate 
22460Sstevel@tonic-gate static int
22470Sstevel@tonic-gate db_initchild(dev_info_t *child)
22480Sstevel@tonic-gate {
22490Sstevel@tonic-gate 	char name[MAXNAMELEN];
22500Sstevel@tonic-gate 	ddi_acc_handle_t config_handle;
22510Sstevel@tonic-gate 	ushort_t command_preserve, command;
22520Sstevel@tonic-gate 	uint_t n;
22530Sstevel@tonic-gate 	ushort_t bcr;
22540Sstevel@tonic-gate 	uchar_t header_type, min_gnt, latency_timer;
22550Sstevel@tonic-gate 	db_ctrl_t *dbp;
22560Sstevel@tonic-gate 
22570Sstevel@tonic-gate 	if (db_name_child(child, name, MAXNAMELEN) != DDI_SUCCESS)
22580Sstevel@tonic-gate 		return (DDI_FAILURE);
22590Sstevel@tonic-gate 
22600Sstevel@tonic-gate 	ddi_set_name_addr(child, name);
22610Sstevel@tonic-gate 	ddi_set_parent_data(child, NULL);
22620Sstevel@tonic-gate 
22630Sstevel@tonic-gate 	/*
22640Sstevel@tonic-gate 	 * Pseudo nodes indicate a prototype node with per-instance
22650Sstevel@tonic-gate 	 * properties to be merged into the real h/w device node.
22660Sstevel@tonic-gate 	 * The interpretation of the unit-address is DD[,F]
22670Sstevel@tonic-gate 	 * where DD is the device id and F is the function.
22680Sstevel@tonic-gate 	 */
22690Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node(child) == 0) {
22700Sstevel@tonic-gate 		extern int pci_allow_pseudo_children;
22710Sstevel@tonic-gate 
22720Sstevel@tonic-gate 		/*
22730Sstevel@tonic-gate 		 * Try to merge the properties from this prototype
22740Sstevel@tonic-gate 		 * node into real h/w nodes.
22750Sstevel@tonic-gate 		 */
22760Sstevel@tonic-gate 		if (ndi_merge_node(child, db_name_child) == DDI_SUCCESS) {
22770Sstevel@tonic-gate 			/*
22780Sstevel@tonic-gate 			 * Merged ok - return failure to remove the node.
22790Sstevel@tonic-gate 			 */
22800Sstevel@tonic-gate 			return (DDI_FAILURE);
22810Sstevel@tonic-gate 		}
22820Sstevel@tonic-gate 
22830Sstevel@tonic-gate 		/* workaround for ddivs to run under PCI */
22840Sstevel@tonic-gate 		if (pci_allow_pseudo_children) {
22850Sstevel@tonic-gate 			return (DDI_SUCCESS);
22860Sstevel@tonic-gate 		}
22870Sstevel@tonic-gate 
22880Sstevel@tonic-gate 		/*
22890Sstevel@tonic-gate 		 * The child was not merged into a h/w node,
22900Sstevel@tonic-gate 		 * but there's not much we can do with it other
22910Sstevel@tonic-gate 		 * than return failure to cause the node to be removed.
22920Sstevel@tonic-gate 		 */
22930Sstevel@tonic-gate 		cmn_err(CE_WARN, "!%s@%s: %s.conf properties not merged",
22940Sstevel@tonic-gate 		    ddi_driver_name(child), ddi_get_name_addr(child),
22950Sstevel@tonic-gate 		    ddi_driver_name(child));
22960Sstevel@tonic-gate 		return (DDI_NOT_WELL_FORMED);
22970Sstevel@tonic-gate 	}
22980Sstevel@tonic-gate 
22990Sstevel@tonic-gate 
23000Sstevel@tonic-gate 	if ((db_create_pci_prop(child) != DDI_SUCCESS) ||
23010Sstevel@tonic-gate 	    (pci_config_setup(child, &config_handle) != DDI_SUCCESS)) {
23020Sstevel@tonic-gate 		db_uninitchild(child);
23030Sstevel@tonic-gate 		return (DDI_FAILURE);
23040Sstevel@tonic-gate 	}
23050Sstevel@tonic-gate 
23060Sstevel@tonic-gate 	/*
23070Sstevel@tonic-gate 	 * Determine the configuration header type.
23080Sstevel@tonic-gate 	 */
23090Sstevel@tonic-gate 	header_type = pci_config_get8(config_handle, PCI_CONF_HEADER);
23100Sstevel@tonic-gate 
23110Sstevel@tonic-gate 	/*
23120Sstevel@tonic-gate 	 * Support for the "command-preserve" property.
23130Sstevel@tonic-gate 	 */
23140Sstevel@tonic-gate 	command_preserve = ddi_prop_get_int(DDI_DEV_T_ANY, child,
23150Sstevel@tonic-gate 		DDI_PROP_DONTPASS, "command-preserve", 0);
23160Sstevel@tonic-gate 	command = pci_config_get16(config_handle, PCI_CONF_COMM);
23170Sstevel@tonic-gate 	command &= (command_preserve | PCI_COMM_BACK2BACK_ENAB);
23180Sstevel@tonic-gate 	command |= (db_command_default & ~command_preserve);
23190Sstevel@tonic-gate 	pci_config_put16(config_handle, PCI_CONF_COMM, command);
23200Sstevel@tonic-gate 
23210Sstevel@tonic-gate 	DB_DEBUG2(DB_INITCHILD, ddi_get_parent(child),
23220Sstevel@tonic-gate 		"initializing device vend=%x, devid=%x\n",
23230Sstevel@tonic-gate 			pci_config_get16(config_handle, PCI_CONF_VENID),
23240Sstevel@tonic-gate 			pci_config_get16(config_handle, PCI_CONF_DEVID));
23250Sstevel@tonic-gate 	/*
23260Sstevel@tonic-gate 	 * If the device has a bus control register then program it
23270Sstevel@tonic-gate 	 * based on the settings in the command register.
23280Sstevel@tonic-gate 	 */
23290Sstevel@tonic-gate 	if ((header_type & PCI_HEADER_TYPE_M) == PCI_HEADER_ONE) {
23300Sstevel@tonic-gate 		bcr = pci_config_get8(config_handle, PCI_BCNF_BCNTRL);
23310Sstevel@tonic-gate 		if (db_command_default & PCI_COMM_PARITY_DETECT)
23320Sstevel@tonic-gate 			bcr |= PCI_BCNF_BCNTRL_PARITY_ENABLE;
23330Sstevel@tonic-gate 		if (db_command_default & PCI_COMM_SERR_ENABLE)
23340Sstevel@tonic-gate 			bcr |= PCI_BCNF_BCNTRL_SERR_ENABLE;
23350Sstevel@tonic-gate 		bcr |= PCI_BCNF_BCNTRL_MAST_AB_MODE;
23360Sstevel@tonic-gate 		pci_config_put8(config_handle, PCI_BCNF_BCNTRL, bcr);
23370Sstevel@tonic-gate 	}
23380Sstevel@tonic-gate 
23390Sstevel@tonic-gate 	dbp = (db_ctrl_t *)ddi_get_soft_state(db_state,
23400Sstevel@tonic-gate 	    ddi_get_instance(ddi_get_parent(child)));
23410Sstevel@tonic-gate 
23420Sstevel@tonic-gate 	/*
23430Sstevel@tonic-gate 	 * Initialize cache-line-size configuration register if needed.
23440Sstevel@tonic-gate 	 */
23450Sstevel@tonic-gate 	if (db_set_cache_line_size_register &&
23460Sstevel@tonic-gate 	    ddi_getprop(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS,
23470Sstevel@tonic-gate 		"cache-line-size", 0) == 0) {
23480Sstevel@tonic-gate 		pci_config_put8(config_handle, PCI_CONF_CACHE_LINESZ,
23490Sstevel@tonic-gate 			dbp->cache_line_size);
23500Sstevel@tonic-gate 		n = pci_config_get8(config_handle, PCI_CONF_CACHE_LINESZ);
23510Sstevel@tonic-gate 		if (n != 0) {
23520Sstevel@tonic-gate 			(void) ndi_prop_update_int(DDI_DEV_T_NONE, child,
23530Sstevel@tonic-gate 					"cache-line-size", n);
23540Sstevel@tonic-gate 		}
23550Sstevel@tonic-gate 		DB_DEBUG1(DB_INITCHILD, ddi_get_parent(child),
23560Sstevel@tonic-gate 			"\nChild Device Cache Size %x\n", dbp->cache_line_size);
23570Sstevel@tonic-gate 	}
23580Sstevel@tonic-gate 
23590Sstevel@tonic-gate 	/*
23600Sstevel@tonic-gate 	 * Initialize latency timer configuration registers if needed.
23610Sstevel@tonic-gate 	 */
23620Sstevel@tonic-gate 	if (db_set_latency_timer_register &&
23630Sstevel@tonic-gate 	    ddi_getprop(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS,
23640Sstevel@tonic-gate 		"latency-timer", 0) == 0) {
23650Sstevel@tonic-gate 
23660Sstevel@tonic-gate 		if ((header_type & PCI_HEADER_TYPE_M) == PCI_HEADER_ONE) {
23670Sstevel@tonic-gate 			latency_timer = dbp->p_latency_timer;
23680Sstevel@tonic-gate 			pci_config_put8(config_handle, PCI_BCNF_LATENCY_TIMER,
23690Sstevel@tonic-gate 				dbp->latency_timer);
23700Sstevel@tonic-gate 		} else {
23710Sstevel@tonic-gate 			min_gnt = pci_config_get8(config_handle,
23720Sstevel@tonic-gate 				PCI_CONF_MIN_G);
23730Sstevel@tonic-gate 			latency_timer = min_gnt * 8;
23740Sstevel@tonic-gate 		}
23750Sstevel@tonic-gate 		pci_config_put8(config_handle, PCI_CONF_LATENCY_TIMER,
23760Sstevel@tonic-gate 			latency_timer);
23770Sstevel@tonic-gate 		n = pci_config_get8(config_handle, PCI_CONF_LATENCY_TIMER);
23780Sstevel@tonic-gate 		if (n != 0) {
23790Sstevel@tonic-gate 			(void) ndi_prop_update_int(DDI_DEV_T_NONE, child,
23800Sstevel@tonic-gate 					"latency-timer", n);
23810Sstevel@tonic-gate 		}
23820Sstevel@tonic-gate 		DB_DEBUG1(DB_INITCHILD, ddi_get_parent(child),
23830Sstevel@tonic-gate 			"\nChild Device latency %x\n", latency_timer);
23840Sstevel@tonic-gate 	}
23850Sstevel@tonic-gate 
23860Sstevel@tonic-gate 	pci_config_teardown(&config_handle);
23870Sstevel@tonic-gate 	return (DDI_SUCCESS);
23880Sstevel@tonic-gate }
23890Sstevel@tonic-gate 
23900Sstevel@tonic-gate static void
23910Sstevel@tonic-gate db_uninitchild(dev_info_t *dip)
23920Sstevel@tonic-gate {
23930Sstevel@tonic-gate 	ddi_set_name_addr(dip, NULL);
23940Sstevel@tonic-gate 
23950Sstevel@tonic-gate 	/*
23960Sstevel@tonic-gate 	 * Strip the node to properly convert it back to prototype form
23970Sstevel@tonic-gate 	 */
23980Sstevel@tonic-gate 	impl_rem_dev_props(dip);
23990Sstevel@tonic-gate }
24000Sstevel@tonic-gate 
24010Sstevel@tonic-gate static int
24020Sstevel@tonic-gate db_create_pci_prop(dev_info_t *child)
24030Sstevel@tonic-gate {
24040Sstevel@tonic-gate 	pci_regspec_t *pci_rp;
24050Sstevel@tonic-gate 	int	length;
24060Sstevel@tonic-gate 	int	value;
24070Sstevel@tonic-gate 
24080Sstevel@tonic-gate 	/* get child "reg" property */
2409506Scth 	value = ddi_getlongprop(DDI_DEV_T_ANY, child, DDI_PROP_CANSLEEP,
24100Sstevel@tonic-gate 		"reg", (caddr_t)&pci_rp, &length);
24110Sstevel@tonic-gate 	if (value != DDI_SUCCESS)
24120Sstevel@tonic-gate 		return (value);
24130Sstevel@tonic-gate 
24140Sstevel@tonic-gate 	(void) ndi_prop_update_byte_array(DDI_DEV_T_NONE, child, "reg",
24150Sstevel@tonic-gate 		(uchar_t *)pci_rp, length);
24160Sstevel@tonic-gate 
24170Sstevel@tonic-gate 	/*
24180Sstevel@tonic-gate 	 * free the memory allocated by ddi_getlongprop ().
24190Sstevel@tonic-gate 	 */
24200Sstevel@tonic-gate 	kmem_free(pci_rp, length);
24210Sstevel@tonic-gate 
24220Sstevel@tonic-gate 	/*
24230Sstevel@tonic-gate 	 * No need to create any 1275 properties here, because either
24240Sstevel@tonic-gate 	 * the OBP creates them or the hotplug framework creates it
24250Sstevel@tonic-gate 	 * during a hotplug operation. So lets return here.
24260Sstevel@tonic-gate 	 */
24270Sstevel@tonic-gate 	return (DDI_SUCCESS);
24280Sstevel@tonic-gate }
24290Sstevel@tonic-gate 
24300Sstevel@tonic-gate /*
24310Sstevel@tonic-gate  * db_save_config_regs
24320Sstevel@tonic-gate  *
24330Sstevel@tonic-gate  * This routine saves the state of the configuration registers of all
24340Sstevel@tonic-gate  * immediate child nodes.
24350Sstevel@tonic-gate  *
24360Sstevel@tonic-gate  * used by: db_detach() on suspends
24370Sstevel@tonic-gate  *
24380Sstevel@tonic-gate  * return value: DDI_SUCCESS: ALl children state saved.
24390Sstevel@tonic-gate  *		 DDI_FAILURE: Child device state could not be saved.
24400Sstevel@tonic-gate  */
24410Sstevel@tonic-gate static int
24420Sstevel@tonic-gate db_save_config_regs(db_ctrl_t *dbp)
24430Sstevel@tonic-gate {
24440Sstevel@tonic-gate 	int i;
24450Sstevel@tonic-gate 	dev_info_t *dip;
24460Sstevel@tonic-gate 	ddi_acc_handle_t config_handle;
24470Sstevel@tonic-gate 	db_cfg_state_t *statep;
24480Sstevel@tonic-gate 
24490Sstevel@tonic-gate 	for (i = 0, dip = ddi_get_child(dbp->dip); dip != NULL;
24500Sstevel@tonic-gate 		dip = ddi_get_next_sibling(dip)) {
24510Sstevel@tonic-gate 		if (i_ddi_node_state(dip) >= DS_ATTACHED)
24520Sstevel@tonic-gate 			i++;
24530Sstevel@tonic-gate 	}
24540Sstevel@tonic-gate 	dbp->config_state_index = i;
24550Sstevel@tonic-gate 
24560Sstevel@tonic-gate 	if (!i) {
24570Sstevel@tonic-gate 		/* no children */
24580Sstevel@tonic-gate 		dbp->db_config_state_p = NULL;
24590Sstevel@tonic-gate 		return (DDI_SUCCESS);
24600Sstevel@tonic-gate 	}
24610Sstevel@tonic-gate 
24620Sstevel@tonic-gate 	/* i now equals the total number of child devices */
24630Sstevel@tonic-gate 	dbp->db_config_state_p =
24640Sstevel@tonic-gate 		kmem_zalloc(i * sizeof (db_cfg_state_t), KM_NOSLEEP);
24650Sstevel@tonic-gate 	if (!dbp->db_config_state_p) {
24660Sstevel@tonic-gate 		cmn_err(CE_WARN,
24670Sstevel@tonic-gate 			"%s#%d: No memory to save state for child %s#%d\n",
24680Sstevel@tonic-gate 			ddi_driver_name(dbp->dip),
24690Sstevel@tonic-gate 			ddi_get_instance(dbp->dip),
24700Sstevel@tonic-gate 			ddi_get_name(dip), ddi_get_instance(dip));
24710Sstevel@tonic-gate 		return (DDI_FAILURE);
24720Sstevel@tonic-gate 	}
24730Sstevel@tonic-gate 
24740Sstevel@tonic-gate 	for (statep = dbp->db_config_state_p,
24750Sstevel@tonic-gate 		dip = ddi_get_child(dbp->dip);
24760Sstevel@tonic-gate 		dip != NULL;
24770Sstevel@tonic-gate 		dip = ddi_get_next_sibling(dip)) {
24780Sstevel@tonic-gate 
24790Sstevel@tonic-gate 		if (i_ddi_node_state(dip) < DS_ATTACHED)
24800Sstevel@tonic-gate 			continue;
24810Sstevel@tonic-gate 
24820Sstevel@tonic-gate 		if (pci_config_setup(dip, &config_handle) != DDI_SUCCESS) {
24830Sstevel@tonic-gate 			cmn_err(CE_WARN,
24840Sstevel@tonic-gate 				"%s#%d: can't config space for %s#%d",
24850Sstevel@tonic-gate 				ddi_driver_name(dbp->dip),
24860Sstevel@tonic-gate 				ddi_get_instance(dbp->dip),
24870Sstevel@tonic-gate 				ddi_driver_name(dip),
24880Sstevel@tonic-gate 				ddi_get_instance(dip));
24890Sstevel@tonic-gate 			continue;
24900Sstevel@tonic-gate 		}
24910Sstevel@tonic-gate 
24920Sstevel@tonic-gate 		statep->dip = dip;
24930Sstevel@tonic-gate 		statep->command =
24940Sstevel@tonic-gate 			pci_config_get16(config_handle, PCI_CONF_COMM);
24950Sstevel@tonic-gate 		statep->header_type =
24960Sstevel@tonic-gate 			pci_config_get8(config_handle, PCI_CONF_HEADER);
24970Sstevel@tonic-gate 		if ((statep->header_type & PCI_HEADER_TYPE_M) == PCI_HEADER_ONE)
24980Sstevel@tonic-gate 			statep->bridge_control =
24990Sstevel@tonic-gate 			    pci_config_get16(config_handle, PCI_BCNF_BCNTRL);
25000Sstevel@tonic-gate 		statep->cache_line_size =
25010Sstevel@tonic-gate 			pci_config_get8(config_handle, PCI_CONF_CACHE_LINESZ);
25020Sstevel@tonic-gate 		statep->latency_timer =
25030Sstevel@tonic-gate 			pci_config_get8(config_handle, PCI_CONF_LATENCY_TIMER);
25040Sstevel@tonic-gate 		if ((statep->header_type & PCI_HEADER_TYPE_M) == PCI_HEADER_ONE)
25050Sstevel@tonic-gate 			statep->sec_latency_timer =
25060Sstevel@tonic-gate 			    pci_config_get8(config_handle,
25070Sstevel@tonic-gate 				PCI_BCNF_LATENCY_TIMER);
25080Sstevel@tonic-gate 		pci_config_teardown(&config_handle);
25090Sstevel@tonic-gate 		statep++;
25100Sstevel@tonic-gate 	}
25110Sstevel@tonic-gate 	return (DDI_SUCCESS);
25120Sstevel@tonic-gate }
25130Sstevel@tonic-gate 
25140Sstevel@tonic-gate 
25150Sstevel@tonic-gate /*
25160Sstevel@tonic-gate  * db_restore_config_regs
25170Sstevel@tonic-gate  *
25180Sstevel@tonic-gate  * This routine restores the state of the configuration registers of
25190Sstevel@tonic-gate  * all immediate child nodes.
25200Sstevel@tonic-gate  *
25210Sstevel@tonic-gate  * used by: db_attach() on resume
25220Sstevel@tonic-gate  *
25230Sstevel@tonic-gate  * return value: none
25240Sstevel@tonic-gate  */
25250Sstevel@tonic-gate static int
25260Sstevel@tonic-gate db_restore_config_regs(db_ctrl_t *dbp)
25270Sstevel@tonic-gate {
25280Sstevel@tonic-gate 	int i;
25290Sstevel@tonic-gate 	dev_info_t *dip;
25300Sstevel@tonic-gate 	ddi_acc_handle_t config_handle;
25310Sstevel@tonic-gate 	db_cfg_state_t *statep = dbp->db_config_state_p;
25320Sstevel@tonic-gate 
25330Sstevel@tonic-gate 	for (i = 0; i < dbp->config_state_index; i++, statep++) {
25340Sstevel@tonic-gate 		dip = statep->dip;
25350Sstevel@tonic-gate 		if (!dip) {
25360Sstevel@tonic-gate 			cmn_err(CE_WARN,
25370Sstevel@tonic-gate 				"%s#%d: skipping bad dev info (index %d)",
25380Sstevel@tonic-gate 				ddi_driver_name(dbp->dip),
25390Sstevel@tonic-gate 				ddi_get_instance(dbp->dip), i);
25400Sstevel@tonic-gate 			continue;
25410Sstevel@tonic-gate 		}
25420Sstevel@tonic-gate 		if (pci_config_setup(dip, &config_handle) != DDI_SUCCESS) {
25430Sstevel@tonic-gate 			cmn_err(CE_WARN,
25440Sstevel@tonic-gate 				"%s#%d: can't config space for %s#%d",
25450Sstevel@tonic-gate 				ddi_driver_name(dbp->dip),
25460Sstevel@tonic-gate 				ddi_get_instance(dbp->dip),
25470Sstevel@tonic-gate 				ddi_driver_name(dip),
25480Sstevel@tonic-gate 				ddi_get_instance(dip));
25490Sstevel@tonic-gate 			continue;
25500Sstevel@tonic-gate 		}
25510Sstevel@tonic-gate 		pci_config_put16(config_handle, PCI_CONF_COMM, statep->command);
25520Sstevel@tonic-gate 		if ((statep->header_type & PCI_HEADER_TYPE_M) == PCI_HEADER_ONE)
25530Sstevel@tonic-gate 			pci_config_put16(config_handle, PCI_BCNF_BCNTRL,
25540Sstevel@tonic-gate 						statep->bridge_control);
25550Sstevel@tonic-gate 		pci_config_put8(config_handle, PCI_CONF_CACHE_LINESZ,
25560Sstevel@tonic-gate 						statep->cache_line_size);
25570Sstevel@tonic-gate 		pci_config_put8(config_handle, PCI_CONF_LATENCY_TIMER,
25580Sstevel@tonic-gate 						statep->latency_timer);
25590Sstevel@tonic-gate 		if ((statep->header_type & PCI_HEADER_TYPE_M) == PCI_HEADER_ONE)
25600Sstevel@tonic-gate 			pci_config_put8(config_handle, PCI_BCNF_LATENCY_TIMER,
25610Sstevel@tonic-gate 						statep->sec_latency_timer);
25620Sstevel@tonic-gate 		pci_config_teardown(&config_handle);
25630Sstevel@tonic-gate 	}
25640Sstevel@tonic-gate 
25650Sstevel@tonic-gate 	kmem_free(dbp->db_config_state_p,
25660Sstevel@tonic-gate 		dbp->config_state_index * sizeof (db_cfg_state_t));
25670Sstevel@tonic-gate 	dbp->db_config_state_p = NULL;
25680Sstevel@tonic-gate 	dbp->config_state_index = 0;
25690Sstevel@tonic-gate 
25700Sstevel@tonic-gate 	return (DDI_SUCCESS);
25710Sstevel@tonic-gate }
25720Sstevel@tonic-gate 
25730Sstevel@tonic-gate /* put a type 0/1 address on the bus */
25740Sstevel@tonic-gate static void
25750Sstevel@tonic-gate db_put_reg_conf_addr(db_acc_pvt_t *db_pvt, uint32_t conf_addr)
25760Sstevel@tonic-gate {
25770Sstevel@tonic-gate 	if (db_pvt->access_mode & DB_PCI_CONF_CYCLE_TYPE0)\
25780Sstevel@tonic-gate 		ddi_put32(db_pvt->handle, db_pvt->addr, (uint32_t)\
25790Sstevel@tonic-gate 			DB_PCI_CONF_CYCLE_TYPE0_ADDR((conf_addr)));\
25800Sstevel@tonic-gate 	else	/* type 1 cycle */\
25810Sstevel@tonic-gate 		ddi_put32(db_pvt->handle, db_pvt->addr, (uint32_t)\
25820Sstevel@tonic-gate 				DB_PCI_CONF_CYCLE_TYPE1_ADDR((conf_addr)));
25830Sstevel@tonic-gate }
25840Sstevel@tonic-gate 
25850Sstevel@tonic-gate /* Get 8bits data off the 32bit data */
25860Sstevel@tonic-gate static uint8_t
25870Sstevel@tonic-gate db_get_data8(uint32_t addr, uint32_t data)
25880Sstevel@tonic-gate {
25890Sstevel@tonic-gate 	return (((data) >> (((addr) & 3) * 8)) & 0xff);
25900Sstevel@tonic-gate }
25910Sstevel@tonic-gate 
25920Sstevel@tonic-gate /* Get 16bits data off the 32bit data */
25930Sstevel@tonic-gate static uint16_t
25940Sstevel@tonic-gate db_get_data16(uint32_t addr, uint32_t data)
25950Sstevel@tonic-gate {
25960Sstevel@tonic-gate 	return (((data) >> (((addr) & 3) * 8)) & 0xffff);
25970Sstevel@tonic-gate }
25980Sstevel@tonic-gate 
25990Sstevel@tonic-gate /* merge 8bit data into the 32bit data */
26000Sstevel@tonic-gate static uint32_t
26010Sstevel@tonic-gate db_put_data8(uint32_t addr, uint32_t rdata, uint8_t wdata)
26020Sstevel@tonic-gate {
26030Sstevel@tonic-gate 	return ((rdata & (~((0xff << ((((addr) & 3) * 8))) & 0xffffffff))) |
26040Sstevel@tonic-gate 			(((wdata) & 0xff)<<((((addr) & 3))*8)));
26050Sstevel@tonic-gate }
26060Sstevel@tonic-gate 
26070Sstevel@tonic-gate /* merge 16bit data into the 32bit data */
26080Sstevel@tonic-gate static uint32_t
26090Sstevel@tonic-gate db_put_data16(uint32_t addr, uint32_t rdata, uint16_t wdata)
26100Sstevel@tonic-gate {
26110Sstevel@tonic-gate 	return ((rdata & (~((0xffff << ((((addr) & 3) * 8))) & 0xffffffff))) |
26120Sstevel@tonic-gate 			(((wdata) & 0xffff) << ((((addr) & 3))*8)));
26130Sstevel@tonic-gate }
26140Sstevel@tonic-gate 
26150Sstevel@tonic-gate 
26160Sstevel@tonic-gate /*
26170Sstevel@tonic-gate  * For the next set of PCI configuration IO calls, we need
26180Sstevel@tonic-gate  * to make sure we own the bus before generating the config cycles,
26190Sstevel@tonic-gate  * using the drawbridge's semaphore method.
26200Sstevel@tonic-gate  */
26210Sstevel@tonic-gate 
26220Sstevel@tonic-gate /*
26230Sstevel@tonic-gate  * Function to read 8 bit data off the PCI configuration space behind
26240Sstevel@tonic-gate  * the 21554's host interface.
26250Sstevel@tonic-gate  */
26260Sstevel@tonic-gate static uint8_t
26270Sstevel@tonic-gate db_ddi_get8(ddi_acc_impl_t *handle, uint8_t *addr)
26280Sstevel@tonic-gate {
26290Sstevel@tonic-gate 	uint32_t data;
26300Sstevel@tonic-gate 
26310Sstevel@tonic-gate 	data = db_ddi_get32(handle, (uint32_t *)addr);
2632*946Smathue 	return (db_get_data8((uint32_t)(uintptr_t)addr, data));
26330Sstevel@tonic-gate }
26340Sstevel@tonic-gate 
26350Sstevel@tonic-gate /*
26360Sstevel@tonic-gate  * Function to read 16 bit data off the PCI configuration space behind
26370Sstevel@tonic-gate  * the 21554's host interface.
26380Sstevel@tonic-gate  */
26390Sstevel@tonic-gate static uint16_t
26400Sstevel@tonic-gate db_ddi_get16(ddi_acc_impl_t *handle, uint16_t *addr)
26410Sstevel@tonic-gate {
26420Sstevel@tonic-gate 	uint32_t data;
26430Sstevel@tonic-gate 
26440Sstevel@tonic-gate 	data = db_ddi_get32(handle, (uint32_t *)addr);
2645*946Smathue 	return (db_get_data16((uint32_t)(uintptr_t)addr, data));
26460Sstevel@tonic-gate }
26470Sstevel@tonic-gate 
26480Sstevel@tonic-gate /*
26490Sstevel@tonic-gate  * Function to read 32 bit data off the PCI configuration space behind
26500Sstevel@tonic-gate  * the 21554's host interface.
26510Sstevel@tonic-gate  */
26520Sstevel@tonic-gate static uint32_t
26530Sstevel@tonic-gate db_ddi_get32(ddi_acc_impl_t *handle, uint32_t *addr)
26540Sstevel@tonic-gate {
26550Sstevel@tonic-gate 	db_acc_pvt_t 	*db_pvt = (db_acc_pvt_t *)
26560Sstevel@tonic-gate 					handle->ahi_common.ah_bus_private;
26570Sstevel@tonic-gate 	uint32_t 	wait_count = 0;
26580Sstevel@tonic-gate 	uint32_t 	data;
26590Sstevel@tonic-gate 	db_ctrl_t	*dbp;
26600Sstevel@tonic-gate 
26610Sstevel@tonic-gate 	dbp = db_pvt->dbp;
26620Sstevel@tonic-gate 
26630Sstevel@tonic-gate 	mutex_enter(&dbp->db_busown);
26640Sstevel@tonic-gate 
26650Sstevel@tonic-gate 	if (db_use_config_own_bit) {
26660Sstevel@tonic-gate 		/*
26670Sstevel@tonic-gate 		 * check if (upstream/downstream)configuration address own
26680Sstevel@tonic-gate 		 * bit set. With this set, we cannot proceed.
26690Sstevel@tonic-gate 		 */
26700Sstevel@tonic-gate 		while (((ddi_get8(db_pvt->handle, db_pvt->bus_own)) &
26710Sstevel@tonic-gate 				db_pvt->mask) == db_pvt->mask) {
26720Sstevel@tonic-gate #ifdef DEBUG
26730Sstevel@tonic-gate 			if (dbp->db_pci_max_wait_count < wait_count)
26740Sstevel@tonic-gate 				dbp->db_pci_max_wait_count = wait_count;
26750Sstevel@tonic-gate #endif
26760Sstevel@tonic-gate 			drv_usecwait(db_pci_own_wait);
26770Sstevel@tonic-gate 			if (++wait_count == db_pci_max_wait) {
26780Sstevel@tonic-gate 				/*
26790Sstevel@tonic-gate 				 * the man page for pci_config_* routines do
26800Sstevel@tonic-gate 				 * Not specify any error condition values.
26810Sstevel@tonic-gate 				 */
26820Sstevel@tonic-gate 				cmn_err(CE_WARN,
26830Sstevel@tonic-gate 					"%s#%d: pci config bus own error",
26840Sstevel@tonic-gate 					ddi_driver_name(dbp->dip),
26850Sstevel@tonic-gate 					ddi_get_instance(dbp->dip));
26860Sstevel@tonic-gate 				dbp->db_pci_err_count++;
26870Sstevel@tonic-gate 				mutex_exit(&dbp->db_busown);
26880Sstevel@tonic-gate 				return ((uint32_t)DB_CONF_FAILURE);
26890Sstevel@tonic-gate 			}
26900Sstevel@tonic-gate 		}
26910Sstevel@tonic-gate 		wait_count = 0;
26920Sstevel@tonic-gate 	}
26930Sstevel@tonic-gate 
2694*946Smathue 	db_put_reg_conf_addr(db_pvt, (uint32_t)(uintptr_t)addr);
26950Sstevel@tonic-gate 	data = ddi_get32(db_pvt->handle, (uint32_t *)db_pvt->data);
26960Sstevel@tonic-gate 
26970Sstevel@tonic-gate 	if (db_use_config_own_bit) {
26980Sstevel@tonic-gate 		while (((ddi_get8(db_pvt->handle, db_pvt->bus_release)) &
26990Sstevel@tonic-gate 				db_pvt->mask) == db_pvt->mask) {
27000Sstevel@tonic-gate #ifdef DEBUG
27010Sstevel@tonic-gate 			if (dbp->db_pci_max_wait_count < wait_count)
27020Sstevel@tonic-gate 				dbp->db_pci_max_wait_count = wait_count;
27030Sstevel@tonic-gate #endif
27040Sstevel@tonic-gate 			drv_usecwait(db_pci_release_wait);
27050Sstevel@tonic-gate 			if (++wait_count == db_pci_max_wait) {
27060Sstevel@tonic-gate 				/*
27070Sstevel@tonic-gate 				 * the man page for pci_config_* routines do
27080Sstevel@tonic-gate 				 * not specify any error condition values.
27090Sstevel@tonic-gate 				 */
27100Sstevel@tonic-gate 				cmn_err(CE_WARN,
27110Sstevel@tonic-gate 					"%s#%d: pci config bus release error",
27120Sstevel@tonic-gate 					ddi_driver_name(dbp->dip),
27130Sstevel@tonic-gate 					ddi_get_instance(dbp->dip));
27140Sstevel@tonic-gate 				dbp->db_pci_err_count++;
27150Sstevel@tonic-gate 				mutex_exit(&dbp->db_busown);
27160Sstevel@tonic-gate 				return ((uint32_t)DB_CONF_FAILURE);
27170Sstevel@tonic-gate 			}
27180Sstevel@tonic-gate 			data = ddi_get32(db_pvt->handle,
27190Sstevel@tonic-gate 					(uint32_t *)db_pvt->data);
27200Sstevel@tonic-gate 		}
27210Sstevel@tonic-gate 	}
27220Sstevel@tonic-gate 
27230Sstevel@tonic-gate 	mutex_exit(&dbp->db_busown);
27240Sstevel@tonic-gate 
27250Sstevel@tonic-gate 	return (data);
27260Sstevel@tonic-gate }
27270Sstevel@tonic-gate 
27280Sstevel@tonic-gate /*
27290Sstevel@tonic-gate  * Function to read 64 bit data off the PCI configuration space behind
27300Sstevel@tonic-gate  * the 21554's host interface.
27310Sstevel@tonic-gate  */
27320Sstevel@tonic-gate static uint64_t
27330Sstevel@tonic-gate db_ddi_get64(ddi_acc_impl_t *handle, uint64_t *addr)
27340Sstevel@tonic-gate {
27350Sstevel@tonic-gate 	uint64_t udata, ldata;
27360Sstevel@tonic-gate 
27370Sstevel@tonic-gate 	ldata = (uint32_t)db_ddi_get32(handle, (uint32_t *)addr);
27380Sstevel@tonic-gate 	udata = (uint32_t)db_ddi_get32(handle, (uint32_t *)addr + 1);
27390Sstevel@tonic-gate 	return (ldata | (udata << 32));
27400Sstevel@tonic-gate }
27410Sstevel@tonic-gate 
27420Sstevel@tonic-gate /*
27430Sstevel@tonic-gate  * Function to write 8 bit data into the PCI configuration space behind
27440Sstevel@tonic-gate  * the 21554's host interface.
27450Sstevel@tonic-gate  */
27460Sstevel@tonic-gate static void
27470Sstevel@tonic-gate db_ddi_put8(ddi_acc_impl_t *handle, uint8_t *addr, uint8_t data)
27480Sstevel@tonic-gate {
27490Sstevel@tonic-gate 	uint32_t rdata;
27500Sstevel@tonic-gate 
27510Sstevel@tonic-gate 	rdata = db_ddi_get32(handle, (uint32_t *)addr);
27520Sstevel@tonic-gate 	db_ddi_put32(handle, (uint32_t *)addr,
2753*946Smathue 		db_put_data8((uint32_t)(uintptr_t)addr, rdata, data));
27540Sstevel@tonic-gate }
27550Sstevel@tonic-gate 
27560Sstevel@tonic-gate /*
27570Sstevel@tonic-gate  * Function to write 16 bit data into the PCI configuration space behind
27580Sstevel@tonic-gate  * the 21554's host interface.
27590Sstevel@tonic-gate  */
27600Sstevel@tonic-gate static void
27610Sstevel@tonic-gate db_ddi_put16(ddi_acc_impl_t *handle, uint16_t *addr, uint16_t data)
27620Sstevel@tonic-gate {
27630Sstevel@tonic-gate 	uint32_t rdata;
27640Sstevel@tonic-gate 
27650Sstevel@tonic-gate 	rdata = db_ddi_get32(handle, (uint32_t *)addr);
27660Sstevel@tonic-gate 	db_ddi_put32(handle, (uint32_t *)addr,
2767*946Smathue 			db_put_data16((uint32_t)(uintptr_t)addr, rdata, data));
27680Sstevel@tonic-gate }
27690Sstevel@tonic-gate 
27700Sstevel@tonic-gate /*
27710Sstevel@tonic-gate  * Function to write 32 bit data into the PCI configuration space behind
27720Sstevel@tonic-gate  * the 21554's host interface.
27730Sstevel@tonic-gate  */
27740Sstevel@tonic-gate static void
27750Sstevel@tonic-gate db_ddi_put32(ddi_acc_impl_t *handle, uint32_t *addr, uint32_t data)
27760Sstevel@tonic-gate {
27770Sstevel@tonic-gate 	db_acc_pvt_t 	*db_pvt = (db_acc_pvt_t *)
27780Sstevel@tonic-gate 					handle->ahi_common.ah_bus_private;
27790Sstevel@tonic-gate 	db_ctrl_t	*dbp;
27800Sstevel@tonic-gate 	uint32_t 	wait_count = 0;
27810Sstevel@tonic-gate 
27820Sstevel@tonic-gate 	dbp = db_pvt->dbp;
27830Sstevel@tonic-gate 
27840Sstevel@tonic-gate 	mutex_enter(&dbp->db_busown);
27850Sstevel@tonic-gate 
27860Sstevel@tonic-gate 	if (db_use_config_own_bit) {
27870Sstevel@tonic-gate 		/*
27880Sstevel@tonic-gate 		 * check if (upstream/downstream)configuration address own
27890Sstevel@tonic-gate 		 * bit set. with this set, we cannot proceed.
27900Sstevel@tonic-gate 		 */
27910Sstevel@tonic-gate 		while (((ddi_get8(db_pvt->handle, db_pvt->bus_own)) &
27920Sstevel@tonic-gate 				db_pvt->mask) == db_pvt->mask) {
27930Sstevel@tonic-gate #ifdef DEBUG
27940Sstevel@tonic-gate 			if (dbp->db_pci_max_wait_count < wait_count)
27950Sstevel@tonic-gate 				dbp->db_pci_max_wait_count = wait_count;
27960Sstevel@tonic-gate #endif
27970Sstevel@tonic-gate 			drv_usecwait(db_pci_own_wait);
27980Sstevel@tonic-gate 			if (++wait_count == db_pci_max_wait) {
27990Sstevel@tonic-gate 				/*
28000Sstevel@tonic-gate 				 * Since the return value is void here,
28010Sstevel@tonic-gate 				 * we may need to print a message, as this
28020Sstevel@tonic-gate 				 * could be a serious situation.
28030Sstevel@tonic-gate 				 */
28040Sstevel@tonic-gate 				cmn_err(CE_WARN,
28050Sstevel@tonic-gate 					"%s#%d: pci config bus own error",
28060Sstevel@tonic-gate 					ddi_driver_name(dbp->dip),
28070Sstevel@tonic-gate 					ddi_get_instance(dbp->dip));
28080Sstevel@tonic-gate 				dbp->db_pci_err_count++;
28090Sstevel@tonic-gate 				mutex_exit(&dbp->db_busown);
28100Sstevel@tonic-gate 				return;
28110Sstevel@tonic-gate 			}
28120Sstevel@tonic-gate 		}
28130Sstevel@tonic-gate 		wait_count = 0;
28140Sstevel@tonic-gate 	}
28150Sstevel@tonic-gate 
2816*946Smathue 	db_put_reg_conf_addr(db_pvt, (uint32_t)(uintptr_t)addr);
28170Sstevel@tonic-gate 	ddi_put32(db_pvt->handle, (uint32_t *)db_pvt->data, data);
28180Sstevel@tonic-gate 
28190Sstevel@tonic-gate 	if (db_use_config_own_bit) {
28200Sstevel@tonic-gate 		while (((ddi_get8(db_pvt->handle, db_pvt->bus_release)) &
28210Sstevel@tonic-gate 				db_pvt->mask) == db_pvt->mask) {
28220Sstevel@tonic-gate #ifdef DEBUG
28230Sstevel@tonic-gate 			if (dbp->db_pci_max_wait_count < wait_count)
28240Sstevel@tonic-gate 				dbp->db_pci_max_wait_count = wait_count;
28250Sstevel@tonic-gate #endif
28260Sstevel@tonic-gate 			drv_usecwait(db_pci_release_wait);
28270Sstevel@tonic-gate 			if (++wait_count == db_pci_max_wait) {
28280Sstevel@tonic-gate 				/*
28290Sstevel@tonic-gate 				 * the man page for pci_config_* routines do
28300Sstevel@tonic-gate 				 * Not specify any error condition values.
28310Sstevel@tonic-gate 				 */
28320Sstevel@tonic-gate 				cmn_err(CE_WARN,
28330Sstevel@tonic-gate 					"%s#%d: pci config bus release error",
28340Sstevel@tonic-gate 					ddi_driver_name(dbp->dip),
28350Sstevel@tonic-gate 					ddi_get_instance(dbp->dip));
28360Sstevel@tonic-gate 				dbp->db_pci_err_count++;
28370Sstevel@tonic-gate 				mutex_exit(&dbp->db_busown);
28380Sstevel@tonic-gate 				return;
28390Sstevel@tonic-gate 			}
28400Sstevel@tonic-gate 			ddi_put32(db_pvt->handle, (uint32_t *)db_pvt->data,
28410Sstevel@tonic-gate 					data);
28420Sstevel@tonic-gate 		}
28430Sstevel@tonic-gate 	}
28440Sstevel@tonic-gate 
28450Sstevel@tonic-gate 	mutex_exit(&dbp->db_busown);
28460Sstevel@tonic-gate }
28470Sstevel@tonic-gate 
28480Sstevel@tonic-gate /*
28490Sstevel@tonic-gate  * Function to write 64 bit data into the PCI configuration space behind
28500Sstevel@tonic-gate  * the 21554's host interface.
28510Sstevel@tonic-gate  */
28520Sstevel@tonic-gate static void
28530Sstevel@tonic-gate db_ddi_put64(ddi_acc_impl_t *handle, uint64_t *addr, uint64_t data)
28540Sstevel@tonic-gate {
28550Sstevel@tonic-gate 	db_ddi_put32(handle, (uint32_t *)addr, (uint32_t)(data & 0xffffffff));
28560Sstevel@tonic-gate 	db_ddi_put32(handle, (uint32_t *)addr + 1, (uint32_t)(data >> 32));
28570Sstevel@tonic-gate }
28580Sstevel@tonic-gate 
28590Sstevel@tonic-gate /*
28600Sstevel@tonic-gate  * Function to rep read 8 bit data off the PCI configuration space behind
28610Sstevel@tonic-gate  * the 21554's host interface.
28620Sstevel@tonic-gate  */
28630Sstevel@tonic-gate static void
28640Sstevel@tonic-gate db_ddi_rep_get8(ddi_acc_impl_t *handle, uint8_t *host_addr,
28650Sstevel@tonic-gate 			uint8_t *dev_addr, size_t repcount, uint_t flags)
28660Sstevel@tonic-gate {
28670Sstevel@tonic-gate 	if (flags == DDI_DEV_AUTOINCR)
28680Sstevel@tonic-gate 		for (; repcount; repcount--)
28690Sstevel@tonic-gate 			*host_addr++ = db_ddi_get8(handle, dev_addr++);
28700Sstevel@tonic-gate 	else
28710Sstevel@tonic-gate 		for (; repcount; repcount--)
28720Sstevel@tonic-gate 			*host_addr++ = db_ddi_get8(handle, dev_addr);
28730Sstevel@tonic-gate }
28740Sstevel@tonic-gate 
28750Sstevel@tonic-gate /*
28760Sstevel@tonic-gate  * Function to rep read 16 bit data off the PCI configuration space behind
28770Sstevel@tonic-gate  * the 21554's host interface.
28780Sstevel@tonic-gate  */
28790Sstevel@tonic-gate static void
28800Sstevel@tonic-gate db_ddi_rep_get16(ddi_acc_impl_t *handle, uint16_t *host_addr,
28810Sstevel@tonic-gate 			uint16_t *dev_addr, size_t repcount, uint_t flags)
28820Sstevel@tonic-gate {
28830Sstevel@tonic-gate 	if (flags == DDI_DEV_AUTOINCR)
28840Sstevel@tonic-gate 		for (; repcount; repcount--)
28850Sstevel@tonic-gate 			*host_addr++ = db_ddi_get16(handle, dev_addr++);
28860Sstevel@tonic-gate 	else
28870Sstevel@tonic-gate 		for (; repcount; repcount--)
28880Sstevel@tonic-gate 			*host_addr++ = db_ddi_get16(handle, dev_addr);
28890Sstevel@tonic-gate }
28900Sstevel@tonic-gate 
28910Sstevel@tonic-gate /*
28920Sstevel@tonic-gate  * Function to rep read 32 bit data off the PCI configuration space behind
28930Sstevel@tonic-gate  * the 21554's host interface.
28940Sstevel@tonic-gate  */
28950Sstevel@tonic-gate static void
28960Sstevel@tonic-gate db_ddi_rep_get32(ddi_acc_impl_t *handle, uint32_t *host_addr,
28970Sstevel@tonic-gate 			uint32_t *dev_addr, size_t repcount, uint_t flags)
28980Sstevel@tonic-gate {
28990Sstevel@tonic-gate 	if (flags == DDI_DEV_AUTOINCR)
29000Sstevel@tonic-gate 		for (; repcount; repcount--)
29010Sstevel@tonic-gate 			*host_addr++ = db_ddi_get32(handle, dev_addr++);
29020Sstevel@tonic-gate 	else
29030Sstevel@tonic-gate 		for (; repcount; repcount--)
29040Sstevel@tonic-gate 			*host_addr++ = db_ddi_get32(handle, dev_addr);
29050Sstevel@tonic-gate }
29060Sstevel@tonic-gate 
29070Sstevel@tonic-gate /*
29080Sstevel@tonic-gate  * Function to rep read 64 bit data off the PCI configuration space behind
29090Sstevel@tonic-gate  * the 21554's host interface.
29100Sstevel@tonic-gate  */
29110Sstevel@tonic-gate static void
29120Sstevel@tonic-gate db_ddi_rep_get64(ddi_acc_impl_t *handle, uint64_t *host_addr,
29130Sstevel@tonic-gate 			uint64_t *dev_addr, size_t repcount, uint_t flags)
29140Sstevel@tonic-gate {
29150Sstevel@tonic-gate 	if (flags == DDI_DEV_AUTOINCR)
29160Sstevel@tonic-gate 		for (; repcount; repcount--)
29170Sstevel@tonic-gate 			*host_addr++ = db_ddi_get64(handle, dev_addr++);
29180Sstevel@tonic-gate 	else
29190Sstevel@tonic-gate 		for (; repcount; repcount--)
29200Sstevel@tonic-gate 			*host_addr++ = db_ddi_get64(handle, dev_addr);
29210Sstevel@tonic-gate }
29220Sstevel@tonic-gate 
29230Sstevel@tonic-gate /*
29240Sstevel@tonic-gate  * Function to rep write 8 bit data into the PCI configuration space behind
29250Sstevel@tonic-gate  * the 21554's host interface.
29260Sstevel@tonic-gate  */
29270Sstevel@tonic-gate static void
29280Sstevel@tonic-gate db_ddi_rep_put8(ddi_acc_impl_t *handle, uint8_t *host_addr,
29290Sstevel@tonic-gate 			uint8_t *dev_addr, size_t repcount, uint_t flags)
29300Sstevel@tonic-gate {
29310Sstevel@tonic-gate 	if (flags == DDI_DEV_AUTOINCR)
29320Sstevel@tonic-gate 		for (; repcount; repcount--)
29330Sstevel@tonic-gate 			db_ddi_put8(handle, dev_addr++, *host_addr++);
29340Sstevel@tonic-gate 	else
29350Sstevel@tonic-gate 		for (; repcount; repcount--)
29360Sstevel@tonic-gate 			db_ddi_put8(handle, dev_addr, *host_addr++);
29370Sstevel@tonic-gate }
29380Sstevel@tonic-gate 
29390Sstevel@tonic-gate /*
29400Sstevel@tonic-gate  * Function to rep write 16 bit data into the PCI configuration space behind
29410Sstevel@tonic-gate  * the 21554's host interface.
29420Sstevel@tonic-gate  */
29430Sstevel@tonic-gate static void
29440Sstevel@tonic-gate db_ddi_rep_put16(ddi_acc_impl_t *handle, uint16_t *host_addr,
29450Sstevel@tonic-gate 			uint16_t *dev_addr, size_t repcount, uint_t flags)
29460Sstevel@tonic-gate {
29470Sstevel@tonic-gate 	if (flags == DDI_DEV_AUTOINCR)
29480Sstevel@tonic-gate 		for (; repcount; repcount--)
29490Sstevel@tonic-gate 			db_ddi_put16(handle, dev_addr++, *host_addr++);
29500Sstevel@tonic-gate 	else
29510Sstevel@tonic-gate 		for (; repcount; repcount--)
29520Sstevel@tonic-gate 			db_ddi_put16(handle, dev_addr, *host_addr++);
29530Sstevel@tonic-gate }
29540Sstevel@tonic-gate 
29550Sstevel@tonic-gate /*
29560Sstevel@tonic-gate  * Function to rep write 32 bit data into the PCI configuration space behind
29570Sstevel@tonic-gate  * the 21554's host interface.
29580Sstevel@tonic-gate  */
29590Sstevel@tonic-gate static void
29600Sstevel@tonic-gate db_ddi_rep_put32(ddi_acc_impl_t *handle, uint32_t *host_addr,
29610Sstevel@tonic-gate 			uint32_t *dev_addr, size_t repcount, uint_t flags)
29620Sstevel@tonic-gate {
29630Sstevel@tonic-gate 	if (flags == DDI_DEV_AUTOINCR)
29640Sstevel@tonic-gate 		for (; repcount; repcount--)
29650Sstevel@tonic-gate 			db_ddi_put32(handle, dev_addr++, *host_addr++);
29660Sstevel@tonic-gate 	else
29670Sstevel@tonic-gate 		for (; repcount; repcount--)
29680Sstevel@tonic-gate 			db_ddi_put32(handle, dev_addr, *host_addr++);
29690Sstevel@tonic-gate }
29700Sstevel@tonic-gate 
29710Sstevel@tonic-gate /*
29720Sstevel@tonic-gate  * Function to rep write 64 bit data into the PCI configuration space behind
29730Sstevel@tonic-gate  * the 21554's host interface.
29740Sstevel@tonic-gate  */
29750Sstevel@tonic-gate static void
29760Sstevel@tonic-gate db_ddi_rep_put64(ddi_acc_impl_t *handle, uint64_t *host_addr,
29770Sstevel@tonic-gate 			uint64_t *dev_addr, size_t repcount, uint_t flags)
29780Sstevel@tonic-gate {
29790Sstevel@tonic-gate 	if (flags == DDI_DEV_AUTOINCR)
29800Sstevel@tonic-gate 		for (; repcount; repcount--)
29810Sstevel@tonic-gate 			db_ddi_put64(handle, dev_addr++, *host_addr++);
29820Sstevel@tonic-gate 	else
29830Sstevel@tonic-gate 		for (; repcount; repcount--)
29840Sstevel@tonic-gate 			db_ddi_put64(handle, dev_addr, *host_addr++);
29850Sstevel@tonic-gate }
29860Sstevel@tonic-gate 
29870Sstevel@tonic-gate #ifdef DEBUG
29880Sstevel@tonic-gate 
29890Sstevel@tonic-gate static void
29900Sstevel@tonic-gate db_debug(uint64_t func_id, dev_info_t *dip, char *fmt,
29910Sstevel@tonic-gate 	uintptr_t a1, uintptr_t a2, uintptr_t a3, uintptr_t a4, uintptr_t a5)
29920Sstevel@tonic-gate {
29930Sstevel@tonic-gate 	char *s = NULL;
29940Sstevel@tonic-gate 	uint_t dip_no_disp = 0;
29950Sstevel@tonic-gate 
29960Sstevel@tonic-gate 	if (func_id & DB_DONT_DISPLAY_DIP) {
29970Sstevel@tonic-gate 		dip_no_disp = 1;
29980Sstevel@tonic-gate 	}
29990Sstevel@tonic-gate 	if (db_debug_funcs & func_id) {
30000Sstevel@tonic-gate 		switch (func_id) {
30010Sstevel@tonic-gate 		case DB_INIT:		s = "_init";			break;
30020Sstevel@tonic-gate 		case DB_FINI:		s = "_fini";			break;
30030Sstevel@tonic-gate 		case DB_INFO:		s = "_info";			break;
30040Sstevel@tonic-gate 		case DB_GETINFO:	s = "getinfo";			break;
30050Sstevel@tonic-gate 		case DB_ATTACH:		s = "attach";			break;
30060Sstevel@tonic-gate 		case DB_DETACH:		s = "detach";			break;
30070Sstevel@tonic-gate 		case DB_CTLOPS:		s = "ctlops";			break;
30080Sstevel@tonic-gate 		case DB_INITCHILD:	s = "initchild";		break;
30090Sstevel@tonic-gate 		case DB_REMOVECHILD:	s = "removechild";		break;
30100Sstevel@tonic-gate 		case DB_INTR_OPS:	s = "intr_ops";			break;
30110Sstevel@tonic-gate 		case DB_PCI_MAP:	s = "map";			break;
30120Sstevel@tonic-gate 		case DB_SAVE_CONF_REGS:	s = "save_conf_regs";		break;
30130Sstevel@tonic-gate 		case DB_REST_CONF_REGS:	s = "restore_conf_regs";	break;
30140Sstevel@tonic-gate 		case DB_INTR:		s = "intr";			break;
30150Sstevel@tonic-gate 		case DB_OPEN:		s = "open";			break;
30160Sstevel@tonic-gate 		case DB_CLOSE:		s = "close";			break;
30170Sstevel@tonic-gate 		case DB_IOCTL:		s = "ioctl";			break;
30180Sstevel@tonic-gate 		case DB_DVMA:		s = "set_dvma_range";		break;
30190Sstevel@tonic-gate 
30200Sstevel@tonic-gate 		default:		s = "PCI debug unknown";	break;
30210Sstevel@tonic-gate 		}
30220Sstevel@tonic-gate 
30230Sstevel@tonic-gate 		if (s && !dip_no_disp) {
30240Sstevel@tonic-gate 			prom_printf("%s(%d): %s: ", ddi_driver_name(dip),
30250Sstevel@tonic-gate 			    ddi_get_instance(dip), s);
30260Sstevel@tonic-gate 		}
30270Sstevel@tonic-gate 		prom_printf(fmt, a1, a2, a3, a4, a5);
30280Sstevel@tonic-gate 	}
30290Sstevel@tonic-gate }
30300Sstevel@tonic-gate #endif
30310Sstevel@tonic-gate 
30320Sstevel@tonic-gate static int db_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op,
30330Sstevel@tonic-gate     int flags, char *name, caddr_t valuep, int *lengthp)
30340Sstevel@tonic-gate {
30350Sstevel@tonic-gate 	minor_t minor = getminor(dev);
30360Sstevel@tonic-gate 	int	instance = PCIHP_AP_MINOR_NUM_TO_INSTANCE(minor);
30370Sstevel@tonic-gate 
30380Sstevel@tonic-gate 	db_ctrl_t *dbp = (db_ctrl_t *)ddi_get_soft_state(db_state, instance);
30390Sstevel@tonic-gate 
30400Sstevel@tonic-gate 
30410Sstevel@tonic-gate 	if (dbp == NULL)
30420Sstevel@tonic-gate 		return (ENXIO);
30430Sstevel@tonic-gate 
30440Sstevel@tonic-gate 	if (dbp->dev_state & DB_SECONDARY_NEXUS)
30450Sstevel@tonic-gate 		return ((pcihp_get_cb_ops())->cb_prop_op(dev, dip,
30460Sstevel@tonic-gate 		    prop_op, flags, name, valuep, lengthp));
30470Sstevel@tonic-gate 
30480Sstevel@tonic-gate 	return (ddi_prop_op(dev, dip, prop_op, flags, name, valuep, lengthp));
30490Sstevel@tonic-gate }
30500Sstevel@tonic-gate 
30510Sstevel@tonic-gate /*
30520Sstevel@tonic-gate  * Initialize our FMA resources
30530Sstevel@tonic-gate  */
30540Sstevel@tonic-gate static void
30550Sstevel@tonic-gate db_fm_init(db_ctrl_t *db_p)
30560Sstevel@tonic-gate {
30570Sstevel@tonic-gate 	ddi_fm_error_t derr;
30580Sstevel@tonic-gate 
30590Sstevel@tonic-gate 	db_p->fm_cap = DDI_FM_EREPORT_CAPABLE | DDI_FM_ERRCB_CAPABLE |
30600Sstevel@tonic-gate 		DDI_FM_ACCCHK_CAPABLE | DDI_FM_DMACHK_CAPABLE;
30610Sstevel@tonic-gate 
30620Sstevel@tonic-gate 	/*
30630Sstevel@tonic-gate 	 * Request our capability level and get our parents capability
30640Sstevel@tonic-gate 	 * and ibc.
30650Sstevel@tonic-gate 	 */
30660Sstevel@tonic-gate 	ddi_fm_init(db_p->dip, &db_p->fm_cap, &db_p->fm_ibc);
30670Sstevel@tonic-gate 	ASSERT((db_p->fm_cap & DDI_FM_EREPORT_CAPABLE) &&
30680Sstevel@tonic-gate 	    (db_p->fm_cap & DDI_FM_ERRCB_CAPABLE));
30690Sstevel@tonic-gate 
30700Sstevel@tonic-gate 	pci_ereport_setup(db_p->dip);
30710Sstevel@tonic-gate 
30720Sstevel@tonic-gate 	/*
30730Sstevel@tonic-gate 	 * clear any outstanding error bits
30740Sstevel@tonic-gate 	 */
30750Sstevel@tonic-gate 	bzero(&derr, sizeof (ddi_fm_error_t));
30760Sstevel@tonic-gate 	derr.fme_version = DDI_FME_VERSION;
30770Sstevel@tonic-gate 	derr.fme_flag = DDI_FM_ERR_EXPECTED;
30780Sstevel@tonic-gate 	pci_ereport_post(db_p->dip, &derr, NULL);
30790Sstevel@tonic-gate 	pci_bdg_ereport_post(db_p->dip, &derr, NULL);
30800Sstevel@tonic-gate 
30810Sstevel@tonic-gate 	/*
30820Sstevel@tonic-gate 	 * Register error callback with our parent.
30830Sstevel@tonic-gate 	 */
30840Sstevel@tonic-gate 	ddi_fm_handler_register(db_p->dip, db_err_callback, NULL);
30850Sstevel@tonic-gate }
30860Sstevel@tonic-gate 
30870Sstevel@tonic-gate /*
30880Sstevel@tonic-gate  * Breakdown our FMA resources
30890Sstevel@tonic-gate  */
30900Sstevel@tonic-gate static void
30910Sstevel@tonic-gate db_fm_fini(db_ctrl_t *db_p)
30920Sstevel@tonic-gate {
30930Sstevel@tonic-gate 	/*
30940Sstevel@tonic-gate 	 * Clean up allocated fm structures
30950Sstevel@tonic-gate 	 */
30960Sstevel@tonic-gate 	ddi_fm_handler_unregister(db_p->dip);
30970Sstevel@tonic-gate 	pci_ereport_teardown(db_p->dip);
30980Sstevel@tonic-gate 	ddi_fm_fini(db_p->dip);
30990Sstevel@tonic-gate }
31000Sstevel@tonic-gate 
31010Sstevel@tonic-gate /*
31020Sstevel@tonic-gate  * Initialize FMA resources for children devices. Called when
31030Sstevel@tonic-gate  * child calls ddi_fm_init().
31040Sstevel@tonic-gate  */
31050Sstevel@tonic-gate /*ARGSUSED*/
31060Sstevel@tonic-gate static int
31070Sstevel@tonic-gate db_fm_init_child(dev_info_t *dip, dev_info_t *tdip, int cap,
31080Sstevel@tonic-gate 		ddi_iblock_cookie_t *ibc)
31090Sstevel@tonic-gate {
31100Sstevel@tonic-gate 	db_ctrl_t *db_p = (db_ctrl_t *)ddi_get_soft_state(db_state,
31110Sstevel@tonic-gate 			ddi_get_instance(dip));
31120Sstevel@tonic-gate 	*ibc = db_p->fm_ibc;
31130Sstevel@tonic-gate 	return (db_p->fm_cap);
31140Sstevel@tonic-gate }
31150Sstevel@tonic-gate 
31160Sstevel@tonic-gate /*
31170Sstevel@tonic-gate  * FMA registered error callback
31180Sstevel@tonic-gate  */
31190Sstevel@tonic-gate static int
31200Sstevel@tonic-gate db_err_callback(dev_info_t *dip, ddi_fm_error_t *derr, const void *impl_data)
31210Sstevel@tonic-gate {
31220Sstevel@tonic-gate 	uint16_t pci_cfg_stat, pci_cfg_sec_stat;
31230Sstevel@tonic-gate 
31240Sstevel@tonic-gate 	ASSERT(impl_data == NULL);
31250Sstevel@tonic-gate 	pci_ereport_post(dip, derr, &pci_cfg_stat);
31260Sstevel@tonic-gate 	pci_bdg_ereport_post(dip, derr, &pci_cfg_sec_stat);
31270Sstevel@tonic-gate 	return (pci_bdg_check_status(dip, derr, pci_cfg_stat,
31280Sstevel@tonic-gate 	    pci_cfg_sec_stat));
31290Sstevel@tonic-gate }
31300Sstevel@tonic-gate 
31310Sstevel@tonic-gate static void
31320Sstevel@tonic-gate db_bus_enter(dev_info_t *dip, ddi_acc_handle_t handle)
31330Sstevel@tonic-gate {
31340Sstevel@tonic-gate 	i_ndi_busop_access_enter(dip, handle);
31350Sstevel@tonic-gate }
31360Sstevel@tonic-gate 
31370Sstevel@tonic-gate /* ARGSUSED */
31380Sstevel@tonic-gate static void
31390Sstevel@tonic-gate db_bus_exit(dev_info_t *dip, ddi_acc_handle_t handle)
31400Sstevel@tonic-gate {
31410Sstevel@tonic-gate 	i_ndi_busop_access_exit(dip, handle);
31420Sstevel@tonic-gate }
3143