xref: /onnv-gate/usr/src/uts/sun4u/io/pci/pci_pci.c (revision 3270:aab02674dadc)
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
51624Spjha  * Common Development and Distribution License (the "License").
61624Spjha  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
221624Spjha  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate /*
290Sstevel@tonic-gate  *	Sun4u PCI to PCI bus bridge nexus driver
300Sstevel@tonic-gate  */
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #include <sys/conf.h>
330Sstevel@tonic-gate #include <sys/kmem.h>
340Sstevel@tonic-gate #include <sys/debug.h>
350Sstevel@tonic-gate #include <sys/modctl.h>
360Sstevel@tonic-gate #include <sys/autoconf.h>
370Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
380Sstevel@tonic-gate #include <sys/ddi_subrdefs.h>
39*3270Set142600 #include <sys/pcie.h>
40*3270Set142600 #include <sys/pcie_impl.h>
411624Spjha #include <sys/pci_cap.h>
420Sstevel@tonic-gate #include <sys/pci/pci_nexus.h>
430Sstevel@tonic-gate #include <sys/pci/pci_regs.h>
440Sstevel@tonic-gate #include <sys/ddi.h>
450Sstevel@tonic-gate #include <sys/sunndi.h>
460Sstevel@tonic-gate #include <sys/sunddi.h>
470Sstevel@tonic-gate #include <sys/fm/protocol.h>
480Sstevel@tonic-gate #include <sys/ddifm.h>
490Sstevel@tonic-gate #include <sys/pci/pci_pwr.h>
500Sstevel@tonic-gate #include <sys/pci/pci_debug.h>
510Sstevel@tonic-gate #include <sys/hotplug/pci/pcihp.h>
520Sstevel@tonic-gate #include <sys/open.h>
530Sstevel@tonic-gate #include <sys/stat.h>
540Sstevel@tonic-gate #include <sys/file.h>
550Sstevel@tonic-gate 
560Sstevel@tonic-gate #define	NUM_LOGICAL_SLOTS	32
570Sstevel@tonic-gate 
580Sstevel@tonic-gate #define	PPB_RANGE_LEN 2
590Sstevel@tonic-gate 
600Sstevel@tonic-gate #define	PPB_32BIT_IO 1
610Sstevel@tonic-gate #define	PPB_32bit_MEM 1
620Sstevel@tonic-gate 
630Sstevel@tonic-gate #define	PPB_MEMGRAIN 0x100000
640Sstevel@tonic-gate #define	PPB_IOGRAIN 0x1000
650Sstevel@tonic-gate 
660Sstevel@tonic-gate #define	PPB_16bit_IOADDR(addr) ((uint16_t)(((uint8_t)(addr) & 0xF0) << 8))
670Sstevel@tonic-gate #define	PPB_LADDR(lo, hi) (((uint16_t)(hi) << 16) | (uint16_t)(lo))
680Sstevel@tonic-gate #define	PPB_32bit_MEMADDR(addr) (PPB_LADDR(0, ((uint16_t)(addr) & 0xFFF0)))
690Sstevel@tonic-gate 
700Sstevel@tonic-gate typedef struct	slot_table {
710Sstevel@tonic-gate 	uchar_t		bus_id[128];
720Sstevel@tonic-gate 	uchar_t		slot_name[32];
730Sstevel@tonic-gate 	uint8_t		device_no;
740Sstevel@tonic-gate 	uint8_t		phys_slot_num;
750Sstevel@tonic-gate } slot_table_t;
760Sstevel@tonic-gate 
770Sstevel@tonic-gate /*
780Sstevel@tonic-gate  * The variable controls the default setting of the command register
790Sstevel@tonic-gate  * for pci devices.  See ppb_initchild() for details.
800Sstevel@tonic-gate  */
810Sstevel@tonic-gate static ushort_t ppb_command_default = PCI_COMM_SERR_ENABLE |
820Sstevel@tonic-gate 					PCI_COMM_WAIT_CYC_ENAB |
830Sstevel@tonic-gate 					PCI_COMM_PARITY_DETECT |
840Sstevel@tonic-gate 					PCI_COMM_ME |
850Sstevel@tonic-gate 					PCI_COMM_MAE |
860Sstevel@tonic-gate 					PCI_COMM_IO;
870Sstevel@tonic-gate 
880Sstevel@tonic-gate static int ppb_bus_map(dev_info_t *, dev_info_t *, ddi_map_req_t *,
890Sstevel@tonic-gate 	off_t, off_t, caddr_t *);
900Sstevel@tonic-gate static int ppb_ctlops(dev_info_t *, dev_info_t *, ddi_ctl_enum_t,
910Sstevel@tonic-gate 	void *, void *);
920Sstevel@tonic-gate static int ppb_intr_ops(dev_info_t *dip, dev_info_t *rdip,
930Sstevel@tonic-gate 	ddi_intr_op_t intr_op, ddi_intr_handle_impl_t *hdlp, void *result);
940Sstevel@tonic-gate 
950Sstevel@tonic-gate /*
960Sstevel@tonic-gate  * fm_init busop to initialize our children
970Sstevel@tonic-gate  */
980Sstevel@tonic-gate static int ppb_fm_init_child(dev_info_t *dip, dev_info_t *tdip, int cap,
990Sstevel@tonic-gate 		ddi_iblock_cookie_t *ibc);
1000Sstevel@tonic-gate static void ppb_bus_enter(dev_info_t *dip, ddi_acc_handle_t handle);
1010Sstevel@tonic-gate static void ppb_bus_exit(dev_info_t *dip, ddi_acc_handle_t handle);
1020Sstevel@tonic-gate static int ppb_bus_power(dev_info_t *dip, void *impl_arg, pm_bus_power_op_t op,
1030Sstevel@tonic-gate     void *arg, void *result);
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate struct bus_ops ppb_bus_ops = {
1060Sstevel@tonic-gate 	BUSO_REV,
1070Sstevel@tonic-gate 	ppb_bus_map,
1080Sstevel@tonic-gate 	0,
1090Sstevel@tonic-gate 	0,
1100Sstevel@tonic-gate 	0,
1110Sstevel@tonic-gate 	i_ddi_map_fault,
1120Sstevel@tonic-gate 	ddi_dma_map,
1130Sstevel@tonic-gate 	ddi_dma_allochdl,
1140Sstevel@tonic-gate 	ddi_dma_freehdl,
1150Sstevel@tonic-gate 	ddi_dma_bindhdl,
1160Sstevel@tonic-gate 	ddi_dma_unbindhdl,
1170Sstevel@tonic-gate 	ddi_dma_flush,
1180Sstevel@tonic-gate 	ddi_dma_win,
1190Sstevel@tonic-gate 	ddi_dma_mctl,
1200Sstevel@tonic-gate 	ppb_ctlops,
1210Sstevel@tonic-gate 	ddi_bus_prop_op,
1220Sstevel@tonic-gate 	ndi_busop_get_eventcookie,	/* (*bus_get_eventcookie)();    */
1230Sstevel@tonic-gate 	ndi_busop_add_eventcall,	/* (*bus_add_eventcall)();	*/
1240Sstevel@tonic-gate 	ndi_busop_remove_eventcall,	/* (*bus_remove_eventcall)();   */
1250Sstevel@tonic-gate 	ndi_post_event,			/* (*bus_post_event)();		*/
1260Sstevel@tonic-gate 	0,				/* (*bus_intr_ctl)();		*/
1270Sstevel@tonic-gate 	0,				/* (*bus_config)(); 		*/
1280Sstevel@tonic-gate 	0,				/* (*bus_unconfig)(); 		*/
1290Sstevel@tonic-gate 	ppb_fm_init_child,		/* (*bus_fm_init)(); 		*/
1300Sstevel@tonic-gate 	NULL,				/* (*bus_fm_fini)(); 		*/
1310Sstevel@tonic-gate 	ppb_bus_enter,			/* (*bus_enter)()		*/
1320Sstevel@tonic-gate 	ppb_bus_exit,			/* (*bus_exit)()		*/
1330Sstevel@tonic-gate 	ppb_bus_power,			/* (*bus_power)()		*/
1340Sstevel@tonic-gate 	ppb_intr_ops			/* (*bus_intr_op)(); 		*/
1350Sstevel@tonic-gate };
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate static int ppb_open(dev_t *devp, int flags, int otyp, cred_t *credp);
1380Sstevel@tonic-gate static int ppb_close(dev_t dev, int flags, int otyp, cred_t *credp);
1390Sstevel@tonic-gate static int ppb_ioctl(dev_t dev, int cmd, intptr_t arg, int mode,
1400Sstevel@tonic-gate 						cred_t *credp, int *rvalp);
1410Sstevel@tonic-gate static int ppb_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op,
1420Sstevel@tonic-gate     int flags, char *name, caddr_t valuep, int *lengthp);
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate static struct cb_ops ppb_cb_ops = {
1450Sstevel@tonic-gate 	ppb_open,			/* open */
1460Sstevel@tonic-gate 	ppb_close,			/* close */
1470Sstevel@tonic-gate 	nulldev,			/* strategy */
1480Sstevel@tonic-gate 	nulldev,			/* print */
1490Sstevel@tonic-gate 	nulldev,			/* dump */
1500Sstevel@tonic-gate 	nulldev,			/* read */
1510Sstevel@tonic-gate 	nulldev,			/* write */
1520Sstevel@tonic-gate 	ppb_ioctl,			/* ioctl */
1530Sstevel@tonic-gate 	nodev,				/* devmap */
1540Sstevel@tonic-gate 	nodev,				/* mmap */
1550Sstevel@tonic-gate 	nodev,				/* segmap */
1560Sstevel@tonic-gate 	nochpoll,			/* poll */
1570Sstevel@tonic-gate 	ppb_prop_op,			/* cb_prop_op */
1580Sstevel@tonic-gate 	NULL,				/* streamtab */
1590Sstevel@tonic-gate 	D_NEW | D_MP | D_HOTPLUG,	/* Driver compatibility flag */
1600Sstevel@tonic-gate 	CB_REV,				/* rev */
1610Sstevel@tonic-gate 	nodev,				/* int (*cb_aread)() */
1620Sstevel@tonic-gate 	nodev				/* int (*cb_awrite)() */
1630Sstevel@tonic-gate };
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate static int ppb_probe(dev_info_t *);
1660Sstevel@tonic-gate static int ppb_attach(dev_info_t *devi, ddi_attach_cmd_t cmd);
1670Sstevel@tonic-gate static int ppb_detach(dev_info_t *devi, ddi_detach_cmd_t cmd);
1680Sstevel@tonic-gate static int ppb_info(dev_info_t *dip, ddi_info_cmd_t infocmd,
1690Sstevel@tonic-gate     void *arg, void **result);
1700Sstevel@tonic-gate static int ppb_pwr(dev_info_t *dip, int component, int level);
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate struct dev_ops ppb_ops = {
1730Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev */
1740Sstevel@tonic-gate 	0,			/* refcnt  */
1750Sstevel@tonic-gate 	ppb_info,		/* info */
1760Sstevel@tonic-gate 	nulldev,		/* identify */
1770Sstevel@tonic-gate 	ppb_probe,		/* probe */
1780Sstevel@tonic-gate 	ppb_attach,		/* attach */
1790Sstevel@tonic-gate 	ppb_detach,		/* detach */
1800Sstevel@tonic-gate 	nulldev,		/* reset */
1810Sstevel@tonic-gate 	&ppb_cb_ops,		/* driver operations */
1820Sstevel@tonic-gate 	&ppb_bus_ops,		/* bus operations */
1830Sstevel@tonic-gate 	ppb_pwr
1840Sstevel@tonic-gate };
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate /*
1870Sstevel@tonic-gate  * Module linkage information for the kernel.
1880Sstevel@tonic-gate  */
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate static struct modldrv modldrv = {
1910Sstevel@tonic-gate 	&mod_driverops, /* Type of module */
192506Scth 	"Standard PCI to PCI bridge nexus driver %I%",
1930Sstevel@tonic-gate 	&ppb_ops,	/* driver ops */
1940Sstevel@tonic-gate };
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate static struct modlinkage modlinkage = {
1970Sstevel@tonic-gate 	MODREV_1,
1980Sstevel@tonic-gate 	(void *)&modldrv,
1990Sstevel@tonic-gate 	NULL
2000Sstevel@tonic-gate };
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate /*
2030Sstevel@tonic-gate  * soft state pointer and structure template:
2040Sstevel@tonic-gate  */
2050Sstevel@tonic-gate static void *ppb_state;
2060Sstevel@tonic-gate 
207946Smathue struct ppb_cfg_state {
2080Sstevel@tonic-gate 	dev_info_t *dip;
2090Sstevel@tonic-gate 	ushort_t command;
2100Sstevel@tonic-gate 	uchar_t cache_line_size;
2110Sstevel@tonic-gate 	uchar_t latency_timer;
2120Sstevel@tonic-gate 	uchar_t header_type;
2130Sstevel@tonic-gate 	uchar_t sec_latency_timer;
2140Sstevel@tonic-gate 	ushort_t bridge_control;
2150Sstevel@tonic-gate };
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate typedef struct {
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 	dev_info_t *dip;
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate 	/*
2220Sstevel@tonic-gate 	 * configuration register state for the bus:
2230Sstevel@tonic-gate 	 */
2240Sstevel@tonic-gate 	uchar_t ppb_cache_line_size;
2250Sstevel@tonic-gate 	uchar_t ppb_latency_timer;
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate 	/*
2280Sstevel@tonic-gate 	 * PM support
2290Sstevel@tonic-gate 	 */
2300Sstevel@tonic-gate 	ddi_acc_handle_t	ppb_conf_hdl;
2311624Spjha 	uint16_t		ppb_pm_cap_ptr;
2320Sstevel@tonic-gate 	pci_pwr_t		*ppb_pwr_p;
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate 	/*
2350Sstevel@tonic-gate 	 * HP support
2360Sstevel@tonic-gate 	 */
2370Sstevel@tonic-gate 	boolean_t		hotplug_capable;
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 	kmutex_t ppb_mutex;
2400Sstevel@tonic-gate 	uint_t ppb_soft_state;
2410Sstevel@tonic-gate #define	PPB_SOFT_STATE_CLOSED		0x00
2420Sstevel@tonic-gate #define	PPB_SOFT_STATE_OPEN		0x01
2430Sstevel@tonic-gate #define	PPB_SOFT_STATE_OPEN_EXCL	0x02
2440Sstevel@tonic-gate 	int fm_cap;
2450Sstevel@tonic-gate 	ddi_iblock_cookie_t fm_ibc;
246*3270Set142600 
247*3270Set142600 	uint8_t parent_bus;
2480Sstevel@tonic-gate } ppb_devstate_t;
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate /*
2510Sstevel@tonic-gate  * The following variable enables a workaround for the following obp bug:
2520Sstevel@tonic-gate  *
2530Sstevel@tonic-gate  *	1234181 - obp should set latency timer registers in pci
2540Sstevel@tonic-gate  *		configuration header
2550Sstevel@tonic-gate  *
2560Sstevel@tonic-gate  * Until this bug gets fixed in the obp, the following workaround should
2570Sstevel@tonic-gate  * be enabled.
2580Sstevel@tonic-gate  */
2590Sstevel@tonic-gate static uint_t ppb_set_latency_timer_register = 1;
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate /*
2620Sstevel@tonic-gate  * The following variable enables a workaround for an obp bug to be
2630Sstevel@tonic-gate  * submitted.  A bug requesting a workaround fof this problem has
2640Sstevel@tonic-gate  * been filed:
2650Sstevel@tonic-gate  *
2660Sstevel@tonic-gate  *	1235094 - need workarounds on positron nexus drivers to set cache
2670Sstevel@tonic-gate  *		line size registers
2680Sstevel@tonic-gate  *
2690Sstevel@tonic-gate  * Until this bug gets fixed in the obp, the following workaround should
2700Sstevel@tonic-gate  * be enabled.
2710Sstevel@tonic-gate  */
2720Sstevel@tonic-gate static uint_t ppb_set_cache_line_size_register = 1;
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate /*
2750Sstevel@tonic-gate  * forward function declarations:
2760Sstevel@tonic-gate  */
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate /*
2790Sstevel@tonic-gate  * FMA error callback
2800Sstevel@tonic-gate  * Register error handling callback with our parent. We will just call
2810Sstevel@tonic-gate  * our children's error callbacks and return their status.
2820Sstevel@tonic-gate  */
2830Sstevel@tonic-gate static int ppb_err_callback(dev_info_t *dip, ddi_fm_error_t *derr,
2840Sstevel@tonic-gate 		const void *impl_data);
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate /*
2870Sstevel@tonic-gate  * init/fini routines to alloc/dealloc fm structures and
2880Sstevel@tonic-gate  * register/unregister our callback.
2890Sstevel@tonic-gate  */
2900Sstevel@tonic-gate static void ppb_fm_init(ppb_devstate_t *ppb_p);
2910Sstevel@tonic-gate static void ppb_fm_fini(ppb_devstate_t *ppb_p);
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate static void ppb_removechild(dev_info_t *);
2940Sstevel@tonic-gate static int ppb_initchild(dev_info_t *child);
295*3270Set142600 static void ppb_uninitchild(dev_info_t *child);
2960Sstevel@tonic-gate static dev_info_t *get_my_childs_dip(dev_info_t *dip, dev_info_t *rdip);
2970Sstevel@tonic-gate static void ppb_pwr_setup(ppb_devstate_t *ppb, dev_info_t *dip);
2980Sstevel@tonic-gate static void ppb_pwr_teardown(ppb_devstate_t *ppb, dev_info_t *dip);
2990Sstevel@tonic-gate static void ppb_init_hotplug(ppb_devstate_t *ppb);
3000Sstevel@tonic-gate static void ppb_create_ranges_prop(dev_info_t *, ddi_acc_handle_t);
301964Smathue uint64_t pci_debug_flags = 0;
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate int
3040Sstevel@tonic-gate _init(void)
3050Sstevel@tonic-gate {
3060Sstevel@tonic-gate 	int e;
3070Sstevel@tonic-gate 	if ((e = ddi_soft_state_init(&ppb_state, sizeof (ppb_devstate_t),
3080Sstevel@tonic-gate 	    1)) == 0 && (e = mod_install(&modlinkage)) != 0)
3090Sstevel@tonic-gate 		ddi_soft_state_fini(&ppb_state);
3100Sstevel@tonic-gate 	return (e);
3110Sstevel@tonic-gate }
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate int
3140Sstevel@tonic-gate _fini(void)
3150Sstevel@tonic-gate {
3160Sstevel@tonic-gate 	int e;
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate 	if ((e = mod_remove(&modlinkage)) == 0)
3190Sstevel@tonic-gate 		ddi_soft_state_fini(&ppb_state);
3200Sstevel@tonic-gate 	return (e);
3210Sstevel@tonic-gate }
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate int
3240Sstevel@tonic-gate _info(struct modinfo *modinfop)
3250Sstevel@tonic-gate {
3260Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
3270Sstevel@tonic-gate }
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate /*ARGSUSED*/
3300Sstevel@tonic-gate static int
3310Sstevel@tonic-gate ppb_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
3320Sstevel@tonic-gate {
3330Sstevel@tonic-gate 	ppb_devstate_t *ppb_p;	/* per ppb state pointer */
3340Sstevel@tonic-gate 	minor_t		minor = getminor((dev_t)arg);
3350Sstevel@tonic-gate 	int		instance = PCIHP_AP_MINOR_NUM_TO_INSTANCE(minor);
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate 	ppb_p = (ppb_devstate_t *)ddi_get_soft_state(ppb_state,
3380Sstevel@tonic-gate 	    instance);
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate 	switch (infocmd) {
3410Sstevel@tonic-gate 	default:
3420Sstevel@tonic-gate 		return (DDI_FAILURE);
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
345946Smathue 		*result = (void *)(uintptr_t)instance;
3460Sstevel@tonic-gate 		return (DDI_SUCCESS);
3470Sstevel@tonic-gate 
3480Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
3490Sstevel@tonic-gate 		if (ppb_p == NULL)
3500Sstevel@tonic-gate 			return (DDI_FAILURE);
3510Sstevel@tonic-gate 		*result = (void *)ppb_p->dip;
3520Sstevel@tonic-gate 		return (DDI_SUCCESS);
3530Sstevel@tonic-gate 	}
3540Sstevel@tonic-gate }
3550Sstevel@tonic-gate 
3560Sstevel@tonic-gate /*ARGSUSED*/
3570Sstevel@tonic-gate static int
3580Sstevel@tonic-gate ppb_probe(register dev_info_t *devi)
3590Sstevel@tonic-gate {
3600Sstevel@tonic-gate 	return (DDI_PROBE_SUCCESS);
3610Sstevel@tonic-gate }
3620Sstevel@tonic-gate 
3630Sstevel@tonic-gate /*ARGSUSED*/
3640Sstevel@tonic-gate static int
3650Sstevel@tonic-gate ppb_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
3660Sstevel@tonic-gate {
3670Sstevel@tonic-gate 	int instance;
3680Sstevel@tonic-gate 	ppb_devstate_t *ppb;
3690Sstevel@tonic-gate 	ddi_acc_handle_t config_handle;
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate 	switch (cmd) {
3720Sstevel@tonic-gate 	case DDI_ATTACH:
3730Sstevel@tonic-gate 
3740Sstevel@tonic-gate 		/*
3750Sstevel@tonic-gate 		 * Make sure the "device_type" property exists.
3760Sstevel@tonic-gate 		 */
3770Sstevel@tonic-gate 		(void) ddi_prop_update_string(DDI_DEV_T_NONE, devi,
3780Sstevel@tonic-gate 		    "device_type", "pci");
3790Sstevel@tonic-gate 
3800Sstevel@tonic-gate 		/*
3810Sstevel@tonic-gate 		 * Allocate and get soft state structure.
3820Sstevel@tonic-gate 		 */
3830Sstevel@tonic-gate 		instance = ddi_get_instance(devi);
3840Sstevel@tonic-gate 		if (ddi_soft_state_zalloc(ppb_state, instance) != DDI_SUCCESS)
3850Sstevel@tonic-gate 			return (DDI_FAILURE);
3860Sstevel@tonic-gate 		ppb = (ppb_devstate_t *)ddi_get_soft_state(ppb_state, instance);
3870Sstevel@tonic-gate 		ppb->dip = devi;
3880Sstevel@tonic-gate 		mutex_init(&ppb->ppb_mutex, NULL, MUTEX_DRIVER, NULL);
3890Sstevel@tonic-gate 		ppb->ppb_soft_state = PPB_SOFT_STATE_CLOSED;
3900Sstevel@tonic-gate 		if (pci_config_setup(devi, &config_handle) != DDI_SUCCESS) {
3910Sstevel@tonic-gate 			mutex_destroy(&ppb->ppb_mutex);
3920Sstevel@tonic-gate 			ddi_soft_state_free(ppb_state, instance);
3930Sstevel@tonic-gate 			return (DDI_FAILURE);
3940Sstevel@tonic-gate 		}
3950Sstevel@tonic-gate 		ppb_pwr_setup(ppb, devi);
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate 		if (PM_CAPABLE(ppb->ppb_pwr_p)) {
3980Sstevel@tonic-gate 			mutex_enter(&ppb->ppb_pwr_p->pwr_mutex);
3990Sstevel@tonic-gate 
4000Sstevel@tonic-gate 			/*
4010Sstevel@tonic-gate 			 * Before reading config registers, make sure power is
4020Sstevel@tonic-gate 			 * on, and remains on.
4030Sstevel@tonic-gate 			 */
4040Sstevel@tonic-gate 			ppb->ppb_pwr_p->pwr_fp++;
4050Sstevel@tonic-gate 
4060Sstevel@tonic-gate 			pci_pwr_change(ppb->ppb_pwr_p,
4070Sstevel@tonic-gate 			    ppb->ppb_pwr_p->current_lvl,
4080Sstevel@tonic-gate 			    pci_pwr_new_lvl(ppb->ppb_pwr_p));
4090Sstevel@tonic-gate 		}
4100Sstevel@tonic-gate 
4110Sstevel@tonic-gate 		ppb->ppb_cache_line_size =
4120Sstevel@tonic-gate 		    pci_config_get8(config_handle, PCI_CONF_CACHE_LINESZ);
4130Sstevel@tonic-gate 		ppb->ppb_latency_timer =
4140Sstevel@tonic-gate 		    pci_config_get8(config_handle, PCI_CONF_LATENCY_TIMER);
4150Sstevel@tonic-gate 
4160Sstevel@tonic-gate 		/*
4170Sstevel@tonic-gate 		 * Check whether the "ranges" property is present.
4180Sstevel@tonic-gate 		 * Otherwise create the ranges property by reading
4190Sstevel@tonic-gate 		 * the configuration registers
4200Sstevel@tonic-gate 		 */
4210Sstevel@tonic-gate 		if (ddi_prop_exists(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
4220Sstevel@tonic-gate 		    "ranges") == 0) {
4230Sstevel@tonic-gate 			ppb_create_ranges_prop(devi, config_handle);
4240Sstevel@tonic-gate 		}
4250Sstevel@tonic-gate 
4260Sstevel@tonic-gate 		pci_config_teardown(&config_handle);
4270Sstevel@tonic-gate 
4280Sstevel@tonic-gate 		if (PM_CAPABLE(ppb->ppb_pwr_p)) {
4290Sstevel@tonic-gate 			ppb->ppb_pwr_p->pwr_fp--;
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate 			pci_pwr_change(ppb->ppb_pwr_p,
4320Sstevel@tonic-gate 			    ppb->ppb_pwr_p->current_lvl,
4330Sstevel@tonic-gate 			    pci_pwr_new_lvl(ppb->ppb_pwr_p));
4340Sstevel@tonic-gate 
4350Sstevel@tonic-gate 			mutex_exit(&ppb->ppb_pwr_p->pwr_mutex);
4360Sstevel@tonic-gate 		}
4370Sstevel@tonic-gate 
4380Sstevel@tonic-gate 		/*
4390Sstevel@tonic-gate 		 * Initialize hotplug support on this bus. At minimum
4400Sstevel@tonic-gate 		 * (for non hotplug bus) this would create ":devctl" minor
4410Sstevel@tonic-gate 		 * node to support DEVCTL_DEVICE_* and DEVCTL_BUS_* ioctls
4420Sstevel@tonic-gate 		 * to this bus. This all takes place if this nexus has hot-plug
4430Sstevel@tonic-gate 		 * slots and successfully initializes Hot Plug Framework.
4440Sstevel@tonic-gate 		 */
4450Sstevel@tonic-gate 		ppb->hotplug_capable = B_FALSE;
4460Sstevel@tonic-gate 		ppb_init_hotplug(ppb);
4470Sstevel@tonic-gate 		if (ppb->hotplug_capable == B_FALSE) {
4480Sstevel@tonic-gate 			/*
4490Sstevel@tonic-gate 			 * create minor node for devctl interfaces
4500Sstevel@tonic-gate 			 */
4510Sstevel@tonic-gate 			if (ddi_create_minor_node(devi, "devctl", S_IFCHR,
4520Sstevel@tonic-gate 			    PCIHP_AP_MINOR_NUM(instance, PCIHP_DEVCTL_MINOR),
4530Sstevel@tonic-gate 			    DDI_NT_NEXUS, 0) != DDI_SUCCESS) {
4540Sstevel@tonic-gate 				if (ppb->ppb_pwr_p != NULL) {
4550Sstevel@tonic-gate 					ppb_pwr_teardown(ppb, devi);
4560Sstevel@tonic-gate 				}
4570Sstevel@tonic-gate 				mutex_destroy(&ppb->ppb_mutex);
4580Sstevel@tonic-gate 				ddi_soft_state_free(ppb_state, instance);
4590Sstevel@tonic-gate 				return (DDI_FAILURE);
4600Sstevel@tonic-gate 			}
4610Sstevel@tonic-gate 		}
4620Sstevel@tonic-gate 
4630Sstevel@tonic-gate 		DEBUG1(DBG_ATTACH, devi,
4640Sstevel@tonic-gate 			"ppb_attach(): this nexus %s hotplug slots\n",
4650Sstevel@tonic-gate 			ppb->hotplug_capable == B_TRUE ? "has":"has no");
4660Sstevel@tonic-gate 
4670Sstevel@tonic-gate 		ppb_fm_init(ppb);
4680Sstevel@tonic-gate 		ddi_report_dev(devi);
4690Sstevel@tonic-gate 
4700Sstevel@tonic-gate 		return (DDI_SUCCESS);
4710Sstevel@tonic-gate 
4720Sstevel@tonic-gate 	case DDI_RESUME:
4730Sstevel@tonic-gate 		/*
4740Sstevel@tonic-gate 		 * Get the soft state structure for the bridge.
4750Sstevel@tonic-gate 		 */
4760Sstevel@tonic-gate 		ppb = (ppb_devstate_t *)
4770Sstevel@tonic-gate 			ddi_get_soft_state(ppb_state, ddi_get_instance(devi));
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate 		pci_pwr_resume(devi, ppb->ppb_pwr_p);
4800Sstevel@tonic-gate 
4810Sstevel@tonic-gate 		return (DDI_SUCCESS);
4820Sstevel@tonic-gate 	}
4830Sstevel@tonic-gate 	return (DDI_FAILURE);
4840Sstevel@tonic-gate }
4850Sstevel@tonic-gate 
4860Sstevel@tonic-gate /*ARGSUSED*/
4870Sstevel@tonic-gate static int
4880Sstevel@tonic-gate ppb_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
4890Sstevel@tonic-gate {
4900Sstevel@tonic-gate 	ppb_devstate_t *ppb;
4910Sstevel@tonic-gate 
4920Sstevel@tonic-gate 	switch (cmd) {
4930Sstevel@tonic-gate 	case DDI_DETACH:
4940Sstevel@tonic-gate 		/*
4950Sstevel@tonic-gate 		 * And finally free the per-pci soft state after
4960Sstevel@tonic-gate 		 * uninitializing hotplug support for this bus.
4970Sstevel@tonic-gate 		 */
4980Sstevel@tonic-gate 		ppb = (ppb_devstate_t *)
4990Sstevel@tonic-gate 		    ddi_get_soft_state(ppb_state, ddi_get_instance(devi));
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate 		ppb_fm_fini(ppb);
5020Sstevel@tonic-gate 
5030Sstevel@tonic-gate 		if (ppb->hotplug_capable == B_TRUE)
5040Sstevel@tonic-gate 			if (pcihp_uninit(devi) == DDI_FAILURE)
5050Sstevel@tonic-gate 				return (DDI_FAILURE);
5060Sstevel@tonic-gate 		else
5070Sstevel@tonic-gate 			ddi_remove_minor_node(devi, "devctl");
5080Sstevel@tonic-gate 
5090Sstevel@tonic-gate 		(void) ddi_prop_remove(DDI_DEV_T_NONE, devi, "device_type");
5100Sstevel@tonic-gate 
5110Sstevel@tonic-gate 		if (ppb->ppb_pwr_p != NULL) {
5120Sstevel@tonic-gate 			ppb_pwr_teardown(ppb, devi);
5130Sstevel@tonic-gate 		}
5140Sstevel@tonic-gate 		mutex_destroy(&ppb->ppb_mutex);
5150Sstevel@tonic-gate 		ddi_soft_state_free(ppb_state, ddi_get_instance(devi));
5160Sstevel@tonic-gate 
5170Sstevel@tonic-gate 		return (DDI_SUCCESS);
5180Sstevel@tonic-gate 
5190Sstevel@tonic-gate 	case DDI_SUSPEND:
5200Sstevel@tonic-gate 		ppb = (ppb_devstate_t *)
5210Sstevel@tonic-gate 			ddi_get_soft_state(ppb_state, ddi_get_instance(devi));
5220Sstevel@tonic-gate 
5230Sstevel@tonic-gate 		pci_pwr_suspend(devi, ppb->ppb_pwr_p);
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate 		return (DDI_SUCCESS);
5260Sstevel@tonic-gate 	}
5270Sstevel@tonic-gate 	return (DDI_FAILURE);
5280Sstevel@tonic-gate }
5290Sstevel@tonic-gate 
5300Sstevel@tonic-gate /*ARGSUSED*/
5310Sstevel@tonic-gate static int
5320Sstevel@tonic-gate ppb_bus_map(dev_info_t *dip, dev_info_t *rdip, ddi_map_req_t *mp,
5330Sstevel@tonic-gate 	off_t offset, off_t len, caddr_t *vaddrp)
5340Sstevel@tonic-gate {
5350Sstevel@tonic-gate 	register dev_info_t *pdip;
5360Sstevel@tonic-gate 
5370Sstevel@tonic-gate 	pdip = (dev_info_t *)DEVI(dip)->devi_parent;
5380Sstevel@tonic-gate 	return ((DEVI(pdip)->devi_ops->devo_bus_ops->bus_map)
5390Sstevel@tonic-gate 	    (pdip, rdip, mp, offset, len, vaddrp));
5400Sstevel@tonic-gate }
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate /*ARGSUSED*/
5430Sstevel@tonic-gate static int
5440Sstevel@tonic-gate ppb_ctlops(dev_info_t *dip, dev_info_t *rdip,
5450Sstevel@tonic-gate 	ddi_ctl_enum_t ctlop, void *arg, void *result)
5460Sstevel@tonic-gate {
5470Sstevel@tonic-gate 	pci_regspec_t *drv_regp;
5480Sstevel@tonic-gate 	int	reglen;
5490Sstevel@tonic-gate 	int	rn;
550*3270Set142600 	struct	attachspec *as;
551*3270Set142600 	struct	detachspec *ds;
552*3270Set142600 	int	totreg;
553*3270Set142600 	ppb_devstate_t *ppb_p;
5540Sstevel@tonic-gate 
555*3270Set142600 	ppb_p = (ppb_devstate_t *)ddi_get_soft_state(ppb_state,
556*3270Set142600 	    ddi_get_instance(dip));
5570Sstevel@tonic-gate 
5580Sstevel@tonic-gate 	switch (ctlop) {
5590Sstevel@tonic-gate 	case DDI_CTLOPS_REPORTDEV:
5600Sstevel@tonic-gate 		if (rdip == (dev_info_t *)0)
5610Sstevel@tonic-gate 			return (DDI_FAILURE);
5620Sstevel@tonic-gate 		cmn_err(CE_CONT, "?PCI-device: %s@%s, %s%d\n",
5630Sstevel@tonic-gate 		    ddi_node_name(rdip), ddi_get_name_addr(rdip),
5640Sstevel@tonic-gate 		    ddi_driver_name(rdip),
5650Sstevel@tonic-gate 		    ddi_get_instance(rdip));
5660Sstevel@tonic-gate 		return (DDI_SUCCESS);
5670Sstevel@tonic-gate 
5680Sstevel@tonic-gate 	case DDI_CTLOPS_INITCHILD:
5690Sstevel@tonic-gate 		return (ppb_initchild((dev_info_t *)arg));
5700Sstevel@tonic-gate 
5710Sstevel@tonic-gate 	case DDI_CTLOPS_UNINITCHILD:
572*3270Set142600 		ppb_uninitchild((dev_info_t *)arg);
573*3270Set142600 		return (DDI_SUCCESS);
574*3270Set142600 
575*3270Set142600 	case DDI_CTLOPS_ATTACH:
576*3270Set142600 		if (!pcie_is_child(dip, rdip))
577*3270Set142600 			return (DDI_SUCCESS);
578*3270Set142600 
579*3270Set142600 		as = (struct attachspec *)arg;
580*3270Set142600 		if ((ppb_p->parent_bus == PCIE_PCIECAP_DEV_TYPE_PCIE_DEV) &&
581*3270Set142600 		    (as->when == DDI_POST))
582*3270Set142600 			pf_init(rdip, ppb_p->fm_ibc);
583*3270Set142600 
584*3270Set142600 		return (DDI_SUCCESS);
585*3270Set142600 
586*3270Set142600 	case DDI_CTLOPS_DETACH:
587*3270Set142600 		if (!pcie_is_child(dip, rdip))
588*3270Set142600 			return (DDI_SUCCESS);
589*3270Set142600 
590*3270Set142600 		ds = (struct detachspec *)arg;
591*3270Set142600 		if ((ppb_p->parent_bus == PCIE_PCIECAP_DEV_TYPE_PCIE_DEV) &&
592*3270Set142600 		    (ds->when == DDI_PRE))
593*3270Set142600 			pf_fini(rdip);
594*3270Set142600 
5950Sstevel@tonic-gate 		return (DDI_SUCCESS);
5960Sstevel@tonic-gate 
5970Sstevel@tonic-gate 	case DDI_CTLOPS_SIDDEV:
5980Sstevel@tonic-gate 		return (DDI_SUCCESS);
5990Sstevel@tonic-gate 
6000Sstevel@tonic-gate 	case DDI_CTLOPS_REGSIZE:
6010Sstevel@tonic-gate 	case DDI_CTLOPS_NREGS:
6020Sstevel@tonic-gate 		if (rdip == (dev_info_t *)0)
6030Sstevel@tonic-gate 			return (DDI_FAILURE);
6040Sstevel@tonic-gate 		break;
6050Sstevel@tonic-gate 	default:
6060Sstevel@tonic-gate 		return (ddi_ctlops(dip, rdip, ctlop, arg, result));
6070Sstevel@tonic-gate 	}
6080Sstevel@tonic-gate 
6090Sstevel@tonic-gate 	*(int *)result = 0;
610506Scth 	if (ddi_getlongprop(DDI_DEV_T_ANY, rdip,
6110Sstevel@tonic-gate 		DDI_PROP_DONTPASS | DDI_PROP_CANSLEEP, "reg",
6120Sstevel@tonic-gate 		(caddr_t)&drv_regp, &reglen) != DDI_SUCCESS)
6130Sstevel@tonic-gate 		return (DDI_FAILURE);
6140Sstevel@tonic-gate 
6150Sstevel@tonic-gate 	totreg = reglen / sizeof (pci_regspec_t);
6160Sstevel@tonic-gate 	if (ctlop == DDI_CTLOPS_NREGS)
6170Sstevel@tonic-gate 		*(int *)result = totreg;
6180Sstevel@tonic-gate 	else if (ctlop == DDI_CTLOPS_REGSIZE) {
6190Sstevel@tonic-gate 		rn = *(int *)arg;
6200Sstevel@tonic-gate 		if (rn >= totreg) {
6210Sstevel@tonic-gate 			kmem_free(drv_regp, reglen);
6220Sstevel@tonic-gate 			return (DDI_FAILURE);
6230Sstevel@tonic-gate 		}
6240Sstevel@tonic-gate 		*(off_t *)result = drv_regp[rn].pci_size_low |
6250Sstevel@tonic-gate 			((uint64_t)drv_regp[rn].pci_size_hi << 32);
6260Sstevel@tonic-gate 	}
6270Sstevel@tonic-gate 
6280Sstevel@tonic-gate 	kmem_free(drv_regp, reglen);
6290Sstevel@tonic-gate 	return (DDI_SUCCESS);
6300Sstevel@tonic-gate }
6310Sstevel@tonic-gate 
6320Sstevel@tonic-gate 
6330Sstevel@tonic-gate static dev_info_t *
6340Sstevel@tonic-gate get_my_childs_dip(dev_info_t *dip, dev_info_t *rdip)
6350Sstevel@tonic-gate {
6360Sstevel@tonic-gate 	dev_info_t *cdip = rdip;
6370Sstevel@tonic-gate 
6380Sstevel@tonic-gate 	for (; ddi_get_parent(cdip) != dip; cdip = ddi_get_parent(cdip))
6390Sstevel@tonic-gate 		;
6400Sstevel@tonic-gate 
6410Sstevel@tonic-gate 	return (cdip);
6420Sstevel@tonic-gate }
6430Sstevel@tonic-gate 
6440Sstevel@tonic-gate 
6450Sstevel@tonic-gate static int
6460Sstevel@tonic-gate ppb_intr_ops(dev_info_t *dip, dev_info_t *rdip, ddi_intr_op_t intr_op,
6470Sstevel@tonic-gate     ddi_intr_handle_impl_t *hdlp, void *result)
6480Sstevel@tonic-gate {
649693Sgovinda 	dev_info_t	*cdip = rdip;
650693Sgovinda 	pci_regspec_t	*pci_rp;
651693Sgovinda 	int		reglen, len;
652693Sgovinda 	uint32_t	d, intr;
6530Sstevel@tonic-gate 
6540Sstevel@tonic-gate 	if (hdlp->ih_type != DDI_INTR_TYPE_FIXED)
6550Sstevel@tonic-gate 		goto done;
6560Sstevel@tonic-gate 
6570Sstevel@tonic-gate 	/*
6580Sstevel@tonic-gate 	 * If the interrupt-map property is defined at this
6590Sstevel@tonic-gate 	 * node, it will have performed the interrupt
6600Sstevel@tonic-gate 	 * translation as part of the property, so no
6610Sstevel@tonic-gate 	 * rotation needs to be done.
6620Sstevel@tonic-gate 	 */
6630Sstevel@tonic-gate 	if (ddi_getproplen(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
6640Sstevel@tonic-gate 	    "interrupt-map", &len) == DDI_PROP_SUCCESS)
6650Sstevel@tonic-gate 		goto done;
6660Sstevel@tonic-gate 
6670Sstevel@tonic-gate 	cdip = get_my_childs_dip(dip, rdip);
6680Sstevel@tonic-gate 
6690Sstevel@tonic-gate 	/*
6700Sstevel@tonic-gate 	 * Use the devices reg property to determine its
6710Sstevel@tonic-gate 	 * PCI bus number and device number.
6720Sstevel@tonic-gate 	 */
673506Scth 	if (ddi_getlongprop(DDI_DEV_T_ANY, cdip, DDI_PROP_DONTPASS,
6740Sstevel@tonic-gate 	    "reg", (caddr_t)&pci_rp, &reglen) != DDI_SUCCESS)
6750Sstevel@tonic-gate 		return (DDI_FAILURE);
6760Sstevel@tonic-gate 
677693Sgovinda 	intr = hdlp->ih_vector;
6780Sstevel@tonic-gate 
6790Sstevel@tonic-gate 	/* Spin the interrupt */
6800Sstevel@tonic-gate 	d = PCI_REG_DEV_G(pci_rp[0].pci_phys_hi);
6810Sstevel@tonic-gate 
6820Sstevel@tonic-gate 	if ((intr >= PCI_INTA) && (intr <= PCI_INTD))
683693Sgovinda 		hdlp->ih_vector = ((intr - 1 + (d % 4)) % 4 + 1);
6840Sstevel@tonic-gate 	else
6850Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d: %s: PCI intr=%x out of range",
6860Sstevel@tonic-gate 		    ddi_driver_name(rdip), ddi_get_instance(rdip),
6870Sstevel@tonic-gate 		    ddi_driver_name(dip), intr);
6880Sstevel@tonic-gate 
6890Sstevel@tonic-gate 	kmem_free(pci_rp, reglen);
6900Sstevel@tonic-gate 
6910Sstevel@tonic-gate done:
6920Sstevel@tonic-gate 	/* Pass up the request to our parent. */
6930Sstevel@tonic-gate 	return (i_ddi_intr_ops(dip, rdip, intr_op, hdlp, result));
6940Sstevel@tonic-gate }
6950Sstevel@tonic-gate 
6960Sstevel@tonic-gate static int
6970Sstevel@tonic-gate ppb_bus_power(dev_info_t *dip, void *impl_arg, pm_bus_power_op_t op,
6980Sstevel@tonic-gate     void *arg, void *result)
6990Sstevel@tonic-gate {
7000Sstevel@tonic-gate 	ppb_devstate_t *ppb;
7010Sstevel@tonic-gate 
7020Sstevel@tonic-gate 	ppb = (ppb_devstate_t *)ddi_get_soft_state(ppb_state,
7030Sstevel@tonic-gate 	    ddi_get_instance(dip));
7040Sstevel@tonic-gate 
7050Sstevel@tonic-gate 	return (pci_pwr_ops(ppb->ppb_pwr_p, dip, impl_arg, op, arg, result));
7060Sstevel@tonic-gate }
7070Sstevel@tonic-gate 
7080Sstevel@tonic-gate 
7090Sstevel@tonic-gate /*
7100Sstevel@tonic-gate  * name_child
7110Sstevel@tonic-gate  *
7120Sstevel@tonic-gate  * This function is called from init_child to name a node. It is
7130Sstevel@tonic-gate  * also passed as a callback for node merging functions.
7140Sstevel@tonic-gate  *
7150Sstevel@tonic-gate  * return value: DDI_SUCCESS, DDI_FAILURE
7160Sstevel@tonic-gate  */
7170Sstevel@tonic-gate static int
7180Sstevel@tonic-gate ppb_name_child(dev_info_t *child, char *name, int namelen)
7190Sstevel@tonic-gate {
7200Sstevel@tonic-gate 	pci_regspec_t *pci_rp;
7210Sstevel@tonic-gate 	uint_t slot, func;
7220Sstevel@tonic-gate 	char **unit_addr;
7230Sstevel@tonic-gate 	uint_t n;
7240Sstevel@tonic-gate 
7250Sstevel@tonic-gate 	/*
7260Sstevel@tonic-gate 	 * Pseudo nodes indicate a prototype node with per-instance
7270Sstevel@tonic-gate 	 * properties to be merged into the real h/w device node.
7280Sstevel@tonic-gate 	 * The interpretation of the unit-address is DD[,F]
7290Sstevel@tonic-gate 	 * where DD is the device id and F is the function.
7300Sstevel@tonic-gate 	 */
7310Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node(child) == 0) {
7320Sstevel@tonic-gate 		if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, child,
7330Sstevel@tonic-gate 		    DDI_PROP_DONTPASS, "unit-address", &unit_addr, &n) !=
7340Sstevel@tonic-gate 		    DDI_PROP_SUCCESS) {
7350Sstevel@tonic-gate 			cmn_err(CE_WARN, "cannot name node from %s.conf",
7360Sstevel@tonic-gate 			    ddi_driver_name(child));
7370Sstevel@tonic-gate 			return (DDI_FAILURE);
7380Sstevel@tonic-gate 		}
7390Sstevel@tonic-gate 		if (n != 1 || *unit_addr == NULL || **unit_addr == 0) {
7400Sstevel@tonic-gate 			cmn_err(CE_WARN, "unit-address property in %s.conf"
7410Sstevel@tonic-gate 			    " not well-formed", ddi_driver_name(child));
7420Sstevel@tonic-gate 			ddi_prop_free(unit_addr);
7430Sstevel@tonic-gate 			return (DDI_FAILURE);
7440Sstevel@tonic-gate 		}
7450Sstevel@tonic-gate 		(void) snprintf(name, namelen, "%s", *unit_addr);
7460Sstevel@tonic-gate 		ddi_prop_free(unit_addr);
7470Sstevel@tonic-gate 		return (DDI_SUCCESS);
7480Sstevel@tonic-gate 	}
7490Sstevel@tonic-gate 
7500Sstevel@tonic-gate 	/*
7510Sstevel@tonic-gate 	 * Get the address portion of the node name based on
7520Sstevel@tonic-gate 	 * the function and device number.
7530Sstevel@tonic-gate 	 */
7540Sstevel@tonic-gate 	if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS,
7550Sstevel@tonic-gate 	    "reg", (int **)&pci_rp, &n) != DDI_SUCCESS) {
7560Sstevel@tonic-gate 		return (DDI_FAILURE);
7570Sstevel@tonic-gate 	}
7580Sstevel@tonic-gate 
7590Sstevel@tonic-gate 	slot = PCI_REG_DEV_G(pci_rp[0].pci_phys_hi);
7600Sstevel@tonic-gate 	func = PCI_REG_FUNC_G(pci_rp[0].pci_phys_hi);
7610Sstevel@tonic-gate 
7620Sstevel@tonic-gate 	if (func != 0)
7630Sstevel@tonic-gate 		(void) snprintf(name, namelen, "%x,%x", slot, func);
7640Sstevel@tonic-gate 	else
7650Sstevel@tonic-gate 		(void) snprintf(name, namelen, "%x", slot);
7660Sstevel@tonic-gate 
7670Sstevel@tonic-gate 	ddi_prop_free(pci_rp);
7680Sstevel@tonic-gate 	return (DDI_SUCCESS);
7690Sstevel@tonic-gate }
7700Sstevel@tonic-gate 
7710Sstevel@tonic-gate static int
7720Sstevel@tonic-gate ppb_initchild(dev_info_t *child)
7730Sstevel@tonic-gate {
7740Sstevel@tonic-gate 	char name[MAXNAMELEN];
7750Sstevel@tonic-gate 	ddi_acc_handle_t config_handle;
7760Sstevel@tonic-gate 	ushort_t command_preserve, command;
7770Sstevel@tonic-gate 	uint_t n;
7780Sstevel@tonic-gate 	ushort_t bcr;
7790Sstevel@tonic-gate 	uchar_t header_type;
7800Sstevel@tonic-gate 	uchar_t min_gnt, latency_timer;
7810Sstevel@tonic-gate 	ppb_devstate_t *ppb;
7820Sstevel@tonic-gate 
7830Sstevel@tonic-gate 	/*
7840Sstevel@tonic-gate 	 * Name the child
7850Sstevel@tonic-gate 	 */
7860Sstevel@tonic-gate 	if (ppb_name_child(child, name, MAXNAMELEN) != DDI_SUCCESS)
7870Sstevel@tonic-gate 		return (DDI_FAILURE);
7880Sstevel@tonic-gate 
7890Sstevel@tonic-gate 	ddi_set_name_addr(child, name);
7900Sstevel@tonic-gate 	ddi_set_parent_data(child, NULL);
7910Sstevel@tonic-gate 
7920Sstevel@tonic-gate 	/*
7930Sstevel@tonic-gate 	 * Pseudo nodes indicate a prototype node with per-instance
7940Sstevel@tonic-gate 	 * properties to be merged into the real h/w device node.
7950Sstevel@tonic-gate 	 * The interpretation of the unit-address is DD[,F]
7960Sstevel@tonic-gate 	 * where DD is the device id and F is the function.
7970Sstevel@tonic-gate 	 */
7980Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node(child) == 0) {
7990Sstevel@tonic-gate 		extern int pci_allow_pseudo_children;
8000Sstevel@tonic-gate 
8010Sstevel@tonic-gate 		/*
8020Sstevel@tonic-gate 		 * Try to merge the properties from this prototype
8030Sstevel@tonic-gate 		 * node into real h/w nodes.
8040Sstevel@tonic-gate 		 */
8050Sstevel@tonic-gate 		if (ndi_merge_node(child, ppb_name_child) == DDI_SUCCESS) {
8060Sstevel@tonic-gate 			/*
8070Sstevel@tonic-gate 			 * Merged ok - return failure to remove the node.
8080Sstevel@tonic-gate 			 */
8090Sstevel@tonic-gate 			ppb_removechild(child);
8100Sstevel@tonic-gate 			return (DDI_FAILURE);
8110Sstevel@tonic-gate 		}
8120Sstevel@tonic-gate 
8130Sstevel@tonic-gate 		/* workaround for ddivs to run under PCI */
8140Sstevel@tonic-gate 		if (pci_allow_pseudo_children)
8150Sstevel@tonic-gate 			return (DDI_SUCCESS);
8160Sstevel@tonic-gate 
8170Sstevel@tonic-gate 		/*
8180Sstevel@tonic-gate 		 * The child was not merged into a h/w node,
8190Sstevel@tonic-gate 		 * but there's not much we can do with it other
8200Sstevel@tonic-gate 		 * than return failure to cause the node to be removed.
8210Sstevel@tonic-gate 		 */
8220Sstevel@tonic-gate 		cmn_err(CE_WARN, "!%s@%s: %s.conf properties not merged",
8230Sstevel@tonic-gate 		    ddi_driver_name(child), ddi_get_name_addr(child),
8240Sstevel@tonic-gate 		    ddi_driver_name(child));
8250Sstevel@tonic-gate 		ppb_removechild(child);
8260Sstevel@tonic-gate 		return (DDI_NOT_WELL_FORMED);
8270Sstevel@tonic-gate 	}
8280Sstevel@tonic-gate 
8290Sstevel@tonic-gate 	ppb = (ppb_devstate_t *)ddi_get_soft_state(ppb_state,
8300Sstevel@tonic-gate 	    ddi_get_instance(ddi_get_parent(child)));
8310Sstevel@tonic-gate 
832*3270Set142600 	ddi_set_parent_data(child, NULL);
833*3270Set142600 
8340Sstevel@tonic-gate 	/*
8350Sstevel@tonic-gate 	 * If hardware is PM capable, set up the power info structure.
8360Sstevel@tonic-gate 	 * This also ensures the the bus will not be off (0MHz) otherwise
8370Sstevel@tonic-gate 	 * system panics during a bus access.
8380Sstevel@tonic-gate 	 */
8390Sstevel@tonic-gate 	if (PM_CAPABLE(ppb->ppb_pwr_p)) {
8400Sstevel@tonic-gate 		/*
8410Sstevel@tonic-gate 		 * Create a pwr_info struct for child.  Bus will be
8420Sstevel@tonic-gate 		 * at full speed after creating info.
8430Sstevel@tonic-gate 		 */
8440Sstevel@tonic-gate 		pci_pwr_create_info(ppb->ppb_pwr_p, child);
8450Sstevel@tonic-gate #ifdef DEBUG
8460Sstevel@tonic-gate 		ASSERT(ppb->ppb_pwr_p->current_lvl == PM_LEVEL_B0);
8470Sstevel@tonic-gate #endif
8480Sstevel@tonic-gate 	}
8490Sstevel@tonic-gate 
8500Sstevel@tonic-gate 	/*
8510Sstevel@tonic-gate 	 * If configuration registers were previously saved by
8520Sstevel@tonic-gate 	 * child (before it entered D3), then let the child do the
8530Sstevel@tonic-gate 	 * restore to set up the config regs as it'll first need to
8540Sstevel@tonic-gate 	 * power the device out of D3.
8550Sstevel@tonic-gate 	 */
8560Sstevel@tonic-gate 	if (ddi_prop_exists(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS,
8570Sstevel@tonic-gate 	    "config-regs-saved-by-child") == 1) {
8580Sstevel@tonic-gate 		DEBUG2(DBG_PWR, ddi_get_parent(child),
8590Sstevel@tonic-gate 			"INITCHILD: config regs to be restored by child"
8600Sstevel@tonic-gate 			" for %s@%s\n", ddi_node_name(child),
8610Sstevel@tonic-gate 				ddi_get_name_addr(child));
8620Sstevel@tonic-gate 
8630Sstevel@tonic-gate 		return (DDI_SUCCESS);
8640Sstevel@tonic-gate 	}
8650Sstevel@tonic-gate 
8660Sstevel@tonic-gate 	DEBUG2(DBG_PWR, ddi_get_parent(child),
8670Sstevel@tonic-gate 	    "INITCHILD: config regs setup for %s@%s\n",
8680Sstevel@tonic-gate 	    ddi_node_name(child), ddi_get_name_addr(child));
8690Sstevel@tonic-gate 
8700Sstevel@tonic-gate 	if (pci_config_setup(child, &config_handle) != DDI_SUCCESS) {
8710Sstevel@tonic-gate 		if (PM_CAPABLE(ppb->ppb_pwr_p)) {
8720Sstevel@tonic-gate 			pci_pwr_rm_info(ppb->ppb_pwr_p, child);
8730Sstevel@tonic-gate 		}
8740Sstevel@tonic-gate 
8750Sstevel@tonic-gate 		return (DDI_FAILURE);
8760Sstevel@tonic-gate 	}
8770Sstevel@tonic-gate 
8780Sstevel@tonic-gate 	/*
8790Sstevel@tonic-gate 	 * Determine the configuration header type.
8800Sstevel@tonic-gate 	 */
8810Sstevel@tonic-gate 	header_type = pci_config_get8(config_handle, PCI_CONF_HEADER);
8820Sstevel@tonic-gate 
8830Sstevel@tonic-gate 	/*
8840Sstevel@tonic-gate 	 * Support for the "command-preserve" property.
8850Sstevel@tonic-gate 	 */
8860Sstevel@tonic-gate 	command_preserve = ddi_prop_get_int(DDI_DEV_T_ANY, child,
8870Sstevel@tonic-gate 		DDI_PROP_DONTPASS, "command-preserve", 0);
8880Sstevel@tonic-gate 	command = pci_config_get16(config_handle, PCI_CONF_COMM);
8890Sstevel@tonic-gate 	command &= (command_preserve | PCI_COMM_BACK2BACK_ENAB);
8900Sstevel@tonic-gate 	command |= (ppb_command_default & ~command_preserve);
8910Sstevel@tonic-gate 	pci_config_put16(config_handle, PCI_CONF_COMM, command);
8920Sstevel@tonic-gate 
8930Sstevel@tonic-gate 	/*
8940Sstevel@tonic-gate 	 * If the device has a bus control register then program it
8950Sstevel@tonic-gate 	 * based on the settings in the command register.
8960Sstevel@tonic-gate 	 */
897*3270Set142600 	if ((header_type  & PCI_HEADER_TYPE_M) == PCI_HEADER_ONE) {
8980Sstevel@tonic-gate 		bcr = pci_config_get8(config_handle, PCI_BCNF_BCNTRL);
8990Sstevel@tonic-gate 		if (ppb_command_default & PCI_COMM_PARITY_DETECT)
9000Sstevel@tonic-gate 			bcr |= PCI_BCNF_BCNTRL_PARITY_ENABLE;
9010Sstevel@tonic-gate 		if (ppb_command_default & PCI_COMM_SERR_ENABLE)
9020Sstevel@tonic-gate 			bcr |= PCI_BCNF_BCNTRL_SERR_ENABLE;
9030Sstevel@tonic-gate 		bcr |= PCI_BCNF_BCNTRL_MAST_AB_MODE;
9040Sstevel@tonic-gate 		pci_config_put8(config_handle, PCI_BCNF_BCNTRL, bcr);
9050Sstevel@tonic-gate 	}
9060Sstevel@tonic-gate 
9070Sstevel@tonic-gate 	/*
9080Sstevel@tonic-gate 	 * Initialize cache-line-size configuration register if needed.
9090Sstevel@tonic-gate 	 */
9100Sstevel@tonic-gate 	if (ppb_set_cache_line_size_register &&
9110Sstevel@tonic-gate 	    ddi_getprop(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS,
9120Sstevel@tonic-gate 		"cache-line-size", 0) == 0) {
9130Sstevel@tonic-gate 		pci_config_put8(config_handle, PCI_CONF_CACHE_LINESZ,
9140Sstevel@tonic-gate 			ppb->ppb_cache_line_size);
9150Sstevel@tonic-gate 		n = pci_config_get8(config_handle, PCI_CONF_CACHE_LINESZ);
9160Sstevel@tonic-gate 		if (n != 0) {
9170Sstevel@tonic-gate 			(void) ndi_prop_update_int(DDI_DEV_T_NONE, child,
9180Sstevel@tonic-gate 					"cache-line-size", n);
9190Sstevel@tonic-gate 		}
9200Sstevel@tonic-gate 	}
9210Sstevel@tonic-gate 
9220Sstevel@tonic-gate 	/*
9230Sstevel@tonic-gate 	 * Initialize latency timer configuration registers if needed.
9240Sstevel@tonic-gate 	 */
9250Sstevel@tonic-gate 	if (ppb_set_latency_timer_register &&
9260Sstevel@tonic-gate 	    ddi_getprop(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS,
9270Sstevel@tonic-gate 		"latency-timer", 0) == 0) {
9280Sstevel@tonic-gate 
9290Sstevel@tonic-gate 		if ((header_type & PCI_HEADER_TYPE_M) == PCI_HEADER_ONE) {
9300Sstevel@tonic-gate 			latency_timer = ppb->ppb_latency_timer;
9310Sstevel@tonic-gate 			pci_config_put8(config_handle, PCI_BCNF_LATENCY_TIMER,
9320Sstevel@tonic-gate 				ppb->ppb_latency_timer);
9330Sstevel@tonic-gate 		} else {
9340Sstevel@tonic-gate 			min_gnt = pci_config_get8(config_handle,
9350Sstevel@tonic-gate 				PCI_CONF_MIN_G);
9360Sstevel@tonic-gate 			latency_timer = min_gnt * 8;
9370Sstevel@tonic-gate 		}
9380Sstevel@tonic-gate 		pci_config_put8(config_handle, PCI_CONF_LATENCY_TIMER,
9390Sstevel@tonic-gate 			latency_timer);
9400Sstevel@tonic-gate 		n = pci_config_get8(config_handle, PCI_CONF_LATENCY_TIMER);
9410Sstevel@tonic-gate 		if (n != 0) {
9420Sstevel@tonic-gate 			(void) ndi_prop_update_int(DDI_DEV_T_NONE, child,
9430Sstevel@tonic-gate 					"latency-timer", n);
9440Sstevel@tonic-gate 		}
9450Sstevel@tonic-gate 	}
9460Sstevel@tonic-gate 
9470Sstevel@tonic-gate 	/*
948*3270Set142600 	 * SPARC PCIe FMA specific
949*3270Set142600 	 *
950*3270Set142600 	 * Note: parent_data for parent is created only if this is sparc PCI-E
951*3270Set142600 	 * platform, for which, SG take a different route to handle device
952*3270Set142600 	 * errors.
953*3270Set142600 	 */
954*3270Set142600 	if (ppb->parent_bus == PCIE_PCIECAP_DEV_TYPE_PCIE_DEV) {
955*3270Set142600 		if (pcie_init_ppd(child) == NULL)
956*3270Set142600 			return (DDI_FAILURE);
957*3270Set142600 	}
958*3270Set142600 
959*3270Set142600 	/*
9600Sstevel@tonic-gate 	 * Check to see if the XMITS/PCI-X workaround applies.
9610Sstevel@tonic-gate 	 */
9620Sstevel@tonic-gate 	n = ddi_getprop(DDI_DEV_T_ANY, child, DDI_PROP_NOTPROM,
9630Sstevel@tonic-gate 	    "pcix-update-cmd-reg", -1);
9640Sstevel@tonic-gate 
9650Sstevel@tonic-gate 	if (n != -1) {
9660Sstevel@tonic-gate 		extern void pcix_set_cmd_reg(dev_info_t *child, uint16_t value);
9670Sstevel@tonic-gate 		DEBUG1(DBG_INIT_CLD, child, "Turning on XMITS NCPQ "
9680Sstevel@tonic-gate 		    "Workaround: value = %x\n", n);
9690Sstevel@tonic-gate 		pcix_set_cmd_reg(child, n);
9700Sstevel@tonic-gate 	}
9710Sstevel@tonic-gate 
972*3270Set142600 	/* since cached, teardown config handle in ppb_uninitchild() */
973*3270Set142600 	return (DDI_SUCCESS);
974*3270Set142600 }
975*3270Set142600 
976*3270Set142600 static void
977*3270Set142600 ppb_uninitchild(dev_info_t *child)
978*3270Set142600 {
979*3270Set142600 	ppb_devstate_t *ppb;
980*3270Set142600 
981*3270Set142600 	ppb = (ppb_devstate_t *)ddi_get_soft_state(ppb_state,
982*3270Set142600 	    ddi_get_instance(ddi_get_parent(child)));
9833156Sgirish 
9843156Sgirish 	/*
985*3270Set142600 	 * SG OPL FMA specific
9863156Sgirish 	 */
987*3270Set142600 	if (ppb->parent_bus == PCIE_PCIECAP_DEV_TYPE_PCIE_DEV)
988*3270Set142600 		pcie_uninit_ppd(child);
9893156Sgirish 
990*3270Set142600 	ppb_removechild(child);
9910Sstevel@tonic-gate }
9920Sstevel@tonic-gate 
9930Sstevel@tonic-gate static void
9940Sstevel@tonic-gate ppb_removechild(dev_info_t *dip)
9950Sstevel@tonic-gate {
9960Sstevel@tonic-gate 	ppb_devstate_t *ppb;
9970Sstevel@tonic-gate 
9980Sstevel@tonic-gate 	ppb = (ppb_devstate_t *)ddi_get_soft_state(ppb_state,
9990Sstevel@tonic-gate 	    ddi_get_instance(ddi_get_parent(dip)));
10000Sstevel@tonic-gate 
10010Sstevel@tonic-gate 	if (PM_CAPABLE(ppb->ppb_pwr_p)) {
10020Sstevel@tonic-gate 
10030Sstevel@tonic-gate 		DEBUG2(DBG_PWR, ddi_get_parent(dip),
10040Sstevel@tonic-gate 		    "UNINITCHILD: removing pwr_info for %s@%s\n",
10050Sstevel@tonic-gate 		    ddi_node_name(dip), ddi_get_name_addr(dip));
10060Sstevel@tonic-gate 		pci_pwr_rm_info(ppb->ppb_pwr_p, dip);
10070Sstevel@tonic-gate 	}
10080Sstevel@tonic-gate 
10090Sstevel@tonic-gate 	ddi_set_name_addr(dip, NULL);
10100Sstevel@tonic-gate 
10110Sstevel@tonic-gate 	/*
10120Sstevel@tonic-gate 	 * Strip the node to properly convert it back to prototype form
10130Sstevel@tonic-gate 	 */
10140Sstevel@tonic-gate 	ddi_remove_minor_node(dip, NULL);
10150Sstevel@tonic-gate 
10160Sstevel@tonic-gate 	impl_rem_dev_props(dip);
10170Sstevel@tonic-gate }
10180Sstevel@tonic-gate 
10190Sstevel@tonic-gate /*
10200Sstevel@tonic-gate  * If bridge is PM capable, set up PM state for nexus.
10210Sstevel@tonic-gate  */
10220Sstevel@tonic-gate static void
10230Sstevel@tonic-gate ppb_pwr_setup(ppb_devstate_t *ppb, dev_info_t *pdip)
10240Sstevel@tonic-gate {
10250Sstevel@tonic-gate 	char *comp_array[5];
10260Sstevel@tonic-gate 	int i;
10270Sstevel@tonic-gate 	ddi_acc_handle_t conf_hdl;
10280Sstevel@tonic-gate 	uint8_t pmcsr_bse;
10290Sstevel@tonic-gate 	uint16_t pmcap;
10300Sstevel@tonic-gate 
10310Sstevel@tonic-gate 	/*
10320Sstevel@tonic-gate 	 * Determine if bridge is PM capable.  If not, leave ppb_pwr_p NULL
10330Sstevel@tonic-gate 	 * and return.
10340Sstevel@tonic-gate 	 */
10350Sstevel@tonic-gate 	if (pci_config_setup(pdip, &ppb->ppb_conf_hdl) != DDI_SUCCESS) {
10360Sstevel@tonic-gate 
10370Sstevel@tonic-gate 		return;
10380Sstevel@tonic-gate 	}
10390Sstevel@tonic-gate 
10400Sstevel@tonic-gate 	conf_hdl = ppb->ppb_conf_hdl;
10410Sstevel@tonic-gate 
10420Sstevel@tonic-gate 	/*
10431624Spjha 	 * Locate and store the power management cap_ptr for future references.
10440Sstevel@tonic-gate 	 */
10451624Spjha 	if ((PCI_CAP_LOCATE(conf_hdl, PCI_CAP_ID_PM, &ppb->ppb_pm_cap_ptr))
10461624Spjha 		== DDI_FAILURE) {
10470Sstevel@tonic-gate 		DEBUG0(DBG_PWR, pdip, "bridge does not support PM. PCI"
10480Sstevel@tonic-gate 		    " PM data structure not found in config header\n");
10490Sstevel@tonic-gate 		pci_config_teardown(&conf_hdl);
10500Sstevel@tonic-gate 
10510Sstevel@tonic-gate 		return;
10520Sstevel@tonic-gate 	}
10530Sstevel@tonic-gate 
10540Sstevel@tonic-gate 	/*
10550Sstevel@tonic-gate 	 * Allocate PM state structure for ppb.
10560Sstevel@tonic-gate 	 */
10570Sstevel@tonic-gate 	ppb->ppb_pwr_p = (pci_pwr_t *)
10580Sstevel@tonic-gate 	    kmem_zalloc(sizeof (pci_pwr_t), KM_SLEEP);
10590Sstevel@tonic-gate 	ppb->ppb_pwr_p->pwr_fp = 0;
10600Sstevel@tonic-gate 
10611624Spjha 	pmcsr_bse = PCI_CAP_GET8(conf_hdl, NULL, ppb->ppb_pm_cap_ptr,
10621624Spjha 		PCI_PMCSR_BSE);
10630Sstevel@tonic-gate 
10641624Spjha 	pmcap = PCI_CAP_GET16(conf_hdl, NULL, ppb->ppb_pm_cap_ptr,
10651624Spjha 		PCI_PMCAP);
10660Sstevel@tonic-gate 
10671774Spjha 	if (pmcap == PCI_CAP_EINVAL16 || pmcsr_bse == PCI_CAP_EINVAL8) {
10681624Spjha 		pci_config_teardown(&conf_hdl);
10691624Spjha 		return;
10701624Spjha 	}
10710Sstevel@tonic-gate 
10720Sstevel@tonic-gate 	if (pmcap & PCI_PMCAP_D1) {
10730Sstevel@tonic-gate 		DEBUG0(DBG_PWR, pdip, "setup: B1 state supported\n");
10740Sstevel@tonic-gate 		ppb->ppb_pwr_p->pwr_flags |= PCI_PWR_B1_CAPABLE;
10750Sstevel@tonic-gate 	} else {
10760Sstevel@tonic-gate 		DEBUG0(DBG_PWR, pdip, "setup: B1 state NOT supported\n");
10770Sstevel@tonic-gate 	}
10780Sstevel@tonic-gate 	if (pmcap & PCI_PMCAP_D2) {
10790Sstevel@tonic-gate 		DEBUG0(DBG_PWR, pdip, "setup: B2 state supported\n");
10800Sstevel@tonic-gate 		ppb->ppb_pwr_p->pwr_flags |= PCI_PWR_B2_CAPABLE;
10810Sstevel@tonic-gate 	} else {
10820Sstevel@tonic-gate 		DEBUG0(DBG_PWR, pdip, "setup: B2 via D2 NOT supported\n");
10830Sstevel@tonic-gate 	}
10840Sstevel@tonic-gate 
10850Sstevel@tonic-gate 	if (pmcsr_bse & PCI_PMCSR_BSE_BPCC_EN) {
10860Sstevel@tonic-gate 		DEBUG0(DBG_PWR, pdip,
10871624Spjha 		"setup: bridge power/clock control enable\n");
10880Sstevel@tonic-gate 	} else {
10890Sstevel@tonic-gate 		DEBUG0(DBG_PWR, pdip,
10901624Spjha 		"setup: bridge power/clock control disabled\n");
10910Sstevel@tonic-gate 
10920Sstevel@tonic-gate 		kmem_free(ppb->ppb_pwr_p, sizeof (pci_pwr_t));
10930Sstevel@tonic-gate 		ppb->ppb_pwr_p = NULL;
10940Sstevel@tonic-gate 		pci_config_teardown(&conf_hdl);
10950Sstevel@tonic-gate 
10960Sstevel@tonic-gate 		return;
10970Sstevel@tonic-gate 	}
10980Sstevel@tonic-gate 
10990Sstevel@tonic-gate 	/*
11000Sstevel@tonic-gate 	 * PCI states D0 and D3 always are supported for normal PCI
11010Sstevel@tonic-gate 	 * devices.  D1 and D2 are optional which are checked for above.
11020Sstevel@tonic-gate 	 * Bridge function states D0-D3 correspond to secondary bus states
11030Sstevel@tonic-gate 	 * B0-B3, EXCEPT if PCI_PMCSR_BSE_B2_B3 is set.  In this case, setting
11040Sstevel@tonic-gate 	 * the bridge function to D3 will set the bridge bus to state B2 instead
11050Sstevel@tonic-gate 	 * of B3.  D2 will not correspond to B2 (and in fact, probably
11060Sstevel@tonic-gate 	 * won't be D2 capable).  Implicitly, this means that if
11070Sstevel@tonic-gate 	 * PCI_PMCSR_BSE_B2_B3 is set, the bus will not be B3 capable.
11080Sstevel@tonic-gate 	 */
11090Sstevel@tonic-gate 	if (pmcsr_bse & PCI_PMCSR_BSE_B2_B3) {
11100Sstevel@tonic-gate 		ppb->ppb_pwr_p->pwr_flags |= PCI_PWR_B2_CAPABLE;
11110Sstevel@tonic-gate 		DEBUG0(DBG_PWR, pdip, "B2 supported via D3\n");
11120Sstevel@tonic-gate 	} else {
11130Sstevel@tonic-gate 		ppb->ppb_pwr_p->pwr_flags |= PCI_PWR_B3_CAPABLE;
11140Sstevel@tonic-gate 		DEBUG0(DBG_PWR, pdip, "B3 supported via D3\n");
11150Sstevel@tonic-gate 	}
11160Sstevel@tonic-gate 
11170Sstevel@tonic-gate 	ppb->ppb_pwr_p->pwr_dip = pdip;
11180Sstevel@tonic-gate 	mutex_init(&ppb->ppb_pwr_p->pwr_mutex, NULL, MUTEX_DRIVER, NULL);
11190Sstevel@tonic-gate 
11200Sstevel@tonic-gate 	i = 0;
11210Sstevel@tonic-gate 	comp_array[i++] = "NAME=PCI bridge PM";
11220Sstevel@tonic-gate 	if (ppb->ppb_pwr_p->pwr_flags & PCI_PWR_B3_CAPABLE) {
11230Sstevel@tonic-gate 		comp_array[i++] = "0=Clock/Power Off (B3)";
11240Sstevel@tonic-gate 	}
11250Sstevel@tonic-gate 	if (ppb->ppb_pwr_p->pwr_flags & PCI_PWR_B2_CAPABLE) {
11260Sstevel@tonic-gate 		comp_array[i++] = "1=Clock Off (B2)";
11270Sstevel@tonic-gate 	}
11280Sstevel@tonic-gate 	if (ppb->ppb_pwr_p->pwr_flags & PCI_PWR_B1_CAPABLE) {
11290Sstevel@tonic-gate 		comp_array[i++] = "2=Bus Inactive (B1)";
11300Sstevel@tonic-gate 	}
11310Sstevel@tonic-gate 	comp_array[i++] = "3=Full Power (B0)";
11320Sstevel@tonic-gate 
11330Sstevel@tonic-gate 	/*
11340Sstevel@tonic-gate 	 * Create pm-components property. It does not already exist.
11350Sstevel@tonic-gate 	 */
11360Sstevel@tonic-gate 	if (ddi_prop_update_string_array(DDI_DEV_T_NONE, pdip,
11370Sstevel@tonic-gate 	    "pm-components", comp_array, i) != DDI_PROP_SUCCESS) {
11380Sstevel@tonic-gate 		cmn_err(CE_WARN,
11390Sstevel@tonic-gate 		    "%s%d pm-components prop update failed",
11400Sstevel@tonic-gate 		    ddi_driver_name(pdip), ddi_get_instance(pdip));
11410Sstevel@tonic-gate 		pci_config_teardown(&conf_hdl);
11420Sstevel@tonic-gate 		mutex_destroy(&ppb->ppb_pwr_p->pwr_mutex);
11430Sstevel@tonic-gate 		kmem_free(ppb->ppb_pwr_p, sizeof (pci_pwr_t));
11440Sstevel@tonic-gate 		ppb->ppb_pwr_p = NULL;
11450Sstevel@tonic-gate 
11460Sstevel@tonic-gate 		return;
11470Sstevel@tonic-gate 	}
11480Sstevel@tonic-gate 
11490Sstevel@tonic-gate 	if (ddi_prop_create(DDI_DEV_T_NONE, pdip, DDI_PROP_CANSLEEP,
11500Sstevel@tonic-gate 	    "pm-want-child-notification?", NULL, NULL) != DDI_PROP_SUCCESS) {
11510Sstevel@tonic-gate 		cmn_err(CE_WARN,
11520Sstevel@tonic-gate 			"%s%d fail to create pm-want-child-notification? prop",
11530Sstevel@tonic-gate 			ddi_driver_name(pdip), ddi_get_instance(pdip));
11540Sstevel@tonic-gate 
11550Sstevel@tonic-gate 		(void) ddi_prop_remove(DDI_DEV_T_NONE, pdip, "pm-components");
11560Sstevel@tonic-gate 		pci_config_teardown(&conf_hdl);
11570Sstevel@tonic-gate 		mutex_destroy(&ppb->ppb_pwr_p->pwr_mutex);
11580Sstevel@tonic-gate 		kmem_free(ppb->ppb_pwr_p, sizeof (pci_pwr_t));
11590Sstevel@tonic-gate 		ppb->ppb_pwr_p = NULL;
11600Sstevel@tonic-gate 
11610Sstevel@tonic-gate 		return;
11620Sstevel@tonic-gate 	}
11630Sstevel@tonic-gate 
11640Sstevel@tonic-gate 	ppb->ppb_pwr_p->current_lvl =
11651624Spjha 		pci_pwr_current_lvl(ppb->ppb_pwr_p);
11660Sstevel@tonic-gate }
11670Sstevel@tonic-gate 
11680Sstevel@tonic-gate /*
11690Sstevel@tonic-gate  * Remove PM state for nexus.
11700Sstevel@tonic-gate  */
11710Sstevel@tonic-gate static void
11720Sstevel@tonic-gate ppb_pwr_teardown(ppb_devstate_t *ppb, dev_info_t *dip)
11730Sstevel@tonic-gate {
11740Sstevel@tonic-gate 	int low_lvl;
11750Sstevel@tonic-gate 
11760Sstevel@tonic-gate 	/*
11770Sstevel@tonic-gate 	 * Determine the lowest power level supported.
11780Sstevel@tonic-gate 	 */
11790Sstevel@tonic-gate 	if (ppb->ppb_pwr_p->pwr_flags & PCI_PWR_B3_CAPABLE) {
11800Sstevel@tonic-gate 		low_lvl = PM_LEVEL_B3;
11810Sstevel@tonic-gate 	} else {
11820Sstevel@tonic-gate 		low_lvl = PM_LEVEL_B2;
11830Sstevel@tonic-gate 	}
11840Sstevel@tonic-gate 
11850Sstevel@tonic-gate 	if (pm_lower_power(dip, PCI_PM_COMP_0, low_lvl) != DDI_SUCCESS) {
11860Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d failed to lower power",
11870Sstevel@tonic-gate 		    ddi_driver_name(dip), ddi_get_instance(dip));
11880Sstevel@tonic-gate 	}
11890Sstevel@tonic-gate 
11900Sstevel@tonic-gate 	pci_config_teardown(&ppb->ppb_conf_hdl);
11910Sstevel@tonic-gate 	mutex_destroy(&ppb->ppb_pwr_p->pwr_mutex);
11920Sstevel@tonic-gate 	kmem_free(ppb->ppb_pwr_p, sizeof (pci_pwr_t));
11930Sstevel@tonic-gate 
11940Sstevel@tonic-gate 	if (ddi_prop_remove(DDI_DEV_T_NONE, dip, "pm-components") !=
11950Sstevel@tonic-gate 		DDI_PROP_SUCCESS) {
11960Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d unable to remove prop pm-components",
11970Sstevel@tonic-gate 		    ddi_driver_name(dip), ddi_get_instance(dip));
11980Sstevel@tonic-gate 	}
11990Sstevel@tonic-gate 
12000Sstevel@tonic-gate 	if (ddi_prop_remove(DDI_DEV_T_NONE, dip,
12010Sstevel@tonic-gate 	    "pm-want-child-notification?") != DDI_PROP_SUCCESS) {
12020Sstevel@tonic-gate 		cmn_err(CE_WARN,
12030Sstevel@tonic-gate 		    "%s%d unable to remove prop pm-want_child_notification?",
12040Sstevel@tonic-gate 		    ddi_driver_name(dip), ddi_get_instance(dip));
12050Sstevel@tonic-gate 	}
12060Sstevel@tonic-gate }
12070Sstevel@tonic-gate 
12080Sstevel@tonic-gate /*
12090Sstevel@tonic-gate  * Examine the pmcsr register and return the software defined
12100Sstevel@tonic-gate  * state (the difference being whether D3 means B2 or B3).
12110Sstevel@tonic-gate  */
12120Sstevel@tonic-gate int
12130Sstevel@tonic-gate pci_pwr_current_lvl(pci_pwr_t *pwr_p)
12140Sstevel@tonic-gate {
12150Sstevel@tonic-gate 	ppb_devstate_t *ppb;
12160Sstevel@tonic-gate 	uint16_t pmcsr;
12170Sstevel@tonic-gate 
12180Sstevel@tonic-gate 	/*
12190Sstevel@tonic-gate 	 * Find out current power level
12200Sstevel@tonic-gate 	 */
12210Sstevel@tonic-gate 	ppb = (ppb_devstate_t *)ddi_get_soft_state(ppb_state,
12220Sstevel@tonic-gate 	    ddi_get_instance(pwr_p->pwr_dip));
12230Sstevel@tonic-gate 
12241624Spjha 	if ((pmcsr = PCI_CAP_GET16(ppb->ppb_conf_hdl, NULL,
12251774Spjha 		ppb->ppb_pm_cap_ptr, PCI_PMCSR)) == PCI_CAP_EINVAL16)
12261624Spjha 		return (DDI_FAILURE);
12270Sstevel@tonic-gate 
12280Sstevel@tonic-gate 	switch (pmcsr & PCI_PMCSR_STATE_MASK) {
12290Sstevel@tonic-gate 	case PCI_PMCSR_D0:
12300Sstevel@tonic-gate 
12310Sstevel@tonic-gate 		return (PM_LEVEL_B0);
12320Sstevel@tonic-gate 	case PCI_PMCSR_D1:
12330Sstevel@tonic-gate 
12340Sstevel@tonic-gate 		return (PM_LEVEL_B1);
12350Sstevel@tonic-gate 	case PCI_PMCSR_D2:
12360Sstevel@tonic-gate 
12370Sstevel@tonic-gate 		return (PM_LEVEL_B2);
12380Sstevel@tonic-gate 	case PCI_PMCSR_D3HOT:
12390Sstevel@tonic-gate 		if ((ppb->ppb_pwr_p->pwr_flags & PCI_PWR_B3_CAPABLE) == 0) {
12400Sstevel@tonic-gate 
12410Sstevel@tonic-gate 			return (PM_LEVEL_B2);
12420Sstevel@tonic-gate 		} else {
12430Sstevel@tonic-gate 
12440Sstevel@tonic-gate 			return (PM_LEVEL_B3);
12450Sstevel@tonic-gate 		}
12460Sstevel@tonic-gate 	}
1247946Smathue 	/*NOTREACHED*/
1248946Smathue 	return (PM_LEVEL_B3);
12490Sstevel@tonic-gate }
12500Sstevel@tonic-gate 
12510Sstevel@tonic-gate /*
12520Sstevel@tonic-gate  * Power entry point.  Called by the PM framework to change the
12530Sstevel@tonic-gate  * current power state of the bus.  This function must first verify that
12540Sstevel@tonic-gate  * the requested power change is still valid.
12550Sstevel@tonic-gate  */
12560Sstevel@tonic-gate /*ARGSUSED*/
12570Sstevel@tonic-gate static int
12580Sstevel@tonic-gate ppb_pwr(dev_info_t *dip, int component, int lvl)
12590Sstevel@tonic-gate {
12600Sstevel@tonic-gate 	ppb_devstate_t *ppb;
12610Sstevel@tonic-gate 	uint16_t pmcsr;
12620Sstevel@tonic-gate 	char *str;
12630Sstevel@tonic-gate 	int lowest_lvl;
12640Sstevel@tonic-gate 	int old_lvl;
12650Sstevel@tonic-gate 	int new_lvl;
12660Sstevel@tonic-gate 
12670Sstevel@tonic-gate 	ppb = (ppb_devstate_t *)ddi_get_soft_state(ppb_state,
12680Sstevel@tonic-gate 	    ddi_get_instance(dip));
12690Sstevel@tonic-gate 	if (ppb == NULL) {
12700Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d ppb_pwr: can't get soft state",
12710Sstevel@tonic-gate 		    ddi_driver_name(dip), ddi_get_instance(dip));
12720Sstevel@tonic-gate 
12730Sstevel@tonic-gate 		return (DDI_FAILURE);
12740Sstevel@tonic-gate 	}
12750Sstevel@tonic-gate 
12760Sstevel@tonic-gate 	DEBUG1(DBG_PWR, dip, "ppb_pwr(): ENTER level = %d\n", lvl);
12770Sstevel@tonic-gate 
12780Sstevel@tonic-gate 	mutex_enter(&ppb->ppb_pwr_p->pwr_mutex);
12790Sstevel@tonic-gate 
12800Sstevel@tonic-gate 	/*
12810Sstevel@tonic-gate 	 * Find out if the power setting is possible.  If it is not,
12820Sstevel@tonic-gate 	 * set component busy and return failure.  If it is possible,
12830Sstevel@tonic-gate 	 * and it is the lowest pwr setting possible, set component
12840Sstevel@tonic-gate 	 * busy so that the framework does not try to lower any further.
12850Sstevel@tonic-gate 	 */
12860Sstevel@tonic-gate 	lowest_lvl = pci_pwr_new_lvl(ppb->ppb_pwr_p);
12870Sstevel@tonic-gate 	if (lowest_lvl > lvl) {
12880Sstevel@tonic-gate 		pci_pwr_component_busy(ppb->ppb_pwr_p);
12890Sstevel@tonic-gate 		DEBUG2(DBG_PWR, dip, "ppb_pwr: failing power request "
12900Sstevel@tonic-gate 			"lowest allowed is %d requested is %d\n",
12910Sstevel@tonic-gate 				lowest_lvl, lvl);
12920Sstevel@tonic-gate 		mutex_exit(&ppb->ppb_pwr_p->pwr_mutex);
12930Sstevel@tonic-gate 
12940Sstevel@tonic-gate 		return (DDI_FAILURE);
12950Sstevel@tonic-gate 	} else if (lowest_lvl == lvl) {
12960Sstevel@tonic-gate 		pci_pwr_component_busy(ppb->ppb_pwr_p);
12970Sstevel@tonic-gate 	} else {
12980Sstevel@tonic-gate 		pci_pwr_component_idle(ppb->ppb_pwr_p);
12990Sstevel@tonic-gate 	}
13000Sstevel@tonic-gate 
13011624Spjha 	if ((pmcsr = PCI_CAP_GET16(ppb->ppb_conf_hdl, NULL,
13021774Spjha 		ppb->ppb_pm_cap_ptr, PCI_PMCSR)) == PCI_CAP_EINVAL16)
13031624Spjha 		return (DDI_FAILURE);
13040Sstevel@tonic-gate 
13050Sstevel@tonic-gate 	/*
13060Sstevel@tonic-gate 	 * Save the current power level.  This is the actual function level,
13070Sstevel@tonic-gate 	 * not the translated bridge level stored in pwr_p->current_lvl
13080Sstevel@tonic-gate 	 */
13090Sstevel@tonic-gate 	old_lvl = pmcsr & PCI_PMCSR_STATE_MASK;
13100Sstevel@tonic-gate 
13110Sstevel@tonic-gate 	pmcsr &= ~PCI_PMCSR_STATE_MASK;
13120Sstevel@tonic-gate 	switch (lvl) {
13130Sstevel@tonic-gate 	case PM_LEVEL_B0:
13140Sstevel@tonic-gate 		str = "PM_LEVEL_B0 (full speed)";
13150Sstevel@tonic-gate 		pmcsr |= PCI_PMCSR_D0;
13160Sstevel@tonic-gate 		break;
13170Sstevel@tonic-gate 	case PM_LEVEL_B1:
13180Sstevel@tonic-gate 		str = "PM_LEVEL_B1 (light sleep. No bus traffic allowed)";
13190Sstevel@tonic-gate 		if ((ppb->ppb_pwr_p->pwr_flags & PCI_PWR_B1_CAPABLE) == 0) {
13200Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s%d PCI PM state B1 not supported",
13210Sstevel@tonic-gate 			    ddi_driver_name(dip), ddi_get_instance(dip));
13220Sstevel@tonic-gate 
13230Sstevel@tonic-gate 			mutex_exit(&ppb->ppb_pwr_p->pwr_mutex);
13240Sstevel@tonic-gate 			return (DDI_FAILURE);
13250Sstevel@tonic-gate 		}
13260Sstevel@tonic-gate 		pmcsr |= PCI_PMCSR_D1;
13270Sstevel@tonic-gate 		break;
13280Sstevel@tonic-gate 	case PM_LEVEL_B2:
13290Sstevel@tonic-gate 		str = "PM_LEVEL_B2 (clock off)";
13300Sstevel@tonic-gate 		if ((ppb->ppb_pwr_p->pwr_flags & PCI_PWR_B2_CAPABLE) == 0) {
13310Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s%d PM state B2 not supported...",
13320Sstevel@tonic-gate 			    ddi_driver_name(dip),
13330Sstevel@tonic-gate 			    ddi_get_instance(dip));
13340Sstevel@tonic-gate 			mutex_exit(&ppb->ppb_pwr_p->pwr_mutex);
13350Sstevel@tonic-gate 
13360Sstevel@tonic-gate 			return (DDI_FAILURE);
13370Sstevel@tonic-gate 		}
13380Sstevel@tonic-gate 
13390Sstevel@tonic-gate 		if ((ppb->ppb_pwr_p->pwr_flags & PCI_PWR_B3_CAPABLE) == 0) {
13400Sstevel@tonic-gate 			/*
13410Sstevel@tonic-gate 			 * If B3 isn't supported, use D3 for B2 to avoid the
13420Sstevel@tonic-gate 			 * possible case that D2 for B2 isn't supported.
13430Sstevel@tonic-gate 			 * Saves and extra check and state flag..
13440Sstevel@tonic-gate 			 */
13450Sstevel@tonic-gate 			pmcsr |= PCI_PMCSR_D3HOT;
13460Sstevel@tonic-gate 		} else {
13470Sstevel@tonic-gate 			pmcsr |= PCI_PMCSR_D2;
13480Sstevel@tonic-gate 		}
13490Sstevel@tonic-gate 		break;
13500Sstevel@tonic-gate 	case PM_LEVEL_B3:
13510Sstevel@tonic-gate 		str = "PM_LEVEL_B30 (clock and power off)";
13520Sstevel@tonic-gate 		if ((ppb->ppb_pwr_p->pwr_flags & PCI_PWR_B3_CAPABLE) == 0) {
13530Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s%d PM state B3 not supported...",
13540Sstevel@tonic-gate 			    ddi_driver_name(dip),
13550Sstevel@tonic-gate 			    ddi_get_instance(dip));
13560Sstevel@tonic-gate 			mutex_exit(&ppb->ppb_pwr_p->pwr_mutex);
13570Sstevel@tonic-gate 
13580Sstevel@tonic-gate 			return (DDI_FAILURE);
13590Sstevel@tonic-gate 		}
13600Sstevel@tonic-gate 		pmcsr |= PCI_PMCSR_D3HOT;
13610Sstevel@tonic-gate 
13620Sstevel@tonic-gate 		break;
13630Sstevel@tonic-gate 
13640Sstevel@tonic-gate 	default:
13650Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d Unknown PM state %d",
13660Sstevel@tonic-gate 		    ddi_driver_name(dip), ddi_get_instance(dip), lvl);
13670Sstevel@tonic-gate 		mutex_exit(&ppb->ppb_pwr_p->pwr_mutex);
13680Sstevel@tonic-gate 
13690Sstevel@tonic-gate 		return (DDI_FAILURE);
13700Sstevel@tonic-gate 	}
13710Sstevel@tonic-gate 
13720Sstevel@tonic-gate 	new_lvl = pmcsr & PCI_PMCSR_STATE_MASK;
13730Sstevel@tonic-gate 
13740Sstevel@tonic-gate 	/*
13750Sstevel@tonic-gate 	 * Save config regs if going into HW state D3 (B2 or B3)
13760Sstevel@tonic-gate 	 */
13770Sstevel@tonic-gate 	if ((old_lvl != PCI_PMCSR_D3HOT) && (new_lvl == PCI_PMCSR_D3HOT)) {
13780Sstevel@tonic-gate 		DEBUG0(DBG_PWR, dip, "ppb_pwr(): SAVING CONFIG REGS\n");
13790Sstevel@tonic-gate 		if (pci_save_config_regs(dip) != DDI_SUCCESS) {
13800Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s%d Save config regs failed",
13810Sstevel@tonic-gate 				ddi_driver_name(dip), ddi_get_instance(dip));
13820Sstevel@tonic-gate 			mutex_exit(&ppb->ppb_pwr_p->pwr_mutex);
13830Sstevel@tonic-gate 
13840Sstevel@tonic-gate 			return (DDI_FAILURE);
13850Sstevel@tonic-gate 		}
13860Sstevel@tonic-gate 	}
13870Sstevel@tonic-gate 
13881624Spjha 	PCI_CAP_PUT16(ppb->ppb_conf_hdl, NULL, ppb->ppb_pm_cap_ptr, PCI_PMCSR,
13891624Spjha 		pmcsr);
13900Sstevel@tonic-gate 
13910Sstevel@tonic-gate 	/*
13920Sstevel@tonic-gate 	 * No bus transactions should occur without waiting for
13930Sstevel@tonic-gate 	 * settle time specified in PCI PM spec rev 2.1 sec 5.6.1
13940Sstevel@tonic-gate 	 * To make things simple, just use the max time specified for
13950Sstevel@tonic-gate 	 * all state transitions.
13960Sstevel@tonic-gate 	 */
13970Sstevel@tonic-gate 	delay(drv_usectohz(PCI_CLK_SETTLE_TIME));
13980Sstevel@tonic-gate 
13990Sstevel@tonic-gate 	/*
14000Sstevel@tonic-gate 	 * Restore configuration registers if coming out of HW state D3
14010Sstevel@tonic-gate 	 */
14020Sstevel@tonic-gate 	if ((old_lvl == PCI_PMCSR_D3HOT) && (new_lvl != PCI_PMCSR_D3HOT)) {
14030Sstevel@tonic-gate 		DEBUG0(DBG_PWR, dip, "ppb_pwr(): RESTORING CONFIG REGS\n");
14040Sstevel@tonic-gate 		if (pci_restore_config_regs(dip) != DDI_SUCCESS) {
14050Sstevel@tonic-gate 			panic("%s%d restore config regs failed",
14060Sstevel@tonic-gate 			    ddi_driver_name(dip), ddi_get_instance(dip));
14070Sstevel@tonic-gate 		}
14080Sstevel@tonic-gate 		/*NOTREACHED*/
14090Sstevel@tonic-gate 	}
14100Sstevel@tonic-gate 
14110Sstevel@tonic-gate 	ppb->ppb_pwr_p->current_lvl = lvl;
14120Sstevel@tonic-gate 
14130Sstevel@tonic-gate 	mutex_exit(&ppb->ppb_pwr_p->pwr_mutex);
14140Sstevel@tonic-gate 
14150Sstevel@tonic-gate 	DEBUG1(DBG_PWR, dip, "ppb_set_pwr: set PM state to %s\n\n", str);
14160Sstevel@tonic-gate 
14170Sstevel@tonic-gate 	return (DDI_SUCCESS);
14180Sstevel@tonic-gate }
14190Sstevel@tonic-gate 
14200Sstevel@tonic-gate /*
14210Sstevel@tonic-gate  * Initialize hotplug framework if we are hotpluggable.
14220Sstevel@tonic-gate  * Sets flag in the soft state if Hot Plug is supported and initialized
14230Sstevel@tonic-gate  * properly.
14240Sstevel@tonic-gate  */
14250Sstevel@tonic-gate /*ARGSUSED*/
14260Sstevel@tonic-gate static void
14270Sstevel@tonic-gate ppb_init_hotplug(ppb_devstate_t *ppb)
14280Sstevel@tonic-gate {
14290Sstevel@tonic-gate 	if (ddi_prop_exists(DDI_DEV_T_ANY, ppb->dip, DDI_PROP_DONTPASS,
14300Sstevel@tonic-gate 	    "hotplug-capable")) {
14310Sstevel@tonic-gate 		(void) modload("misc", "pcihp");
14320Sstevel@tonic-gate 
14330Sstevel@tonic-gate 		if (pcihp_init(ppb->dip) != DDI_SUCCESS) {
14340Sstevel@tonic-gate 			cmn_err(CE_WARN,
14350Sstevel@tonic-gate 			    "%s #%d: Failed setting hotplug framework",
14360Sstevel@tonic-gate 			    ddi_driver_name(ppb->dip),
14370Sstevel@tonic-gate 			    ddi_get_instance(ppb->dip));
14380Sstevel@tonic-gate 		} else
14390Sstevel@tonic-gate 			ppb->hotplug_capable = B_TRUE;
14400Sstevel@tonic-gate 	}
14410Sstevel@tonic-gate 
14420Sstevel@tonic-gate }
14430Sstevel@tonic-gate 
14440Sstevel@tonic-gate static void
14450Sstevel@tonic-gate ppb_create_ranges_prop(dev_info_t *dip,
14460Sstevel@tonic-gate 	ddi_acc_handle_t config_handle)
14470Sstevel@tonic-gate {
14480Sstevel@tonic-gate 	uint32_t base, limit;
14490Sstevel@tonic-gate 	ppb_ranges_t	ranges[PPB_RANGE_LEN];
14500Sstevel@tonic-gate 	uint8_t io_base_lo, io_limit_lo;
14510Sstevel@tonic-gate 	uint16_t io_base_hi, io_limit_hi, mem_base, mem_limit;
14520Sstevel@tonic-gate 	int i = 0, rangelen = sizeof (ppb_ranges_t)/sizeof (int);
14530Sstevel@tonic-gate 
14540Sstevel@tonic-gate 	io_base_lo = pci_config_get8(config_handle, PCI_BCNF_IO_BASE_LOW);
14550Sstevel@tonic-gate 	io_limit_lo = pci_config_get8(config_handle, PCI_BCNF_IO_LIMIT_LOW);
14560Sstevel@tonic-gate 	io_base_hi = pci_config_get16(config_handle, PCI_BCNF_IO_BASE_HI);
14570Sstevel@tonic-gate 	io_limit_hi = pci_config_get16(config_handle, PCI_BCNF_IO_LIMIT_HI);
14580Sstevel@tonic-gate 	mem_base = pci_config_get16(config_handle, PCI_BCNF_MEM_BASE);
14590Sstevel@tonic-gate 	mem_limit = pci_config_get16(config_handle, PCI_BCNF_MEM_LIMIT);
14600Sstevel@tonic-gate 
14610Sstevel@tonic-gate 	/*
14620Sstevel@tonic-gate 	 * Create ranges for IO space
14630Sstevel@tonic-gate 	 */
14640Sstevel@tonic-gate 	ranges[i].size_low = ranges[i].size_high = 0;
14650Sstevel@tonic-gate 	ranges[i].parent_mid = ranges[i].child_mid =
14660Sstevel@tonic-gate 		ranges[i].parent_high = 0;
14670Sstevel@tonic-gate 	ranges[i].child_high = ranges[i].parent_high |=
14680Sstevel@tonic-gate 		(PCI_REG_REL_M | PCI_ADDR_IO);
14690Sstevel@tonic-gate 	base = PPB_16bit_IOADDR(io_base_lo);
14700Sstevel@tonic-gate 	limit = PPB_16bit_IOADDR(io_limit_lo);
14710Sstevel@tonic-gate 
14720Sstevel@tonic-gate 	if ((io_base_lo & 0xf) == PPB_32BIT_IO) {
14730Sstevel@tonic-gate 		base = PPB_LADDR(base, io_base_hi);
14740Sstevel@tonic-gate 	}
14750Sstevel@tonic-gate 	if ((io_limit_lo & 0xf) == PPB_32BIT_IO) {
14760Sstevel@tonic-gate 		limit = PPB_LADDR(limit, io_limit_hi);
14770Sstevel@tonic-gate 	}
14780Sstevel@tonic-gate 
14790Sstevel@tonic-gate 	if ((io_base_lo & PPB_32BIT_IO) && (io_limit_hi > 0)) {
14800Sstevel@tonic-gate 		base = PPB_LADDR(base, io_base_hi);
14810Sstevel@tonic-gate 		limit = PPB_LADDR(limit, io_limit_hi);
14820Sstevel@tonic-gate 	}
14830Sstevel@tonic-gate 
14840Sstevel@tonic-gate 	/*
14850Sstevel@tonic-gate 	 * Create ranges for 32bit memory space
14860Sstevel@tonic-gate 	 */
14870Sstevel@tonic-gate 	base = PPB_32bit_MEMADDR(mem_base);
14880Sstevel@tonic-gate 	limit = PPB_32bit_MEMADDR(mem_limit);
14890Sstevel@tonic-gate 	ranges[i].size_low = ranges[i].size_high = 0;
14900Sstevel@tonic-gate 	ranges[i].parent_mid = ranges[i].child_mid =
14910Sstevel@tonic-gate 		ranges[i].parent_high = 0;
14920Sstevel@tonic-gate 	ranges[i].child_high = ranges[i].parent_high |=
14930Sstevel@tonic-gate 		(PCI_REG_REL_M | PCI_ADDR_MEM32);
14940Sstevel@tonic-gate 	ranges[i].child_low = ranges[i].parent_low = base;
14950Sstevel@tonic-gate 	if (limit >= base) {
14960Sstevel@tonic-gate 		ranges[i].size_low = limit - base + PPB_MEMGRAIN;
14970Sstevel@tonic-gate 		i++;
14980Sstevel@tonic-gate 	}
14990Sstevel@tonic-gate 
15000Sstevel@tonic-gate 	if (i) {
15010Sstevel@tonic-gate 		(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, dip, "ranges",
15020Sstevel@tonic-gate 		    (int *)ranges, i * rangelen);
15030Sstevel@tonic-gate 	}
15040Sstevel@tonic-gate }
15050Sstevel@tonic-gate 
15060Sstevel@tonic-gate /* ARGSUSED */
15070Sstevel@tonic-gate static int
15080Sstevel@tonic-gate ppb_open(dev_t *devp, int flags, int otyp, cred_t *credp)
15090Sstevel@tonic-gate {
15100Sstevel@tonic-gate 	ppb_devstate_t *ppb_p;
15110Sstevel@tonic-gate 	minor_t		minor = getminor(*devp);
15120Sstevel@tonic-gate 	int		instance = PCIHP_AP_MINOR_NUM_TO_INSTANCE(minor);
15130Sstevel@tonic-gate 
15140Sstevel@tonic-gate 	/*
15150Sstevel@tonic-gate 	 * Make sure the open is for the right file type.
15160Sstevel@tonic-gate 	 */
15170Sstevel@tonic-gate 	if (otyp != OTYP_CHR)
15180Sstevel@tonic-gate 		return (EINVAL);
15190Sstevel@tonic-gate 
15200Sstevel@tonic-gate 	/*
15210Sstevel@tonic-gate 	 * Get the soft state structure for the device.
15220Sstevel@tonic-gate 	 */
15230Sstevel@tonic-gate 	ppb_p = (ppb_devstate_t *)ddi_get_soft_state(ppb_state,
15240Sstevel@tonic-gate 	    instance);
15250Sstevel@tonic-gate 
15260Sstevel@tonic-gate 	if (ppb_p == NULL)
15270Sstevel@tonic-gate 		return (ENXIO);
15280Sstevel@tonic-gate 
15290Sstevel@tonic-gate 	if (ppb_p->hotplug_capable == B_TRUE)
15300Sstevel@tonic-gate 		return ((pcihp_get_cb_ops())->cb_open(devp, flags,
15310Sstevel@tonic-gate 		    otyp, credp));
15320Sstevel@tonic-gate 
15330Sstevel@tonic-gate 	/*
15340Sstevel@tonic-gate 	 * Handle the open by tracking the device state.
15350Sstevel@tonic-gate 	 */
15360Sstevel@tonic-gate 	mutex_enter(&ppb_p->ppb_mutex);
15370Sstevel@tonic-gate 	if (flags & FEXCL) {
15380Sstevel@tonic-gate 		if (ppb_p->ppb_soft_state != PPB_SOFT_STATE_CLOSED) {
15390Sstevel@tonic-gate 			mutex_exit(&ppb_p->ppb_mutex);
15400Sstevel@tonic-gate 			return (EBUSY);
15410Sstevel@tonic-gate 		}
15420Sstevel@tonic-gate 		ppb_p->ppb_soft_state = PPB_SOFT_STATE_OPEN_EXCL;
15430Sstevel@tonic-gate 	} else {
15440Sstevel@tonic-gate 		if (ppb_p->ppb_soft_state == PPB_SOFT_STATE_OPEN_EXCL) {
15450Sstevel@tonic-gate 			mutex_exit(&ppb_p->ppb_mutex);
15460Sstevel@tonic-gate 			return (EBUSY);
15470Sstevel@tonic-gate 		}
15480Sstevel@tonic-gate 		ppb_p->ppb_soft_state = PPB_SOFT_STATE_OPEN;
15490Sstevel@tonic-gate 	}
15500Sstevel@tonic-gate 	mutex_exit(&ppb_p->ppb_mutex);
15510Sstevel@tonic-gate 	return (0);
15520Sstevel@tonic-gate }
15530Sstevel@tonic-gate 
15540Sstevel@tonic-gate 
15550Sstevel@tonic-gate /* ARGSUSED */
15560Sstevel@tonic-gate static int
15570Sstevel@tonic-gate ppb_close(dev_t dev, int flags, int otyp, cred_t *credp)
15580Sstevel@tonic-gate {
15590Sstevel@tonic-gate 	ppb_devstate_t *ppb_p;
15600Sstevel@tonic-gate 	minor_t		minor = getminor(dev);
15610Sstevel@tonic-gate 	int		instance = PCIHP_AP_MINOR_NUM_TO_INSTANCE(minor);
15620Sstevel@tonic-gate 
15630Sstevel@tonic-gate 	if (otyp != OTYP_CHR)
15640Sstevel@tonic-gate 		return (EINVAL);
15650Sstevel@tonic-gate 
15660Sstevel@tonic-gate 	ppb_p = (ppb_devstate_t *)ddi_get_soft_state(ppb_state,
15670Sstevel@tonic-gate 	    instance);
15680Sstevel@tonic-gate 
15690Sstevel@tonic-gate 	if (ppb_p == NULL)
15700Sstevel@tonic-gate 		return (ENXIO);
15710Sstevel@tonic-gate 
15720Sstevel@tonic-gate 	if (ppb_p->hotplug_capable == B_TRUE)
15730Sstevel@tonic-gate 		return ((pcihp_get_cb_ops())->cb_close(dev, flags,
15740Sstevel@tonic-gate 		    otyp, credp));
15750Sstevel@tonic-gate 
15760Sstevel@tonic-gate 	mutex_enter(&ppb_p->ppb_mutex);
15770Sstevel@tonic-gate 	ppb_p->ppb_soft_state = PPB_SOFT_STATE_CLOSED;
15780Sstevel@tonic-gate 	mutex_exit(&ppb_p->ppb_mutex);
15790Sstevel@tonic-gate 	return (0);
15800Sstevel@tonic-gate }
15810Sstevel@tonic-gate 
15820Sstevel@tonic-gate 
15830Sstevel@tonic-gate /*
15840Sstevel@tonic-gate  * ppb_ioctl: devctl hotplug controls
15850Sstevel@tonic-gate  */
15860Sstevel@tonic-gate /* ARGSUSED */
15870Sstevel@tonic-gate static int
15880Sstevel@tonic-gate ppb_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp,
15890Sstevel@tonic-gate 	int *rvalp)
15900Sstevel@tonic-gate {
15910Sstevel@tonic-gate 	ppb_devstate_t *ppb_p;
15920Sstevel@tonic-gate 	dev_info_t *self;
15930Sstevel@tonic-gate 	struct devctl_iocdata *dcp;
15940Sstevel@tonic-gate 	uint_t bus_state;
15950Sstevel@tonic-gate 	int rv = 0;
15960Sstevel@tonic-gate 	minor_t		minor = getminor(dev);
15970Sstevel@tonic-gate 	int		instance = PCIHP_AP_MINOR_NUM_TO_INSTANCE(minor);
15980Sstevel@tonic-gate 
15990Sstevel@tonic-gate 	ppb_p = (ppb_devstate_t *)ddi_get_soft_state(ppb_state,
16000Sstevel@tonic-gate 	    instance);
16010Sstevel@tonic-gate 
16020Sstevel@tonic-gate 	if (ppb_p == NULL)
16030Sstevel@tonic-gate 		return (ENXIO);
16040Sstevel@tonic-gate 
16050Sstevel@tonic-gate 	if (ppb_p->hotplug_capable == B_TRUE)
16060Sstevel@tonic-gate 		return ((pcihp_get_cb_ops())->cb_ioctl(dev, cmd,
16070Sstevel@tonic-gate 		    arg, mode, credp, rvalp));
16080Sstevel@tonic-gate 
16090Sstevel@tonic-gate 	self = ppb_p->dip;
16100Sstevel@tonic-gate 
16110Sstevel@tonic-gate 	/*
16120Sstevel@tonic-gate 	 * We can use the generic implementation for these ioctls
16130Sstevel@tonic-gate 	 */
16140Sstevel@tonic-gate 	switch (cmd) {
16150Sstevel@tonic-gate 	case DEVCTL_DEVICE_GETSTATE:
16160Sstevel@tonic-gate 	case DEVCTL_DEVICE_ONLINE:
16170Sstevel@tonic-gate 	case DEVCTL_DEVICE_OFFLINE:
16180Sstevel@tonic-gate 	case DEVCTL_BUS_GETSTATE:
16190Sstevel@tonic-gate 		return (ndi_devctl_ioctl(self, cmd, arg, mode, 0));
16200Sstevel@tonic-gate 	}
16210Sstevel@tonic-gate 
16220Sstevel@tonic-gate 	/*
16230Sstevel@tonic-gate 	 * read devctl ioctl data
16240Sstevel@tonic-gate 	 */
16250Sstevel@tonic-gate 	if (ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS)
16260Sstevel@tonic-gate 		return (EFAULT);
16270Sstevel@tonic-gate 
16280Sstevel@tonic-gate 	switch (cmd) {
16290Sstevel@tonic-gate 
16300Sstevel@tonic-gate 	case DEVCTL_DEVICE_RESET:
16310Sstevel@tonic-gate 		rv = ENOTSUP;
16320Sstevel@tonic-gate 		break;
16330Sstevel@tonic-gate 
16340Sstevel@tonic-gate 	case DEVCTL_BUS_QUIESCE:
16350Sstevel@tonic-gate 		if (ndi_get_bus_state(self, &bus_state) == NDI_SUCCESS)
16360Sstevel@tonic-gate 			if (bus_state == BUS_QUIESCED)
16370Sstevel@tonic-gate 				break;
16380Sstevel@tonic-gate 		(void) ndi_set_bus_state(self, BUS_QUIESCED);
16390Sstevel@tonic-gate 		break;
16400Sstevel@tonic-gate 
16410Sstevel@tonic-gate 	case DEVCTL_BUS_UNQUIESCE:
16420Sstevel@tonic-gate 		if (ndi_get_bus_state(self, &bus_state) == NDI_SUCCESS)
16430Sstevel@tonic-gate 			if (bus_state == BUS_ACTIVE)
16440Sstevel@tonic-gate 				break;
16450Sstevel@tonic-gate 		(void) ndi_set_bus_state(self, BUS_ACTIVE);
16460Sstevel@tonic-gate 		break;
16470Sstevel@tonic-gate 
16480Sstevel@tonic-gate 	case DEVCTL_BUS_RESET:
16490Sstevel@tonic-gate 		rv = ENOTSUP;
16500Sstevel@tonic-gate 		break;
16510Sstevel@tonic-gate 
16520Sstevel@tonic-gate 	case DEVCTL_BUS_RESETALL:
16530Sstevel@tonic-gate 		rv = ENOTSUP;
16540Sstevel@tonic-gate 		break;
16550Sstevel@tonic-gate 
16560Sstevel@tonic-gate 	default:
16570Sstevel@tonic-gate 		rv = ENOTTY;
16580Sstevel@tonic-gate 	}
16590Sstevel@tonic-gate 
16600Sstevel@tonic-gate 	ndi_dc_freehdl(dcp);
16610Sstevel@tonic-gate 	return (rv);
16620Sstevel@tonic-gate }
16630Sstevel@tonic-gate 
16640Sstevel@tonic-gate static int ppb_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op,
16650Sstevel@tonic-gate     int flags, char *name, caddr_t valuep, int *lengthp)
16660Sstevel@tonic-gate {
16670Sstevel@tonic-gate 	ppb_devstate_t *ppb_p;
16680Sstevel@tonic-gate 	minor_t		minor = getminor(dev);
16690Sstevel@tonic-gate 	int		instance = PCIHP_AP_MINOR_NUM_TO_INSTANCE(minor);
16700Sstevel@tonic-gate 
16710Sstevel@tonic-gate 	ppb_p = (ppb_devstate_t *)ddi_get_soft_state(ppb_state,
16720Sstevel@tonic-gate 	    instance);
16730Sstevel@tonic-gate 
16740Sstevel@tonic-gate 	if (ppb_p == NULL)
16750Sstevel@tonic-gate 		return (ENXIO);
16760Sstevel@tonic-gate 
16770Sstevel@tonic-gate 	if (ppb_p->hotplug_capable == B_TRUE)
16780Sstevel@tonic-gate 		return ((pcihp_get_cb_ops())->cb_prop_op(dev, dip, prop_op,
16790Sstevel@tonic-gate 		    flags, name, valuep, lengthp));
16800Sstevel@tonic-gate 
16810Sstevel@tonic-gate 	return (ddi_prop_op(dev, dip, prop_op, flags, name, valuep, lengthp));
16820Sstevel@tonic-gate }
16830Sstevel@tonic-gate 
16840Sstevel@tonic-gate /*
16850Sstevel@tonic-gate  * Initialize our FMA resources
16860Sstevel@tonic-gate  */
16870Sstevel@tonic-gate static void
16880Sstevel@tonic-gate ppb_fm_init(ppb_devstate_t *ppb_p)
16890Sstevel@tonic-gate {
1690*3270Set142600 	dev_info_t *root = ddi_root_node();
1691*3270Set142600 	dev_info_t *pdip;
1692*3270Set142600 	char *bus;
1693*3270Set142600 
16940Sstevel@tonic-gate 	ppb_p->fm_cap = DDI_FM_EREPORT_CAPABLE | DDI_FM_ERRCB_CAPABLE |
16950Sstevel@tonic-gate 		DDI_FM_ACCCHK_CAPABLE | DDI_FM_DMACHK_CAPABLE;
16960Sstevel@tonic-gate 
16970Sstevel@tonic-gate 	/*
16980Sstevel@tonic-gate 	 * Request our capability level and get our parents capability
16990Sstevel@tonic-gate 	 * and ibc.
17000Sstevel@tonic-gate 	 */
17010Sstevel@tonic-gate 	ddi_fm_init(ppb_p->dip, &ppb_p->fm_cap, &ppb_p->fm_ibc);
17020Sstevel@tonic-gate 	ASSERT((ppb_p->fm_cap & DDI_FM_EREPORT_CAPABLE) &&
17030Sstevel@tonic-gate 	    (ppb_p->fm_cap & DDI_FM_ERRCB_CAPABLE));
17040Sstevel@tonic-gate 
17050Sstevel@tonic-gate 	pci_ereport_setup(ppb_p->dip);
17060Sstevel@tonic-gate 
17070Sstevel@tonic-gate 	/*
17080Sstevel@tonic-gate 	 * Register error callback with our parent.
17090Sstevel@tonic-gate 	 */
17100Sstevel@tonic-gate 	ddi_fm_handler_register(ppb_p->dip, ppb_err_callback, NULL);
1711*3270Set142600 
1712*3270Set142600 	ppb_p->parent_bus = PCIE_PCIECAP_DEV_TYPE_PCI_DEV;
1713*3270Set142600 	for (pdip = ddi_get_parent(ppb_p->dip); pdip && (pdip != root) &&
1714*3270Set142600 	    (ppb_p->parent_bus != PCIE_PCIECAP_DEV_TYPE_PCIE_DEV);
1715*3270Set142600 	    pdip = ddi_get_parent(pdip)) {
1716*3270Set142600 		if (ddi_prop_lookup_string(DDI_DEV_T_ANY, pdip,
1717*3270Set142600 		    DDI_PROP_DONTPASS, "device_type", &bus) !=
1718*3270Set142600 		    DDI_PROP_SUCCESS)
1719*3270Set142600 			break;
1720*3270Set142600 
1721*3270Set142600 		if (strcmp(bus, "pciex") == 0)
1722*3270Set142600 			ppb_p->parent_bus = PCIE_PCIECAP_DEV_TYPE_PCIE_DEV;
1723*3270Set142600 
1724*3270Set142600 		ddi_prop_free(bus);
1725*3270Set142600 	}
17260Sstevel@tonic-gate }
17270Sstevel@tonic-gate 
17280Sstevel@tonic-gate /*
17290Sstevel@tonic-gate  * Breakdown our FMA resources
17300Sstevel@tonic-gate  */
17310Sstevel@tonic-gate static void
17320Sstevel@tonic-gate ppb_fm_fini(ppb_devstate_t *ppb_p)
17330Sstevel@tonic-gate {
17340Sstevel@tonic-gate 	/*
17350Sstevel@tonic-gate 	 * Clean up allocated fm structures
17360Sstevel@tonic-gate 	 */
17370Sstevel@tonic-gate 	ddi_fm_handler_unregister(ppb_p->dip);
17380Sstevel@tonic-gate 	pci_ereport_teardown(ppb_p->dip);
17390Sstevel@tonic-gate 	ddi_fm_fini(ppb_p->dip);
17400Sstevel@tonic-gate }
17410Sstevel@tonic-gate 
17420Sstevel@tonic-gate /*
17430Sstevel@tonic-gate  * Initialize FMA resources for children devices. Called when
17440Sstevel@tonic-gate  * child calls ddi_fm_init().
17450Sstevel@tonic-gate  */
17460Sstevel@tonic-gate /*ARGSUSED*/
17470Sstevel@tonic-gate static int
17480Sstevel@tonic-gate ppb_fm_init_child(dev_info_t *dip, dev_info_t *tdip, int cap,
17490Sstevel@tonic-gate 		ddi_iblock_cookie_t *ibc)
17500Sstevel@tonic-gate {
17510Sstevel@tonic-gate 	ppb_devstate_t *ppb_p = (ppb_devstate_t *)ddi_get_soft_state(ppb_state,
17520Sstevel@tonic-gate 			ddi_get_instance(dip));
17530Sstevel@tonic-gate 	*ibc = ppb_p->fm_ibc;
17540Sstevel@tonic-gate 	return (ppb_p->fm_cap);
17550Sstevel@tonic-gate }
17560Sstevel@tonic-gate 
17570Sstevel@tonic-gate /*
17580Sstevel@tonic-gate  * FMA registered error callback
17590Sstevel@tonic-gate  */
17600Sstevel@tonic-gate static int
17610Sstevel@tonic-gate ppb_err_callback(dev_info_t *dip, ddi_fm_error_t *derr, const void *impl_data)
17620Sstevel@tonic-gate {
1763*3270Set142600 	ppb_devstate_t *ppb_p = (ppb_devstate_t *)ddi_get_soft_state(ppb_state,
1764*3270Set142600 			ddi_get_instance(dip));
1765*3270Set142600 
1766*3270Set142600 	/*
1767*3270Set142600 	 * errors handled by SPARC PCI-E framework for PCIe platforms
1768*3270Set142600 	 */
1769*3270Set142600 	if (ppb_p->parent_bus == PCIE_PCIECAP_DEV_TYPE_PCIE_DEV)
1770*3270Set142600 		return (DDI_FM_OK);
1771*3270Set142600 
1772*3270Set142600 	/*
1773*3270Set142600 	 * do the following for SPARC PCI platforms
1774*3270Set142600 	 */
17750Sstevel@tonic-gate 	ASSERT(impl_data == NULL);
17761865Sdilpreet 	pci_ereport_post(dip, derr, NULL);
17771865Sdilpreet 	return (derr->fme_status);
17780Sstevel@tonic-gate }
17790Sstevel@tonic-gate 
17800Sstevel@tonic-gate static void
17810Sstevel@tonic-gate ppb_bus_enter(dev_info_t *dip, ddi_acc_handle_t handle)
17820Sstevel@tonic-gate {
17830Sstevel@tonic-gate 	i_ndi_busop_access_enter(dip, handle);
17840Sstevel@tonic-gate }
17850Sstevel@tonic-gate 
17860Sstevel@tonic-gate /* ARGSUSED */
17870Sstevel@tonic-gate static void
17880Sstevel@tonic-gate ppb_bus_exit(dev_info_t *dip, ddi_acc_handle_t handle)
17890Sstevel@tonic-gate {
17900Sstevel@tonic-gate 	i_ndi_busop_access_exit(dip, handle);
17910Sstevel@tonic-gate }
1792