xref: /onnv-gate/usr/src/uts/sun4u/io/pci/pci_pci.c (revision 10548:e1a70121b226)
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 /*
229921SKrishna.Elango@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*
280Sstevel@tonic-gate  *	Sun4u PCI to PCI bus bridge nexus driver
290Sstevel@tonic-gate  */
300Sstevel@tonic-gate 
310Sstevel@tonic-gate #include <sys/conf.h>
320Sstevel@tonic-gate #include <sys/kmem.h>
330Sstevel@tonic-gate #include <sys/debug.h>
340Sstevel@tonic-gate #include <sys/modctl.h>
350Sstevel@tonic-gate #include <sys/autoconf.h>
360Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
370Sstevel@tonic-gate #include <sys/ddi_subrdefs.h>
383274Set142600 #include <sys/pcie.h>
393274Set142600 #include <sys/pcie_impl.h>
401624Spjha #include <sys/pci_cap.h>
410Sstevel@tonic-gate #include <sys/pci/pci_nexus.h>
420Sstevel@tonic-gate #include <sys/pci/pci_regs.h>
430Sstevel@tonic-gate #include <sys/ddi.h>
440Sstevel@tonic-gate #include <sys/sunndi.h>
450Sstevel@tonic-gate #include <sys/sunddi.h>
460Sstevel@tonic-gate #include <sys/fm/protocol.h>
470Sstevel@tonic-gate #include <sys/ddifm.h>
480Sstevel@tonic-gate #include <sys/pci/pci_pwr.h>
490Sstevel@tonic-gate #include <sys/pci/pci_debug.h>
500Sstevel@tonic-gate #include <sys/hotplug/pci/pcihp.h>
510Sstevel@tonic-gate #include <sys/open.h>
520Sstevel@tonic-gate #include <sys/stat.h>
530Sstevel@tonic-gate #include <sys/file.h>
540Sstevel@tonic-gate 
550Sstevel@tonic-gate #define	NUM_LOGICAL_SLOTS	32
560Sstevel@tonic-gate 
570Sstevel@tonic-gate #define	PPB_RANGE_LEN 2
580Sstevel@tonic-gate 
590Sstevel@tonic-gate #define	PPB_32BIT_IO 1
600Sstevel@tonic-gate #define	PPB_32bit_MEM 1
610Sstevel@tonic-gate 
620Sstevel@tonic-gate #define	PPB_MEMGRAIN 0x100000
630Sstevel@tonic-gate #define	PPB_IOGRAIN 0x1000
640Sstevel@tonic-gate 
650Sstevel@tonic-gate #define	PPB_16bit_IOADDR(addr) ((uint16_t)(((uint8_t)(addr) & 0xF0) << 8))
660Sstevel@tonic-gate #define	PPB_LADDR(lo, hi) (((uint16_t)(hi) << 16) | (uint16_t)(lo))
670Sstevel@tonic-gate #define	PPB_32bit_MEMADDR(addr) (PPB_LADDR(0, ((uint16_t)(addr) & 0xFFF0)))
680Sstevel@tonic-gate 
690Sstevel@tonic-gate typedef struct	slot_table {
700Sstevel@tonic-gate 	uchar_t		bus_id[128];
710Sstevel@tonic-gate 	uchar_t		slot_name[32];
720Sstevel@tonic-gate 	uint8_t		device_no;
730Sstevel@tonic-gate 	uint8_t		phys_slot_num;
740Sstevel@tonic-gate } slot_table_t;
750Sstevel@tonic-gate 
760Sstevel@tonic-gate /*
770Sstevel@tonic-gate  * The variable controls the default setting of the command register
780Sstevel@tonic-gate  * for pci devices.  See ppb_initchild() for details.
790Sstevel@tonic-gate  */
800Sstevel@tonic-gate static ushort_t ppb_command_default = PCI_COMM_SERR_ENABLE |
810Sstevel@tonic-gate 					PCI_COMM_WAIT_CYC_ENAB |
820Sstevel@tonic-gate 					PCI_COMM_PARITY_DETECT |
830Sstevel@tonic-gate 					PCI_COMM_ME |
840Sstevel@tonic-gate 					PCI_COMM_MAE |
850Sstevel@tonic-gate 					PCI_COMM_IO;
860Sstevel@tonic-gate 
870Sstevel@tonic-gate static int ppb_bus_map(dev_info_t *, dev_info_t *, ddi_map_req_t *,
880Sstevel@tonic-gate 	off_t, off_t, caddr_t *);
890Sstevel@tonic-gate static int ppb_ctlops(dev_info_t *, dev_info_t *, ddi_ctl_enum_t,
900Sstevel@tonic-gate 	void *, void *);
910Sstevel@tonic-gate static int ppb_intr_ops(dev_info_t *dip, dev_info_t *rdip,
920Sstevel@tonic-gate 	ddi_intr_op_t intr_op, ddi_intr_handle_impl_t *hdlp, void *result);
930Sstevel@tonic-gate 
940Sstevel@tonic-gate /*
950Sstevel@tonic-gate  * fm_init busop to initialize our children
960Sstevel@tonic-gate  */
970Sstevel@tonic-gate static int ppb_fm_init_child(dev_info_t *dip, dev_info_t *tdip, int cap,
980Sstevel@tonic-gate 		ddi_iblock_cookie_t *ibc);
990Sstevel@tonic-gate static void ppb_bus_enter(dev_info_t *dip, ddi_acc_handle_t handle);
1000Sstevel@tonic-gate static void ppb_bus_exit(dev_info_t *dip, ddi_acc_handle_t handle);
1010Sstevel@tonic-gate static int ppb_bus_power(dev_info_t *dip, void *impl_arg, pm_bus_power_op_t op,
1020Sstevel@tonic-gate     void *arg, void *result);
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate struct bus_ops ppb_bus_ops = {
1050Sstevel@tonic-gate 	BUSO_REV,
1060Sstevel@tonic-gate 	ppb_bus_map,
1070Sstevel@tonic-gate 	0,
1080Sstevel@tonic-gate 	0,
1090Sstevel@tonic-gate 	0,
1100Sstevel@tonic-gate 	i_ddi_map_fault,
1110Sstevel@tonic-gate 	ddi_dma_map,
1120Sstevel@tonic-gate 	ddi_dma_allochdl,
1130Sstevel@tonic-gate 	ddi_dma_freehdl,
1140Sstevel@tonic-gate 	ddi_dma_bindhdl,
1150Sstevel@tonic-gate 	ddi_dma_unbindhdl,
1160Sstevel@tonic-gate 	ddi_dma_flush,
1170Sstevel@tonic-gate 	ddi_dma_win,
1180Sstevel@tonic-gate 	ddi_dma_mctl,
1190Sstevel@tonic-gate 	ppb_ctlops,
1200Sstevel@tonic-gate 	ddi_bus_prop_op,
1210Sstevel@tonic-gate 	ndi_busop_get_eventcookie,	/* (*bus_get_eventcookie)();    */
1220Sstevel@tonic-gate 	ndi_busop_add_eventcall,	/* (*bus_add_eventcall)();	*/
1230Sstevel@tonic-gate 	ndi_busop_remove_eventcall,	/* (*bus_remove_eventcall)();   */
1240Sstevel@tonic-gate 	ndi_post_event,			/* (*bus_post_event)();		*/
1250Sstevel@tonic-gate 	0,				/* (*bus_intr_ctl)();		*/
1260Sstevel@tonic-gate 	0,				/* (*bus_config)(); 		*/
1270Sstevel@tonic-gate 	0,				/* (*bus_unconfig)(); 		*/
1280Sstevel@tonic-gate 	ppb_fm_init_child,		/* (*bus_fm_init)(); 		*/
1290Sstevel@tonic-gate 	NULL,				/* (*bus_fm_fini)(); 		*/
1300Sstevel@tonic-gate 	ppb_bus_enter,			/* (*bus_enter)()		*/
1310Sstevel@tonic-gate 	ppb_bus_exit,			/* (*bus_exit)()		*/
1320Sstevel@tonic-gate 	ppb_bus_power,			/* (*bus_power)()		*/
1330Sstevel@tonic-gate 	ppb_intr_ops			/* (*bus_intr_op)(); 		*/
1340Sstevel@tonic-gate };
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate static int ppb_open(dev_t *devp, int flags, int otyp, cred_t *credp);
1370Sstevel@tonic-gate static int ppb_close(dev_t dev, int flags, int otyp, cred_t *credp);
1380Sstevel@tonic-gate static int ppb_ioctl(dev_t dev, int cmd, intptr_t arg, int mode,
1390Sstevel@tonic-gate 						cred_t *credp, int *rvalp);
1400Sstevel@tonic-gate static int ppb_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op,
1410Sstevel@tonic-gate     int flags, char *name, caddr_t valuep, int *lengthp);
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate static struct cb_ops ppb_cb_ops = {
1440Sstevel@tonic-gate 	ppb_open,			/* open */
1450Sstevel@tonic-gate 	ppb_close,			/* close */
1460Sstevel@tonic-gate 	nulldev,			/* strategy */
1470Sstevel@tonic-gate 	nulldev,			/* print */
1480Sstevel@tonic-gate 	nulldev,			/* dump */
1490Sstevel@tonic-gate 	nulldev,			/* read */
1500Sstevel@tonic-gate 	nulldev,			/* write */
1510Sstevel@tonic-gate 	ppb_ioctl,			/* ioctl */
1520Sstevel@tonic-gate 	nodev,				/* devmap */
1530Sstevel@tonic-gate 	nodev,				/* mmap */
1540Sstevel@tonic-gate 	nodev,				/* segmap */
1550Sstevel@tonic-gate 	nochpoll,			/* poll */
1560Sstevel@tonic-gate 	ppb_prop_op,			/* cb_prop_op */
1570Sstevel@tonic-gate 	NULL,				/* streamtab */
1580Sstevel@tonic-gate 	D_NEW | D_MP | D_HOTPLUG,	/* Driver compatibility flag */
1590Sstevel@tonic-gate 	CB_REV,				/* rev */
1600Sstevel@tonic-gate 	nodev,				/* int (*cb_aread)() */
1610Sstevel@tonic-gate 	nodev				/* int (*cb_awrite)() */
1620Sstevel@tonic-gate };
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate static int ppb_probe(dev_info_t *);
1650Sstevel@tonic-gate static int ppb_attach(dev_info_t *devi, ddi_attach_cmd_t cmd);
1660Sstevel@tonic-gate static int ppb_detach(dev_info_t *devi, ddi_detach_cmd_t cmd);
1670Sstevel@tonic-gate static int ppb_info(dev_info_t *dip, ddi_info_cmd_t infocmd,
1680Sstevel@tonic-gate     void *arg, void **result);
1690Sstevel@tonic-gate static int ppb_pwr(dev_info_t *dip, int component, int level);
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate struct dev_ops ppb_ops = {
1720Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev */
1730Sstevel@tonic-gate 	0,			/* refcnt  */
1740Sstevel@tonic-gate 	ppb_info,		/* info */
1750Sstevel@tonic-gate 	nulldev,		/* identify */
1760Sstevel@tonic-gate 	ppb_probe,		/* probe */
1770Sstevel@tonic-gate 	ppb_attach,		/* attach */
1780Sstevel@tonic-gate 	ppb_detach,		/* detach */
1790Sstevel@tonic-gate 	nulldev,		/* reset */
1800Sstevel@tonic-gate 	&ppb_cb_ops,		/* driver operations */
1810Sstevel@tonic-gate 	&ppb_bus_ops,		/* bus operations */
1827656SSherry.Moore@Sun.COM 	ppb_pwr,		/* power */
1837656SSherry.Moore@Sun.COM 	ddi_quiesce_not_needed,		/* quiesce */
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 */
1927656SSherry.Moore@Sun.COM 	"Standard PCI to PCI bridge nexus driver",
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;
2463274Set142600 
2479921SKrishna.Elango@Sun.COM 	uint16_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);
2953274Set142600 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,
4647656SSherry.Moore@Sun.COM 		    "ppb_attach(): this nexus %s hotplug slots\n",
4657656SSherry.Moore@Sun.COM 		    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 *)
4777656SSherry.Moore@Sun.COM 		    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 *)
5217656SSherry.Moore@Sun.COM 		    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;
5503274Set142600 	struct	attachspec *as;
5513274Set142600 	struct	detachspec *ds;
5523274Set142600 	int	totreg;
5533274Set142600 	ppb_devstate_t *ppb_p;
5543272Sdduvall 
5553274Set142600 	ppb_p = (ppb_devstate_t *)ddi_get_soft_state(ppb_state,
5563274Set142600 	    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:
5723274Set142600 		ppb_uninitchild((dev_info_t *)arg);
5733274Set142600 		return (DDI_SUCCESS);
5743274Set142600 
5753274Set142600 	case DDI_CTLOPS_ATTACH:
5763274Set142600 		if (!pcie_is_child(dip, rdip))
5773274Set142600 			return (DDI_SUCCESS);
5783274Set142600 
5793274Set142600 		as = (struct attachspec *)arg;
5803274Set142600 		if ((ppb_p->parent_bus == PCIE_PCIECAP_DEV_TYPE_PCIE_DEV) &&
5814005Skrishnae 		    (as->when == DDI_POST) && (as->result == DDI_SUCCESS))
5823756Skrishnae 			pf_init(rdip, ppb_p->fm_ibc, as->cmd);
5833274Set142600 
5843274Set142600 		return (DDI_SUCCESS);
5853274Set142600 
5863274Set142600 	case DDI_CTLOPS_DETACH:
5873274Set142600 		if (!pcie_is_child(dip, rdip))
5883274Set142600 			return (DDI_SUCCESS);
5893274Set142600 
5903274Set142600 		ds = (struct detachspec *)arg;
5913274Set142600 		if ((ppb_p->parent_bus == PCIE_PCIECAP_DEV_TYPE_PCIE_DEV) &&
5923274Set142600 		    (ds->when == DDI_PRE))
5933756Skrishnae 			pf_fini(rdip, ds->cmd);
5943274Set142600 
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,
6117656SSherry.Moore@Sun.COM 	    DDI_PROP_DONTPASS | DDI_PROP_CANSLEEP, "reg",
6127656SSherry.Moore@Sun.COM 	    (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 |
6257656SSherry.Moore@Sun.COM 		    ((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 
6544395Sgovinda 	if ((intr_op == DDI_INTROP_SUPPORTED_TYPES) ||
6554395Sgovinda 	    (hdlp->ih_type != DDI_INTR_TYPE_FIXED))
6560Sstevel@tonic-gate 		goto done;
6570Sstevel@tonic-gate 
6580Sstevel@tonic-gate 	/*
6590Sstevel@tonic-gate 	 * If the interrupt-map property is defined at this
6600Sstevel@tonic-gate 	 * node, it will have performed the interrupt
6610Sstevel@tonic-gate 	 * translation as part of the property, so no
6620Sstevel@tonic-gate 	 * rotation needs to be done.
6630Sstevel@tonic-gate 	 */
6640Sstevel@tonic-gate 	if (ddi_getproplen(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
6650Sstevel@tonic-gate 	    "interrupt-map", &len) == DDI_PROP_SUCCESS)
6660Sstevel@tonic-gate 		goto done;
6670Sstevel@tonic-gate 
6680Sstevel@tonic-gate 	cdip = get_my_childs_dip(dip, rdip);
6690Sstevel@tonic-gate 
6700Sstevel@tonic-gate 	/*
6710Sstevel@tonic-gate 	 * Use the devices reg property to determine its
6720Sstevel@tonic-gate 	 * PCI bus number and device number.
6730Sstevel@tonic-gate 	 */
674506Scth 	if (ddi_getlongprop(DDI_DEV_T_ANY, cdip, DDI_PROP_DONTPASS,
6750Sstevel@tonic-gate 	    "reg", (caddr_t)&pci_rp, &reglen) != DDI_SUCCESS)
6760Sstevel@tonic-gate 		return (DDI_FAILURE);
6770Sstevel@tonic-gate 
678693Sgovinda 	intr = hdlp->ih_vector;
6790Sstevel@tonic-gate 
6800Sstevel@tonic-gate 	/* Spin the interrupt */
6810Sstevel@tonic-gate 	d = PCI_REG_DEV_G(pci_rp[0].pci_phys_hi);
6820Sstevel@tonic-gate 
6830Sstevel@tonic-gate 	if ((intr >= PCI_INTA) && (intr <= PCI_INTD))
684693Sgovinda 		hdlp->ih_vector = ((intr - 1 + (d % 4)) % 4 + 1);
6850Sstevel@tonic-gate 	else
6860Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d: %s: PCI intr=%x out of range",
6870Sstevel@tonic-gate 		    ddi_driver_name(rdip), ddi_get_instance(rdip),
6880Sstevel@tonic-gate 		    ddi_driver_name(dip), intr);
6890Sstevel@tonic-gate 
6900Sstevel@tonic-gate 	kmem_free(pci_rp, reglen);
6910Sstevel@tonic-gate 
6920Sstevel@tonic-gate done:
6930Sstevel@tonic-gate 	/* Pass up the request to our parent. */
6940Sstevel@tonic-gate 	return (i_ddi_intr_ops(dip, rdip, intr_op, hdlp, result));
6950Sstevel@tonic-gate }
6960Sstevel@tonic-gate 
6970Sstevel@tonic-gate static int
6980Sstevel@tonic-gate ppb_bus_power(dev_info_t *dip, void *impl_arg, pm_bus_power_op_t op,
6990Sstevel@tonic-gate     void *arg, void *result)
7000Sstevel@tonic-gate {
7010Sstevel@tonic-gate 	ppb_devstate_t *ppb;
7020Sstevel@tonic-gate 
7030Sstevel@tonic-gate 	ppb = (ppb_devstate_t *)ddi_get_soft_state(ppb_state,
7040Sstevel@tonic-gate 	    ddi_get_instance(dip));
7050Sstevel@tonic-gate 
7060Sstevel@tonic-gate 	return (pci_pwr_ops(ppb->ppb_pwr_p, dip, impl_arg, op, arg, result));
7070Sstevel@tonic-gate }
7080Sstevel@tonic-gate 
7090Sstevel@tonic-gate 
7100Sstevel@tonic-gate /*
7110Sstevel@tonic-gate  * name_child
7120Sstevel@tonic-gate  *
7130Sstevel@tonic-gate  * This function is called from init_child to name a node. It is
7140Sstevel@tonic-gate  * also passed as a callback for node merging functions.
7150Sstevel@tonic-gate  *
7160Sstevel@tonic-gate  * return value: DDI_SUCCESS, DDI_FAILURE
7170Sstevel@tonic-gate  */
7180Sstevel@tonic-gate static int
7190Sstevel@tonic-gate ppb_name_child(dev_info_t *child, char *name, int namelen)
7200Sstevel@tonic-gate {
7210Sstevel@tonic-gate 	pci_regspec_t *pci_rp;
7220Sstevel@tonic-gate 	uint_t slot, func;
7230Sstevel@tonic-gate 	char **unit_addr;
7240Sstevel@tonic-gate 	uint_t n;
7250Sstevel@tonic-gate 
7260Sstevel@tonic-gate 	/*
7270Sstevel@tonic-gate 	 * Pseudo nodes indicate a prototype node with per-instance
7280Sstevel@tonic-gate 	 * properties to be merged into the real h/w device node.
7290Sstevel@tonic-gate 	 * The interpretation of the unit-address is DD[,F]
7300Sstevel@tonic-gate 	 * where DD is the device id and F is the function.
7310Sstevel@tonic-gate 	 */
7320Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node(child) == 0) {
7330Sstevel@tonic-gate 		if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, child,
7340Sstevel@tonic-gate 		    DDI_PROP_DONTPASS, "unit-address", &unit_addr, &n) !=
7350Sstevel@tonic-gate 		    DDI_PROP_SUCCESS) {
7360Sstevel@tonic-gate 			cmn_err(CE_WARN, "cannot name node from %s.conf",
7370Sstevel@tonic-gate 			    ddi_driver_name(child));
7380Sstevel@tonic-gate 			return (DDI_FAILURE);
7390Sstevel@tonic-gate 		}
7400Sstevel@tonic-gate 		if (n != 1 || *unit_addr == NULL || **unit_addr == 0) {
7410Sstevel@tonic-gate 			cmn_err(CE_WARN, "unit-address property in %s.conf"
7420Sstevel@tonic-gate 			    " not well-formed", ddi_driver_name(child));
7430Sstevel@tonic-gate 			ddi_prop_free(unit_addr);
7440Sstevel@tonic-gate 			return (DDI_FAILURE);
7450Sstevel@tonic-gate 		}
7460Sstevel@tonic-gate 		(void) snprintf(name, namelen, "%s", *unit_addr);
7470Sstevel@tonic-gate 		ddi_prop_free(unit_addr);
7480Sstevel@tonic-gate 		return (DDI_SUCCESS);
7490Sstevel@tonic-gate 	}
7500Sstevel@tonic-gate 
7510Sstevel@tonic-gate 	/*
7520Sstevel@tonic-gate 	 * Get the address portion of the node name based on
7530Sstevel@tonic-gate 	 * the function and device number.
7540Sstevel@tonic-gate 	 */
7550Sstevel@tonic-gate 	if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS,
7560Sstevel@tonic-gate 	    "reg", (int **)&pci_rp, &n) != DDI_SUCCESS) {
7570Sstevel@tonic-gate 		return (DDI_FAILURE);
7580Sstevel@tonic-gate 	}
7590Sstevel@tonic-gate 
7600Sstevel@tonic-gate 	slot = PCI_REG_DEV_G(pci_rp[0].pci_phys_hi);
7610Sstevel@tonic-gate 	func = PCI_REG_FUNC_G(pci_rp[0].pci_phys_hi);
7620Sstevel@tonic-gate 
7630Sstevel@tonic-gate 	if (func != 0)
7640Sstevel@tonic-gate 		(void) snprintf(name, namelen, "%x,%x", slot, func);
7650Sstevel@tonic-gate 	else
7660Sstevel@tonic-gate 		(void) snprintf(name, namelen, "%x", slot);
7670Sstevel@tonic-gate 
7680Sstevel@tonic-gate 	ddi_prop_free(pci_rp);
7690Sstevel@tonic-gate 	return (DDI_SUCCESS);
7700Sstevel@tonic-gate }
7710Sstevel@tonic-gate 
7720Sstevel@tonic-gate static int
7730Sstevel@tonic-gate ppb_initchild(dev_info_t *child)
7740Sstevel@tonic-gate {
7750Sstevel@tonic-gate 	char name[MAXNAMELEN];
7760Sstevel@tonic-gate 	ddi_acc_handle_t config_handle;
7770Sstevel@tonic-gate 	ushort_t command_preserve, command;
7780Sstevel@tonic-gate 	uint_t n;
7790Sstevel@tonic-gate 	ushort_t bcr;
7800Sstevel@tonic-gate 	uchar_t header_type;
7810Sstevel@tonic-gate 	uchar_t min_gnt, latency_timer;
7820Sstevel@tonic-gate 	ppb_devstate_t *ppb;
7830Sstevel@tonic-gate 
7840Sstevel@tonic-gate 	/*
7850Sstevel@tonic-gate 	 * Name the child
7860Sstevel@tonic-gate 	 */
7870Sstevel@tonic-gate 	if (ppb_name_child(child, name, MAXNAMELEN) != DDI_SUCCESS)
7880Sstevel@tonic-gate 		return (DDI_FAILURE);
7890Sstevel@tonic-gate 
7900Sstevel@tonic-gate 	ddi_set_name_addr(child, name);
7910Sstevel@tonic-gate 	ddi_set_parent_data(child, NULL);
7920Sstevel@tonic-gate 
7930Sstevel@tonic-gate 	/*
7940Sstevel@tonic-gate 	 * Pseudo nodes indicate a prototype node with per-instance
7950Sstevel@tonic-gate 	 * properties to be merged into the real h/w device node.
7960Sstevel@tonic-gate 	 * The interpretation of the unit-address is DD[,F]
7970Sstevel@tonic-gate 	 * where DD is the device id and F is the function.
7980Sstevel@tonic-gate 	 */
7990Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node(child) == 0) {
8000Sstevel@tonic-gate 		extern int pci_allow_pseudo_children;
8010Sstevel@tonic-gate 
8020Sstevel@tonic-gate 		/*
8030Sstevel@tonic-gate 		 * Try to merge the properties from this prototype
8040Sstevel@tonic-gate 		 * node into real h/w nodes.
8050Sstevel@tonic-gate 		 */
8060Sstevel@tonic-gate 		if (ndi_merge_node(child, ppb_name_child) == DDI_SUCCESS) {
8070Sstevel@tonic-gate 			/*
8080Sstevel@tonic-gate 			 * Merged ok - return failure to remove the node.
8090Sstevel@tonic-gate 			 */
8100Sstevel@tonic-gate 			ppb_removechild(child);
8110Sstevel@tonic-gate 			return (DDI_FAILURE);
8120Sstevel@tonic-gate 		}
8130Sstevel@tonic-gate 
8140Sstevel@tonic-gate 		/* workaround for ddivs to run under PCI */
8150Sstevel@tonic-gate 		if (pci_allow_pseudo_children)
8160Sstevel@tonic-gate 			return (DDI_SUCCESS);
8170Sstevel@tonic-gate 
8180Sstevel@tonic-gate 		/*
8190Sstevel@tonic-gate 		 * The child was not merged into a h/w node,
8200Sstevel@tonic-gate 		 * but there's not much we can do with it other
8210Sstevel@tonic-gate 		 * than return failure to cause the node to be removed.
8220Sstevel@tonic-gate 		 */
8230Sstevel@tonic-gate 		cmn_err(CE_WARN, "!%s@%s: %s.conf properties not merged",
8240Sstevel@tonic-gate 		    ddi_driver_name(child), ddi_get_name_addr(child),
8250Sstevel@tonic-gate 		    ddi_driver_name(child));
8260Sstevel@tonic-gate 		ppb_removechild(child);
8270Sstevel@tonic-gate 		return (DDI_NOT_WELL_FORMED);
8280Sstevel@tonic-gate 	}
8290Sstevel@tonic-gate 
8300Sstevel@tonic-gate 	ppb = (ppb_devstate_t *)ddi_get_soft_state(ppb_state,
8310Sstevel@tonic-gate 	    ddi_get_instance(ddi_get_parent(child)));
8320Sstevel@tonic-gate 
8333274Set142600 	ddi_set_parent_data(child, NULL);
8343274Set142600 
8350Sstevel@tonic-gate 	/*
8360Sstevel@tonic-gate 	 * If hardware is PM capable, set up the power info structure.
8370Sstevel@tonic-gate 	 * This also ensures the the bus will not be off (0MHz) otherwise
8380Sstevel@tonic-gate 	 * system panics during a bus access.
8390Sstevel@tonic-gate 	 */
8400Sstevel@tonic-gate 	if (PM_CAPABLE(ppb->ppb_pwr_p)) {
8410Sstevel@tonic-gate 		/*
8420Sstevel@tonic-gate 		 * Create a pwr_info struct for child.  Bus will be
8430Sstevel@tonic-gate 		 * at full speed after creating info.
8440Sstevel@tonic-gate 		 */
8450Sstevel@tonic-gate 		pci_pwr_create_info(ppb->ppb_pwr_p, child);
8460Sstevel@tonic-gate #ifdef DEBUG
8470Sstevel@tonic-gate 		ASSERT(ppb->ppb_pwr_p->current_lvl == PM_LEVEL_B0);
8480Sstevel@tonic-gate #endif
8490Sstevel@tonic-gate 	}
8500Sstevel@tonic-gate 
8510Sstevel@tonic-gate 	/*
8520Sstevel@tonic-gate 	 * If configuration registers were previously saved by
8530Sstevel@tonic-gate 	 * child (before it entered D3), then let the child do the
8540Sstevel@tonic-gate 	 * restore to set up the config regs as it'll first need to
8550Sstevel@tonic-gate 	 * power the device out of D3.
8560Sstevel@tonic-gate 	 */
8570Sstevel@tonic-gate 	if (ddi_prop_exists(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS,
8580Sstevel@tonic-gate 	    "config-regs-saved-by-child") == 1) {
8590Sstevel@tonic-gate 		DEBUG2(DBG_PWR, ddi_get_parent(child),
8607656SSherry.Moore@Sun.COM 		    "INITCHILD: config regs to be restored by child"
8617656SSherry.Moore@Sun.COM 		    " for %s@%s\n", ddi_node_name(child),
8627656SSherry.Moore@Sun.COM 		    ddi_get_name_addr(child));
8630Sstevel@tonic-gate 
8640Sstevel@tonic-gate 		return (DDI_SUCCESS);
8650Sstevel@tonic-gate 	}
8660Sstevel@tonic-gate 
8670Sstevel@tonic-gate 	DEBUG2(DBG_PWR, ddi_get_parent(child),
8680Sstevel@tonic-gate 	    "INITCHILD: config regs setup for %s@%s\n",
8690Sstevel@tonic-gate 	    ddi_node_name(child), ddi_get_name_addr(child));
8700Sstevel@tonic-gate 
8710Sstevel@tonic-gate 	if (pci_config_setup(child, &config_handle) != DDI_SUCCESS) {
8720Sstevel@tonic-gate 		if (PM_CAPABLE(ppb->ppb_pwr_p)) {
8730Sstevel@tonic-gate 			pci_pwr_rm_info(ppb->ppb_pwr_p, child);
8740Sstevel@tonic-gate 		}
8750Sstevel@tonic-gate 
8760Sstevel@tonic-gate 		return (DDI_FAILURE);
8770Sstevel@tonic-gate 	}
8780Sstevel@tonic-gate 
8790Sstevel@tonic-gate 	/*
8800Sstevel@tonic-gate 	 * Determine the configuration header type.
8810Sstevel@tonic-gate 	 */
8820Sstevel@tonic-gate 	header_type = pci_config_get8(config_handle, PCI_CONF_HEADER);
8830Sstevel@tonic-gate 
8840Sstevel@tonic-gate 	/*
8850Sstevel@tonic-gate 	 * Support for the "command-preserve" property.
8860Sstevel@tonic-gate 	 */
8870Sstevel@tonic-gate 	command_preserve = ddi_prop_get_int(DDI_DEV_T_ANY, child,
8887656SSherry.Moore@Sun.COM 	    DDI_PROP_DONTPASS, "command-preserve", 0);
8890Sstevel@tonic-gate 	command = pci_config_get16(config_handle, PCI_CONF_COMM);
8900Sstevel@tonic-gate 	command &= (command_preserve | PCI_COMM_BACK2BACK_ENAB);
8910Sstevel@tonic-gate 	command |= (ppb_command_default & ~command_preserve);
8920Sstevel@tonic-gate 	pci_config_put16(config_handle, PCI_CONF_COMM, command);
8930Sstevel@tonic-gate 
8940Sstevel@tonic-gate 	/*
8950Sstevel@tonic-gate 	 * If the device has a bus control register then program it
8960Sstevel@tonic-gate 	 * based on the settings in the command register.
8970Sstevel@tonic-gate 	 */
8983274Set142600 	if ((header_type  & PCI_HEADER_TYPE_M) == PCI_HEADER_ONE) {
8990Sstevel@tonic-gate 		bcr = pci_config_get8(config_handle, PCI_BCNF_BCNTRL);
9000Sstevel@tonic-gate 		if (ppb_command_default & PCI_COMM_PARITY_DETECT)
9010Sstevel@tonic-gate 			bcr |= PCI_BCNF_BCNTRL_PARITY_ENABLE;
9020Sstevel@tonic-gate 		if (ppb_command_default & PCI_COMM_SERR_ENABLE)
9030Sstevel@tonic-gate 			bcr |= PCI_BCNF_BCNTRL_SERR_ENABLE;
9040Sstevel@tonic-gate 		bcr |= PCI_BCNF_BCNTRL_MAST_AB_MODE;
9050Sstevel@tonic-gate 		pci_config_put8(config_handle, PCI_BCNF_BCNTRL, bcr);
9060Sstevel@tonic-gate 	}
9070Sstevel@tonic-gate 
9080Sstevel@tonic-gate 	/*
9090Sstevel@tonic-gate 	 * Initialize cache-line-size configuration register if needed.
9100Sstevel@tonic-gate 	 */
9110Sstevel@tonic-gate 	if (ppb_set_cache_line_size_register &&
9120Sstevel@tonic-gate 	    ddi_getprop(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS,
9137656SSherry.Moore@Sun.COM 	    "cache-line-size", 0) == 0) {
9140Sstevel@tonic-gate 		pci_config_put8(config_handle, PCI_CONF_CACHE_LINESZ,
9157656SSherry.Moore@Sun.COM 		    ppb->ppb_cache_line_size);
9160Sstevel@tonic-gate 		n = pci_config_get8(config_handle, PCI_CONF_CACHE_LINESZ);
9170Sstevel@tonic-gate 		if (n != 0) {
9180Sstevel@tonic-gate 			(void) ndi_prop_update_int(DDI_DEV_T_NONE, child,
9197656SSherry.Moore@Sun.COM 			    "cache-line-size", n);
9200Sstevel@tonic-gate 		}
9210Sstevel@tonic-gate 	}
9220Sstevel@tonic-gate 
9230Sstevel@tonic-gate 	/*
9240Sstevel@tonic-gate 	 * Initialize latency timer configuration registers if needed.
9250Sstevel@tonic-gate 	 */
9260Sstevel@tonic-gate 	if (ppb_set_latency_timer_register &&
9270Sstevel@tonic-gate 	    ddi_getprop(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS,
9287656SSherry.Moore@Sun.COM 	    "latency-timer", 0) == 0) {
9290Sstevel@tonic-gate 
9300Sstevel@tonic-gate 		if ((header_type & PCI_HEADER_TYPE_M) == PCI_HEADER_ONE) {
9310Sstevel@tonic-gate 			latency_timer = ppb->ppb_latency_timer;
9320Sstevel@tonic-gate 			pci_config_put8(config_handle, PCI_BCNF_LATENCY_TIMER,
9337656SSherry.Moore@Sun.COM 			    ppb->ppb_latency_timer);
9340Sstevel@tonic-gate 		} else {
9350Sstevel@tonic-gate 			min_gnt = pci_config_get8(config_handle,
9367656SSherry.Moore@Sun.COM 			    PCI_CONF_MIN_G);
9370Sstevel@tonic-gate 			latency_timer = min_gnt * 8;
9380Sstevel@tonic-gate 		}
9390Sstevel@tonic-gate 		pci_config_put8(config_handle, PCI_CONF_LATENCY_TIMER,
9407656SSherry.Moore@Sun.COM 		    latency_timer);
9410Sstevel@tonic-gate 		n = pci_config_get8(config_handle, PCI_CONF_LATENCY_TIMER);
9420Sstevel@tonic-gate 		if (n != 0) {
9430Sstevel@tonic-gate 			(void) ndi_prop_update_int(DDI_DEV_T_NONE, child,
9447656SSherry.Moore@Sun.COM 			    "latency-timer", n);
9450Sstevel@tonic-gate 		}
9460Sstevel@tonic-gate 	}
9470Sstevel@tonic-gate 
9480Sstevel@tonic-gate 	/*
9493274Set142600 	 * SPARC PCIe FMA specific
9503274Set142600 	 *
9513274Set142600 	 * Note: parent_data for parent is created only if this is sparc PCI-E
9523274Set142600 	 * platform, for which, SG take a different route to handle device
9533274Set142600 	 * errors.
9543274Set142600 	 */
9553274Set142600 	if (ppb->parent_bus == PCIE_PCIECAP_DEV_TYPE_PCIE_DEV) {
9566313Skrishnae 		if (pcie_init_bus(child) == NULL) {
9573724Srameshc 			pci_config_teardown(&config_handle);
9583274Set142600 			return (DDI_FAILURE);
9593724Srameshc 		}
9603274Set142600 	}
9613274Set142600 
9623274Set142600 	/*
9630Sstevel@tonic-gate 	 * Check to see if the XMITS/PCI-X workaround applies.
9640Sstevel@tonic-gate 	 */
9650Sstevel@tonic-gate 	n = ddi_getprop(DDI_DEV_T_ANY, child, DDI_PROP_NOTPROM,
9660Sstevel@tonic-gate 	    "pcix-update-cmd-reg", -1);
9670Sstevel@tonic-gate 
9680Sstevel@tonic-gate 	if (n != -1) {
9690Sstevel@tonic-gate 		extern void pcix_set_cmd_reg(dev_info_t *child, uint16_t value);
9700Sstevel@tonic-gate 		DEBUG1(DBG_INIT_CLD, child, "Turning on XMITS NCPQ "
9710Sstevel@tonic-gate 		    "Workaround: value = %x\n", n);
9720Sstevel@tonic-gate 		pcix_set_cmd_reg(child, n);
9730Sstevel@tonic-gate 	}
9743724Srameshc 	pci_config_teardown(&config_handle);
9753274Set142600 	return (DDI_SUCCESS);
9763274Set142600 }
9773274Set142600 
9783274Set142600 static void
9793274Set142600 ppb_uninitchild(dev_info_t *child)
9803274Set142600 {
9813274Set142600 	ppb_devstate_t *ppb;
9823274Set142600 
9833274Set142600 	ppb = (ppb_devstate_t *)ddi_get_soft_state(ppb_state,
9843274Set142600 	    ddi_get_instance(ddi_get_parent(child)));
9853156Sgirish 
9863156Sgirish 	/*
9873274Set142600 	 * SG OPL FMA specific
9883156Sgirish 	 */
9893274Set142600 	if (ppb->parent_bus == PCIE_PCIECAP_DEV_TYPE_PCIE_DEV)
9906313Skrishnae 		pcie_fini_bus(child);
9913156Sgirish 
9923274Set142600 	ppb_removechild(child);
9930Sstevel@tonic-gate }
9940Sstevel@tonic-gate 
9950Sstevel@tonic-gate static void
9960Sstevel@tonic-gate ppb_removechild(dev_info_t *dip)
9970Sstevel@tonic-gate {
9980Sstevel@tonic-gate 	ppb_devstate_t *ppb;
9990Sstevel@tonic-gate 
10000Sstevel@tonic-gate 	ppb = (ppb_devstate_t *)ddi_get_soft_state(ppb_state,
10010Sstevel@tonic-gate 	    ddi_get_instance(ddi_get_parent(dip)));
10020Sstevel@tonic-gate 
10030Sstevel@tonic-gate 	if (PM_CAPABLE(ppb->ppb_pwr_p)) {
10040Sstevel@tonic-gate 
10050Sstevel@tonic-gate 		DEBUG2(DBG_PWR, ddi_get_parent(dip),
10060Sstevel@tonic-gate 		    "UNINITCHILD: removing pwr_info for %s@%s\n",
10070Sstevel@tonic-gate 		    ddi_node_name(dip), ddi_get_name_addr(dip));
10080Sstevel@tonic-gate 		pci_pwr_rm_info(ppb->ppb_pwr_p, dip);
10090Sstevel@tonic-gate 	}
10100Sstevel@tonic-gate 
10110Sstevel@tonic-gate 	ddi_set_name_addr(dip, NULL);
10120Sstevel@tonic-gate 
10130Sstevel@tonic-gate 	/*
10140Sstevel@tonic-gate 	 * Strip the node to properly convert it back to prototype form
10150Sstevel@tonic-gate 	 */
10160Sstevel@tonic-gate 	ddi_remove_minor_node(dip, NULL);
10170Sstevel@tonic-gate 
10180Sstevel@tonic-gate 	impl_rem_dev_props(dip);
10190Sstevel@tonic-gate }
10200Sstevel@tonic-gate 
10210Sstevel@tonic-gate /*
10220Sstevel@tonic-gate  * If bridge is PM capable, set up PM state for nexus.
10230Sstevel@tonic-gate  */
10240Sstevel@tonic-gate static void
10250Sstevel@tonic-gate ppb_pwr_setup(ppb_devstate_t *ppb, dev_info_t *pdip)
10260Sstevel@tonic-gate {
10270Sstevel@tonic-gate 	char *comp_array[5];
10280Sstevel@tonic-gate 	int i;
10290Sstevel@tonic-gate 	ddi_acc_handle_t conf_hdl;
10300Sstevel@tonic-gate 	uint8_t pmcsr_bse;
10310Sstevel@tonic-gate 	uint16_t pmcap;
10320Sstevel@tonic-gate 
10330Sstevel@tonic-gate 	/*
10340Sstevel@tonic-gate 	 * Determine if bridge is PM capable.  If not, leave ppb_pwr_p NULL
10350Sstevel@tonic-gate 	 * and return.
10360Sstevel@tonic-gate 	 */
10370Sstevel@tonic-gate 	if (pci_config_setup(pdip, &ppb->ppb_conf_hdl) != DDI_SUCCESS) {
10380Sstevel@tonic-gate 
10390Sstevel@tonic-gate 		return;
10400Sstevel@tonic-gate 	}
10410Sstevel@tonic-gate 
10420Sstevel@tonic-gate 	conf_hdl = ppb->ppb_conf_hdl;
10430Sstevel@tonic-gate 
10440Sstevel@tonic-gate 	/*
10451624Spjha 	 * Locate and store the power management cap_ptr for future references.
10460Sstevel@tonic-gate 	 */
10471624Spjha 	if ((PCI_CAP_LOCATE(conf_hdl, PCI_CAP_ID_PM, &ppb->ppb_pm_cap_ptr))
10487656SSherry.Moore@Sun.COM 	    == DDI_FAILURE) {
10490Sstevel@tonic-gate 		DEBUG0(DBG_PWR, pdip, "bridge does not support PM. PCI"
10500Sstevel@tonic-gate 		    " PM data structure not found in config header\n");
10510Sstevel@tonic-gate 		pci_config_teardown(&conf_hdl);
10520Sstevel@tonic-gate 
10530Sstevel@tonic-gate 		return;
10540Sstevel@tonic-gate 	}
10550Sstevel@tonic-gate 
10560Sstevel@tonic-gate 	/*
10570Sstevel@tonic-gate 	 * Allocate PM state structure for ppb.
10580Sstevel@tonic-gate 	 */
10590Sstevel@tonic-gate 	ppb->ppb_pwr_p = (pci_pwr_t *)
10600Sstevel@tonic-gate 	    kmem_zalloc(sizeof (pci_pwr_t), KM_SLEEP);
10610Sstevel@tonic-gate 	ppb->ppb_pwr_p->pwr_fp = 0;
10620Sstevel@tonic-gate 
10631624Spjha 	pmcsr_bse = PCI_CAP_GET8(conf_hdl, NULL, ppb->ppb_pm_cap_ptr,
10647656SSherry.Moore@Sun.COM 	    PCI_PMCSR_BSE);
10650Sstevel@tonic-gate 
10661624Spjha 	pmcap = PCI_CAP_GET16(conf_hdl, NULL, ppb->ppb_pm_cap_ptr,
10677656SSherry.Moore@Sun.COM 	    PCI_PMCAP);
10680Sstevel@tonic-gate 
10691774Spjha 	if (pmcap == PCI_CAP_EINVAL16 || pmcsr_bse == PCI_CAP_EINVAL8) {
10701624Spjha 		pci_config_teardown(&conf_hdl);
10711624Spjha 		return;
10721624Spjha 	}
10730Sstevel@tonic-gate 
10740Sstevel@tonic-gate 	if (pmcap & PCI_PMCAP_D1) {
10750Sstevel@tonic-gate 		DEBUG0(DBG_PWR, pdip, "setup: B1 state supported\n");
10760Sstevel@tonic-gate 		ppb->ppb_pwr_p->pwr_flags |= PCI_PWR_B1_CAPABLE;
10770Sstevel@tonic-gate 	} else {
10780Sstevel@tonic-gate 		DEBUG0(DBG_PWR, pdip, "setup: B1 state NOT supported\n");
10790Sstevel@tonic-gate 	}
10800Sstevel@tonic-gate 	if (pmcap & PCI_PMCAP_D2) {
10810Sstevel@tonic-gate 		DEBUG0(DBG_PWR, pdip, "setup: B2 state supported\n");
10820Sstevel@tonic-gate 		ppb->ppb_pwr_p->pwr_flags |= PCI_PWR_B2_CAPABLE;
10830Sstevel@tonic-gate 	} else {
10840Sstevel@tonic-gate 		DEBUG0(DBG_PWR, pdip, "setup: B2 via D2 NOT supported\n");
10850Sstevel@tonic-gate 	}
10860Sstevel@tonic-gate 
10870Sstevel@tonic-gate 	if (pmcsr_bse & PCI_PMCSR_BSE_BPCC_EN) {
10880Sstevel@tonic-gate 		DEBUG0(DBG_PWR, pdip,
10891624Spjha 		"setup: bridge power/clock control enable\n");
10900Sstevel@tonic-gate 	} else {
10910Sstevel@tonic-gate 		DEBUG0(DBG_PWR, pdip,
10921624Spjha 		"setup: bridge power/clock control disabled\n");
10930Sstevel@tonic-gate 
10940Sstevel@tonic-gate 		kmem_free(ppb->ppb_pwr_p, sizeof (pci_pwr_t));
10950Sstevel@tonic-gate 		ppb->ppb_pwr_p = NULL;
10960Sstevel@tonic-gate 		pci_config_teardown(&conf_hdl);
10970Sstevel@tonic-gate 
10980Sstevel@tonic-gate 		return;
10990Sstevel@tonic-gate 	}
11000Sstevel@tonic-gate 
11010Sstevel@tonic-gate 	/*
11020Sstevel@tonic-gate 	 * PCI states D0 and D3 always are supported for normal PCI
11030Sstevel@tonic-gate 	 * devices.  D1 and D2 are optional which are checked for above.
11040Sstevel@tonic-gate 	 * Bridge function states D0-D3 correspond to secondary bus states
11050Sstevel@tonic-gate 	 * B0-B3, EXCEPT if PCI_PMCSR_BSE_B2_B3 is set.  In this case, setting
11060Sstevel@tonic-gate 	 * the bridge function to D3 will set the bridge bus to state B2 instead
11070Sstevel@tonic-gate 	 * of B3.  D2 will not correspond to B2 (and in fact, probably
11080Sstevel@tonic-gate 	 * won't be D2 capable).  Implicitly, this means that if
11090Sstevel@tonic-gate 	 * PCI_PMCSR_BSE_B2_B3 is set, the bus will not be B3 capable.
11100Sstevel@tonic-gate 	 */
11110Sstevel@tonic-gate 	if (pmcsr_bse & PCI_PMCSR_BSE_B2_B3) {
11120Sstevel@tonic-gate 		ppb->ppb_pwr_p->pwr_flags |= PCI_PWR_B2_CAPABLE;
11130Sstevel@tonic-gate 		DEBUG0(DBG_PWR, pdip, "B2 supported via D3\n");
11140Sstevel@tonic-gate 	} else {
11150Sstevel@tonic-gate 		ppb->ppb_pwr_p->pwr_flags |= PCI_PWR_B3_CAPABLE;
11160Sstevel@tonic-gate 		DEBUG0(DBG_PWR, pdip, "B3 supported via D3\n");
11170Sstevel@tonic-gate 	}
11180Sstevel@tonic-gate 
11190Sstevel@tonic-gate 	ppb->ppb_pwr_p->pwr_dip = pdip;
11200Sstevel@tonic-gate 	mutex_init(&ppb->ppb_pwr_p->pwr_mutex, NULL, MUTEX_DRIVER, NULL);
11210Sstevel@tonic-gate 
11220Sstevel@tonic-gate 	i = 0;
11230Sstevel@tonic-gate 	comp_array[i++] = "NAME=PCI bridge PM";
11240Sstevel@tonic-gate 	if (ppb->ppb_pwr_p->pwr_flags & PCI_PWR_B3_CAPABLE) {
11250Sstevel@tonic-gate 		comp_array[i++] = "0=Clock/Power Off (B3)";
11260Sstevel@tonic-gate 	}
11270Sstevel@tonic-gate 	if (ppb->ppb_pwr_p->pwr_flags & PCI_PWR_B2_CAPABLE) {
11280Sstevel@tonic-gate 		comp_array[i++] = "1=Clock Off (B2)";
11290Sstevel@tonic-gate 	}
11300Sstevel@tonic-gate 	if (ppb->ppb_pwr_p->pwr_flags & PCI_PWR_B1_CAPABLE) {
11310Sstevel@tonic-gate 		comp_array[i++] = "2=Bus Inactive (B1)";
11320Sstevel@tonic-gate 	}
11330Sstevel@tonic-gate 	comp_array[i++] = "3=Full Power (B0)";
11340Sstevel@tonic-gate 
11350Sstevel@tonic-gate 	/*
11360Sstevel@tonic-gate 	 * Create pm-components property. It does not already exist.
11370Sstevel@tonic-gate 	 */
11380Sstevel@tonic-gate 	if (ddi_prop_update_string_array(DDI_DEV_T_NONE, pdip,
11390Sstevel@tonic-gate 	    "pm-components", comp_array, i) != DDI_PROP_SUCCESS) {
11400Sstevel@tonic-gate 		cmn_err(CE_WARN,
11410Sstevel@tonic-gate 		    "%s%d pm-components prop update failed",
11420Sstevel@tonic-gate 		    ddi_driver_name(pdip), ddi_get_instance(pdip));
11430Sstevel@tonic-gate 		pci_config_teardown(&conf_hdl);
11440Sstevel@tonic-gate 		mutex_destroy(&ppb->ppb_pwr_p->pwr_mutex);
11450Sstevel@tonic-gate 		kmem_free(ppb->ppb_pwr_p, sizeof (pci_pwr_t));
11460Sstevel@tonic-gate 		ppb->ppb_pwr_p = NULL;
11470Sstevel@tonic-gate 
11480Sstevel@tonic-gate 		return;
11490Sstevel@tonic-gate 	}
11500Sstevel@tonic-gate 
11510Sstevel@tonic-gate 	if (ddi_prop_create(DDI_DEV_T_NONE, pdip, DDI_PROP_CANSLEEP,
11520Sstevel@tonic-gate 	    "pm-want-child-notification?", NULL, NULL) != DDI_PROP_SUCCESS) {
11530Sstevel@tonic-gate 		cmn_err(CE_WARN,
11547656SSherry.Moore@Sun.COM 		    "%s%d fail to create pm-want-child-notification? prop",
11557656SSherry.Moore@Sun.COM 		    ddi_driver_name(pdip), ddi_get_instance(pdip));
11560Sstevel@tonic-gate 
11570Sstevel@tonic-gate 		(void) ddi_prop_remove(DDI_DEV_T_NONE, pdip, "pm-components");
11580Sstevel@tonic-gate 		pci_config_teardown(&conf_hdl);
11590Sstevel@tonic-gate 		mutex_destroy(&ppb->ppb_pwr_p->pwr_mutex);
11600Sstevel@tonic-gate 		kmem_free(ppb->ppb_pwr_p, sizeof (pci_pwr_t));
11610Sstevel@tonic-gate 		ppb->ppb_pwr_p = NULL;
11620Sstevel@tonic-gate 
11630Sstevel@tonic-gate 		return;
11640Sstevel@tonic-gate 	}
11650Sstevel@tonic-gate 
11660Sstevel@tonic-gate 	ppb->ppb_pwr_p->current_lvl =
11677656SSherry.Moore@Sun.COM 	    pci_pwr_current_lvl(ppb->ppb_pwr_p);
11680Sstevel@tonic-gate }
11690Sstevel@tonic-gate 
11700Sstevel@tonic-gate /*
11710Sstevel@tonic-gate  * Remove PM state for nexus.
11720Sstevel@tonic-gate  */
11730Sstevel@tonic-gate static void
11740Sstevel@tonic-gate ppb_pwr_teardown(ppb_devstate_t *ppb, dev_info_t *dip)
11750Sstevel@tonic-gate {
11760Sstevel@tonic-gate 	int low_lvl;
11770Sstevel@tonic-gate 
11780Sstevel@tonic-gate 	/*
11790Sstevel@tonic-gate 	 * Determine the lowest power level supported.
11800Sstevel@tonic-gate 	 */
11810Sstevel@tonic-gate 	if (ppb->ppb_pwr_p->pwr_flags & PCI_PWR_B3_CAPABLE) {
11820Sstevel@tonic-gate 		low_lvl = PM_LEVEL_B3;
11830Sstevel@tonic-gate 	} else {
11840Sstevel@tonic-gate 		low_lvl = PM_LEVEL_B2;
11850Sstevel@tonic-gate 	}
11860Sstevel@tonic-gate 
11870Sstevel@tonic-gate 	if (pm_lower_power(dip, PCI_PM_COMP_0, low_lvl) != DDI_SUCCESS) {
11880Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d failed to lower power",
11890Sstevel@tonic-gate 		    ddi_driver_name(dip), ddi_get_instance(dip));
11900Sstevel@tonic-gate 	}
11910Sstevel@tonic-gate 
11920Sstevel@tonic-gate 	pci_config_teardown(&ppb->ppb_conf_hdl);
11930Sstevel@tonic-gate 	mutex_destroy(&ppb->ppb_pwr_p->pwr_mutex);
11940Sstevel@tonic-gate 	kmem_free(ppb->ppb_pwr_p, sizeof (pci_pwr_t));
11950Sstevel@tonic-gate 
11960Sstevel@tonic-gate 	if (ddi_prop_remove(DDI_DEV_T_NONE, dip, "pm-components") !=
11977656SSherry.Moore@Sun.COM 	    DDI_PROP_SUCCESS) {
11980Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d unable to remove prop pm-components",
11990Sstevel@tonic-gate 		    ddi_driver_name(dip), ddi_get_instance(dip));
12000Sstevel@tonic-gate 	}
12010Sstevel@tonic-gate 
12020Sstevel@tonic-gate 	if (ddi_prop_remove(DDI_DEV_T_NONE, dip,
12030Sstevel@tonic-gate 	    "pm-want-child-notification?") != DDI_PROP_SUCCESS) {
12040Sstevel@tonic-gate 		cmn_err(CE_WARN,
12050Sstevel@tonic-gate 		    "%s%d unable to remove prop pm-want_child_notification?",
12060Sstevel@tonic-gate 		    ddi_driver_name(dip), ddi_get_instance(dip));
12070Sstevel@tonic-gate 	}
12080Sstevel@tonic-gate }
12090Sstevel@tonic-gate 
12100Sstevel@tonic-gate /*
12110Sstevel@tonic-gate  * Examine the pmcsr register and return the software defined
12120Sstevel@tonic-gate  * state (the difference being whether D3 means B2 or B3).
12130Sstevel@tonic-gate  */
12140Sstevel@tonic-gate int
12150Sstevel@tonic-gate pci_pwr_current_lvl(pci_pwr_t *pwr_p)
12160Sstevel@tonic-gate {
12170Sstevel@tonic-gate 	ppb_devstate_t *ppb;
12180Sstevel@tonic-gate 	uint16_t pmcsr;
12190Sstevel@tonic-gate 
12200Sstevel@tonic-gate 	/*
12210Sstevel@tonic-gate 	 * Find out current power level
12220Sstevel@tonic-gate 	 */
12230Sstevel@tonic-gate 	ppb = (ppb_devstate_t *)ddi_get_soft_state(ppb_state,
12240Sstevel@tonic-gate 	    ddi_get_instance(pwr_p->pwr_dip));
12250Sstevel@tonic-gate 
12261624Spjha 	if ((pmcsr = PCI_CAP_GET16(ppb->ppb_conf_hdl, NULL,
12277656SSherry.Moore@Sun.COM 	    ppb->ppb_pm_cap_ptr, PCI_PMCSR)) == PCI_CAP_EINVAL16)
12281624Spjha 		return (DDI_FAILURE);
12290Sstevel@tonic-gate 
12300Sstevel@tonic-gate 	switch (pmcsr & PCI_PMCSR_STATE_MASK) {
12310Sstevel@tonic-gate 	case PCI_PMCSR_D0:
12320Sstevel@tonic-gate 
12330Sstevel@tonic-gate 		return (PM_LEVEL_B0);
12340Sstevel@tonic-gate 	case PCI_PMCSR_D1:
12350Sstevel@tonic-gate 
12360Sstevel@tonic-gate 		return (PM_LEVEL_B1);
12370Sstevel@tonic-gate 	case PCI_PMCSR_D2:
12380Sstevel@tonic-gate 
12390Sstevel@tonic-gate 		return (PM_LEVEL_B2);
12400Sstevel@tonic-gate 	case PCI_PMCSR_D3HOT:
12410Sstevel@tonic-gate 		if ((ppb->ppb_pwr_p->pwr_flags & PCI_PWR_B3_CAPABLE) == 0) {
12420Sstevel@tonic-gate 
12430Sstevel@tonic-gate 			return (PM_LEVEL_B2);
12440Sstevel@tonic-gate 		} else {
12450Sstevel@tonic-gate 
12460Sstevel@tonic-gate 			return (PM_LEVEL_B3);
12470Sstevel@tonic-gate 		}
12480Sstevel@tonic-gate 	}
1249946Smathue 	/*NOTREACHED*/
1250946Smathue 	return (PM_LEVEL_B3);
12510Sstevel@tonic-gate }
12520Sstevel@tonic-gate 
12530Sstevel@tonic-gate /*
12540Sstevel@tonic-gate  * Power entry point.  Called by the PM framework to change the
12550Sstevel@tonic-gate  * current power state of the bus.  This function must first verify that
12560Sstevel@tonic-gate  * the requested power change is still valid.
12570Sstevel@tonic-gate  */
12580Sstevel@tonic-gate /*ARGSUSED*/
12590Sstevel@tonic-gate static int
12600Sstevel@tonic-gate ppb_pwr(dev_info_t *dip, int component, int lvl)
12610Sstevel@tonic-gate {
12620Sstevel@tonic-gate 	ppb_devstate_t *ppb;
12630Sstevel@tonic-gate 	uint16_t pmcsr;
12640Sstevel@tonic-gate 	char *str;
12650Sstevel@tonic-gate 	int lowest_lvl;
12660Sstevel@tonic-gate 	int old_lvl;
12670Sstevel@tonic-gate 	int new_lvl;
12680Sstevel@tonic-gate 
12690Sstevel@tonic-gate 	ppb = (ppb_devstate_t *)ddi_get_soft_state(ppb_state,
12700Sstevel@tonic-gate 	    ddi_get_instance(dip));
12710Sstevel@tonic-gate 	if (ppb == NULL) {
12720Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d ppb_pwr: can't get soft state",
12730Sstevel@tonic-gate 		    ddi_driver_name(dip), ddi_get_instance(dip));
12740Sstevel@tonic-gate 
12750Sstevel@tonic-gate 		return (DDI_FAILURE);
12760Sstevel@tonic-gate 	}
12770Sstevel@tonic-gate 
12780Sstevel@tonic-gate 	DEBUG1(DBG_PWR, dip, "ppb_pwr(): ENTER level = %d\n", lvl);
12790Sstevel@tonic-gate 
12800Sstevel@tonic-gate 	mutex_enter(&ppb->ppb_pwr_p->pwr_mutex);
12810Sstevel@tonic-gate 
12820Sstevel@tonic-gate 	/*
12830Sstevel@tonic-gate 	 * Find out if the power setting is possible.  If it is not,
12840Sstevel@tonic-gate 	 * set component busy and return failure.  If it is possible,
12850Sstevel@tonic-gate 	 * and it is the lowest pwr setting possible, set component
12860Sstevel@tonic-gate 	 * busy so that the framework does not try to lower any further.
12870Sstevel@tonic-gate 	 */
12880Sstevel@tonic-gate 	lowest_lvl = pci_pwr_new_lvl(ppb->ppb_pwr_p);
12890Sstevel@tonic-gate 	if (lowest_lvl > lvl) {
12900Sstevel@tonic-gate 		pci_pwr_component_busy(ppb->ppb_pwr_p);
12910Sstevel@tonic-gate 		DEBUG2(DBG_PWR, dip, "ppb_pwr: failing power request "
12927656SSherry.Moore@Sun.COM 		    "lowest allowed is %d requested is %d\n",
12937656SSherry.Moore@Sun.COM 		    lowest_lvl, lvl);
12940Sstevel@tonic-gate 		mutex_exit(&ppb->ppb_pwr_p->pwr_mutex);
12950Sstevel@tonic-gate 
12960Sstevel@tonic-gate 		return (DDI_FAILURE);
12970Sstevel@tonic-gate 	} else if (lowest_lvl == lvl) {
12980Sstevel@tonic-gate 		pci_pwr_component_busy(ppb->ppb_pwr_p);
12990Sstevel@tonic-gate 	} else {
13000Sstevel@tonic-gate 		pci_pwr_component_idle(ppb->ppb_pwr_p);
13010Sstevel@tonic-gate 	}
13020Sstevel@tonic-gate 
13031624Spjha 	if ((pmcsr = PCI_CAP_GET16(ppb->ppb_conf_hdl, NULL,
13047656SSherry.Moore@Sun.COM 	    ppb->ppb_pm_cap_ptr, PCI_PMCSR)) == PCI_CAP_EINVAL16)
13051624Spjha 		return (DDI_FAILURE);
13060Sstevel@tonic-gate 
13070Sstevel@tonic-gate 	/*
13080Sstevel@tonic-gate 	 * Save the current power level.  This is the actual function level,
13090Sstevel@tonic-gate 	 * not the translated bridge level stored in pwr_p->current_lvl
13100Sstevel@tonic-gate 	 */
13110Sstevel@tonic-gate 	old_lvl = pmcsr & PCI_PMCSR_STATE_MASK;
13120Sstevel@tonic-gate 
13130Sstevel@tonic-gate 	pmcsr &= ~PCI_PMCSR_STATE_MASK;
13140Sstevel@tonic-gate 	switch (lvl) {
13150Sstevel@tonic-gate 	case PM_LEVEL_B0:
13160Sstevel@tonic-gate 		str = "PM_LEVEL_B0 (full speed)";
13170Sstevel@tonic-gate 		pmcsr |= PCI_PMCSR_D0;
13180Sstevel@tonic-gate 		break;
13190Sstevel@tonic-gate 	case PM_LEVEL_B1:
13200Sstevel@tonic-gate 		str = "PM_LEVEL_B1 (light sleep. No bus traffic allowed)";
13210Sstevel@tonic-gate 		if ((ppb->ppb_pwr_p->pwr_flags & PCI_PWR_B1_CAPABLE) == 0) {
13220Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s%d PCI PM state B1 not supported",
13230Sstevel@tonic-gate 			    ddi_driver_name(dip), ddi_get_instance(dip));
13240Sstevel@tonic-gate 
13250Sstevel@tonic-gate 			mutex_exit(&ppb->ppb_pwr_p->pwr_mutex);
13260Sstevel@tonic-gate 			return (DDI_FAILURE);
13270Sstevel@tonic-gate 		}
13280Sstevel@tonic-gate 		pmcsr |= PCI_PMCSR_D1;
13290Sstevel@tonic-gate 		break;
13300Sstevel@tonic-gate 	case PM_LEVEL_B2:
13310Sstevel@tonic-gate 		str = "PM_LEVEL_B2 (clock off)";
13320Sstevel@tonic-gate 		if ((ppb->ppb_pwr_p->pwr_flags & PCI_PWR_B2_CAPABLE) == 0) {
13330Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s%d PM state B2 not supported...",
13340Sstevel@tonic-gate 			    ddi_driver_name(dip),
13350Sstevel@tonic-gate 			    ddi_get_instance(dip));
13360Sstevel@tonic-gate 			mutex_exit(&ppb->ppb_pwr_p->pwr_mutex);
13370Sstevel@tonic-gate 
13380Sstevel@tonic-gate 			return (DDI_FAILURE);
13390Sstevel@tonic-gate 		}
13400Sstevel@tonic-gate 
13410Sstevel@tonic-gate 		if ((ppb->ppb_pwr_p->pwr_flags & PCI_PWR_B3_CAPABLE) == 0) {
13420Sstevel@tonic-gate 			/*
13430Sstevel@tonic-gate 			 * If B3 isn't supported, use D3 for B2 to avoid the
13440Sstevel@tonic-gate 			 * possible case that D2 for B2 isn't supported.
13450Sstevel@tonic-gate 			 * Saves and extra check and state flag..
13460Sstevel@tonic-gate 			 */
13470Sstevel@tonic-gate 			pmcsr |= PCI_PMCSR_D3HOT;
13480Sstevel@tonic-gate 		} else {
13490Sstevel@tonic-gate 			pmcsr |= PCI_PMCSR_D2;
13500Sstevel@tonic-gate 		}
13510Sstevel@tonic-gate 		break;
13520Sstevel@tonic-gate 	case PM_LEVEL_B3:
13530Sstevel@tonic-gate 		str = "PM_LEVEL_B30 (clock and power off)";
13540Sstevel@tonic-gate 		if ((ppb->ppb_pwr_p->pwr_flags & PCI_PWR_B3_CAPABLE) == 0) {
13550Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s%d PM state B3 not supported...",
13560Sstevel@tonic-gate 			    ddi_driver_name(dip),
13570Sstevel@tonic-gate 			    ddi_get_instance(dip));
13580Sstevel@tonic-gate 			mutex_exit(&ppb->ppb_pwr_p->pwr_mutex);
13590Sstevel@tonic-gate 
13600Sstevel@tonic-gate 			return (DDI_FAILURE);
13610Sstevel@tonic-gate 		}
13620Sstevel@tonic-gate 		pmcsr |= PCI_PMCSR_D3HOT;
13630Sstevel@tonic-gate 
13640Sstevel@tonic-gate 		break;
13650Sstevel@tonic-gate 
13660Sstevel@tonic-gate 	default:
13670Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d Unknown PM state %d",
13680Sstevel@tonic-gate 		    ddi_driver_name(dip), ddi_get_instance(dip), lvl);
13690Sstevel@tonic-gate 		mutex_exit(&ppb->ppb_pwr_p->pwr_mutex);
13700Sstevel@tonic-gate 
13710Sstevel@tonic-gate 		return (DDI_FAILURE);
13720Sstevel@tonic-gate 	}
13730Sstevel@tonic-gate 
13740Sstevel@tonic-gate 	new_lvl = pmcsr & PCI_PMCSR_STATE_MASK;
13750Sstevel@tonic-gate 
13760Sstevel@tonic-gate 	/*
13770Sstevel@tonic-gate 	 * Save config regs if going into HW state D3 (B2 or B3)
13780Sstevel@tonic-gate 	 */
13790Sstevel@tonic-gate 	if ((old_lvl != PCI_PMCSR_D3HOT) && (new_lvl == PCI_PMCSR_D3HOT)) {
13800Sstevel@tonic-gate 		DEBUG0(DBG_PWR, dip, "ppb_pwr(): SAVING CONFIG REGS\n");
13810Sstevel@tonic-gate 		if (pci_save_config_regs(dip) != DDI_SUCCESS) {
13820Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s%d Save config regs failed",
13837656SSherry.Moore@Sun.COM 			    ddi_driver_name(dip), ddi_get_instance(dip));
13840Sstevel@tonic-gate 			mutex_exit(&ppb->ppb_pwr_p->pwr_mutex);
13850Sstevel@tonic-gate 
13860Sstevel@tonic-gate 			return (DDI_FAILURE);
13870Sstevel@tonic-gate 		}
13880Sstevel@tonic-gate 	}
13890Sstevel@tonic-gate 
13901624Spjha 	PCI_CAP_PUT16(ppb->ppb_conf_hdl, NULL, ppb->ppb_pm_cap_ptr, PCI_PMCSR,
13917656SSherry.Moore@Sun.COM 	    pmcsr);
13920Sstevel@tonic-gate 
13930Sstevel@tonic-gate 	/*
13940Sstevel@tonic-gate 	 * No bus transactions should occur without waiting for
13950Sstevel@tonic-gate 	 * settle time specified in PCI PM spec rev 2.1 sec 5.6.1
13960Sstevel@tonic-gate 	 * To make things simple, just use the max time specified for
13970Sstevel@tonic-gate 	 * all state transitions.
13980Sstevel@tonic-gate 	 */
13990Sstevel@tonic-gate 	delay(drv_usectohz(PCI_CLK_SETTLE_TIME));
14000Sstevel@tonic-gate 
14010Sstevel@tonic-gate 	/*
14020Sstevel@tonic-gate 	 * Restore configuration registers if coming out of HW state D3
14030Sstevel@tonic-gate 	 */
14040Sstevel@tonic-gate 	if ((old_lvl == PCI_PMCSR_D3HOT) && (new_lvl != PCI_PMCSR_D3HOT)) {
14050Sstevel@tonic-gate 		DEBUG0(DBG_PWR, dip, "ppb_pwr(): RESTORING CONFIG REGS\n");
14060Sstevel@tonic-gate 		if (pci_restore_config_regs(dip) != DDI_SUCCESS) {
14070Sstevel@tonic-gate 			panic("%s%d restore config regs failed",
14080Sstevel@tonic-gate 			    ddi_driver_name(dip), ddi_get_instance(dip));
14090Sstevel@tonic-gate 		}
14100Sstevel@tonic-gate 		/*NOTREACHED*/
14110Sstevel@tonic-gate 	}
14120Sstevel@tonic-gate 
14130Sstevel@tonic-gate 	ppb->ppb_pwr_p->current_lvl = lvl;
14140Sstevel@tonic-gate 
14150Sstevel@tonic-gate 	mutex_exit(&ppb->ppb_pwr_p->pwr_mutex);
14160Sstevel@tonic-gate 
14170Sstevel@tonic-gate 	DEBUG1(DBG_PWR, dip, "ppb_set_pwr: set PM state to %s\n\n", str);
14180Sstevel@tonic-gate 
14190Sstevel@tonic-gate 	return (DDI_SUCCESS);
14200Sstevel@tonic-gate }
14210Sstevel@tonic-gate 
14220Sstevel@tonic-gate /*
14230Sstevel@tonic-gate  * Initialize hotplug framework if we are hotpluggable.
14240Sstevel@tonic-gate  * Sets flag in the soft state if Hot Plug is supported and initialized
14250Sstevel@tonic-gate  * properly.
14260Sstevel@tonic-gate  */
14270Sstevel@tonic-gate /*ARGSUSED*/
14280Sstevel@tonic-gate static void
14290Sstevel@tonic-gate ppb_init_hotplug(ppb_devstate_t *ppb)
14300Sstevel@tonic-gate {
14310Sstevel@tonic-gate 	if (ddi_prop_exists(DDI_DEV_T_ANY, ppb->dip, DDI_PROP_DONTPASS,
14320Sstevel@tonic-gate 	    "hotplug-capable")) {
14330Sstevel@tonic-gate 		(void) modload("misc", "pcihp");
14340Sstevel@tonic-gate 
14350Sstevel@tonic-gate 		if (pcihp_init(ppb->dip) != DDI_SUCCESS) {
14360Sstevel@tonic-gate 			cmn_err(CE_WARN,
14370Sstevel@tonic-gate 			    "%s #%d: Failed setting hotplug framework",
14380Sstevel@tonic-gate 			    ddi_driver_name(ppb->dip),
14390Sstevel@tonic-gate 			    ddi_get_instance(ppb->dip));
14400Sstevel@tonic-gate 		} else
14410Sstevel@tonic-gate 			ppb->hotplug_capable = B_TRUE;
14420Sstevel@tonic-gate 	}
14430Sstevel@tonic-gate 
14440Sstevel@tonic-gate }
14450Sstevel@tonic-gate 
14460Sstevel@tonic-gate static void
14470Sstevel@tonic-gate ppb_create_ranges_prop(dev_info_t *dip,
14480Sstevel@tonic-gate 	ddi_acc_handle_t config_handle)
14490Sstevel@tonic-gate {
14500Sstevel@tonic-gate 	uint32_t base, limit;
14510Sstevel@tonic-gate 	ppb_ranges_t	ranges[PPB_RANGE_LEN];
14520Sstevel@tonic-gate 	uint8_t io_base_lo, io_limit_lo;
14530Sstevel@tonic-gate 	uint16_t io_base_hi, io_limit_hi, mem_base, mem_limit;
14540Sstevel@tonic-gate 	int i = 0, rangelen = sizeof (ppb_ranges_t)/sizeof (int);
14550Sstevel@tonic-gate 
14560Sstevel@tonic-gate 	io_base_lo = pci_config_get8(config_handle, PCI_BCNF_IO_BASE_LOW);
14570Sstevel@tonic-gate 	io_limit_lo = pci_config_get8(config_handle, PCI_BCNF_IO_LIMIT_LOW);
14580Sstevel@tonic-gate 	io_base_hi = pci_config_get16(config_handle, PCI_BCNF_IO_BASE_HI);
14590Sstevel@tonic-gate 	io_limit_hi = pci_config_get16(config_handle, PCI_BCNF_IO_LIMIT_HI);
14600Sstevel@tonic-gate 	mem_base = pci_config_get16(config_handle, PCI_BCNF_MEM_BASE);
14610Sstevel@tonic-gate 	mem_limit = pci_config_get16(config_handle, PCI_BCNF_MEM_LIMIT);
14620Sstevel@tonic-gate 
14630Sstevel@tonic-gate 	/*
14640Sstevel@tonic-gate 	 * Create ranges for IO space
14650Sstevel@tonic-gate 	 */
14660Sstevel@tonic-gate 	ranges[i].size_low = ranges[i].size_high = 0;
14670Sstevel@tonic-gate 	ranges[i].parent_mid = ranges[i].child_mid =
14687656SSherry.Moore@Sun.COM 	    ranges[i].parent_high = 0;
14690Sstevel@tonic-gate 	ranges[i].child_high = ranges[i].parent_high |=
14707656SSherry.Moore@Sun.COM 	    (PCI_REG_REL_M | PCI_ADDR_IO);
14710Sstevel@tonic-gate 	base = PPB_16bit_IOADDR(io_base_lo);
14720Sstevel@tonic-gate 	limit = PPB_16bit_IOADDR(io_limit_lo);
14730Sstevel@tonic-gate 
1474*10548SPavel.Potoplyak@Sun.COM 	/*
1475*10548SPavel.Potoplyak@Sun.COM 	 * Check for 32-bit I/O support as per PCI-to-PCI Bridge Arch Spec
1476*10548SPavel.Potoplyak@Sun.COM 	 */
14770Sstevel@tonic-gate 	if ((io_base_lo & 0xf) == PPB_32BIT_IO) {
14780Sstevel@tonic-gate 		base = PPB_LADDR(base, io_base_hi);
14790Sstevel@tonic-gate 		limit = PPB_LADDR(limit, io_limit_hi);
14800Sstevel@tonic-gate 	}
14810Sstevel@tonic-gate 
1482*10548SPavel.Potoplyak@Sun.COM 	/*
1483*10548SPavel.Potoplyak@Sun.COM 	 * Check if the bridge implements an I/O address range as per
1484*10548SPavel.Potoplyak@Sun.COM 	 * PCI-to-PCI Bridge Arch Spec
1485*10548SPavel.Potoplyak@Sun.COM 	 */
1486*10548SPavel.Potoplyak@Sun.COM 	if ((io_base_lo != 0 || io_limit_lo != 0) && limit >= base) {
1487*10548SPavel.Potoplyak@Sun.COM 		ranges[i].parent_low = ranges[i].child_low =
1488*10548SPavel.Potoplyak@Sun.COM 		    base;
1489*10548SPavel.Potoplyak@Sun.COM 		ranges[i].size_low = limit - base + PPB_IOGRAIN;
1490*10548SPavel.Potoplyak@Sun.COM 		i++;
14910Sstevel@tonic-gate 	}
14920Sstevel@tonic-gate 
14930Sstevel@tonic-gate 	/*
14940Sstevel@tonic-gate 	 * Create ranges for 32bit memory space
14950Sstevel@tonic-gate 	 */
14960Sstevel@tonic-gate 	base = PPB_32bit_MEMADDR(mem_base);
14970Sstevel@tonic-gate 	limit = PPB_32bit_MEMADDR(mem_limit);
14980Sstevel@tonic-gate 	ranges[i].size_low = ranges[i].size_high = 0;
14990Sstevel@tonic-gate 	ranges[i].parent_mid = ranges[i].child_mid =
15007656SSherry.Moore@Sun.COM 	    ranges[i].parent_high = 0;
15010Sstevel@tonic-gate 	ranges[i].child_high = ranges[i].parent_high |=
15027656SSherry.Moore@Sun.COM 	    (PCI_REG_REL_M | PCI_ADDR_MEM32);
15030Sstevel@tonic-gate 	ranges[i].child_low = ranges[i].parent_low = base;
15040Sstevel@tonic-gate 	if (limit >= base) {
15050Sstevel@tonic-gate 		ranges[i].size_low = limit - base + PPB_MEMGRAIN;
15060Sstevel@tonic-gate 		i++;
15070Sstevel@tonic-gate 	}
15080Sstevel@tonic-gate 
15090Sstevel@tonic-gate 	if (i) {
15100Sstevel@tonic-gate 		(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, dip, "ranges",
15110Sstevel@tonic-gate 		    (int *)ranges, i * rangelen);
15120Sstevel@tonic-gate 	}
15130Sstevel@tonic-gate }
15140Sstevel@tonic-gate 
15150Sstevel@tonic-gate /* ARGSUSED */
15160Sstevel@tonic-gate static int
15170Sstevel@tonic-gate ppb_open(dev_t *devp, int flags, int otyp, cred_t *credp)
15180Sstevel@tonic-gate {
15190Sstevel@tonic-gate 	ppb_devstate_t *ppb_p;
15200Sstevel@tonic-gate 	minor_t		minor = getminor(*devp);
15210Sstevel@tonic-gate 	int		instance = PCIHP_AP_MINOR_NUM_TO_INSTANCE(minor);
15220Sstevel@tonic-gate 
15230Sstevel@tonic-gate 	/*
15240Sstevel@tonic-gate 	 * Make sure the open is for the right file type.
15250Sstevel@tonic-gate 	 */
15260Sstevel@tonic-gate 	if (otyp != OTYP_CHR)
15270Sstevel@tonic-gate 		return (EINVAL);
15280Sstevel@tonic-gate 
15290Sstevel@tonic-gate 	/*
15300Sstevel@tonic-gate 	 * Get the soft state structure for the device.
15310Sstevel@tonic-gate 	 */
15320Sstevel@tonic-gate 	ppb_p = (ppb_devstate_t *)ddi_get_soft_state(ppb_state,
15330Sstevel@tonic-gate 	    instance);
15340Sstevel@tonic-gate 
15350Sstevel@tonic-gate 	if (ppb_p == NULL)
15360Sstevel@tonic-gate 		return (ENXIO);
15370Sstevel@tonic-gate 
15380Sstevel@tonic-gate 	if (ppb_p->hotplug_capable == B_TRUE)
15390Sstevel@tonic-gate 		return ((pcihp_get_cb_ops())->cb_open(devp, flags,
15400Sstevel@tonic-gate 		    otyp, credp));
15410Sstevel@tonic-gate 
15420Sstevel@tonic-gate 	/*
15430Sstevel@tonic-gate 	 * Handle the open by tracking the device state.
15440Sstevel@tonic-gate 	 */
15450Sstevel@tonic-gate 	mutex_enter(&ppb_p->ppb_mutex);
15460Sstevel@tonic-gate 	if (flags & FEXCL) {
15470Sstevel@tonic-gate 		if (ppb_p->ppb_soft_state != PPB_SOFT_STATE_CLOSED) {
15480Sstevel@tonic-gate 			mutex_exit(&ppb_p->ppb_mutex);
15490Sstevel@tonic-gate 			return (EBUSY);
15500Sstevel@tonic-gate 		}
15510Sstevel@tonic-gate 		ppb_p->ppb_soft_state = PPB_SOFT_STATE_OPEN_EXCL;
15520Sstevel@tonic-gate 	} else {
15530Sstevel@tonic-gate 		if (ppb_p->ppb_soft_state == PPB_SOFT_STATE_OPEN_EXCL) {
15540Sstevel@tonic-gate 			mutex_exit(&ppb_p->ppb_mutex);
15550Sstevel@tonic-gate 			return (EBUSY);
15560Sstevel@tonic-gate 		}
15570Sstevel@tonic-gate 		ppb_p->ppb_soft_state = PPB_SOFT_STATE_OPEN;
15580Sstevel@tonic-gate 	}
15590Sstevel@tonic-gate 	mutex_exit(&ppb_p->ppb_mutex);
15600Sstevel@tonic-gate 	return (0);
15610Sstevel@tonic-gate }
15620Sstevel@tonic-gate 
15630Sstevel@tonic-gate 
15640Sstevel@tonic-gate /* ARGSUSED */
15650Sstevel@tonic-gate static int
15660Sstevel@tonic-gate ppb_close(dev_t dev, int flags, int otyp, cred_t *credp)
15670Sstevel@tonic-gate {
15680Sstevel@tonic-gate 	ppb_devstate_t *ppb_p;
15690Sstevel@tonic-gate 	minor_t		minor = getminor(dev);
15700Sstevel@tonic-gate 	int		instance = PCIHP_AP_MINOR_NUM_TO_INSTANCE(minor);
15710Sstevel@tonic-gate 
15720Sstevel@tonic-gate 	if (otyp != OTYP_CHR)
15730Sstevel@tonic-gate 		return (EINVAL);
15740Sstevel@tonic-gate 
15750Sstevel@tonic-gate 	ppb_p = (ppb_devstate_t *)ddi_get_soft_state(ppb_state,
15760Sstevel@tonic-gate 	    instance);
15770Sstevel@tonic-gate 
15780Sstevel@tonic-gate 	if (ppb_p == NULL)
15790Sstevel@tonic-gate 		return (ENXIO);
15800Sstevel@tonic-gate 
15810Sstevel@tonic-gate 	if (ppb_p->hotplug_capable == B_TRUE)
15820Sstevel@tonic-gate 		return ((pcihp_get_cb_ops())->cb_close(dev, flags,
15830Sstevel@tonic-gate 		    otyp, credp));
15840Sstevel@tonic-gate 
15850Sstevel@tonic-gate 	mutex_enter(&ppb_p->ppb_mutex);
15860Sstevel@tonic-gate 	ppb_p->ppb_soft_state = PPB_SOFT_STATE_CLOSED;
15870Sstevel@tonic-gate 	mutex_exit(&ppb_p->ppb_mutex);
15880Sstevel@tonic-gate 	return (0);
15890Sstevel@tonic-gate }
15900Sstevel@tonic-gate 
15910Sstevel@tonic-gate 
15920Sstevel@tonic-gate /*
15930Sstevel@tonic-gate  * ppb_ioctl: devctl hotplug controls
15940Sstevel@tonic-gate  */
15950Sstevel@tonic-gate /* ARGSUSED */
15960Sstevel@tonic-gate static int
15970Sstevel@tonic-gate ppb_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp,
15980Sstevel@tonic-gate 	int *rvalp)
15990Sstevel@tonic-gate {
16000Sstevel@tonic-gate 	ppb_devstate_t *ppb_p;
16010Sstevel@tonic-gate 	dev_info_t *self;
16020Sstevel@tonic-gate 	struct devctl_iocdata *dcp;
16030Sstevel@tonic-gate 	uint_t bus_state;
16040Sstevel@tonic-gate 	int rv = 0;
16050Sstevel@tonic-gate 	minor_t		minor = getminor(dev);
16060Sstevel@tonic-gate 	int		instance = PCIHP_AP_MINOR_NUM_TO_INSTANCE(minor);
16070Sstevel@tonic-gate 
16080Sstevel@tonic-gate 	ppb_p = (ppb_devstate_t *)ddi_get_soft_state(ppb_state,
16090Sstevel@tonic-gate 	    instance);
16100Sstevel@tonic-gate 
16110Sstevel@tonic-gate 	if (ppb_p == NULL)
16120Sstevel@tonic-gate 		return (ENXIO);
16130Sstevel@tonic-gate 
16140Sstevel@tonic-gate 	if (ppb_p->hotplug_capable == B_TRUE)
16150Sstevel@tonic-gate 		return ((pcihp_get_cb_ops())->cb_ioctl(dev, cmd,
16160Sstevel@tonic-gate 		    arg, mode, credp, rvalp));
16170Sstevel@tonic-gate 
16180Sstevel@tonic-gate 	self = ppb_p->dip;
16190Sstevel@tonic-gate 
16200Sstevel@tonic-gate 	/*
16210Sstevel@tonic-gate 	 * We can use the generic implementation for these ioctls
16220Sstevel@tonic-gate 	 */
16230Sstevel@tonic-gate 	switch (cmd) {
16240Sstevel@tonic-gate 	case DEVCTL_DEVICE_GETSTATE:
16250Sstevel@tonic-gate 	case DEVCTL_DEVICE_ONLINE:
16260Sstevel@tonic-gate 	case DEVCTL_DEVICE_OFFLINE:
16270Sstevel@tonic-gate 	case DEVCTL_BUS_GETSTATE:
16280Sstevel@tonic-gate 		return (ndi_devctl_ioctl(self, cmd, arg, mode, 0));
16290Sstevel@tonic-gate 	}
16300Sstevel@tonic-gate 
16310Sstevel@tonic-gate 	/*
16320Sstevel@tonic-gate 	 * read devctl ioctl data
16330Sstevel@tonic-gate 	 */
16340Sstevel@tonic-gate 	if (ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS)
16350Sstevel@tonic-gate 		return (EFAULT);
16360Sstevel@tonic-gate 
16370Sstevel@tonic-gate 	switch (cmd) {
16380Sstevel@tonic-gate 
16390Sstevel@tonic-gate 	case DEVCTL_DEVICE_RESET:
16400Sstevel@tonic-gate 		rv = ENOTSUP;
16410Sstevel@tonic-gate 		break;
16420Sstevel@tonic-gate 
16430Sstevel@tonic-gate 	case DEVCTL_BUS_QUIESCE:
16440Sstevel@tonic-gate 		if (ndi_get_bus_state(self, &bus_state) == NDI_SUCCESS)
16450Sstevel@tonic-gate 			if (bus_state == BUS_QUIESCED)
16460Sstevel@tonic-gate 				break;
16470Sstevel@tonic-gate 		(void) ndi_set_bus_state(self, BUS_QUIESCED);
16480Sstevel@tonic-gate 		break;
16490Sstevel@tonic-gate 
16500Sstevel@tonic-gate 	case DEVCTL_BUS_UNQUIESCE:
16510Sstevel@tonic-gate 		if (ndi_get_bus_state(self, &bus_state) == NDI_SUCCESS)
16520Sstevel@tonic-gate 			if (bus_state == BUS_ACTIVE)
16530Sstevel@tonic-gate 				break;
16540Sstevel@tonic-gate 		(void) ndi_set_bus_state(self, BUS_ACTIVE);
16550Sstevel@tonic-gate 		break;
16560Sstevel@tonic-gate 
16570Sstevel@tonic-gate 	case DEVCTL_BUS_RESET:
16580Sstevel@tonic-gate 		rv = ENOTSUP;
16590Sstevel@tonic-gate 		break;
16600Sstevel@tonic-gate 
16610Sstevel@tonic-gate 	case DEVCTL_BUS_RESETALL:
16620Sstevel@tonic-gate 		rv = ENOTSUP;
16630Sstevel@tonic-gate 		break;
16640Sstevel@tonic-gate 
16650Sstevel@tonic-gate 	default:
16660Sstevel@tonic-gate 		rv = ENOTTY;
16670Sstevel@tonic-gate 	}
16680Sstevel@tonic-gate 
16690Sstevel@tonic-gate 	ndi_dc_freehdl(dcp);
16700Sstevel@tonic-gate 	return (rv);
16710Sstevel@tonic-gate }
16720Sstevel@tonic-gate 
16730Sstevel@tonic-gate static int ppb_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op,
16740Sstevel@tonic-gate     int flags, char *name, caddr_t valuep, int *lengthp)
16750Sstevel@tonic-gate {
16760Sstevel@tonic-gate 	ppb_devstate_t *ppb_p;
16770Sstevel@tonic-gate 	minor_t		minor = getminor(dev);
16780Sstevel@tonic-gate 	int		instance = PCIHP_AP_MINOR_NUM_TO_INSTANCE(minor);
16790Sstevel@tonic-gate 
16800Sstevel@tonic-gate 	ppb_p = (ppb_devstate_t *)ddi_get_soft_state(ppb_state,
16810Sstevel@tonic-gate 	    instance);
16820Sstevel@tonic-gate 
16830Sstevel@tonic-gate 	if (ppb_p == NULL)
16840Sstevel@tonic-gate 		return (ENXIO);
16850Sstevel@tonic-gate 
16860Sstevel@tonic-gate 	if (ppb_p->hotplug_capable == B_TRUE)
16870Sstevel@tonic-gate 		return ((pcihp_get_cb_ops())->cb_prop_op(dev, dip, prop_op,
16880Sstevel@tonic-gate 		    flags, name, valuep, lengthp));
16890Sstevel@tonic-gate 
16900Sstevel@tonic-gate 	return (ddi_prop_op(dev, dip, prop_op, flags, name, valuep, lengthp));
16910Sstevel@tonic-gate }
16920Sstevel@tonic-gate 
16930Sstevel@tonic-gate /*
16940Sstevel@tonic-gate  * Initialize our FMA resources
16950Sstevel@tonic-gate  */
16960Sstevel@tonic-gate static void
16970Sstevel@tonic-gate ppb_fm_init(ppb_devstate_t *ppb_p)
16980Sstevel@tonic-gate {
16993274Set142600 	dev_info_t *root = ddi_root_node();
17003274Set142600 	dev_info_t *pdip;
17013274Set142600 	char *bus;
17023274Set142600 
17030Sstevel@tonic-gate 	ppb_p->fm_cap = DDI_FM_EREPORT_CAPABLE | DDI_FM_ERRCB_CAPABLE |
17047656SSherry.Moore@Sun.COM 	    DDI_FM_ACCCHK_CAPABLE | DDI_FM_DMACHK_CAPABLE;
17050Sstevel@tonic-gate 
17060Sstevel@tonic-gate 	/*
17070Sstevel@tonic-gate 	 * Request our capability level and get our parents capability
17080Sstevel@tonic-gate 	 * and ibc.
17090Sstevel@tonic-gate 	 */
17100Sstevel@tonic-gate 	ddi_fm_init(ppb_p->dip, &ppb_p->fm_cap, &ppb_p->fm_ibc);
17110Sstevel@tonic-gate 	ASSERT((ppb_p->fm_cap & DDI_FM_EREPORT_CAPABLE) &&
17120Sstevel@tonic-gate 	    (ppb_p->fm_cap & DDI_FM_ERRCB_CAPABLE));
17130Sstevel@tonic-gate 
17140Sstevel@tonic-gate 	pci_ereport_setup(ppb_p->dip);
17150Sstevel@tonic-gate 
17160Sstevel@tonic-gate 	/*
17170Sstevel@tonic-gate 	 * Register error callback with our parent.
17180Sstevel@tonic-gate 	 */
17190Sstevel@tonic-gate 	ddi_fm_handler_register(ppb_p->dip, ppb_err_callback, NULL);
17203274Set142600 
17219921SKrishna.Elango@Sun.COM 	ppb_p->parent_bus = PCIE_PCIECAP_DEV_TYPE_PCI_PSEUDO;
17223274Set142600 	for (pdip = ddi_get_parent(ppb_p->dip); pdip && (pdip != root) &&
17233274Set142600 	    (ppb_p->parent_bus != PCIE_PCIECAP_DEV_TYPE_PCIE_DEV);
17243274Set142600 	    pdip = ddi_get_parent(pdip)) {
17253274Set142600 		if (ddi_prop_lookup_string(DDI_DEV_T_ANY, pdip,
17263274Set142600 		    DDI_PROP_DONTPASS, "device_type", &bus) !=
17273274Set142600 		    DDI_PROP_SUCCESS)
17283274Set142600 			break;
17293274Set142600 
17303274Set142600 		if (strcmp(bus, "pciex") == 0)
17313274Set142600 			ppb_p->parent_bus = PCIE_PCIECAP_DEV_TYPE_PCIE_DEV;
17323274Set142600 
17333274Set142600 		ddi_prop_free(bus);
17343274Set142600 	}
17350Sstevel@tonic-gate }
17360Sstevel@tonic-gate 
17370Sstevel@tonic-gate /*
17380Sstevel@tonic-gate  * Breakdown our FMA resources
17390Sstevel@tonic-gate  */
17400Sstevel@tonic-gate static void
17410Sstevel@tonic-gate ppb_fm_fini(ppb_devstate_t *ppb_p)
17420Sstevel@tonic-gate {
17430Sstevel@tonic-gate 	/*
17440Sstevel@tonic-gate 	 * Clean up allocated fm structures
17450Sstevel@tonic-gate 	 */
17460Sstevel@tonic-gate 	ddi_fm_handler_unregister(ppb_p->dip);
17470Sstevel@tonic-gate 	pci_ereport_teardown(ppb_p->dip);
17480Sstevel@tonic-gate 	ddi_fm_fini(ppb_p->dip);
17490Sstevel@tonic-gate }
17500Sstevel@tonic-gate 
17510Sstevel@tonic-gate /*
17520Sstevel@tonic-gate  * Initialize FMA resources for children devices. Called when
17530Sstevel@tonic-gate  * child calls ddi_fm_init().
17540Sstevel@tonic-gate  */
17550Sstevel@tonic-gate /*ARGSUSED*/
17560Sstevel@tonic-gate static int
17570Sstevel@tonic-gate ppb_fm_init_child(dev_info_t *dip, dev_info_t *tdip, int cap,
17580Sstevel@tonic-gate 		ddi_iblock_cookie_t *ibc)
17590Sstevel@tonic-gate {
17600Sstevel@tonic-gate 	ppb_devstate_t *ppb_p = (ppb_devstate_t *)ddi_get_soft_state(ppb_state,
17617656SSherry.Moore@Sun.COM 	    ddi_get_instance(dip));
17620Sstevel@tonic-gate 	*ibc = ppb_p->fm_ibc;
17630Sstevel@tonic-gate 	return (ppb_p->fm_cap);
17640Sstevel@tonic-gate }
17650Sstevel@tonic-gate 
17660Sstevel@tonic-gate /*
17670Sstevel@tonic-gate  * FMA registered error callback
17680Sstevel@tonic-gate  */
17690Sstevel@tonic-gate static int
17700Sstevel@tonic-gate ppb_err_callback(dev_info_t *dip, ddi_fm_error_t *derr, const void *impl_data)
17710Sstevel@tonic-gate {
17723274Set142600 	ppb_devstate_t *ppb_p = (ppb_devstate_t *)ddi_get_soft_state(ppb_state,
17737656SSherry.Moore@Sun.COM 	    ddi_get_instance(dip));
17743274Set142600 
17753274Set142600 	/*
17763274Set142600 	 * errors handled by SPARC PCI-E framework for PCIe platforms
17773274Set142600 	 */
17783274Set142600 	if (ppb_p->parent_bus == PCIE_PCIECAP_DEV_TYPE_PCIE_DEV)
17793274Set142600 		return (DDI_FM_OK);
17803274Set142600 
17813274Set142600 	/*
17823274Set142600 	 * do the following for SPARC PCI platforms
17833274Set142600 	 */
17840Sstevel@tonic-gate 	ASSERT(impl_data == NULL);
17851865Sdilpreet 	pci_ereport_post(dip, derr, NULL);
17861865Sdilpreet 	return (derr->fme_status);
17870Sstevel@tonic-gate }
17880Sstevel@tonic-gate 
17890Sstevel@tonic-gate static void
17900Sstevel@tonic-gate ppb_bus_enter(dev_info_t *dip, ddi_acc_handle_t handle)
17910Sstevel@tonic-gate {
17920Sstevel@tonic-gate 	i_ndi_busop_access_enter(dip, handle);
17930Sstevel@tonic-gate }
17940Sstevel@tonic-gate 
17950Sstevel@tonic-gate /* ARGSUSED */
17960Sstevel@tonic-gate static void
17970Sstevel@tonic-gate ppb_bus_exit(dev_info_t *dip, ddi_acc_handle_t handle)
17980Sstevel@tonic-gate {
17990Sstevel@tonic-gate 	i_ndi_busop_access_exit(dip, handle);
18000Sstevel@tonic-gate }
1801