xref: /onnv-gate/usr/src/uts/sun4/io/px/px.c (revision 3162:b8af42ef962d)
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 /*
221531Skini  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate /*
290Sstevel@tonic-gate  * PCI Express nexus driver interface
300Sstevel@tonic-gate  */
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #include <sys/types.h>
330Sstevel@tonic-gate #include <sys/conf.h>		/* nulldev */
340Sstevel@tonic-gate #include <sys/stat.h>		/* devctl */
350Sstevel@tonic-gate #include <sys/kmem.h>
360Sstevel@tonic-gate #include <sys/sunddi.h>
370Sstevel@tonic-gate #include <sys/sunndi.h>
380Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
390Sstevel@tonic-gate #include <sys/ddi_subrdefs.h>
40118Sjchu #include <sys/spl.h>
410Sstevel@tonic-gate #include <sys/epm.h>
420Sstevel@tonic-gate #include <sys/iommutsb.h>
431531Skini #include <sys/hotplug/pci/pcihp.h>
441531Skini #include <sys/hotplug/pci/pciehpc.h>
450Sstevel@tonic-gate #include "px_obj.h"
46624Sschwartz #include <sys/pci_tools.h>
47777Sschwartz #include "px_tools_ext.h"
480Sstevel@tonic-gate #include "pcie_pwr.h"
490Sstevel@tonic-gate 
500Sstevel@tonic-gate /*LINTLIBRARY*/
510Sstevel@tonic-gate 
520Sstevel@tonic-gate /*
530Sstevel@tonic-gate  * function prototypes for dev ops routines:
540Sstevel@tonic-gate  */
550Sstevel@tonic-gate static int px_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
560Sstevel@tonic-gate static int px_detach(dev_info_t *dip, ddi_detach_cmd_t cmd);
570Sstevel@tonic-gate static int px_info(dev_info_t *dip, ddi_info_cmd_t infocmd,
580Sstevel@tonic-gate 	void *arg, void **result);
591648Sjchu static int px_cb_attach(px_t *);
601648Sjchu static void px_cb_detach(px_t *);
610Sstevel@tonic-gate static int px_pwr_setup(dev_info_t *dip);
620Sstevel@tonic-gate static void px_pwr_teardown(dev_info_t *dip);
630Sstevel@tonic-gate 
641865Sdilpreet extern errorq_t *pci_target_queue;
651865Sdilpreet 
660Sstevel@tonic-gate /*
671531Skini  * function prototypes for hotplug routines:
681531Skini  */
692840Scarlsonj static int px_init_hotplug(px_t *px_p);
702840Scarlsonj static int px_uninit_hotplug(dev_info_t *dip);
711531Skini 
721531Skini /*
730Sstevel@tonic-gate  * bus ops and dev ops structures:
740Sstevel@tonic-gate  */
750Sstevel@tonic-gate static struct bus_ops px_bus_ops = {
760Sstevel@tonic-gate 	BUSO_REV,
770Sstevel@tonic-gate 	px_map,
780Sstevel@tonic-gate 	0,
790Sstevel@tonic-gate 	0,
800Sstevel@tonic-gate 	0,
810Sstevel@tonic-gate 	i_ddi_map_fault,
820Sstevel@tonic-gate 	px_dma_setup,
830Sstevel@tonic-gate 	px_dma_allochdl,
840Sstevel@tonic-gate 	px_dma_freehdl,
850Sstevel@tonic-gate 	px_dma_bindhdl,
860Sstevel@tonic-gate 	px_dma_unbindhdl,
870Sstevel@tonic-gate 	px_lib_dma_sync,
880Sstevel@tonic-gate 	px_dma_win,
890Sstevel@tonic-gate 	px_dma_ctlops,
900Sstevel@tonic-gate 	px_ctlops,
910Sstevel@tonic-gate 	ddi_bus_prop_op,
920Sstevel@tonic-gate 	ndi_busop_get_eventcookie,
930Sstevel@tonic-gate 	ndi_busop_add_eventcall,
940Sstevel@tonic-gate 	ndi_busop_remove_eventcall,
950Sstevel@tonic-gate 	ndi_post_event,
960Sstevel@tonic-gate 	NULL,
970Sstevel@tonic-gate 	NULL,			/* (*bus_config)(); */
980Sstevel@tonic-gate 	NULL,			/* (*bus_unconfig)(); */
990Sstevel@tonic-gate 	px_fm_init_child,	/* (*bus_fm_init)(); */
1000Sstevel@tonic-gate 	NULL,			/* (*bus_fm_fini)(); */
10127Sjchu 	px_bus_enter,		/* (*bus_fm_access_enter)(); */
10227Sjchu 	px_bus_exit,		/* (*bus_fm_access_fini)(); */
1030Sstevel@tonic-gate 	pcie_bus_power,		/* (*bus_power)(); */
1040Sstevel@tonic-gate 	px_intr_ops		/* (*bus_intr_op)(); */
1050Sstevel@tonic-gate };
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate extern struct cb_ops px_cb_ops;
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate static struct dev_ops px_ops = {
1100Sstevel@tonic-gate 	DEVO_REV,
1110Sstevel@tonic-gate 	0,
1120Sstevel@tonic-gate 	px_info,
1130Sstevel@tonic-gate 	nulldev,
1140Sstevel@tonic-gate 	0,
1150Sstevel@tonic-gate 	px_attach,
1160Sstevel@tonic-gate 	px_detach,
1170Sstevel@tonic-gate 	nodev,
1180Sstevel@tonic-gate 	&px_cb_ops,
1190Sstevel@tonic-gate 	&px_bus_ops,
1200Sstevel@tonic-gate 	nulldev
1210Sstevel@tonic-gate };
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate /*
1240Sstevel@tonic-gate  * module definitions:
1250Sstevel@tonic-gate  */
1260Sstevel@tonic-gate #include <sys/modctl.h>
1270Sstevel@tonic-gate extern struct mod_ops mod_driverops;
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate static struct modldrv modldrv = {
1300Sstevel@tonic-gate 	&mod_driverops, 		/* Type of module - driver */
1310Sstevel@tonic-gate 	"PCI Express nexus driver %I%",	/* Name of module. */
1320Sstevel@tonic-gate 	&px_ops,			/* driver ops */
1330Sstevel@tonic-gate };
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate static struct modlinkage modlinkage = {
1360Sstevel@tonic-gate 	MODREV_1, (void *)&modldrv, NULL
1370Sstevel@tonic-gate };
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate /* driver soft state */
1400Sstevel@tonic-gate void *px_state_p;
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate int
1430Sstevel@tonic-gate _init(void)
1440Sstevel@tonic-gate {
1450Sstevel@tonic-gate 	int e;
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 	/*
1480Sstevel@tonic-gate 	 * Initialize per-px bus soft state pointer.
1490Sstevel@tonic-gate 	 */
1500Sstevel@tonic-gate 	e = ddi_soft_state_init(&px_state_p, sizeof (px_t), 1);
1510Sstevel@tonic-gate 	if (e != DDI_SUCCESS)
1520Sstevel@tonic-gate 		return (e);
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate 	/*
1550Sstevel@tonic-gate 	 * Install the module.
1560Sstevel@tonic-gate 	 */
1570Sstevel@tonic-gate 	e = mod_install(&modlinkage);
1580Sstevel@tonic-gate 	if (e != DDI_SUCCESS)
1590Sstevel@tonic-gate 		ddi_soft_state_fini(&px_state_p);
1600Sstevel@tonic-gate 	return (e);
1610Sstevel@tonic-gate }
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate int
1640Sstevel@tonic-gate _fini(void)
1650Sstevel@tonic-gate {
1660Sstevel@tonic-gate 	int e;
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 	/*
1690Sstevel@tonic-gate 	 * Remove the module.
1700Sstevel@tonic-gate 	 */
1710Sstevel@tonic-gate 	e = mod_remove(&modlinkage);
1720Sstevel@tonic-gate 	if (e != DDI_SUCCESS)
1730Sstevel@tonic-gate 		return (e);
1741865Sdilpreet 	/*
1751865Sdilpreet 	 * Destroy pci_target_queue, and set it to NULL.
1761865Sdilpreet 	 */
1771865Sdilpreet 	if (pci_target_queue)
1781865Sdilpreet 		errorq_destroy(pci_target_queue);
1791865Sdilpreet 
1801865Sdilpreet 	pci_target_queue = NULL;
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 	/* Free px soft state */
1830Sstevel@tonic-gate 	ddi_soft_state_fini(&px_state_p);
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate 	return (e);
1860Sstevel@tonic-gate }
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate int
1890Sstevel@tonic-gate _info(struct modinfo *modinfop)
1900Sstevel@tonic-gate {
1910Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
1920Sstevel@tonic-gate }
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate /* ARGSUSED */
1950Sstevel@tonic-gate static int
1960Sstevel@tonic-gate px_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
1970Sstevel@tonic-gate {
1980Sstevel@tonic-gate 	int	instance = getminor((dev_t)arg);
1990Sstevel@tonic-gate 	px_t	*px_p = INST_TO_STATE(instance);
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate 	/*
2020Sstevel@tonic-gate 	 * Allow hotplug to deal with ones it manages
2030Sstevel@tonic-gate 	 * Hot Plug will be done later.
2040Sstevel@tonic-gate 	 */
2051531Skini 	if (px_p && (px_p->px_dev_caps & PX_HOTPLUG_CAPABLE))
2060Sstevel@tonic-gate 		return (pcihp_info(dip, infocmd, arg, result));
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate 	/* non-hotplug or not attached */
2090Sstevel@tonic-gate 	switch (infocmd) {
2100Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
211671Skrishnae 		*result = (void *)(intptr_t)instance;
2120Sstevel@tonic-gate 		return (DDI_SUCCESS);
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
2150Sstevel@tonic-gate 		if (px_p == NULL)
2160Sstevel@tonic-gate 			return (DDI_FAILURE);
2170Sstevel@tonic-gate 		*result = (void *)px_p->px_dip;
2180Sstevel@tonic-gate 		return (DDI_SUCCESS);
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate 	default:
2210Sstevel@tonic-gate 		return (DDI_FAILURE);
2220Sstevel@tonic-gate 	}
2230Sstevel@tonic-gate }
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate /* device driver entry points */
2260Sstevel@tonic-gate /*
2270Sstevel@tonic-gate  * attach entry point:
2280Sstevel@tonic-gate  */
2290Sstevel@tonic-gate /*ARGSUSED*/
2300Sstevel@tonic-gate static int
2310Sstevel@tonic-gate px_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
2320Sstevel@tonic-gate {
2330Sstevel@tonic-gate 	px_t		*px_p;	/* per bus state pointer */
2340Sstevel@tonic-gate 	int		instance = DIP_TO_INST(dip);
2350Sstevel@tonic-gate 	int		ret = DDI_SUCCESS;
2360Sstevel@tonic-gate 	devhandle_t	dev_hdl = NULL;
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate 	switch (cmd) {
2390Sstevel@tonic-gate 	case DDI_ATTACH:
2400Sstevel@tonic-gate 		DBG(DBG_ATTACH, dip, "DDI_ATTACH\n");
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 		/*
2430Sstevel@tonic-gate 		 * Allocate and get the per-px soft state structure.
2440Sstevel@tonic-gate 		 */
2450Sstevel@tonic-gate 		if (ddi_soft_state_zalloc(px_state_p, instance)
2460Sstevel@tonic-gate 		    != DDI_SUCCESS) {
2470Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s%d: can't allocate px state",
2480Sstevel@tonic-gate 				ddi_driver_name(dip), instance);
2490Sstevel@tonic-gate 			goto err_bad_px_softstate;
2500Sstevel@tonic-gate 		}
2510Sstevel@tonic-gate 		px_p = INST_TO_STATE(instance);
2520Sstevel@tonic-gate 		px_p->px_dip = dip;
2530Sstevel@tonic-gate 		mutex_init(&px_p->px_mutex, NULL, MUTEX_DRIVER, NULL);
2540Sstevel@tonic-gate 		px_p->px_soft_state = PX_SOFT_STATE_CLOSED;
2550Sstevel@tonic-gate 		px_p->px_open_count = 0;
2560Sstevel@tonic-gate 
2571531Skini 		(void) ddi_prop_update_string(DDI_DEV_T_NONE, dip,
2581531Skini 				"device_type", "pciex");
2590Sstevel@tonic-gate 		/*
2600Sstevel@tonic-gate 		 * Get key properties of the pci bridge node and
2610Sstevel@tonic-gate 		 * determine it's type (psycho, schizo, etc ...).
2620Sstevel@tonic-gate 		 */
2630Sstevel@tonic-gate 		if (px_get_props(px_p, dip) == DDI_FAILURE)
2640Sstevel@tonic-gate 			goto err_bad_px_prop;
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate 		if (px_lib_dev_init(dip, &dev_hdl) != DDI_SUCCESS)
2670Sstevel@tonic-gate 			goto err_bad_dev_init;
2680Sstevel@tonic-gate 
2691725Segillett 		/* Initialize device handle */
2700Sstevel@tonic-gate 		px_p->px_dev_hdl = dev_hdl;
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate 		/*
2730Sstevel@tonic-gate 		 * Initialize interrupt block.  Note that this
2740Sstevel@tonic-gate 		 * initialize error handling for the PEC as well.
2750Sstevel@tonic-gate 		 */
2760Sstevel@tonic-gate 		if ((ret = px_ib_attach(px_p)) != DDI_SUCCESS)
2770Sstevel@tonic-gate 			goto err_bad_ib;
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate 		if (px_cb_attach(px_p) != DDI_SUCCESS)
2800Sstevel@tonic-gate 			goto err_bad_cb;
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate 		/*
2830Sstevel@tonic-gate 		 * Start creating the modules.
2840Sstevel@tonic-gate 		 * Note that attach() routines should
2850Sstevel@tonic-gate 		 * register and enable their own interrupts.
2860Sstevel@tonic-gate 		 */
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate 		if ((px_mmu_attach(px_p)) != DDI_SUCCESS)
2890Sstevel@tonic-gate 			goto err_bad_mmu;
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate 		if ((px_msiq_attach(px_p)) != DDI_SUCCESS)
2920Sstevel@tonic-gate 			goto err_bad_msiq;
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate 		if ((px_msi_attach(px_p)) != DDI_SUCCESS)
2950Sstevel@tonic-gate 			goto err_bad_msi;
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate 		if ((px_pec_attach(px_p)) != DDI_SUCCESS)
2980Sstevel@tonic-gate 			goto err_bad_pec;
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate 		if ((px_dma_attach(px_p)) != DDI_SUCCESS)
3012510Sjchu 			goto err_bad_dma; /* nothing to uninitialize on DMA */
3020Sstevel@tonic-gate 
3032587Spjha 		if ((px_fm_attach(px_p)) != DDI_SUCCESS)
3042587Spjha 			goto err_bad_dma;
3052587Spjha 
3060Sstevel@tonic-gate 		/*
3070Sstevel@tonic-gate 		 * All of the error handlers have been registered
3080Sstevel@tonic-gate 		 * by now so it's time to activate the interrupt.
3090Sstevel@tonic-gate 		 */
31027Sjchu 		if ((ret = px_err_add_intr(&px_p->px_fault)) != DDI_SUCCESS)
3112587Spjha 			goto err_bad_intr;
3120Sstevel@tonic-gate 
3131531Skini 		(void) px_init_hotplug(px_p);
3141531Skini 
3150Sstevel@tonic-gate 		/*
3160Sstevel@tonic-gate 		 * Create the "devctl" node for hotplug and pcitool support.
3170Sstevel@tonic-gate 		 * For non-hotplug bus, we still need ":devctl" to
3180Sstevel@tonic-gate 		 * support DEVCTL_DEVICE_* and DEVCTL_BUS_* ioctls.
3190Sstevel@tonic-gate 		 */
3200Sstevel@tonic-gate 		if (ddi_create_minor_node(dip, "devctl", S_IFCHR,
3210Sstevel@tonic-gate 		    PCIHP_AP_MINOR_NUM(instance, PCIHP_DEVCTL_MINOR),
3220Sstevel@tonic-gate 		    DDI_NT_NEXUS, 0) != DDI_SUCCESS) {
3230Sstevel@tonic-gate 			goto err_bad_devctl_node;
3240Sstevel@tonic-gate 		}
325624Sschwartz 
326624Sschwartz 		if (pxtool_init(dip) != DDI_SUCCESS)
327624Sschwartz 			goto err_bad_pcitool_node;
328624Sschwartz 
3290Sstevel@tonic-gate 		/*
3300Sstevel@tonic-gate 		 * power management setup. Even if it fails, attach will
3310Sstevel@tonic-gate 		 * succeed as this is a optional feature. Since we are
3320Sstevel@tonic-gate 		 * always at full power, this is not critical.
3330Sstevel@tonic-gate 		 */
3340Sstevel@tonic-gate 		if (pwr_common_setup(dip) != DDI_SUCCESS) {
3350Sstevel@tonic-gate 			DBG(DBG_PWR, dip, "pwr_common_setup failed\n");
3360Sstevel@tonic-gate 		} else if (px_pwr_setup(dip) != DDI_SUCCESS) {
3370Sstevel@tonic-gate 			DBG(DBG_PWR, dip, "px_pwr_setup failed \n");
3380Sstevel@tonic-gate 			pwr_common_teardown(dip);
3390Sstevel@tonic-gate 		}
3400Sstevel@tonic-gate 
341435Sjchu 		/*
342435Sjchu 		 * add cpr callback
343435Sjchu 		 */
344435Sjchu 		px_cpr_add_callb(px_p);
345435Sjchu 
3460Sstevel@tonic-gate 		ddi_report_dev(dip);
3470Sstevel@tonic-gate 
3480Sstevel@tonic-gate 		px_p->px_state = PX_ATTACHED;
3490Sstevel@tonic-gate 		DBG(DBG_ATTACH, dip, "attach success\n");
3500Sstevel@tonic-gate 		break;
3510Sstevel@tonic-gate 
352624Sschwartz err_bad_pcitool_node:
353624Sschwartz 		ddi_remove_minor_node(dip, "devctl");
3540Sstevel@tonic-gate err_bad_devctl_node:
3552587Spjha 		px_err_rem_intr(&px_p->px_fault);
3562587Spjha err_bad_intr:
3572510Sjchu 		px_fm_detach(px_p);
3582510Sjchu err_bad_dma:
3590Sstevel@tonic-gate 		px_pec_detach(px_p);
3600Sstevel@tonic-gate err_bad_pec:
3610Sstevel@tonic-gate 		px_msi_detach(px_p);
3620Sstevel@tonic-gate err_bad_msi:
3630Sstevel@tonic-gate 		px_msiq_detach(px_p);
3640Sstevel@tonic-gate err_bad_msiq:
3650Sstevel@tonic-gate 		px_mmu_detach(px_p);
3660Sstevel@tonic-gate err_bad_mmu:
3670Sstevel@tonic-gate 		px_cb_detach(px_p);
3680Sstevel@tonic-gate err_bad_cb:
3690Sstevel@tonic-gate 		px_ib_detach(px_p);
3700Sstevel@tonic-gate err_bad_ib:
3710Sstevel@tonic-gate 		(void) px_lib_dev_fini(dip);
3720Sstevel@tonic-gate err_bad_dev_init:
3730Sstevel@tonic-gate 		px_free_props(px_p);
3740Sstevel@tonic-gate err_bad_px_prop:
3750Sstevel@tonic-gate 		mutex_destroy(&px_p->px_mutex);
3760Sstevel@tonic-gate 		ddi_soft_state_free(px_state_p, instance);
3770Sstevel@tonic-gate err_bad_px_softstate:
3780Sstevel@tonic-gate 		ret = DDI_FAILURE;
3790Sstevel@tonic-gate 		break;
3800Sstevel@tonic-gate 
3810Sstevel@tonic-gate 	case DDI_RESUME:
3820Sstevel@tonic-gate 		DBG(DBG_ATTACH, dip, "DDI_RESUME\n");
3830Sstevel@tonic-gate 
3840Sstevel@tonic-gate 		px_p = INST_TO_STATE(instance);
3850Sstevel@tonic-gate 
3860Sstevel@tonic-gate 		mutex_enter(&px_p->px_mutex);
3870Sstevel@tonic-gate 
3880Sstevel@tonic-gate 		/* suspend might have not succeeded */
3890Sstevel@tonic-gate 		if (px_p->px_state != PX_SUSPENDED) {
3900Sstevel@tonic-gate 			DBG(DBG_ATTACH, px_p->px_dip,
3910Sstevel@tonic-gate 			    "instance NOT suspended\n");
3920Sstevel@tonic-gate 			ret = DDI_FAILURE;
3930Sstevel@tonic-gate 			break;
3940Sstevel@tonic-gate 		}
3950Sstevel@tonic-gate 
3962588Segillett 		px_msiq_resume(px_p);
3970Sstevel@tonic-gate 		px_lib_resume(dip);
3980Sstevel@tonic-gate 		(void) pcie_pwr_resume(dip);
3990Sstevel@tonic-gate 		px_p->px_state = PX_ATTACHED;
4000Sstevel@tonic-gate 
4010Sstevel@tonic-gate 		mutex_exit(&px_p->px_mutex);
4020Sstevel@tonic-gate 
4030Sstevel@tonic-gate 		break;
4040Sstevel@tonic-gate 	default:
4050Sstevel@tonic-gate 		DBG(DBG_ATTACH, dip, "unsupported attach op\n");
4060Sstevel@tonic-gate 		ret = DDI_FAILURE;
4070Sstevel@tonic-gate 		break;
4080Sstevel@tonic-gate 	}
4090Sstevel@tonic-gate 
4100Sstevel@tonic-gate 	return (ret);
4110Sstevel@tonic-gate }
4120Sstevel@tonic-gate 
4130Sstevel@tonic-gate /*
4140Sstevel@tonic-gate  * detach entry point:
4150Sstevel@tonic-gate  */
4160Sstevel@tonic-gate /*ARGSUSED*/
4170Sstevel@tonic-gate static int
4180Sstevel@tonic-gate px_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
4190Sstevel@tonic-gate {
4200Sstevel@tonic-gate 	int instance = ddi_get_instance(dip);
4210Sstevel@tonic-gate 	px_t *px_p = INST_TO_STATE(instance);
4220Sstevel@tonic-gate 	int ret;
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate 	/*
4250Sstevel@tonic-gate 	 * Make sure we are currently attached
4260Sstevel@tonic-gate 	 */
4270Sstevel@tonic-gate 	if (px_p->px_state != PX_ATTACHED) {
4281648Sjchu 		DBG(DBG_DETACH, dip, "Instance not attached\n");
4290Sstevel@tonic-gate 		return (DDI_FAILURE);
4300Sstevel@tonic-gate 	}
4310Sstevel@tonic-gate 
4320Sstevel@tonic-gate 	mutex_enter(&px_p->px_mutex);
4330Sstevel@tonic-gate 
4340Sstevel@tonic-gate 	switch (cmd) {
4350Sstevel@tonic-gate 	case DDI_DETACH:
4360Sstevel@tonic-gate 		DBG(DBG_DETACH, dip, "DDI_DETACH\n");
4370Sstevel@tonic-gate 
438435Sjchu 		/*
439435Sjchu 		 * remove cpr callback
440435Sjchu 		 */
441435Sjchu 		px_cpr_rem_callb(px_p);
442435Sjchu 
4431531Skini 		if (px_p->px_dev_caps & PX_HOTPLUG_CAPABLE)
4441531Skini 			if (px_uninit_hotplug(dip) != DDI_SUCCESS) {
4450Sstevel@tonic-gate 				mutex_exit(&px_p->px_mutex);
4460Sstevel@tonic-gate 				return (DDI_FAILURE);
4470Sstevel@tonic-gate 			}
4480Sstevel@tonic-gate 
4490Sstevel@tonic-gate 		/*
4500Sstevel@tonic-gate 		 * things which used to be done in obj_destroy
4510Sstevel@tonic-gate 		 * are now in-lined here.
4520Sstevel@tonic-gate 		 */
4530Sstevel@tonic-gate 
4540Sstevel@tonic-gate 		px_p->px_state = PX_DETACHED;
4550Sstevel@tonic-gate 
456624Sschwartz 		pxtool_uninit(dip);
457624Sschwartz 
4580Sstevel@tonic-gate 		ddi_remove_minor_node(dip, "devctl");
4592587Spjha 		px_err_rem_intr(&px_p->px_fault);
4602510Sjchu 		px_fm_detach(px_p);
4610Sstevel@tonic-gate 		px_pec_detach(px_p);
462965Sgovinda 		px_pwr_teardown(dip);
463965Sgovinda 		pwr_common_teardown(dip);
4640Sstevel@tonic-gate 		px_msi_detach(px_p);
4650Sstevel@tonic-gate 		px_msiq_detach(px_p);
4660Sstevel@tonic-gate 		px_mmu_detach(px_p);
4670Sstevel@tonic-gate 		px_cb_detach(px_p);
4680Sstevel@tonic-gate 		px_ib_detach(px_p);
4690Sstevel@tonic-gate 		(void) px_lib_dev_fini(dip);
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate 		/*
4720Sstevel@tonic-gate 		 * Free the px soft state structure and the rest of the
4730Sstevel@tonic-gate 		 * resources it's using.
4740Sstevel@tonic-gate 		 */
4750Sstevel@tonic-gate 		px_free_props(px_p);
4760Sstevel@tonic-gate 		mutex_exit(&px_p->px_mutex);
4770Sstevel@tonic-gate 		mutex_destroy(&px_p->px_mutex);
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate 		/* Free the interrupt-priorities prop if we created it. */ {
4800Sstevel@tonic-gate 			int len;
4810Sstevel@tonic-gate 
4820Sstevel@tonic-gate 			if (ddi_getproplen(DDI_DEV_T_ANY, dip,
4830Sstevel@tonic-gate 			    DDI_PROP_NOTPROM | DDI_PROP_DONTPASS,
4840Sstevel@tonic-gate 			    "interrupt-priorities", &len) == DDI_PROP_SUCCESS)
4850Sstevel@tonic-gate 				(void) ddi_prop_remove(DDI_DEV_T_NONE, dip,
4860Sstevel@tonic-gate 				    "interrupt-priorities");
4870Sstevel@tonic-gate 		}
4880Sstevel@tonic-gate 
4890Sstevel@tonic-gate 		px_p->px_dev_hdl = NULL;
4901064Sschwartz 		ddi_soft_state_free(px_state_p, instance);
4910Sstevel@tonic-gate 
4920Sstevel@tonic-gate 		return (DDI_SUCCESS);
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate 	case DDI_SUSPEND:
4950Sstevel@tonic-gate 		if (pcie_pwr_suspend(dip) != DDI_SUCCESS) {
4960Sstevel@tonic-gate 			mutex_exit(&px_p->px_mutex);
4970Sstevel@tonic-gate 			return (DDI_FAILURE);
4980Sstevel@tonic-gate 		}
4990Sstevel@tonic-gate 		if ((ret = px_lib_suspend(dip)) == DDI_SUCCESS)
5000Sstevel@tonic-gate 			px_p->px_state = PX_SUSPENDED;
5010Sstevel@tonic-gate 		mutex_exit(&px_p->px_mutex);
5020Sstevel@tonic-gate 
5030Sstevel@tonic-gate 		return (ret);
5040Sstevel@tonic-gate 
5050Sstevel@tonic-gate 	default:
5060Sstevel@tonic-gate 		DBG(DBG_DETACH, dip, "unsupported detach op\n");
5070Sstevel@tonic-gate 		mutex_exit(&px_p->px_mutex);
5080Sstevel@tonic-gate 		return (DDI_FAILURE);
5090Sstevel@tonic-gate 	}
5100Sstevel@tonic-gate }
5110Sstevel@tonic-gate 
5121648Sjchu int
5131648Sjchu px_cb_attach(px_t *px_p)
5141648Sjchu {
5151648Sjchu 	px_fault_t	*fault_p = &px_p->px_cb_fault;
5161648Sjchu 	dev_info_t	*dip = px_p->px_dip;
5171648Sjchu 	sysino_t	sysino;
5181648Sjchu 
5191648Sjchu 	if (px_lib_intr_devino_to_sysino(dip,
5201648Sjchu 	    px_p->px_inos[PX_INTR_XBC], &sysino) != DDI_SUCCESS)
5211648Sjchu 		return (DDI_FAILURE);
5221648Sjchu 
5231648Sjchu 	fault_p->px_fh_dip = dip;
5241648Sjchu 	fault_p->px_fh_sysino = sysino;
5251648Sjchu 	fault_p->px_err_func = px_err_cb_intr;
5261648Sjchu 	fault_p->px_intr_ino = px_p->px_inos[PX_INTR_XBC];
5271648Sjchu 
5281648Sjchu 	return (px_cb_add_intr(fault_p));
5291648Sjchu }
5301648Sjchu 
5311648Sjchu void
5321648Sjchu px_cb_detach(px_t *px_p)
5331648Sjchu {
5341648Sjchu 	px_cb_rem_intr(&px_p->px_cb_fault);
5351648Sjchu }
5361648Sjchu 
5370Sstevel@tonic-gate /*
5380Sstevel@tonic-gate  * power management related initialization specific to px
5390Sstevel@tonic-gate  * called by px_attach()
5400Sstevel@tonic-gate  */
5410Sstevel@tonic-gate static int
5420Sstevel@tonic-gate px_pwr_setup(dev_info_t *dip)
5430Sstevel@tonic-gate {
5440Sstevel@tonic-gate 	pcie_pwr_t *pwr_p;
545118Sjchu 	int instance = ddi_get_instance(dip);
546118Sjchu 	px_t *px_p = INST_TO_STATE(instance);
5470Sstevel@tonic-gate 	ddi_intr_handle_impl_t hdl;
5480Sstevel@tonic-gate 
5490Sstevel@tonic-gate 	ASSERT(PCIE_PMINFO(dip));
5500Sstevel@tonic-gate 	pwr_p = PCIE_NEXUS_PMINFO(dip);
5510Sstevel@tonic-gate 	ASSERT(pwr_p);
5520Sstevel@tonic-gate 
5530Sstevel@tonic-gate 	/*
5540Sstevel@tonic-gate 	 * indicate support LDI (Layered Driver Interface)
5550Sstevel@tonic-gate 	 * Create the property, if it is not already there
5560Sstevel@tonic-gate 	 */
5570Sstevel@tonic-gate 	if (!ddi_prop_exists(DDI_DEV_T_NONE, dip, DDI_PROP_DONTPASS,
5580Sstevel@tonic-gate 	    DDI_KERNEL_IOCTL)) {
5590Sstevel@tonic-gate 		if (ddi_prop_create(DDI_DEV_T_NONE, dip, DDI_PROP_CANSLEEP,
5600Sstevel@tonic-gate 		    DDI_KERNEL_IOCTL, NULL, 0) != DDI_PROP_SUCCESS) {
5610Sstevel@tonic-gate 			DBG(DBG_PWR, dip, "can't create kernel ioctl prop\n");
5620Sstevel@tonic-gate 			return (DDI_FAILURE);
5630Sstevel@tonic-gate 		}
5640Sstevel@tonic-gate 	}
5650Sstevel@tonic-gate 	/* No support for device PM. We are always at full power */
5660Sstevel@tonic-gate 	pwr_p->pwr_func_lvl = PM_LEVEL_D0;
5670Sstevel@tonic-gate 
568118Sjchu 	mutex_init(&px_p->px_l23ready_lock, NULL, MUTEX_DRIVER,
569693Sgovinda 	    DDI_INTR_PRI(px_pwr_pil));
570118Sjchu 	cv_init(&px_p->px_l23ready_cv, NULL, CV_DRIVER, NULL);
571118Sjchu 
5721725Segillett 	/* Initialize handle */
5731725Segillett 	bzero(&hdl, sizeof (ddi_intr_handle_impl_t));
574118Sjchu 	hdl.ih_cb_arg1 = px_p;
5750Sstevel@tonic-gate 	hdl.ih_ver = DDI_INTR_VERSION;
5760Sstevel@tonic-gate 	hdl.ih_state = DDI_IHDL_STATE_ALLOC;
5770Sstevel@tonic-gate 	hdl.ih_dip = dip;
5780Sstevel@tonic-gate 	hdl.ih_pri = px_pwr_pil;
5790Sstevel@tonic-gate 
5800Sstevel@tonic-gate 	/* Add PME_TO_ACK message handler */
581118Sjchu 	hdl.ih_cb_func = (ddi_intr_handler_t *)px_pmeq_intr;
5820Sstevel@tonic-gate 	if (px_add_msiq_intr(dip, dip, &hdl, MSG_REC,
583118Sjchu 	    (msgcode_t)PCIE_PME_ACK_MSG, &px_p->px_pm_msiq_id) != DDI_SUCCESS) {
584118Sjchu 		DBG(DBG_PWR, dip, "px_pwr_setup: couldn't add "
585118Sjchu 		    " PME_TO_ACK intr\n");
5861147Sjchu 		goto pwr_setup_err1;
5870Sstevel@tonic-gate 	}
588118Sjchu 	px_lib_msg_setmsiq(dip, PCIE_PME_ACK_MSG, px_p->px_pm_msiq_id);
5890Sstevel@tonic-gate 	px_lib_msg_setvalid(dip, PCIE_PME_ACK_MSG, PCIE_MSG_VALID);
5900Sstevel@tonic-gate 
591909Segillett 	if (px_ib_update_intr_state(px_p, px_p->px_dip, hdl.ih_inum,
5922973Sgovinda 	    px_msiqid_to_devino(px_p, px_p->px_pm_msiq_id), px_pwr_pil,
593909Segillett 	    PX_INTR_STATE_ENABLE, MSG_REC, PCIE_PME_ACK_MSG) != DDI_SUCCESS) {
594909Segillett 		DBG(DBG_PWR, dip, "px_pwr_setup: PME_TO_ACK update interrupt"
595909Segillett 		    " state failed\n");
596909Segillett 		goto px_pwrsetup_err_state;
597909Segillett 	}
598909Segillett 
5990Sstevel@tonic-gate 	return (DDI_SUCCESS);
6000Sstevel@tonic-gate 
601909Segillett px_pwrsetup_err_state:
602909Segillett 	px_lib_msg_setvalid(dip, PCIE_PME_ACK_MSG, PCIE_MSG_INVALID);
603909Segillett 	(void) px_rem_msiq_intr(dip, dip, &hdl, MSG_REC, PCIE_PME_ACK_MSG,
604909Segillett 	    px_p->px_pm_msiq_id);
605118Sjchu pwr_setup_err1:
606118Sjchu 	mutex_destroy(&px_p->px_l23ready_lock);
607118Sjchu 	cv_destroy(&px_p->px_l23ready_cv);
608118Sjchu 
6090Sstevel@tonic-gate 	return (DDI_FAILURE);
6100Sstevel@tonic-gate }
6110Sstevel@tonic-gate 
6120Sstevel@tonic-gate /*
6130Sstevel@tonic-gate  * undo whatever is done in px_pwr_setup. called by px_detach()
6140Sstevel@tonic-gate  */
6150Sstevel@tonic-gate static void
6160Sstevel@tonic-gate px_pwr_teardown(dev_info_t *dip)
6170Sstevel@tonic-gate {
618118Sjchu 	int instance = ddi_get_instance(dip);
619118Sjchu 	px_t *px_p = INST_TO_STATE(instance);
620118Sjchu 	ddi_intr_handle_impl_t	hdl;
6210Sstevel@tonic-gate 
622118Sjchu 	if (!PCIE_PMINFO(dip) || !PCIE_NEXUS_PMINFO(dip))
6230Sstevel@tonic-gate 		return;
6240Sstevel@tonic-gate 
6251725Segillett 	/* Initialize handle */
6261725Segillett 	bzero(&hdl, sizeof (ddi_intr_handle_impl_t));
6270Sstevel@tonic-gate 	hdl.ih_ver = DDI_INTR_VERSION;
6280Sstevel@tonic-gate 	hdl.ih_state = DDI_IHDL_STATE_ALLOC;
6290Sstevel@tonic-gate 	hdl.ih_dip = dip;
630*3162Sgovinda 	hdl.ih_pri = px_pwr_pil;
6310Sstevel@tonic-gate 
6320Sstevel@tonic-gate 	px_lib_msg_setvalid(dip, PCIE_PME_ACK_MSG, PCIE_MSG_INVALID);
6330Sstevel@tonic-gate 	(void) px_rem_msiq_intr(dip, dip, &hdl, MSG_REC, PCIE_PME_ACK_MSG,
634118Sjchu 	    px_p->px_pm_msiq_id);
635118Sjchu 
636909Segillett 	(void) px_ib_update_intr_state(px_p, px_p->px_dip, hdl.ih_inum,
6372973Sgovinda 	    px_msiqid_to_devino(px_p, px_p->px_pm_msiq_id), px_pwr_pil,
638909Segillett 	    PX_INTR_STATE_DISABLE, MSG_REC, PCIE_PME_ACK_MSG);
639909Segillett 
6402840Scarlsonj 	px_p->px_pm_msiq_id = (msiqid_t)-1;
6410Sstevel@tonic-gate 
642118Sjchu 	cv_destroy(&px_p->px_l23ready_cv);
643118Sjchu 	mutex_destroy(&px_p->px_l23ready_lock);
6440Sstevel@tonic-gate }
6450Sstevel@tonic-gate 
6460Sstevel@tonic-gate /* bus driver entry points */
6470Sstevel@tonic-gate 
6480Sstevel@tonic-gate /*
6490Sstevel@tonic-gate  * bus map entry point:
6500Sstevel@tonic-gate  *
6510Sstevel@tonic-gate  * 	if map request is for an rnumber
6520Sstevel@tonic-gate  *		get the corresponding regspec from device node
6530Sstevel@tonic-gate  * 	build a new regspec in our parent's format
6540Sstevel@tonic-gate  *	build a new map_req with the new regspec
6550Sstevel@tonic-gate  *	call up the tree to complete the mapping
6560Sstevel@tonic-gate  */
6570Sstevel@tonic-gate int
6580Sstevel@tonic-gate px_map(dev_info_t *dip, dev_info_t *rdip, ddi_map_req_t *mp,
6590Sstevel@tonic-gate 	off_t off, off_t len, caddr_t *addrp)
6600Sstevel@tonic-gate {
6610Sstevel@tonic-gate 	px_t *px_p = DIP_TO_STATE(dip);
6620Sstevel@tonic-gate 	struct regspec p_regspec;
6630Sstevel@tonic-gate 	ddi_map_req_t p_mapreq;
6640Sstevel@tonic-gate 	int reglen, rval, r_no;
6650Sstevel@tonic-gate 	pci_regspec_t reloc_reg, *rp = &reloc_reg;
6660Sstevel@tonic-gate 
6670Sstevel@tonic-gate 	DBG(DBG_MAP, dip, "rdip=%s%d:",
6680Sstevel@tonic-gate 		ddi_driver_name(rdip), ddi_get_instance(rdip));
6690Sstevel@tonic-gate 
6700Sstevel@tonic-gate 	if (mp->map_flags & DDI_MF_USER_MAPPING)
6710Sstevel@tonic-gate 		return (DDI_ME_UNIMPLEMENTED);
6720Sstevel@tonic-gate 
6730Sstevel@tonic-gate 	switch (mp->map_type) {
6740Sstevel@tonic-gate 	case DDI_MT_REGSPEC:
6750Sstevel@tonic-gate 		reloc_reg = *(pci_regspec_t *)mp->map_obj.rp;	/* dup whole */
6760Sstevel@tonic-gate 		break;
6770Sstevel@tonic-gate 
6780Sstevel@tonic-gate 	case DDI_MT_RNUMBER:
6790Sstevel@tonic-gate 		r_no = mp->map_obj.rnumber;
6800Sstevel@tonic-gate 		DBG(DBG_MAP | DBG_CONT, dip, " r#=%x", r_no);
6810Sstevel@tonic-gate 
682506Scth 		if (ddi_getlongprop(DDI_DEV_T_ANY, rdip, DDI_PROP_DONTPASS,
6830Sstevel@tonic-gate 			"reg", (caddr_t)&rp, &reglen) != DDI_SUCCESS)
6840Sstevel@tonic-gate 				return (DDI_ME_RNUMBER_RANGE);
6850Sstevel@tonic-gate 
6860Sstevel@tonic-gate 		if (r_no < 0 || r_no >= reglen / sizeof (pci_regspec_t)) {
6870Sstevel@tonic-gate 			kmem_free(rp, reglen);
6880Sstevel@tonic-gate 			return (DDI_ME_RNUMBER_RANGE);
6890Sstevel@tonic-gate 		}
6900Sstevel@tonic-gate 		rp += r_no;
6910Sstevel@tonic-gate 		break;
6920Sstevel@tonic-gate 
6930Sstevel@tonic-gate 	default:
6940Sstevel@tonic-gate 		return (DDI_ME_INVAL);
6950Sstevel@tonic-gate 	}
6960Sstevel@tonic-gate 	DBG(DBG_MAP | DBG_CONT, dip, "\n");
6970Sstevel@tonic-gate 
6980Sstevel@tonic-gate 	if ((rp->pci_phys_hi & PCI_REG_ADDR_M) == PCI_ADDR_CONFIG) {
6990Sstevel@tonic-gate 		/*
7000Sstevel@tonic-gate 		 * There may be a need to differentiate between PCI
7010Sstevel@tonic-gate 		 * and PCI-Ex devices so the following range check is
7020Sstevel@tonic-gate 		 * done correctly, depending on the implementation of
7030Sstevel@tonic-gate 		 * px_pci bridge nexus driver.
7040Sstevel@tonic-gate 		 */
7050Sstevel@tonic-gate 		if ((off >= PCIE_CONF_HDR_SIZE) ||
7060Sstevel@tonic-gate 				(len > PCIE_CONF_HDR_SIZE) ||
7070Sstevel@tonic-gate 				(off + len > PCIE_CONF_HDR_SIZE))
7080Sstevel@tonic-gate 			return (DDI_ME_INVAL);
7090Sstevel@tonic-gate 		/*
7100Sstevel@tonic-gate 		 * the following function returning a DDI_FAILURE assumes
7110Sstevel@tonic-gate 		 * that there are no virtual config space access services
7120Sstevel@tonic-gate 		 * defined in this layer. Otherwise it is availed right
7130Sstevel@tonic-gate 		 * here and we return.
7140Sstevel@tonic-gate 		 */
7150Sstevel@tonic-gate 		rval = px_lib_map_vconfig(dip, mp, off, rp, addrp);
7160Sstevel@tonic-gate 		if (rval == DDI_SUCCESS)
7170Sstevel@tonic-gate 			goto done;
7180Sstevel@tonic-gate 	}
7190Sstevel@tonic-gate 
7200Sstevel@tonic-gate 	/*
7210Sstevel@tonic-gate 	 * No virtual config space services or we are mapping
7220Sstevel@tonic-gate 	 * a region of memory mapped config/IO/memory space, so proceed
7230Sstevel@tonic-gate 	 * to the parent.
7240Sstevel@tonic-gate 	 */
7250Sstevel@tonic-gate 
7260Sstevel@tonic-gate 	/* relocate within 64-bit pci space through "assigned-addresses" */
7270Sstevel@tonic-gate 	if (rval = px_reloc_reg(dip, rdip, px_p, rp))
7280Sstevel@tonic-gate 		goto done;
7290Sstevel@tonic-gate 
7300Sstevel@tonic-gate 	if (len)	/* adjust regspec according to mapping request */
7310Sstevel@tonic-gate 		rp->pci_size_low = len;	/* MIN ? */
7320Sstevel@tonic-gate 	rp->pci_phys_low += off;
7330Sstevel@tonic-gate 
7340Sstevel@tonic-gate 	/* translate relocated pci regspec into parent space through "ranges" */
7350Sstevel@tonic-gate 	if (rval = px_xlate_reg(px_p, rp, &p_regspec))
7360Sstevel@tonic-gate 		goto done;
7370Sstevel@tonic-gate 
7380Sstevel@tonic-gate 	p_mapreq = *mp;		/* dup the whole structure */
7390Sstevel@tonic-gate 	p_mapreq.map_type = DDI_MT_REGSPEC;
7400Sstevel@tonic-gate 	p_mapreq.map_obj.rp = &p_regspec;
741677Sjchu 	px_lib_map_attr_check(&p_mapreq);
7420Sstevel@tonic-gate 	rval = ddi_map(dip, &p_mapreq, 0, 0, addrp);
7430Sstevel@tonic-gate 
7440Sstevel@tonic-gate 	if (rval == DDI_SUCCESS) {
7450Sstevel@tonic-gate 		/*
7460Sstevel@tonic-gate 		 * Set-up access functions for FM access error capable drivers.
7470Sstevel@tonic-gate 		 */
7480Sstevel@tonic-gate 		if (DDI_FM_ACC_ERR_CAP(ddi_fm_capable(rdip)) &&
7490Sstevel@tonic-gate 		    mp->map_handlep->ah_acc.devacc_attr_access !=
7500Sstevel@tonic-gate 		    DDI_DEFAULT_ACC)
7510Sstevel@tonic-gate 			px_fm_acc_setup(mp, rdip);
7520Sstevel@tonic-gate 	}
7530Sstevel@tonic-gate 
7540Sstevel@tonic-gate done:
7550Sstevel@tonic-gate 	if (mp->map_type == DDI_MT_RNUMBER)
7560Sstevel@tonic-gate 		kmem_free(rp - r_no, reglen);
7570Sstevel@tonic-gate 
7580Sstevel@tonic-gate 	return (rval);
7590Sstevel@tonic-gate }
7600Sstevel@tonic-gate 
7610Sstevel@tonic-gate /*
7620Sstevel@tonic-gate  * bus dma map entry point
7630Sstevel@tonic-gate  * return value:
7640Sstevel@tonic-gate  *	DDI_DMA_PARTIAL_MAP	 1
7650Sstevel@tonic-gate  *	DDI_DMA_MAPOK		 0
7660Sstevel@tonic-gate  *	DDI_DMA_MAPPED		 0
7670Sstevel@tonic-gate  *	DDI_DMA_NORESOURCES	-1
7680Sstevel@tonic-gate  *	DDI_DMA_NOMAPPING	-2
7690Sstevel@tonic-gate  *	DDI_DMA_TOOBIG		-3
7700Sstevel@tonic-gate  */
7710Sstevel@tonic-gate int
7720Sstevel@tonic-gate px_dma_setup(dev_info_t *dip, dev_info_t *rdip, ddi_dma_req_t *dmareq,
7730Sstevel@tonic-gate 	ddi_dma_handle_t *handlep)
7740Sstevel@tonic-gate {
7750Sstevel@tonic-gate 	px_t *px_p = DIP_TO_STATE(dip);
7760Sstevel@tonic-gate 	px_mmu_t *mmu_p = px_p->px_mmu_p;
7770Sstevel@tonic-gate 	ddi_dma_impl_t *mp;
7780Sstevel@tonic-gate 	int ret;
7790Sstevel@tonic-gate 
7800Sstevel@tonic-gate 	DBG(DBG_DMA_MAP, dip, "mapping - rdip=%s%d type=%s\n",
7810Sstevel@tonic-gate 		ddi_driver_name(rdip), ddi_get_instance(rdip),
7820Sstevel@tonic-gate 		handlep ? "alloc" : "advisory");
7830Sstevel@tonic-gate 
7840Sstevel@tonic-gate 	if (!(mp = px_dma_lmts2hdl(dip, rdip, mmu_p, dmareq)))
7850Sstevel@tonic-gate 		return (DDI_DMA_NORESOURCES);
7860Sstevel@tonic-gate 	if (mp == (ddi_dma_impl_t *)DDI_DMA_NOMAPPING)
7870Sstevel@tonic-gate 		return (DDI_DMA_NOMAPPING);
7880Sstevel@tonic-gate 	if (ret = px_dma_type(px_p, dmareq, mp))
7890Sstevel@tonic-gate 		goto freehandle;
7900Sstevel@tonic-gate 	if (ret = px_dma_pfn(px_p, dmareq, mp))
7910Sstevel@tonic-gate 		goto freehandle;
7920Sstevel@tonic-gate 
7930Sstevel@tonic-gate 	switch (PX_DMA_TYPE(mp)) {
794909Segillett 	case PX_DMAI_FLAGS_DVMA:	/* LINTED E_EQUALITY_NOT_ASSIGNMENT */
7950Sstevel@tonic-gate 		if ((ret = px_dvma_win(px_p, dmareq, mp)) || !handlep)
7960Sstevel@tonic-gate 			goto freehandle;
7970Sstevel@tonic-gate 		if (!PX_DMA_CANCACHE(mp)) {	/* try fast track */
7980Sstevel@tonic-gate 			if (PX_DMA_CANFAST(mp)) {
7990Sstevel@tonic-gate 				if (!px_dvma_map_fast(mmu_p, mp))
8000Sstevel@tonic-gate 					break;
8010Sstevel@tonic-gate 			/* LINTED E_NOP_ELSE_STMT */
8020Sstevel@tonic-gate 			} else {
8030Sstevel@tonic-gate 				PX_DVMA_FASTTRAK_PROF(mp);
8040Sstevel@tonic-gate 			}
8050Sstevel@tonic-gate 		}
8060Sstevel@tonic-gate 		if (ret = px_dvma_map(mp, dmareq, mmu_p))
8070Sstevel@tonic-gate 			goto freehandle;
8080Sstevel@tonic-gate 		break;
809909Segillett 	case PX_DMAI_FLAGS_PTP:	/* LINTED E_EQUALITY_NOT_ASSIGNMENT */
8100Sstevel@tonic-gate 		if ((ret = px_dma_physwin(px_p, dmareq, mp)) || !handlep)
8110Sstevel@tonic-gate 			goto freehandle;
8120Sstevel@tonic-gate 		break;
813909Segillett 	case PX_DMAI_FLAGS_BYPASS:
8140Sstevel@tonic-gate 	default:
8150Sstevel@tonic-gate 		cmn_err(CE_PANIC, "%s%d: px_dma_setup: bad dma type 0x%x",
8160Sstevel@tonic-gate 			ddi_driver_name(rdip), ddi_get_instance(rdip),
8170Sstevel@tonic-gate 			PX_DMA_TYPE(mp));
8180Sstevel@tonic-gate 		/*NOTREACHED*/
8190Sstevel@tonic-gate 	}
8200Sstevel@tonic-gate 	*handlep = (ddi_dma_handle_t)mp;
821909Segillett 	mp->dmai_flags |= PX_DMAI_FLAGS_INUSE;
8220Sstevel@tonic-gate 	px_dump_dma_handle(DBG_DMA_MAP, dip, mp);
8230Sstevel@tonic-gate 
8240Sstevel@tonic-gate 	return ((mp->dmai_nwin == 1) ? DDI_DMA_MAPPED : DDI_DMA_PARTIAL_MAP);
8250Sstevel@tonic-gate freehandle:
8260Sstevel@tonic-gate 	if (ret == DDI_DMA_NORESOURCES)
8270Sstevel@tonic-gate 		px_dma_freemp(mp); /* don't run_callback() */
8280Sstevel@tonic-gate 	else
8290Sstevel@tonic-gate 		(void) px_dma_freehdl(dip, rdip, (ddi_dma_handle_t)mp);
8300Sstevel@tonic-gate 	return (ret);
8310Sstevel@tonic-gate }
8320Sstevel@tonic-gate 
8330Sstevel@tonic-gate 
8340Sstevel@tonic-gate /*
8350Sstevel@tonic-gate  * bus dma alloc handle entry point:
8360Sstevel@tonic-gate  */
8370Sstevel@tonic-gate int
8380Sstevel@tonic-gate px_dma_allochdl(dev_info_t *dip, dev_info_t *rdip, ddi_dma_attr_t *attrp,
8390Sstevel@tonic-gate 	int (*waitfp)(caddr_t), caddr_t arg, ddi_dma_handle_t *handlep)
8400Sstevel@tonic-gate {
8410Sstevel@tonic-gate 	px_t *px_p = DIP_TO_STATE(dip);
8420Sstevel@tonic-gate 	ddi_dma_impl_t *mp;
8430Sstevel@tonic-gate 	int rval;
8440Sstevel@tonic-gate 
8450Sstevel@tonic-gate 	DBG(DBG_DMA_ALLOCH, dip, "rdip=%s%d\n",
8460Sstevel@tonic-gate 		ddi_driver_name(rdip), ddi_get_instance(rdip));
8470Sstevel@tonic-gate 
8480Sstevel@tonic-gate 	if (attrp->dma_attr_version != DMA_ATTR_V0)
8490Sstevel@tonic-gate 		return (DDI_DMA_BADATTR);
8500Sstevel@tonic-gate 
8510Sstevel@tonic-gate 	if (!(mp = px_dma_allocmp(dip, rdip, waitfp, arg)))
8520Sstevel@tonic-gate 		return (DDI_DMA_NORESOURCES);
8530Sstevel@tonic-gate 
8540Sstevel@tonic-gate 	/*
8550Sstevel@tonic-gate 	 * Save requestor's information
8560Sstevel@tonic-gate 	 */
8570Sstevel@tonic-gate 	mp->dmai_attr	= *attrp; /* whole object - augmented later  */
858909Segillett 	*PX_DEV_ATTR(mp)	= *attrp; /* whole object - device orig attr */
8590Sstevel@tonic-gate 	DBG(DBG_DMA_ALLOCH, dip, "mp=%p\n", mp);
8600Sstevel@tonic-gate 
8610Sstevel@tonic-gate 	/* check and convert dma attributes to handle parameters */
8620Sstevel@tonic-gate 	if (rval = px_dma_attr2hdl(px_p, mp)) {
8630Sstevel@tonic-gate 		px_dma_freehdl(dip, rdip, (ddi_dma_handle_t)mp);
8640Sstevel@tonic-gate 		*handlep = NULL;
8650Sstevel@tonic-gate 		return (rval);
8660Sstevel@tonic-gate 	}
8670Sstevel@tonic-gate 	*handlep = (ddi_dma_handle_t)mp;
8680Sstevel@tonic-gate 	return (DDI_SUCCESS);
8690Sstevel@tonic-gate }
8700Sstevel@tonic-gate 
8710Sstevel@tonic-gate 
8720Sstevel@tonic-gate /*
8730Sstevel@tonic-gate  * bus dma free handle entry point:
8740Sstevel@tonic-gate  */
8750Sstevel@tonic-gate /*ARGSUSED*/
8760Sstevel@tonic-gate int
8770Sstevel@tonic-gate px_dma_freehdl(dev_info_t *dip, dev_info_t *rdip, ddi_dma_handle_t handle)
8780Sstevel@tonic-gate {
8790Sstevel@tonic-gate 	DBG(DBG_DMA_FREEH, dip, "rdip=%s%d mp=%p\n",
8800Sstevel@tonic-gate 		ddi_driver_name(rdip), ddi_get_instance(rdip), handle);
8810Sstevel@tonic-gate 	px_dma_freemp((ddi_dma_impl_t *)handle);
8820Sstevel@tonic-gate 
8830Sstevel@tonic-gate 	if (px_kmem_clid) {
8840Sstevel@tonic-gate 		DBG(DBG_DMA_FREEH, dip, "run handle callback\n");
8850Sstevel@tonic-gate 		ddi_run_callback(&px_kmem_clid);
8860Sstevel@tonic-gate 	}
8870Sstevel@tonic-gate 	return (DDI_SUCCESS);
8880Sstevel@tonic-gate }
8890Sstevel@tonic-gate 
8900Sstevel@tonic-gate 
8910Sstevel@tonic-gate /*
8920Sstevel@tonic-gate  * bus dma bind handle entry point:
8930Sstevel@tonic-gate  */
8940Sstevel@tonic-gate int
8950Sstevel@tonic-gate px_dma_bindhdl(dev_info_t *dip, dev_info_t *rdip,
8960Sstevel@tonic-gate 	ddi_dma_handle_t handle, ddi_dma_req_t *dmareq,
8970Sstevel@tonic-gate 	ddi_dma_cookie_t *cookiep, uint_t *ccountp)
8980Sstevel@tonic-gate {
8990Sstevel@tonic-gate 	px_t *px_p = DIP_TO_STATE(dip);
9000Sstevel@tonic-gate 	px_mmu_t *mmu_p = px_p->px_mmu_p;
9010Sstevel@tonic-gate 	ddi_dma_impl_t *mp = (ddi_dma_impl_t *)handle;
9020Sstevel@tonic-gate 	int ret;
9030Sstevel@tonic-gate 
9040Sstevel@tonic-gate 	DBG(DBG_DMA_BINDH, dip, "rdip=%s%d mp=%p dmareq=%p\n",
9050Sstevel@tonic-gate 		ddi_driver_name(rdip), ddi_get_instance(rdip), mp, dmareq);
9060Sstevel@tonic-gate 
907909Segillett 	if (mp->dmai_flags & PX_DMAI_FLAGS_INUSE)
9080Sstevel@tonic-gate 		return (DDI_DMA_INUSE);
9090Sstevel@tonic-gate 
910909Segillett 	ASSERT((mp->dmai_flags & ~PX_DMAI_FLAGS_PRESERVE) == 0);
911909Segillett 	mp->dmai_flags |= PX_DMAI_FLAGS_INUSE;
9120Sstevel@tonic-gate 
9130Sstevel@tonic-gate 	if (ret = px_dma_type(px_p, dmareq, mp))
9140Sstevel@tonic-gate 		goto err;
9150Sstevel@tonic-gate 	if (ret = px_dma_pfn(px_p, dmareq, mp))
9160Sstevel@tonic-gate 		goto err;
9170Sstevel@tonic-gate 
9180Sstevel@tonic-gate 	switch (PX_DMA_TYPE(mp)) {
919909Segillett 	case PX_DMAI_FLAGS_DVMA:
9200Sstevel@tonic-gate 		if (ret = px_dvma_win(px_p, dmareq, mp))
9210Sstevel@tonic-gate 			goto map_err;
9220Sstevel@tonic-gate 		if (!PX_DMA_CANCACHE(mp)) {	/* try fast track */
9230Sstevel@tonic-gate 			if (PX_DMA_CANFAST(mp)) {
9240Sstevel@tonic-gate 				if (!px_dvma_map_fast(mmu_p, mp))
9250Sstevel@tonic-gate 					goto mapped; /*LINTED E_NOP_ELSE_STMT*/
9260Sstevel@tonic-gate 			} else {
9270Sstevel@tonic-gate 				PX_DVMA_FASTTRAK_PROF(mp);
9280Sstevel@tonic-gate 			}
9290Sstevel@tonic-gate 		}
9300Sstevel@tonic-gate 		if (ret = px_dvma_map(mp, dmareq, mmu_p))
9310Sstevel@tonic-gate 			goto map_err;
9320Sstevel@tonic-gate mapped:
9330Sstevel@tonic-gate 		*ccountp = 1;
9340Sstevel@tonic-gate 		MAKE_DMA_COOKIE(cookiep, mp->dmai_mapping, mp->dmai_size);
9350Sstevel@tonic-gate 		break;
936909Segillett 	case PX_DMAI_FLAGS_BYPASS:
937909Segillett 	case PX_DMAI_FLAGS_PTP:
9380Sstevel@tonic-gate 		if (ret = px_dma_physwin(px_p, dmareq, mp))
9390Sstevel@tonic-gate 			goto map_err;
940909Segillett 		*ccountp = PX_WINLST(mp)->win_ncookies;
941909Segillett 		*cookiep =
942909Segillett 		    *(ddi_dma_cookie_t *)(PX_WINLST(mp) + 1); /* wholeobj */
9430Sstevel@tonic-gate 		break;
9440Sstevel@tonic-gate 	default:
9450Sstevel@tonic-gate 		cmn_err(CE_PANIC, "%s%d: px_dma_bindhdl(%p): bad dma type",
9460Sstevel@tonic-gate 			ddi_driver_name(rdip), ddi_get_instance(rdip), mp);
9470Sstevel@tonic-gate 		/*NOTREACHED*/
9480Sstevel@tonic-gate 	}
949624Sschwartz 	DBG(DBG_DMA_BINDH, dip, "cookie %" PRIx64 "+%x\n",
950624Sschwartz 		cookiep->dmac_address, cookiep->dmac_size);
9510Sstevel@tonic-gate 	px_dump_dma_handle(DBG_DMA_MAP, dip, mp);
95227Sjchu 
95327Sjchu 	/* insert dma handle into FMA cache */
9541865Sdilpreet 	if (mp->dmai_attr.dma_attr_flags & DDI_DMA_FLAGERR) {
95527Sjchu 		(void) ndi_fmc_insert(rdip, DMA_HANDLE, mp, NULL);
9561865Sdilpreet 		mp->dmai_error.err_cf = impl_dma_check;
9571865Sdilpreet 	}
95827Sjchu 
9590Sstevel@tonic-gate 	return (mp->dmai_nwin == 1 ? DDI_DMA_MAPPED : DDI_DMA_PARTIAL_MAP);
9600Sstevel@tonic-gate map_err:
9610Sstevel@tonic-gate 	px_dma_freepfn(mp);
9620Sstevel@tonic-gate err:
963909Segillett 	mp->dmai_flags &= PX_DMAI_FLAGS_PRESERVE;
9640Sstevel@tonic-gate 	return (ret);
9650Sstevel@tonic-gate }
9660Sstevel@tonic-gate 
9670Sstevel@tonic-gate 
9680Sstevel@tonic-gate /*
9690Sstevel@tonic-gate  * bus dma unbind handle entry point:
9700Sstevel@tonic-gate  */
9710Sstevel@tonic-gate /*ARGSUSED*/
9720Sstevel@tonic-gate int
9730Sstevel@tonic-gate px_dma_unbindhdl(dev_info_t *dip, dev_info_t *rdip, ddi_dma_handle_t handle)
9740Sstevel@tonic-gate {
9750Sstevel@tonic-gate 	ddi_dma_impl_t *mp = (ddi_dma_impl_t *)handle;
9760Sstevel@tonic-gate 	px_t *px_p = DIP_TO_STATE(dip);
9770Sstevel@tonic-gate 	px_mmu_t *mmu_p = px_p->px_mmu_p;
9780Sstevel@tonic-gate 
9790Sstevel@tonic-gate 	DBG(DBG_DMA_UNBINDH, dip, "rdip=%s%d, mp=%p\n",
9800Sstevel@tonic-gate 		ddi_driver_name(rdip), ddi_get_instance(rdip), handle);
981909Segillett 	if ((mp->dmai_flags & PX_DMAI_FLAGS_INUSE) == 0) {
9820Sstevel@tonic-gate 		DBG(DBG_DMA_UNBINDH, dip, "handle not inuse\n");
9830Sstevel@tonic-gate 		return (DDI_FAILURE);
9840Sstevel@tonic-gate 	}
9850Sstevel@tonic-gate 
98627Sjchu 	/* remove dma handle from FMA cache */
98727Sjchu 	if (mp->dmai_attr.dma_attr_flags & DDI_DMA_FLAGERR) {
98827Sjchu 		if (DEVI(rdip)->devi_fmhdl != NULL &&
98927Sjchu 		    DDI_FM_DMA_ERR_CAP(DEVI(rdip)->devi_fmhdl->fh_cap)) {
99027Sjchu 			(void) ndi_fmc_remove(rdip, DMA_HANDLE, mp);
99127Sjchu 		}
99227Sjchu 	}
99327Sjchu 
9940Sstevel@tonic-gate 	/*
9950Sstevel@tonic-gate 	 * Here if the handle is using the iommu.  Unload all the iommu
9960Sstevel@tonic-gate 	 * translations.
9970Sstevel@tonic-gate 	 */
9980Sstevel@tonic-gate 	switch (PX_DMA_TYPE(mp)) {
999909Segillett 	case PX_DMAI_FLAGS_DVMA:
10000Sstevel@tonic-gate 		px_mmu_unmap_window(mmu_p, mp);
10010Sstevel@tonic-gate 		px_dvma_unmap(mmu_p, mp);
10020Sstevel@tonic-gate 		px_dma_freepfn(mp);
10030Sstevel@tonic-gate 		break;
1004909Segillett 	case PX_DMAI_FLAGS_BYPASS:
1005909Segillett 	case PX_DMAI_FLAGS_PTP:
10060Sstevel@tonic-gate 		px_dma_freewin(mp);
10070Sstevel@tonic-gate 		break;
10080Sstevel@tonic-gate 	default:
10090Sstevel@tonic-gate 		cmn_err(CE_PANIC, "%s%d: px_dma_unbindhdl:bad dma type %p",
10100Sstevel@tonic-gate 			ddi_driver_name(rdip), ddi_get_instance(rdip), mp);
10110Sstevel@tonic-gate 		/*NOTREACHED*/
10120Sstevel@tonic-gate 	}
10130Sstevel@tonic-gate 	if (mmu_p->mmu_dvma_clid != 0) {
10140Sstevel@tonic-gate 		DBG(DBG_DMA_UNBINDH, dip, "run dvma callback\n");
10150Sstevel@tonic-gate 		ddi_run_callback(&mmu_p->mmu_dvma_clid);
10160Sstevel@tonic-gate 	}
10170Sstevel@tonic-gate 	if (px_kmem_clid) {
10180Sstevel@tonic-gate 		DBG(DBG_DMA_UNBINDH, dip, "run handle callback\n");
10190Sstevel@tonic-gate 		ddi_run_callback(&px_kmem_clid);
10200Sstevel@tonic-gate 	}
1021909Segillett 	mp->dmai_flags &= PX_DMAI_FLAGS_PRESERVE;
102227Sjchu 
10230Sstevel@tonic-gate 	return (DDI_SUCCESS);
10240Sstevel@tonic-gate }
10250Sstevel@tonic-gate 
10260Sstevel@tonic-gate /*
10270Sstevel@tonic-gate  * bus dma win entry point:
10280Sstevel@tonic-gate  */
10290Sstevel@tonic-gate int
10300Sstevel@tonic-gate px_dma_win(dev_info_t *dip, dev_info_t *rdip,
10310Sstevel@tonic-gate 	ddi_dma_handle_t handle, uint_t win, off_t *offp,
10320Sstevel@tonic-gate 	size_t *lenp, ddi_dma_cookie_t *cookiep, uint_t *ccountp)
10330Sstevel@tonic-gate {
10340Sstevel@tonic-gate 	ddi_dma_impl_t	*mp = (ddi_dma_impl_t *)handle;
10350Sstevel@tonic-gate 	int		ret;
10360Sstevel@tonic-gate 
10370Sstevel@tonic-gate 	DBG(DBG_DMA_WIN, dip, "rdip=%s%d\n",
10380Sstevel@tonic-gate 		ddi_driver_name(rdip), ddi_get_instance(rdip));
10390Sstevel@tonic-gate 
10400Sstevel@tonic-gate 	px_dump_dma_handle(DBG_DMA_WIN, dip, mp);
10410Sstevel@tonic-gate 	if (win >= mp->dmai_nwin) {
10420Sstevel@tonic-gate 		DBG(DBG_DMA_WIN, dip, "%x out of range\n", win);
10430Sstevel@tonic-gate 		return (DDI_FAILURE);
10440Sstevel@tonic-gate 	}
10450Sstevel@tonic-gate 
10460Sstevel@tonic-gate 	switch (PX_DMA_TYPE(mp)) {
1047909Segillett 	case PX_DMAI_FLAGS_DVMA:
10480Sstevel@tonic-gate 		if (win != PX_DMA_CURWIN(mp)) {
10490Sstevel@tonic-gate 			px_t *px_p = DIP_TO_STATE(dip);
10500Sstevel@tonic-gate 			px_mmu_t *mmu_p = px_p->px_mmu_p;
10510Sstevel@tonic-gate 			px_mmu_unmap_window(mmu_p, mp);
10520Sstevel@tonic-gate 
10530Sstevel@tonic-gate 			/* map_window sets dmai_mapping/size/offset */
10540Sstevel@tonic-gate 			px_mmu_map_window(mmu_p, mp, win);
10550Sstevel@tonic-gate 			if ((ret = px_mmu_map_window(mmu_p,
10560Sstevel@tonic-gate 			    mp, win)) != DDI_SUCCESS)
10570Sstevel@tonic-gate 				return (ret);
10580Sstevel@tonic-gate 		}
10590Sstevel@tonic-gate 		if (cookiep)
10600Sstevel@tonic-gate 			MAKE_DMA_COOKIE(cookiep, mp->dmai_mapping,
10610Sstevel@tonic-gate 				mp->dmai_size);
10620Sstevel@tonic-gate 		if (ccountp)
10630Sstevel@tonic-gate 			*ccountp = 1;
10640Sstevel@tonic-gate 		break;
1065909Segillett 	case PX_DMAI_FLAGS_PTP:
1066909Segillett 	case PX_DMAI_FLAGS_BYPASS: {
10670Sstevel@tonic-gate 		int i;
10680Sstevel@tonic-gate 		ddi_dma_cookie_t *ck_p;
10690Sstevel@tonic-gate 		px_dma_win_t *win_p = mp->dmai_winlst;
10700Sstevel@tonic-gate 
10710Sstevel@tonic-gate 		for (i = 0; i < win; win_p = win_p->win_next, i++);
10720Sstevel@tonic-gate 		ck_p = (ddi_dma_cookie_t *)(win_p + 1);
10730Sstevel@tonic-gate 		*cookiep = *ck_p;
10740Sstevel@tonic-gate 		mp->dmai_offset = win_p->win_offset;
10750Sstevel@tonic-gate 		mp->dmai_size   = win_p->win_size;
10760Sstevel@tonic-gate 		mp->dmai_mapping = ck_p->dmac_laddress;
10770Sstevel@tonic-gate 		mp->dmai_cookie = ck_p + 1;
10780Sstevel@tonic-gate 		win_p->win_curseg = 0;
10790Sstevel@tonic-gate 		if (ccountp)
10800Sstevel@tonic-gate 			*ccountp = win_p->win_ncookies;
10810Sstevel@tonic-gate 		}
10820Sstevel@tonic-gate 		break;
10830Sstevel@tonic-gate 	default:
10840Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d: px_dma_win:bad dma type 0x%x",
10850Sstevel@tonic-gate 			ddi_driver_name(rdip), ddi_get_instance(rdip),
10860Sstevel@tonic-gate 			PX_DMA_TYPE(mp));
10870Sstevel@tonic-gate 		return (DDI_FAILURE);
10880Sstevel@tonic-gate 	}
10890Sstevel@tonic-gate 	if (cookiep)
10900Sstevel@tonic-gate 		DBG(DBG_DMA_WIN, dip,
10910Sstevel@tonic-gate 			"cookie - dmac_address=%x dmac_size=%x\n",
10920Sstevel@tonic-gate 			cookiep->dmac_address, cookiep->dmac_size);
10930Sstevel@tonic-gate 	if (offp)
10940Sstevel@tonic-gate 		*offp = (off_t)mp->dmai_offset;
10950Sstevel@tonic-gate 	if (lenp)
10960Sstevel@tonic-gate 		*lenp = mp->dmai_size;
10970Sstevel@tonic-gate 	return (DDI_SUCCESS);
10980Sstevel@tonic-gate }
10990Sstevel@tonic-gate 
11000Sstevel@tonic-gate #ifdef	DEBUG
11010Sstevel@tonic-gate static char *px_dmactl_str[] = {
11020Sstevel@tonic-gate 	"DDI_DMA_FREE",
11030Sstevel@tonic-gate 	"DDI_DMA_SYNC",
11040Sstevel@tonic-gate 	"DDI_DMA_HTOC",
11050Sstevel@tonic-gate 	"DDI_DMA_KVADDR",
11060Sstevel@tonic-gate 	"DDI_DMA_MOVWIN",
11070Sstevel@tonic-gate 	"DDI_DMA_REPWIN",
11080Sstevel@tonic-gate 	"DDI_DMA_GETERR",
11090Sstevel@tonic-gate 	"DDI_DMA_COFF",
11100Sstevel@tonic-gate 	"DDI_DMA_NEXTWIN",
11110Sstevel@tonic-gate 	"DDI_DMA_NEXTSEG",
11120Sstevel@tonic-gate 	"DDI_DMA_SEGTOC",
11130Sstevel@tonic-gate 	"DDI_DMA_RESERVE",
11140Sstevel@tonic-gate 	"DDI_DMA_RELEASE",
11150Sstevel@tonic-gate 	"DDI_DMA_RESETH",
11160Sstevel@tonic-gate 	"DDI_DMA_CKSYNC",
11170Sstevel@tonic-gate 	"DDI_DMA_IOPB_ALLOC",
11180Sstevel@tonic-gate 	"DDI_DMA_IOPB_FREE",
11190Sstevel@tonic-gate 	"DDI_DMA_SMEM_ALLOC",
11200Sstevel@tonic-gate 	"DDI_DMA_SMEM_FREE",
11210Sstevel@tonic-gate 	"DDI_DMA_SET_SBUS64"
11220Sstevel@tonic-gate };
11230Sstevel@tonic-gate #endif	/* DEBUG */
11240Sstevel@tonic-gate 
11250Sstevel@tonic-gate /*
11260Sstevel@tonic-gate  * bus dma control entry point:
11270Sstevel@tonic-gate  */
11280Sstevel@tonic-gate /*ARGSUSED*/
11290Sstevel@tonic-gate int
11300Sstevel@tonic-gate px_dma_ctlops(dev_info_t *dip, dev_info_t *rdip, ddi_dma_handle_t handle,
11310Sstevel@tonic-gate 	enum ddi_dma_ctlops cmd, off_t *offp, size_t *lenp, caddr_t *objp,
11320Sstevel@tonic-gate 	uint_t cache_flags)
11330Sstevel@tonic-gate {
11340Sstevel@tonic-gate 	ddi_dma_impl_t *mp = (ddi_dma_impl_t *)handle;
11350Sstevel@tonic-gate 
11360Sstevel@tonic-gate #ifdef	DEBUG
11370Sstevel@tonic-gate 	DBG(DBG_DMA_CTL, dip, "%s: rdip=%s%d\n", px_dmactl_str[cmd],
11380Sstevel@tonic-gate 		ddi_driver_name(rdip), ddi_get_instance(rdip));
11390Sstevel@tonic-gate #endif	/* DEBUG */
11400Sstevel@tonic-gate 
11410Sstevel@tonic-gate 	switch (cmd) {
11420Sstevel@tonic-gate 	case DDI_DMA_FREE:
11430Sstevel@tonic-gate 		(void) px_dma_unbindhdl(dip, rdip, handle);
11440Sstevel@tonic-gate 		(void) px_dma_freehdl(dip, rdip, handle);
11450Sstevel@tonic-gate 		return (DDI_SUCCESS);
11460Sstevel@tonic-gate 	case DDI_DMA_RESERVE: {
11470Sstevel@tonic-gate 		px_t *px_p = DIP_TO_STATE(dip);
11480Sstevel@tonic-gate 		return (px_fdvma_reserve(dip, rdip, px_p,
11490Sstevel@tonic-gate 			(ddi_dma_req_t *)offp, (ddi_dma_handle_t *)objp));
11500Sstevel@tonic-gate 		}
11510Sstevel@tonic-gate 	case DDI_DMA_RELEASE: {
11520Sstevel@tonic-gate 		px_t *px_p = DIP_TO_STATE(dip);
11530Sstevel@tonic-gate 		return (px_fdvma_release(dip, px_p, mp));
11540Sstevel@tonic-gate 		}
11550Sstevel@tonic-gate 	default:
11560Sstevel@tonic-gate 		break;
11570Sstevel@tonic-gate 	}
11580Sstevel@tonic-gate 
11590Sstevel@tonic-gate 	switch (PX_DMA_TYPE(mp)) {
1160909Segillett 	case PX_DMAI_FLAGS_DVMA:
11610Sstevel@tonic-gate 		return (px_dvma_ctl(dip, rdip, mp, cmd, offp, lenp, objp,
11620Sstevel@tonic-gate 			cache_flags));
1163909Segillett 	case PX_DMAI_FLAGS_PTP:
1164909Segillett 	case PX_DMAI_FLAGS_BYPASS:
11650Sstevel@tonic-gate 		return (px_dma_ctl(dip, rdip, mp, cmd, offp, lenp, objp,
11660Sstevel@tonic-gate 			cache_flags));
11670Sstevel@tonic-gate 	default:
11680Sstevel@tonic-gate 		cmn_err(CE_PANIC, "%s%d: px_dma_ctlops(%x):bad dma type %x",
11690Sstevel@tonic-gate 			ddi_driver_name(rdip), ddi_get_instance(rdip), cmd,
11700Sstevel@tonic-gate 			mp->dmai_flags);
11710Sstevel@tonic-gate 		/*NOTREACHED*/
11720Sstevel@tonic-gate 	}
1173671Skrishnae 	return (0);
11740Sstevel@tonic-gate }
11750Sstevel@tonic-gate 
11760Sstevel@tonic-gate /*
11770Sstevel@tonic-gate  * control ops entry point:
11780Sstevel@tonic-gate  *
11790Sstevel@tonic-gate  * Requests handled completely:
11800Sstevel@tonic-gate  *	DDI_CTLOPS_INITCHILD	see init_child() for details
11810Sstevel@tonic-gate  *	DDI_CTLOPS_UNINITCHILD
11820Sstevel@tonic-gate  *	DDI_CTLOPS_REPORTDEV	see report_dev() for details
11830Sstevel@tonic-gate  *	DDI_CTLOPS_IOMIN	cache line size if streaming otherwise 1
11840Sstevel@tonic-gate  *	DDI_CTLOPS_REGSIZE
11850Sstevel@tonic-gate  *	DDI_CTLOPS_NREGS
11860Sstevel@tonic-gate  *	DDI_CTLOPS_DVMAPAGESIZE
11870Sstevel@tonic-gate  *	DDI_CTLOPS_POKE
11880Sstevel@tonic-gate  *	DDI_CTLOPS_PEEK
11890Sstevel@tonic-gate  *
11900Sstevel@tonic-gate  * All others passed to parent.
11910Sstevel@tonic-gate  */
11920Sstevel@tonic-gate int
11930Sstevel@tonic-gate px_ctlops(dev_info_t *dip, dev_info_t *rdip,
11940Sstevel@tonic-gate 	ddi_ctl_enum_t op, void *arg, void *result)
11950Sstevel@tonic-gate {
11960Sstevel@tonic-gate 	px_t *px_p = DIP_TO_STATE(dip);
11970Sstevel@tonic-gate 	struct detachspec *ds;
11980Sstevel@tonic-gate 	struct attachspec *as;
11990Sstevel@tonic-gate 
12000Sstevel@tonic-gate 	switch (op) {
12010Sstevel@tonic-gate 	case DDI_CTLOPS_INITCHILD:
12020Sstevel@tonic-gate 		return (px_init_child(px_p, (dev_info_t *)arg));
12030Sstevel@tonic-gate 
12040Sstevel@tonic-gate 	case DDI_CTLOPS_UNINITCHILD:
12050Sstevel@tonic-gate 		return (px_uninit_child(px_p, (dev_info_t *)arg));
12060Sstevel@tonic-gate 
12070Sstevel@tonic-gate 	case DDI_CTLOPS_ATTACH:
12080Sstevel@tonic-gate 		as = (struct attachspec *)arg;
12090Sstevel@tonic-gate 		switch (as->when) {
12100Sstevel@tonic-gate 		case DDI_PRE:
12110Sstevel@tonic-gate 			if (as->cmd == DDI_ATTACH) {
12120Sstevel@tonic-gate 				DBG(DBG_PWR, dip, "PRE_ATTACH for %s@%d\n",
12130Sstevel@tonic-gate 				    ddi_driver_name(rdip),
12140Sstevel@tonic-gate 				    ddi_get_instance(rdip));
12150Sstevel@tonic-gate 				return (pcie_pm_hold(dip));
12160Sstevel@tonic-gate 			}
1217383Set142600 			if (as->cmd == DDI_RESUME) {
1218383Set142600 				ddi_acc_handle_t	config_handle;
1219383Set142600 				DBG(DBG_PWR, dip, "PRE_RESUME for %s@%d\n",
1220383Set142600 				    ddi_driver_name(rdip),
1221383Set142600 				    ddi_get_instance(rdip));
1222383Set142600 
1223383Set142600 				if (pci_config_setup(rdip, &config_handle) ==
1224383Set142600 				    DDI_SUCCESS) {
1225383Set142600 					pcie_clear_errors(rdip, config_handle);
1226383Set142600 					pci_config_teardown(&config_handle);
1227383Set142600 				}
1228383Set142600 			}
12290Sstevel@tonic-gate 			return (DDI_SUCCESS);
12300Sstevel@tonic-gate 
12310Sstevel@tonic-gate 		case DDI_POST:
12320Sstevel@tonic-gate 			DBG(DBG_PWR, dip, "POST_ATTACH for %s@%d\n",
12330Sstevel@tonic-gate 			    ddi_driver_name(rdip), ddi_get_instance(rdip));
12340Sstevel@tonic-gate 			if (as->cmd == DDI_ATTACH && as->result != DDI_SUCCESS)
12350Sstevel@tonic-gate 				pcie_pm_release(dip);
12362738Skrishnae 
12372738Skrishnae 			(void) pcie_postattach_child(rdip);
12382738Skrishnae 
12390Sstevel@tonic-gate 			return (DDI_SUCCESS);
12400Sstevel@tonic-gate 		default:
12410Sstevel@tonic-gate 			break;
12420Sstevel@tonic-gate 		}
12430Sstevel@tonic-gate 		break;
12440Sstevel@tonic-gate 
12450Sstevel@tonic-gate 	case DDI_CTLOPS_DETACH:
12460Sstevel@tonic-gate 		ds = (struct detachspec *)arg;
12470Sstevel@tonic-gate 		switch (ds->when) {
12480Sstevel@tonic-gate 		case DDI_POST:
12490Sstevel@tonic-gate 			if (ds->cmd == DDI_DETACH &&
12500Sstevel@tonic-gate 			    ds->result == DDI_SUCCESS) {
12510Sstevel@tonic-gate 				DBG(DBG_PWR, dip, "POST_DETACH for %s@%d\n",
12520Sstevel@tonic-gate 				    ddi_driver_name(rdip),
12530Sstevel@tonic-gate 				    ddi_get_instance(rdip));
12540Sstevel@tonic-gate 				return (pcie_pm_remove_child(dip, rdip));
12550Sstevel@tonic-gate 			}
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_REPORTDEV:
12630Sstevel@tonic-gate 		return (px_report_dev(rdip));
12640Sstevel@tonic-gate 
12650Sstevel@tonic-gate 	case DDI_CTLOPS_IOMIN:
12660Sstevel@tonic-gate 		return (DDI_SUCCESS);
12670Sstevel@tonic-gate 
12680Sstevel@tonic-gate 	case DDI_CTLOPS_REGSIZE:
12690Sstevel@tonic-gate 		*((off_t *)result) = px_get_reg_set_size(rdip, *((int *)arg));
127027Sjchu 		return (*((off_t *)result) == 0 ? DDI_FAILURE : DDI_SUCCESS);
12710Sstevel@tonic-gate 
12720Sstevel@tonic-gate 	case DDI_CTLOPS_NREGS:
12730Sstevel@tonic-gate 		*((uint_t *)result) = px_get_nreg_set(rdip);
12740Sstevel@tonic-gate 		return (DDI_SUCCESS);
12750Sstevel@tonic-gate 
12760Sstevel@tonic-gate 	case DDI_CTLOPS_DVMAPAGESIZE:
12770Sstevel@tonic-gate 		*((ulong_t *)result) = MMU_PAGE_SIZE;
12780Sstevel@tonic-gate 		return (DDI_SUCCESS);
12790Sstevel@tonic-gate 
12800Sstevel@tonic-gate 	case DDI_CTLOPS_POKE:	/* platform dependent implementation. */
12810Sstevel@tonic-gate 		return (px_lib_ctlops_poke(dip, rdip,
12820Sstevel@tonic-gate 		    (peekpoke_ctlops_t *)arg));
12830Sstevel@tonic-gate 
12840Sstevel@tonic-gate 	case DDI_CTLOPS_PEEK:	/* platform dependent implementation. */
12850Sstevel@tonic-gate 		return (px_lib_ctlops_peek(dip, rdip,
12860Sstevel@tonic-gate 		    (peekpoke_ctlops_t *)arg, result));
12870Sstevel@tonic-gate 
12880Sstevel@tonic-gate 	case DDI_CTLOPS_POWER:
12890Sstevel@tonic-gate 	default:
12900Sstevel@tonic-gate 		break;
12910Sstevel@tonic-gate 	}
12920Sstevel@tonic-gate 
12930Sstevel@tonic-gate 	/*
12940Sstevel@tonic-gate 	 * Now pass the request up to our parent.
12950Sstevel@tonic-gate 	 */
12960Sstevel@tonic-gate 	DBG(DBG_CTLOPS, dip, "passing request to parent: rdip=%s%d\n",
12970Sstevel@tonic-gate 		ddi_driver_name(rdip), ddi_get_instance(rdip));
12980Sstevel@tonic-gate 	return (ddi_ctlops(dip, rdip, op, arg, result));
12990Sstevel@tonic-gate }
13000Sstevel@tonic-gate 
13010Sstevel@tonic-gate /* ARGSUSED */
13020Sstevel@tonic-gate int
13030Sstevel@tonic-gate px_intr_ops(dev_info_t *dip, dev_info_t *rdip, ddi_intr_op_t intr_op,
13040Sstevel@tonic-gate     ddi_intr_handle_impl_t *hdlp, void *result)
13050Sstevel@tonic-gate {
13060Sstevel@tonic-gate 	int	intr_types, ret = DDI_SUCCESS;
13070Sstevel@tonic-gate 
13080Sstevel@tonic-gate 	DBG(DBG_INTROPS, dip, "px_intr_ops: rdip=%s%d\n",
13090Sstevel@tonic-gate 	    ddi_driver_name(rdip), ddi_get_instance(rdip));
13100Sstevel@tonic-gate 
13110Sstevel@tonic-gate 	/* Process DDI_INTROP_SUPPORTED_TYPES request here */
13120Sstevel@tonic-gate 	if (intr_op == DDI_INTROP_SUPPORTED_TYPES) {
13132580Sanish 		*(int *)result = i_ddi_get_intx_nintrs(rdip) ?
13140Sstevel@tonic-gate 		    DDI_INTR_TYPE_FIXED : 0;
13150Sstevel@tonic-gate 
13160Sstevel@tonic-gate 		if ((pci_msi_get_supported_type(rdip,
13170Sstevel@tonic-gate 		    &intr_types)) == DDI_SUCCESS) {
13180Sstevel@tonic-gate 			/*
13190Sstevel@tonic-gate 			 * Double check supported interrupt types vs.
13200Sstevel@tonic-gate 			 * what the host bridge supports.
13210Sstevel@tonic-gate 			 */
13221725Segillett 			*(int *)result |= intr_types;
13230Sstevel@tonic-gate 		}
13240Sstevel@tonic-gate 
13250Sstevel@tonic-gate 		return (ret);
13260Sstevel@tonic-gate 	}
13270Sstevel@tonic-gate 
13280Sstevel@tonic-gate 	/*
13290Sstevel@tonic-gate 	 * PCI-E nexus driver supports fixed, MSI and MSI-X interrupts.
13300Sstevel@tonic-gate 	 * Return failure if interrupt type is not supported.
13310Sstevel@tonic-gate 	 */
13320Sstevel@tonic-gate 	switch (hdlp->ih_type) {
13330Sstevel@tonic-gate 	case DDI_INTR_TYPE_FIXED:
13340Sstevel@tonic-gate 		ret = px_intx_ops(dip, rdip, intr_op, hdlp, result);
13350Sstevel@tonic-gate 		break;
13360Sstevel@tonic-gate 	case DDI_INTR_TYPE_MSI:
13370Sstevel@tonic-gate 	case DDI_INTR_TYPE_MSIX:
13380Sstevel@tonic-gate 		ret = px_msix_ops(dip, rdip, intr_op, hdlp, result);
13390Sstevel@tonic-gate 		break;
13400Sstevel@tonic-gate 	default:
13410Sstevel@tonic-gate 		ret = DDI_ENOTSUP;
13420Sstevel@tonic-gate 		break;
13430Sstevel@tonic-gate 	}
13440Sstevel@tonic-gate 
13450Sstevel@tonic-gate 	return (ret);
13460Sstevel@tonic-gate }
13471531Skini 
13482840Scarlsonj static int
13491531Skini px_init_hotplug(px_t *px_p)
13501531Skini {
13511531Skini 	px_bus_range_t bus_range;
13521531Skini 	dev_info_t *dip;
13531531Skini 	pciehpc_regops_t regops;
13541531Skini 
13551531Skini 	dip = px_p->px_dip;
13561531Skini 
13571531Skini 	if (ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
13581531Skini 	    "hotplug-capable") == 0)
13591531Skini 		return (DDI_FAILURE);
13601531Skini 
13611531Skini 	/*
13621531Skini 	 * Before initializing hotplug - open up bus range.  The busra
13631531Skini 	 * module will initialize its pool of bus numbers from this.
13641531Skini 	 * "busra" will be the agent that keeps track of them during
13651531Skini 	 * hotplug.  Also, note, that busra will remove any bus numbers
13661531Skini 	 * already in use from boot time.
13671531Skini 	 */
13681531Skini 	if (ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
13691531Skini 	    "bus-range") == 0) {
13701531Skini 		cmn_err(CE_WARN, "%s%d: bus-range not found\n",
13711531Skini 		    ddi_driver_name(dip), ddi_get_instance(dip));
13721531Skini #ifdef	DEBUG
13731531Skini 		bus_range.lo = 0x0;
13741531Skini 		bus_range.hi = 0xff;
13751531Skini 
13761531Skini 		if (ndi_prop_update_int_array(DDI_DEV_T_NONE,
13771531Skini 		    dip, "bus-range", (int *)&bus_range, 2)
13781531Skini 		    != DDI_PROP_SUCCESS) {
13791531Skini 			return (DDI_FAILURE);
13801531Skini 		}
13811531Skini #else
13821531Skini 		return (DDI_FAILURE);
13831531Skini #endif
13841531Skini 	}
13851531Skini 
13861531Skini 	if (px_lib_hotplug_init(dip, (void *)&regops) != DDI_SUCCESS)
13871531Skini 		return (DDI_FAILURE);
13881531Skini 
13891531Skini 	if (pciehpc_init(dip, &regops) != DDI_SUCCESS) {
13901531Skini 		px_lib_hotplug_uninit(dip);
13911531Skini 		return (DDI_FAILURE);
13921531Skini 	}
13931531Skini 
13941531Skini 	if (pcihp_init(dip) != DDI_SUCCESS) {
13951531Skini 		(void) pciehpc_uninit(dip);
13961531Skini 		px_lib_hotplug_uninit(dip);
13971531Skini 		return (DDI_FAILURE);
13981531Skini 	}
13991531Skini 
14001531Skini 	if (pcihp_get_cb_ops() != NULL) {
14011531Skini 		DBG(DBG_ATTACH, dip, "%s%d hotplug enabled",
14021531Skini 		    ddi_driver_name(dip), ddi_get_instance(dip));
14031531Skini 		px_p->px_dev_caps |= PX_HOTPLUG_CAPABLE;
14041531Skini 	}
14051531Skini 
14061531Skini 	return (DDI_SUCCESS);
14071531Skini }
14081531Skini 
14092840Scarlsonj static int
14101531Skini px_uninit_hotplug(dev_info_t *dip)
14111531Skini {
14121531Skini 	if (pcihp_uninit(dip) != DDI_SUCCESS)
14131531Skini 		return (DDI_FAILURE);
14141531Skini 
14151531Skini 	if (pciehpc_uninit(dip) != DDI_SUCCESS)
14161531Skini 		return (DDI_FAILURE);
14171531Skini 
14181531Skini 	px_lib_hotplug_uninit(dip);
14191531Skini 
14201531Skini 	return (DDI_SUCCESS);
14211531Skini }
1422