xref: /onnv-gate/usr/src/uts/sun4/io/px/px.c (revision 7596:b89686e7a43b)
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
51540Skini  * Common Development and Distribution License (the "License").
61540Skini  * 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 /*
226313Skrishnae  * Copyright 2008 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  * PCI Express nexus driver interface
280Sstevel@tonic-gate  */
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #include <sys/types.h>
310Sstevel@tonic-gate #include <sys/conf.h>		/* nulldev */
320Sstevel@tonic-gate #include <sys/stat.h>		/* devctl */
330Sstevel@tonic-gate #include <sys/kmem.h>
340Sstevel@tonic-gate #include <sys/sunddi.h>
350Sstevel@tonic-gate #include <sys/sunndi.h>
360Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
370Sstevel@tonic-gate #include <sys/ddi_subrdefs.h>
38118Sjchu #include <sys/spl.h>
390Sstevel@tonic-gate #include <sys/epm.h>
400Sstevel@tonic-gate #include <sys/iommutsb.h>
411531Skini #include <sys/hotplug/pci/pcihp.h>
421531Skini #include <sys/hotplug/pci/pciehpc.h>
430Sstevel@tonic-gate #include "px_obj.h"
44624Sschwartz #include <sys/pci_tools.h>
45777Sschwartz #include "px_tools_ext.h"
460Sstevel@tonic-gate #include "pcie_pwr.h"
470Sstevel@tonic-gate 
480Sstevel@tonic-gate /*LINTLIBRARY*/
490Sstevel@tonic-gate 
500Sstevel@tonic-gate /*
510Sstevel@tonic-gate  * function prototypes for dev ops routines:
520Sstevel@tonic-gate  */
530Sstevel@tonic-gate static int px_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
540Sstevel@tonic-gate static int px_detach(dev_info_t *dip, ddi_detach_cmd_t cmd);
550Sstevel@tonic-gate static int px_info(dev_info_t *dip, ddi_info_cmd_t infocmd,
560Sstevel@tonic-gate 	void *arg, void **result);
571648Sjchu static int px_cb_attach(px_t *);
581648Sjchu static void px_cb_detach(px_t *);
590Sstevel@tonic-gate static int px_pwr_setup(dev_info_t *dip);
600Sstevel@tonic-gate static void px_pwr_teardown(dev_info_t *dip);
610Sstevel@tonic-gate 
62*7596SAlan.Adamson@Sun.COM static void px_set_mps(px_t *px_p);
63*7596SAlan.Adamson@Sun.COM 
64*7596SAlan.Adamson@Sun.COM extern int pcie_max_mps;
65*7596SAlan.Adamson@Sun.COM 
661865Sdilpreet extern errorq_t *pci_target_queue;
671865Sdilpreet 
680Sstevel@tonic-gate /*
691531Skini  * function prototypes for hotplug routines:
701531Skini  */
712840Scarlsonj static int px_init_hotplug(px_t *px_p);
722840Scarlsonj static int px_uninit_hotplug(dev_info_t *dip);
731531Skini 
741531Skini /*
750Sstevel@tonic-gate  * bus ops and dev ops structures:
760Sstevel@tonic-gate  */
770Sstevel@tonic-gate static struct bus_ops px_bus_ops = {
780Sstevel@tonic-gate 	BUSO_REV,
790Sstevel@tonic-gate 	px_map,
800Sstevel@tonic-gate 	0,
810Sstevel@tonic-gate 	0,
820Sstevel@tonic-gate 	0,
830Sstevel@tonic-gate 	i_ddi_map_fault,
840Sstevel@tonic-gate 	px_dma_setup,
850Sstevel@tonic-gate 	px_dma_allochdl,
860Sstevel@tonic-gate 	px_dma_freehdl,
870Sstevel@tonic-gate 	px_dma_bindhdl,
880Sstevel@tonic-gate 	px_dma_unbindhdl,
890Sstevel@tonic-gate 	px_lib_dma_sync,
900Sstevel@tonic-gate 	px_dma_win,
910Sstevel@tonic-gate 	px_dma_ctlops,
920Sstevel@tonic-gate 	px_ctlops,
930Sstevel@tonic-gate 	ddi_bus_prop_op,
940Sstevel@tonic-gate 	ndi_busop_get_eventcookie,
950Sstevel@tonic-gate 	ndi_busop_add_eventcall,
960Sstevel@tonic-gate 	ndi_busop_remove_eventcall,
970Sstevel@tonic-gate 	ndi_post_event,
980Sstevel@tonic-gate 	NULL,
990Sstevel@tonic-gate 	NULL,			/* (*bus_config)(); */
1000Sstevel@tonic-gate 	NULL,			/* (*bus_unconfig)(); */
1010Sstevel@tonic-gate 	px_fm_init_child,	/* (*bus_fm_init)(); */
1020Sstevel@tonic-gate 	NULL,			/* (*bus_fm_fini)(); */
10327Sjchu 	px_bus_enter,		/* (*bus_fm_access_enter)(); */
10427Sjchu 	px_bus_exit,		/* (*bus_fm_access_fini)(); */
1050Sstevel@tonic-gate 	pcie_bus_power,		/* (*bus_power)(); */
1060Sstevel@tonic-gate 	px_intr_ops		/* (*bus_intr_op)(); */
1070Sstevel@tonic-gate };
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate extern struct cb_ops px_cb_ops;
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate static struct dev_ops px_ops = {
1120Sstevel@tonic-gate 	DEVO_REV,
1130Sstevel@tonic-gate 	0,
1140Sstevel@tonic-gate 	px_info,
1150Sstevel@tonic-gate 	nulldev,
1160Sstevel@tonic-gate 	0,
1170Sstevel@tonic-gate 	px_attach,
1180Sstevel@tonic-gate 	px_detach,
1190Sstevel@tonic-gate 	nodev,
1200Sstevel@tonic-gate 	&px_cb_ops,
1210Sstevel@tonic-gate 	&px_bus_ops,
1220Sstevel@tonic-gate 	nulldev
1230Sstevel@tonic-gate };
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate /*
1260Sstevel@tonic-gate  * module definitions:
1270Sstevel@tonic-gate  */
1280Sstevel@tonic-gate #include <sys/modctl.h>
1290Sstevel@tonic-gate extern struct mod_ops mod_driverops;
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate static struct modldrv modldrv = {
1320Sstevel@tonic-gate 	&mod_driverops, 		/* Type of module - driver */
133*7596SAlan.Adamson@Sun.COM 	"PCI Express nexus driver",	/* Name of module. */
1340Sstevel@tonic-gate 	&px_ops,			/* driver ops */
1350Sstevel@tonic-gate };
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate static struct modlinkage modlinkage = {
1380Sstevel@tonic-gate 	MODREV_1, (void *)&modldrv, NULL
1390Sstevel@tonic-gate };
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate /* driver soft state */
1420Sstevel@tonic-gate void *px_state_p;
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate int
1450Sstevel@tonic-gate _init(void)
1460Sstevel@tonic-gate {
1470Sstevel@tonic-gate 	int e;
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate 	/*
1500Sstevel@tonic-gate 	 * Initialize per-px bus soft state pointer.
1510Sstevel@tonic-gate 	 */
1520Sstevel@tonic-gate 	e = ddi_soft_state_init(&px_state_p, sizeof (px_t), 1);
1530Sstevel@tonic-gate 	if (e != DDI_SUCCESS)
1540Sstevel@tonic-gate 		return (e);
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate 	/*
1570Sstevel@tonic-gate 	 * Install the module.
1580Sstevel@tonic-gate 	 */
1590Sstevel@tonic-gate 	e = mod_install(&modlinkage);
1600Sstevel@tonic-gate 	if (e != DDI_SUCCESS)
1610Sstevel@tonic-gate 		ddi_soft_state_fini(&px_state_p);
1620Sstevel@tonic-gate 	return (e);
1630Sstevel@tonic-gate }
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate int
1660Sstevel@tonic-gate _fini(void)
1670Sstevel@tonic-gate {
1680Sstevel@tonic-gate 	int e;
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 	/*
1710Sstevel@tonic-gate 	 * Remove the module.
1720Sstevel@tonic-gate 	 */
1730Sstevel@tonic-gate 	e = mod_remove(&modlinkage);
1740Sstevel@tonic-gate 	if (e != DDI_SUCCESS)
1750Sstevel@tonic-gate 		return (e);
1761865Sdilpreet 	/*
1771865Sdilpreet 	 * Destroy pci_target_queue, and set it to NULL.
1781865Sdilpreet 	 */
1791865Sdilpreet 	if (pci_target_queue)
1801865Sdilpreet 		errorq_destroy(pci_target_queue);
1811865Sdilpreet 
1821865Sdilpreet 	pci_target_queue = NULL;
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate 	/* Free px soft state */
1850Sstevel@tonic-gate 	ddi_soft_state_fini(&px_state_p);
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate 	return (e);
1880Sstevel@tonic-gate }
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate int
1910Sstevel@tonic-gate _info(struct modinfo *modinfop)
1920Sstevel@tonic-gate {
1930Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
1940Sstevel@tonic-gate }
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate /* ARGSUSED */
1970Sstevel@tonic-gate static int
1980Sstevel@tonic-gate px_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
1990Sstevel@tonic-gate {
2000Sstevel@tonic-gate 	int	instance = getminor((dev_t)arg);
2010Sstevel@tonic-gate 	px_t	*px_p = INST_TO_STATE(instance);
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate 	/*
2040Sstevel@tonic-gate 	 * Allow hotplug to deal with ones it manages
2050Sstevel@tonic-gate 	 * Hot Plug will be done later.
2060Sstevel@tonic-gate 	 */
2071531Skini 	if (px_p && (px_p->px_dev_caps & PX_HOTPLUG_CAPABLE))
2080Sstevel@tonic-gate 		return (pcihp_info(dip, infocmd, arg, result));
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 	/* non-hotplug or not attached */
2110Sstevel@tonic-gate 	switch (infocmd) {
2120Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
213671Skrishnae 		*result = (void *)(intptr_t)instance;
2140Sstevel@tonic-gate 		return (DDI_SUCCESS);
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
2170Sstevel@tonic-gate 		if (px_p == NULL)
2180Sstevel@tonic-gate 			return (DDI_FAILURE);
2190Sstevel@tonic-gate 		*result = (void *)px_p->px_dip;
2200Sstevel@tonic-gate 		return (DDI_SUCCESS);
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate 	default:
2230Sstevel@tonic-gate 		return (DDI_FAILURE);
2240Sstevel@tonic-gate 	}
2250Sstevel@tonic-gate }
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate /* device driver entry points */
2280Sstevel@tonic-gate /*
2290Sstevel@tonic-gate  * attach entry point:
2300Sstevel@tonic-gate  */
2310Sstevel@tonic-gate /*ARGSUSED*/
2320Sstevel@tonic-gate static int
2330Sstevel@tonic-gate px_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
2340Sstevel@tonic-gate {
2350Sstevel@tonic-gate 	px_t		*px_p;	/* per bus state pointer */
2360Sstevel@tonic-gate 	int		instance = DIP_TO_INST(dip);
2370Sstevel@tonic-gate 	int		ret = DDI_SUCCESS;
2380Sstevel@tonic-gate 	devhandle_t	dev_hdl = NULL;
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 	switch (cmd) {
2410Sstevel@tonic-gate 	case DDI_ATTACH:
2420Sstevel@tonic-gate 		DBG(DBG_ATTACH, dip, "DDI_ATTACH\n");
2430Sstevel@tonic-gate 
2440Sstevel@tonic-gate 		/*
2450Sstevel@tonic-gate 		 * Allocate and get the per-px soft state structure.
2460Sstevel@tonic-gate 		 */
2470Sstevel@tonic-gate 		if (ddi_soft_state_zalloc(px_state_p, instance)
2480Sstevel@tonic-gate 		    != DDI_SUCCESS) {
2490Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s%d: can't allocate px state",
2506313Skrishnae 			    ddi_driver_name(dip), instance);
2510Sstevel@tonic-gate 			goto err_bad_px_softstate;
2520Sstevel@tonic-gate 		}
2530Sstevel@tonic-gate 		px_p = INST_TO_STATE(instance);
2540Sstevel@tonic-gate 		px_p->px_dip = dip;
2550Sstevel@tonic-gate 		mutex_init(&px_p->px_mutex, NULL, MUTEX_DRIVER, NULL);
2560Sstevel@tonic-gate 		px_p->px_soft_state = PX_SOFT_STATE_CLOSED;
2570Sstevel@tonic-gate 		px_p->px_open_count = 0;
2580Sstevel@tonic-gate 
2591531Skini 		(void) ddi_prop_update_string(DDI_DEV_T_NONE, dip,
2606313Skrishnae 		    "device_type", "pciex");
2613274Set142600 
2623274Set142600 		/* Initialize px_dbg for high pil printing */
2633274Set142600 		px_dbg_attach(dip, &px_p->px_dbg_hdl);
2643274Set142600 
2650Sstevel@tonic-gate 		/*
2660Sstevel@tonic-gate 		 * Get key properties of the pci bridge node and
2670Sstevel@tonic-gate 		 * determine it's type (psycho, schizo, etc ...).
2680Sstevel@tonic-gate 		 */
2690Sstevel@tonic-gate 		if (px_get_props(px_p, dip) == DDI_FAILURE)
2700Sstevel@tonic-gate 			goto err_bad_px_prop;
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate 		if (px_lib_dev_init(dip, &dev_hdl) != DDI_SUCCESS)
2730Sstevel@tonic-gate 			goto err_bad_dev_init;
2740Sstevel@tonic-gate 
2751725Segillett 		/* Initialize device handle */
2760Sstevel@tonic-gate 		px_p->px_dev_hdl = dev_hdl;
2770Sstevel@tonic-gate 
2783613Set142600 		/* Cache the BDF of the root port nexus */
2793613Set142600 		px_p->px_bdf = px_lib_get_bdf(px_p);
2803613Set142600 
2810Sstevel@tonic-gate 		/*
2820Sstevel@tonic-gate 		 * Initialize interrupt block.  Note that this
2830Sstevel@tonic-gate 		 * initialize error handling for the PEC as well.
2840Sstevel@tonic-gate 		 */
2850Sstevel@tonic-gate 		if ((ret = px_ib_attach(px_p)) != DDI_SUCCESS)
2860Sstevel@tonic-gate 			goto err_bad_ib;
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate 		if (px_cb_attach(px_p) != DDI_SUCCESS)
2890Sstevel@tonic-gate 			goto err_bad_cb;
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate 		/*
2920Sstevel@tonic-gate 		 * Start creating the modules.
2930Sstevel@tonic-gate 		 * Note that attach() routines should
2940Sstevel@tonic-gate 		 * register and enable their own interrupts.
2950Sstevel@tonic-gate 		 */
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate 		if ((px_mmu_attach(px_p)) != DDI_SUCCESS)
2980Sstevel@tonic-gate 			goto err_bad_mmu;
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate 		if ((px_msiq_attach(px_p)) != DDI_SUCCESS)
3010Sstevel@tonic-gate 			goto err_bad_msiq;
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate 		if ((px_msi_attach(px_p)) != DDI_SUCCESS)
3040Sstevel@tonic-gate 			goto err_bad_msi;
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate 		if ((px_pec_attach(px_p)) != DDI_SUCCESS)
3070Sstevel@tonic-gate 			goto err_bad_pec;
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate 		if ((px_dma_attach(px_p)) != DDI_SUCCESS)
3102510Sjchu 			goto err_bad_dma; /* nothing to uninitialize on DMA */
3110Sstevel@tonic-gate 
3122587Spjha 		if ((px_fm_attach(px_p)) != DDI_SUCCESS)
3132587Spjha 			goto err_bad_dma;
3142587Spjha 
3150Sstevel@tonic-gate 		/*
3160Sstevel@tonic-gate 		 * All of the error handlers have been registered
3170Sstevel@tonic-gate 		 * by now so it's time to activate the interrupt.
3180Sstevel@tonic-gate 		 */
31927Sjchu 		if ((ret = px_err_add_intr(&px_p->px_fault)) != DDI_SUCCESS)
3202587Spjha 			goto err_bad_intr;
3210Sstevel@tonic-gate 
3221531Skini 		(void) px_init_hotplug(px_p);
3231531Skini 
324*7596SAlan.Adamson@Sun.COM 		(void) px_set_mps(px_p);
325*7596SAlan.Adamson@Sun.COM 
3260Sstevel@tonic-gate 		/*
3270Sstevel@tonic-gate 		 * Create the "devctl" node for hotplug and pcitool support.
3280Sstevel@tonic-gate 		 * For non-hotplug bus, we still need ":devctl" to
3290Sstevel@tonic-gate 		 * support DEVCTL_DEVICE_* and DEVCTL_BUS_* ioctls.
3300Sstevel@tonic-gate 		 */
3310Sstevel@tonic-gate 		if (ddi_create_minor_node(dip, "devctl", S_IFCHR,
3320Sstevel@tonic-gate 		    PCIHP_AP_MINOR_NUM(instance, PCIHP_DEVCTL_MINOR),
3330Sstevel@tonic-gate 		    DDI_NT_NEXUS, 0) != DDI_SUCCESS) {
3340Sstevel@tonic-gate 			goto err_bad_devctl_node;
3350Sstevel@tonic-gate 		}
336624Sschwartz 
337624Sschwartz 		if (pxtool_init(dip) != DDI_SUCCESS)
338624Sschwartz 			goto err_bad_pcitool_node;
339624Sschwartz 
3400Sstevel@tonic-gate 		/*
3410Sstevel@tonic-gate 		 * power management setup. Even if it fails, attach will
3420Sstevel@tonic-gate 		 * succeed as this is a optional feature. Since we are
3430Sstevel@tonic-gate 		 * always at full power, this is not critical.
3440Sstevel@tonic-gate 		 */
3450Sstevel@tonic-gate 		if (pwr_common_setup(dip) != DDI_SUCCESS) {
3460Sstevel@tonic-gate 			DBG(DBG_PWR, dip, "pwr_common_setup failed\n");
3470Sstevel@tonic-gate 		} else if (px_pwr_setup(dip) != DDI_SUCCESS) {
3480Sstevel@tonic-gate 			DBG(DBG_PWR, dip, "px_pwr_setup failed \n");
3490Sstevel@tonic-gate 			pwr_common_teardown(dip);
3500Sstevel@tonic-gate 		}
3510Sstevel@tonic-gate 
352435Sjchu 		/*
353435Sjchu 		 * add cpr callback
354435Sjchu 		 */
355435Sjchu 		px_cpr_add_callb(px_p);
356435Sjchu 
3570Sstevel@tonic-gate 		ddi_report_dev(dip);
3580Sstevel@tonic-gate 
3590Sstevel@tonic-gate 		px_p->px_state = PX_ATTACHED;
3600Sstevel@tonic-gate 		DBG(DBG_ATTACH, dip, "attach success\n");
3610Sstevel@tonic-gate 		break;
3620Sstevel@tonic-gate 
363624Sschwartz err_bad_pcitool_node:
364624Sschwartz 		ddi_remove_minor_node(dip, "devctl");
3650Sstevel@tonic-gate err_bad_devctl_node:
3662587Spjha 		px_err_rem_intr(&px_p->px_fault);
3672587Spjha err_bad_intr:
3682510Sjchu 		px_fm_detach(px_p);
3692510Sjchu err_bad_dma:
3700Sstevel@tonic-gate 		px_pec_detach(px_p);
3710Sstevel@tonic-gate err_bad_pec:
3720Sstevel@tonic-gate 		px_msi_detach(px_p);
3730Sstevel@tonic-gate err_bad_msi:
3740Sstevel@tonic-gate 		px_msiq_detach(px_p);
3750Sstevel@tonic-gate err_bad_msiq:
3760Sstevel@tonic-gate 		px_mmu_detach(px_p);
3770Sstevel@tonic-gate err_bad_mmu:
3780Sstevel@tonic-gate 		px_cb_detach(px_p);
3790Sstevel@tonic-gate err_bad_cb:
3800Sstevel@tonic-gate 		px_ib_detach(px_p);
3810Sstevel@tonic-gate err_bad_ib:
3827124Sanbui 		if (px_lib_dev_fini(dip) != DDI_SUCCESS) {
3837124Sanbui 			DBG(DBG_ATTACH, dip, "px_lib_dev_fini failed\n");
3847124Sanbui 		}
3850Sstevel@tonic-gate err_bad_dev_init:
3860Sstevel@tonic-gate 		px_free_props(px_p);
3870Sstevel@tonic-gate err_bad_px_prop:
3883274Set142600 		px_dbg_detach(dip, &px_p->px_dbg_hdl);
3890Sstevel@tonic-gate 		mutex_destroy(&px_p->px_mutex);
3900Sstevel@tonic-gate 		ddi_soft_state_free(px_state_p, instance);
3910Sstevel@tonic-gate err_bad_px_softstate:
3920Sstevel@tonic-gate 		ret = DDI_FAILURE;
3930Sstevel@tonic-gate 		break;
3940Sstevel@tonic-gate 
3950Sstevel@tonic-gate 	case DDI_RESUME:
3960Sstevel@tonic-gate 		DBG(DBG_ATTACH, dip, "DDI_RESUME\n");
3970Sstevel@tonic-gate 
3980Sstevel@tonic-gate 		px_p = INST_TO_STATE(instance);
3990Sstevel@tonic-gate 
4000Sstevel@tonic-gate 		mutex_enter(&px_p->px_mutex);
4010Sstevel@tonic-gate 
4020Sstevel@tonic-gate 		/* suspend might have not succeeded */
4030Sstevel@tonic-gate 		if (px_p->px_state != PX_SUSPENDED) {
4040Sstevel@tonic-gate 			DBG(DBG_ATTACH, px_p->px_dip,
4050Sstevel@tonic-gate 			    "instance NOT suspended\n");
4060Sstevel@tonic-gate 			ret = DDI_FAILURE;
4070Sstevel@tonic-gate 			break;
4080Sstevel@tonic-gate 		}
4090Sstevel@tonic-gate 
4102588Segillett 		px_msiq_resume(px_p);
4110Sstevel@tonic-gate 		px_lib_resume(dip);
4120Sstevel@tonic-gate 		(void) pcie_pwr_resume(dip);
4130Sstevel@tonic-gate 		px_p->px_state = PX_ATTACHED;
4140Sstevel@tonic-gate 
4150Sstevel@tonic-gate 		mutex_exit(&px_p->px_mutex);
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate 		break;
4180Sstevel@tonic-gate 	default:
4190Sstevel@tonic-gate 		DBG(DBG_ATTACH, dip, "unsupported attach op\n");
4200Sstevel@tonic-gate 		ret = DDI_FAILURE;
4210Sstevel@tonic-gate 		break;
4220Sstevel@tonic-gate 	}
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate 	return (ret);
4250Sstevel@tonic-gate }
4260Sstevel@tonic-gate 
4270Sstevel@tonic-gate /*
4280Sstevel@tonic-gate  * detach entry point:
4290Sstevel@tonic-gate  */
4300Sstevel@tonic-gate /*ARGSUSED*/
4310Sstevel@tonic-gate static int
4320Sstevel@tonic-gate px_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
4330Sstevel@tonic-gate {
4340Sstevel@tonic-gate 	int instance = ddi_get_instance(dip);
4350Sstevel@tonic-gate 	px_t *px_p = INST_TO_STATE(instance);
4360Sstevel@tonic-gate 	int ret;
4370Sstevel@tonic-gate 
4380Sstevel@tonic-gate 	/*
4390Sstevel@tonic-gate 	 * Make sure we are currently attached
4400Sstevel@tonic-gate 	 */
4410Sstevel@tonic-gate 	if (px_p->px_state != PX_ATTACHED) {
4421648Sjchu 		DBG(DBG_DETACH, dip, "Instance not attached\n");
4430Sstevel@tonic-gate 		return (DDI_FAILURE);
4440Sstevel@tonic-gate 	}
4450Sstevel@tonic-gate 
4460Sstevel@tonic-gate 	mutex_enter(&px_p->px_mutex);
4470Sstevel@tonic-gate 
4480Sstevel@tonic-gate 	switch (cmd) {
4490Sstevel@tonic-gate 	case DDI_DETACH:
4500Sstevel@tonic-gate 		DBG(DBG_DETACH, dip, "DDI_DETACH\n");
4510Sstevel@tonic-gate 
452435Sjchu 		/*
453435Sjchu 		 * remove cpr callback
454435Sjchu 		 */
455435Sjchu 		px_cpr_rem_callb(px_p);
456435Sjchu 
4571531Skini 		if (px_p->px_dev_caps & PX_HOTPLUG_CAPABLE)
4581531Skini 			if (px_uninit_hotplug(dip) != DDI_SUCCESS) {
4590Sstevel@tonic-gate 				mutex_exit(&px_p->px_mutex);
4600Sstevel@tonic-gate 				return (DDI_FAILURE);
4610Sstevel@tonic-gate 			}
4620Sstevel@tonic-gate 
4630Sstevel@tonic-gate 		/*
4640Sstevel@tonic-gate 		 * things which used to be done in obj_destroy
4650Sstevel@tonic-gate 		 * are now in-lined here.
4660Sstevel@tonic-gate 		 */
4670Sstevel@tonic-gate 
4680Sstevel@tonic-gate 		px_p->px_state = PX_DETACHED;
4690Sstevel@tonic-gate 
470624Sschwartz 		pxtool_uninit(dip);
471624Sschwartz 
4720Sstevel@tonic-gate 		ddi_remove_minor_node(dip, "devctl");
4732587Spjha 		px_err_rem_intr(&px_p->px_fault);
4742510Sjchu 		px_fm_detach(px_p);
4750Sstevel@tonic-gate 		px_pec_detach(px_p);
476965Sgovinda 		px_pwr_teardown(dip);
477965Sgovinda 		pwr_common_teardown(dip);
4780Sstevel@tonic-gate 		px_msi_detach(px_p);
4790Sstevel@tonic-gate 		px_msiq_detach(px_p);
4800Sstevel@tonic-gate 		px_mmu_detach(px_p);
4810Sstevel@tonic-gate 		px_cb_detach(px_p);
4820Sstevel@tonic-gate 		px_ib_detach(px_p);
4837124Sanbui 		if (px_lib_dev_fini(dip) != DDI_SUCCESS) {
4847124Sanbui 			DBG(DBG_DETACH, dip, "px_lib_dev_fini failed\n");
4857124Sanbui 		}
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate 		/*
4880Sstevel@tonic-gate 		 * Free the px soft state structure and the rest of the
4890Sstevel@tonic-gate 		 * resources it's using.
4900Sstevel@tonic-gate 		 */
4910Sstevel@tonic-gate 		px_free_props(px_p);
4923274Set142600 		px_dbg_detach(dip, &px_p->px_dbg_hdl);
4930Sstevel@tonic-gate 		mutex_exit(&px_p->px_mutex);
4940Sstevel@tonic-gate 		mutex_destroy(&px_p->px_mutex);
4950Sstevel@tonic-gate 
4966313Skrishnae 		/* Free the interrupt-priorities prop if we created it. */
4976313Skrishnae 		{
4980Sstevel@tonic-gate 			int len;
4990Sstevel@tonic-gate 
5000Sstevel@tonic-gate 			if (ddi_getproplen(DDI_DEV_T_ANY, dip,
5010Sstevel@tonic-gate 			    DDI_PROP_NOTPROM | DDI_PROP_DONTPASS,
5020Sstevel@tonic-gate 			    "interrupt-priorities", &len) == DDI_PROP_SUCCESS)
5030Sstevel@tonic-gate 				(void) ddi_prop_remove(DDI_DEV_T_NONE, dip,
5040Sstevel@tonic-gate 				    "interrupt-priorities");
5050Sstevel@tonic-gate 		}
5060Sstevel@tonic-gate 
5070Sstevel@tonic-gate 		px_p->px_dev_hdl = NULL;
5081064Sschwartz 		ddi_soft_state_free(px_state_p, instance);
5090Sstevel@tonic-gate 
5100Sstevel@tonic-gate 		return (DDI_SUCCESS);
5110Sstevel@tonic-gate 
5120Sstevel@tonic-gate 	case DDI_SUSPEND:
5130Sstevel@tonic-gate 		if (pcie_pwr_suspend(dip) != DDI_SUCCESS) {
5140Sstevel@tonic-gate 			mutex_exit(&px_p->px_mutex);
5150Sstevel@tonic-gate 			return (DDI_FAILURE);
5160Sstevel@tonic-gate 		}
5170Sstevel@tonic-gate 		if ((ret = px_lib_suspend(dip)) == DDI_SUCCESS)
5180Sstevel@tonic-gate 			px_p->px_state = PX_SUSPENDED;
5190Sstevel@tonic-gate 		mutex_exit(&px_p->px_mutex);
5200Sstevel@tonic-gate 
5210Sstevel@tonic-gate 		return (ret);
5220Sstevel@tonic-gate 
5230Sstevel@tonic-gate 	default:
5240Sstevel@tonic-gate 		DBG(DBG_DETACH, dip, "unsupported detach op\n");
5250Sstevel@tonic-gate 		mutex_exit(&px_p->px_mutex);
5260Sstevel@tonic-gate 		return (DDI_FAILURE);
5270Sstevel@tonic-gate 	}
5280Sstevel@tonic-gate }
5290Sstevel@tonic-gate 
5301648Sjchu int
5311648Sjchu px_cb_attach(px_t *px_p)
5321648Sjchu {
5331648Sjchu 	px_fault_t	*fault_p = &px_p->px_cb_fault;
5341648Sjchu 	dev_info_t	*dip = px_p->px_dip;
5351648Sjchu 	sysino_t	sysino;
5361648Sjchu 
5371648Sjchu 	if (px_lib_intr_devino_to_sysino(dip,
5381648Sjchu 	    px_p->px_inos[PX_INTR_XBC], &sysino) != DDI_SUCCESS)
5391648Sjchu 		return (DDI_FAILURE);
5401648Sjchu 
5411648Sjchu 	fault_p->px_fh_dip = dip;
5421648Sjchu 	fault_p->px_fh_sysino = sysino;
5431648Sjchu 	fault_p->px_err_func = px_err_cb_intr;
5441648Sjchu 	fault_p->px_intr_ino = px_p->px_inos[PX_INTR_XBC];
5451648Sjchu 
5461648Sjchu 	return (px_cb_add_intr(fault_p));
5471648Sjchu }
5481648Sjchu 
5491648Sjchu void
5501648Sjchu px_cb_detach(px_t *px_p)
5511648Sjchu {
5521648Sjchu 	px_cb_rem_intr(&px_p->px_cb_fault);
5531648Sjchu }
5541648Sjchu 
5550Sstevel@tonic-gate /*
5560Sstevel@tonic-gate  * power management related initialization specific to px
5570Sstevel@tonic-gate  * called by px_attach()
5580Sstevel@tonic-gate  */
5590Sstevel@tonic-gate static int
5600Sstevel@tonic-gate px_pwr_setup(dev_info_t *dip)
5610Sstevel@tonic-gate {
5620Sstevel@tonic-gate 	pcie_pwr_t *pwr_p;
563118Sjchu 	int instance = ddi_get_instance(dip);
564118Sjchu 	px_t *px_p = INST_TO_STATE(instance);
5650Sstevel@tonic-gate 	ddi_intr_handle_impl_t hdl;
5660Sstevel@tonic-gate 
5670Sstevel@tonic-gate 	ASSERT(PCIE_PMINFO(dip));
5680Sstevel@tonic-gate 	pwr_p = PCIE_NEXUS_PMINFO(dip);
5690Sstevel@tonic-gate 	ASSERT(pwr_p);
5700Sstevel@tonic-gate 
5710Sstevel@tonic-gate 	/*
5720Sstevel@tonic-gate 	 * indicate support LDI (Layered Driver Interface)
5730Sstevel@tonic-gate 	 * Create the property, if it is not already there
5740Sstevel@tonic-gate 	 */
5750Sstevel@tonic-gate 	if (!ddi_prop_exists(DDI_DEV_T_NONE, dip, DDI_PROP_DONTPASS,
5760Sstevel@tonic-gate 	    DDI_KERNEL_IOCTL)) {
5770Sstevel@tonic-gate 		if (ddi_prop_create(DDI_DEV_T_NONE, dip, DDI_PROP_CANSLEEP,
5780Sstevel@tonic-gate 		    DDI_KERNEL_IOCTL, NULL, 0) != DDI_PROP_SUCCESS) {
5790Sstevel@tonic-gate 			DBG(DBG_PWR, dip, "can't create kernel ioctl prop\n");
5800Sstevel@tonic-gate 			return (DDI_FAILURE);
5810Sstevel@tonic-gate 		}
5820Sstevel@tonic-gate 	}
5830Sstevel@tonic-gate 	/* No support for device PM. We are always at full power */
5840Sstevel@tonic-gate 	pwr_p->pwr_func_lvl = PM_LEVEL_D0;
5850Sstevel@tonic-gate 
586118Sjchu 	mutex_init(&px_p->px_l23ready_lock, NULL, MUTEX_DRIVER,
587693Sgovinda 	    DDI_INTR_PRI(px_pwr_pil));
588118Sjchu 	cv_init(&px_p->px_l23ready_cv, NULL, CV_DRIVER, NULL);
589118Sjchu 
5901725Segillett 	/* Initialize handle */
5911725Segillett 	bzero(&hdl, sizeof (ddi_intr_handle_impl_t));
592118Sjchu 	hdl.ih_cb_arg1 = px_p;
5930Sstevel@tonic-gate 	hdl.ih_ver = DDI_INTR_VERSION;
5940Sstevel@tonic-gate 	hdl.ih_state = DDI_IHDL_STATE_ALLOC;
5950Sstevel@tonic-gate 	hdl.ih_dip = dip;
5960Sstevel@tonic-gate 	hdl.ih_pri = px_pwr_pil;
5970Sstevel@tonic-gate 
5980Sstevel@tonic-gate 	/* Add PME_TO_ACK message handler */
599118Sjchu 	hdl.ih_cb_func = (ddi_intr_handler_t *)px_pmeq_intr;
6000Sstevel@tonic-gate 	if (px_add_msiq_intr(dip, dip, &hdl, MSG_REC,
601118Sjchu 	    (msgcode_t)PCIE_PME_ACK_MSG, &px_p->px_pm_msiq_id) != DDI_SUCCESS) {
602118Sjchu 		DBG(DBG_PWR, dip, "px_pwr_setup: couldn't add "
603118Sjchu 		    " PME_TO_ACK intr\n");
6041147Sjchu 		goto pwr_setup_err1;
6050Sstevel@tonic-gate 	}
606118Sjchu 	px_lib_msg_setmsiq(dip, PCIE_PME_ACK_MSG, px_p->px_pm_msiq_id);
6070Sstevel@tonic-gate 	px_lib_msg_setvalid(dip, PCIE_PME_ACK_MSG, PCIE_MSG_VALID);
6080Sstevel@tonic-gate 
609909Segillett 	if (px_ib_update_intr_state(px_p, px_p->px_dip, hdl.ih_inum,
6102973Sgovinda 	    px_msiqid_to_devino(px_p, px_p->px_pm_msiq_id), px_pwr_pil,
611909Segillett 	    PX_INTR_STATE_ENABLE, MSG_REC, PCIE_PME_ACK_MSG) != DDI_SUCCESS) {
612909Segillett 		DBG(DBG_PWR, dip, "px_pwr_setup: PME_TO_ACK update interrupt"
613909Segillett 		    " state failed\n");
614909Segillett 		goto px_pwrsetup_err_state;
615909Segillett 	}
616909Segillett 
6170Sstevel@tonic-gate 	return (DDI_SUCCESS);
6180Sstevel@tonic-gate 
619909Segillett px_pwrsetup_err_state:
620909Segillett 	px_lib_msg_setvalid(dip, PCIE_PME_ACK_MSG, PCIE_MSG_INVALID);
621909Segillett 	(void) px_rem_msiq_intr(dip, dip, &hdl, MSG_REC, PCIE_PME_ACK_MSG,
622909Segillett 	    px_p->px_pm_msiq_id);
623118Sjchu pwr_setup_err1:
624118Sjchu 	mutex_destroy(&px_p->px_l23ready_lock);
625118Sjchu 	cv_destroy(&px_p->px_l23ready_cv);
626118Sjchu 
6270Sstevel@tonic-gate 	return (DDI_FAILURE);
6280Sstevel@tonic-gate }
6290Sstevel@tonic-gate 
6300Sstevel@tonic-gate /*
6310Sstevel@tonic-gate  * undo whatever is done in px_pwr_setup. called by px_detach()
6320Sstevel@tonic-gate  */
6330Sstevel@tonic-gate static void
6340Sstevel@tonic-gate px_pwr_teardown(dev_info_t *dip)
6350Sstevel@tonic-gate {
636118Sjchu 	int instance = ddi_get_instance(dip);
637118Sjchu 	px_t *px_p = INST_TO_STATE(instance);
638118Sjchu 	ddi_intr_handle_impl_t	hdl;
6390Sstevel@tonic-gate 
640118Sjchu 	if (!PCIE_PMINFO(dip) || !PCIE_NEXUS_PMINFO(dip))
6410Sstevel@tonic-gate 		return;
6420Sstevel@tonic-gate 
6431725Segillett 	/* Initialize handle */
6441725Segillett 	bzero(&hdl, sizeof (ddi_intr_handle_impl_t));
6450Sstevel@tonic-gate 	hdl.ih_ver = DDI_INTR_VERSION;
6460Sstevel@tonic-gate 	hdl.ih_state = DDI_IHDL_STATE_ALLOC;
6470Sstevel@tonic-gate 	hdl.ih_dip = dip;
6483162Sgovinda 	hdl.ih_pri = px_pwr_pil;
6490Sstevel@tonic-gate 
6500Sstevel@tonic-gate 	px_lib_msg_setvalid(dip, PCIE_PME_ACK_MSG, PCIE_MSG_INVALID);
6510Sstevel@tonic-gate 	(void) px_rem_msiq_intr(dip, dip, &hdl, MSG_REC, PCIE_PME_ACK_MSG,
652118Sjchu 	    px_p->px_pm_msiq_id);
653118Sjchu 
654909Segillett 	(void) px_ib_update_intr_state(px_p, px_p->px_dip, hdl.ih_inum,
6552973Sgovinda 	    px_msiqid_to_devino(px_p, px_p->px_pm_msiq_id), px_pwr_pil,
656909Segillett 	    PX_INTR_STATE_DISABLE, MSG_REC, PCIE_PME_ACK_MSG);
657909Segillett 
6582840Scarlsonj 	px_p->px_pm_msiq_id = (msiqid_t)-1;
6590Sstevel@tonic-gate 
660118Sjchu 	cv_destroy(&px_p->px_l23ready_cv);
661118Sjchu 	mutex_destroy(&px_p->px_l23ready_lock);
6620Sstevel@tonic-gate }
6630Sstevel@tonic-gate 
6640Sstevel@tonic-gate /* bus driver entry points */
6650Sstevel@tonic-gate 
6660Sstevel@tonic-gate /*
6670Sstevel@tonic-gate  * bus map entry point:
6680Sstevel@tonic-gate  *
6690Sstevel@tonic-gate  * 	if map request is for an rnumber
6700Sstevel@tonic-gate  *		get the corresponding regspec from device node
6710Sstevel@tonic-gate  * 	build a new regspec in our parent's format
6720Sstevel@tonic-gate  *	build a new map_req with the new regspec
6730Sstevel@tonic-gate  *	call up the tree to complete the mapping
6740Sstevel@tonic-gate  */
6750Sstevel@tonic-gate int
6760Sstevel@tonic-gate px_map(dev_info_t *dip, dev_info_t *rdip, ddi_map_req_t *mp,
6770Sstevel@tonic-gate 	off_t off, off_t len, caddr_t *addrp)
6780Sstevel@tonic-gate {
6790Sstevel@tonic-gate 	px_t *px_p = DIP_TO_STATE(dip);
6800Sstevel@tonic-gate 	struct regspec p_regspec;
6810Sstevel@tonic-gate 	ddi_map_req_t p_mapreq;
6820Sstevel@tonic-gate 	int reglen, rval, r_no;
6830Sstevel@tonic-gate 	pci_regspec_t reloc_reg, *rp = &reloc_reg;
6840Sstevel@tonic-gate 
6850Sstevel@tonic-gate 	DBG(DBG_MAP, dip, "rdip=%s%d:",
6866313Skrishnae 	    ddi_driver_name(rdip), ddi_get_instance(rdip));
6870Sstevel@tonic-gate 
6880Sstevel@tonic-gate 	if (mp->map_flags & DDI_MF_USER_MAPPING)
6890Sstevel@tonic-gate 		return (DDI_ME_UNIMPLEMENTED);
6900Sstevel@tonic-gate 
6910Sstevel@tonic-gate 	switch (mp->map_type) {
6920Sstevel@tonic-gate 	case DDI_MT_REGSPEC:
6930Sstevel@tonic-gate 		reloc_reg = *(pci_regspec_t *)mp->map_obj.rp;	/* dup whole */
6940Sstevel@tonic-gate 		break;
6950Sstevel@tonic-gate 
6960Sstevel@tonic-gate 	case DDI_MT_RNUMBER:
6970Sstevel@tonic-gate 		r_no = mp->map_obj.rnumber;
6980Sstevel@tonic-gate 		DBG(DBG_MAP | DBG_CONT, dip, " r#=%x", r_no);
6990Sstevel@tonic-gate 
700506Scth 		if (ddi_getlongprop(DDI_DEV_T_ANY, rdip, DDI_PROP_DONTPASS,
7016313Skrishnae 		    "reg", (caddr_t)&rp, &reglen) != DDI_SUCCESS)
7026313Skrishnae 			return (DDI_ME_RNUMBER_RANGE);
7030Sstevel@tonic-gate 
7040Sstevel@tonic-gate 		if (r_no < 0 || r_no >= reglen / sizeof (pci_regspec_t)) {
7050Sstevel@tonic-gate 			kmem_free(rp, reglen);
7060Sstevel@tonic-gate 			return (DDI_ME_RNUMBER_RANGE);
7070Sstevel@tonic-gate 		}
7080Sstevel@tonic-gate 		rp += r_no;
7090Sstevel@tonic-gate 		break;
7100Sstevel@tonic-gate 
7110Sstevel@tonic-gate 	default:
7120Sstevel@tonic-gate 		return (DDI_ME_INVAL);
7130Sstevel@tonic-gate 	}
7140Sstevel@tonic-gate 	DBG(DBG_MAP | DBG_CONT, dip, "\n");
7150Sstevel@tonic-gate 
7160Sstevel@tonic-gate 	if ((rp->pci_phys_hi & PCI_REG_ADDR_M) == PCI_ADDR_CONFIG) {
7170Sstevel@tonic-gate 		/*
7180Sstevel@tonic-gate 		 * There may be a need to differentiate between PCI
7190Sstevel@tonic-gate 		 * and PCI-Ex devices so the following range check is
7200Sstevel@tonic-gate 		 * done correctly, depending on the implementation of
7210Sstevel@tonic-gate 		 * px_pci bridge nexus driver.
7220Sstevel@tonic-gate 		 */
7230Sstevel@tonic-gate 		if ((off >= PCIE_CONF_HDR_SIZE) ||
7246313Skrishnae 		    (len > PCIE_CONF_HDR_SIZE) ||
7256313Skrishnae 		    (off + len > PCIE_CONF_HDR_SIZE))
7260Sstevel@tonic-gate 			return (DDI_ME_INVAL);
7270Sstevel@tonic-gate 		/*
7280Sstevel@tonic-gate 		 * the following function returning a DDI_FAILURE assumes
7290Sstevel@tonic-gate 		 * that there are no virtual config space access services
7300Sstevel@tonic-gate 		 * defined in this layer. Otherwise it is availed right
7310Sstevel@tonic-gate 		 * here and we return.
7320Sstevel@tonic-gate 		 */
7330Sstevel@tonic-gate 		rval = px_lib_map_vconfig(dip, mp, off, rp, addrp);
7340Sstevel@tonic-gate 		if (rval == DDI_SUCCESS)
7350Sstevel@tonic-gate 			goto done;
7360Sstevel@tonic-gate 	}
7370Sstevel@tonic-gate 
7380Sstevel@tonic-gate 	/*
7390Sstevel@tonic-gate 	 * No virtual config space services or we are mapping
7400Sstevel@tonic-gate 	 * a region of memory mapped config/IO/memory space, so proceed
7410Sstevel@tonic-gate 	 * to the parent.
7420Sstevel@tonic-gate 	 */
7430Sstevel@tonic-gate 
7440Sstevel@tonic-gate 	/* relocate within 64-bit pci space through "assigned-addresses" */
7450Sstevel@tonic-gate 	if (rval = px_reloc_reg(dip, rdip, px_p, rp))
7460Sstevel@tonic-gate 		goto done;
7470Sstevel@tonic-gate 
7480Sstevel@tonic-gate 	if (len)	/* adjust regspec according to mapping request */
7490Sstevel@tonic-gate 		rp->pci_size_low = len;	/* MIN ? */
7500Sstevel@tonic-gate 	rp->pci_phys_low += off;
7510Sstevel@tonic-gate 
7520Sstevel@tonic-gate 	/* translate relocated pci regspec into parent space through "ranges" */
7530Sstevel@tonic-gate 	if (rval = px_xlate_reg(px_p, rp, &p_regspec))
7540Sstevel@tonic-gate 		goto done;
7550Sstevel@tonic-gate 
7560Sstevel@tonic-gate 	p_mapreq = *mp;		/* dup the whole structure */
7570Sstevel@tonic-gate 	p_mapreq.map_type = DDI_MT_REGSPEC;
7580Sstevel@tonic-gate 	p_mapreq.map_obj.rp = &p_regspec;
759677Sjchu 	px_lib_map_attr_check(&p_mapreq);
7600Sstevel@tonic-gate 	rval = ddi_map(dip, &p_mapreq, 0, 0, addrp);
7610Sstevel@tonic-gate 
7620Sstevel@tonic-gate 	if (rval == DDI_SUCCESS) {
7630Sstevel@tonic-gate 		/*
7640Sstevel@tonic-gate 		 * Set-up access functions for FM access error capable drivers.
7650Sstevel@tonic-gate 		 */
7666313Skrishnae 		if (DDI_FM_ACC_ERR_CAP(ddi_fm_capable(rdip)))
7676313Skrishnae 			px_fm_acc_setup(mp, rdip, rp);
7680Sstevel@tonic-gate 	}
7690Sstevel@tonic-gate 
7700Sstevel@tonic-gate done:
7710Sstevel@tonic-gate 	if (mp->map_type == DDI_MT_RNUMBER)
7720Sstevel@tonic-gate 		kmem_free(rp - r_no, reglen);
7730Sstevel@tonic-gate 
7740Sstevel@tonic-gate 	return (rval);
7750Sstevel@tonic-gate }
7760Sstevel@tonic-gate 
7770Sstevel@tonic-gate /*
7780Sstevel@tonic-gate  * bus dma map entry point
7790Sstevel@tonic-gate  * return value:
7800Sstevel@tonic-gate  *	DDI_DMA_PARTIAL_MAP	 1
7810Sstevel@tonic-gate  *	DDI_DMA_MAPOK		 0
7820Sstevel@tonic-gate  *	DDI_DMA_MAPPED		 0
7830Sstevel@tonic-gate  *	DDI_DMA_NORESOURCES	-1
7840Sstevel@tonic-gate  *	DDI_DMA_NOMAPPING	-2
7850Sstevel@tonic-gate  *	DDI_DMA_TOOBIG		-3
7860Sstevel@tonic-gate  */
7870Sstevel@tonic-gate int
7880Sstevel@tonic-gate px_dma_setup(dev_info_t *dip, dev_info_t *rdip, ddi_dma_req_t *dmareq,
7890Sstevel@tonic-gate 	ddi_dma_handle_t *handlep)
7900Sstevel@tonic-gate {
7910Sstevel@tonic-gate 	px_t *px_p = DIP_TO_STATE(dip);
7920Sstevel@tonic-gate 	px_mmu_t *mmu_p = px_p->px_mmu_p;
7930Sstevel@tonic-gate 	ddi_dma_impl_t *mp;
7940Sstevel@tonic-gate 	int ret;
7950Sstevel@tonic-gate 
7960Sstevel@tonic-gate 	DBG(DBG_DMA_MAP, dip, "mapping - rdip=%s%d type=%s\n",
7976313Skrishnae 	    ddi_driver_name(rdip), ddi_get_instance(rdip),
7986313Skrishnae 	    handlep ? "alloc" : "advisory");
7990Sstevel@tonic-gate 
8000Sstevel@tonic-gate 	if (!(mp = px_dma_lmts2hdl(dip, rdip, mmu_p, dmareq)))
8010Sstevel@tonic-gate 		return (DDI_DMA_NORESOURCES);
8020Sstevel@tonic-gate 	if (mp == (ddi_dma_impl_t *)DDI_DMA_NOMAPPING)
8030Sstevel@tonic-gate 		return (DDI_DMA_NOMAPPING);
8040Sstevel@tonic-gate 	if (ret = px_dma_type(px_p, dmareq, mp))
8050Sstevel@tonic-gate 		goto freehandle;
8060Sstevel@tonic-gate 	if (ret = px_dma_pfn(px_p, dmareq, mp))
8070Sstevel@tonic-gate 		goto freehandle;
8080Sstevel@tonic-gate 
8090Sstevel@tonic-gate 	switch (PX_DMA_TYPE(mp)) {
810909Segillett 	case PX_DMAI_FLAGS_DVMA:	/* LINTED E_EQUALITY_NOT_ASSIGNMENT */
8110Sstevel@tonic-gate 		if ((ret = px_dvma_win(px_p, dmareq, mp)) || !handlep)
8120Sstevel@tonic-gate 			goto freehandle;
8130Sstevel@tonic-gate 		if (!PX_DMA_CANCACHE(mp)) {	/* try fast track */
8140Sstevel@tonic-gate 			if (PX_DMA_CANFAST(mp)) {
8150Sstevel@tonic-gate 				if (!px_dvma_map_fast(mmu_p, mp))
8160Sstevel@tonic-gate 					break;
8170Sstevel@tonic-gate 			/* LINTED E_NOP_ELSE_STMT */
8180Sstevel@tonic-gate 			} else {
8190Sstevel@tonic-gate 				PX_DVMA_FASTTRAK_PROF(mp);
8200Sstevel@tonic-gate 			}
8210Sstevel@tonic-gate 		}
8220Sstevel@tonic-gate 		if (ret = px_dvma_map(mp, dmareq, mmu_p))
8230Sstevel@tonic-gate 			goto freehandle;
8240Sstevel@tonic-gate 		break;
825909Segillett 	case PX_DMAI_FLAGS_PTP:	/* LINTED E_EQUALITY_NOT_ASSIGNMENT */
8260Sstevel@tonic-gate 		if ((ret = px_dma_physwin(px_p, dmareq, mp)) || !handlep)
8270Sstevel@tonic-gate 			goto freehandle;
8280Sstevel@tonic-gate 		break;
829909Segillett 	case PX_DMAI_FLAGS_BYPASS:
8300Sstevel@tonic-gate 	default:
8310Sstevel@tonic-gate 		cmn_err(CE_PANIC, "%s%d: px_dma_setup: bad dma type 0x%x",
8326313Skrishnae 		    ddi_driver_name(rdip), ddi_get_instance(rdip),
8336313Skrishnae 		    PX_DMA_TYPE(mp));
8340Sstevel@tonic-gate 		/*NOTREACHED*/
8350Sstevel@tonic-gate 	}
8360Sstevel@tonic-gate 	*handlep = (ddi_dma_handle_t)mp;
837909Segillett 	mp->dmai_flags |= PX_DMAI_FLAGS_INUSE;
8380Sstevel@tonic-gate 	px_dump_dma_handle(DBG_DMA_MAP, dip, mp);
8390Sstevel@tonic-gate 
8400Sstevel@tonic-gate 	return ((mp->dmai_nwin == 1) ? DDI_DMA_MAPPED : DDI_DMA_PARTIAL_MAP);
8410Sstevel@tonic-gate freehandle:
8420Sstevel@tonic-gate 	if (ret == DDI_DMA_NORESOURCES)
8430Sstevel@tonic-gate 		px_dma_freemp(mp); /* don't run_callback() */
8440Sstevel@tonic-gate 	else
8450Sstevel@tonic-gate 		(void) px_dma_freehdl(dip, rdip, (ddi_dma_handle_t)mp);
8460Sstevel@tonic-gate 	return (ret);
8470Sstevel@tonic-gate }
8480Sstevel@tonic-gate 
8490Sstevel@tonic-gate 
8500Sstevel@tonic-gate /*
8510Sstevel@tonic-gate  * bus dma alloc handle entry point:
8520Sstevel@tonic-gate  */
8530Sstevel@tonic-gate int
8540Sstevel@tonic-gate px_dma_allochdl(dev_info_t *dip, dev_info_t *rdip, ddi_dma_attr_t *attrp,
8550Sstevel@tonic-gate 	int (*waitfp)(caddr_t), caddr_t arg, ddi_dma_handle_t *handlep)
8560Sstevel@tonic-gate {
8570Sstevel@tonic-gate 	px_t *px_p = DIP_TO_STATE(dip);
8580Sstevel@tonic-gate 	ddi_dma_impl_t *mp;
8590Sstevel@tonic-gate 	int rval;
8600Sstevel@tonic-gate 
8610Sstevel@tonic-gate 	DBG(DBG_DMA_ALLOCH, dip, "rdip=%s%d\n",
8626313Skrishnae 	    ddi_driver_name(rdip), ddi_get_instance(rdip));
8630Sstevel@tonic-gate 
8640Sstevel@tonic-gate 	if (attrp->dma_attr_version != DMA_ATTR_V0)
8650Sstevel@tonic-gate 		return (DDI_DMA_BADATTR);
8660Sstevel@tonic-gate 
8670Sstevel@tonic-gate 	if (!(mp = px_dma_allocmp(dip, rdip, waitfp, arg)))
8680Sstevel@tonic-gate 		return (DDI_DMA_NORESOURCES);
8690Sstevel@tonic-gate 
8700Sstevel@tonic-gate 	/*
8710Sstevel@tonic-gate 	 * Save requestor's information
8720Sstevel@tonic-gate 	 */
8730Sstevel@tonic-gate 	mp->dmai_attr	= *attrp; /* whole object - augmented later  */
874909Segillett 	*PX_DEV_ATTR(mp)	= *attrp; /* whole object - device orig attr */
8750Sstevel@tonic-gate 	DBG(DBG_DMA_ALLOCH, dip, "mp=%p\n", mp);
8760Sstevel@tonic-gate 
8770Sstevel@tonic-gate 	/* check and convert dma attributes to handle parameters */
8780Sstevel@tonic-gate 	if (rval = px_dma_attr2hdl(px_p, mp)) {
8790Sstevel@tonic-gate 		px_dma_freehdl(dip, rdip, (ddi_dma_handle_t)mp);
8800Sstevel@tonic-gate 		*handlep = NULL;
8810Sstevel@tonic-gate 		return (rval);
8820Sstevel@tonic-gate 	}
8830Sstevel@tonic-gate 	*handlep = (ddi_dma_handle_t)mp;
8840Sstevel@tonic-gate 	return (DDI_SUCCESS);
8850Sstevel@tonic-gate }
8860Sstevel@tonic-gate 
8870Sstevel@tonic-gate 
8880Sstevel@tonic-gate /*
8890Sstevel@tonic-gate  * bus dma free handle entry point:
8900Sstevel@tonic-gate  */
8910Sstevel@tonic-gate /*ARGSUSED*/
8920Sstevel@tonic-gate int
8930Sstevel@tonic-gate px_dma_freehdl(dev_info_t *dip, dev_info_t *rdip, ddi_dma_handle_t handle)
8940Sstevel@tonic-gate {
8950Sstevel@tonic-gate 	DBG(DBG_DMA_FREEH, dip, "rdip=%s%d mp=%p\n",
8966313Skrishnae 	    ddi_driver_name(rdip), ddi_get_instance(rdip), handle);
8970Sstevel@tonic-gate 	px_dma_freemp((ddi_dma_impl_t *)handle);
8980Sstevel@tonic-gate 
8990Sstevel@tonic-gate 	if (px_kmem_clid) {
9000Sstevel@tonic-gate 		DBG(DBG_DMA_FREEH, dip, "run handle callback\n");
9010Sstevel@tonic-gate 		ddi_run_callback(&px_kmem_clid);
9020Sstevel@tonic-gate 	}
9030Sstevel@tonic-gate 	return (DDI_SUCCESS);
9040Sstevel@tonic-gate }
9050Sstevel@tonic-gate 
9060Sstevel@tonic-gate 
9070Sstevel@tonic-gate /*
9080Sstevel@tonic-gate  * bus dma bind handle entry point:
9090Sstevel@tonic-gate  */
9100Sstevel@tonic-gate int
9110Sstevel@tonic-gate px_dma_bindhdl(dev_info_t *dip, dev_info_t *rdip,
9120Sstevel@tonic-gate 	ddi_dma_handle_t handle, ddi_dma_req_t *dmareq,
9130Sstevel@tonic-gate 	ddi_dma_cookie_t *cookiep, uint_t *ccountp)
9140Sstevel@tonic-gate {
9150Sstevel@tonic-gate 	px_t *px_p = DIP_TO_STATE(dip);
9160Sstevel@tonic-gate 	px_mmu_t *mmu_p = px_p->px_mmu_p;
9170Sstevel@tonic-gate 	ddi_dma_impl_t *mp = (ddi_dma_impl_t *)handle;
9180Sstevel@tonic-gate 	int ret;
9190Sstevel@tonic-gate 
9200Sstevel@tonic-gate 	DBG(DBG_DMA_BINDH, dip, "rdip=%s%d mp=%p dmareq=%p\n",
9216313Skrishnae 	    ddi_driver_name(rdip), ddi_get_instance(rdip), mp, dmareq);
9220Sstevel@tonic-gate 
923909Segillett 	if (mp->dmai_flags & PX_DMAI_FLAGS_INUSE)
9240Sstevel@tonic-gate 		return (DDI_DMA_INUSE);
9250Sstevel@tonic-gate 
926909Segillett 	ASSERT((mp->dmai_flags & ~PX_DMAI_FLAGS_PRESERVE) == 0);
927909Segillett 	mp->dmai_flags |= PX_DMAI_FLAGS_INUSE;
9280Sstevel@tonic-gate 
9290Sstevel@tonic-gate 	if (ret = px_dma_type(px_p, dmareq, mp))
9300Sstevel@tonic-gate 		goto err;
9310Sstevel@tonic-gate 	if (ret = px_dma_pfn(px_p, dmareq, mp))
9320Sstevel@tonic-gate 		goto err;
9330Sstevel@tonic-gate 
9340Sstevel@tonic-gate 	switch (PX_DMA_TYPE(mp)) {
935909Segillett 	case PX_DMAI_FLAGS_DVMA:
9360Sstevel@tonic-gate 		if (ret = px_dvma_win(px_p, dmareq, mp))
9370Sstevel@tonic-gate 			goto map_err;
9380Sstevel@tonic-gate 		if (!PX_DMA_CANCACHE(mp)) {	/* try fast track */
9390Sstevel@tonic-gate 			if (PX_DMA_CANFAST(mp)) {
9400Sstevel@tonic-gate 				if (!px_dvma_map_fast(mmu_p, mp))
9410Sstevel@tonic-gate 					goto mapped; /*LINTED E_NOP_ELSE_STMT*/
9420Sstevel@tonic-gate 			} else {
9430Sstevel@tonic-gate 				PX_DVMA_FASTTRAK_PROF(mp);
9440Sstevel@tonic-gate 			}
9450Sstevel@tonic-gate 		}
9460Sstevel@tonic-gate 		if (ret = px_dvma_map(mp, dmareq, mmu_p))
9470Sstevel@tonic-gate 			goto map_err;
9480Sstevel@tonic-gate mapped:
9490Sstevel@tonic-gate 		*ccountp = 1;
9500Sstevel@tonic-gate 		MAKE_DMA_COOKIE(cookiep, mp->dmai_mapping, mp->dmai_size);
9510Sstevel@tonic-gate 		break;
952909Segillett 	case PX_DMAI_FLAGS_BYPASS:
953909Segillett 	case PX_DMAI_FLAGS_PTP:
9540Sstevel@tonic-gate 		if (ret = px_dma_physwin(px_p, dmareq, mp))
9550Sstevel@tonic-gate 			goto map_err;
956909Segillett 		*ccountp = PX_WINLST(mp)->win_ncookies;
957909Segillett 		*cookiep =
958909Segillett 		    *(ddi_dma_cookie_t *)(PX_WINLST(mp) + 1); /* wholeobj */
9590Sstevel@tonic-gate 		break;
9600Sstevel@tonic-gate 	default:
9610Sstevel@tonic-gate 		cmn_err(CE_PANIC, "%s%d: px_dma_bindhdl(%p): bad dma type",
9626313Skrishnae 		    ddi_driver_name(rdip), ddi_get_instance(rdip), mp);
9630Sstevel@tonic-gate 		/*NOTREACHED*/
9640Sstevel@tonic-gate 	}
965624Sschwartz 	DBG(DBG_DMA_BINDH, dip, "cookie %" PRIx64 "+%x\n",
9666313Skrishnae 	    cookiep->dmac_address, cookiep->dmac_size);
9670Sstevel@tonic-gate 	px_dump_dma_handle(DBG_DMA_MAP, dip, mp);
96827Sjchu 
96927Sjchu 	/* insert dma handle into FMA cache */
9701865Sdilpreet 	if (mp->dmai_attr.dma_attr_flags & DDI_DMA_FLAGERR) {
97127Sjchu 		(void) ndi_fmc_insert(rdip, DMA_HANDLE, mp, NULL);
9726313Skrishnae 		mp->dmai_error.err_cf = px_err_dma_hdl_check;
9731865Sdilpreet 	}
97427Sjchu 
9750Sstevel@tonic-gate 	return (mp->dmai_nwin == 1 ? DDI_DMA_MAPPED : DDI_DMA_PARTIAL_MAP);
9760Sstevel@tonic-gate map_err:
9770Sstevel@tonic-gate 	px_dma_freepfn(mp);
9780Sstevel@tonic-gate err:
979909Segillett 	mp->dmai_flags &= PX_DMAI_FLAGS_PRESERVE;
9800Sstevel@tonic-gate 	return (ret);
9810Sstevel@tonic-gate }
9820Sstevel@tonic-gate 
9830Sstevel@tonic-gate 
9840Sstevel@tonic-gate /*
9850Sstevel@tonic-gate  * bus dma unbind handle entry point:
9860Sstevel@tonic-gate  */
9870Sstevel@tonic-gate /*ARGSUSED*/
9880Sstevel@tonic-gate int
9890Sstevel@tonic-gate px_dma_unbindhdl(dev_info_t *dip, dev_info_t *rdip, ddi_dma_handle_t handle)
9900Sstevel@tonic-gate {
9910Sstevel@tonic-gate 	ddi_dma_impl_t *mp = (ddi_dma_impl_t *)handle;
9920Sstevel@tonic-gate 	px_t *px_p = DIP_TO_STATE(dip);
9930Sstevel@tonic-gate 	px_mmu_t *mmu_p = px_p->px_mmu_p;
9940Sstevel@tonic-gate 
9950Sstevel@tonic-gate 	DBG(DBG_DMA_UNBINDH, dip, "rdip=%s%d, mp=%p\n",
9966313Skrishnae 	    ddi_driver_name(rdip), ddi_get_instance(rdip), handle);
997909Segillett 	if ((mp->dmai_flags & PX_DMAI_FLAGS_INUSE) == 0) {
9980Sstevel@tonic-gate 		DBG(DBG_DMA_UNBINDH, dip, "handle not inuse\n");
9990Sstevel@tonic-gate 		return (DDI_FAILURE);
10000Sstevel@tonic-gate 	}
10010Sstevel@tonic-gate 
100227Sjchu 	/* remove dma handle from FMA cache */
100327Sjchu 	if (mp->dmai_attr.dma_attr_flags & DDI_DMA_FLAGERR) {
100427Sjchu 		if (DEVI(rdip)->devi_fmhdl != NULL &&
100527Sjchu 		    DDI_FM_DMA_ERR_CAP(DEVI(rdip)->devi_fmhdl->fh_cap)) {
100627Sjchu 			(void) ndi_fmc_remove(rdip, DMA_HANDLE, mp);
100727Sjchu 		}
100827Sjchu 	}
100927Sjchu 
10100Sstevel@tonic-gate 	/*
10110Sstevel@tonic-gate 	 * Here if the handle is using the iommu.  Unload all the iommu
10120Sstevel@tonic-gate 	 * translations.
10130Sstevel@tonic-gate 	 */
10140Sstevel@tonic-gate 	switch (PX_DMA_TYPE(mp)) {
1015909Segillett 	case PX_DMAI_FLAGS_DVMA:
10160Sstevel@tonic-gate 		px_mmu_unmap_window(mmu_p, mp);
10170Sstevel@tonic-gate 		px_dvma_unmap(mmu_p, mp);
10180Sstevel@tonic-gate 		px_dma_freepfn(mp);
10190Sstevel@tonic-gate 		break;
1020909Segillett 	case PX_DMAI_FLAGS_BYPASS:
1021909Segillett 	case PX_DMAI_FLAGS_PTP:
10220Sstevel@tonic-gate 		px_dma_freewin(mp);
10230Sstevel@tonic-gate 		break;
10240Sstevel@tonic-gate 	default:
10250Sstevel@tonic-gate 		cmn_err(CE_PANIC, "%s%d: px_dma_unbindhdl:bad dma type %p",
10266313Skrishnae 		    ddi_driver_name(rdip), ddi_get_instance(rdip), mp);
10270Sstevel@tonic-gate 		/*NOTREACHED*/
10280Sstevel@tonic-gate 	}
10290Sstevel@tonic-gate 	if (mmu_p->mmu_dvma_clid != 0) {
10300Sstevel@tonic-gate 		DBG(DBG_DMA_UNBINDH, dip, "run dvma callback\n");
10310Sstevel@tonic-gate 		ddi_run_callback(&mmu_p->mmu_dvma_clid);
10320Sstevel@tonic-gate 	}
10330Sstevel@tonic-gate 	if (px_kmem_clid) {
10340Sstevel@tonic-gate 		DBG(DBG_DMA_UNBINDH, dip, "run handle callback\n");
10350Sstevel@tonic-gate 		ddi_run_callback(&px_kmem_clid);
10360Sstevel@tonic-gate 	}
1037909Segillett 	mp->dmai_flags &= PX_DMAI_FLAGS_PRESERVE;
103827Sjchu 
10390Sstevel@tonic-gate 	return (DDI_SUCCESS);
10400Sstevel@tonic-gate }
10410Sstevel@tonic-gate 
10420Sstevel@tonic-gate /*
10430Sstevel@tonic-gate  * bus dma win entry point:
10440Sstevel@tonic-gate  */
10450Sstevel@tonic-gate int
10460Sstevel@tonic-gate px_dma_win(dev_info_t *dip, dev_info_t *rdip,
10470Sstevel@tonic-gate 	ddi_dma_handle_t handle, uint_t win, off_t *offp,
10480Sstevel@tonic-gate 	size_t *lenp, ddi_dma_cookie_t *cookiep, uint_t *ccountp)
10490Sstevel@tonic-gate {
10500Sstevel@tonic-gate 	ddi_dma_impl_t	*mp = (ddi_dma_impl_t *)handle;
10510Sstevel@tonic-gate 	int		ret;
10520Sstevel@tonic-gate 
10530Sstevel@tonic-gate 	DBG(DBG_DMA_WIN, dip, "rdip=%s%d\n",
10546313Skrishnae 	    ddi_driver_name(rdip), ddi_get_instance(rdip));
10550Sstevel@tonic-gate 
10560Sstevel@tonic-gate 	px_dump_dma_handle(DBG_DMA_WIN, dip, mp);
10570Sstevel@tonic-gate 	if (win >= mp->dmai_nwin) {
10580Sstevel@tonic-gate 		DBG(DBG_DMA_WIN, dip, "%x out of range\n", win);
10590Sstevel@tonic-gate 		return (DDI_FAILURE);
10600Sstevel@tonic-gate 	}
10610Sstevel@tonic-gate 
10620Sstevel@tonic-gate 	switch (PX_DMA_TYPE(mp)) {
1063909Segillett 	case PX_DMAI_FLAGS_DVMA:
10640Sstevel@tonic-gate 		if (win != PX_DMA_CURWIN(mp)) {
10650Sstevel@tonic-gate 			px_t *px_p = DIP_TO_STATE(dip);
10660Sstevel@tonic-gate 			px_mmu_t *mmu_p = px_p->px_mmu_p;
10670Sstevel@tonic-gate 			px_mmu_unmap_window(mmu_p, mp);
10680Sstevel@tonic-gate 
10690Sstevel@tonic-gate 			/* map_window sets dmai_mapping/size/offset */
10700Sstevel@tonic-gate 			px_mmu_map_window(mmu_p, mp, win);
10710Sstevel@tonic-gate 			if ((ret = px_mmu_map_window(mmu_p,
10720Sstevel@tonic-gate 			    mp, win)) != DDI_SUCCESS)
10730Sstevel@tonic-gate 				return (ret);
10740Sstevel@tonic-gate 		}
10750Sstevel@tonic-gate 		if (cookiep)
10760Sstevel@tonic-gate 			MAKE_DMA_COOKIE(cookiep, mp->dmai_mapping,
10776313Skrishnae 			    mp->dmai_size);
10780Sstevel@tonic-gate 		if (ccountp)
10790Sstevel@tonic-gate 			*ccountp = 1;
10800Sstevel@tonic-gate 		break;
1081909Segillett 	case PX_DMAI_FLAGS_PTP:
1082909Segillett 	case PX_DMAI_FLAGS_BYPASS: {
10830Sstevel@tonic-gate 		int i;
10840Sstevel@tonic-gate 		ddi_dma_cookie_t *ck_p;
10850Sstevel@tonic-gate 		px_dma_win_t *win_p = mp->dmai_winlst;
10860Sstevel@tonic-gate 
10876313Skrishnae 		for (i = 0; i < win; win_p = win_p->win_next, i++) {};
10880Sstevel@tonic-gate 		ck_p = (ddi_dma_cookie_t *)(win_p + 1);
10890Sstevel@tonic-gate 		*cookiep = *ck_p;
10900Sstevel@tonic-gate 		mp->dmai_offset = win_p->win_offset;
10910Sstevel@tonic-gate 		mp->dmai_size   = win_p->win_size;
10920Sstevel@tonic-gate 		mp->dmai_mapping = ck_p->dmac_laddress;
10930Sstevel@tonic-gate 		mp->dmai_cookie = ck_p + 1;
10940Sstevel@tonic-gate 		win_p->win_curseg = 0;
10950Sstevel@tonic-gate 		if (ccountp)
10960Sstevel@tonic-gate 			*ccountp = win_p->win_ncookies;
10970Sstevel@tonic-gate 		}
10980Sstevel@tonic-gate 		break;
10990Sstevel@tonic-gate 	default:
11000Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d: px_dma_win:bad dma type 0x%x",
11016313Skrishnae 		    ddi_driver_name(rdip), ddi_get_instance(rdip),
11026313Skrishnae 		    PX_DMA_TYPE(mp));
11030Sstevel@tonic-gate 		return (DDI_FAILURE);
11040Sstevel@tonic-gate 	}
11050Sstevel@tonic-gate 	if (cookiep)
11060Sstevel@tonic-gate 		DBG(DBG_DMA_WIN, dip,
11076313Skrishnae 		    "cookie - dmac_address=%x dmac_size=%x\n",
11086313Skrishnae 		    cookiep->dmac_address, cookiep->dmac_size);
11090Sstevel@tonic-gate 	if (offp)
11100Sstevel@tonic-gate 		*offp = (off_t)mp->dmai_offset;
11110Sstevel@tonic-gate 	if (lenp)
11120Sstevel@tonic-gate 		*lenp = mp->dmai_size;
11130Sstevel@tonic-gate 	return (DDI_SUCCESS);
11140Sstevel@tonic-gate }
11150Sstevel@tonic-gate 
11160Sstevel@tonic-gate #ifdef	DEBUG
11170Sstevel@tonic-gate static char *px_dmactl_str[] = {
11180Sstevel@tonic-gate 	"DDI_DMA_FREE",
11190Sstevel@tonic-gate 	"DDI_DMA_SYNC",
11200Sstevel@tonic-gate 	"DDI_DMA_HTOC",
11210Sstevel@tonic-gate 	"DDI_DMA_KVADDR",
11220Sstevel@tonic-gate 	"DDI_DMA_MOVWIN",
11230Sstevel@tonic-gate 	"DDI_DMA_REPWIN",
11240Sstevel@tonic-gate 	"DDI_DMA_GETERR",
11250Sstevel@tonic-gate 	"DDI_DMA_COFF",
11260Sstevel@tonic-gate 	"DDI_DMA_NEXTWIN",
11270Sstevel@tonic-gate 	"DDI_DMA_NEXTSEG",
11280Sstevel@tonic-gate 	"DDI_DMA_SEGTOC",
11290Sstevel@tonic-gate 	"DDI_DMA_RESERVE",
11300Sstevel@tonic-gate 	"DDI_DMA_RELEASE",
11310Sstevel@tonic-gate 	"DDI_DMA_RESETH",
11320Sstevel@tonic-gate 	"DDI_DMA_CKSYNC",
11330Sstevel@tonic-gate 	"DDI_DMA_IOPB_ALLOC",
11340Sstevel@tonic-gate 	"DDI_DMA_IOPB_FREE",
11350Sstevel@tonic-gate 	"DDI_DMA_SMEM_ALLOC",
11360Sstevel@tonic-gate 	"DDI_DMA_SMEM_FREE",
11370Sstevel@tonic-gate 	"DDI_DMA_SET_SBUS64"
11380Sstevel@tonic-gate };
11390Sstevel@tonic-gate #endif	/* DEBUG */
11400Sstevel@tonic-gate 
11410Sstevel@tonic-gate /*
11420Sstevel@tonic-gate  * bus dma control entry point:
11430Sstevel@tonic-gate  */
11440Sstevel@tonic-gate /*ARGSUSED*/
11450Sstevel@tonic-gate int
11460Sstevel@tonic-gate px_dma_ctlops(dev_info_t *dip, dev_info_t *rdip, ddi_dma_handle_t handle,
11470Sstevel@tonic-gate 	enum ddi_dma_ctlops cmd, off_t *offp, size_t *lenp, caddr_t *objp,
11480Sstevel@tonic-gate 	uint_t cache_flags)
11490Sstevel@tonic-gate {
11500Sstevel@tonic-gate 	ddi_dma_impl_t *mp = (ddi_dma_impl_t *)handle;
11510Sstevel@tonic-gate 
11520Sstevel@tonic-gate #ifdef	DEBUG
11530Sstevel@tonic-gate 	DBG(DBG_DMA_CTL, dip, "%s: rdip=%s%d\n", px_dmactl_str[cmd],
11546313Skrishnae 	    ddi_driver_name(rdip), ddi_get_instance(rdip));
11550Sstevel@tonic-gate #endif	/* DEBUG */
11560Sstevel@tonic-gate 
11570Sstevel@tonic-gate 	switch (cmd) {
11580Sstevel@tonic-gate 	case DDI_DMA_FREE:
11590Sstevel@tonic-gate 		(void) px_dma_unbindhdl(dip, rdip, handle);
11600Sstevel@tonic-gate 		(void) px_dma_freehdl(dip, rdip, handle);
11610Sstevel@tonic-gate 		return (DDI_SUCCESS);
11620Sstevel@tonic-gate 	case DDI_DMA_RESERVE: {
11630Sstevel@tonic-gate 		px_t *px_p = DIP_TO_STATE(dip);
11640Sstevel@tonic-gate 		return (px_fdvma_reserve(dip, rdip, px_p,
11656313Skrishnae 		    (ddi_dma_req_t *)offp, (ddi_dma_handle_t *)objp));
11660Sstevel@tonic-gate 		}
11670Sstevel@tonic-gate 	case DDI_DMA_RELEASE: {
11680Sstevel@tonic-gate 		px_t *px_p = DIP_TO_STATE(dip);
11690Sstevel@tonic-gate 		return (px_fdvma_release(dip, px_p, mp));
11700Sstevel@tonic-gate 		}
11710Sstevel@tonic-gate 	default:
11720Sstevel@tonic-gate 		break;
11730Sstevel@tonic-gate 	}
11740Sstevel@tonic-gate 
11750Sstevel@tonic-gate 	switch (PX_DMA_TYPE(mp)) {
1176909Segillett 	case PX_DMAI_FLAGS_DVMA:
11770Sstevel@tonic-gate 		return (px_dvma_ctl(dip, rdip, mp, cmd, offp, lenp, objp,
11786313Skrishnae 		    cache_flags));
1179909Segillett 	case PX_DMAI_FLAGS_PTP:
1180909Segillett 	case PX_DMAI_FLAGS_BYPASS:
11810Sstevel@tonic-gate 		return (px_dma_ctl(dip, rdip, mp, cmd, offp, lenp, objp,
11826313Skrishnae 		    cache_flags));
11830Sstevel@tonic-gate 	default:
11840Sstevel@tonic-gate 		cmn_err(CE_PANIC, "%s%d: px_dma_ctlops(%x):bad dma type %x",
11856313Skrishnae 		    ddi_driver_name(rdip), ddi_get_instance(rdip), cmd,
11866313Skrishnae 		    mp->dmai_flags);
11870Sstevel@tonic-gate 		/*NOTREACHED*/
11880Sstevel@tonic-gate 	}
1189671Skrishnae 	return (0);
11900Sstevel@tonic-gate }
11910Sstevel@tonic-gate 
11920Sstevel@tonic-gate /*
11930Sstevel@tonic-gate  * control ops entry point:
11940Sstevel@tonic-gate  *
11950Sstevel@tonic-gate  * Requests handled completely:
11960Sstevel@tonic-gate  *	DDI_CTLOPS_INITCHILD	see init_child() for details
11970Sstevel@tonic-gate  *	DDI_CTLOPS_UNINITCHILD
11980Sstevel@tonic-gate  *	DDI_CTLOPS_REPORTDEV	see report_dev() for details
11990Sstevel@tonic-gate  *	DDI_CTLOPS_IOMIN	cache line size if streaming otherwise 1
12000Sstevel@tonic-gate  *	DDI_CTLOPS_REGSIZE
12010Sstevel@tonic-gate  *	DDI_CTLOPS_NREGS
12020Sstevel@tonic-gate  *	DDI_CTLOPS_DVMAPAGESIZE
12030Sstevel@tonic-gate  *	DDI_CTLOPS_POKE
12040Sstevel@tonic-gate  *	DDI_CTLOPS_PEEK
12050Sstevel@tonic-gate  *
12060Sstevel@tonic-gate  * All others passed to parent.
12070Sstevel@tonic-gate  */
12080Sstevel@tonic-gate int
12090Sstevel@tonic-gate px_ctlops(dev_info_t *dip, dev_info_t *rdip,
12100Sstevel@tonic-gate 	ddi_ctl_enum_t op, void *arg, void *result)
12110Sstevel@tonic-gate {
12120Sstevel@tonic-gate 	px_t *px_p = DIP_TO_STATE(dip);
12130Sstevel@tonic-gate 	struct detachspec *ds;
12140Sstevel@tonic-gate 	struct attachspec *as;
12150Sstevel@tonic-gate 
12160Sstevel@tonic-gate 	switch (op) {
12170Sstevel@tonic-gate 	case DDI_CTLOPS_INITCHILD:
12180Sstevel@tonic-gate 		return (px_init_child(px_p, (dev_info_t *)arg));
12190Sstevel@tonic-gate 
12200Sstevel@tonic-gate 	case DDI_CTLOPS_UNINITCHILD:
12210Sstevel@tonic-gate 		return (px_uninit_child(px_p, (dev_info_t *)arg));
12220Sstevel@tonic-gate 
12230Sstevel@tonic-gate 	case DDI_CTLOPS_ATTACH:
12243421Sjchu 		if (!pcie_is_child(dip, rdip))
12253421Sjchu 			return (DDI_SUCCESS);
12263421Sjchu 
12270Sstevel@tonic-gate 		as = (struct attachspec *)arg;
12280Sstevel@tonic-gate 		switch (as->when) {
12290Sstevel@tonic-gate 		case DDI_PRE:
12300Sstevel@tonic-gate 			if (as->cmd == DDI_ATTACH) {
12310Sstevel@tonic-gate 				DBG(DBG_PWR, dip, "PRE_ATTACH for %s@%d\n",
12320Sstevel@tonic-gate 				    ddi_driver_name(rdip),
12330Sstevel@tonic-gate 				    ddi_get_instance(rdip));
12340Sstevel@tonic-gate 				return (pcie_pm_hold(dip));
12350Sstevel@tonic-gate 			}
1236383Set142600 			if (as->cmd == DDI_RESUME) {
1237383Set142600 				DBG(DBG_PWR, dip, "PRE_RESUME for %s@%d\n",
1238383Set142600 				    ddi_driver_name(rdip),
1239383Set142600 				    ddi_get_instance(rdip));
1240383Set142600 
12416313Skrishnae 				pcie_clear_errors(rdip);
1242383Set142600 			}
12430Sstevel@tonic-gate 			return (DDI_SUCCESS);
12440Sstevel@tonic-gate 
12450Sstevel@tonic-gate 		case DDI_POST:
12460Sstevel@tonic-gate 			DBG(DBG_PWR, dip, "POST_ATTACH for %s@%d\n",
12470Sstevel@tonic-gate 			    ddi_driver_name(rdip), ddi_get_instance(rdip));
12480Sstevel@tonic-gate 			if (as->cmd == DDI_ATTACH && as->result != DDI_SUCCESS)
12490Sstevel@tonic-gate 				pcie_pm_release(dip);
12502738Skrishnae 
12514005Skrishnae 			if (as->result == DDI_SUCCESS)
12524005Skrishnae 				pf_init(rdip, (void *)px_p->px_fm_ibc, as->cmd);
12533274Set142600 
12542738Skrishnae 			(void) pcie_postattach_child(rdip);
12552738Skrishnae 
12560Sstevel@tonic-gate 			return (DDI_SUCCESS);
12570Sstevel@tonic-gate 		default:
12580Sstevel@tonic-gate 			break;
12590Sstevel@tonic-gate 		}
12600Sstevel@tonic-gate 		break;
12610Sstevel@tonic-gate 
12620Sstevel@tonic-gate 	case DDI_CTLOPS_DETACH:
12633714Sjchu 		if (!pcie_is_child(dip, rdip))
12643714Sjchu 			return (DDI_SUCCESS);
12653714Sjchu 
12660Sstevel@tonic-gate 		ds = (struct detachspec *)arg;
12670Sstevel@tonic-gate 		switch (ds->when) {
12680Sstevel@tonic-gate 		case DDI_POST:
12690Sstevel@tonic-gate 			if (ds->cmd == DDI_DETACH &&
12700Sstevel@tonic-gate 			    ds->result == DDI_SUCCESS) {
12710Sstevel@tonic-gate 				DBG(DBG_PWR, dip, "POST_DETACH for %s@%d\n",
12720Sstevel@tonic-gate 				    ddi_driver_name(rdip),
12730Sstevel@tonic-gate 				    ddi_get_instance(rdip));
12740Sstevel@tonic-gate 				return (pcie_pm_remove_child(dip, rdip));
12750Sstevel@tonic-gate 			}
12760Sstevel@tonic-gate 			return (DDI_SUCCESS);
12773274Set142600 		case DDI_PRE:
12783756Skrishnae 			pf_fini(rdip, ds->cmd);
12793274Set142600 			return (DDI_SUCCESS);
12800Sstevel@tonic-gate 		default:
12810Sstevel@tonic-gate 			break;
12820Sstevel@tonic-gate 		}
12830Sstevel@tonic-gate 		break;
12840Sstevel@tonic-gate 
12850Sstevel@tonic-gate 	case DDI_CTLOPS_REPORTDEV:
12860Sstevel@tonic-gate 		return (px_report_dev(rdip));
12870Sstevel@tonic-gate 
12880Sstevel@tonic-gate 	case DDI_CTLOPS_IOMIN:
12890Sstevel@tonic-gate 		return (DDI_SUCCESS);
12900Sstevel@tonic-gate 
12910Sstevel@tonic-gate 	case DDI_CTLOPS_REGSIZE:
12920Sstevel@tonic-gate 		*((off_t *)result) = px_get_reg_set_size(rdip, *((int *)arg));
129327Sjchu 		return (*((off_t *)result) == 0 ? DDI_FAILURE : DDI_SUCCESS);
12940Sstevel@tonic-gate 
12950Sstevel@tonic-gate 	case DDI_CTLOPS_NREGS:
12960Sstevel@tonic-gate 		*((uint_t *)result) = px_get_nreg_set(rdip);
12970Sstevel@tonic-gate 		return (DDI_SUCCESS);
12980Sstevel@tonic-gate 
12990Sstevel@tonic-gate 	case DDI_CTLOPS_DVMAPAGESIZE:
13000Sstevel@tonic-gate 		*((ulong_t *)result) = MMU_PAGE_SIZE;
13010Sstevel@tonic-gate 		return (DDI_SUCCESS);
13020Sstevel@tonic-gate 
13030Sstevel@tonic-gate 	case DDI_CTLOPS_POKE:	/* platform dependent implementation. */
13040Sstevel@tonic-gate 		return (px_lib_ctlops_poke(dip, rdip,
13050Sstevel@tonic-gate 		    (peekpoke_ctlops_t *)arg));
13060Sstevel@tonic-gate 
13070Sstevel@tonic-gate 	case DDI_CTLOPS_PEEK:	/* platform dependent implementation. */
13080Sstevel@tonic-gate 		return (px_lib_ctlops_peek(dip, rdip,
13090Sstevel@tonic-gate 		    (peekpoke_ctlops_t *)arg, result));
13100Sstevel@tonic-gate 
13110Sstevel@tonic-gate 	case DDI_CTLOPS_POWER:
13120Sstevel@tonic-gate 	default:
13130Sstevel@tonic-gate 		break;
13140Sstevel@tonic-gate 	}
13150Sstevel@tonic-gate 
13160Sstevel@tonic-gate 	/*
13170Sstevel@tonic-gate 	 * Now pass the request up to our parent.
13180Sstevel@tonic-gate 	 */
13190Sstevel@tonic-gate 	DBG(DBG_CTLOPS, dip, "passing request to parent: rdip=%s%d\n",
13206313Skrishnae 	    ddi_driver_name(rdip), ddi_get_instance(rdip));
13210Sstevel@tonic-gate 	return (ddi_ctlops(dip, rdip, op, arg, result));
13220Sstevel@tonic-gate }
13230Sstevel@tonic-gate 
13240Sstevel@tonic-gate /* ARGSUSED */
13250Sstevel@tonic-gate int
13260Sstevel@tonic-gate px_intr_ops(dev_info_t *dip, dev_info_t *rdip, ddi_intr_op_t intr_op,
13270Sstevel@tonic-gate     ddi_intr_handle_impl_t *hdlp, void *result)
13280Sstevel@tonic-gate {
13290Sstevel@tonic-gate 	int	intr_types, ret = DDI_SUCCESS;
13300Sstevel@tonic-gate 
13310Sstevel@tonic-gate 	DBG(DBG_INTROPS, dip, "px_intr_ops: rdip=%s%d\n",
13320Sstevel@tonic-gate 	    ddi_driver_name(rdip), ddi_get_instance(rdip));
13330Sstevel@tonic-gate 
13340Sstevel@tonic-gate 	/* Process DDI_INTROP_SUPPORTED_TYPES request here */
13350Sstevel@tonic-gate 	if (intr_op == DDI_INTROP_SUPPORTED_TYPES) {
13362580Sanish 		*(int *)result = i_ddi_get_intx_nintrs(rdip) ?
13370Sstevel@tonic-gate 		    DDI_INTR_TYPE_FIXED : 0;
13380Sstevel@tonic-gate 
13390Sstevel@tonic-gate 		if ((pci_msi_get_supported_type(rdip,
13400Sstevel@tonic-gate 		    &intr_types)) == DDI_SUCCESS) {
13410Sstevel@tonic-gate 			/*
13420Sstevel@tonic-gate 			 * Double check supported interrupt types vs.
13430Sstevel@tonic-gate 			 * what the host bridge supports.
13440Sstevel@tonic-gate 			 */
13451725Segillett 			*(int *)result |= intr_types;
13460Sstevel@tonic-gate 		}
13470Sstevel@tonic-gate 
13480Sstevel@tonic-gate 		return (ret);
13490Sstevel@tonic-gate 	}
13500Sstevel@tonic-gate 
13510Sstevel@tonic-gate 	/*
13520Sstevel@tonic-gate 	 * PCI-E nexus driver supports fixed, MSI and MSI-X interrupts.
13530Sstevel@tonic-gate 	 * Return failure if interrupt type is not supported.
13540Sstevel@tonic-gate 	 */
13550Sstevel@tonic-gate 	switch (hdlp->ih_type) {
13560Sstevel@tonic-gate 	case DDI_INTR_TYPE_FIXED:
13570Sstevel@tonic-gate 		ret = px_intx_ops(dip, rdip, intr_op, hdlp, result);
13580Sstevel@tonic-gate 		break;
13590Sstevel@tonic-gate 	case DDI_INTR_TYPE_MSI:
13600Sstevel@tonic-gate 	case DDI_INTR_TYPE_MSIX:
13610Sstevel@tonic-gate 		ret = px_msix_ops(dip, rdip, intr_op, hdlp, result);
13620Sstevel@tonic-gate 		break;
13630Sstevel@tonic-gate 	default:
13640Sstevel@tonic-gate 		ret = DDI_ENOTSUP;
13650Sstevel@tonic-gate 		break;
13660Sstevel@tonic-gate 	}
13670Sstevel@tonic-gate 
13680Sstevel@tonic-gate 	return (ret);
13690Sstevel@tonic-gate }
13701531Skini 
13712840Scarlsonj static int
13721531Skini px_init_hotplug(px_t *px_p)
13731531Skini {
13741531Skini 	px_bus_range_t bus_range;
13751531Skini 	dev_info_t *dip;
13761531Skini 	pciehpc_regops_t regops;
13771531Skini 
13781531Skini 	dip = px_p->px_dip;
13791531Skini 
13801531Skini 	if (ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
13811531Skini 	    "hotplug-capable") == 0)
13821531Skini 		return (DDI_FAILURE);
13831531Skini 
13841531Skini 	/*
13851531Skini 	 * Before initializing hotplug - open up bus range.  The busra
13861531Skini 	 * module will initialize its pool of bus numbers from this.
13871531Skini 	 * "busra" will be the agent that keeps track of them during
13881531Skini 	 * hotplug.  Also, note, that busra will remove any bus numbers
13891531Skini 	 * already in use from boot time.
13901531Skini 	 */
13911531Skini 	if (ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
13921531Skini 	    "bus-range") == 0) {
13931531Skini 		cmn_err(CE_WARN, "%s%d: bus-range not found\n",
13941531Skini 		    ddi_driver_name(dip), ddi_get_instance(dip));
13951531Skini #ifdef	DEBUG
13961531Skini 		bus_range.lo = 0x0;
13971531Skini 		bus_range.hi = 0xff;
13981531Skini 
13991531Skini 		if (ndi_prop_update_int_array(DDI_DEV_T_NONE,
14001531Skini 		    dip, "bus-range", (int *)&bus_range, 2)
14011531Skini 		    != DDI_PROP_SUCCESS) {
14021531Skini 			return (DDI_FAILURE);
14031531Skini 		}
14041531Skini #else
14051531Skini 		return (DDI_FAILURE);
14061531Skini #endif
14071531Skini 	}
14081531Skini 
14091531Skini 	if (px_lib_hotplug_init(dip, (void *)&regops) != DDI_SUCCESS)
14101531Skini 		return (DDI_FAILURE);
14111531Skini 
14121531Skini 	if (pciehpc_init(dip, &regops) != DDI_SUCCESS) {
14131531Skini 		px_lib_hotplug_uninit(dip);
14141531Skini 		return (DDI_FAILURE);
14151531Skini 	}
14161531Skini 
14171531Skini 	if (pcihp_init(dip) != DDI_SUCCESS) {
14181531Skini 		(void) pciehpc_uninit(dip);
14191531Skini 		px_lib_hotplug_uninit(dip);
14201531Skini 		return (DDI_FAILURE);
14211531Skini 	}
14221531Skini 
14231531Skini 	if (pcihp_get_cb_ops() != NULL) {
14241531Skini 		DBG(DBG_ATTACH, dip, "%s%d hotplug enabled",
14251531Skini 		    ddi_driver_name(dip), ddi_get_instance(dip));
14261531Skini 		px_p->px_dev_caps |= PX_HOTPLUG_CAPABLE;
14271531Skini 	}
14281531Skini 
14291531Skini 	return (DDI_SUCCESS);
14301531Skini }
14311531Skini 
14322840Scarlsonj static int
14331531Skini px_uninit_hotplug(dev_info_t *dip)
14341531Skini {
14351531Skini 	if (pcihp_uninit(dip) != DDI_SUCCESS)
14361531Skini 		return (DDI_FAILURE);
14371531Skini 
14381531Skini 	if (pciehpc_uninit(dip) != DDI_SUCCESS)
14391531Skini 		return (DDI_FAILURE);
14401531Skini 
14411531Skini 	px_lib_hotplug_uninit(dip);
14421531Skini 
14431531Skini 	return (DDI_SUCCESS);
14441531Skini }
1445*7596SAlan.Adamson@Sun.COM 
1446*7596SAlan.Adamson@Sun.COM static void
1447*7596SAlan.Adamson@Sun.COM px_set_mps(px_t *px_p)
1448*7596SAlan.Adamson@Sun.COM {
1449*7596SAlan.Adamson@Sun.COM 	dev_info_t	*dip;
1450*7596SAlan.Adamson@Sun.COM 	pcie_bus_t	*bus_p;
1451*7596SAlan.Adamson@Sun.COM 	int		max_supported;
1452*7596SAlan.Adamson@Sun.COM 
1453*7596SAlan.Adamson@Sun.COM 	dip = px_p->px_dip;
1454*7596SAlan.Adamson@Sun.COM 	bus_p = PCIE_DIP2BUS(dip);
1455*7596SAlan.Adamson@Sun.COM 
1456*7596SAlan.Adamson@Sun.COM 	bus_p->bus_mps = -1;
1457*7596SAlan.Adamson@Sun.COM 
1458*7596SAlan.Adamson@Sun.COM 	if (pcie_root_port(dip) == DDI_FAILURE) {
1459*7596SAlan.Adamson@Sun.COM 		if (px_lib_get_root_complex_mps(px_p, dip,
1460*7596SAlan.Adamson@Sun.COM 		    &max_supported) < 0) {
1461*7596SAlan.Adamson@Sun.COM 
1462*7596SAlan.Adamson@Sun.COM 			DBG(DBG_MPS, dip, "MPS:  Can not get RC MPS\n");
1463*7596SAlan.Adamson@Sun.COM 			return;
1464*7596SAlan.Adamson@Sun.COM 		}
1465*7596SAlan.Adamson@Sun.COM 
1466*7596SAlan.Adamson@Sun.COM 		DBG(DBG_MPS, dip, "MPS: Root Complex MPS Cap of = %x\n",
1467*7596SAlan.Adamson@Sun.COM 		    max_supported);
1468*7596SAlan.Adamson@Sun.COM 
1469*7596SAlan.Adamson@Sun.COM 		if (pcie_max_mps < max_supported)
1470*7596SAlan.Adamson@Sun.COM 			max_supported = pcie_max_mps;
1471*7596SAlan.Adamson@Sun.COM 
1472*7596SAlan.Adamson@Sun.COM 		(void) pcie_get_fabric_mps(dip, ddi_get_child(dip),
1473*7596SAlan.Adamson@Sun.COM 		    &max_supported);
1474*7596SAlan.Adamson@Sun.COM 
1475*7596SAlan.Adamson@Sun.COM 		bus_p->bus_mps = max_supported;
1476*7596SAlan.Adamson@Sun.COM 
1477*7596SAlan.Adamson@Sun.COM 		(void) px_lib_set_root_complex_mps(px_p, dip, bus_p->bus_mps);
1478*7596SAlan.Adamson@Sun.COM 
1479*7596SAlan.Adamson@Sun.COM 		DBG(DBG_MPS, dip, "MPS: Root Complex MPS Set to = %x\n",
1480*7596SAlan.Adamson@Sun.COM 		    bus_p->bus_mps);
1481*7596SAlan.Adamson@Sun.COM 	}
1482*7596SAlan.Adamson@Sun.COM }
1483