xref: /illumos-gate/usr/src/uts/intel/io/pci/pci_pci.c (revision b5cf5bc2)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5685482d6Sjohnny  * Common Development and Distribution License (the "License").
6685482d6Sjohnny  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
229757e35cSStephen Hanson  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
25cd21e7c5SGarrett D'Amore /*
26cd21e7c5SGarrett D'Amore  * Copyright 2012 Garrett D'Amore <garrett@damore.org>.  All rights reserved.
27*b5cf5bc2SHans Rosenfeld  * Copyright 2018 Joyent, Inc.
28cd21e7c5SGarrett D'Amore  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * PCI to PCI bus bridge nexus driver
327c478bd9Sstevel@tonic-gate  */
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #include <sys/conf.h>
357c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
367c478bd9Sstevel@tonic-gate #include <sys/debug.h>
377c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
387c478bd9Sstevel@tonic-gate #include <sys/autoconf.h>
397c478bd9Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
407c478bd9Sstevel@tonic-gate #include <sys/pci.h>
4126947304SEvan Yan #include <sys/pci_impl.h>
42eae2e508Skrishnae #include <sys/pcie_impl.h>
437c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
447c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
457c478bd9Sstevel@tonic-gate #include <sys/sunndi.h>
4600d0963fSdilpreet #include <sys/ddifm.h>
4700d0963fSdilpreet #include <sys/ndifm.h>
4800d0963fSdilpreet #include <sys/fm/protocol.h>
4926947304SEvan Yan #include <sys/hotplug/pci/pcie_hp.h>
507c478bd9Sstevel@tonic-gate #include <sys/hotplug/pci/pcihp.h>
51685482d6Sjohnny #include <sys/pci_intr_lib.h>
52685482d6Sjohnny #include <sys/psm.h>
53cb7ea99dSJimmy Vetayases #include <sys/pci_cap.h>
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate /*
567c478bd9Sstevel@tonic-gate  * The variable controls the default setting of the command register
577c478bd9Sstevel@tonic-gate  * for pci devices.  See ppb_initchild() for details.
587c478bd9Sstevel@tonic-gate  */
5969889278Sanish static ushort_t ppb_command_default = PCI_COMM_ME | PCI_COMM_MAE | PCI_COMM_IO;
6069889278Sanish 
6169889278Sanish 
6269889278Sanish static int	ppb_bus_map(dev_info_t *, dev_info_t *, ddi_map_req_t *,
6369889278Sanish 		    off_t, off_t, caddr_t *);
6469889278Sanish static int	ppb_ctlops(dev_info_t *, dev_info_t *, ddi_ctl_enum_t,
6569889278Sanish 		    void *, void *);
6600d0963fSdilpreet static int	ppb_fm_init(dev_info_t *, dev_info_t *, int,
6700d0963fSdilpreet 		    ddi_iblock_cookie_t *);
6800d0963fSdilpreet static int	ppb_fm_callback(dev_info_t *, ddi_fm_error_t *, const void *);
6969889278Sanish static int	ppb_intr_ops(dev_info_t *, dev_info_t *, ddi_intr_op_t,
7069889278Sanish 		    ddi_intr_handle_impl_t *, void *);
71685482d6Sjohnny 
72685482d6Sjohnny /*
7369889278Sanish  * ppb_support_msi: Flag that controls MSI support across P2P Bridges.
741f77e19aSjveta  * By default, MSI is not supported except for special cases like HT
751f77e19aSjveta  * bridges/tunnels that have HT MSI mapping enabled.
7669889278Sanish  *
7769889278Sanish  * However, MSI support behavior can be patched on a system by changing
7869889278Sanish  * the value of this flag as shown below:-
791f77e19aSjveta  *	 0 = default value, MSI is allowed by this driver for special cases
801f77e19aSjveta  *	 1 = MSI supported without any checks for this driver
8169889278Sanish  *	-1 = MSI not supported at all
82685482d6Sjohnny  */
83685482d6Sjohnny int ppb_support_msi = 0;
8469889278Sanish 
851f77e19aSjveta /*
861f77e19aSjveta  * Controls the usage of the Hypertransport MSI mapping capability
871f77e19aSjveta  *	0 = default value, leave hardware function as it is
881f77e19aSjveta  *	1 = always enable HT MSI mapping
891f77e19aSjveta  *     -1 = always disable HT MSI mapping
901f77e19aSjveta  */
911f77e19aSjveta int ppb_support_ht_msimap = 0;
921f77e19aSjveta 
937c478bd9Sstevel@tonic-gate struct bus_ops ppb_bus_ops = {
947c478bd9Sstevel@tonic-gate 	BUSO_REV,
957c478bd9Sstevel@tonic-gate 	ppb_bus_map,
967c478bd9Sstevel@tonic-gate 	0,
977c478bd9Sstevel@tonic-gate 	0,
987c478bd9Sstevel@tonic-gate 	0,
997c478bd9Sstevel@tonic-gate 	i_ddi_map_fault,
100cd21e7c5SGarrett D'Amore 	0,
1017c478bd9Sstevel@tonic-gate 	ddi_dma_allochdl,
1027c478bd9Sstevel@tonic-gate 	ddi_dma_freehdl,
1037c478bd9Sstevel@tonic-gate 	ddi_dma_bindhdl,
1047c478bd9Sstevel@tonic-gate 	ddi_dma_unbindhdl,
1057c478bd9Sstevel@tonic-gate 	ddi_dma_flush,
1067c478bd9Sstevel@tonic-gate 	ddi_dma_win,
1077c478bd9Sstevel@tonic-gate 	ddi_dma_mctl,
1087c478bd9Sstevel@tonic-gate 	ppb_ctlops,
1097c478bd9Sstevel@tonic-gate 	ddi_bus_prop_op,
11026947304SEvan Yan 	0,			/* (*bus_get_eventcookie)();	*/
11126947304SEvan Yan 	0,			/* (*bus_add_eventcall)();	*/
11226947304SEvan Yan 	0,			/* (*bus_remove_eventcall)();	*/
11326947304SEvan Yan 	0,			/* (*bus_post_event)();		*/
11426947304SEvan Yan 	0,			/* (*bus_intr_ctl)();		*/
11558b49504SHans Rosenfeld 	0,			/* (*bus_config)();		*/
11658b49504SHans Rosenfeld 	0,			/* (*bus_unconfig)();		*/
11758b49504SHans Rosenfeld 	ppb_fm_init,		/* (*bus_fm_init)();		*/
11858b49504SHans Rosenfeld 	NULL,			/* (*bus_fm_fini)();		*/
11958b49504SHans Rosenfeld 	NULL,			/* (*bus_fm_access_enter)();	*/
12058b49504SHans Rosenfeld 	NULL,			/* (*bus_fm_access_exit)();	*/
12158b49504SHans Rosenfeld 	NULL,			/* (*bus_power)();	*/
12258b49504SHans Rosenfeld 	ppb_intr_ops,		/* (*bus_intr_op)();		*/
12358b49504SHans Rosenfeld 	pcie_hp_common_ops	/* (*bus_hp_op)();		*/
1247c478bd9Sstevel@tonic-gate };
1257c478bd9Sstevel@tonic-gate 
12670025d76Sjohnny /*
12770025d76Sjohnny  * The goal here is to leverage off of the pcihp.c source without making
12870025d76Sjohnny  * changes to it.  Call into it's cb_ops directly if needed.
12970025d76Sjohnny  */
13070025d76Sjohnny static int	ppb_open(dev_t *, int, int, cred_t *);
13170025d76Sjohnny static int	ppb_close(dev_t, int, int, cred_t *);
13270025d76Sjohnny static int	ppb_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
13370025d76Sjohnny static int	ppb_prop_op(dev_t, dev_info_t *, ddi_prop_op_t, int, char *,
13470025d76Sjohnny 		    caddr_t, int *);
13570025d76Sjohnny static int	ppb_info(dev_info_t *, ddi_info_cmd_t, void *, void **);
136eae2e508Skrishnae static void	ppb_peekpoke_cb(dev_info_t *, ddi_fm_error_t *);
13770025d76Sjohnny 
13870025d76Sjohnny struct cb_ops ppb_cb_ops = {
13970025d76Sjohnny 	ppb_open,			/* open */
14070025d76Sjohnny 	ppb_close,			/* close */
14170025d76Sjohnny 	nodev,				/* strategy */
14270025d76Sjohnny 	nodev,				/* print */
14370025d76Sjohnny 	nodev,				/* dump */
14470025d76Sjohnny 	nodev,				/* read */
14570025d76Sjohnny 	nodev,				/* write */
14670025d76Sjohnny 	ppb_ioctl,			/* ioctl */
14770025d76Sjohnny 	nodev,				/* devmap */
14870025d76Sjohnny 	nodev,				/* mmap */
14970025d76Sjohnny 	nodev,				/* segmap */
15070025d76Sjohnny 	nochpoll,			/* poll */
15170025d76Sjohnny 	ppb_prop_op,			/* cb_prop_op */
15270025d76Sjohnny 	NULL,				/* streamtab */
15370025d76Sjohnny 	D_NEW | D_MP | D_HOTPLUG,	/* Driver compatibility flag */
15470025d76Sjohnny 	CB_REV,				/* rev */
15570025d76Sjohnny 	nodev,				/* int (*cb_aread)() */
15670025d76Sjohnny 	nodev				/* int (*cb_awrite)() */
15770025d76Sjohnny };
15870025d76Sjohnny 
15970025d76Sjohnny 
1607c478bd9Sstevel@tonic-gate static int ppb_probe(dev_info_t *);
1617c478bd9Sstevel@tonic-gate static int ppb_attach(dev_info_t *devi, ddi_attach_cmd_t cmd);
1627c478bd9Sstevel@tonic-gate static int ppb_detach(dev_info_t *devi, ddi_detach_cmd_t cmd);
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate struct dev_ops ppb_ops = {
1657c478bd9Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev */
1667c478bd9Sstevel@tonic-gate 	0,			/* refcnt  */
16770025d76Sjohnny 	ppb_info,		/* info */
1687c478bd9Sstevel@tonic-gate 	nulldev,		/* identify */
1697c478bd9Sstevel@tonic-gate 	ppb_probe,		/* probe */
1707c478bd9Sstevel@tonic-gate 	ppb_attach,		/* attach */
1717c478bd9Sstevel@tonic-gate 	ppb_detach,		/* detach */
1727c478bd9Sstevel@tonic-gate 	nulldev,		/* reset */
17370025d76Sjohnny 	&ppb_cb_ops,		/* driver operations */
17419397407SSherry Moore 	&ppb_bus_ops,		/* bus operations */
17519397407SSherry Moore 	NULL,			/* power */
17619397407SSherry Moore 	ddi_quiesce_not_needed,		/* quiesce */
1777c478bd9Sstevel@tonic-gate };
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate /*
1807c478bd9Sstevel@tonic-gate  * Module linkage information for the kernel.
1817c478bd9Sstevel@tonic-gate  */
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate static struct modldrv modldrv = {
1847c478bd9Sstevel@tonic-gate 	&mod_driverops, /* Type of module */
18526947304SEvan Yan 	"Standard PCI to PCI bridge nexus driver",
1867c478bd9Sstevel@tonic-gate 	&ppb_ops,	/* driver ops */
1877c478bd9Sstevel@tonic-gate };
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
1907c478bd9Sstevel@tonic-gate 	MODREV_1,
1917c478bd9Sstevel@tonic-gate 	(void *)&modldrv,
1927c478bd9Sstevel@tonic-gate 	NULL
1937c478bd9Sstevel@tonic-gate };
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate /*
1967c478bd9Sstevel@tonic-gate  * soft state pointer and structure template:
1977c478bd9Sstevel@tonic-gate  */
1987c478bd9Sstevel@tonic-gate static void *ppb_state;
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate typedef struct {
2017c478bd9Sstevel@tonic-gate 	dev_info_t *dip;
20200d0963fSdilpreet 	int ppb_fmcap;
20300d0963fSdilpreet 	ddi_iblock_cookie_t ppb_fm_ibc;
20426947304SEvan Yan 	kmutex_t ppb_mutex;
20500d0963fSdilpreet 	kmutex_t ppb_peek_poke_mutex;
20600d0963fSdilpreet 	kmutex_t ppb_err_mutex;
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	/*
2097c478bd9Sstevel@tonic-gate 	 * cpr support:
2107c478bd9Sstevel@tonic-gate 	 */
2117c478bd9Sstevel@tonic-gate 	uint_t config_state_index;
2127c478bd9Sstevel@tonic-gate 	struct {
2137c478bd9Sstevel@tonic-gate 		dev_info_t *dip;
2147c478bd9Sstevel@tonic-gate 		ushort_t command;
2157c478bd9Sstevel@tonic-gate 		uchar_t cache_line_size;
2167c478bd9Sstevel@tonic-gate 		uchar_t latency_timer;
2177c478bd9Sstevel@tonic-gate 		uchar_t header_type;
2187c478bd9Sstevel@tonic-gate 		uchar_t sec_latency_timer;
2197c478bd9Sstevel@tonic-gate 		ushort_t bridge_control;
2207c478bd9Sstevel@tonic-gate 	} config_state[PCI_MAX_CHILDREN];
221eae2e508Skrishnae 
222c85864d8SKrishna Elango 	uint16_t parent_bus;
2237c478bd9Sstevel@tonic-gate } ppb_devstate_t;
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate /*
2277c478bd9Sstevel@tonic-gate  * forward function declarations:
2287c478bd9Sstevel@tonic-gate  */
22969889278Sanish static void	ppb_removechild(dev_info_t *);
23069889278Sanish static int	ppb_initchild(dev_info_t *child);
23169889278Sanish static void	ppb_save_config_regs(ppb_devstate_t *ppb_p);
23269889278Sanish static void	ppb_restore_config_regs(ppb_devstate_t *ppb_p);
2331f77e19aSjveta static boolean_t	ppb_ht_msimap_check(ddi_acc_handle_t cfg_hdl);
2341f77e19aSjveta static int	ppb_ht_msimap_set(ddi_acc_handle_t cfg_hdl, int cmd);
2351f77e19aSjveta 
2361f77e19aSjveta /*
2371f77e19aSjveta  * for <cmd> in ppb_ht_msimap_set
2381f77e19aSjveta  */
2391f77e19aSjveta #define	HT_MSIMAP_ENABLE	1
2401f77e19aSjveta #define	HT_MSIMAP_DISABLE	0
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate int
_init(void)2447c478bd9Sstevel@tonic-gate _init(void)
2457c478bd9Sstevel@tonic-gate {
2467c478bd9Sstevel@tonic-gate 	int e;
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 	if ((e = ddi_soft_state_init(&ppb_state, sizeof (ppb_devstate_t),
2497c478bd9Sstevel@tonic-gate 	    1)) == 0 && (e = mod_install(&modlinkage)) != 0)
2507c478bd9Sstevel@tonic-gate 		ddi_soft_state_fini(&ppb_state);
2517c478bd9Sstevel@tonic-gate 	return (e);
2527c478bd9Sstevel@tonic-gate }
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate int
_fini(void)2557c478bd9Sstevel@tonic-gate _fini(void)
2567c478bd9Sstevel@tonic-gate {
2577c478bd9Sstevel@tonic-gate 	int e;
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 	if ((e = mod_remove(&modlinkage)) == 0)
2607c478bd9Sstevel@tonic-gate 		ddi_soft_state_fini(&ppb_state);
2617c478bd9Sstevel@tonic-gate 	return (e);
2627c478bd9Sstevel@tonic-gate }
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate int
_info(struct modinfo * modinfop)2657c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
2667c478bd9Sstevel@tonic-gate {
2677c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
2687c478bd9Sstevel@tonic-gate }
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2717c478bd9Sstevel@tonic-gate static int
ppb_probe(dev_info_t * devi)2727c478bd9Sstevel@tonic-gate ppb_probe(dev_info_t *devi)
2737c478bd9Sstevel@tonic-gate {
2747c478bd9Sstevel@tonic-gate 	return (DDI_PROBE_SUCCESS);
2757c478bd9Sstevel@tonic-gate }
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2787c478bd9Sstevel@tonic-gate static int
ppb_attach(dev_info_t * devi,ddi_attach_cmd_t cmd)2797c478bd9Sstevel@tonic-gate ppb_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
2807c478bd9Sstevel@tonic-gate {
281eae2e508Skrishnae 	dev_info_t *root = ddi_root_node();
2827c478bd9Sstevel@tonic-gate 	int instance;
2837c478bd9Sstevel@tonic-gate 	ppb_devstate_t *ppb;
284eae2e508Skrishnae 	dev_info_t *pdip;
2857c478bd9Sstevel@tonic-gate 	ddi_acc_handle_t config_handle;
286eae2e508Skrishnae 	char *bus;
28726947304SEvan Yan 	int ret;
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 	switch (cmd) {
2907c478bd9Sstevel@tonic-gate 	case DDI_ATTACH:
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 		/*
2937c478bd9Sstevel@tonic-gate 		 * Make sure the "device_type" property exists.
2947c478bd9Sstevel@tonic-gate 		 */
2957c478bd9Sstevel@tonic-gate 		(void) ddi_prop_update_string(DDI_DEV_T_NONE, devi,
2967c478bd9Sstevel@tonic-gate 		    "device_type", "pci");
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 		/*
2997c478bd9Sstevel@tonic-gate 		 * Allocate and get soft state structure.
3007c478bd9Sstevel@tonic-gate 		 */
3017c478bd9Sstevel@tonic-gate 		instance = ddi_get_instance(devi);
3027c478bd9Sstevel@tonic-gate 		if (ddi_soft_state_zalloc(ppb_state, instance) != DDI_SUCCESS)
3037c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
3047c478bd9Sstevel@tonic-gate 		ppb = ddi_get_soft_state(ppb_state, instance);
3057c478bd9Sstevel@tonic-gate 		ppb->dip = devi;
30600d0963fSdilpreet 
30700d0963fSdilpreet 		/*
30800d0963fSdilpreet 		 * don't enable ereports if immediate child of npe
30900d0963fSdilpreet 		 */
31000d0963fSdilpreet 		if (strcmp(ddi_driver_name(ddi_get_parent(devi)), "npe") == 0)
31100d0963fSdilpreet 			ppb->ppb_fmcap = DDI_FM_ERRCB_CAPABLE |
31200d0963fSdilpreet 			    DDI_FM_ACCCHK_CAPABLE | DDI_FM_DMACHK_CAPABLE;
31300d0963fSdilpreet 		else
31400d0963fSdilpreet 			ppb->ppb_fmcap = DDI_FM_EREPORT_CAPABLE |
31500d0963fSdilpreet 			    DDI_FM_ERRCB_CAPABLE | DDI_FM_ACCCHK_CAPABLE |
31600d0963fSdilpreet 			    DDI_FM_DMACHK_CAPABLE;
31700d0963fSdilpreet 
31800d0963fSdilpreet 		ddi_fm_init(devi, &ppb->ppb_fmcap, &ppb->ppb_fm_ibc);
31926947304SEvan Yan 		mutex_init(&ppb->ppb_mutex, NULL, MUTEX_DRIVER, NULL);
32000d0963fSdilpreet 		mutex_init(&ppb->ppb_err_mutex, NULL, MUTEX_DRIVER,
32100d0963fSdilpreet 		    (void *)ppb->ppb_fm_ibc);
32200d0963fSdilpreet 		mutex_init(&ppb->ppb_peek_poke_mutex, NULL, MUTEX_DRIVER,
32300d0963fSdilpreet 		    (void *)ppb->ppb_fm_ibc);
32400d0963fSdilpreet 
32500d0963fSdilpreet 		if (ppb->ppb_fmcap & (DDI_FM_ERRCB_CAPABLE |
32600d0963fSdilpreet 		    DDI_FM_EREPORT_CAPABLE))
32700d0963fSdilpreet 			pci_ereport_setup(devi);
32800d0963fSdilpreet 		if (ppb->ppb_fmcap & DDI_FM_ERRCB_CAPABLE)
3292df1fe9cSrandyf 			ddi_fm_handler_register(devi, ppb_fm_callback, NULL);
33000d0963fSdilpreet 
3317c478bd9Sstevel@tonic-gate 		if (pci_config_setup(devi, &config_handle) != DDI_SUCCESS) {
33200d0963fSdilpreet 			if (ppb->ppb_fmcap & DDI_FM_ERRCB_CAPABLE)
33300d0963fSdilpreet 				ddi_fm_handler_unregister(devi);
33400d0963fSdilpreet 			if (ppb->ppb_fmcap & (DDI_FM_ERRCB_CAPABLE |
33500d0963fSdilpreet 			    DDI_FM_EREPORT_CAPABLE))
33600d0963fSdilpreet 				pci_ereport_teardown(devi);
33700d0963fSdilpreet 			ddi_fm_fini(devi);
3387c478bd9Sstevel@tonic-gate 			ddi_soft_state_free(ppb_state, instance);
3397c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
3407c478bd9Sstevel@tonic-gate 		}
3411f77e19aSjveta 
342c85864d8SKrishna Elango 		ppb->parent_bus = PCIE_PCIECAP_DEV_TYPE_PCI_PSEUDO;
343eae2e508Skrishnae 		for (pdip = ddi_get_parent(devi); pdip && (pdip != root) &&
344eae2e508Skrishnae 		    (ppb->parent_bus != PCIE_PCIECAP_DEV_TYPE_PCIE_DEV);
345eae2e508Skrishnae 		    pdip = ddi_get_parent(pdip)) {
346eae2e508Skrishnae 			if (ddi_prop_lookup_string(DDI_DEV_T_ANY, pdip,
347eae2e508Skrishnae 			    DDI_PROP_DONTPASS, "device_type", &bus) !=
348eae2e508Skrishnae 			    DDI_PROP_SUCCESS)
349eae2e508Skrishnae 				break;
350eae2e508Skrishnae 
351eae2e508Skrishnae 			if (strcmp(bus, "pciex") == 0)
352eae2e508Skrishnae 				ppb->parent_bus =
353eae2e508Skrishnae 				    PCIE_PCIECAP_DEV_TYPE_PCIE_DEV;
354eae2e508Skrishnae 
355eae2e508Skrishnae 			ddi_prop_free(bus);
356eae2e508Skrishnae 		}
357eae2e508Skrishnae 
3581f77e19aSjveta 		if (ppb_support_ht_msimap == 1)
3591f77e19aSjveta 			(void) ppb_ht_msimap_set(config_handle,
3601f77e19aSjveta 			    HT_MSIMAP_ENABLE);
3611f77e19aSjveta 		else if (ppb_support_ht_msimap == -1)
3621f77e19aSjveta 			(void) ppb_ht_msimap_set(config_handle,
3631f77e19aSjveta 			    HT_MSIMAP_DISABLE);
3641f77e19aSjveta 
3657c478bd9Sstevel@tonic-gate 		pci_config_teardown(&config_handle);
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 		/*
36826947304SEvan Yan 		 * Initialize hotplug support on this bus.
3697c478bd9Sstevel@tonic-gate 		 */
37026947304SEvan Yan 		if (ppb->parent_bus == PCIE_PCIECAP_DEV_TYPE_PCIE_DEV)
37126947304SEvan Yan 			ret = pcie_init(devi, NULL);
37226947304SEvan Yan 		else
37326947304SEvan Yan 			ret = pcihp_init(devi);
37426947304SEvan Yan 
37526947304SEvan Yan 		if (ret != DDI_SUCCESS) {
3762df1fe9cSrandyf 			cmn_err(CE_WARN,
3772df1fe9cSrandyf 			    "pci: Failed to setup hotplug framework");
37826947304SEvan Yan 			(void) ppb_detach(devi, DDI_DETACH);
37926947304SEvan Yan 			return (ret);
38026947304SEvan Yan 		}
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 		ddi_report_dev(devi);
3837c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 	case DDI_RESUME:
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate 		/*
3887c478bd9Sstevel@tonic-gate 		 * Get the soft state structure for the bridge.
3897c478bd9Sstevel@tonic-gate 		 */
3907c478bd9Sstevel@tonic-gate 		ppb = ddi_get_soft_state(ppb_state, ddi_get_instance(devi));
3917c478bd9Sstevel@tonic-gate 		ppb_restore_config_regs(ppb);
3927c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 	default:
3957c478bd9Sstevel@tonic-gate 		break;
3967c478bd9Sstevel@tonic-gate 	}
3977c478bd9Sstevel@tonic-gate 	return (DDI_FAILURE);
3987c478bd9Sstevel@tonic-gate }
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4017c478bd9Sstevel@tonic-gate static int
ppb_detach(dev_info_t * devi,ddi_detach_cmd_t cmd)4027c478bd9Sstevel@tonic-gate ppb_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
4037c478bd9Sstevel@tonic-gate {
4047c478bd9Sstevel@tonic-gate 	ppb_devstate_t *ppb;
40526947304SEvan Yan 	int		ret;
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate 	switch (cmd) {
4087c478bd9Sstevel@tonic-gate 	case DDI_DETACH:
4097c478bd9Sstevel@tonic-gate 		(void) ddi_prop_remove(DDI_DEV_T_NONE, devi, "device_type");
41000d0963fSdilpreet 
41100d0963fSdilpreet 		ppb = ddi_get_soft_state(ppb_state, ddi_get_instance(devi));
41200d0963fSdilpreet 		if (ppb->ppb_fmcap & DDI_FM_ERRCB_CAPABLE)
41300d0963fSdilpreet 			ddi_fm_handler_unregister(devi);
41400d0963fSdilpreet 		if (ppb->ppb_fmcap & (DDI_FM_ERRCB_CAPABLE |
41500d0963fSdilpreet 		    DDI_FM_EREPORT_CAPABLE))
41600d0963fSdilpreet 			pci_ereport_teardown(devi);
41726947304SEvan Yan 
41826947304SEvan Yan 		/*
41926947304SEvan Yan 		 * Uninitialize hotplug support on this bus.
42026947304SEvan Yan 		 */
42126947304SEvan Yan 		ret = (ppb->parent_bus == PCIE_PCIECAP_DEV_TYPE_PCIE_DEV) ?
42226947304SEvan Yan 		    pcie_uninit(devi) : pcihp_uninit(devi);
42326947304SEvan Yan 		if (ret != DDI_SUCCESS)
42426947304SEvan Yan 			return (DDI_FAILURE);
42526947304SEvan Yan 
42600d0963fSdilpreet 		mutex_destroy(&ppb->ppb_peek_poke_mutex);
42700d0963fSdilpreet 		mutex_destroy(&ppb->ppb_err_mutex);
42826947304SEvan Yan 		mutex_destroy(&ppb->ppb_mutex);
42900d0963fSdilpreet 		ddi_fm_fini(devi);
43000d0963fSdilpreet 
4317c478bd9Sstevel@tonic-gate 		/*
4327c478bd9Sstevel@tonic-gate 		 * And finally free the per-pci soft state.
4337c478bd9Sstevel@tonic-gate 		 */
4347c478bd9Sstevel@tonic-gate 		ddi_soft_state_free(ppb_state, ddi_get_instance(devi));
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate 	case DDI_SUSPEND:
4397c478bd9Sstevel@tonic-gate 		ppb = ddi_get_soft_state(ppb_state, ddi_get_instance(devi));
4407c478bd9Sstevel@tonic-gate 		ppb_save_config_regs(ppb);
4417c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate 	default:
4447c478bd9Sstevel@tonic-gate 		break;
4457c478bd9Sstevel@tonic-gate 	}
4467c478bd9Sstevel@tonic-gate 	return (DDI_FAILURE);
4477c478bd9Sstevel@tonic-gate }
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4507c478bd9Sstevel@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)4517c478bd9Sstevel@tonic-gate ppb_bus_map(dev_info_t *dip, dev_info_t *rdip, ddi_map_req_t *mp,
45258b49504SHans Rosenfeld     off_t offset, off_t len, caddr_t *vaddrp)
4537c478bd9Sstevel@tonic-gate {
4547c478bd9Sstevel@tonic-gate 	dev_info_t *pdip;
455837c1ac4SStephen Hanson 	ppb_devstate_t *ppb = ddi_get_soft_state(ppb_state,
456837c1ac4SStephen Hanson 	    ddi_get_instance(dip));
457837c1ac4SStephen Hanson 
4589757e35cSStephen Hanson 	if (strcmp(ddi_driver_name(ddi_get_parent(dip)), "npe") == 0 &&
4599757e35cSStephen Hanson 	    mp->map_handlep != NULL) {
460837c1ac4SStephen Hanson 		ddi_acc_impl_t *hdlp =
461837c1ac4SStephen Hanson 		    (ddi_acc_impl_t *)(mp->map_handlep)->ah_platform_private;
462837c1ac4SStephen Hanson 		hdlp->ahi_err_mutexp = &ppb->ppb_err_mutex;
463837c1ac4SStephen Hanson 		hdlp->ahi_peekpoke_mutexp = &ppb->ppb_peek_poke_mutex;
464837c1ac4SStephen Hanson 		hdlp->ahi_scan_dip = dip;
465837c1ac4SStephen Hanson 		hdlp->ahi_scan = ppb_peekpoke_cb;
466837c1ac4SStephen Hanson 	}
4677c478bd9Sstevel@tonic-gate 	pdip = (dev_info_t *)DEVI(dip)->devi_parent;
4687c478bd9Sstevel@tonic-gate 	return ((DEVI(pdip)->devi_ops->devo_bus_ops->bus_map)(pdip,
4692df1fe9cSrandyf 	    rdip, mp, offset, len, vaddrp));
4707c478bd9Sstevel@tonic-gate }
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4737c478bd9Sstevel@tonic-gate static int
ppb_ctlops(dev_info_t * dip,dev_info_t * rdip,ddi_ctl_enum_t ctlop,void * arg,void * result)4747c478bd9Sstevel@tonic-gate ppb_ctlops(dev_info_t *dip, dev_info_t *rdip,
47558b49504SHans Rosenfeld     ddi_ctl_enum_t ctlop, void *arg, void *result)
4767c478bd9Sstevel@tonic-gate {
4777c478bd9Sstevel@tonic-gate 	pci_regspec_t *drv_regp;
4787c478bd9Sstevel@tonic-gate 	int	reglen;
4797c478bd9Sstevel@tonic-gate 	int	rn;
4807c478bd9Sstevel@tonic-gate 	int	totreg;
481eae2e508Skrishnae 	ppb_devstate_t *ppb = ddi_get_soft_state(ppb_state,
482eae2e508Skrishnae 	    ddi_get_instance(dip));
483eae2e508Skrishnae 	struct detachspec *dsp;
4842df1fe9cSrandyf 	struct attachspec *asp;
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate 	switch (ctlop) {
4877c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_REPORTDEV:
4887c478bd9Sstevel@tonic-gate 		if (rdip == (dev_info_t *)0)
4897c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
4907c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "?PCI-device: %s@%s, %s%d\n",
4917c478bd9Sstevel@tonic-gate 		    ddi_node_name(rdip), ddi_get_name_addr(rdip),
4927c478bd9Sstevel@tonic-gate 		    ddi_driver_name(rdip),
4937c478bd9Sstevel@tonic-gate 		    ddi_get_instance(rdip));
4947c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_INITCHILD:
4977c478bd9Sstevel@tonic-gate 		return (ppb_initchild((dev_info_t *)arg));
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_UNINITCHILD:
5007c478bd9Sstevel@tonic-gate 		ppb_removechild((dev_info_t *)arg);
5017c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_SIDDEV:
5047c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_REGSIZE:
5077c478bd9Sstevel@tonic-gate 	case DDI_CTLOPS_NREGS:
5087c478bd9Sstevel@tonic-gate 		if (rdip == (dev_info_t *)0)
5097c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
5107c478bd9Sstevel@tonic-gate 		break;
5117c478bd9Sstevel@tonic-gate 
5122df1fe9cSrandyf 	/* X86 systems support PME wakeup from suspend */
5132df1fe9cSrandyf 	case DDI_CTLOPS_ATTACH:
514eae2e508Skrishnae 		if (!pcie_is_child(dip, rdip))
515eae2e508Skrishnae 			return (DDI_SUCCESS);
516eae2e508Skrishnae 
5172df1fe9cSrandyf 		asp = (struct attachspec *)arg;
518eae2e508Skrishnae 		if ((ppb->parent_bus == PCIE_PCIECAP_DEV_TYPE_PCIE_DEV) &&
519eae2e508Skrishnae 		    (asp->when == DDI_POST) && (asp->result == DDI_SUCCESS))
520eae2e508Skrishnae 			pf_init(rdip, (void *)ppb->ppb_fm_ibc, asp->cmd);
521eae2e508Skrishnae 
5222df1fe9cSrandyf 		if (asp->cmd == DDI_RESUME && asp->when == DDI_PRE)
5232df1fe9cSrandyf 			if (pci_pre_resume(rdip) != DDI_SUCCESS)
5242df1fe9cSrandyf 				return (DDI_FAILURE);
5252df1fe9cSrandyf 
526eae2e508Skrishnae 		return (DDI_SUCCESS);
5272df1fe9cSrandyf 
5282df1fe9cSrandyf 	case DDI_CTLOPS_DETACH:
529eae2e508Skrishnae 		if (!pcie_is_child(dip, rdip))
530eae2e508Skrishnae 			return (DDI_SUCCESS);
531eae2e508Skrishnae 
532eae2e508Skrishnae 		dsp = (struct detachspec *)arg;
533eae2e508Skrishnae 		if ((ppb->parent_bus == PCIE_PCIECAP_DEV_TYPE_PCIE_DEV) &&
534eae2e508Skrishnae 		    (dsp->when == DDI_PRE))
535eae2e508Skrishnae 			pf_fini(rdip, dsp->cmd);
536eae2e508Skrishnae 
537eae2e508Skrishnae 		if (dsp->cmd == DDI_SUSPEND && dsp->when == DDI_POST)
5382df1fe9cSrandyf 			if (pci_post_suspend(rdip) != DDI_SUCCESS)
5392df1fe9cSrandyf 				return (DDI_FAILURE);
540eae2e508Skrishnae 
541eae2e508Skrishnae 		return (DDI_SUCCESS);
5422df1fe9cSrandyf 
54300d0963fSdilpreet 	case DDI_CTLOPS_PEEK:
54400d0963fSdilpreet 	case DDI_CTLOPS_POKE:
54500d0963fSdilpreet 		if (strcmp(ddi_driver_name(ddi_get_parent(dip)), "npe") != 0)
54600d0963fSdilpreet 			return (ddi_ctlops(dip, rdip, ctlop, arg, result));
54700d0963fSdilpreet 		return (pci_peekpoke_check(dip, rdip, ctlop, arg, result,
54800d0963fSdilpreet 		    ddi_ctlops, &ppb->ppb_err_mutex,
549eae2e508Skrishnae 		    &ppb->ppb_peek_poke_mutex, ppb_peekpoke_cb));
55000d0963fSdilpreet 
5517c478bd9Sstevel@tonic-gate 	default:
5527c478bd9Sstevel@tonic-gate 		return (ddi_ctlops(dip, rdip, ctlop, arg, result));
5537c478bd9Sstevel@tonic-gate 	}
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate 	*(int *)result = 0;
556c9968fe7Scth 	if (ddi_getlongprop(DDI_DEV_T_ANY, rdip,
5572df1fe9cSrandyf 	    DDI_PROP_DONTPASS | DDI_PROP_CANSLEEP, "reg",
5582df1fe9cSrandyf 	    (caddr_t)&drv_regp, &reglen) != DDI_SUCCESS)
5597c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
5607c478bd9Sstevel@tonic-gate 
5617c478bd9Sstevel@tonic-gate 	totreg = reglen / sizeof (pci_regspec_t);
5627c478bd9Sstevel@tonic-gate 	if (ctlop == DDI_CTLOPS_NREGS)
5637c478bd9Sstevel@tonic-gate 		*(int *)result = totreg;
5647c478bd9Sstevel@tonic-gate 	else if (ctlop == DDI_CTLOPS_REGSIZE) {
565*b5cf5bc2SHans Rosenfeld 		uint64_t rs;
566*b5cf5bc2SHans Rosenfeld 
5677c478bd9Sstevel@tonic-gate 		rn = *(int *)arg;
5687c478bd9Sstevel@tonic-gate 		if (rn >= totreg) {
5697c478bd9Sstevel@tonic-gate 			kmem_free(drv_regp, reglen);
5707c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
5717c478bd9Sstevel@tonic-gate 		}
572*b5cf5bc2SHans Rosenfeld 
573*b5cf5bc2SHans Rosenfeld 		rs = drv_regp[rn].pci_size_low |
574*b5cf5bc2SHans Rosenfeld 		    ((uint64_t)drv_regp[rn].pci_size_hi << 32);
575*b5cf5bc2SHans Rosenfeld 		if (rs > OFF_MAX) {
576*b5cf5bc2SHans Rosenfeld 			kmem_free(drv_regp, reglen);
577*b5cf5bc2SHans Rosenfeld 			return (DDI_FAILURE);
578*b5cf5bc2SHans Rosenfeld 		}
579*b5cf5bc2SHans Rosenfeld 		*(off_t *)result = rs;
5807c478bd9Sstevel@tonic-gate 	}
5817c478bd9Sstevel@tonic-gate 
5827c478bd9Sstevel@tonic-gate 	kmem_free(drv_regp, reglen);
5837c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
5847c478bd9Sstevel@tonic-gate }
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate static int
ppb_name_child(dev_info_t * child,char * name,int namelen)5877c478bd9Sstevel@tonic-gate ppb_name_child(dev_info_t *child, char *name, int namelen)
5887c478bd9Sstevel@tonic-gate {
5897c478bd9Sstevel@tonic-gate 	pci_regspec_t *pci_rp;
5907c478bd9Sstevel@tonic-gate 	uint_t slot, func;
5917c478bd9Sstevel@tonic-gate 	char **unit_addr;
5927c478bd9Sstevel@tonic-gate 	uint_t n;
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate 	/*
5957c478bd9Sstevel@tonic-gate 	 * For .conf nodes, use unit-address property as name
5967c478bd9Sstevel@tonic-gate 	 */
5977c478bd9Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node(child) == 0) {
5987c478bd9Sstevel@tonic-gate 		if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, child,
5997c478bd9Sstevel@tonic-gate 		    DDI_PROP_DONTPASS, "unit-address", &unit_addr, &n) !=
6007c478bd9Sstevel@tonic-gate 		    DDI_PROP_SUCCESS) {
6017c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN,
6027c478bd9Sstevel@tonic-gate 			    "cannot find unit-address in %s.conf",
6037c478bd9Sstevel@tonic-gate 			    ddi_driver_name(child));
6047c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
6057c478bd9Sstevel@tonic-gate 		}
6067c478bd9Sstevel@tonic-gate 		if (n != 1 || *unit_addr == NULL || **unit_addr == 0) {
6077c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "unit-address property in %s.conf"
6087c478bd9Sstevel@tonic-gate 			    " not well-formed", ddi_driver_name(child));
6097c478bd9Sstevel@tonic-gate 			ddi_prop_free(unit_addr);
6107c478bd9Sstevel@tonic-gate 			return (DDI_SUCCESS);
6117c478bd9Sstevel@tonic-gate 		}
6127c478bd9Sstevel@tonic-gate 		(void) snprintf(name, namelen, "%s", *unit_addr);
6137c478bd9Sstevel@tonic-gate 		ddi_prop_free(unit_addr);
6147c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
6157c478bd9Sstevel@tonic-gate 	}
6167c478bd9Sstevel@tonic-gate 
6177c478bd9Sstevel@tonic-gate 	/* get child "reg" property */
6187c478bd9Sstevel@tonic-gate 	if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, child,
6197c478bd9Sstevel@tonic-gate 	    DDI_PROP_DONTPASS, "reg", (int **)&pci_rp, &n) != DDI_SUCCESS) {
6207c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
6217c478bd9Sstevel@tonic-gate 	}
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate 	/* copy the device identifications */
6247c478bd9Sstevel@tonic-gate 	slot = PCI_REG_DEV_G(pci_rp->pci_phys_hi);
6257c478bd9Sstevel@tonic-gate 	func = PCI_REG_FUNC_G(pci_rp->pci_phys_hi);
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate 	if (func != 0)
6287c478bd9Sstevel@tonic-gate 		(void) snprintf(name, namelen, "%x,%x", slot, func);
6297c478bd9Sstevel@tonic-gate 	else
6307c478bd9Sstevel@tonic-gate 		(void) snprintf(name, namelen, "%x", slot);
6317c478bd9Sstevel@tonic-gate 
6327c478bd9Sstevel@tonic-gate 	ddi_prop_free(pci_rp);
6337c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
6347c478bd9Sstevel@tonic-gate }
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate static int
ppb_initchild(dev_info_t * child)6377c478bd9Sstevel@tonic-gate ppb_initchild(dev_info_t *child)
6387c478bd9Sstevel@tonic-gate {
6397c478bd9Sstevel@tonic-gate 	struct ddi_parent_private_data *pdptr;
640eae2e508Skrishnae 	ppb_devstate_t *ppb;
6417c478bd9Sstevel@tonic-gate 	char name[MAXNAMELEN];
6427c478bd9Sstevel@tonic-gate 	ddi_acc_handle_t config_handle;
6437c478bd9Sstevel@tonic-gate 	ushort_t command_preserve, command;
6447c478bd9Sstevel@tonic-gate 
645eae2e508Skrishnae 	ppb = (ppb_devstate_t *)ddi_get_soft_state(ppb_state,
646eae2e508Skrishnae 	    ddi_get_instance(ddi_get_parent(child)));
647eae2e508Skrishnae 
6487c478bd9Sstevel@tonic-gate 	if (ppb_name_child(child, name, MAXNAMELEN) != DDI_SUCCESS)
6497c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
6507c478bd9Sstevel@tonic-gate 	ddi_set_name_addr(child, name);
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate 	/*
6537c478bd9Sstevel@tonic-gate 	 * Pseudo nodes indicate a prototype node with per-instance
6547c478bd9Sstevel@tonic-gate 	 * properties to be merged into the real h/w device node.
6557c478bd9Sstevel@tonic-gate 	 * The interpretation of the unit-address is DD[,F]
6567c478bd9Sstevel@tonic-gate 	 * where DD is the device id and F is the function.
6577c478bd9Sstevel@tonic-gate 	 */
6587c478bd9Sstevel@tonic-gate 	if (ndi_dev_is_persistent_node(child) == 0) {
6597c478bd9Sstevel@tonic-gate 		extern int pci_allow_pseudo_children;
6607c478bd9Sstevel@tonic-gate 
6617c478bd9Sstevel@tonic-gate 		ddi_set_parent_data(child, NULL);
6627c478bd9Sstevel@tonic-gate 
6637c478bd9Sstevel@tonic-gate 		/*
6647c478bd9Sstevel@tonic-gate 		 * Try to merge the properties from this prototype
6657c478bd9Sstevel@tonic-gate 		 * node into real h/w nodes.
6667c478bd9Sstevel@tonic-gate 		 */
6675c653d84Smabloom 		if (ndi_merge_node(child, ppb_name_child) == DDI_SUCCESS) {
6687c478bd9Sstevel@tonic-gate 			/*
6697c478bd9Sstevel@tonic-gate 			 * Merged ok - return failure to remove the node.
6707c478bd9Sstevel@tonic-gate 			 */
6717c478bd9Sstevel@tonic-gate 			ddi_set_name_addr(child, NULL);
6727c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
6737c478bd9Sstevel@tonic-gate 		}
6747c478bd9Sstevel@tonic-gate 
6757c478bd9Sstevel@tonic-gate 		/* workaround for ddivs to run under PCI */
6767c478bd9Sstevel@tonic-gate 		if (pci_allow_pseudo_children)
6777c478bd9Sstevel@tonic-gate 			return (DDI_SUCCESS);
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate 		/*
6807c478bd9Sstevel@tonic-gate 		 * The child was not merged into a h/w node,
6817c478bd9Sstevel@tonic-gate 		 * but there's not much we can do with it other
6827c478bd9Sstevel@tonic-gate 		 * than return failure to cause the node to be removed.
6837c478bd9Sstevel@tonic-gate 		 */
6847c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "!%s@%s: %s.conf properties not merged",
6857c478bd9Sstevel@tonic-gate 		    ddi_driver_name(child), ddi_get_name_addr(child),
6867c478bd9Sstevel@tonic-gate 		    ddi_driver_name(child));
6877c478bd9Sstevel@tonic-gate 		ddi_set_name_addr(child, NULL);
6887c478bd9Sstevel@tonic-gate 		return (DDI_NOT_WELL_FORMED);
6897c478bd9Sstevel@tonic-gate 	}
6907c478bd9Sstevel@tonic-gate 
691eae2e508Skrishnae 	ddi_set_parent_data(child, NULL);
692eae2e508Skrishnae 
693eae2e508Skrishnae 	/*
694eae2e508Skrishnae 	 * PCIe FMA specific
695eae2e508Skrishnae 	 *
696eae2e508Skrishnae 	 * Note: parent_data for parent is created only if this is PCI-E
697eae2e508Skrishnae 	 * platform, for which, SG take a different route to handle device
698eae2e508Skrishnae 	 * errors.
699eae2e508Skrishnae 	 */
700eae2e508Skrishnae 	if (ppb->parent_bus == PCIE_PCIECAP_DEV_TYPE_PCIE_DEV) {
701c0da6274SZhi-Jun Robin Fu 		if (pcie_init_cfghdl(child) != DDI_SUCCESS)
702eae2e508Skrishnae 			return (DDI_FAILURE);
703fc256490SJason Beloro 		pcie_init_dom(child);
704eae2e508Skrishnae 	}
705eae2e508Skrishnae 
706c9968fe7Scth 	/* transfer select properties from PROM to kernel */
7072df1fe9cSrandyf 	if (ddi_getprop(DDI_DEV_T_NONE, child, DDI_PROP_DONTPASS,
7082df1fe9cSrandyf 	    "interrupts", -1) != -1) {
7097c478bd9Sstevel@tonic-gate 		pdptr = kmem_zalloc((sizeof (struct ddi_parent_private_data) +
7107c478bd9Sstevel@tonic-gate 		    sizeof (struct intrspec)), KM_SLEEP);
7117c478bd9Sstevel@tonic-gate 		pdptr->par_intr = (struct intrspec *)(pdptr + 1);
7127c478bd9Sstevel@tonic-gate 		pdptr->par_nintr = 1;
7137c478bd9Sstevel@tonic-gate 		ddi_set_parent_data(child, pdptr);
7147c478bd9Sstevel@tonic-gate 	} else
7157c478bd9Sstevel@tonic-gate 		ddi_set_parent_data(child, NULL);
7167c478bd9Sstevel@tonic-gate 
717fc256490SJason Beloro 	if (pci_config_setup(child, &config_handle) != DDI_SUCCESS) {
718fc256490SJason Beloro 		pcie_fini_dom(child);
7197c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
720fc256490SJason Beloro 	}
7217c478bd9Sstevel@tonic-gate 
7227c478bd9Sstevel@tonic-gate 	/*
7237c478bd9Sstevel@tonic-gate 	 * Support for the "command-preserve" property.
7247c478bd9Sstevel@tonic-gate 	 */
7257c478bd9Sstevel@tonic-gate 	command_preserve = ddi_prop_get_int(DDI_DEV_T_ANY, child,
72669889278Sanish 	    DDI_PROP_DONTPASS, "command-preserve", 0);
7277c478bd9Sstevel@tonic-gate 	command = pci_config_get16(config_handle, PCI_CONF_COMM);
7287c478bd9Sstevel@tonic-gate 	command &= (command_preserve | PCI_COMM_BACK2BACK_ENAB);
7297c478bd9Sstevel@tonic-gate 	command |= (ppb_command_default & ~command_preserve);
7307c478bd9Sstevel@tonic-gate 	pci_config_put16(config_handle, PCI_CONF_COMM, command);
7317c478bd9Sstevel@tonic-gate 
7327c478bd9Sstevel@tonic-gate 	pci_config_teardown(&config_handle);
7337c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
7347c478bd9Sstevel@tonic-gate }
7357c478bd9Sstevel@tonic-gate 
7367c478bd9Sstevel@tonic-gate static void
ppb_removechild(dev_info_t * dip)7377c478bd9Sstevel@tonic-gate ppb_removechild(dev_info_t *dip)
7387c478bd9Sstevel@tonic-gate {
7397c478bd9Sstevel@tonic-gate 	struct ddi_parent_private_data *pdptr;
740eae2e508Skrishnae 	ppb_devstate_t *ppb;
741eae2e508Skrishnae 
742eae2e508Skrishnae 	ppb = (ppb_devstate_t *)ddi_get_soft_state(ppb_state,
743eae2e508Skrishnae 	    ddi_get_instance(ddi_get_parent(dip)));
7447c478bd9Sstevel@tonic-gate 
745fc256490SJason Beloro 	if (ppb->parent_bus == PCIE_PCIECAP_DEV_TYPE_PCIE_DEV) {
746fc256490SJason Beloro 		pcie_fini_dom(dip);
747c0da6274SZhi-Jun Robin Fu 		pcie_fini_cfghdl(dip);
748fc256490SJason Beloro 	} else if ((pdptr = ddi_get_parent_data(dip)) != NULL) {
7497c478bd9Sstevel@tonic-gate 		kmem_free(pdptr, (sizeof (*pdptr) + sizeof (struct intrspec)));
7507c478bd9Sstevel@tonic-gate 		ddi_set_parent_data(dip, NULL);
7517c478bd9Sstevel@tonic-gate 	}
7527c478bd9Sstevel@tonic-gate 	ddi_set_name_addr(dip, NULL);
7537c478bd9Sstevel@tonic-gate 
7547c478bd9Sstevel@tonic-gate 	/*
7557c478bd9Sstevel@tonic-gate 	 * Strip the node to properly convert it back to prototype form
7567c478bd9Sstevel@tonic-gate 	 */
7577c478bd9Sstevel@tonic-gate 	ddi_remove_minor_node(dip, NULL);
7587c478bd9Sstevel@tonic-gate 
7597c478bd9Sstevel@tonic-gate 	impl_rem_dev_props(dip);
7607c478bd9Sstevel@tonic-gate }
7617c478bd9Sstevel@tonic-gate 
7627c478bd9Sstevel@tonic-gate /*
7637c478bd9Sstevel@tonic-gate  * ppb_save_config_regs
7647c478bd9Sstevel@tonic-gate  *
7657c478bd9Sstevel@tonic-gate  * This routine saves the state of the configuration registers of all
7667c478bd9Sstevel@tonic-gate  * the child nodes of each PBM.
7677c478bd9Sstevel@tonic-gate  *
7687c478bd9Sstevel@tonic-gate  * used by: ppb_detach() on suspends
7697c478bd9Sstevel@tonic-gate  *
7707c478bd9Sstevel@tonic-gate  * return value: none
7717c478bd9Sstevel@tonic-gate  */
7727c478bd9Sstevel@tonic-gate static void
ppb_save_config_regs(ppb_devstate_t * ppb_p)7737c478bd9Sstevel@tonic-gate ppb_save_config_regs(ppb_devstate_t *ppb_p)
7747c478bd9Sstevel@tonic-gate {
7757c478bd9Sstevel@tonic-gate 	int i;
7767c478bd9Sstevel@tonic-gate 	dev_info_t *dip;
7777c478bd9Sstevel@tonic-gate 	ddi_acc_handle_t config_handle;
7787c478bd9Sstevel@tonic-gate 
7797c478bd9Sstevel@tonic-gate 	for (i = 0, dip = ddi_get_child(ppb_p->dip); dip != NULL;
7802df1fe9cSrandyf 	    i++, dip = ddi_get_next_sibling(dip)) {
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate 		if (pci_config_setup(dip, &config_handle) != DDI_SUCCESS) {
7837c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s%d: can't config space for %s%d\n",
7842df1fe9cSrandyf 			    ddi_driver_name(ppb_p->dip),
7852df1fe9cSrandyf 			    ddi_get_instance(ppb_p->dip),
7862df1fe9cSrandyf 			    ddi_driver_name(dip),
7872df1fe9cSrandyf 			    ddi_get_instance(dip));
7887c478bd9Sstevel@tonic-gate 			continue;
7897c478bd9Sstevel@tonic-gate 		}
7907c478bd9Sstevel@tonic-gate 
7917c478bd9Sstevel@tonic-gate 		ppb_p->config_state[i].dip = dip;
7927c478bd9Sstevel@tonic-gate 		ppb_p->config_state[i].command =
7932df1fe9cSrandyf 		    pci_config_get16(config_handle, PCI_CONF_COMM);
7947c478bd9Sstevel@tonic-gate 		pci_config_teardown(&config_handle);
7957c478bd9Sstevel@tonic-gate 	}
7967c478bd9Sstevel@tonic-gate 	ppb_p->config_state_index = i;
7977c478bd9Sstevel@tonic-gate }
7987c478bd9Sstevel@tonic-gate 
7997c478bd9Sstevel@tonic-gate 
8007c478bd9Sstevel@tonic-gate /*
8017c478bd9Sstevel@tonic-gate  * ppb_restore_config_regs
8027c478bd9Sstevel@tonic-gate  *
8037c478bd9Sstevel@tonic-gate  * This routine restores the state of the configuration registers of all
8047c478bd9Sstevel@tonic-gate  * the child nodes of each PBM.
8057c478bd9Sstevel@tonic-gate  *
8067c478bd9Sstevel@tonic-gate  * used by: ppb_attach() on resume
8077c478bd9Sstevel@tonic-gate  *
8087c478bd9Sstevel@tonic-gate  * return value: none
8097c478bd9Sstevel@tonic-gate  */
8107c478bd9Sstevel@tonic-gate static void
ppb_restore_config_regs(ppb_devstate_t * ppb_p)8117c478bd9Sstevel@tonic-gate ppb_restore_config_regs(ppb_devstate_t *ppb_p)
8127c478bd9Sstevel@tonic-gate {
8137c478bd9Sstevel@tonic-gate 	int i;
8147c478bd9Sstevel@tonic-gate 	dev_info_t *dip;
8157c478bd9Sstevel@tonic-gate 	ddi_acc_handle_t config_handle;
8167c478bd9Sstevel@tonic-gate 
8177c478bd9Sstevel@tonic-gate 	for (i = 0; i < ppb_p->config_state_index; i++) {
8187c478bd9Sstevel@tonic-gate 		dip = ppb_p->config_state[i].dip;
8197c478bd9Sstevel@tonic-gate 		if (pci_config_setup(dip, &config_handle) != DDI_SUCCESS) {
8207c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s%d: can't config space for %s%d\n",
8212df1fe9cSrandyf 			    ddi_driver_name(ppb_p->dip),
8222df1fe9cSrandyf 			    ddi_get_instance(ppb_p->dip),
8232df1fe9cSrandyf 			    ddi_driver_name(dip),
8242df1fe9cSrandyf 			    ddi_get_instance(dip));
8257c478bd9Sstevel@tonic-gate 			continue;
8267c478bd9Sstevel@tonic-gate 		}
8277c478bd9Sstevel@tonic-gate 		pci_config_put16(config_handle, PCI_CONF_COMM,
8282df1fe9cSrandyf 		    ppb_p->config_state[i].command);
8297c478bd9Sstevel@tonic-gate 		pci_config_teardown(&config_handle);
8307c478bd9Sstevel@tonic-gate 	}
8317c478bd9Sstevel@tonic-gate }
83270025d76Sjohnny 
833685482d6Sjohnny 
8341f77e19aSjveta static boolean_t
ppb_ht_msimap_check(ddi_acc_handle_t cfg_hdl)8351f77e19aSjveta ppb_ht_msimap_check(ddi_acc_handle_t cfg_hdl)
8361f77e19aSjveta {
837cb7ea99dSJimmy Vetayases 	uint16_t ptr;
8381f77e19aSjveta 
839cb7ea99dSJimmy Vetayases 	if (pci_htcap_locate(cfg_hdl,
840cb7ea99dSJimmy Vetayases 	    PCI_HTCAP_TYPE_MASK | PCI_HTCAP_MSIMAP_ENABLE_MASK,
841cb7ea99dSJimmy Vetayases 	    PCI_HTCAP_MSIMAP_TYPE | PCI_HTCAP_MSIMAP_ENABLE, &ptr) !=
842cb7ea99dSJimmy Vetayases 	    DDI_SUCCESS)
8431f77e19aSjveta 		return (B_FALSE);
8441f77e19aSjveta 
8451f77e19aSjveta 	return (B_TRUE);
8461f77e19aSjveta }
8471f77e19aSjveta 
8481f77e19aSjveta 
8491f77e19aSjveta static int
ppb_ht_msimap_set(ddi_acc_handle_t cfg_hdl,int cmd)8501f77e19aSjveta ppb_ht_msimap_set(ddi_acc_handle_t cfg_hdl, int cmd)
8511f77e19aSjveta {
852cb7ea99dSJimmy Vetayases 	uint16_t ptr;
8531f77e19aSjveta 	uint16_t reg;
8541f77e19aSjveta 
855cb7ea99dSJimmy Vetayases 	if (pci_htcap_locate(cfg_hdl, PCI_HTCAP_TYPE_MASK,
856cb7ea99dSJimmy Vetayases 	    PCI_HTCAP_MSIMAP_TYPE, &ptr) != DDI_SUCCESS)
8571f77e19aSjveta 		return (0);
8581f77e19aSjveta 
8591f77e19aSjveta 	reg = pci_config_get16(cfg_hdl, ptr + PCI_CAP_ID_REGS_OFF);
8601f77e19aSjveta 	switch (cmd) {
8611f77e19aSjveta 	case HT_MSIMAP_ENABLE:
862cb7ea99dSJimmy Vetayases 		reg |= PCI_HTCAP_MSIMAP_ENABLE;
8631f77e19aSjveta 		break;
8641f77e19aSjveta 	case HT_MSIMAP_DISABLE:
8651f77e19aSjveta 	default:
866cb7ea99dSJimmy Vetayases 		reg &= ~(uint16_t)PCI_HTCAP_MSIMAP_ENABLE;
8671f77e19aSjveta 	}
8681f77e19aSjveta 
8691f77e19aSjveta 	pci_config_put16(cfg_hdl, ptr + PCI_CAP_ID_REGS_OFF, reg);
8701f77e19aSjveta 	return (1);
8711f77e19aSjveta }
8721f77e19aSjveta 
8731f77e19aSjveta 
8741f77e19aSjveta /*
8751f77e19aSjveta  * intercept certain interrupt services to handle special cases
876685482d6Sjohnny  */
877685482d6Sjohnny static int
ppb_intr_ops(dev_info_t * pdip,dev_info_t * rdip,ddi_intr_op_t intr_op,ddi_intr_handle_impl_t * hdlp,void * result)878685482d6Sjohnny ppb_intr_ops(dev_info_t *pdip, dev_info_t *rdip, ddi_intr_op_t intr_op,
879685482d6Sjohnny     ddi_intr_handle_impl_t *hdlp, void *result)
880685482d6Sjohnny {
8811f77e19aSjveta 	ddi_acc_handle_t cfg_hdl;
8821f77e19aSjveta 	int rv = DDI_SUCCESS;
883685482d6Sjohnny 
884685482d6Sjohnny 	if (intr_op != DDI_INTROP_SUPPORTED_TYPES)
885685482d6Sjohnny 		return (i_ddi_intr_ops(pdip, rdip, intr_op, hdlp, result));
886685482d6Sjohnny 
887685482d6Sjohnny 	DDI_INTR_NEXDBG((CE_CONT,
888685482d6Sjohnny 	    "ppb_intr_ops: pdip 0x%p, rdip 0x%p, op %x handle 0x%p\n",
889685482d6Sjohnny 	    (void *)pdip, (void *)rdip, intr_op, (void *)hdlp));
890685482d6Sjohnny 
891685482d6Sjohnny 	/* Fixed interrupt is supported by default */
892685482d6Sjohnny 	*(int *)result = DDI_INTR_TYPE_FIXED;
893685482d6Sjohnny 
8941f77e19aSjveta 	if (ppb_support_msi == -1) {
8951f77e19aSjveta 		DDI_INTR_NEXDBG((CE_CONT,
8961f77e19aSjveta 		    "ppb_intr_ops: MSI is not allowed\n"));
8971f77e19aSjveta 		goto OUT;
8981f77e19aSjveta 	}
8991f77e19aSjveta 
9001f77e19aSjveta 	if (ppb_support_msi == 1) {
901685482d6Sjohnny 		DDI_INTR_NEXDBG((CE_CONT,
902685482d6Sjohnny 		    "ppb_intr_ops: MSI is always allowed\n"));
9031f77e19aSjveta 		rv = i_ddi_intr_ops(pdip, rdip, intr_op, hdlp, result);
9041f77e19aSjveta 		goto OUT;
9051f77e19aSjveta 	}
9061f77e19aSjveta 
9071f77e19aSjveta 	if (pci_config_setup(pdip, &cfg_hdl) != DDI_SUCCESS) {
9081f77e19aSjveta 		DDI_INTR_NEXDBG((CE_CONT,
9091f77e19aSjveta 		    "ppb_intr_ops: pci_config_setup() failed\n"));
9101f77e19aSjveta 		goto OUT;
911685482d6Sjohnny 	}
9121f77e19aSjveta 
9131f77e19aSjveta 	/*
9141f77e19aSjveta 	 * check for hypertransport msi mapping capability
9151f77e19aSjveta 	 */
9161f77e19aSjveta 	if (ppb_ht_msimap_check(cfg_hdl)) {
9171f77e19aSjveta 		DDI_INTR_NEXDBG((CE_CONT,
9181f77e19aSjveta 		    "ppb_intr_ops: HT MSI mapping enabled\n"));
9191f77e19aSjveta 		rv = i_ddi_intr_ops(pdip, rdip, intr_op, hdlp, result);
9201f77e19aSjveta 	}
9211f77e19aSjveta 
9221f77e19aSjveta 	/*
9231f77e19aSjveta 	 * if we add failure conditions after pci_config_setup, move this to
9241f77e19aSjveta 	 * OUT and use an extra flag to indicate the need to teardown cfg_hdl
9251f77e19aSjveta 	 */
9261f77e19aSjveta 	pci_config_teardown(&cfg_hdl);
9271f77e19aSjveta 
9281f77e19aSjveta OUT:
929685482d6Sjohnny 	DDI_INTR_NEXDBG((CE_CONT,
930685482d6Sjohnny 	    "ppb_intr_ops: rdip 0x%p, returns supported types: 0x%x\n",
931685482d6Sjohnny 	    (void *)rdip, *(int *)result));
9321f77e19aSjveta 	return (rv);
933685482d6Sjohnny }
934685482d6Sjohnny 
93526947304SEvan Yan /* ARGSUSED */
93670025d76Sjohnny static int
ppb_open(dev_t * devp,int flags,int otyp,cred_t * credp)93770025d76Sjohnny ppb_open(dev_t *devp, int flags, int otyp, cred_t *credp)
93870025d76Sjohnny {
93926947304SEvan Yan 	int		instance = PCI_MINOR_NUM_TO_INSTANCE(getminor(*devp));
94026947304SEvan Yan 	ppb_devstate_t	*ppb_p = ddi_get_soft_state(ppb_state, instance);
94126947304SEvan Yan 	int	rv;
94226947304SEvan Yan 
94326947304SEvan Yan 	if (ppb_p == NULL)
94426947304SEvan Yan 		return (ENXIO);
94526947304SEvan Yan 
94626947304SEvan Yan 	/*
94726947304SEvan Yan 	 * Ioctls will be handled by PCI Express framework for all
94826947304SEvan Yan 	 * PCIe platforms
94926947304SEvan Yan 	 */
95026947304SEvan Yan 	if (ppb_p->parent_bus == PCIE_PCIECAP_DEV_TYPE_PCIE_DEV) {
95126947304SEvan Yan 		mutex_enter(&ppb_p->ppb_mutex);
95226947304SEvan Yan 		rv = pcie_open(ppb_p->dip, devp, flags, otyp, credp);
95326947304SEvan Yan 		mutex_exit(&ppb_p->ppb_mutex);
95426947304SEvan Yan 		return (rv);
95526947304SEvan Yan 	}
95626947304SEvan Yan 
95770025d76Sjohnny 	return ((pcihp_get_cb_ops())->cb_open(devp, flags, otyp, credp));
95870025d76Sjohnny }
95970025d76Sjohnny 
96026947304SEvan Yan /* ARGSUSED */
96170025d76Sjohnny static int
ppb_close(dev_t dev,int flags,int otyp,cred_t * credp)96270025d76Sjohnny ppb_close(dev_t dev, int flags, int otyp, cred_t *credp)
96370025d76Sjohnny {
96426947304SEvan Yan 	int		instance = PCI_MINOR_NUM_TO_INSTANCE(getminor(dev));
96526947304SEvan Yan 	ppb_devstate_t	*ppb_p = ddi_get_soft_state(ppb_state, instance);
96626947304SEvan Yan 	int	rv;
96726947304SEvan Yan 
96826947304SEvan Yan 	if (ppb_p == NULL)
96926947304SEvan Yan 		return (ENXIO);
97026947304SEvan Yan 
97126947304SEvan Yan 	mutex_enter(&ppb_p->ppb_mutex);
97226947304SEvan Yan 	/*
97326947304SEvan Yan 	 * Ioctls will be handled by PCI Express framework for all
97426947304SEvan Yan 	 * PCIe platforms
97526947304SEvan Yan 	 */
97626947304SEvan Yan 	if (ppb_p->parent_bus == PCIE_PCIECAP_DEV_TYPE_PCIE_DEV) {
97726947304SEvan Yan 		rv = pcie_close(ppb_p->dip, dev, flags, otyp, credp);
97826947304SEvan Yan 		mutex_exit(&ppb_p->ppb_mutex);
97926947304SEvan Yan 		return (rv);
98026947304SEvan Yan 	}
98126947304SEvan Yan 
98226947304SEvan Yan 	mutex_exit(&ppb_p->ppb_mutex);
98370025d76Sjohnny 	return ((pcihp_get_cb_ops())->cb_close(dev, flags, otyp, credp));
98470025d76Sjohnny }
98570025d76Sjohnny 
98626947304SEvan Yan /*
98726947304SEvan Yan  * ppb_ioctl: devctl hotplug controls
98826947304SEvan Yan  */
98926947304SEvan Yan /* ARGSUSED */
99070025d76Sjohnny static int
ppb_ioctl(dev_t dev,int cmd,intptr_t arg,int mode,cred_t * credp,int * rvalp)99126947304SEvan Yan ppb_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *credp,
99258b49504SHans Rosenfeld     int *rvalp)
99370025d76Sjohnny {
99426947304SEvan Yan 	int		instance = PCI_MINOR_NUM_TO_INSTANCE(getminor(dev));
99526947304SEvan Yan 	ppb_devstate_t	*ppb_p = ddi_get_soft_state(ppb_state, instance);
99626947304SEvan Yan 
99726947304SEvan Yan 	if (ppb_p == NULL)
99826947304SEvan Yan 		return (ENXIO);
99926947304SEvan Yan 
100026947304SEvan Yan 	/*
100126947304SEvan Yan 	 * Ioctls will be handled by PCI Express framework for all
100226947304SEvan Yan 	 * PCIe platforms
100326947304SEvan Yan 	 */
100426947304SEvan Yan 	if (ppb_p->parent_bus == PCIE_PCIECAP_DEV_TYPE_PCIE_DEV)
100526947304SEvan Yan 		return (pcie_ioctl(ppb_p->dip, dev, cmd, arg, mode, credp,
100626947304SEvan Yan 		    rvalp));
100726947304SEvan Yan 
100870025d76Sjohnny 	return ((pcihp_get_cb_ops())->cb_ioctl(dev, cmd, arg, mode, credp,
100970025d76Sjohnny 	    rvalp));
101070025d76Sjohnny }
101170025d76Sjohnny 
101270025d76Sjohnny 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)101326947304SEvan Yan ppb_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, int flags,
101426947304SEvan Yan     char *name, caddr_t valuep, int *lengthp)
101570025d76Sjohnny {
101626947304SEvan Yan 	int		instance = PCI_MINOR_NUM_TO_INSTANCE(getminor(dev));
101726947304SEvan Yan 	ppb_devstate_t	*ppb_p = ddi_get_soft_state(ppb_state, instance);
101826947304SEvan Yan 
101926947304SEvan Yan 	if (ppb_p == NULL)
102026947304SEvan Yan 		return (ENXIO);
102126947304SEvan Yan 
102226947304SEvan Yan 	if (ppb_p->parent_bus == PCIE_PCIECAP_DEV_TYPE_PCIE_DEV)
102326947304SEvan Yan 		return (pcie_prop_op(dev, dip, prop_op, flags, name,
102426947304SEvan Yan 		    valuep, lengthp));
102526947304SEvan Yan 
102670025d76Sjohnny 	return ((pcihp_get_cb_ops())->cb_prop_op(dev, dip, prop_op, flags,
102770025d76Sjohnny 	    name, valuep, lengthp));
102870025d76Sjohnny }
102970025d76Sjohnny 
103070025d76Sjohnny static int
ppb_info(dev_info_t * dip,ddi_info_cmd_t cmd,void * arg,void ** result)103170025d76Sjohnny ppb_info(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **result)
103270025d76Sjohnny {
103326947304SEvan Yan 	minor_t		minor = getminor((dev_t)arg);
103426947304SEvan Yan 	int		instance = PCI_MINOR_NUM_TO_INSTANCE(minor);
103526947304SEvan Yan 	ppb_devstate_t	*ppb_p = ddi_get_soft_state(ppb_state, instance);
103626947304SEvan Yan 
103726947304SEvan Yan 	if (ppb_p == NULL)
103826947304SEvan Yan 		return (DDI_FAILURE);
103926947304SEvan Yan 
104026947304SEvan Yan 	if (ppb_p->parent_bus != PCIE_PCIECAP_DEV_TYPE_PCIE_DEV)
104126947304SEvan Yan 		return (pcihp_info(dip, cmd, arg, result));
104226947304SEvan Yan 
104326947304SEvan Yan 	switch (cmd) {
104426947304SEvan Yan 	default:
104526947304SEvan Yan 		return (DDI_FAILURE);
104626947304SEvan Yan 
104726947304SEvan Yan 	case DDI_INFO_DEVT2INSTANCE:
104826947304SEvan Yan 		*result = (void *)(uintptr_t)instance;
104926947304SEvan Yan 		return (DDI_SUCCESS);
105026947304SEvan Yan 
105126947304SEvan Yan 	case DDI_INFO_DEVT2DEVINFO:
105226947304SEvan Yan 		if (ppb_p == NULL)
105326947304SEvan Yan 			return (DDI_FAILURE);
105426947304SEvan Yan 		*result = (void *)ppb_p->dip;
105526947304SEvan Yan 		return (DDI_SUCCESS);
105626947304SEvan Yan 	}
105770025d76Sjohnny }
105800d0963fSdilpreet 
ppb_peekpoke_cb(dev_info_t * dip,ddi_fm_error_t * derr)1059eae2e508Skrishnae void ppb_peekpoke_cb(dev_info_t *dip, ddi_fm_error_t *derr) {
1060eae2e508Skrishnae 	(void) pci_ereport_post(dip, derr, NULL);
1061eae2e508Skrishnae }
1062eae2e508Skrishnae 
106300d0963fSdilpreet /*ARGSUSED*/
106400d0963fSdilpreet static int
ppb_fm_init(dev_info_t * dip,dev_info_t * tdip,int cap,ddi_iblock_cookie_t * ibc)106500d0963fSdilpreet ppb_fm_init(dev_info_t *dip, dev_info_t *tdip, int cap,
106600d0963fSdilpreet     ddi_iblock_cookie_t *ibc)
106700d0963fSdilpreet {
106800d0963fSdilpreet 	ppb_devstate_t  *ppb = ddi_get_soft_state(ppb_state,
106900d0963fSdilpreet 	    ddi_get_instance(dip));
107000d0963fSdilpreet 
107100d0963fSdilpreet 	ASSERT(ibc != NULL);
107200d0963fSdilpreet 	*ibc = ppb->ppb_fm_ibc;
107300d0963fSdilpreet 
107400d0963fSdilpreet 	return (ppb->ppb_fmcap);
107500d0963fSdilpreet }
107600d0963fSdilpreet 
107700d0963fSdilpreet /*ARGSUSED*/
107800d0963fSdilpreet static int
ppb_fm_callback(dev_info_t * dip,ddi_fm_error_t * derr,const void * no_used)107900d0963fSdilpreet ppb_fm_callback(dev_info_t *dip, ddi_fm_error_t *derr, const void *no_used)
108000d0963fSdilpreet {
108100d0963fSdilpreet 	ppb_devstate_t  *ppb = ddi_get_soft_state(ppb_state,
108200d0963fSdilpreet 	    ddi_get_instance(dip));
108300d0963fSdilpreet 
108400d0963fSdilpreet 	mutex_enter(&ppb->ppb_err_mutex);
108500d0963fSdilpreet 	pci_ereport_post(dip, derr, NULL);
108600d0963fSdilpreet 	mutex_exit(&ppb->ppb_err_mutex);
108700d0963fSdilpreet 	return (derr->fme_status);
108800d0963fSdilpreet }
1089