xref: /onnv-gate/usr/src/uts/sun4u/io/pci/pci_pci.c (revision 11596:e9010337bcd3)
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 /*
22*11596SJason.Beloro@Sun.COM  * Copyright 2010 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  *	Sun4u PCI to PCI bus bridge nexus driver
280Sstevel@tonic-gate  */
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #include <sys/conf.h>
310Sstevel@tonic-gate #include <sys/kmem.h>
320Sstevel@tonic-gate #include <sys/debug.h>
330Sstevel@tonic-gate #include <sys/modctl.h>
340Sstevel@tonic-gate #include <sys/autoconf.h>
350Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
360Sstevel@tonic-gate #include <sys/ddi_subrdefs.h>
3710923SEvan.Yan@Sun.COM #include <sys/pci_impl.h>
383274Set142600 #include <sys/pcie_impl.h>
391624Spjha #include <sys/pci_cap.h>
400Sstevel@tonic-gate #include <sys/pci/pci_nexus.h>
410Sstevel@tonic-gate #include <sys/pci/pci_regs.h>
420Sstevel@tonic-gate #include <sys/ddi.h>
430Sstevel@tonic-gate #include <sys/sunndi.h>
440Sstevel@tonic-gate #include <sys/sunddi.h>
450Sstevel@tonic-gate #include <sys/fm/protocol.h>
460Sstevel@tonic-gate #include <sys/ddifm.h>
470Sstevel@tonic-gate #include <sys/pci/pci_pwr.h>
480Sstevel@tonic-gate #include <sys/pci/pci_debug.h>
4910923SEvan.Yan@Sun.COM #include <sys/hotplug/pci/pcie_hp.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)()		*/
13310923SEvan.Yan@Sun.COM 	ppb_intr_ops,			/* (*bus_intr_op)(); 		*/
13410923SEvan.Yan@Sun.COM 	pcie_hp_common_ops		/* (*bus_hp_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);
16810923SEvan.Yan@Sun.COM static int ppb_info(dev_info_t *dip, ddi_info_cmd_t cmd,
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 */
1837656SSherry.Moore@Sun.COM 	ppb_pwr,		/* power */
1847656SSherry.Moore@Sun.COM 	ddi_quiesce_not_needed,		/* quiesce */
1850Sstevel@tonic-gate };
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate /*
1880Sstevel@tonic-gate  * Module linkage information for the kernel.
1890Sstevel@tonic-gate  */
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate static struct modldrv modldrv = {
1920Sstevel@tonic-gate 	&mod_driverops, /* Type of module */
1937656SSherry.Moore@Sun.COM 	"Standard PCI to PCI bridge nexus driver",
1940Sstevel@tonic-gate 	&ppb_ops,	/* driver ops */
1950Sstevel@tonic-gate };
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate static struct modlinkage modlinkage = {
1980Sstevel@tonic-gate 	MODREV_1,
1990Sstevel@tonic-gate 	(void *)&modldrv,
2000Sstevel@tonic-gate 	NULL
2010Sstevel@tonic-gate };
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate /*
2040Sstevel@tonic-gate  * soft state pointer and structure template:
2050Sstevel@tonic-gate  */
2060Sstevel@tonic-gate static void *ppb_state;
2070Sstevel@tonic-gate 
208946Smathue struct ppb_cfg_state {
2090Sstevel@tonic-gate 	dev_info_t *dip;
2100Sstevel@tonic-gate 	ushort_t command;
2110Sstevel@tonic-gate 	uchar_t cache_line_size;
2120Sstevel@tonic-gate 	uchar_t latency_timer;
2130Sstevel@tonic-gate 	uchar_t header_type;
2140Sstevel@tonic-gate 	uchar_t sec_latency_timer;
2150Sstevel@tonic-gate 	ushort_t bridge_control;
2160Sstevel@tonic-gate };
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate typedef struct {
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate 	dev_info_t *dip;
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate 	/*
2230Sstevel@tonic-gate 	 * configuration register state for the bus:
2240Sstevel@tonic-gate 	 */
2250Sstevel@tonic-gate 	uchar_t ppb_cache_line_size;
2260Sstevel@tonic-gate 	uchar_t ppb_latency_timer;
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate 	/*
2290Sstevel@tonic-gate 	 * PM support
2300Sstevel@tonic-gate 	 */
2310Sstevel@tonic-gate 	ddi_acc_handle_t	ppb_conf_hdl;
2321624Spjha 	uint16_t		ppb_pm_cap_ptr;
2330Sstevel@tonic-gate 	pci_pwr_t		*ppb_pwr_p;
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate 	/*
2360Sstevel@tonic-gate 	 * HP support
2370Sstevel@tonic-gate 	 */
2380Sstevel@tonic-gate 	boolean_t		hotplug_capable;
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 	kmutex_t ppb_mutex;
2410Sstevel@tonic-gate 	uint_t ppb_soft_state;
2420Sstevel@tonic-gate 	int fm_cap;
2430Sstevel@tonic-gate 	ddi_iblock_cookie_t fm_ibc;
2443274Set142600 
2459921SKrishna.Elango@Sun.COM 	uint16_t parent_bus;
2460Sstevel@tonic-gate } ppb_devstate_t;
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate /*
2490Sstevel@tonic-gate  * The following variable enables a workaround for the following obp bug:
2500Sstevel@tonic-gate  *
2510Sstevel@tonic-gate  *	1234181 - obp should set latency timer registers in pci
2520Sstevel@tonic-gate  *		configuration header
2530Sstevel@tonic-gate  *
2540Sstevel@tonic-gate  * Until this bug gets fixed in the obp, the following workaround should
2550Sstevel@tonic-gate  * be enabled.
2560Sstevel@tonic-gate  */
2570Sstevel@tonic-gate static uint_t ppb_set_latency_timer_register = 1;
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate /*
2600Sstevel@tonic-gate  * The following variable enables a workaround for an obp bug to be
2610Sstevel@tonic-gate  * submitted.  A bug requesting a workaround fof this problem has
2620Sstevel@tonic-gate  * been filed:
2630Sstevel@tonic-gate  *
2640Sstevel@tonic-gate  *	1235094 - need workarounds on positron nexus drivers to set cache
2650Sstevel@tonic-gate  *		line size registers
2660Sstevel@tonic-gate  *
2670Sstevel@tonic-gate  * Until this bug gets fixed in the obp, the following workaround should
2680Sstevel@tonic-gate  * be enabled.
2690Sstevel@tonic-gate  */
2700Sstevel@tonic-gate static uint_t ppb_set_cache_line_size_register = 1;
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate /*
2730Sstevel@tonic-gate  * forward function declarations:
2740Sstevel@tonic-gate  */
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate /*
2770Sstevel@tonic-gate  * FMA error callback
2780Sstevel@tonic-gate  * Register error handling callback with our parent. We will just call
2790Sstevel@tonic-gate  * our children's error callbacks and return their status.
2800Sstevel@tonic-gate  */
2810Sstevel@tonic-gate static int ppb_err_callback(dev_info_t *dip, ddi_fm_error_t *derr,
2820Sstevel@tonic-gate 		const void *impl_data);
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate /*
2850Sstevel@tonic-gate  * init/fini routines to alloc/dealloc fm structures and
2860Sstevel@tonic-gate  * register/unregister our callback.
2870Sstevel@tonic-gate  */
2880Sstevel@tonic-gate static void ppb_fm_init(ppb_devstate_t *ppb_p);
2890Sstevel@tonic-gate static void ppb_fm_fini(ppb_devstate_t *ppb_p);
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate static void ppb_removechild(dev_info_t *);
2920Sstevel@tonic-gate static int ppb_initchild(dev_info_t *child);
2933274Set142600 static void ppb_uninitchild(dev_info_t *child);
2940Sstevel@tonic-gate static dev_info_t *get_my_childs_dip(dev_info_t *dip, dev_info_t *rdip);
2950Sstevel@tonic-gate static void ppb_pwr_setup(ppb_devstate_t *ppb, dev_info_t *dip);
2960Sstevel@tonic-gate static void ppb_pwr_teardown(ppb_devstate_t *ppb, dev_info_t *dip);
2970Sstevel@tonic-gate static void ppb_init_hotplug(ppb_devstate_t *ppb);
2980Sstevel@tonic-gate static void ppb_create_ranges_prop(dev_info_t *, ddi_acc_handle_t);
299964Smathue uint64_t pci_debug_flags = 0;
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate int
_init(void)3020Sstevel@tonic-gate _init(void)
3030Sstevel@tonic-gate {
3040Sstevel@tonic-gate 	int e;
3050Sstevel@tonic-gate 	if ((e = ddi_soft_state_init(&ppb_state, sizeof (ppb_devstate_t),
3060Sstevel@tonic-gate 	    1)) == 0 && (e = mod_install(&modlinkage)) != 0)
3070Sstevel@tonic-gate 		ddi_soft_state_fini(&ppb_state);
3080Sstevel@tonic-gate 	return (e);
3090Sstevel@tonic-gate }
3100Sstevel@tonic-gate 
3110Sstevel@tonic-gate int
_fini(void)3120Sstevel@tonic-gate _fini(void)
3130Sstevel@tonic-gate {
3140Sstevel@tonic-gate 	int e;
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate 	if ((e = mod_remove(&modlinkage)) == 0)
3170Sstevel@tonic-gate 		ddi_soft_state_fini(&ppb_state);
3180Sstevel@tonic-gate 	return (e);
3190Sstevel@tonic-gate }
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate int
_info(struct modinfo * modinfop)3220Sstevel@tonic-gate _info(struct modinfo *modinfop)
3230Sstevel@tonic-gate {
3240Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
3250Sstevel@tonic-gate }
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate /*ARGSUSED*/
3280Sstevel@tonic-gate static int
ppb_info(dev_info_t * dip,ddi_info_cmd_t cmd,void * arg,void ** result)32910923SEvan.Yan@Sun.COM ppb_info(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **result)
3300Sstevel@tonic-gate {
3310Sstevel@tonic-gate 	minor_t		minor = getminor((dev_t)arg);
33210923SEvan.Yan@Sun.COM 	int		instance = PCI_MINOR_NUM_TO_INSTANCE(minor);
33310923SEvan.Yan@Sun.COM 	ppb_devstate_t	*ppb_p = (ppb_devstate_t *)ddi_get_soft_state(ppb_state,
3340Sstevel@tonic-gate 	    instance);
3350Sstevel@tonic-gate 
33610923SEvan.Yan@Sun.COM 
33710923SEvan.Yan@Sun.COM 	if (ppb_p->parent_bus != PCIE_PCIECAP_DEV_TYPE_PCIE_DEV)
33810923SEvan.Yan@Sun.COM 		return (pcihp_info(dip, cmd, arg, result));
33910923SEvan.Yan@Sun.COM 
34010923SEvan.Yan@Sun.COM 	switch (cmd) {
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
ppb_probe(register dev_info_t * devi)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
ppb_attach(dev_info_t * devi,ddi_attach_cmd_t cmd)3650Sstevel@tonic-gate ppb_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
3660Sstevel@tonic-gate {
36710923SEvan.Yan@Sun.COM 	dev_info_t *root = ddi_root_node();
3680Sstevel@tonic-gate 	int instance;
3690Sstevel@tonic-gate 	ppb_devstate_t *ppb;
37010923SEvan.Yan@Sun.COM 	dev_info_t *pdip;
3710Sstevel@tonic-gate 	ddi_acc_handle_t config_handle;
37210923SEvan.Yan@Sun.COM 	char *bus;
3730Sstevel@tonic-gate 
3740Sstevel@tonic-gate 	switch (cmd) {
3750Sstevel@tonic-gate 	case DDI_ATTACH:
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate 		/*
3780Sstevel@tonic-gate 		 * Make sure the "device_type" property exists.
3790Sstevel@tonic-gate 		 */
3800Sstevel@tonic-gate 		(void) ddi_prop_update_string(DDI_DEV_T_NONE, devi,
3810Sstevel@tonic-gate 		    "device_type", "pci");
3820Sstevel@tonic-gate 
3830Sstevel@tonic-gate 		/*
3840Sstevel@tonic-gate 		 * Allocate and get soft state structure.
3850Sstevel@tonic-gate 		 */
3860Sstevel@tonic-gate 		instance = ddi_get_instance(devi);
3870Sstevel@tonic-gate 		if (ddi_soft_state_zalloc(ppb_state, instance) != DDI_SUCCESS)
3880Sstevel@tonic-gate 			return (DDI_FAILURE);
3890Sstevel@tonic-gate 		ppb = (ppb_devstate_t *)ddi_get_soft_state(ppb_state, instance);
3900Sstevel@tonic-gate 		ppb->dip = devi;
3910Sstevel@tonic-gate 		mutex_init(&ppb->ppb_mutex, NULL, MUTEX_DRIVER, NULL);
39210923SEvan.Yan@Sun.COM 		ppb->ppb_soft_state = PCI_SOFT_STATE_CLOSED;
3930Sstevel@tonic-gate 		if (pci_config_setup(devi, &config_handle) != DDI_SUCCESS) {
3940Sstevel@tonic-gate 			mutex_destroy(&ppb->ppb_mutex);
3950Sstevel@tonic-gate 			ddi_soft_state_free(ppb_state, instance);
3960Sstevel@tonic-gate 			return (DDI_FAILURE);
3970Sstevel@tonic-gate 		}
3980Sstevel@tonic-gate 		ppb_pwr_setup(ppb, devi);
3990Sstevel@tonic-gate 
4000Sstevel@tonic-gate 		if (PM_CAPABLE(ppb->ppb_pwr_p)) {
4010Sstevel@tonic-gate 			mutex_enter(&ppb->ppb_pwr_p->pwr_mutex);
4020Sstevel@tonic-gate 
4030Sstevel@tonic-gate 			/*
4040Sstevel@tonic-gate 			 * Before reading config registers, make sure power is
4050Sstevel@tonic-gate 			 * on, and remains on.
4060Sstevel@tonic-gate 			 */
4070Sstevel@tonic-gate 			ppb->ppb_pwr_p->pwr_fp++;
4080Sstevel@tonic-gate 
4090Sstevel@tonic-gate 			pci_pwr_change(ppb->ppb_pwr_p,
4100Sstevel@tonic-gate 			    ppb->ppb_pwr_p->current_lvl,
4110Sstevel@tonic-gate 			    pci_pwr_new_lvl(ppb->ppb_pwr_p));
4120Sstevel@tonic-gate 		}
4130Sstevel@tonic-gate 
4140Sstevel@tonic-gate 		ppb->ppb_cache_line_size =
4150Sstevel@tonic-gate 		    pci_config_get8(config_handle, PCI_CONF_CACHE_LINESZ);
4160Sstevel@tonic-gate 		ppb->ppb_latency_timer =
4170Sstevel@tonic-gate 		    pci_config_get8(config_handle, PCI_CONF_LATENCY_TIMER);
4180Sstevel@tonic-gate 
4190Sstevel@tonic-gate 		/*
4200Sstevel@tonic-gate 		 * Check whether the "ranges" property is present.
4210Sstevel@tonic-gate 		 * Otherwise create the ranges property by reading
4220Sstevel@tonic-gate 		 * the configuration registers
4230Sstevel@tonic-gate 		 */
4240Sstevel@tonic-gate 		if (ddi_prop_exists(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
4250Sstevel@tonic-gate 		    "ranges") == 0) {
4260Sstevel@tonic-gate 			ppb_create_ranges_prop(devi, config_handle);
4270Sstevel@tonic-gate 		}
4280Sstevel@tonic-gate 
4290Sstevel@tonic-gate 		pci_config_teardown(&config_handle);
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate 		if (PM_CAPABLE(ppb->ppb_pwr_p)) {
4320Sstevel@tonic-gate 			ppb->ppb_pwr_p->pwr_fp--;
4330Sstevel@tonic-gate 
4340Sstevel@tonic-gate 			pci_pwr_change(ppb->ppb_pwr_p,
4350Sstevel@tonic-gate 			    ppb->ppb_pwr_p->current_lvl,
4360Sstevel@tonic-gate 			    pci_pwr_new_lvl(ppb->ppb_pwr_p));
4370Sstevel@tonic-gate 
4380Sstevel@tonic-gate 			mutex_exit(&ppb->ppb_pwr_p->pwr_mutex);
4390Sstevel@tonic-gate 		}
4400Sstevel@tonic-gate 
44110923SEvan.Yan@Sun.COM 		ppb->parent_bus = PCIE_PCIECAP_DEV_TYPE_PCI_PSEUDO;
44210923SEvan.Yan@Sun.COM 		for (pdip = ddi_get_parent(ppb->dip); pdip && (pdip != root) &&
44310923SEvan.Yan@Sun.COM 		    (ppb->parent_bus != PCIE_PCIECAP_DEV_TYPE_PCIE_DEV);
44410923SEvan.Yan@Sun.COM 		    pdip = ddi_get_parent(pdip)) {
44510923SEvan.Yan@Sun.COM 			if (ddi_prop_lookup_string(DDI_DEV_T_ANY, pdip,
44610923SEvan.Yan@Sun.COM 			    DDI_PROP_DONTPASS, "device_type", &bus) !=
44710923SEvan.Yan@Sun.COM 			    DDI_PROP_SUCCESS)
44810923SEvan.Yan@Sun.COM 				break;
44910923SEvan.Yan@Sun.COM 
45010923SEvan.Yan@Sun.COM 			if (strcmp(bus, "pciex") == 0)
45110923SEvan.Yan@Sun.COM 				ppb->parent_bus =
45210923SEvan.Yan@Sun.COM 				    PCIE_PCIECAP_DEV_TYPE_PCIE_DEV;
45310923SEvan.Yan@Sun.COM 
45410923SEvan.Yan@Sun.COM 			ddi_prop_free(bus);
45510923SEvan.Yan@Sun.COM 		}
45610923SEvan.Yan@Sun.COM 
4570Sstevel@tonic-gate 		/*
45810923SEvan.Yan@Sun.COM 		 * Initialize hotplug support on this bus.
4590Sstevel@tonic-gate 		 */
46010923SEvan.Yan@Sun.COM 		if (ppb->parent_bus == PCIE_PCIECAP_DEV_TYPE_PCIE_DEV)
46110923SEvan.Yan@Sun.COM 			if (pcie_init(devi, NULL) != DDI_SUCCESS) {
46210923SEvan.Yan@Sun.COM 				(void) ppb_detach(devi, DDI_DETACH);
4630Sstevel@tonic-gate 				return (DDI_FAILURE);
4640Sstevel@tonic-gate 			}
46510923SEvan.Yan@Sun.COM 		else
46610923SEvan.Yan@Sun.COM 			ppb_init_hotplug(ppb);
4670Sstevel@tonic-gate 
4680Sstevel@tonic-gate 		DEBUG1(DBG_ATTACH, devi,
4697656SSherry.Moore@Sun.COM 		    "ppb_attach(): this nexus %s hotplug slots\n",
4707656SSherry.Moore@Sun.COM 		    ppb->hotplug_capable == B_TRUE ? "has":"has no");
4710Sstevel@tonic-gate 
4720Sstevel@tonic-gate 		ppb_fm_init(ppb);
4730Sstevel@tonic-gate 		ddi_report_dev(devi);
4740Sstevel@tonic-gate 
4750Sstevel@tonic-gate 		return (DDI_SUCCESS);
4760Sstevel@tonic-gate 
4770Sstevel@tonic-gate 	case DDI_RESUME:
4780Sstevel@tonic-gate 		/*
4790Sstevel@tonic-gate 		 * Get the soft state structure for the bridge.
4800Sstevel@tonic-gate 		 */
4810Sstevel@tonic-gate 		ppb = (ppb_devstate_t *)
4827656SSherry.Moore@Sun.COM 		    ddi_get_soft_state(ppb_state, ddi_get_instance(devi));
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate 		pci_pwr_resume(devi, ppb->ppb_pwr_p);
4850Sstevel@tonic-gate 
4860Sstevel@tonic-gate 		return (DDI_SUCCESS);
4870Sstevel@tonic-gate 	}
4880Sstevel@tonic-gate 	return (DDI_FAILURE);
4890Sstevel@tonic-gate }
4900Sstevel@tonic-gate 
4910Sstevel@tonic-gate /*ARGSUSED*/
4920Sstevel@tonic-gate static int
ppb_detach(dev_info_t * devi,ddi_detach_cmd_t cmd)4930Sstevel@tonic-gate ppb_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
4940Sstevel@tonic-gate {
4950Sstevel@tonic-gate 	ppb_devstate_t *ppb;
49610923SEvan.Yan@Sun.COM 	int		ret = DDI_SUCCESS;
4970Sstevel@tonic-gate 
4980Sstevel@tonic-gate 	switch (cmd) {
4990Sstevel@tonic-gate 	case DDI_DETACH:
5000Sstevel@tonic-gate 		/*
5010Sstevel@tonic-gate 		 * And finally free the per-pci soft state after
5020Sstevel@tonic-gate 		 * uninitializing hotplug support for this bus.
5030Sstevel@tonic-gate 		 */
5040Sstevel@tonic-gate 		ppb = (ppb_devstate_t *)
5050Sstevel@tonic-gate 		    ddi_get_soft_state(ppb_state, ddi_get_instance(devi));
5060Sstevel@tonic-gate 
5070Sstevel@tonic-gate 		ppb_fm_fini(ppb);
5080Sstevel@tonic-gate 
50910923SEvan.Yan@Sun.COM 		if (ppb->parent_bus == PCIE_PCIECAP_DEV_TYPE_PCIE_DEV)
51010923SEvan.Yan@Sun.COM 			ret = pcie_uninit(devi);
51110923SEvan.Yan@Sun.COM 		else if (ppb->hotplug_capable == B_TRUE)
51210923SEvan.Yan@Sun.COM 			ret = pcihp_init(devi);
5130Sstevel@tonic-gate 		else
5140Sstevel@tonic-gate 			ddi_remove_minor_node(devi, "devctl");
5150Sstevel@tonic-gate 
51610923SEvan.Yan@Sun.COM 		if (ret != DDI_SUCCESS)
51710923SEvan.Yan@Sun.COM 			return (DDI_FAILURE);
51810923SEvan.Yan@Sun.COM 
5190Sstevel@tonic-gate 		(void) ddi_prop_remove(DDI_DEV_T_NONE, devi, "device_type");
5200Sstevel@tonic-gate 
5210Sstevel@tonic-gate 		if (ppb->ppb_pwr_p != NULL) {
5220Sstevel@tonic-gate 			ppb_pwr_teardown(ppb, devi);
5230Sstevel@tonic-gate 		}
5240Sstevel@tonic-gate 		mutex_destroy(&ppb->ppb_mutex);
5250Sstevel@tonic-gate 		ddi_soft_state_free(ppb_state, ddi_get_instance(devi));
5260Sstevel@tonic-gate 
5270Sstevel@tonic-gate 		return (DDI_SUCCESS);
5280Sstevel@tonic-gate 
5290Sstevel@tonic-gate 	case DDI_SUSPEND:
5300Sstevel@tonic-gate 		ppb = (ppb_devstate_t *)
5317656SSherry.Moore@Sun.COM 		    ddi_get_soft_state(ppb_state, ddi_get_instance(devi));
5320Sstevel@tonic-gate 
5330Sstevel@tonic-gate 		pci_pwr_suspend(devi, ppb->ppb_pwr_p);
5340Sstevel@tonic-gate 
5350Sstevel@tonic-gate 		return (DDI_SUCCESS);
5360Sstevel@tonic-gate 	}
5370Sstevel@tonic-gate 	return (DDI_FAILURE);
5380Sstevel@tonic-gate }
5390Sstevel@tonic-gate 
5400Sstevel@tonic-gate /*ARGSUSED*/
5410Sstevel@tonic-gate static int
ppb_bus_map(dev_info_t * dip,dev_info_t * rdip,ddi_map_req_t * mp,off_t offset,off_t len,caddr_t * vaddrp)5420Sstevel@tonic-gate ppb_bus_map(dev_info_t *dip, dev_info_t *rdip, ddi_map_req_t *mp,
5430Sstevel@tonic-gate 	off_t offset, off_t len, caddr_t *vaddrp)
5440Sstevel@tonic-gate {
5450Sstevel@tonic-gate 	register dev_info_t *pdip;
5460Sstevel@tonic-gate 
5470Sstevel@tonic-gate 	pdip = (dev_info_t *)DEVI(dip)->devi_parent;
5480Sstevel@tonic-gate 	return ((DEVI(pdip)->devi_ops->devo_bus_ops->bus_map)
5490Sstevel@tonic-gate 	    (pdip, rdip, mp, offset, len, vaddrp));
5500Sstevel@tonic-gate }
5510Sstevel@tonic-gate 
5520Sstevel@tonic-gate /*ARGSUSED*/
5530Sstevel@tonic-gate static int
ppb_ctlops(dev_info_t * dip,dev_info_t * rdip,ddi_ctl_enum_t ctlop,void * arg,void * result)5540Sstevel@tonic-gate ppb_ctlops(dev_info_t *dip, dev_info_t *rdip,
5550Sstevel@tonic-gate 	ddi_ctl_enum_t ctlop, void *arg, void *result)
5560Sstevel@tonic-gate {
5570Sstevel@tonic-gate 	pci_regspec_t *drv_regp;
5580Sstevel@tonic-gate 	int	reglen;
5590Sstevel@tonic-gate 	int	rn;
5603274Set142600 	struct	attachspec *as;
5613274Set142600 	struct	detachspec *ds;
5623274Set142600 	int	totreg;
5633274Set142600 	ppb_devstate_t *ppb_p;
5643272Sdduvall 
5653274Set142600 	ppb_p = (ppb_devstate_t *)ddi_get_soft_state(ppb_state,
5663274Set142600 	    ddi_get_instance(dip));
5670Sstevel@tonic-gate 
5680Sstevel@tonic-gate 	switch (ctlop) {
5690Sstevel@tonic-gate 	case DDI_CTLOPS_REPORTDEV:
5700Sstevel@tonic-gate 		if (rdip == (dev_info_t *)0)
5710Sstevel@tonic-gate 			return (DDI_FAILURE);
5720Sstevel@tonic-gate 		cmn_err(CE_CONT, "?PCI-device: %s@%s, %s%d\n",
5730Sstevel@tonic-gate 		    ddi_node_name(rdip), ddi_get_name_addr(rdip),
5740Sstevel@tonic-gate 		    ddi_driver_name(rdip),
5750Sstevel@tonic-gate 		    ddi_get_instance(rdip));
5760Sstevel@tonic-gate 		return (DDI_SUCCESS);
5770Sstevel@tonic-gate 
5780Sstevel@tonic-gate 	case DDI_CTLOPS_INITCHILD:
5790Sstevel@tonic-gate 		return (ppb_initchild((dev_info_t *)arg));
5800Sstevel@tonic-gate 
5810Sstevel@tonic-gate 	case DDI_CTLOPS_UNINITCHILD:
5823274Set142600 		ppb_uninitchild((dev_info_t *)arg);
5833274Set142600 		return (DDI_SUCCESS);
5843274Set142600 
5853274Set142600 	case DDI_CTLOPS_ATTACH:
5863274Set142600 		if (!pcie_is_child(dip, rdip))
5873274Set142600 			return (DDI_SUCCESS);
5883274Set142600 
5893274Set142600 		as = (struct attachspec *)arg;
5903274Set142600 		if ((ppb_p->parent_bus == PCIE_PCIECAP_DEV_TYPE_PCIE_DEV) &&
5914005Skrishnae 		    (as->when == DDI_POST) && (as->result == DDI_SUCCESS))
5923756Skrishnae 			pf_init(rdip, ppb_p->fm_ibc, as->cmd);
5933274Set142600 
5943274Set142600 		return (DDI_SUCCESS);
5953274Set142600 
5963274Set142600 	case DDI_CTLOPS_DETACH:
5973274Set142600 		if (!pcie_is_child(dip, rdip))
5983274Set142600 			return (DDI_SUCCESS);
5993274Set142600 
6003274Set142600 		ds = (struct detachspec *)arg;
6013274Set142600 		if ((ppb_p->parent_bus == PCIE_PCIECAP_DEV_TYPE_PCIE_DEV) &&
6023274Set142600 		    (ds->when == DDI_PRE))
6033756Skrishnae 			pf_fini(rdip, ds->cmd);
6043274Set142600 
6050Sstevel@tonic-gate 		return (DDI_SUCCESS);
6060Sstevel@tonic-gate 
6070Sstevel@tonic-gate 	case DDI_CTLOPS_SIDDEV:
6080Sstevel@tonic-gate 		return (DDI_SUCCESS);
6090Sstevel@tonic-gate 
6100Sstevel@tonic-gate 	case DDI_CTLOPS_REGSIZE:
6110Sstevel@tonic-gate 	case DDI_CTLOPS_NREGS:
6120Sstevel@tonic-gate 		if (rdip == (dev_info_t *)0)
6130Sstevel@tonic-gate 			return (DDI_FAILURE);
6140Sstevel@tonic-gate 		break;
6150Sstevel@tonic-gate 	default:
6160Sstevel@tonic-gate 		return (ddi_ctlops(dip, rdip, ctlop, arg, result));
6170Sstevel@tonic-gate 	}
6180Sstevel@tonic-gate 
6190Sstevel@tonic-gate 	*(int *)result = 0;
620506Scth 	if (ddi_getlongprop(DDI_DEV_T_ANY, rdip,
6217656SSherry.Moore@Sun.COM 	    DDI_PROP_DONTPASS | DDI_PROP_CANSLEEP, "reg",
6227656SSherry.Moore@Sun.COM 	    (caddr_t)&drv_regp, &reglen) != DDI_SUCCESS)
6230Sstevel@tonic-gate 		return (DDI_FAILURE);
6240Sstevel@tonic-gate 
6250Sstevel@tonic-gate 	totreg = reglen / sizeof (pci_regspec_t);
6260Sstevel@tonic-gate 	if (ctlop == DDI_CTLOPS_NREGS)
6270Sstevel@tonic-gate 		*(int *)result = totreg;
6280Sstevel@tonic-gate 	else if (ctlop == DDI_CTLOPS_REGSIZE) {
6290Sstevel@tonic-gate 		rn = *(int *)arg;
6300Sstevel@tonic-gate 		if (rn >= totreg) {
6310Sstevel@tonic-gate 			kmem_free(drv_regp, reglen);
6320Sstevel@tonic-gate 			return (DDI_FAILURE);
6330Sstevel@tonic-gate 		}
6340Sstevel@tonic-gate 		*(off_t *)result = drv_regp[rn].pci_size_low |
6357656SSherry.Moore@Sun.COM 		    ((uint64_t)drv_regp[rn].pci_size_hi << 32);
6360Sstevel@tonic-gate 	}
6370Sstevel@tonic-gate 
6380Sstevel@tonic-gate 	kmem_free(drv_regp, reglen);
6390Sstevel@tonic-gate 	return (DDI_SUCCESS);
6400Sstevel@tonic-gate }
6410Sstevel@tonic-gate 
6420Sstevel@tonic-gate 
6430Sstevel@tonic-gate static dev_info_t *
get_my_childs_dip(dev_info_t * dip,dev_info_t * rdip)6440Sstevel@tonic-gate get_my_childs_dip(dev_info_t *dip, dev_info_t *rdip)
6450Sstevel@tonic-gate {
6460Sstevel@tonic-gate 	dev_info_t *cdip = rdip;
6470Sstevel@tonic-gate 
6480Sstevel@tonic-gate 	for (; ddi_get_parent(cdip) != dip; cdip = ddi_get_parent(cdip))
6490Sstevel@tonic-gate 		;
6500Sstevel@tonic-gate 
6510Sstevel@tonic-gate 	return (cdip);
6520Sstevel@tonic-gate }
6530Sstevel@tonic-gate 
6540Sstevel@tonic-gate 
6550Sstevel@tonic-gate static int
ppb_intr_ops(dev_info_t * dip,dev_info_t * rdip,ddi_intr_op_t intr_op,ddi_intr_handle_impl_t * hdlp,void * result)6560Sstevel@tonic-gate ppb_intr_ops(dev_info_t *dip, dev_info_t *rdip, ddi_intr_op_t intr_op,
6570Sstevel@tonic-gate     ddi_intr_handle_impl_t *hdlp, void *result)
6580Sstevel@tonic-gate {
659693Sgovinda 	dev_info_t	*cdip = rdip;
660693Sgovinda 	pci_regspec_t	*pci_rp;
661693Sgovinda 	int		reglen, len;
662693Sgovinda 	uint32_t	d, intr;
6630Sstevel@tonic-gate 
6644395Sgovinda 	if ((intr_op == DDI_INTROP_SUPPORTED_TYPES) ||
6654395Sgovinda 	    (hdlp->ih_type != DDI_INTR_TYPE_FIXED))
6660Sstevel@tonic-gate 		goto done;
6670Sstevel@tonic-gate 
6680Sstevel@tonic-gate 	/*
6690Sstevel@tonic-gate 	 * If the interrupt-map property is defined at this
6700Sstevel@tonic-gate 	 * node, it will have performed the interrupt
6710Sstevel@tonic-gate 	 * translation as part of the property, so no
6720Sstevel@tonic-gate 	 * rotation needs to be done.
6730Sstevel@tonic-gate 	 */
6740Sstevel@tonic-gate 	if (ddi_getproplen(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
6750Sstevel@tonic-gate 	    "interrupt-map", &len) == DDI_PROP_SUCCESS)
6760Sstevel@tonic-gate 		goto done;
6770Sstevel@tonic-gate 
6780Sstevel@tonic-gate 	cdip = get_my_childs_dip(dip, rdip);
6790Sstevel@tonic-gate 
6800Sstevel@tonic-gate 	/*
6810Sstevel@tonic-gate 	 * Use the devices reg property to determine its
6820Sstevel@tonic-gate 	 * PCI bus number and device number.
6830Sstevel@tonic-gate 	 */
684506Scth 	if (ddi_getlongprop(DDI_DEV_T_ANY, cdip, DDI_PROP_DONTPASS,
6850Sstevel@tonic-gate 	    "reg", (caddr_t)&pci_rp, &reglen) != DDI_SUCCESS)
6860Sstevel@tonic-gate 		return (DDI_FAILURE);
6870Sstevel@tonic-gate 
688693Sgovinda 	intr = hdlp->ih_vector;
6890Sstevel@tonic-gate 
6900Sstevel@tonic-gate 	/* Spin the interrupt */
6910Sstevel@tonic-gate 	d = PCI_REG_DEV_G(pci_rp[0].pci_phys_hi);
6920Sstevel@tonic-gate 
6930Sstevel@tonic-gate 	if ((intr >= PCI_INTA) && (intr <= PCI_INTD))
694693Sgovinda 		hdlp->ih_vector = ((intr - 1 + (d % 4)) % 4 + 1);
6950Sstevel@tonic-gate 	else
6960Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d: %s: PCI intr=%x out of range",
6970Sstevel@tonic-gate 		    ddi_driver_name(rdip), ddi_get_instance(rdip),
6980Sstevel@tonic-gate 		    ddi_driver_name(dip), intr);
6990Sstevel@tonic-gate 
7000Sstevel@tonic-gate 	kmem_free(pci_rp, reglen);
7010Sstevel@tonic-gate 
7020Sstevel@tonic-gate done:
7030Sstevel@tonic-gate 	/* Pass up the request to our parent. */
7040Sstevel@tonic-gate 	return (i_ddi_intr_ops(dip, rdip, intr_op, hdlp, result));
7050Sstevel@tonic-gate }
7060Sstevel@tonic-gate 
7070Sstevel@tonic-gate static int
ppb_bus_power(dev_info_t * dip,void * impl_arg,pm_bus_power_op_t op,void * arg,void * result)7080Sstevel@tonic-gate ppb_bus_power(dev_info_t *dip, void *impl_arg, pm_bus_power_op_t op,
7090Sstevel@tonic-gate     void *arg, void *result)
7100Sstevel@tonic-gate {
7110Sstevel@tonic-gate 	ppb_devstate_t *ppb;
7120Sstevel@tonic-gate 
7130Sstevel@tonic-gate 	ppb = (ppb_devstate_t *)ddi_get_soft_state(ppb_state,
7140Sstevel@tonic-gate 	    ddi_get_instance(dip));
7150Sstevel@tonic-gate 
7160Sstevel@tonic-gate 	return (pci_pwr_ops(ppb->ppb_pwr_p, dip, impl_arg, op, arg, result));
7170Sstevel@tonic-gate }
7180Sstevel@tonic-gate 
7190Sstevel@tonic-gate 
7200Sstevel@tonic-gate /*
7210Sstevel@tonic-gate  * name_child
7220Sstevel@tonic-gate  *
7230Sstevel@tonic-gate  * This function is called from init_child to name a node. It is
7240Sstevel@tonic-gate  * also passed as a callback for node merging functions.
7250Sstevel@tonic-gate  *
7260Sstevel@tonic-gate  * return value: DDI_SUCCESS, DDI_FAILURE
7270Sstevel@tonic-gate  */
7280Sstevel@tonic-gate static int
ppb_name_child(dev_info_t * child,char * name,int namelen)7290Sstevel@tonic-gate ppb_name_child(dev_info_t *child, char *name, int namelen)
7300Sstevel@tonic-gate {
7310Sstevel@tonic-gate 	pci_regspec_t *pci_rp;
7320Sstevel@tonic-gate 	uint_t slot, func;
7330Sstevel@tonic-gate 	char **unit_addr;
7340Sstevel@tonic-gate 	uint_t n;
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate 	/*
7370Sstevel@tonic-gate 	 * Pseudo nodes indicate a prototype node with per-instance
7380Sstevel@tonic-gate 	 * properties to be merged into the real h/w device node.
7390Sstevel@tonic-gate 	 * The interpretation of the unit-address is DD[,F]
7400Sstevel@tonic-gate 	 * where DD is the device id and F is the function.
7410Sstevel@tonic-gate 	 */
7420Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node(child) == 0) {
7430Sstevel@tonic-gate 		if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, child,
7440Sstevel@tonic-gate 		    DDI_PROP_DONTPASS, "unit-address", &unit_addr, &n) !=
7450Sstevel@tonic-gate 		    DDI_PROP_SUCCESS) {
7460Sstevel@tonic-gate 			cmn_err(CE_WARN, "cannot name node from %s.conf",
7470Sstevel@tonic-gate 			    ddi_driver_name(child));
7480Sstevel@tonic-gate 			return (DDI_FAILURE);
7490Sstevel@tonic-gate 		}
7500Sstevel@tonic-gate 		if (n != 1 || *unit_addr == NULL || **unit_addr == 0) {
7510Sstevel@tonic-gate 			cmn_err(CE_WARN, "unit-address property in %s.conf"
7520Sstevel@tonic-gate 			    " not well-formed", ddi_driver_name(child));
7530Sstevel@tonic-gate 			ddi_prop_free(unit_addr);
7540Sstevel@tonic-gate 			return (DDI_FAILURE);
7550Sstevel@tonic-gate 		}
7560Sstevel@tonic-gate 		(void) snprintf(name, namelen, "%s", *unit_addr);
7570Sstevel@tonic-gate 		ddi_prop_free(unit_addr);
7580Sstevel@tonic-gate 		return (DDI_SUCCESS);
7590Sstevel@tonic-gate 	}
7600Sstevel@tonic-gate 
7610Sstevel@tonic-gate 	/*
7620Sstevel@tonic-gate 	 * Get the address portion of the node name based on
7630Sstevel@tonic-gate 	 * the function and device number.
7640Sstevel@tonic-gate 	 */
7650Sstevel@tonic-gate 	if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS,
7660Sstevel@tonic-gate 	    "reg", (int **)&pci_rp, &n) != DDI_SUCCESS) {
7670Sstevel@tonic-gate 		return (DDI_FAILURE);
7680Sstevel@tonic-gate 	}
7690Sstevel@tonic-gate 
7700Sstevel@tonic-gate 	slot = PCI_REG_DEV_G(pci_rp[0].pci_phys_hi);
7710Sstevel@tonic-gate 	func = PCI_REG_FUNC_G(pci_rp[0].pci_phys_hi);
7720Sstevel@tonic-gate 
7730Sstevel@tonic-gate 	if (func != 0)
7740Sstevel@tonic-gate 		(void) snprintf(name, namelen, "%x,%x", slot, func);
7750Sstevel@tonic-gate 	else
7760Sstevel@tonic-gate 		(void) snprintf(name, namelen, "%x", slot);
7770Sstevel@tonic-gate 
7780Sstevel@tonic-gate 	ddi_prop_free(pci_rp);
7790Sstevel@tonic-gate 	return (DDI_SUCCESS);
7800Sstevel@tonic-gate }
7810Sstevel@tonic-gate 
7820Sstevel@tonic-gate static int
ppb_initchild(dev_info_t * child)7830Sstevel@tonic-gate ppb_initchild(dev_info_t *child)
7840Sstevel@tonic-gate {
7850Sstevel@tonic-gate 	char name[MAXNAMELEN];
7860Sstevel@tonic-gate 	ddi_acc_handle_t config_handle;
7870Sstevel@tonic-gate 	ushort_t command_preserve, command;
7880Sstevel@tonic-gate 	uint_t n;
7890Sstevel@tonic-gate 	ushort_t bcr;
7900Sstevel@tonic-gate 	uchar_t header_type;
7910Sstevel@tonic-gate 	uchar_t min_gnt, latency_timer;
7920Sstevel@tonic-gate 	ppb_devstate_t *ppb;
7930Sstevel@tonic-gate 
7940Sstevel@tonic-gate 	/*
7950Sstevel@tonic-gate 	 * Name the child
7960Sstevel@tonic-gate 	 */
7970Sstevel@tonic-gate 	if (ppb_name_child(child, name, MAXNAMELEN) != DDI_SUCCESS)
7980Sstevel@tonic-gate 		return (DDI_FAILURE);
7990Sstevel@tonic-gate 
8000Sstevel@tonic-gate 	ddi_set_name_addr(child, name);
8010Sstevel@tonic-gate 	ddi_set_parent_data(child, NULL);
8020Sstevel@tonic-gate 
8030Sstevel@tonic-gate 	/*
8040Sstevel@tonic-gate 	 * Pseudo nodes indicate a prototype node with per-instance
8050Sstevel@tonic-gate 	 * properties to be merged into the real h/w device node.
8060Sstevel@tonic-gate 	 * The interpretation of the unit-address is DD[,F]
8070Sstevel@tonic-gate 	 * where DD is the device id and F is the function.
8080Sstevel@tonic-gate 	 */
8090Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node(child) == 0) {
8100Sstevel@tonic-gate 		extern int pci_allow_pseudo_children;
8110Sstevel@tonic-gate 
8120Sstevel@tonic-gate 		/*
8130Sstevel@tonic-gate 		 * Try to merge the properties from this prototype
8140Sstevel@tonic-gate 		 * node into real h/w nodes.
8150Sstevel@tonic-gate 		 */
8160Sstevel@tonic-gate 		if (ndi_merge_node(child, ppb_name_child) == DDI_SUCCESS) {
8170Sstevel@tonic-gate 			/*
8180Sstevel@tonic-gate 			 * Merged ok - return failure to remove the node.
8190Sstevel@tonic-gate 			 */
8200Sstevel@tonic-gate 			ppb_removechild(child);
8210Sstevel@tonic-gate 			return (DDI_FAILURE);
8220Sstevel@tonic-gate 		}
8230Sstevel@tonic-gate 
8240Sstevel@tonic-gate 		/* workaround for ddivs to run under PCI */
8250Sstevel@tonic-gate 		if (pci_allow_pseudo_children)
8260Sstevel@tonic-gate 			return (DDI_SUCCESS);
8270Sstevel@tonic-gate 
8280Sstevel@tonic-gate 		/*
8290Sstevel@tonic-gate 		 * The child was not merged into a h/w node,
8300Sstevel@tonic-gate 		 * but there's not much we can do with it other
8310Sstevel@tonic-gate 		 * than return failure to cause the node to be removed.
8320Sstevel@tonic-gate 		 */
8330Sstevel@tonic-gate 		cmn_err(CE_WARN, "!%s@%s: %s.conf properties not merged",
8340Sstevel@tonic-gate 		    ddi_driver_name(child), ddi_get_name_addr(child),
8350Sstevel@tonic-gate 		    ddi_driver_name(child));
8360Sstevel@tonic-gate 		ppb_removechild(child);
8370Sstevel@tonic-gate 		return (DDI_NOT_WELL_FORMED);
8380Sstevel@tonic-gate 	}
8390Sstevel@tonic-gate 
8400Sstevel@tonic-gate 	ppb = (ppb_devstate_t *)ddi_get_soft_state(ppb_state,
8410Sstevel@tonic-gate 	    ddi_get_instance(ddi_get_parent(child)));
8420Sstevel@tonic-gate 
8433274Set142600 	ddi_set_parent_data(child, NULL);
8443274Set142600 
8450Sstevel@tonic-gate 	/*
8460Sstevel@tonic-gate 	 * If hardware is PM capable, set up the power info structure.
8470Sstevel@tonic-gate 	 * This also ensures the the bus will not be off (0MHz) otherwise
8480Sstevel@tonic-gate 	 * system panics during a bus access.
8490Sstevel@tonic-gate 	 */
8500Sstevel@tonic-gate 	if (PM_CAPABLE(ppb->ppb_pwr_p)) {
8510Sstevel@tonic-gate 		/*
8520Sstevel@tonic-gate 		 * Create a pwr_info struct for child.  Bus will be
8530Sstevel@tonic-gate 		 * at full speed after creating info.
8540Sstevel@tonic-gate 		 */
8550Sstevel@tonic-gate 		pci_pwr_create_info(ppb->ppb_pwr_p, child);
8560Sstevel@tonic-gate #ifdef DEBUG
8570Sstevel@tonic-gate 		ASSERT(ppb->ppb_pwr_p->current_lvl == PM_LEVEL_B0);
8580Sstevel@tonic-gate #endif
8590Sstevel@tonic-gate 	}
8600Sstevel@tonic-gate 
8610Sstevel@tonic-gate 	/*
8620Sstevel@tonic-gate 	 * If configuration registers were previously saved by
8630Sstevel@tonic-gate 	 * child (before it entered D3), then let the child do the
8640Sstevel@tonic-gate 	 * restore to set up the config regs as it'll first need to
8650Sstevel@tonic-gate 	 * power the device out of D3.
8660Sstevel@tonic-gate 	 */
8670Sstevel@tonic-gate 	if (ddi_prop_exists(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS,
8680Sstevel@tonic-gate 	    "config-regs-saved-by-child") == 1) {
8690Sstevel@tonic-gate 		DEBUG2(DBG_PWR, ddi_get_parent(child),
8707656SSherry.Moore@Sun.COM 		    "INITCHILD: config regs to be restored by child"
8717656SSherry.Moore@Sun.COM 		    " for %s@%s\n", ddi_node_name(child),
8727656SSherry.Moore@Sun.COM 		    ddi_get_name_addr(child));
8730Sstevel@tonic-gate 
8740Sstevel@tonic-gate 		return (DDI_SUCCESS);
8750Sstevel@tonic-gate 	}
8760Sstevel@tonic-gate 
8770Sstevel@tonic-gate 	DEBUG2(DBG_PWR, ddi_get_parent(child),
8780Sstevel@tonic-gate 	    "INITCHILD: config regs setup for %s@%s\n",
8790Sstevel@tonic-gate 	    ddi_node_name(child), ddi_get_name_addr(child));
8800Sstevel@tonic-gate 
8810Sstevel@tonic-gate 	if (pci_config_setup(child, &config_handle) != DDI_SUCCESS) {
8820Sstevel@tonic-gate 		if (PM_CAPABLE(ppb->ppb_pwr_p)) {
8830Sstevel@tonic-gate 			pci_pwr_rm_info(ppb->ppb_pwr_p, child);
8840Sstevel@tonic-gate 		}
8850Sstevel@tonic-gate 
8860Sstevel@tonic-gate 		return (DDI_FAILURE);
8870Sstevel@tonic-gate 	}
8880Sstevel@tonic-gate 
8890Sstevel@tonic-gate 	/*
8900Sstevel@tonic-gate 	 * Determine the configuration header type.
8910Sstevel@tonic-gate 	 */
8920Sstevel@tonic-gate 	header_type = pci_config_get8(config_handle, PCI_CONF_HEADER);
8930Sstevel@tonic-gate 
8940Sstevel@tonic-gate 	/*
8950Sstevel@tonic-gate 	 * Support for the "command-preserve" property.
8960Sstevel@tonic-gate 	 */
8970Sstevel@tonic-gate 	command_preserve = ddi_prop_get_int(DDI_DEV_T_ANY, child,
8987656SSherry.Moore@Sun.COM 	    DDI_PROP_DONTPASS, "command-preserve", 0);
8990Sstevel@tonic-gate 	command = pci_config_get16(config_handle, PCI_CONF_COMM);
9000Sstevel@tonic-gate 	command &= (command_preserve | PCI_COMM_BACK2BACK_ENAB);
9010Sstevel@tonic-gate 	command |= (ppb_command_default & ~command_preserve);
9020Sstevel@tonic-gate 	pci_config_put16(config_handle, PCI_CONF_COMM, command);
9030Sstevel@tonic-gate 
9040Sstevel@tonic-gate 	/*
9050Sstevel@tonic-gate 	 * If the device has a bus control register then program it
9060Sstevel@tonic-gate 	 * based on the settings in the command register.
9070Sstevel@tonic-gate 	 */
9083274Set142600 	if ((header_type  & PCI_HEADER_TYPE_M) == PCI_HEADER_ONE) {
9090Sstevel@tonic-gate 		bcr = pci_config_get8(config_handle, PCI_BCNF_BCNTRL);
9100Sstevel@tonic-gate 		if (ppb_command_default & PCI_COMM_PARITY_DETECT)
9110Sstevel@tonic-gate 			bcr |= PCI_BCNF_BCNTRL_PARITY_ENABLE;
9120Sstevel@tonic-gate 		if (ppb_command_default & PCI_COMM_SERR_ENABLE)
9130Sstevel@tonic-gate 			bcr |= PCI_BCNF_BCNTRL_SERR_ENABLE;
9140Sstevel@tonic-gate 		bcr |= PCI_BCNF_BCNTRL_MAST_AB_MODE;
9150Sstevel@tonic-gate 		pci_config_put8(config_handle, PCI_BCNF_BCNTRL, bcr);
9160Sstevel@tonic-gate 	}
9170Sstevel@tonic-gate 
9180Sstevel@tonic-gate 	/*
9190Sstevel@tonic-gate 	 * Initialize cache-line-size configuration register if needed.
9200Sstevel@tonic-gate 	 */
9210Sstevel@tonic-gate 	if (ppb_set_cache_line_size_register &&
9220Sstevel@tonic-gate 	    ddi_getprop(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS,
9237656SSherry.Moore@Sun.COM 	    "cache-line-size", 0) == 0) {
9240Sstevel@tonic-gate 		pci_config_put8(config_handle, PCI_CONF_CACHE_LINESZ,
9257656SSherry.Moore@Sun.COM 		    ppb->ppb_cache_line_size);
9260Sstevel@tonic-gate 		n = pci_config_get8(config_handle, PCI_CONF_CACHE_LINESZ);
9270Sstevel@tonic-gate 		if (n != 0) {
9280Sstevel@tonic-gate 			(void) ndi_prop_update_int(DDI_DEV_T_NONE, child,
9297656SSherry.Moore@Sun.COM 			    "cache-line-size", n);
9300Sstevel@tonic-gate 		}
9310Sstevel@tonic-gate 	}
9320Sstevel@tonic-gate 
9330Sstevel@tonic-gate 	/*
9340Sstevel@tonic-gate 	 * Initialize latency timer configuration registers if needed.
9350Sstevel@tonic-gate 	 */
9360Sstevel@tonic-gate 	if (ppb_set_latency_timer_register &&
9370Sstevel@tonic-gate 	    ddi_getprop(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS,
9387656SSherry.Moore@Sun.COM 	    "latency-timer", 0) == 0) {
9390Sstevel@tonic-gate 
9400Sstevel@tonic-gate 		if ((header_type & PCI_HEADER_TYPE_M) == PCI_HEADER_ONE) {
9410Sstevel@tonic-gate 			latency_timer = ppb->ppb_latency_timer;
9420Sstevel@tonic-gate 			pci_config_put8(config_handle, PCI_BCNF_LATENCY_TIMER,
9437656SSherry.Moore@Sun.COM 			    ppb->ppb_latency_timer);
9440Sstevel@tonic-gate 		} else {
9450Sstevel@tonic-gate 			min_gnt = pci_config_get8(config_handle,
9467656SSherry.Moore@Sun.COM 			    PCI_CONF_MIN_G);
9470Sstevel@tonic-gate 			latency_timer = min_gnt * 8;
9480Sstevel@tonic-gate 		}
9490Sstevel@tonic-gate 		pci_config_put8(config_handle, PCI_CONF_LATENCY_TIMER,
9507656SSherry.Moore@Sun.COM 		    latency_timer);
9510Sstevel@tonic-gate 		n = pci_config_get8(config_handle, PCI_CONF_LATENCY_TIMER);
9520Sstevel@tonic-gate 		if (n != 0) {
9530Sstevel@tonic-gate 			(void) ndi_prop_update_int(DDI_DEV_T_NONE, child,
9547656SSherry.Moore@Sun.COM 			    "latency-timer", n);
9550Sstevel@tonic-gate 		}
9560Sstevel@tonic-gate 	}
9570Sstevel@tonic-gate 
9580Sstevel@tonic-gate 	/*
9593274Set142600 	 * SPARC PCIe FMA specific
9603274Set142600 	 *
9613274Set142600 	 * Note: parent_data for parent is created only if this is sparc PCI-E
9623274Set142600 	 * platform, for which, SG take a different route to handle device
9633274Set142600 	 * errors.
9643274Set142600 	 */
9653274Set142600 	if (ppb->parent_bus == PCIE_PCIECAP_DEV_TYPE_PCIE_DEV) {
96611245SZhijun.Fu@Sun.COM 		if (pcie_init_cfghdl(child) != DDI_SUCCESS) {
9673724Srameshc 			pci_config_teardown(&config_handle);
9683274Set142600 			return (DDI_FAILURE);
9693724Srameshc 		}
970*11596SJason.Beloro@Sun.COM 		pcie_init_dom(child);
9713274Set142600 	}
9723274Set142600 
9733274Set142600 	/*
9740Sstevel@tonic-gate 	 * Check to see if the XMITS/PCI-X workaround applies.
9750Sstevel@tonic-gate 	 */
9760Sstevel@tonic-gate 	n = ddi_getprop(DDI_DEV_T_ANY, child, DDI_PROP_NOTPROM,
9770Sstevel@tonic-gate 	    "pcix-update-cmd-reg", -1);
9780Sstevel@tonic-gate 
9790Sstevel@tonic-gate 	if (n != -1) {
9800Sstevel@tonic-gate 		extern void pcix_set_cmd_reg(dev_info_t *child, uint16_t value);
9810Sstevel@tonic-gate 		DEBUG1(DBG_INIT_CLD, child, "Turning on XMITS NCPQ "
9820Sstevel@tonic-gate 		    "Workaround: value = %x\n", n);
9830Sstevel@tonic-gate 		pcix_set_cmd_reg(child, n);
9840Sstevel@tonic-gate 	}
9853724Srameshc 	pci_config_teardown(&config_handle);
9863274Set142600 	return (DDI_SUCCESS);
9873274Set142600 }
9883274Set142600 
9893274Set142600 static void
ppb_uninitchild(dev_info_t * child)9903274Set142600 ppb_uninitchild(dev_info_t *child)
9913274Set142600 {
9923274Set142600 	ppb_devstate_t *ppb;
9933274Set142600 
9943274Set142600 	ppb = (ppb_devstate_t *)ddi_get_soft_state(ppb_state,
9953274Set142600 	    ddi_get_instance(ddi_get_parent(child)));
9963156Sgirish 
9973156Sgirish 	/*
9983274Set142600 	 * SG OPL FMA specific
9993156Sgirish 	 */
1000*11596SJason.Beloro@Sun.COM 	if (ppb->parent_bus == PCIE_PCIECAP_DEV_TYPE_PCIE_DEV) {
1001*11596SJason.Beloro@Sun.COM 		pcie_fini_dom(child);
100211245SZhijun.Fu@Sun.COM 		pcie_fini_cfghdl(child);
1003*11596SJason.Beloro@Sun.COM 	}
10043156Sgirish 
10053274Set142600 	ppb_removechild(child);
10060Sstevel@tonic-gate }
10070Sstevel@tonic-gate 
10080Sstevel@tonic-gate static void
ppb_removechild(dev_info_t * dip)10090Sstevel@tonic-gate ppb_removechild(dev_info_t *dip)
10100Sstevel@tonic-gate {
10110Sstevel@tonic-gate 	ppb_devstate_t *ppb;
10120Sstevel@tonic-gate 
10130Sstevel@tonic-gate 	ppb = (ppb_devstate_t *)ddi_get_soft_state(ppb_state,
10140Sstevel@tonic-gate 	    ddi_get_instance(ddi_get_parent(dip)));
10150Sstevel@tonic-gate 
10160Sstevel@tonic-gate 	if (PM_CAPABLE(ppb->ppb_pwr_p)) {
10170Sstevel@tonic-gate 
10180Sstevel@tonic-gate 		DEBUG2(DBG_PWR, ddi_get_parent(dip),
10190Sstevel@tonic-gate 		    "UNINITCHILD: removing pwr_info for %s@%s\n",
10200Sstevel@tonic-gate 		    ddi_node_name(dip), ddi_get_name_addr(dip));
10210Sstevel@tonic-gate 		pci_pwr_rm_info(ppb->ppb_pwr_p, dip);
10220Sstevel@tonic-gate 	}
10230Sstevel@tonic-gate 
10240Sstevel@tonic-gate 	ddi_set_name_addr(dip, NULL);
10250Sstevel@tonic-gate 
10260Sstevel@tonic-gate 	/*
10270Sstevel@tonic-gate 	 * Strip the node to properly convert it back to prototype form
10280Sstevel@tonic-gate 	 */
10290Sstevel@tonic-gate 	ddi_remove_minor_node(dip, NULL);
10300Sstevel@tonic-gate 
10310Sstevel@tonic-gate 	impl_rem_dev_props(dip);
10320Sstevel@tonic-gate }
10330Sstevel@tonic-gate 
10340Sstevel@tonic-gate /*
10350Sstevel@tonic-gate  * If bridge is PM capable, set up PM state for nexus.
10360Sstevel@tonic-gate  */
10370Sstevel@tonic-gate static void
ppb_pwr_setup(ppb_devstate_t * ppb,dev_info_t * pdip)10380Sstevel@tonic-gate ppb_pwr_setup(ppb_devstate_t *ppb, dev_info_t *pdip)
10390Sstevel@tonic-gate {
10400Sstevel@tonic-gate 	char *comp_array[5];
10410Sstevel@tonic-gate 	int i;
10420Sstevel@tonic-gate 	ddi_acc_handle_t conf_hdl;
10430Sstevel@tonic-gate 	uint8_t pmcsr_bse;
10440Sstevel@tonic-gate 	uint16_t pmcap;
10450Sstevel@tonic-gate 
10460Sstevel@tonic-gate 	/*
10470Sstevel@tonic-gate 	 * Determine if bridge is PM capable.  If not, leave ppb_pwr_p NULL
10480Sstevel@tonic-gate 	 * and return.
10490Sstevel@tonic-gate 	 */
10500Sstevel@tonic-gate 	if (pci_config_setup(pdip, &ppb->ppb_conf_hdl) != DDI_SUCCESS) {
10510Sstevel@tonic-gate 
10520Sstevel@tonic-gate 		return;
10530Sstevel@tonic-gate 	}
10540Sstevel@tonic-gate 
10550Sstevel@tonic-gate 	conf_hdl = ppb->ppb_conf_hdl;
10560Sstevel@tonic-gate 
10570Sstevel@tonic-gate 	/*
10581624Spjha 	 * Locate and store the power management cap_ptr for future references.
10590Sstevel@tonic-gate 	 */
10601624Spjha 	if ((PCI_CAP_LOCATE(conf_hdl, PCI_CAP_ID_PM, &ppb->ppb_pm_cap_ptr))
10617656SSherry.Moore@Sun.COM 	    == DDI_FAILURE) {
10620Sstevel@tonic-gate 		DEBUG0(DBG_PWR, pdip, "bridge does not support PM. PCI"
10630Sstevel@tonic-gate 		    " PM data structure not found in config header\n");
10640Sstevel@tonic-gate 		pci_config_teardown(&conf_hdl);
10650Sstevel@tonic-gate 
10660Sstevel@tonic-gate 		return;
10670Sstevel@tonic-gate 	}
10680Sstevel@tonic-gate 
10690Sstevel@tonic-gate 	/*
10700Sstevel@tonic-gate 	 * Allocate PM state structure for ppb.
10710Sstevel@tonic-gate 	 */
10720Sstevel@tonic-gate 	ppb->ppb_pwr_p = (pci_pwr_t *)
10730Sstevel@tonic-gate 	    kmem_zalloc(sizeof (pci_pwr_t), KM_SLEEP);
10740Sstevel@tonic-gate 	ppb->ppb_pwr_p->pwr_fp = 0;
10750Sstevel@tonic-gate 
10761624Spjha 	pmcsr_bse = PCI_CAP_GET8(conf_hdl, NULL, ppb->ppb_pm_cap_ptr,
10777656SSherry.Moore@Sun.COM 	    PCI_PMCSR_BSE);
10780Sstevel@tonic-gate 
10791624Spjha 	pmcap = PCI_CAP_GET16(conf_hdl, NULL, ppb->ppb_pm_cap_ptr,
10807656SSherry.Moore@Sun.COM 	    PCI_PMCAP);
10810Sstevel@tonic-gate 
10821774Spjha 	if (pmcap == PCI_CAP_EINVAL16 || pmcsr_bse == PCI_CAP_EINVAL8) {
10831624Spjha 		pci_config_teardown(&conf_hdl);
10841624Spjha 		return;
10851624Spjha 	}
10860Sstevel@tonic-gate 
10870Sstevel@tonic-gate 	if (pmcap & PCI_PMCAP_D1) {
10880Sstevel@tonic-gate 		DEBUG0(DBG_PWR, pdip, "setup: B1 state supported\n");
10890Sstevel@tonic-gate 		ppb->ppb_pwr_p->pwr_flags |= PCI_PWR_B1_CAPABLE;
10900Sstevel@tonic-gate 	} else {
10910Sstevel@tonic-gate 		DEBUG0(DBG_PWR, pdip, "setup: B1 state NOT supported\n");
10920Sstevel@tonic-gate 	}
10930Sstevel@tonic-gate 	if (pmcap & PCI_PMCAP_D2) {
10940Sstevel@tonic-gate 		DEBUG0(DBG_PWR, pdip, "setup: B2 state supported\n");
10950Sstevel@tonic-gate 		ppb->ppb_pwr_p->pwr_flags |= PCI_PWR_B2_CAPABLE;
10960Sstevel@tonic-gate 	} else {
10970Sstevel@tonic-gate 		DEBUG0(DBG_PWR, pdip, "setup: B2 via D2 NOT supported\n");
10980Sstevel@tonic-gate 	}
10990Sstevel@tonic-gate 
11000Sstevel@tonic-gate 	if (pmcsr_bse & PCI_PMCSR_BSE_BPCC_EN) {
11010Sstevel@tonic-gate 		DEBUG0(DBG_PWR, pdip,
11021624Spjha 		"setup: bridge power/clock control enable\n");
11030Sstevel@tonic-gate 	} else {
11040Sstevel@tonic-gate 		DEBUG0(DBG_PWR, pdip,
11051624Spjha 		"setup: bridge power/clock control disabled\n");
11060Sstevel@tonic-gate 
11070Sstevel@tonic-gate 		kmem_free(ppb->ppb_pwr_p, sizeof (pci_pwr_t));
11080Sstevel@tonic-gate 		ppb->ppb_pwr_p = NULL;
11090Sstevel@tonic-gate 		pci_config_teardown(&conf_hdl);
11100Sstevel@tonic-gate 
11110Sstevel@tonic-gate 		return;
11120Sstevel@tonic-gate 	}
11130Sstevel@tonic-gate 
11140Sstevel@tonic-gate 	/*
11150Sstevel@tonic-gate 	 * PCI states D0 and D3 always are supported for normal PCI
11160Sstevel@tonic-gate 	 * devices.  D1 and D2 are optional which are checked for above.
11170Sstevel@tonic-gate 	 * Bridge function states D0-D3 correspond to secondary bus states
11180Sstevel@tonic-gate 	 * B0-B3, EXCEPT if PCI_PMCSR_BSE_B2_B3 is set.  In this case, setting
11190Sstevel@tonic-gate 	 * the bridge function to D3 will set the bridge bus to state B2 instead
11200Sstevel@tonic-gate 	 * of B3.  D2 will not correspond to B2 (and in fact, probably
11210Sstevel@tonic-gate 	 * won't be D2 capable).  Implicitly, this means that if
11220Sstevel@tonic-gate 	 * PCI_PMCSR_BSE_B2_B3 is set, the bus will not be B3 capable.
11230Sstevel@tonic-gate 	 */
11240Sstevel@tonic-gate 	if (pmcsr_bse & PCI_PMCSR_BSE_B2_B3) {
11250Sstevel@tonic-gate 		ppb->ppb_pwr_p->pwr_flags |= PCI_PWR_B2_CAPABLE;
11260Sstevel@tonic-gate 		DEBUG0(DBG_PWR, pdip, "B2 supported via D3\n");
11270Sstevel@tonic-gate 	} else {
11280Sstevel@tonic-gate 		ppb->ppb_pwr_p->pwr_flags |= PCI_PWR_B3_CAPABLE;
11290Sstevel@tonic-gate 		DEBUG0(DBG_PWR, pdip, "B3 supported via D3\n");
11300Sstevel@tonic-gate 	}
11310Sstevel@tonic-gate 
11320Sstevel@tonic-gate 	ppb->ppb_pwr_p->pwr_dip = pdip;
11330Sstevel@tonic-gate 	mutex_init(&ppb->ppb_pwr_p->pwr_mutex, NULL, MUTEX_DRIVER, NULL);
11340Sstevel@tonic-gate 
11350Sstevel@tonic-gate 	i = 0;
11360Sstevel@tonic-gate 	comp_array[i++] = "NAME=PCI bridge PM";
11370Sstevel@tonic-gate 	if (ppb->ppb_pwr_p->pwr_flags & PCI_PWR_B3_CAPABLE) {
11380Sstevel@tonic-gate 		comp_array[i++] = "0=Clock/Power Off (B3)";
11390Sstevel@tonic-gate 	}
11400Sstevel@tonic-gate 	if (ppb->ppb_pwr_p->pwr_flags & PCI_PWR_B2_CAPABLE) {
11410Sstevel@tonic-gate 		comp_array[i++] = "1=Clock Off (B2)";
11420Sstevel@tonic-gate 	}
11430Sstevel@tonic-gate 	if (ppb->ppb_pwr_p->pwr_flags & PCI_PWR_B1_CAPABLE) {
11440Sstevel@tonic-gate 		comp_array[i++] = "2=Bus Inactive (B1)";
11450Sstevel@tonic-gate 	}
11460Sstevel@tonic-gate 	comp_array[i++] = "3=Full Power (B0)";
11470Sstevel@tonic-gate 
11480Sstevel@tonic-gate 	/*
11490Sstevel@tonic-gate 	 * Create pm-components property. It does not already exist.
11500Sstevel@tonic-gate 	 */
11510Sstevel@tonic-gate 	if (ddi_prop_update_string_array(DDI_DEV_T_NONE, pdip,
11520Sstevel@tonic-gate 	    "pm-components", comp_array, i) != DDI_PROP_SUCCESS) {
11530Sstevel@tonic-gate 		cmn_err(CE_WARN,
11540Sstevel@tonic-gate 		    "%s%d pm-components prop update failed",
11550Sstevel@tonic-gate 		    ddi_driver_name(pdip), ddi_get_instance(pdip));
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 	if (ddi_prop_create(DDI_DEV_T_NONE, pdip, DDI_PROP_CANSLEEP,
11650Sstevel@tonic-gate 	    "pm-want-child-notification?", NULL, NULL) != DDI_PROP_SUCCESS) {
11660Sstevel@tonic-gate 		cmn_err(CE_WARN,
11677656SSherry.Moore@Sun.COM 		    "%s%d fail to create pm-want-child-notification? prop",
11687656SSherry.Moore@Sun.COM 		    ddi_driver_name(pdip), ddi_get_instance(pdip));
11690Sstevel@tonic-gate 
11700Sstevel@tonic-gate 		(void) ddi_prop_remove(DDI_DEV_T_NONE, pdip, "pm-components");
11710Sstevel@tonic-gate 		pci_config_teardown(&conf_hdl);
11720Sstevel@tonic-gate 		mutex_destroy(&ppb->ppb_pwr_p->pwr_mutex);
11730Sstevel@tonic-gate 		kmem_free(ppb->ppb_pwr_p, sizeof (pci_pwr_t));
11740Sstevel@tonic-gate 		ppb->ppb_pwr_p = NULL;
11750Sstevel@tonic-gate 
11760Sstevel@tonic-gate 		return;
11770Sstevel@tonic-gate 	}
11780Sstevel@tonic-gate 
11790Sstevel@tonic-gate 	ppb->ppb_pwr_p->current_lvl =
11807656SSherry.Moore@Sun.COM 	    pci_pwr_current_lvl(ppb->ppb_pwr_p);
11810Sstevel@tonic-gate }
11820Sstevel@tonic-gate 
11830Sstevel@tonic-gate /*
11840Sstevel@tonic-gate  * Remove PM state for nexus.
11850Sstevel@tonic-gate  */
11860Sstevel@tonic-gate static void
ppb_pwr_teardown(ppb_devstate_t * ppb,dev_info_t * dip)11870Sstevel@tonic-gate ppb_pwr_teardown(ppb_devstate_t *ppb, dev_info_t *dip)
11880Sstevel@tonic-gate {
11890Sstevel@tonic-gate 	int low_lvl;
11900Sstevel@tonic-gate 
11910Sstevel@tonic-gate 	/*
11920Sstevel@tonic-gate 	 * Determine the lowest power level supported.
11930Sstevel@tonic-gate 	 */
11940Sstevel@tonic-gate 	if (ppb->ppb_pwr_p->pwr_flags & PCI_PWR_B3_CAPABLE) {
11950Sstevel@tonic-gate 		low_lvl = PM_LEVEL_B3;
11960Sstevel@tonic-gate 	} else {
11970Sstevel@tonic-gate 		low_lvl = PM_LEVEL_B2;
11980Sstevel@tonic-gate 	}
11990Sstevel@tonic-gate 
12000Sstevel@tonic-gate 	if (pm_lower_power(dip, PCI_PM_COMP_0, low_lvl) != DDI_SUCCESS) {
12010Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d failed to lower power",
12020Sstevel@tonic-gate 		    ddi_driver_name(dip), ddi_get_instance(dip));
12030Sstevel@tonic-gate 	}
12040Sstevel@tonic-gate 
12050Sstevel@tonic-gate 	pci_config_teardown(&ppb->ppb_conf_hdl);
12060Sstevel@tonic-gate 	mutex_destroy(&ppb->ppb_pwr_p->pwr_mutex);
12070Sstevel@tonic-gate 	kmem_free(ppb->ppb_pwr_p, sizeof (pci_pwr_t));
12080Sstevel@tonic-gate 
12090Sstevel@tonic-gate 	if (ddi_prop_remove(DDI_DEV_T_NONE, dip, "pm-components") !=
12107656SSherry.Moore@Sun.COM 	    DDI_PROP_SUCCESS) {
12110Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d unable to remove prop pm-components",
12120Sstevel@tonic-gate 		    ddi_driver_name(dip), ddi_get_instance(dip));
12130Sstevel@tonic-gate 	}
12140Sstevel@tonic-gate 
12150Sstevel@tonic-gate 	if (ddi_prop_remove(DDI_DEV_T_NONE, dip,
12160Sstevel@tonic-gate 	    "pm-want-child-notification?") != DDI_PROP_SUCCESS) {
12170Sstevel@tonic-gate 		cmn_err(CE_WARN,
12180Sstevel@tonic-gate 		    "%s%d unable to remove prop pm-want_child_notification?",
12190Sstevel@tonic-gate 		    ddi_driver_name(dip), ddi_get_instance(dip));
12200Sstevel@tonic-gate 	}
12210Sstevel@tonic-gate }
12220Sstevel@tonic-gate 
12230Sstevel@tonic-gate /*
12240Sstevel@tonic-gate  * Examine the pmcsr register and return the software defined
12250Sstevel@tonic-gate  * state (the difference being whether D3 means B2 or B3).
12260Sstevel@tonic-gate  */
12270Sstevel@tonic-gate int
pci_pwr_current_lvl(pci_pwr_t * pwr_p)12280Sstevel@tonic-gate pci_pwr_current_lvl(pci_pwr_t *pwr_p)
12290Sstevel@tonic-gate {
12300Sstevel@tonic-gate 	ppb_devstate_t *ppb;
12310Sstevel@tonic-gate 	uint16_t pmcsr;
12320Sstevel@tonic-gate 
12330Sstevel@tonic-gate 	/*
12340Sstevel@tonic-gate 	 * Find out current power level
12350Sstevel@tonic-gate 	 */
12360Sstevel@tonic-gate 	ppb = (ppb_devstate_t *)ddi_get_soft_state(ppb_state,
12370Sstevel@tonic-gate 	    ddi_get_instance(pwr_p->pwr_dip));
12380Sstevel@tonic-gate 
12391624Spjha 	if ((pmcsr = PCI_CAP_GET16(ppb->ppb_conf_hdl, NULL,
12407656SSherry.Moore@Sun.COM 	    ppb->ppb_pm_cap_ptr, PCI_PMCSR)) == PCI_CAP_EINVAL16)
12411624Spjha 		return (DDI_FAILURE);
12420Sstevel@tonic-gate 
12430Sstevel@tonic-gate 	switch (pmcsr & PCI_PMCSR_STATE_MASK) {
12440Sstevel@tonic-gate 	case PCI_PMCSR_D0:
12450Sstevel@tonic-gate 
12460Sstevel@tonic-gate 		return (PM_LEVEL_B0);
12470Sstevel@tonic-gate 	case PCI_PMCSR_D1:
12480Sstevel@tonic-gate 
12490Sstevel@tonic-gate 		return (PM_LEVEL_B1);
12500Sstevel@tonic-gate 	case PCI_PMCSR_D2:
12510Sstevel@tonic-gate 
12520Sstevel@tonic-gate 		return (PM_LEVEL_B2);
12530Sstevel@tonic-gate 	case PCI_PMCSR_D3HOT:
12540Sstevel@tonic-gate 		if ((ppb->ppb_pwr_p->pwr_flags & PCI_PWR_B3_CAPABLE) == 0) {
12550Sstevel@tonic-gate 
12560Sstevel@tonic-gate 			return (PM_LEVEL_B2);
12570Sstevel@tonic-gate 		} else {
12580Sstevel@tonic-gate 
12590Sstevel@tonic-gate 			return (PM_LEVEL_B3);
12600Sstevel@tonic-gate 		}
12610Sstevel@tonic-gate 	}
1262946Smathue 	/*NOTREACHED*/
1263946Smathue 	return (PM_LEVEL_B3);
12640Sstevel@tonic-gate }
12650Sstevel@tonic-gate 
12660Sstevel@tonic-gate /*
12670Sstevel@tonic-gate  * Power entry point.  Called by the PM framework to change the
12680Sstevel@tonic-gate  * current power state of the bus.  This function must first verify that
12690Sstevel@tonic-gate  * the requested power change is still valid.
12700Sstevel@tonic-gate  */
12710Sstevel@tonic-gate /*ARGSUSED*/
12720Sstevel@tonic-gate static int
ppb_pwr(dev_info_t * dip,int component,int lvl)12730Sstevel@tonic-gate ppb_pwr(dev_info_t *dip, int component, int lvl)
12740Sstevel@tonic-gate {
12750Sstevel@tonic-gate 	ppb_devstate_t *ppb;
12760Sstevel@tonic-gate 	uint16_t pmcsr;
12770Sstevel@tonic-gate 	char *str;
12780Sstevel@tonic-gate 	int lowest_lvl;
12790Sstevel@tonic-gate 	int old_lvl;
12800Sstevel@tonic-gate 	int new_lvl;
12810Sstevel@tonic-gate 
12820Sstevel@tonic-gate 	ppb = (ppb_devstate_t *)ddi_get_soft_state(ppb_state,
12830Sstevel@tonic-gate 	    ddi_get_instance(dip));
12840Sstevel@tonic-gate 	if (ppb == NULL) {
12850Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d ppb_pwr: can't get soft state",
12860Sstevel@tonic-gate 		    ddi_driver_name(dip), ddi_get_instance(dip));
12870Sstevel@tonic-gate 
12880Sstevel@tonic-gate 		return (DDI_FAILURE);
12890Sstevel@tonic-gate 	}
12900Sstevel@tonic-gate 
12910Sstevel@tonic-gate 	DEBUG1(DBG_PWR, dip, "ppb_pwr(): ENTER level = %d\n", lvl);
12920Sstevel@tonic-gate 
12930Sstevel@tonic-gate 	mutex_enter(&ppb->ppb_pwr_p->pwr_mutex);
12940Sstevel@tonic-gate 
12950Sstevel@tonic-gate 	/*
12960Sstevel@tonic-gate 	 * Find out if the power setting is possible.  If it is not,
12970Sstevel@tonic-gate 	 * set component busy and return failure.  If it is possible,
12980Sstevel@tonic-gate 	 * and it is the lowest pwr setting possible, set component
12990Sstevel@tonic-gate 	 * busy so that the framework does not try to lower any further.
13000Sstevel@tonic-gate 	 */
13010Sstevel@tonic-gate 	lowest_lvl = pci_pwr_new_lvl(ppb->ppb_pwr_p);
13020Sstevel@tonic-gate 	if (lowest_lvl > lvl) {
13030Sstevel@tonic-gate 		pci_pwr_component_busy(ppb->ppb_pwr_p);
13040Sstevel@tonic-gate 		DEBUG2(DBG_PWR, dip, "ppb_pwr: failing power request "
13057656SSherry.Moore@Sun.COM 		    "lowest allowed is %d requested is %d\n",
13067656SSherry.Moore@Sun.COM 		    lowest_lvl, lvl);
13070Sstevel@tonic-gate 		mutex_exit(&ppb->ppb_pwr_p->pwr_mutex);
13080Sstevel@tonic-gate 
13090Sstevel@tonic-gate 		return (DDI_FAILURE);
13100Sstevel@tonic-gate 	} else if (lowest_lvl == lvl) {
13110Sstevel@tonic-gate 		pci_pwr_component_busy(ppb->ppb_pwr_p);
13120Sstevel@tonic-gate 	} else {
13130Sstevel@tonic-gate 		pci_pwr_component_idle(ppb->ppb_pwr_p);
13140Sstevel@tonic-gate 	}
13150Sstevel@tonic-gate 
13161624Spjha 	if ((pmcsr = PCI_CAP_GET16(ppb->ppb_conf_hdl, NULL,
13177656SSherry.Moore@Sun.COM 	    ppb->ppb_pm_cap_ptr, PCI_PMCSR)) == PCI_CAP_EINVAL16)
13181624Spjha 		return (DDI_FAILURE);
13190Sstevel@tonic-gate 
13200Sstevel@tonic-gate 	/*
13210Sstevel@tonic-gate 	 * Save the current power level.  This is the actual function level,
13220Sstevel@tonic-gate 	 * not the translated bridge level stored in pwr_p->current_lvl
13230Sstevel@tonic-gate 	 */
13240Sstevel@tonic-gate 	old_lvl = pmcsr & PCI_PMCSR_STATE_MASK;
13250Sstevel@tonic-gate 
13260Sstevel@tonic-gate 	pmcsr &= ~PCI_PMCSR_STATE_MASK;
13270Sstevel@tonic-gate 	switch (lvl) {
13280Sstevel@tonic-gate 	case PM_LEVEL_B0:
13290Sstevel@tonic-gate 		str = "PM_LEVEL_B0 (full speed)";
13300Sstevel@tonic-gate 		pmcsr |= PCI_PMCSR_D0;
13310Sstevel@tonic-gate 		break;
13320Sstevel@tonic-gate 	case PM_LEVEL_B1:
13330Sstevel@tonic-gate 		str = "PM_LEVEL_B1 (light sleep. No bus traffic allowed)";
13340Sstevel@tonic-gate 		if ((ppb->ppb_pwr_p->pwr_flags & PCI_PWR_B1_CAPABLE) == 0) {
13350Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s%d PCI PM state B1 not supported",
13360Sstevel@tonic-gate 			    ddi_driver_name(dip), ddi_get_instance(dip));
13370Sstevel@tonic-gate 
13380Sstevel@tonic-gate 			mutex_exit(&ppb->ppb_pwr_p->pwr_mutex);
13390Sstevel@tonic-gate 			return (DDI_FAILURE);
13400Sstevel@tonic-gate 		}
13410Sstevel@tonic-gate 		pmcsr |= PCI_PMCSR_D1;
13420Sstevel@tonic-gate 		break;
13430Sstevel@tonic-gate 	case PM_LEVEL_B2:
13440Sstevel@tonic-gate 		str = "PM_LEVEL_B2 (clock off)";
13450Sstevel@tonic-gate 		if ((ppb->ppb_pwr_p->pwr_flags & PCI_PWR_B2_CAPABLE) == 0) {
13460Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s%d PM state B2 not supported...",
13470Sstevel@tonic-gate 			    ddi_driver_name(dip),
13480Sstevel@tonic-gate 			    ddi_get_instance(dip));
13490Sstevel@tonic-gate 			mutex_exit(&ppb->ppb_pwr_p->pwr_mutex);
13500Sstevel@tonic-gate 
13510Sstevel@tonic-gate 			return (DDI_FAILURE);
13520Sstevel@tonic-gate 		}
13530Sstevel@tonic-gate 
13540Sstevel@tonic-gate 		if ((ppb->ppb_pwr_p->pwr_flags & PCI_PWR_B3_CAPABLE) == 0) {
13550Sstevel@tonic-gate 			/*
13560Sstevel@tonic-gate 			 * If B3 isn't supported, use D3 for B2 to avoid the
13570Sstevel@tonic-gate 			 * possible case that D2 for B2 isn't supported.
13580Sstevel@tonic-gate 			 * Saves and extra check and state flag..
13590Sstevel@tonic-gate 			 */
13600Sstevel@tonic-gate 			pmcsr |= PCI_PMCSR_D3HOT;
13610Sstevel@tonic-gate 		} else {
13620Sstevel@tonic-gate 			pmcsr |= PCI_PMCSR_D2;
13630Sstevel@tonic-gate 		}
13640Sstevel@tonic-gate 		break;
13650Sstevel@tonic-gate 	case PM_LEVEL_B3:
13660Sstevel@tonic-gate 		str = "PM_LEVEL_B30 (clock and power off)";
13670Sstevel@tonic-gate 		if ((ppb->ppb_pwr_p->pwr_flags & PCI_PWR_B3_CAPABLE) == 0) {
13680Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s%d PM state B3 not supported...",
13690Sstevel@tonic-gate 			    ddi_driver_name(dip),
13700Sstevel@tonic-gate 			    ddi_get_instance(dip));
13710Sstevel@tonic-gate 			mutex_exit(&ppb->ppb_pwr_p->pwr_mutex);
13720Sstevel@tonic-gate 
13730Sstevel@tonic-gate 			return (DDI_FAILURE);
13740Sstevel@tonic-gate 		}
13750Sstevel@tonic-gate 		pmcsr |= PCI_PMCSR_D3HOT;
13760Sstevel@tonic-gate 
13770Sstevel@tonic-gate 		break;
13780Sstevel@tonic-gate 
13790Sstevel@tonic-gate 	default:
13800Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d Unknown PM state %d",
13810Sstevel@tonic-gate 		    ddi_driver_name(dip), ddi_get_instance(dip), lvl);
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 	new_lvl = pmcsr & PCI_PMCSR_STATE_MASK;
13880Sstevel@tonic-gate 
13890Sstevel@tonic-gate 	/*
13900Sstevel@tonic-gate 	 * Save config regs if going into HW state D3 (B2 or B3)
13910Sstevel@tonic-gate 	 */
13920Sstevel@tonic-gate 	if ((old_lvl != PCI_PMCSR_D3HOT) && (new_lvl == PCI_PMCSR_D3HOT)) {
13930Sstevel@tonic-gate 		DEBUG0(DBG_PWR, dip, "ppb_pwr(): SAVING CONFIG REGS\n");
13940Sstevel@tonic-gate 		if (pci_save_config_regs(dip) != DDI_SUCCESS) {
13950Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s%d Save config regs failed",
13967656SSherry.Moore@Sun.COM 			    ddi_driver_name(dip), ddi_get_instance(dip));
13970Sstevel@tonic-gate 			mutex_exit(&ppb->ppb_pwr_p->pwr_mutex);
13980Sstevel@tonic-gate 
13990Sstevel@tonic-gate 			return (DDI_FAILURE);
14000Sstevel@tonic-gate 		}
14010Sstevel@tonic-gate 	}
14020Sstevel@tonic-gate 
14031624Spjha 	PCI_CAP_PUT16(ppb->ppb_conf_hdl, NULL, ppb->ppb_pm_cap_ptr, PCI_PMCSR,
14047656SSherry.Moore@Sun.COM 	    pmcsr);
14050Sstevel@tonic-gate 
14060Sstevel@tonic-gate 	/*
14070Sstevel@tonic-gate 	 * No bus transactions should occur without waiting for
14080Sstevel@tonic-gate 	 * settle time specified in PCI PM spec rev 2.1 sec 5.6.1
14090Sstevel@tonic-gate 	 * To make things simple, just use the max time specified for
14100Sstevel@tonic-gate 	 * all state transitions.
14110Sstevel@tonic-gate 	 */
14120Sstevel@tonic-gate 	delay(drv_usectohz(PCI_CLK_SETTLE_TIME));
14130Sstevel@tonic-gate 
14140Sstevel@tonic-gate 	/*
14150Sstevel@tonic-gate 	 * Restore configuration registers if coming out of HW state D3
14160Sstevel@tonic-gate 	 */
14170Sstevel@tonic-gate 	if ((old_lvl == PCI_PMCSR_D3HOT) && (new_lvl != PCI_PMCSR_D3HOT)) {
14180Sstevel@tonic-gate 		DEBUG0(DBG_PWR, dip, "ppb_pwr(): RESTORING CONFIG REGS\n");
14190Sstevel@tonic-gate 		if (pci_restore_config_regs(dip) != DDI_SUCCESS) {
14200Sstevel@tonic-gate 			panic("%s%d restore config regs failed",
14210Sstevel@tonic-gate 			    ddi_driver_name(dip), ddi_get_instance(dip));
14220Sstevel@tonic-gate 		}
14230Sstevel@tonic-gate 		/*NOTREACHED*/
14240Sstevel@tonic-gate 	}
14250Sstevel@tonic-gate 
14260Sstevel@tonic-gate 	ppb->ppb_pwr_p->current_lvl = lvl;
14270Sstevel@tonic-gate 
14280Sstevel@tonic-gate 	mutex_exit(&ppb->ppb_pwr_p->pwr_mutex);
14290Sstevel@tonic-gate 
14300Sstevel@tonic-gate 	DEBUG1(DBG_PWR, dip, "ppb_set_pwr: set PM state to %s\n\n", str);
14310Sstevel@tonic-gate 
14320Sstevel@tonic-gate 	return (DDI_SUCCESS);
14330Sstevel@tonic-gate }
14340Sstevel@tonic-gate 
14350Sstevel@tonic-gate /*
14360Sstevel@tonic-gate  * Initialize hotplug framework if we are hotpluggable.
14370Sstevel@tonic-gate  * Sets flag in the soft state if Hot Plug is supported and initialized
14380Sstevel@tonic-gate  * properly.
14390Sstevel@tonic-gate  */
14400Sstevel@tonic-gate /*ARGSUSED*/
14410Sstevel@tonic-gate static void
ppb_init_hotplug(ppb_devstate_t * ppb)14420Sstevel@tonic-gate ppb_init_hotplug(ppb_devstate_t *ppb)
14430Sstevel@tonic-gate {
144410923SEvan.Yan@Sun.COM 	ppb->hotplug_capable = B_FALSE;
144510923SEvan.Yan@Sun.COM 
14460Sstevel@tonic-gate 	if (ddi_prop_exists(DDI_DEV_T_ANY, ppb->dip, DDI_PROP_DONTPASS,
14470Sstevel@tonic-gate 	    "hotplug-capable")) {
14480Sstevel@tonic-gate 		(void) modload("misc", "pcihp");
14490Sstevel@tonic-gate 
14500Sstevel@tonic-gate 		if (pcihp_init(ppb->dip) != DDI_SUCCESS) {
14510Sstevel@tonic-gate 			cmn_err(CE_WARN,
14520Sstevel@tonic-gate 			    "%s #%d: Failed setting hotplug framework",
14530Sstevel@tonic-gate 			    ddi_driver_name(ppb->dip),
14540Sstevel@tonic-gate 			    ddi_get_instance(ppb->dip));
14550Sstevel@tonic-gate 		} else
14560Sstevel@tonic-gate 			ppb->hotplug_capable = B_TRUE;
14570Sstevel@tonic-gate 	}
14580Sstevel@tonic-gate 
145910923SEvan.Yan@Sun.COM 	if (ppb->hotplug_capable == B_FALSE) {
146010923SEvan.Yan@Sun.COM 		/*
146110923SEvan.Yan@Sun.COM 		 * create minor node for devctl interfaces
146210923SEvan.Yan@Sun.COM 		 */
146310923SEvan.Yan@Sun.COM 		if (ddi_create_minor_node(ppb->dip, "devctl", S_IFCHR,
146410923SEvan.Yan@Sun.COM 		    PCI_MINOR_NUM(ddi_get_instance(ppb->dip), PCI_DEVCTL_MINOR),
146510923SEvan.Yan@Sun.COM 		    DDI_NT_NEXUS, 0) != DDI_SUCCESS)
146610923SEvan.Yan@Sun.COM 			cmn_err(CE_WARN,
146710923SEvan.Yan@Sun.COM 			    "%s #%d: Failed to create a minor node",
146810923SEvan.Yan@Sun.COM 			    ddi_driver_name(ppb->dip),
146910923SEvan.Yan@Sun.COM 			    ddi_get_instance(ppb->dip));
147010923SEvan.Yan@Sun.COM 	}
14710Sstevel@tonic-gate }
14720Sstevel@tonic-gate 
14730Sstevel@tonic-gate static void
ppb_create_ranges_prop(dev_info_t * dip,ddi_acc_handle_t config_handle)14740Sstevel@tonic-gate ppb_create_ranges_prop(dev_info_t *dip,
14750Sstevel@tonic-gate 	ddi_acc_handle_t config_handle)
14760Sstevel@tonic-gate {
14770Sstevel@tonic-gate 	uint32_t base, limit;
14780Sstevel@tonic-gate 	ppb_ranges_t	ranges[PPB_RANGE_LEN];
14790Sstevel@tonic-gate 	uint8_t io_base_lo, io_limit_lo;
14800Sstevel@tonic-gate 	uint16_t io_base_hi, io_limit_hi, mem_base, mem_limit;
14810Sstevel@tonic-gate 	int i = 0, rangelen = sizeof (ppb_ranges_t)/sizeof (int);
14820Sstevel@tonic-gate 
14830Sstevel@tonic-gate 	io_base_lo = pci_config_get8(config_handle, PCI_BCNF_IO_BASE_LOW);
14840Sstevel@tonic-gate 	io_limit_lo = pci_config_get8(config_handle, PCI_BCNF_IO_LIMIT_LOW);
14850Sstevel@tonic-gate 	io_base_hi = pci_config_get16(config_handle, PCI_BCNF_IO_BASE_HI);
14860Sstevel@tonic-gate 	io_limit_hi = pci_config_get16(config_handle, PCI_BCNF_IO_LIMIT_HI);
14870Sstevel@tonic-gate 	mem_base = pci_config_get16(config_handle, PCI_BCNF_MEM_BASE);
14880Sstevel@tonic-gate 	mem_limit = pci_config_get16(config_handle, PCI_BCNF_MEM_LIMIT);
14890Sstevel@tonic-gate 
14900Sstevel@tonic-gate 	/*
14910Sstevel@tonic-gate 	 * Create ranges for IO space
14920Sstevel@tonic-gate 	 */
14930Sstevel@tonic-gate 	ranges[i].size_low = ranges[i].size_high = 0;
14940Sstevel@tonic-gate 	ranges[i].parent_mid = ranges[i].child_mid =
14957656SSherry.Moore@Sun.COM 	    ranges[i].parent_high = 0;
14960Sstevel@tonic-gate 	ranges[i].child_high = ranges[i].parent_high |=
14977656SSherry.Moore@Sun.COM 	    (PCI_REG_REL_M | PCI_ADDR_IO);
14980Sstevel@tonic-gate 	base = PPB_16bit_IOADDR(io_base_lo);
14990Sstevel@tonic-gate 	limit = PPB_16bit_IOADDR(io_limit_lo);
15000Sstevel@tonic-gate 
150110548SPavel.Potoplyak@Sun.COM 	/*
150210548SPavel.Potoplyak@Sun.COM 	 * Check for 32-bit I/O support as per PCI-to-PCI Bridge Arch Spec
150310548SPavel.Potoplyak@Sun.COM 	 */
15040Sstevel@tonic-gate 	if ((io_base_lo & 0xf) == PPB_32BIT_IO) {
15050Sstevel@tonic-gate 		base = PPB_LADDR(base, io_base_hi);
15060Sstevel@tonic-gate 		limit = PPB_LADDR(limit, io_limit_hi);
15070Sstevel@tonic-gate 	}
15080Sstevel@tonic-gate 
150910548SPavel.Potoplyak@Sun.COM 	/*
151010548SPavel.Potoplyak@Sun.COM 	 * Check if the bridge implements an I/O address range as per
151110548SPavel.Potoplyak@Sun.COM 	 * PCI-to-PCI Bridge Arch Spec
151210548SPavel.Potoplyak@Sun.COM 	 */
151310548SPavel.Potoplyak@Sun.COM 	if ((io_base_lo != 0 || io_limit_lo != 0) && limit >= base) {
151410548SPavel.Potoplyak@Sun.COM 		ranges[i].parent_low = ranges[i].child_low =
151510548SPavel.Potoplyak@Sun.COM 		    base;
151610548SPavel.Potoplyak@Sun.COM 		ranges[i].size_low = limit - base + PPB_IOGRAIN;
151710548SPavel.Potoplyak@Sun.COM 		i++;
15180Sstevel@tonic-gate 	}
15190Sstevel@tonic-gate 
15200Sstevel@tonic-gate 	/*
15210Sstevel@tonic-gate 	 * Create ranges for 32bit memory space
15220Sstevel@tonic-gate 	 */
15230Sstevel@tonic-gate 	base = PPB_32bit_MEMADDR(mem_base);
15240Sstevel@tonic-gate 	limit = PPB_32bit_MEMADDR(mem_limit);
15250Sstevel@tonic-gate 	ranges[i].size_low = ranges[i].size_high = 0;
15260Sstevel@tonic-gate 	ranges[i].parent_mid = ranges[i].child_mid =
15277656SSherry.Moore@Sun.COM 	    ranges[i].parent_high = 0;
15280Sstevel@tonic-gate 	ranges[i].child_high = ranges[i].parent_high |=
15297656SSherry.Moore@Sun.COM 	    (PCI_REG_REL_M | PCI_ADDR_MEM32);
15300Sstevel@tonic-gate 	ranges[i].child_low = ranges[i].parent_low = base;
15310Sstevel@tonic-gate 	if (limit >= base) {
15320Sstevel@tonic-gate 		ranges[i].size_low = limit - base + PPB_MEMGRAIN;
15330Sstevel@tonic-gate 		i++;
15340Sstevel@tonic-gate 	}
15350Sstevel@tonic-gate 
15360Sstevel@tonic-gate 	if (i) {
15370Sstevel@tonic-gate 		(void) ndi_prop_update_int_array(DDI_DEV_T_NONE, dip, "ranges",
15380Sstevel@tonic-gate 		    (int *)ranges, i * rangelen);
15390Sstevel@tonic-gate 	}
15400Sstevel@tonic-gate }
15410Sstevel@tonic-gate 
15420Sstevel@tonic-gate /* ARGSUSED */
15430Sstevel@tonic-gate static int
ppb_open(dev_t * devp,int flags,int otyp,cred_t * credp)15440Sstevel@tonic-gate ppb_open(dev_t *devp, int flags, int otyp, cred_t *credp)
15450Sstevel@tonic-gate {
154610923SEvan.Yan@Sun.COM 	int		instance = PCI_MINOR_NUM_TO_INSTANCE(getminor(*devp));
154710923SEvan.Yan@Sun.COM 	ppb_devstate_t	*ppb_p = ddi_get_soft_state(ppb_state, instance);
15480Sstevel@tonic-gate 
15490Sstevel@tonic-gate 	/*
15500Sstevel@tonic-gate 	 * Make sure the open is for the right file type.
15510Sstevel@tonic-gate 	 */
15520Sstevel@tonic-gate 	if (otyp != OTYP_CHR)
15530Sstevel@tonic-gate 		return (EINVAL);
15540Sstevel@tonic-gate 
15550Sstevel@tonic-gate 	if (ppb_p == NULL)
15560Sstevel@tonic-gate 		return (ENXIO);
15570Sstevel@tonic-gate 
155810923SEvan.Yan@Sun.COM 	mutex_enter(&ppb_p->ppb_mutex);
155910923SEvan.Yan@Sun.COM 
156010923SEvan.Yan@Sun.COM 	/*
156110923SEvan.Yan@Sun.COM 	 * Ioctls will be handled by SPARC PCI Express framework for all
156210923SEvan.Yan@Sun.COM 	 * PCIe platforms
156310923SEvan.Yan@Sun.COM 	 */
156410923SEvan.Yan@Sun.COM 	if (ppb_p->parent_bus == PCIE_PCIECAP_DEV_TYPE_PCIE_DEV) {
156510923SEvan.Yan@Sun.COM 		int	rv;
156610923SEvan.Yan@Sun.COM 
156710923SEvan.Yan@Sun.COM 		rv = pcie_open(ppb_p->dip, devp, flags, otyp, credp);
156810923SEvan.Yan@Sun.COM 		mutex_exit(&ppb_p->ppb_mutex);
156910923SEvan.Yan@Sun.COM 
157010923SEvan.Yan@Sun.COM 		return (rv);
157110923SEvan.Yan@Sun.COM 	} else if (ppb_p->hotplug_capable == B_TRUE) {
157210923SEvan.Yan@Sun.COM 		mutex_exit(&ppb_p->ppb_mutex);
157310923SEvan.Yan@Sun.COM 
157410923SEvan.Yan@Sun.COM 		return ((pcihp_get_cb_ops())->cb_open(devp, flags, otyp,
157510923SEvan.Yan@Sun.COM 		    credp));
157610923SEvan.Yan@Sun.COM 	}
15770Sstevel@tonic-gate 
15780Sstevel@tonic-gate 	/*
15790Sstevel@tonic-gate 	 * Handle the open by tracking the device state.
15800Sstevel@tonic-gate 	 */
15810Sstevel@tonic-gate 	if (flags & FEXCL) {
158210923SEvan.Yan@Sun.COM 		if (ppb_p->ppb_soft_state != PCI_SOFT_STATE_CLOSED) {
15830Sstevel@tonic-gate 			mutex_exit(&ppb_p->ppb_mutex);
15840Sstevel@tonic-gate 			return (EBUSY);
15850Sstevel@tonic-gate 		}
158610923SEvan.Yan@Sun.COM 		ppb_p->ppb_soft_state = PCI_SOFT_STATE_OPEN_EXCL;
15870Sstevel@tonic-gate 	} else {
158810923SEvan.Yan@Sun.COM 		if (ppb_p->ppb_soft_state == PCI_SOFT_STATE_OPEN_EXCL) {
15890Sstevel@tonic-gate 			mutex_exit(&ppb_p->ppb_mutex);
15900Sstevel@tonic-gate 			return (EBUSY);
15910Sstevel@tonic-gate 		}
159210923SEvan.Yan@Sun.COM 		ppb_p->ppb_soft_state = PCI_SOFT_STATE_OPEN;
15930Sstevel@tonic-gate 	}
15940Sstevel@tonic-gate 	mutex_exit(&ppb_p->ppb_mutex);
15950Sstevel@tonic-gate 	return (0);
15960Sstevel@tonic-gate }
15970Sstevel@tonic-gate 
15980Sstevel@tonic-gate 
15990Sstevel@tonic-gate /* ARGSUSED */
16000Sstevel@tonic-gate static int
ppb_close(dev_t dev,int flags,int otyp,cred_t * credp)16010Sstevel@tonic-gate ppb_close(dev_t dev, int flags, int otyp, cred_t *credp)
16020Sstevel@tonic-gate {
160310923SEvan.Yan@Sun.COM 	int		instance = PCI_MINOR_NUM_TO_INSTANCE(getminor(dev));
160410923SEvan.Yan@Sun.COM 	ppb_devstate_t	*ppb_p = ddi_get_soft_state(ppb_state, instance);
16050Sstevel@tonic-gate 
16060Sstevel@tonic-gate 	if (otyp != OTYP_CHR)
16070Sstevel@tonic-gate 		return (EINVAL);
16080Sstevel@tonic-gate 
16090Sstevel@tonic-gate 	if (ppb_p == NULL)
16100Sstevel@tonic-gate 		return (ENXIO);
16110Sstevel@tonic-gate 
161210923SEvan.Yan@Sun.COM 	mutex_enter(&ppb_p->ppb_mutex);
161310923SEvan.Yan@Sun.COM 	/*
161410923SEvan.Yan@Sun.COM 	 * Ioctls will be handled by SPARC PCI Express framework for all
161510923SEvan.Yan@Sun.COM 	 * PCIe platforms
161610923SEvan.Yan@Sun.COM 	 */
161710923SEvan.Yan@Sun.COM 	if (ppb_p->parent_bus == PCIE_PCIECAP_DEV_TYPE_PCIE_DEV) {
161810923SEvan.Yan@Sun.COM 		int	rv;
16190Sstevel@tonic-gate 
162010923SEvan.Yan@Sun.COM 		rv = pcie_close(ppb_p->dip, dev, flags, otyp, credp);
162110923SEvan.Yan@Sun.COM 		mutex_exit(&ppb_p->ppb_mutex);
162210923SEvan.Yan@Sun.COM 
162310923SEvan.Yan@Sun.COM 		return (rv);
162410923SEvan.Yan@Sun.COM 	} else if (ppb_p->hotplug_capable == B_TRUE) {
162510923SEvan.Yan@Sun.COM 		mutex_exit(&ppb_p->ppb_mutex);
162610923SEvan.Yan@Sun.COM 		return ((pcihp_get_cb_ops())->cb_close(dev, flags, otyp,
162710923SEvan.Yan@Sun.COM 		    credp));
162810923SEvan.Yan@Sun.COM 	}
162910923SEvan.Yan@Sun.COM 
163010923SEvan.Yan@Sun.COM 	ppb_p->ppb_soft_state = PCI_SOFT_STATE_CLOSED;
16310Sstevel@tonic-gate 	mutex_exit(&ppb_p->ppb_mutex);
16320Sstevel@tonic-gate 	return (0);
16330Sstevel@tonic-gate }
16340Sstevel@tonic-gate 
16350Sstevel@tonic-gate 
16360Sstevel@tonic-gate /*
16370Sstevel@tonic-gate  * ppb_ioctl: devctl hotplug controls
16380Sstevel@tonic-gate  */
16390Sstevel@tonic-gate /* ARGSUSED */
16400Sstevel@tonic-gate static int
ppb_ioctl(dev_t dev,int cmd,intptr_t arg,int mode,cred_t * credp,int * rvalp)16410Sstevel@tonic-gate ppb_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp,
16420Sstevel@tonic-gate 	int *rvalp)
16430Sstevel@tonic-gate {
164410923SEvan.Yan@Sun.COM 	int		instance = PCI_MINOR_NUM_TO_INSTANCE(getminor(dev));
164510923SEvan.Yan@Sun.COM 	ppb_devstate_t	*ppb_p = ddi_get_soft_state(ppb_state, instance);
16460Sstevel@tonic-gate 	struct devctl_iocdata *dcp;
164710923SEvan.Yan@Sun.COM 	uint_t		bus_state;
164810923SEvan.Yan@Sun.COM 	dev_info_t	*self;
164910923SEvan.Yan@Sun.COM 	int		rv = 0;
16500Sstevel@tonic-gate 
16510Sstevel@tonic-gate 	if (ppb_p == NULL)
16520Sstevel@tonic-gate 		return (ENXIO);
16530Sstevel@tonic-gate 
165410923SEvan.Yan@Sun.COM 	/*
165510923SEvan.Yan@Sun.COM 	 * Ioctls will be handled by SPARC PCI Express framework for all
165610923SEvan.Yan@Sun.COM 	 * PCIe platforms
165710923SEvan.Yan@Sun.COM 	 */
165810923SEvan.Yan@Sun.COM 	if (ppb_p->parent_bus == PCIE_PCIECAP_DEV_TYPE_PCIE_DEV)
165910923SEvan.Yan@Sun.COM 		return (pcie_ioctl(ppb_p->dip, dev, cmd, arg, mode, credp,
166010923SEvan.Yan@Sun.COM 		    rvalp));
166110923SEvan.Yan@Sun.COM 	else if (ppb_p->hotplug_capable == B_TRUE)
166210923SEvan.Yan@Sun.COM 		return ((pcihp_get_cb_ops())->cb_ioctl(dev, cmd, arg, mode,
166310923SEvan.Yan@Sun.COM 		    credp, rvalp));
16640Sstevel@tonic-gate 
16650Sstevel@tonic-gate 	self = ppb_p->dip;
16660Sstevel@tonic-gate 
16670Sstevel@tonic-gate 	/*
16680Sstevel@tonic-gate 	 * We can use the generic implementation for these ioctls
16690Sstevel@tonic-gate 	 */
16700Sstevel@tonic-gate 	switch (cmd) {
16710Sstevel@tonic-gate 	case DEVCTL_DEVICE_GETSTATE:
16720Sstevel@tonic-gate 	case DEVCTL_DEVICE_ONLINE:
16730Sstevel@tonic-gate 	case DEVCTL_DEVICE_OFFLINE:
16740Sstevel@tonic-gate 	case DEVCTL_BUS_GETSTATE:
16750Sstevel@tonic-gate 		return (ndi_devctl_ioctl(self, cmd, arg, mode, 0));
16760Sstevel@tonic-gate 	}
16770Sstevel@tonic-gate 
16780Sstevel@tonic-gate 	/*
16790Sstevel@tonic-gate 	 * read devctl ioctl data
16800Sstevel@tonic-gate 	 */
16810Sstevel@tonic-gate 	if (ndi_dc_allochdl((void *)arg, &dcp) != NDI_SUCCESS)
16820Sstevel@tonic-gate 		return (EFAULT);
16830Sstevel@tonic-gate 
16840Sstevel@tonic-gate 	switch (cmd) {
16850Sstevel@tonic-gate 
16860Sstevel@tonic-gate 	case DEVCTL_DEVICE_RESET:
16870Sstevel@tonic-gate 		rv = ENOTSUP;
16880Sstevel@tonic-gate 		break;
16890Sstevel@tonic-gate 
16900Sstevel@tonic-gate 	case DEVCTL_BUS_QUIESCE:
16910Sstevel@tonic-gate 		if (ndi_get_bus_state(self, &bus_state) == NDI_SUCCESS)
16920Sstevel@tonic-gate 			if (bus_state == BUS_QUIESCED)
16930Sstevel@tonic-gate 				break;
16940Sstevel@tonic-gate 		(void) ndi_set_bus_state(self, BUS_QUIESCED);
16950Sstevel@tonic-gate 		break;
16960Sstevel@tonic-gate 
16970Sstevel@tonic-gate 	case DEVCTL_BUS_UNQUIESCE:
16980Sstevel@tonic-gate 		if (ndi_get_bus_state(self, &bus_state) == NDI_SUCCESS)
16990Sstevel@tonic-gate 			if (bus_state == BUS_ACTIVE)
17000Sstevel@tonic-gate 				break;
17010Sstevel@tonic-gate 		(void) ndi_set_bus_state(self, BUS_ACTIVE);
17020Sstevel@tonic-gate 		break;
17030Sstevel@tonic-gate 
17040Sstevel@tonic-gate 	case DEVCTL_BUS_RESET:
17050Sstevel@tonic-gate 		rv = ENOTSUP;
17060Sstevel@tonic-gate 		break;
17070Sstevel@tonic-gate 
17080Sstevel@tonic-gate 	case DEVCTL_BUS_RESETALL:
17090Sstevel@tonic-gate 		rv = ENOTSUP;
17100Sstevel@tonic-gate 		break;
17110Sstevel@tonic-gate 
17120Sstevel@tonic-gate 	default:
17130Sstevel@tonic-gate 		rv = ENOTTY;
17140Sstevel@tonic-gate 	}
17150Sstevel@tonic-gate 
17160Sstevel@tonic-gate 	ndi_dc_freehdl(dcp);
17170Sstevel@tonic-gate 	return (rv);
17180Sstevel@tonic-gate }
17190Sstevel@tonic-gate 
172010923SEvan.Yan@Sun.COM static int
ppb_prop_op(dev_t dev,dev_info_t * dip,ddi_prop_op_t prop_op,int flags,char * name,caddr_t valuep,int * lengthp)172110923SEvan.Yan@Sun.COM ppb_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, int flags,
172210923SEvan.Yan@Sun.COM     char *name, caddr_t valuep, int *lengthp)
17230Sstevel@tonic-gate {
172410923SEvan.Yan@Sun.COM 	int		instance = PCI_MINOR_NUM_TO_INSTANCE(getminor(dev));
172510923SEvan.Yan@Sun.COM 	ppb_devstate_t	*ppb_p = (ppb_devstate_t *)
172610923SEvan.Yan@Sun.COM 	    ddi_get_soft_state(ppb_state, instance);
17270Sstevel@tonic-gate 
17280Sstevel@tonic-gate 	if (ppb_p == NULL)
17290Sstevel@tonic-gate 		return (ENXIO);
17300Sstevel@tonic-gate 
173110923SEvan.Yan@Sun.COM 	if (ppb_p->parent_bus == PCIE_PCIECAP_DEV_TYPE_PCIE_DEV)
173210923SEvan.Yan@Sun.COM 		return (pcie_prop_op(dev, dip, prop_op, flags, name,
173310923SEvan.Yan@Sun.COM 		    valuep, lengthp));
17340Sstevel@tonic-gate 
173510923SEvan.Yan@Sun.COM 	return ((pcihp_get_cb_ops())->cb_prop_op(dev, dip, prop_op, flags,
173610923SEvan.Yan@Sun.COM 	    name, valuep, lengthp));
17370Sstevel@tonic-gate }
17380Sstevel@tonic-gate 
17390Sstevel@tonic-gate /*
17400Sstevel@tonic-gate  * Initialize our FMA resources
17410Sstevel@tonic-gate  */
17420Sstevel@tonic-gate static void
ppb_fm_init(ppb_devstate_t * ppb_p)17430Sstevel@tonic-gate ppb_fm_init(ppb_devstate_t *ppb_p)
17440Sstevel@tonic-gate {
17450Sstevel@tonic-gate 	ppb_p->fm_cap = DDI_FM_EREPORT_CAPABLE | DDI_FM_ERRCB_CAPABLE |
17467656SSherry.Moore@Sun.COM 	    DDI_FM_ACCCHK_CAPABLE | DDI_FM_DMACHK_CAPABLE;
17470Sstevel@tonic-gate 
17480Sstevel@tonic-gate 	/*
17490Sstevel@tonic-gate 	 * Request our capability level and get our parents capability
17500Sstevel@tonic-gate 	 * and ibc.
17510Sstevel@tonic-gate 	 */
17520Sstevel@tonic-gate 	ddi_fm_init(ppb_p->dip, &ppb_p->fm_cap, &ppb_p->fm_ibc);
17530Sstevel@tonic-gate 	ASSERT((ppb_p->fm_cap & DDI_FM_EREPORT_CAPABLE) &&
17540Sstevel@tonic-gate 	    (ppb_p->fm_cap & DDI_FM_ERRCB_CAPABLE));
17550Sstevel@tonic-gate 
17560Sstevel@tonic-gate 	pci_ereport_setup(ppb_p->dip);
17570Sstevel@tonic-gate 
17580Sstevel@tonic-gate 	/*
17590Sstevel@tonic-gate 	 * Register error callback with our parent.
17600Sstevel@tonic-gate 	 */
17610Sstevel@tonic-gate 	ddi_fm_handler_register(ppb_p->dip, ppb_err_callback, NULL);
17620Sstevel@tonic-gate }
17630Sstevel@tonic-gate 
17640Sstevel@tonic-gate /*
17650Sstevel@tonic-gate  * Breakdown our FMA resources
17660Sstevel@tonic-gate  */
17670Sstevel@tonic-gate static void
ppb_fm_fini(ppb_devstate_t * ppb_p)17680Sstevel@tonic-gate ppb_fm_fini(ppb_devstate_t *ppb_p)
17690Sstevel@tonic-gate {
17700Sstevel@tonic-gate 	/*
17710Sstevel@tonic-gate 	 * Clean up allocated fm structures
17720Sstevel@tonic-gate 	 */
17730Sstevel@tonic-gate 	ddi_fm_handler_unregister(ppb_p->dip);
17740Sstevel@tonic-gate 	pci_ereport_teardown(ppb_p->dip);
17750Sstevel@tonic-gate 	ddi_fm_fini(ppb_p->dip);
17760Sstevel@tonic-gate }
17770Sstevel@tonic-gate 
17780Sstevel@tonic-gate /*
17790Sstevel@tonic-gate  * Initialize FMA resources for children devices. Called when
17800Sstevel@tonic-gate  * child calls ddi_fm_init().
17810Sstevel@tonic-gate  */
17820Sstevel@tonic-gate /*ARGSUSED*/
17830Sstevel@tonic-gate static int
ppb_fm_init_child(dev_info_t * dip,dev_info_t * tdip,int cap,ddi_iblock_cookie_t * ibc)17840Sstevel@tonic-gate ppb_fm_init_child(dev_info_t *dip, dev_info_t *tdip, int cap,
17850Sstevel@tonic-gate 		ddi_iblock_cookie_t *ibc)
17860Sstevel@tonic-gate {
17870Sstevel@tonic-gate 	ppb_devstate_t *ppb_p = (ppb_devstate_t *)ddi_get_soft_state(ppb_state,
17887656SSherry.Moore@Sun.COM 	    ddi_get_instance(dip));
17890Sstevel@tonic-gate 	*ibc = ppb_p->fm_ibc;
17900Sstevel@tonic-gate 	return (ppb_p->fm_cap);
17910Sstevel@tonic-gate }
17920Sstevel@tonic-gate 
17930Sstevel@tonic-gate /*
17940Sstevel@tonic-gate  * FMA registered error callback
17950Sstevel@tonic-gate  */
17960Sstevel@tonic-gate static int
ppb_err_callback(dev_info_t * dip,ddi_fm_error_t * derr,const void * impl_data)17970Sstevel@tonic-gate ppb_err_callback(dev_info_t *dip, ddi_fm_error_t *derr, const void *impl_data)
17980Sstevel@tonic-gate {
17993274Set142600 	ppb_devstate_t *ppb_p = (ppb_devstate_t *)ddi_get_soft_state(ppb_state,
18007656SSherry.Moore@Sun.COM 	    ddi_get_instance(dip));
18013274Set142600 
18023274Set142600 	/*
18033274Set142600 	 * errors handled by SPARC PCI-E framework for PCIe platforms
18043274Set142600 	 */
18053274Set142600 	if (ppb_p->parent_bus == PCIE_PCIECAP_DEV_TYPE_PCIE_DEV)
18063274Set142600 		return (DDI_FM_OK);
18073274Set142600 
18083274Set142600 	/*
18093274Set142600 	 * do the following for SPARC PCI platforms
18103274Set142600 	 */
18110Sstevel@tonic-gate 	ASSERT(impl_data == NULL);
18121865Sdilpreet 	pci_ereport_post(dip, derr, NULL);
18131865Sdilpreet 	return (derr->fme_status);
18140Sstevel@tonic-gate }
18150Sstevel@tonic-gate 
18160Sstevel@tonic-gate static void
ppb_bus_enter(dev_info_t * dip,ddi_acc_handle_t handle)18170Sstevel@tonic-gate ppb_bus_enter(dev_info_t *dip, ddi_acc_handle_t handle)
18180Sstevel@tonic-gate {
18190Sstevel@tonic-gate 	i_ndi_busop_access_enter(dip, handle);
18200Sstevel@tonic-gate }
18210Sstevel@tonic-gate 
18220Sstevel@tonic-gate /* ARGSUSED */
18230Sstevel@tonic-gate static void
ppb_bus_exit(dev_info_t * dip,ddi_acc_handle_t handle)18240Sstevel@tonic-gate ppb_bus_exit(dev_info_t *dip, ddi_acc_handle_t handle)
18250Sstevel@tonic-gate {
18260Sstevel@tonic-gate 	i_ndi_busop_access_exit(dip, handle);
18270Sstevel@tonic-gate }
1828