xref: /onnv-gate/usr/src/uts/sun4/io/px/px.c (revision 624:8c5206bfd8f1)
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
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
230Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * PCI Express nexus driver interface
310Sstevel@tonic-gate  */
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #include <sys/types.h>
340Sstevel@tonic-gate #include <sys/conf.h>		/* nulldev */
350Sstevel@tonic-gate #include <sys/stat.h>		/* devctl */
360Sstevel@tonic-gate #include <sys/kmem.h>
370Sstevel@tonic-gate #include <sys/sunddi.h>
380Sstevel@tonic-gate #include <sys/sunndi.h>
390Sstevel@tonic-gate #include <sys/hotplug/pci/pcihp.h>
400Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
410Sstevel@tonic-gate #include <sys/ddi_subrdefs.h>
42118Sjchu #include <sys/spl.h>
430Sstevel@tonic-gate #include <sys/epm.h>
440Sstevel@tonic-gate #include <sys/iommutsb.h>
450Sstevel@tonic-gate #include "px_obj.h"
46*624Sschwartz #include <sys/pci_tools.h>
47*624Sschwartz #include "px_tools.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);
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 
620Sstevel@tonic-gate /*
630Sstevel@tonic-gate  * bus ops and dev ops structures:
640Sstevel@tonic-gate  */
650Sstevel@tonic-gate static struct bus_ops px_bus_ops = {
660Sstevel@tonic-gate 	BUSO_REV,
670Sstevel@tonic-gate 	px_map,
680Sstevel@tonic-gate 	0,
690Sstevel@tonic-gate 	0,
700Sstevel@tonic-gate 	0,
710Sstevel@tonic-gate 	i_ddi_map_fault,
720Sstevel@tonic-gate 	px_dma_setup,
730Sstevel@tonic-gate 	px_dma_allochdl,
740Sstevel@tonic-gate 	px_dma_freehdl,
750Sstevel@tonic-gate 	px_dma_bindhdl,
760Sstevel@tonic-gate 	px_dma_unbindhdl,
770Sstevel@tonic-gate 	px_lib_dma_sync,
780Sstevel@tonic-gate 	px_dma_win,
790Sstevel@tonic-gate 	px_dma_ctlops,
800Sstevel@tonic-gate 	px_ctlops,
810Sstevel@tonic-gate 	ddi_bus_prop_op,
820Sstevel@tonic-gate 	ndi_busop_get_eventcookie,
830Sstevel@tonic-gate 	ndi_busop_add_eventcall,
840Sstevel@tonic-gate 	ndi_busop_remove_eventcall,
850Sstevel@tonic-gate 	ndi_post_event,
860Sstevel@tonic-gate 	NULL,
870Sstevel@tonic-gate 	NULL,			/* (*bus_config)(); */
880Sstevel@tonic-gate 	NULL,			/* (*bus_unconfig)(); */
890Sstevel@tonic-gate 	px_fm_init_child,	/* (*bus_fm_init)(); */
900Sstevel@tonic-gate 	NULL,			/* (*bus_fm_fini)(); */
9127Sjchu 	px_bus_enter,		/* (*bus_fm_access_enter)(); */
9227Sjchu 	px_bus_exit,		/* (*bus_fm_access_fini)(); */
930Sstevel@tonic-gate 	pcie_bus_power,		/* (*bus_power)(); */
940Sstevel@tonic-gate 	px_intr_ops		/* (*bus_intr_op)(); */
950Sstevel@tonic-gate };
960Sstevel@tonic-gate 
970Sstevel@tonic-gate extern struct cb_ops px_cb_ops;
980Sstevel@tonic-gate 
990Sstevel@tonic-gate static struct dev_ops px_ops = {
1000Sstevel@tonic-gate 	DEVO_REV,
1010Sstevel@tonic-gate 	0,
1020Sstevel@tonic-gate 	px_info,
1030Sstevel@tonic-gate 	nulldev,
1040Sstevel@tonic-gate 	0,
1050Sstevel@tonic-gate 	px_attach,
1060Sstevel@tonic-gate 	px_detach,
1070Sstevel@tonic-gate 	nodev,
1080Sstevel@tonic-gate 	&px_cb_ops,
1090Sstevel@tonic-gate 	&px_bus_ops,
1100Sstevel@tonic-gate 	nulldev
1110Sstevel@tonic-gate };
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate /*
1140Sstevel@tonic-gate  * module definitions:
1150Sstevel@tonic-gate  */
1160Sstevel@tonic-gate #include <sys/modctl.h>
1170Sstevel@tonic-gate extern struct mod_ops mod_driverops;
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate static struct modldrv modldrv = {
1200Sstevel@tonic-gate 	&mod_driverops, 		/* Type of module - driver */
1210Sstevel@tonic-gate 	"PCI Express nexus driver %I%",	/* Name of module. */
1220Sstevel@tonic-gate 	&px_ops,			/* driver ops */
1230Sstevel@tonic-gate };
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate static struct modlinkage modlinkage = {
1260Sstevel@tonic-gate 	MODREV_1, (void *)&modldrv, NULL
1270Sstevel@tonic-gate };
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate /* driver soft state */
1300Sstevel@tonic-gate void *px_state_p;
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate int
1330Sstevel@tonic-gate _init(void)
1340Sstevel@tonic-gate {
1350Sstevel@tonic-gate 	int e;
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate 	/*
1380Sstevel@tonic-gate 	 * Initialize per-px bus soft state pointer.
1390Sstevel@tonic-gate 	 */
1400Sstevel@tonic-gate 	e = ddi_soft_state_init(&px_state_p, sizeof (px_t), 1);
1410Sstevel@tonic-gate 	if (e != DDI_SUCCESS)
1420Sstevel@tonic-gate 		return (e);
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate 	/*
1450Sstevel@tonic-gate 	 * Install the module.
1460Sstevel@tonic-gate 	 */
1470Sstevel@tonic-gate 	e = mod_install(&modlinkage);
1480Sstevel@tonic-gate 	if (e != DDI_SUCCESS)
1490Sstevel@tonic-gate 		ddi_soft_state_fini(&px_state_p);
1500Sstevel@tonic-gate 	return (e);
1510Sstevel@tonic-gate }
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate int
1540Sstevel@tonic-gate _fini(void)
1550Sstevel@tonic-gate {
1560Sstevel@tonic-gate 	int e;
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate 	/*
1590Sstevel@tonic-gate 	 * Remove the module.
1600Sstevel@tonic-gate 	 */
1610Sstevel@tonic-gate 	e = mod_remove(&modlinkage);
1620Sstevel@tonic-gate 	if (e != DDI_SUCCESS)
1630Sstevel@tonic-gate 		return (e);
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate 	/* Free px soft state */
1660Sstevel@tonic-gate 	ddi_soft_state_fini(&px_state_p);
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 	return (e);
1690Sstevel@tonic-gate }
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate int
1720Sstevel@tonic-gate _info(struct modinfo *modinfop)
1730Sstevel@tonic-gate {
1740Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
1750Sstevel@tonic-gate }
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate /* ARGSUSED */
1780Sstevel@tonic-gate static int
1790Sstevel@tonic-gate px_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
1800Sstevel@tonic-gate {
1810Sstevel@tonic-gate 	int	instance = getminor((dev_t)arg);
1820Sstevel@tonic-gate 	px_t	*px_p = INST_TO_STATE(instance);
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate #ifdef	HOTPLUG
1850Sstevel@tonic-gate 	/*
1860Sstevel@tonic-gate 	 * Allow hotplug to deal with ones it manages
1870Sstevel@tonic-gate 	 * Hot Plug will be done later.
1880Sstevel@tonic-gate 	 */
1890Sstevel@tonic-gate 	if (px_p && (px_p->hotplug_capable == B_TRUE))
1900Sstevel@tonic-gate 		return (pcihp_info(dip, infocmd, arg, result));
1910Sstevel@tonic-gate #endif	/* HOTPLUG */
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate 	/* non-hotplug or not attached */
1940Sstevel@tonic-gate 	switch (infocmd) {
1950Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
1960Sstevel@tonic-gate 		*result = (void *)instance;
1970Sstevel@tonic-gate 		return (DDI_SUCCESS);
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
2000Sstevel@tonic-gate 		if (px_p == NULL)
2010Sstevel@tonic-gate 			return (DDI_FAILURE);
2020Sstevel@tonic-gate 		*result = (void *)px_p->px_dip;
2030Sstevel@tonic-gate 		return (DDI_SUCCESS);
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate 	default:
2060Sstevel@tonic-gate 		return (DDI_FAILURE);
2070Sstevel@tonic-gate 	}
2080Sstevel@tonic-gate }
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate /* device driver entry points */
2110Sstevel@tonic-gate /*
2120Sstevel@tonic-gate  * attach entry point:
2130Sstevel@tonic-gate  */
2140Sstevel@tonic-gate /*ARGSUSED*/
2150Sstevel@tonic-gate static int
2160Sstevel@tonic-gate px_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
2170Sstevel@tonic-gate {
2180Sstevel@tonic-gate 	px_t		*px_p;	/* per bus state pointer */
2190Sstevel@tonic-gate 	int		instance = DIP_TO_INST(dip);
2200Sstevel@tonic-gate 	int		ret = DDI_SUCCESS;
2210Sstevel@tonic-gate 	devhandle_t	dev_hdl = NULL;
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate 	switch (cmd) {
2240Sstevel@tonic-gate 	case DDI_ATTACH:
2250Sstevel@tonic-gate 		DBG(DBG_ATTACH, dip, "DDI_ATTACH\n");
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate 		/*
2280Sstevel@tonic-gate 		 * Allocate and get the per-px soft state structure.
2290Sstevel@tonic-gate 		 */
2300Sstevel@tonic-gate 		if (ddi_soft_state_zalloc(px_state_p, instance)
2310Sstevel@tonic-gate 		    != DDI_SUCCESS) {
2320Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s%d: can't allocate px state",
2330Sstevel@tonic-gate 				ddi_driver_name(dip), instance);
2340Sstevel@tonic-gate 			goto err_bad_px_softstate;
2350Sstevel@tonic-gate 		}
2360Sstevel@tonic-gate 		px_p = INST_TO_STATE(instance);
2370Sstevel@tonic-gate 		px_p->px_dip = dip;
2380Sstevel@tonic-gate 		mutex_init(&px_p->px_mutex, NULL, MUTEX_DRIVER, NULL);
2390Sstevel@tonic-gate 		px_p->px_soft_state = PX_SOFT_STATE_CLOSED;
2400Sstevel@tonic-gate 		px_p->px_open_count = 0;
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 		/*
2430Sstevel@tonic-gate 		 * Get key properties of the pci bridge node and
2440Sstevel@tonic-gate 		 * determine it's type (psycho, schizo, etc ...).
2450Sstevel@tonic-gate 		 */
2460Sstevel@tonic-gate 		if (px_get_props(px_p, dip) == DDI_FAILURE)
2470Sstevel@tonic-gate 			goto err_bad_px_prop;
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate 		if ((px_fm_attach(px_p)) != DDI_SUCCESS)
2500Sstevel@tonic-gate 			goto err_bad_fm;
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate 		if (px_lib_dev_init(dip, &dev_hdl) != DDI_SUCCESS)
2530Sstevel@tonic-gate 			goto err_bad_dev_init;
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate 		/* Initilize device handle */
2560Sstevel@tonic-gate 		px_p->px_dev_hdl = dev_hdl;
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate 		/*
2590Sstevel@tonic-gate 		 * Initialize interrupt block.  Note that this
2600Sstevel@tonic-gate 		 * initialize error handling for the PEC as well.
2610Sstevel@tonic-gate 		 */
2620Sstevel@tonic-gate 		if ((ret = px_ib_attach(px_p)) != DDI_SUCCESS)
2630Sstevel@tonic-gate 			goto err_bad_ib;
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate 		if (px_cb_attach(px_p) != DDI_SUCCESS)
2660Sstevel@tonic-gate 			goto err_bad_cb;
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate 		/*
2690Sstevel@tonic-gate 		 * Start creating the modules.
2700Sstevel@tonic-gate 		 * Note that attach() routines should
2710Sstevel@tonic-gate 		 * register and enable their own interrupts.
2720Sstevel@tonic-gate 		 */
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate 		if ((px_mmu_attach(px_p)) != DDI_SUCCESS)
2750Sstevel@tonic-gate 			goto err_bad_mmu;
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 		if ((px_msiq_attach(px_p)) != DDI_SUCCESS)
2780Sstevel@tonic-gate 			goto err_bad_msiq;
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate 		if ((px_msi_attach(px_p)) != DDI_SUCCESS)
2810Sstevel@tonic-gate 			goto err_bad_msi;
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate 		if ((px_pec_attach(px_p)) != DDI_SUCCESS)
2840Sstevel@tonic-gate 			goto err_bad_pec;
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate 		if ((px_dma_attach(px_p)) != DDI_SUCCESS)
2870Sstevel@tonic-gate 			goto err_bad_pec; /* nothing to uninitialize on DMA */
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate 		/*
2900Sstevel@tonic-gate 		 * All of the error handlers have been registered
2910Sstevel@tonic-gate 		 * by now so it's time to activate the interrupt.
2920Sstevel@tonic-gate 		 */
29327Sjchu 		if ((ret = px_err_add_intr(&px_p->px_fault)) != DDI_SUCCESS)
2940Sstevel@tonic-gate 			goto err_bad_pec_add_intr;
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate 		/*
2970Sstevel@tonic-gate 		 * Create the "devctl" node for hotplug and pcitool support.
2980Sstevel@tonic-gate 		 * For non-hotplug bus, we still need ":devctl" to
2990Sstevel@tonic-gate 		 * support DEVCTL_DEVICE_* and DEVCTL_BUS_* ioctls.
3000Sstevel@tonic-gate 		 */
3010Sstevel@tonic-gate 		if (ddi_create_minor_node(dip, "devctl", S_IFCHR,
3020Sstevel@tonic-gate 		    PCIHP_AP_MINOR_NUM(instance, PCIHP_DEVCTL_MINOR),
3030Sstevel@tonic-gate 		    DDI_NT_NEXUS, 0) != DDI_SUCCESS) {
3040Sstevel@tonic-gate 			goto err_bad_devctl_node;
3050Sstevel@tonic-gate 		}
306*624Sschwartz 
307*624Sschwartz 		if (pxtool_init(dip) != DDI_SUCCESS)
308*624Sschwartz 			goto err_bad_pcitool_node;
309*624Sschwartz 
3100Sstevel@tonic-gate 		/*
3110Sstevel@tonic-gate 		 * power management setup. Even if it fails, attach will
3120Sstevel@tonic-gate 		 * succeed as this is a optional feature. Since we are
3130Sstevel@tonic-gate 		 * always at full power, this is not critical.
3140Sstevel@tonic-gate 		 */
3150Sstevel@tonic-gate 		if (pwr_common_setup(dip) != DDI_SUCCESS) {
3160Sstevel@tonic-gate 			DBG(DBG_PWR, dip, "pwr_common_setup failed\n");
3170Sstevel@tonic-gate 		} else if (px_pwr_setup(dip) != DDI_SUCCESS) {
3180Sstevel@tonic-gate 			DBG(DBG_PWR, dip, "px_pwr_setup failed \n");
3190Sstevel@tonic-gate 			pwr_common_teardown(dip);
3200Sstevel@tonic-gate 		}
3210Sstevel@tonic-gate 
322435Sjchu 		/*
323435Sjchu 		 * add cpr callback
324435Sjchu 		 */
325435Sjchu 		px_cpr_add_callb(px_p);
326435Sjchu 
3270Sstevel@tonic-gate 		ddi_report_dev(dip);
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate 		px_p->px_state = PX_ATTACHED;
3300Sstevel@tonic-gate 		DBG(DBG_ATTACH, dip, "attach success\n");
3310Sstevel@tonic-gate 		break;
3320Sstevel@tonic-gate 
333*624Sschwartz err_bad_pcitool_node:
334*624Sschwartz 		ddi_remove_minor_node(dip, "devctl");
3350Sstevel@tonic-gate err_bad_devctl_node:
33627Sjchu 		px_err_rem_intr(&px_p->px_fault);
3370Sstevel@tonic-gate err_bad_pec_add_intr:
3380Sstevel@tonic-gate 		px_pec_detach(px_p);
3390Sstevel@tonic-gate err_bad_pec:
3400Sstevel@tonic-gate 		px_msi_detach(px_p);
3410Sstevel@tonic-gate err_bad_msi:
3420Sstevel@tonic-gate 		px_msiq_detach(px_p);
3430Sstevel@tonic-gate err_bad_msiq:
3440Sstevel@tonic-gate 		px_mmu_detach(px_p);
3450Sstevel@tonic-gate err_bad_mmu:
3460Sstevel@tonic-gate 		px_cb_detach(px_p);
3470Sstevel@tonic-gate err_bad_cb:
3480Sstevel@tonic-gate 		px_ib_detach(px_p);
3490Sstevel@tonic-gate err_bad_ib:
3500Sstevel@tonic-gate 		(void) px_lib_dev_fini(dip);
3510Sstevel@tonic-gate err_bad_dev_init:
3520Sstevel@tonic-gate 		px_fm_detach(px_p);
3530Sstevel@tonic-gate err_bad_fm:
3540Sstevel@tonic-gate 		px_free_props(px_p);
3550Sstevel@tonic-gate err_bad_px_prop:
3560Sstevel@tonic-gate 		mutex_destroy(&px_p->px_mutex);
3570Sstevel@tonic-gate 		ddi_soft_state_free(px_state_p, instance);
3580Sstevel@tonic-gate err_bad_px_softstate:
3590Sstevel@tonic-gate 		ret = DDI_FAILURE;
3600Sstevel@tonic-gate 		break;
3610Sstevel@tonic-gate 
3620Sstevel@tonic-gate 	case DDI_RESUME:
3630Sstevel@tonic-gate 		DBG(DBG_ATTACH, dip, "DDI_RESUME\n");
3640Sstevel@tonic-gate 
3650Sstevel@tonic-gate 		px_p = INST_TO_STATE(instance);
3660Sstevel@tonic-gate 
3670Sstevel@tonic-gate 		mutex_enter(&px_p->px_mutex);
3680Sstevel@tonic-gate 
3690Sstevel@tonic-gate 		/* suspend might have not succeeded */
3700Sstevel@tonic-gate 		if (px_p->px_state != PX_SUSPENDED) {
3710Sstevel@tonic-gate 			DBG(DBG_ATTACH, px_p->px_dip,
3720Sstevel@tonic-gate 			    "instance NOT suspended\n");
3730Sstevel@tonic-gate 			ret = DDI_FAILURE;
3740Sstevel@tonic-gate 			break;
3750Sstevel@tonic-gate 		}
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate 		px_lib_resume(dip);
3780Sstevel@tonic-gate 		(void) pcie_pwr_resume(dip);
3790Sstevel@tonic-gate 		px_p->px_state = PX_ATTACHED;
3800Sstevel@tonic-gate 
3810Sstevel@tonic-gate 		mutex_exit(&px_p->px_mutex);
3820Sstevel@tonic-gate 
3830Sstevel@tonic-gate 		break;
3840Sstevel@tonic-gate 	default:
3850Sstevel@tonic-gate 		DBG(DBG_ATTACH, dip, "unsupported attach op\n");
3860Sstevel@tonic-gate 		ret = DDI_FAILURE;
3870Sstevel@tonic-gate 		break;
3880Sstevel@tonic-gate 	}
3890Sstevel@tonic-gate 
3900Sstevel@tonic-gate 	return (ret);
3910Sstevel@tonic-gate }
3920Sstevel@tonic-gate 
3930Sstevel@tonic-gate /*
3940Sstevel@tonic-gate  * detach entry point:
3950Sstevel@tonic-gate  */
3960Sstevel@tonic-gate /*ARGSUSED*/
3970Sstevel@tonic-gate static int
3980Sstevel@tonic-gate px_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
3990Sstevel@tonic-gate {
4000Sstevel@tonic-gate 	int instance = ddi_get_instance(dip);
4010Sstevel@tonic-gate 	px_t *px_p = INST_TO_STATE(instance);
4020Sstevel@tonic-gate 	int ret;
4030Sstevel@tonic-gate 
4040Sstevel@tonic-gate 	/*
4050Sstevel@tonic-gate 	 * Make sure we are currently attached
4060Sstevel@tonic-gate 	 */
4070Sstevel@tonic-gate 	if (px_p->px_state != PX_ATTACHED) {
4080Sstevel@tonic-gate 		DBG(DBG_DETACH, dip, "failed - instance not attached\n");
4090Sstevel@tonic-gate 		return (DDI_FAILURE);
4100Sstevel@tonic-gate 	}
4110Sstevel@tonic-gate 
4120Sstevel@tonic-gate 	mutex_enter(&px_p->px_mutex);
4130Sstevel@tonic-gate 
4140Sstevel@tonic-gate 	switch (cmd) {
4150Sstevel@tonic-gate 	case DDI_DETACH:
4160Sstevel@tonic-gate 		DBG(DBG_DETACH, dip, "DDI_DETACH\n");
4170Sstevel@tonic-gate 
418435Sjchu 		/*
419435Sjchu 		 * remove cpr callback
420435Sjchu 		 */
421435Sjchu 		px_cpr_rem_callb(px_p);
422435Sjchu 
4230Sstevel@tonic-gate #ifdef	HOTPLUG
4240Sstevel@tonic-gate 		/*
4250Sstevel@tonic-gate 		 * Hot plug will be done later.
4260Sstevel@tonic-gate 		 */
4270Sstevel@tonic-gate 		if (px_p->hotplug_capable == B_TRUE) {
4280Sstevel@tonic-gate 			if (pxhp_uninit(dip) == DDI_FAILURE) {
4290Sstevel@tonic-gate 				mutex_exit(&px_p->px_mutex);
4300Sstevel@tonic-gate 				return (DDI_FAILURE);
4310Sstevel@tonic-gate 			}
4320Sstevel@tonic-gate 		}
4330Sstevel@tonic-gate #endif	/* HOTPLUG */
4340Sstevel@tonic-gate 
4350Sstevel@tonic-gate 		/*
4360Sstevel@tonic-gate 		 * things which used to be done in obj_destroy
4370Sstevel@tonic-gate 		 * are now in-lined here.
4380Sstevel@tonic-gate 		 */
4390Sstevel@tonic-gate 
4400Sstevel@tonic-gate 		px_p->px_state = PX_DETACHED;
4410Sstevel@tonic-gate 
442*624Sschwartz 		pxtool_uninit(dip);
443*624Sschwartz 
4440Sstevel@tonic-gate 		ddi_remove_minor_node(dip, "devctl");
44527Sjchu 		px_err_rem_intr(&px_p->px_fault);
4460Sstevel@tonic-gate 		px_pec_detach(px_p);
4470Sstevel@tonic-gate 		px_msi_detach(px_p);
4480Sstevel@tonic-gate 		px_msiq_detach(px_p);
4490Sstevel@tonic-gate 		px_mmu_detach(px_p);
4500Sstevel@tonic-gate 		px_cb_detach(px_p);
4510Sstevel@tonic-gate 		px_ib_detach(px_p);
4520Sstevel@tonic-gate 		(void) px_lib_dev_fini(dip);
4530Sstevel@tonic-gate 		px_fm_detach(px_p);
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate 		/*
4560Sstevel@tonic-gate 		 * Free the px soft state structure and the rest of the
4570Sstevel@tonic-gate 		 * resources it's using.
4580Sstevel@tonic-gate 		 */
4590Sstevel@tonic-gate 		px_free_props(px_p);
4600Sstevel@tonic-gate 		px_pwr_teardown(dip);
4610Sstevel@tonic-gate 		pwr_common_teardown(dip);
4620Sstevel@tonic-gate 		mutex_exit(&px_p->px_mutex);
4630Sstevel@tonic-gate 		mutex_destroy(&px_p->px_mutex);
4640Sstevel@tonic-gate 		ddi_soft_state_free(px_state_p, instance);
4650Sstevel@tonic-gate 
4660Sstevel@tonic-gate 		/* Free the interrupt-priorities prop if we created it. */ {
4670Sstevel@tonic-gate 			int len;
4680Sstevel@tonic-gate 
4690Sstevel@tonic-gate 			if (ddi_getproplen(DDI_DEV_T_ANY, dip,
4700Sstevel@tonic-gate 			    DDI_PROP_NOTPROM | DDI_PROP_DONTPASS,
4710Sstevel@tonic-gate 			    "interrupt-priorities", &len) == DDI_PROP_SUCCESS)
4720Sstevel@tonic-gate 				(void) ddi_prop_remove(DDI_DEV_T_NONE, dip,
4730Sstevel@tonic-gate 				    "interrupt-priorities");
4740Sstevel@tonic-gate 		}
4750Sstevel@tonic-gate 
4760Sstevel@tonic-gate 		px_p->px_dev_hdl = NULL;
4770Sstevel@tonic-gate 
4780Sstevel@tonic-gate 		return (DDI_SUCCESS);
4790Sstevel@tonic-gate 
4800Sstevel@tonic-gate 	case DDI_SUSPEND:
4810Sstevel@tonic-gate 		if (pcie_pwr_suspend(dip) != DDI_SUCCESS) {
4820Sstevel@tonic-gate 			mutex_exit(&px_p->px_mutex);
4830Sstevel@tonic-gate 			return (DDI_FAILURE);
4840Sstevel@tonic-gate 		}
4850Sstevel@tonic-gate 		if ((ret = px_lib_suspend(dip)) == DDI_SUCCESS)
4860Sstevel@tonic-gate 			px_p->px_state = PX_SUSPENDED;
4870Sstevel@tonic-gate 		mutex_exit(&px_p->px_mutex);
4880Sstevel@tonic-gate 
4890Sstevel@tonic-gate 		return (ret);
4900Sstevel@tonic-gate 
4910Sstevel@tonic-gate 	default:
4920Sstevel@tonic-gate 		DBG(DBG_DETACH, dip, "unsupported detach op\n");
4930Sstevel@tonic-gate 		mutex_exit(&px_p->px_mutex);
4940Sstevel@tonic-gate 		return (DDI_FAILURE);
4950Sstevel@tonic-gate 	}
4960Sstevel@tonic-gate }
4970Sstevel@tonic-gate 
4980Sstevel@tonic-gate /*
4990Sstevel@tonic-gate  * power management related initialization specific to px
5000Sstevel@tonic-gate  * called by px_attach()
5010Sstevel@tonic-gate  */
5020Sstevel@tonic-gate static int
5030Sstevel@tonic-gate px_pwr_setup(dev_info_t *dip)
5040Sstevel@tonic-gate {
5050Sstevel@tonic-gate 	pcie_pwr_t *pwr_p;
506118Sjchu 	int instance = ddi_get_instance(dip);
507118Sjchu 	px_t *px_p = INST_TO_STATE(instance);
5080Sstevel@tonic-gate 	ddi_intr_handle_impl_t hdl;
509118Sjchu 	ddi_iblock_cookie_t iblk_cookie;
5100Sstevel@tonic-gate 
5110Sstevel@tonic-gate 	ASSERT(PCIE_PMINFO(dip));
5120Sstevel@tonic-gate 	pwr_p = PCIE_NEXUS_PMINFO(dip);
5130Sstevel@tonic-gate 	ASSERT(pwr_p);
5140Sstevel@tonic-gate 
5150Sstevel@tonic-gate 	/*
5160Sstevel@tonic-gate 	 * indicate support LDI (Layered Driver Interface)
5170Sstevel@tonic-gate 	 * Create the property, if it is not already there
5180Sstevel@tonic-gate 	 */
5190Sstevel@tonic-gate 	if (!ddi_prop_exists(DDI_DEV_T_NONE, dip, DDI_PROP_DONTPASS,
5200Sstevel@tonic-gate 	    DDI_KERNEL_IOCTL)) {
5210Sstevel@tonic-gate 		if (ddi_prop_create(DDI_DEV_T_NONE, dip, DDI_PROP_CANSLEEP,
5220Sstevel@tonic-gate 		    DDI_KERNEL_IOCTL, NULL, 0) != DDI_PROP_SUCCESS) {
5230Sstevel@tonic-gate 			DBG(DBG_PWR, dip, "can't create kernel ioctl prop\n");
5240Sstevel@tonic-gate 			return (DDI_FAILURE);
5250Sstevel@tonic-gate 		}
5260Sstevel@tonic-gate 	}
5270Sstevel@tonic-gate 	/* No support for device PM. We are always at full power */
5280Sstevel@tonic-gate 	pwr_p->pwr_func_lvl = PM_LEVEL_D0;
5290Sstevel@tonic-gate 
530118Sjchu 	mutex_init(&px_p->px_l23ready_lock, NULL, MUTEX_DRIVER,
531118Sjchu 	    (void *)px_pwr_pil);
532118Sjchu 	cv_init(&px_p->px_l23ready_cv, NULL, CV_DRIVER, NULL);
533118Sjchu 
534118Sjchu 	mutex_init(&px_p->px_lup_lock, NULL, MUTEX_DRIVER,
535118Sjchu 	    (void *)PX_ERR_PIL);
536118Sjchu 	cv_init(&px_p->px_lup_cv, NULL, CV_DRIVER, NULL);
537118Sjchu 
538118Sjchu 	if (ddi_get_soft_iblock_cookie(dip, DDI_SOFTINT_HIGH,
539118Sjchu 	    &iblk_cookie) != DDI_SUCCESS) {
540118Sjchu 		DBG(DBG_PWR, dip, "px_pwr_setup: couldn't get iblock cookie\n");
541118Sjchu 		goto pwr_setup_err1;
542118Sjchu 	}
543118Sjchu 	mutex_init(&px_p->px_lupsoft_lock, NULL, MUTEX_DRIVER,
544118Sjchu 	    (void *)iblk_cookie);
545118Sjchu 	if (ddi_add_softintr(dip, DDI_SOFTINT_HIGH, &px_p->px_lupsoft_id,
546118Sjchu 	    NULL, NULL, px_lup_softintr, (caddr_t)px_p) != DDI_SUCCESS) {
547118Sjchu 		DBG(DBG_PWR, dip, "px_pwr_setup: couldn't add soft intr \n");
548118Sjchu 		goto pwr_setup_err2;
549118Sjchu 	}
5500Sstevel@tonic-gate 
5510Sstevel@tonic-gate 	/* Initilize handle */
552118Sjchu 	hdl.ih_cb_arg1 = px_p;
553118Sjchu 	hdl.ih_cb_arg2 = NULL;
5540Sstevel@tonic-gate 	hdl.ih_ver = DDI_INTR_VERSION;
5550Sstevel@tonic-gate 	hdl.ih_state = DDI_IHDL_STATE_ALLOC;
5560Sstevel@tonic-gate 	hdl.ih_dip = dip;
5570Sstevel@tonic-gate 	hdl.ih_inum = 0;
5580Sstevel@tonic-gate 	hdl.ih_pri = px_pwr_pil;
5590Sstevel@tonic-gate 
5600Sstevel@tonic-gate 	/* Add PME_TO_ACK message handler */
561118Sjchu 	hdl.ih_cb_func = (ddi_intr_handler_t *)px_pmeq_intr;
5620Sstevel@tonic-gate 	if (px_add_msiq_intr(dip, dip, &hdl, MSG_REC,
563118Sjchu 	    (msgcode_t)PCIE_PME_ACK_MSG, &px_p->px_pm_msiq_id) != DDI_SUCCESS) {
564118Sjchu 		DBG(DBG_PWR, dip, "px_pwr_setup: couldn't add "
565118Sjchu 		    " PME_TO_ACK intr\n");
5660Sstevel@tonic-gate 		goto px_pwrsetup_err;
5670Sstevel@tonic-gate 	}
568118Sjchu 	px_lib_msg_setmsiq(dip, PCIE_PME_ACK_MSG, px_p->px_pm_msiq_id);
5690Sstevel@tonic-gate 	px_lib_msg_setvalid(dip, PCIE_PME_ACK_MSG, PCIE_MSG_VALID);
5700Sstevel@tonic-gate 
5710Sstevel@tonic-gate 	return (DDI_SUCCESS);
5720Sstevel@tonic-gate 
5730Sstevel@tonic-gate px_pwrsetup_err:
574118Sjchu 	ddi_remove_softintr(px_p->px_lupsoft_id);
575118Sjchu pwr_setup_err2:
576118Sjchu 	mutex_destroy(&px_p->px_lupsoft_lock);
577118Sjchu pwr_setup_err1:
578118Sjchu 	mutex_destroy(&px_p->px_lup_lock);
579118Sjchu 	cv_destroy(&px_p->px_lup_cv);
580118Sjchu 	mutex_destroy(&px_p->px_l23ready_lock);
581118Sjchu 	cv_destroy(&px_p->px_l23ready_cv);
582118Sjchu 
5830Sstevel@tonic-gate 	return (DDI_FAILURE);
5840Sstevel@tonic-gate }
5850Sstevel@tonic-gate 
5860Sstevel@tonic-gate /*
5870Sstevel@tonic-gate  * undo whatever is done in px_pwr_setup. called by px_detach()
5880Sstevel@tonic-gate  */
5890Sstevel@tonic-gate static void
5900Sstevel@tonic-gate px_pwr_teardown(dev_info_t *dip)
5910Sstevel@tonic-gate {
592118Sjchu 	int instance = ddi_get_instance(dip);
593118Sjchu 	px_t *px_p = INST_TO_STATE(instance);
594118Sjchu 	ddi_intr_handle_impl_t	hdl;
5950Sstevel@tonic-gate 
596118Sjchu 	if (!PCIE_PMINFO(dip) || !PCIE_NEXUS_PMINFO(dip))
5970Sstevel@tonic-gate 		return;
5980Sstevel@tonic-gate 
5990Sstevel@tonic-gate 	/* Initilize handle */
6000Sstevel@tonic-gate 	hdl.ih_ver = DDI_INTR_VERSION;
6010Sstevel@tonic-gate 	hdl.ih_state = DDI_IHDL_STATE_ALLOC;
6020Sstevel@tonic-gate 	hdl.ih_dip = dip;
6030Sstevel@tonic-gate 	hdl.ih_inum = 0;
6040Sstevel@tonic-gate 
6050Sstevel@tonic-gate 	px_lib_msg_setvalid(dip, PCIE_PME_ACK_MSG, PCIE_MSG_INVALID);
6060Sstevel@tonic-gate 	(void) px_rem_msiq_intr(dip, dip, &hdl, MSG_REC, PCIE_PME_ACK_MSG,
607118Sjchu 	    px_p->px_pm_msiq_id);
608118Sjchu 
609118Sjchu 	px_p->px_pm_msiq_id = -1;
6100Sstevel@tonic-gate 
611118Sjchu 	cv_destroy(&px_p->px_l23ready_cv);
612118Sjchu 	ddi_remove_softintr(px_p->px_lupsoft_id);
613118Sjchu 	mutex_destroy(&px_p->px_lupsoft_lock);
614118Sjchu 	mutex_destroy(&px_p->px_lup_lock);
615118Sjchu 	mutex_destroy(&px_p->px_l23ready_lock);
6160Sstevel@tonic-gate }
6170Sstevel@tonic-gate 
6180Sstevel@tonic-gate /* bus driver entry points */
6190Sstevel@tonic-gate 
6200Sstevel@tonic-gate /*
6210Sstevel@tonic-gate  * bus map entry point:
6220Sstevel@tonic-gate  *
6230Sstevel@tonic-gate  * 	if map request is for an rnumber
6240Sstevel@tonic-gate  *		get the corresponding regspec from device node
6250Sstevel@tonic-gate  * 	build a new regspec in our parent's format
6260Sstevel@tonic-gate  *	build a new map_req with the new regspec
6270Sstevel@tonic-gate  *	call up the tree to complete the mapping
6280Sstevel@tonic-gate  */
6290Sstevel@tonic-gate int
6300Sstevel@tonic-gate px_map(dev_info_t *dip, dev_info_t *rdip, ddi_map_req_t *mp,
6310Sstevel@tonic-gate 	off_t off, off_t len, caddr_t *addrp)
6320Sstevel@tonic-gate {
6330Sstevel@tonic-gate 	px_t *px_p = DIP_TO_STATE(dip);
6340Sstevel@tonic-gate 	struct regspec p_regspec;
6350Sstevel@tonic-gate 	ddi_map_req_t p_mapreq;
6360Sstevel@tonic-gate 	int reglen, rval, r_no;
6370Sstevel@tonic-gate 	pci_regspec_t reloc_reg, *rp = &reloc_reg;
6380Sstevel@tonic-gate 
6390Sstevel@tonic-gate 	DBG(DBG_MAP, dip, "rdip=%s%d:",
6400Sstevel@tonic-gate 		ddi_driver_name(rdip), ddi_get_instance(rdip));
6410Sstevel@tonic-gate 
6420Sstevel@tonic-gate 	if (mp->map_flags & DDI_MF_USER_MAPPING)
6430Sstevel@tonic-gate 		return (DDI_ME_UNIMPLEMENTED);
6440Sstevel@tonic-gate 
6450Sstevel@tonic-gate 	switch (mp->map_type) {
6460Sstevel@tonic-gate 	case DDI_MT_REGSPEC:
6470Sstevel@tonic-gate 		reloc_reg = *(pci_regspec_t *)mp->map_obj.rp;	/* dup whole */
6480Sstevel@tonic-gate 		break;
6490Sstevel@tonic-gate 
6500Sstevel@tonic-gate 	case DDI_MT_RNUMBER:
6510Sstevel@tonic-gate 		r_no = mp->map_obj.rnumber;
6520Sstevel@tonic-gate 		DBG(DBG_MAP | DBG_CONT, dip, " r#=%x", r_no);
6530Sstevel@tonic-gate 
654506Scth 		if (ddi_getlongprop(DDI_DEV_T_ANY, rdip, DDI_PROP_DONTPASS,
6550Sstevel@tonic-gate 			"reg", (caddr_t)&rp, &reglen) != DDI_SUCCESS)
6560Sstevel@tonic-gate 				return (DDI_ME_RNUMBER_RANGE);
6570Sstevel@tonic-gate 
6580Sstevel@tonic-gate 		if (r_no < 0 || r_no >= reglen / sizeof (pci_regspec_t)) {
6590Sstevel@tonic-gate 			kmem_free(rp, reglen);
6600Sstevel@tonic-gate 			return (DDI_ME_RNUMBER_RANGE);
6610Sstevel@tonic-gate 		}
6620Sstevel@tonic-gate 		rp += r_no;
6630Sstevel@tonic-gate 		break;
6640Sstevel@tonic-gate 
6650Sstevel@tonic-gate 	default:
6660Sstevel@tonic-gate 		return (DDI_ME_INVAL);
6670Sstevel@tonic-gate 	}
6680Sstevel@tonic-gate 	DBG(DBG_MAP | DBG_CONT, dip, "\n");
6690Sstevel@tonic-gate 
6700Sstevel@tonic-gate 	if ((rp->pci_phys_hi & PCI_REG_ADDR_M) == PCI_ADDR_CONFIG) {
6710Sstevel@tonic-gate 		/*
6720Sstevel@tonic-gate 		 * There may be a need to differentiate between PCI
6730Sstevel@tonic-gate 		 * and PCI-Ex devices so the following range check is
6740Sstevel@tonic-gate 		 * done correctly, depending on the implementation of
6750Sstevel@tonic-gate 		 * px_pci bridge nexus driver.
6760Sstevel@tonic-gate 		 */
6770Sstevel@tonic-gate 		if ((off >= PCIE_CONF_HDR_SIZE) ||
6780Sstevel@tonic-gate 				(len > PCIE_CONF_HDR_SIZE) ||
6790Sstevel@tonic-gate 				(off + len > PCIE_CONF_HDR_SIZE))
6800Sstevel@tonic-gate 			return (DDI_ME_INVAL);
6810Sstevel@tonic-gate 		/*
6820Sstevel@tonic-gate 		 * the following function returning a DDI_FAILURE assumes
6830Sstevel@tonic-gate 		 * that there are no virtual config space access services
6840Sstevel@tonic-gate 		 * defined in this layer. Otherwise it is availed right
6850Sstevel@tonic-gate 		 * here and we return.
6860Sstevel@tonic-gate 		 */
6870Sstevel@tonic-gate 		rval = px_lib_map_vconfig(dip, mp, off, rp, addrp);
6880Sstevel@tonic-gate 		if (rval == DDI_SUCCESS)
6890Sstevel@tonic-gate 			goto done;
6900Sstevel@tonic-gate 	}
6910Sstevel@tonic-gate 
6920Sstevel@tonic-gate 	/*
6930Sstevel@tonic-gate 	 * No virtual config space services or we are mapping
6940Sstevel@tonic-gate 	 * a region of memory mapped config/IO/memory space, so proceed
6950Sstevel@tonic-gate 	 * to the parent.
6960Sstevel@tonic-gate 	 */
6970Sstevel@tonic-gate 
6980Sstevel@tonic-gate 	/* relocate within 64-bit pci space through "assigned-addresses" */
6990Sstevel@tonic-gate 	if (rval = px_reloc_reg(dip, rdip, px_p, rp))
7000Sstevel@tonic-gate 		goto done;
7010Sstevel@tonic-gate 
7020Sstevel@tonic-gate 	if (len)	/* adjust regspec according to mapping request */
7030Sstevel@tonic-gate 		rp->pci_size_low = len;	/* MIN ? */
7040Sstevel@tonic-gate 	rp->pci_phys_low += off;
7050Sstevel@tonic-gate 
7060Sstevel@tonic-gate 	/* translate relocated pci regspec into parent space through "ranges" */
7070Sstevel@tonic-gate 	if (rval = px_xlate_reg(px_p, rp, &p_regspec))
7080Sstevel@tonic-gate 		goto done;
7090Sstevel@tonic-gate 
7100Sstevel@tonic-gate 	p_mapreq = *mp;		/* dup the whole structure */
7110Sstevel@tonic-gate 	p_mapreq.map_type = DDI_MT_REGSPEC;
7120Sstevel@tonic-gate 	p_mapreq.map_obj.rp = &p_regspec;
7130Sstevel@tonic-gate 	rval = ddi_map(dip, &p_mapreq, 0, 0, addrp);
7140Sstevel@tonic-gate 
7150Sstevel@tonic-gate 	if (rval == DDI_SUCCESS) {
7160Sstevel@tonic-gate 		/*
7170Sstevel@tonic-gate 		 * Set-up access functions for FM access error capable drivers.
7180Sstevel@tonic-gate 		 */
7190Sstevel@tonic-gate 		if (DDI_FM_ACC_ERR_CAP(ddi_fm_capable(rdip)) &&
7200Sstevel@tonic-gate 		    mp->map_handlep->ah_acc.devacc_attr_access !=
7210Sstevel@tonic-gate 		    DDI_DEFAULT_ACC)
7220Sstevel@tonic-gate 			px_fm_acc_setup(mp, rdip);
7230Sstevel@tonic-gate 	}
7240Sstevel@tonic-gate 
7250Sstevel@tonic-gate done:
7260Sstevel@tonic-gate 	if (mp->map_type == DDI_MT_RNUMBER)
7270Sstevel@tonic-gate 		kmem_free(rp - r_no, reglen);
7280Sstevel@tonic-gate 
7290Sstevel@tonic-gate 	return (rval);
7300Sstevel@tonic-gate }
7310Sstevel@tonic-gate 
7320Sstevel@tonic-gate /*
7330Sstevel@tonic-gate  * bus dma map entry point
7340Sstevel@tonic-gate  * return value:
7350Sstevel@tonic-gate  *	DDI_DMA_PARTIAL_MAP	 1
7360Sstevel@tonic-gate  *	DDI_DMA_MAPOK		 0
7370Sstevel@tonic-gate  *	DDI_DMA_MAPPED		 0
7380Sstevel@tonic-gate  *	DDI_DMA_NORESOURCES	-1
7390Sstevel@tonic-gate  *	DDI_DMA_NOMAPPING	-2
7400Sstevel@tonic-gate  *	DDI_DMA_TOOBIG		-3
7410Sstevel@tonic-gate  */
7420Sstevel@tonic-gate int
7430Sstevel@tonic-gate px_dma_setup(dev_info_t *dip, dev_info_t *rdip, ddi_dma_req_t *dmareq,
7440Sstevel@tonic-gate 	ddi_dma_handle_t *handlep)
7450Sstevel@tonic-gate {
7460Sstevel@tonic-gate 	px_t *px_p = DIP_TO_STATE(dip);
7470Sstevel@tonic-gate 	px_mmu_t *mmu_p = px_p->px_mmu_p;
7480Sstevel@tonic-gate 	ddi_dma_impl_t *mp;
7490Sstevel@tonic-gate 	int ret;
7500Sstevel@tonic-gate 
7510Sstevel@tonic-gate 	DBG(DBG_DMA_MAP, dip, "mapping - rdip=%s%d type=%s\n",
7520Sstevel@tonic-gate 		ddi_driver_name(rdip), ddi_get_instance(rdip),
7530Sstevel@tonic-gate 		handlep ? "alloc" : "advisory");
7540Sstevel@tonic-gate 
7550Sstevel@tonic-gate 	if (!(mp = px_dma_lmts2hdl(dip, rdip, mmu_p, dmareq)))
7560Sstevel@tonic-gate 		return (DDI_DMA_NORESOURCES);
7570Sstevel@tonic-gate 	if (mp == (ddi_dma_impl_t *)DDI_DMA_NOMAPPING)
7580Sstevel@tonic-gate 		return (DDI_DMA_NOMAPPING);
7590Sstevel@tonic-gate 	if (ret = px_dma_type(px_p, dmareq, mp))
7600Sstevel@tonic-gate 		goto freehandle;
7610Sstevel@tonic-gate 	if (ret = px_dma_pfn(px_p, dmareq, mp))
7620Sstevel@tonic-gate 		goto freehandle;
7630Sstevel@tonic-gate 
7640Sstevel@tonic-gate 	switch (PX_DMA_TYPE(mp)) {
7650Sstevel@tonic-gate 	case DMAI_FLAGS_DVMA:	/* LINTED E_EQUALITY_NOT_ASSIGNMENT */
7660Sstevel@tonic-gate 		if ((ret = px_dvma_win(px_p, dmareq, mp)) || !handlep)
7670Sstevel@tonic-gate 			goto freehandle;
7680Sstevel@tonic-gate 		if (!PX_DMA_CANCACHE(mp)) {	/* try fast track */
7690Sstevel@tonic-gate 			if (PX_DMA_CANFAST(mp)) {
7700Sstevel@tonic-gate 				if (!px_dvma_map_fast(mmu_p, mp))
7710Sstevel@tonic-gate 					break;
7720Sstevel@tonic-gate 			/* LINTED E_NOP_ELSE_STMT */
7730Sstevel@tonic-gate 			} else {
7740Sstevel@tonic-gate 				PX_DVMA_FASTTRAK_PROF(mp);
7750Sstevel@tonic-gate 			}
7760Sstevel@tonic-gate 		}
7770Sstevel@tonic-gate 		if (ret = px_dvma_map(mp, dmareq, mmu_p))
7780Sstevel@tonic-gate 			goto freehandle;
7790Sstevel@tonic-gate 		break;
7800Sstevel@tonic-gate 	case DMAI_FLAGS_PTP:	/* LINTED E_EQUALITY_NOT_ASSIGNMENT */
7810Sstevel@tonic-gate 		if ((ret = px_dma_physwin(px_p, dmareq, mp)) || !handlep)
7820Sstevel@tonic-gate 			goto freehandle;
7830Sstevel@tonic-gate 		break;
7840Sstevel@tonic-gate 	case DMAI_FLAGS_BYPASS:
7850Sstevel@tonic-gate 	default:
7860Sstevel@tonic-gate 		cmn_err(CE_PANIC, "%s%d: px_dma_setup: bad dma type 0x%x",
7870Sstevel@tonic-gate 			ddi_driver_name(rdip), ddi_get_instance(rdip),
7880Sstevel@tonic-gate 			PX_DMA_TYPE(mp));
7890Sstevel@tonic-gate 		/*NOTREACHED*/
7900Sstevel@tonic-gate 	}
7910Sstevel@tonic-gate 	*handlep = (ddi_dma_handle_t)mp;
7920Sstevel@tonic-gate 	mp->dmai_flags |= DMAI_FLAGS_INUSE;
7930Sstevel@tonic-gate 	px_dump_dma_handle(DBG_DMA_MAP, dip, mp);
7940Sstevel@tonic-gate 
7950Sstevel@tonic-gate 	return ((mp->dmai_nwin == 1) ? DDI_DMA_MAPPED : DDI_DMA_PARTIAL_MAP);
7960Sstevel@tonic-gate freehandle:
7970Sstevel@tonic-gate 	if (ret == DDI_DMA_NORESOURCES)
7980Sstevel@tonic-gate 		px_dma_freemp(mp); /* don't run_callback() */
7990Sstevel@tonic-gate 	else
8000Sstevel@tonic-gate 		(void) px_dma_freehdl(dip, rdip, (ddi_dma_handle_t)mp);
8010Sstevel@tonic-gate 	return (ret);
8020Sstevel@tonic-gate }
8030Sstevel@tonic-gate 
8040Sstevel@tonic-gate 
8050Sstevel@tonic-gate /*
8060Sstevel@tonic-gate  * bus dma alloc handle entry point:
8070Sstevel@tonic-gate  */
8080Sstevel@tonic-gate int
8090Sstevel@tonic-gate px_dma_allochdl(dev_info_t *dip, dev_info_t *rdip, ddi_dma_attr_t *attrp,
8100Sstevel@tonic-gate 	int (*waitfp)(caddr_t), caddr_t arg, ddi_dma_handle_t *handlep)
8110Sstevel@tonic-gate {
8120Sstevel@tonic-gate 	px_t *px_p = DIP_TO_STATE(dip);
8130Sstevel@tonic-gate 	ddi_dma_impl_t *mp;
8140Sstevel@tonic-gate 	int rval;
8150Sstevel@tonic-gate 
8160Sstevel@tonic-gate 	DBG(DBG_DMA_ALLOCH, dip, "rdip=%s%d\n",
8170Sstevel@tonic-gate 		ddi_driver_name(rdip), ddi_get_instance(rdip));
8180Sstevel@tonic-gate 
8190Sstevel@tonic-gate 	if (attrp->dma_attr_version != DMA_ATTR_V0)
8200Sstevel@tonic-gate 		return (DDI_DMA_BADATTR);
8210Sstevel@tonic-gate 
8220Sstevel@tonic-gate 	if (!(mp = px_dma_allocmp(dip, rdip, waitfp, arg)))
8230Sstevel@tonic-gate 		return (DDI_DMA_NORESOURCES);
8240Sstevel@tonic-gate 
8250Sstevel@tonic-gate 	/*
8260Sstevel@tonic-gate 	 * Save requestor's information
8270Sstevel@tonic-gate 	 */
8280Sstevel@tonic-gate 	mp->dmai_attr	= *attrp; /* whole object - augmented later  */
8290Sstevel@tonic-gate 	*DEV_ATTR(mp)	= *attrp; /* whole object - device orig attr */
8300Sstevel@tonic-gate 	DBG(DBG_DMA_ALLOCH, dip, "mp=%p\n", mp);
8310Sstevel@tonic-gate 
8320Sstevel@tonic-gate 	/* check and convert dma attributes to handle parameters */
8330Sstevel@tonic-gate 	if (rval = px_dma_attr2hdl(px_p, mp)) {
8340Sstevel@tonic-gate 		px_dma_freehdl(dip, rdip, (ddi_dma_handle_t)mp);
8350Sstevel@tonic-gate 		*handlep = NULL;
8360Sstevel@tonic-gate 		return (rval);
8370Sstevel@tonic-gate 	}
8380Sstevel@tonic-gate 	*handlep = (ddi_dma_handle_t)mp;
8390Sstevel@tonic-gate 	return (DDI_SUCCESS);
8400Sstevel@tonic-gate }
8410Sstevel@tonic-gate 
8420Sstevel@tonic-gate 
8430Sstevel@tonic-gate /*
8440Sstevel@tonic-gate  * bus dma free handle entry point:
8450Sstevel@tonic-gate  */
8460Sstevel@tonic-gate /*ARGSUSED*/
8470Sstevel@tonic-gate int
8480Sstevel@tonic-gate px_dma_freehdl(dev_info_t *dip, dev_info_t *rdip, ddi_dma_handle_t handle)
8490Sstevel@tonic-gate {
8500Sstevel@tonic-gate 	DBG(DBG_DMA_FREEH, dip, "rdip=%s%d mp=%p\n",
8510Sstevel@tonic-gate 		ddi_driver_name(rdip), ddi_get_instance(rdip), handle);
8520Sstevel@tonic-gate 	px_dma_freemp((ddi_dma_impl_t *)handle);
8530Sstevel@tonic-gate 
8540Sstevel@tonic-gate 	if (px_kmem_clid) {
8550Sstevel@tonic-gate 		DBG(DBG_DMA_FREEH, dip, "run handle callback\n");
8560Sstevel@tonic-gate 		ddi_run_callback(&px_kmem_clid);
8570Sstevel@tonic-gate 	}
8580Sstevel@tonic-gate 	return (DDI_SUCCESS);
8590Sstevel@tonic-gate }
8600Sstevel@tonic-gate 
8610Sstevel@tonic-gate 
8620Sstevel@tonic-gate /*
8630Sstevel@tonic-gate  * bus dma bind handle entry point:
8640Sstevel@tonic-gate  */
8650Sstevel@tonic-gate int
8660Sstevel@tonic-gate px_dma_bindhdl(dev_info_t *dip, dev_info_t *rdip,
8670Sstevel@tonic-gate 	ddi_dma_handle_t handle, ddi_dma_req_t *dmareq,
8680Sstevel@tonic-gate 	ddi_dma_cookie_t *cookiep, uint_t *ccountp)
8690Sstevel@tonic-gate {
8700Sstevel@tonic-gate 	px_t *px_p = DIP_TO_STATE(dip);
8710Sstevel@tonic-gate 	px_mmu_t *mmu_p = px_p->px_mmu_p;
8720Sstevel@tonic-gate 	ddi_dma_impl_t *mp = (ddi_dma_impl_t *)handle;
8730Sstevel@tonic-gate 	int ret;
8740Sstevel@tonic-gate 
8750Sstevel@tonic-gate 	DBG(DBG_DMA_BINDH, dip, "rdip=%s%d mp=%p dmareq=%p\n",
8760Sstevel@tonic-gate 		ddi_driver_name(rdip), ddi_get_instance(rdip), mp, dmareq);
8770Sstevel@tonic-gate 
8780Sstevel@tonic-gate 	if (mp->dmai_flags & DMAI_FLAGS_INUSE)
8790Sstevel@tonic-gate 		return (DDI_DMA_INUSE);
8800Sstevel@tonic-gate 
8810Sstevel@tonic-gate 	ASSERT((mp->dmai_flags & ~DMAI_FLAGS_PRESERVE) == 0);
8820Sstevel@tonic-gate 	mp->dmai_flags |= DMAI_FLAGS_INUSE;
8830Sstevel@tonic-gate 
8840Sstevel@tonic-gate 	if (ret = px_dma_type(px_p, dmareq, mp))
8850Sstevel@tonic-gate 		goto err;
8860Sstevel@tonic-gate 	if (ret = px_dma_pfn(px_p, dmareq, mp))
8870Sstevel@tonic-gate 		goto err;
8880Sstevel@tonic-gate 
8890Sstevel@tonic-gate 	switch (PX_DMA_TYPE(mp)) {
8900Sstevel@tonic-gate 	case DMAI_FLAGS_DVMA:
8910Sstevel@tonic-gate 		if (ret = px_dvma_win(px_p, dmareq, mp))
8920Sstevel@tonic-gate 			goto map_err;
8930Sstevel@tonic-gate 		if (!PX_DMA_CANCACHE(mp)) {	/* try fast track */
8940Sstevel@tonic-gate 			if (PX_DMA_CANFAST(mp)) {
8950Sstevel@tonic-gate 				if (!px_dvma_map_fast(mmu_p, mp))
8960Sstevel@tonic-gate 					goto mapped; /*LINTED E_NOP_ELSE_STMT*/
8970Sstevel@tonic-gate 			} else {
8980Sstevel@tonic-gate 				PX_DVMA_FASTTRAK_PROF(mp);
8990Sstevel@tonic-gate 			}
9000Sstevel@tonic-gate 		}
9010Sstevel@tonic-gate 		if (ret = px_dvma_map(mp, dmareq, mmu_p))
9020Sstevel@tonic-gate 			goto map_err;
9030Sstevel@tonic-gate mapped:
9040Sstevel@tonic-gate 		*ccountp = 1;
9050Sstevel@tonic-gate 		MAKE_DMA_COOKIE(cookiep, mp->dmai_mapping, mp->dmai_size);
9060Sstevel@tonic-gate 		break;
9070Sstevel@tonic-gate 	case DMAI_FLAGS_BYPASS:
9080Sstevel@tonic-gate 	case DMAI_FLAGS_PTP:
9090Sstevel@tonic-gate 		if (ret = px_dma_physwin(px_p, dmareq, mp))
9100Sstevel@tonic-gate 			goto map_err;
9110Sstevel@tonic-gate 		*ccountp = WINLST(mp)->win_ncookies;
9120Sstevel@tonic-gate 		*cookiep = *(ddi_dma_cookie_t *)(WINLST(mp) + 1); /* wholeobj */
9130Sstevel@tonic-gate 		break;
9140Sstevel@tonic-gate 	default:
9150Sstevel@tonic-gate 		cmn_err(CE_PANIC, "%s%d: px_dma_bindhdl(%p): bad dma type",
9160Sstevel@tonic-gate 			ddi_driver_name(rdip), ddi_get_instance(rdip), mp);
9170Sstevel@tonic-gate 		/*NOTREACHED*/
9180Sstevel@tonic-gate 	}
919*624Sschwartz 	DBG(DBG_DMA_BINDH, dip, "cookie %" PRIx64 "+%x\n",
920*624Sschwartz 		cookiep->dmac_address, cookiep->dmac_size);
9210Sstevel@tonic-gate 	px_dump_dma_handle(DBG_DMA_MAP, dip, mp);
92227Sjchu 
92327Sjchu 	/* insert dma handle into FMA cache */
92427Sjchu 	if (mp->dmai_attr.dma_attr_flags & DDI_DMA_FLAGERR)
92527Sjchu 		(void) ndi_fmc_insert(rdip, DMA_HANDLE, mp, NULL);
92627Sjchu 
9270Sstevel@tonic-gate 	return (mp->dmai_nwin == 1 ? DDI_DMA_MAPPED : DDI_DMA_PARTIAL_MAP);
9280Sstevel@tonic-gate map_err:
9290Sstevel@tonic-gate 	px_dma_freepfn(mp);
9300Sstevel@tonic-gate err:
9310Sstevel@tonic-gate 	mp->dmai_flags &= DMAI_FLAGS_PRESERVE;
9320Sstevel@tonic-gate 	return (ret);
9330Sstevel@tonic-gate }
9340Sstevel@tonic-gate 
9350Sstevel@tonic-gate 
9360Sstevel@tonic-gate /*
9370Sstevel@tonic-gate  * bus dma unbind handle entry point:
9380Sstevel@tonic-gate  */
9390Sstevel@tonic-gate /*ARGSUSED*/
9400Sstevel@tonic-gate int
9410Sstevel@tonic-gate px_dma_unbindhdl(dev_info_t *dip, dev_info_t *rdip, ddi_dma_handle_t handle)
9420Sstevel@tonic-gate {
9430Sstevel@tonic-gate 	ddi_dma_impl_t *mp = (ddi_dma_impl_t *)handle;
9440Sstevel@tonic-gate 	px_t *px_p = DIP_TO_STATE(dip);
9450Sstevel@tonic-gate 	px_mmu_t *mmu_p = px_p->px_mmu_p;
9460Sstevel@tonic-gate 
9470Sstevel@tonic-gate 	DBG(DBG_DMA_UNBINDH, dip, "rdip=%s%d, mp=%p\n",
9480Sstevel@tonic-gate 		ddi_driver_name(rdip), ddi_get_instance(rdip), handle);
9490Sstevel@tonic-gate 	if ((mp->dmai_flags & DMAI_FLAGS_INUSE) == 0) {
9500Sstevel@tonic-gate 		DBG(DBG_DMA_UNBINDH, dip, "handle not inuse\n");
9510Sstevel@tonic-gate 		return (DDI_FAILURE);
9520Sstevel@tonic-gate 	}
9530Sstevel@tonic-gate 
95427Sjchu 	/* remove dma handle from FMA cache */
95527Sjchu 	if (mp->dmai_attr.dma_attr_flags & DDI_DMA_FLAGERR) {
95627Sjchu 		if (DEVI(rdip)->devi_fmhdl != NULL &&
95727Sjchu 		    DDI_FM_DMA_ERR_CAP(DEVI(rdip)->devi_fmhdl->fh_cap)) {
95827Sjchu 			(void) ndi_fmc_remove(rdip, DMA_HANDLE, mp);
95927Sjchu 		}
96027Sjchu 	}
96127Sjchu 
9620Sstevel@tonic-gate 	/*
9630Sstevel@tonic-gate 	 * Here if the handle is using the iommu.  Unload all the iommu
9640Sstevel@tonic-gate 	 * translations.
9650Sstevel@tonic-gate 	 */
9660Sstevel@tonic-gate 	switch (PX_DMA_TYPE(mp)) {
9670Sstevel@tonic-gate 	case DMAI_FLAGS_DVMA:
9680Sstevel@tonic-gate 		px_mmu_unmap_window(mmu_p, mp);
9690Sstevel@tonic-gate 		px_dvma_unmap(mmu_p, mp);
9700Sstevel@tonic-gate 		px_dma_freepfn(mp);
9710Sstevel@tonic-gate 		break;
9720Sstevel@tonic-gate 	case DMAI_FLAGS_BYPASS:
9730Sstevel@tonic-gate 	case DMAI_FLAGS_PTP:
9740Sstevel@tonic-gate 		px_dma_freewin(mp);
9750Sstevel@tonic-gate 		break;
9760Sstevel@tonic-gate 	default:
9770Sstevel@tonic-gate 		cmn_err(CE_PANIC, "%s%d: px_dma_unbindhdl:bad dma type %p",
9780Sstevel@tonic-gate 			ddi_driver_name(rdip), ddi_get_instance(rdip), mp);
9790Sstevel@tonic-gate 		/*NOTREACHED*/
9800Sstevel@tonic-gate 	}
9810Sstevel@tonic-gate 	if (mmu_p->mmu_dvma_clid != 0) {
9820Sstevel@tonic-gate 		DBG(DBG_DMA_UNBINDH, dip, "run dvma callback\n");
9830Sstevel@tonic-gate 		ddi_run_callback(&mmu_p->mmu_dvma_clid);
9840Sstevel@tonic-gate 	}
9850Sstevel@tonic-gate 	if (px_kmem_clid) {
9860Sstevel@tonic-gate 		DBG(DBG_DMA_UNBINDH, dip, "run handle callback\n");
9870Sstevel@tonic-gate 		ddi_run_callback(&px_kmem_clid);
9880Sstevel@tonic-gate 	}
9890Sstevel@tonic-gate 	mp->dmai_flags &= DMAI_FLAGS_PRESERVE;
99027Sjchu 
9910Sstevel@tonic-gate 	return (DDI_SUCCESS);
9920Sstevel@tonic-gate }
9930Sstevel@tonic-gate 
9940Sstevel@tonic-gate /*
9950Sstevel@tonic-gate  * bus dma win entry point:
9960Sstevel@tonic-gate  */
9970Sstevel@tonic-gate int
9980Sstevel@tonic-gate px_dma_win(dev_info_t *dip, dev_info_t *rdip,
9990Sstevel@tonic-gate 	ddi_dma_handle_t handle, uint_t win, off_t *offp,
10000Sstevel@tonic-gate 	size_t *lenp, ddi_dma_cookie_t *cookiep, uint_t *ccountp)
10010Sstevel@tonic-gate {
10020Sstevel@tonic-gate 	ddi_dma_impl_t	*mp = (ddi_dma_impl_t *)handle;
10030Sstevel@tonic-gate 	int		ret;
10040Sstevel@tonic-gate 
10050Sstevel@tonic-gate 	DBG(DBG_DMA_WIN, dip, "rdip=%s%d\n",
10060Sstevel@tonic-gate 		ddi_driver_name(rdip), ddi_get_instance(rdip));
10070Sstevel@tonic-gate 
10080Sstevel@tonic-gate 	px_dump_dma_handle(DBG_DMA_WIN, dip, mp);
10090Sstevel@tonic-gate 	if (win >= mp->dmai_nwin) {
10100Sstevel@tonic-gate 		DBG(DBG_DMA_WIN, dip, "%x out of range\n", win);
10110Sstevel@tonic-gate 		return (DDI_FAILURE);
10120Sstevel@tonic-gate 	}
10130Sstevel@tonic-gate 
10140Sstevel@tonic-gate 	switch (PX_DMA_TYPE(mp)) {
10150Sstevel@tonic-gate 	case DMAI_FLAGS_DVMA:
10160Sstevel@tonic-gate 		if (win != PX_DMA_CURWIN(mp)) {
10170Sstevel@tonic-gate 			px_t *px_p = DIP_TO_STATE(dip);
10180Sstevel@tonic-gate 			px_mmu_t *mmu_p = px_p->px_mmu_p;
10190Sstevel@tonic-gate 			px_mmu_unmap_window(mmu_p, mp);
10200Sstevel@tonic-gate 
10210Sstevel@tonic-gate 			/* map_window sets dmai_mapping/size/offset */
10220Sstevel@tonic-gate 			px_mmu_map_window(mmu_p, mp, win);
10230Sstevel@tonic-gate 			if ((ret = px_mmu_map_window(mmu_p,
10240Sstevel@tonic-gate 			    mp, win)) != DDI_SUCCESS)
10250Sstevel@tonic-gate 				return (ret);
10260Sstevel@tonic-gate 		}
10270Sstevel@tonic-gate 		if (cookiep)
10280Sstevel@tonic-gate 			MAKE_DMA_COOKIE(cookiep, mp->dmai_mapping,
10290Sstevel@tonic-gate 				mp->dmai_size);
10300Sstevel@tonic-gate 		if (ccountp)
10310Sstevel@tonic-gate 			*ccountp = 1;
10320Sstevel@tonic-gate 		break;
10330Sstevel@tonic-gate 	case DMAI_FLAGS_PTP:
10340Sstevel@tonic-gate 	case DMAI_FLAGS_BYPASS: {
10350Sstevel@tonic-gate 		int i;
10360Sstevel@tonic-gate 		ddi_dma_cookie_t *ck_p;
10370Sstevel@tonic-gate 		px_dma_win_t *win_p = mp->dmai_winlst;
10380Sstevel@tonic-gate 
10390Sstevel@tonic-gate 		for (i = 0; i < win; win_p = win_p->win_next, i++);
10400Sstevel@tonic-gate 		ck_p = (ddi_dma_cookie_t *)(win_p + 1);
10410Sstevel@tonic-gate 		*cookiep = *ck_p;
10420Sstevel@tonic-gate 		mp->dmai_offset = win_p->win_offset;
10430Sstevel@tonic-gate 		mp->dmai_size   = win_p->win_size;
10440Sstevel@tonic-gate 		mp->dmai_mapping = ck_p->dmac_laddress;
10450Sstevel@tonic-gate 		mp->dmai_cookie = ck_p + 1;
10460Sstevel@tonic-gate 		win_p->win_curseg = 0;
10470Sstevel@tonic-gate 		if (ccountp)
10480Sstevel@tonic-gate 			*ccountp = win_p->win_ncookies;
10490Sstevel@tonic-gate 		}
10500Sstevel@tonic-gate 		break;
10510Sstevel@tonic-gate 	default:
10520Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d: px_dma_win:bad dma type 0x%x",
10530Sstevel@tonic-gate 			ddi_driver_name(rdip), ddi_get_instance(rdip),
10540Sstevel@tonic-gate 			PX_DMA_TYPE(mp));
10550Sstevel@tonic-gate 		return (DDI_FAILURE);
10560Sstevel@tonic-gate 	}
10570Sstevel@tonic-gate 	if (cookiep)
10580Sstevel@tonic-gate 		DBG(DBG_DMA_WIN, dip,
10590Sstevel@tonic-gate 			"cookie - dmac_address=%x dmac_size=%x\n",
10600Sstevel@tonic-gate 			cookiep->dmac_address, cookiep->dmac_size);
10610Sstevel@tonic-gate 	if (offp)
10620Sstevel@tonic-gate 		*offp = (off_t)mp->dmai_offset;
10630Sstevel@tonic-gate 	if (lenp)
10640Sstevel@tonic-gate 		*lenp = mp->dmai_size;
10650Sstevel@tonic-gate 	return (DDI_SUCCESS);
10660Sstevel@tonic-gate }
10670Sstevel@tonic-gate 
10680Sstevel@tonic-gate #ifdef	DEBUG
10690Sstevel@tonic-gate static char *px_dmactl_str[] = {
10700Sstevel@tonic-gate 	"DDI_DMA_FREE",
10710Sstevel@tonic-gate 	"DDI_DMA_SYNC",
10720Sstevel@tonic-gate 	"DDI_DMA_HTOC",
10730Sstevel@tonic-gate 	"DDI_DMA_KVADDR",
10740Sstevel@tonic-gate 	"DDI_DMA_MOVWIN",
10750Sstevel@tonic-gate 	"DDI_DMA_REPWIN",
10760Sstevel@tonic-gate 	"DDI_DMA_GETERR",
10770Sstevel@tonic-gate 	"DDI_DMA_COFF",
10780Sstevel@tonic-gate 	"DDI_DMA_NEXTWIN",
10790Sstevel@tonic-gate 	"DDI_DMA_NEXTSEG",
10800Sstevel@tonic-gate 	"DDI_DMA_SEGTOC",
10810Sstevel@tonic-gate 	"DDI_DMA_RESERVE",
10820Sstevel@tonic-gate 	"DDI_DMA_RELEASE",
10830Sstevel@tonic-gate 	"DDI_DMA_RESETH",
10840Sstevel@tonic-gate 	"DDI_DMA_CKSYNC",
10850Sstevel@tonic-gate 	"DDI_DMA_IOPB_ALLOC",
10860Sstevel@tonic-gate 	"DDI_DMA_IOPB_FREE",
10870Sstevel@tonic-gate 	"DDI_DMA_SMEM_ALLOC",
10880Sstevel@tonic-gate 	"DDI_DMA_SMEM_FREE",
10890Sstevel@tonic-gate 	"DDI_DMA_SET_SBUS64"
10900Sstevel@tonic-gate };
10910Sstevel@tonic-gate #endif	/* DEBUG */
10920Sstevel@tonic-gate 
10930Sstevel@tonic-gate /*
10940Sstevel@tonic-gate  * bus dma control entry point:
10950Sstevel@tonic-gate  */
10960Sstevel@tonic-gate /*ARGSUSED*/
10970Sstevel@tonic-gate int
10980Sstevel@tonic-gate px_dma_ctlops(dev_info_t *dip, dev_info_t *rdip, ddi_dma_handle_t handle,
10990Sstevel@tonic-gate 	enum ddi_dma_ctlops cmd, off_t *offp, size_t *lenp, caddr_t *objp,
11000Sstevel@tonic-gate 	uint_t cache_flags)
11010Sstevel@tonic-gate {
11020Sstevel@tonic-gate 	ddi_dma_impl_t *mp = (ddi_dma_impl_t *)handle;
11030Sstevel@tonic-gate 
11040Sstevel@tonic-gate #ifdef	DEBUG
11050Sstevel@tonic-gate 	DBG(DBG_DMA_CTL, dip, "%s: rdip=%s%d\n", px_dmactl_str[cmd],
11060Sstevel@tonic-gate 		ddi_driver_name(rdip), ddi_get_instance(rdip));
11070Sstevel@tonic-gate #endif	/* DEBUG */
11080Sstevel@tonic-gate 
11090Sstevel@tonic-gate 	switch (cmd) {
11100Sstevel@tonic-gate 	case DDI_DMA_FREE:
11110Sstevel@tonic-gate 		(void) px_dma_unbindhdl(dip, rdip, handle);
11120Sstevel@tonic-gate 		(void) px_dma_freehdl(dip, rdip, handle);
11130Sstevel@tonic-gate 		return (DDI_SUCCESS);
11140Sstevel@tonic-gate 	case DDI_DMA_RESERVE: {
11150Sstevel@tonic-gate 		px_t *px_p = DIP_TO_STATE(dip);
11160Sstevel@tonic-gate 		return (px_fdvma_reserve(dip, rdip, px_p,
11170Sstevel@tonic-gate 			(ddi_dma_req_t *)offp, (ddi_dma_handle_t *)objp));
11180Sstevel@tonic-gate 		}
11190Sstevel@tonic-gate 	case DDI_DMA_RELEASE: {
11200Sstevel@tonic-gate 		px_t *px_p = DIP_TO_STATE(dip);
11210Sstevel@tonic-gate 		return (px_fdvma_release(dip, px_p, mp));
11220Sstevel@tonic-gate 		}
11230Sstevel@tonic-gate 	default:
11240Sstevel@tonic-gate 		break;
11250Sstevel@tonic-gate 	}
11260Sstevel@tonic-gate 
11270Sstevel@tonic-gate 	switch (PX_DMA_TYPE(mp)) {
11280Sstevel@tonic-gate 	case DMAI_FLAGS_DVMA:
11290Sstevel@tonic-gate 		return (px_dvma_ctl(dip, rdip, mp, cmd, offp, lenp, objp,
11300Sstevel@tonic-gate 			cache_flags));
11310Sstevel@tonic-gate 	case DMAI_FLAGS_PTP:
11320Sstevel@tonic-gate 	case DMAI_FLAGS_BYPASS:
11330Sstevel@tonic-gate 		return (px_dma_ctl(dip, rdip, mp, cmd, offp, lenp, objp,
11340Sstevel@tonic-gate 			cache_flags));
11350Sstevel@tonic-gate 	default:
11360Sstevel@tonic-gate 		cmn_err(CE_PANIC, "%s%d: px_dma_ctlops(%x):bad dma type %x",
11370Sstevel@tonic-gate 			ddi_driver_name(rdip), ddi_get_instance(rdip), cmd,
11380Sstevel@tonic-gate 			mp->dmai_flags);
11390Sstevel@tonic-gate 		/*NOTREACHED*/
11400Sstevel@tonic-gate 	}
11410Sstevel@tonic-gate }
11420Sstevel@tonic-gate 
11430Sstevel@tonic-gate /*
11440Sstevel@tonic-gate  * control ops entry point:
11450Sstevel@tonic-gate  *
11460Sstevel@tonic-gate  * Requests handled completely:
11470Sstevel@tonic-gate  *	DDI_CTLOPS_INITCHILD	see init_child() for details
11480Sstevel@tonic-gate  *	DDI_CTLOPS_UNINITCHILD
11490Sstevel@tonic-gate  *	DDI_CTLOPS_REPORTDEV	see report_dev() for details
11500Sstevel@tonic-gate  *	DDI_CTLOPS_XLATE_INTRS	nothing to do
11510Sstevel@tonic-gate  *	DDI_CTLOPS_IOMIN	cache line size if streaming otherwise 1
11520Sstevel@tonic-gate  *	DDI_CTLOPS_REGSIZE
11530Sstevel@tonic-gate  *	DDI_CTLOPS_NREGS
11540Sstevel@tonic-gate  *	DDI_CTLOPS_NINTRS
11550Sstevel@tonic-gate  *	DDI_CTLOPS_DVMAPAGESIZE
11560Sstevel@tonic-gate  *	DDI_CTLOPS_POKE
11570Sstevel@tonic-gate  *	DDI_CTLOPS_PEEK
11580Sstevel@tonic-gate  *
11590Sstevel@tonic-gate  * All others passed to parent.
11600Sstevel@tonic-gate  */
11610Sstevel@tonic-gate int
11620Sstevel@tonic-gate px_ctlops(dev_info_t *dip, dev_info_t *rdip,
11630Sstevel@tonic-gate 	ddi_ctl_enum_t op, void *arg, void *result)
11640Sstevel@tonic-gate {
11650Sstevel@tonic-gate 	px_t *px_p = DIP_TO_STATE(dip);
11660Sstevel@tonic-gate 	struct detachspec *ds;
11670Sstevel@tonic-gate 	struct attachspec *as;
11680Sstevel@tonic-gate 
11690Sstevel@tonic-gate 	switch (op) {
11700Sstevel@tonic-gate 	case DDI_CTLOPS_INITCHILD:
11710Sstevel@tonic-gate 		return (px_init_child(px_p, (dev_info_t *)arg));
11720Sstevel@tonic-gate 
11730Sstevel@tonic-gate 	case DDI_CTLOPS_UNINITCHILD:
11740Sstevel@tonic-gate 		return (px_uninit_child(px_p, (dev_info_t *)arg));
11750Sstevel@tonic-gate 
11760Sstevel@tonic-gate 	case DDI_CTLOPS_ATTACH:
11770Sstevel@tonic-gate 		as = (struct attachspec *)arg;
11780Sstevel@tonic-gate 		switch (as->when) {
11790Sstevel@tonic-gate 		case DDI_PRE:
11800Sstevel@tonic-gate 			if (as->cmd == DDI_ATTACH) {
11810Sstevel@tonic-gate 				DBG(DBG_PWR, dip, "PRE_ATTACH for %s@%d\n",
11820Sstevel@tonic-gate 				    ddi_driver_name(rdip),
11830Sstevel@tonic-gate 				    ddi_get_instance(rdip));
11840Sstevel@tonic-gate 				return (pcie_pm_hold(dip));
11850Sstevel@tonic-gate 			}
1186383Set142600 			if (as->cmd == DDI_RESUME) {
1187383Set142600 				ddi_acc_handle_t	config_handle;
1188383Set142600 				DBG(DBG_PWR, dip, "PRE_RESUME for %s@%d\n",
1189383Set142600 				    ddi_driver_name(rdip),
1190383Set142600 				    ddi_get_instance(rdip));
1191383Set142600 
1192383Set142600 				if (pci_config_setup(rdip, &config_handle) ==
1193383Set142600 				    DDI_SUCCESS) {
1194383Set142600 					pcie_clear_errors(rdip, config_handle);
1195383Set142600 					pci_config_teardown(&config_handle);
1196383Set142600 				}
1197383Set142600 			}
11980Sstevel@tonic-gate 			return (DDI_SUCCESS);
11990Sstevel@tonic-gate 
12000Sstevel@tonic-gate 		case DDI_POST:
12010Sstevel@tonic-gate 			DBG(DBG_PWR, dip, "POST_ATTACH for %s@%d\n",
12020Sstevel@tonic-gate 			    ddi_driver_name(rdip), ddi_get_instance(rdip));
12030Sstevel@tonic-gate 			if (as->cmd == DDI_ATTACH && as->result != DDI_SUCCESS)
12040Sstevel@tonic-gate 				pcie_pm_release(dip);
12050Sstevel@tonic-gate 			return (DDI_SUCCESS);
12060Sstevel@tonic-gate 		default:
12070Sstevel@tonic-gate 			break;
12080Sstevel@tonic-gate 		}
12090Sstevel@tonic-gate 		break;
12100Sstevel@tonic-gate 
12110Sstevel@tonic-gate 	case DDI_CTLOPS_DETACH:
12120Sstevel@tonic-gate 		ds = (struct detachspec *)arg;
12130Sstevel@tonic-gate 		switch (ds->when) {
12140Sstevel@tonic-gate 		case DDI_POST:
12150Sstevel@tonic-gate 			if (ds->cmd == DDI_DETACH &&
12160Sstevel@tonic-gate 			    ds->result == DDI_SUCCESS) {
12170Sstevel@tonic-gate 				DBG(DBG_PWR, dip, "POST_DETACH for %s@%d\n",
12180Sstevel@tonic-gate 				    ddi_driver_name(rdip),
12190Sstevel@tonic-gate 				    ddi_get_instance(rdip));
12200Sstevel@tonic-gate 				return (pcie_pm_remove_child(dip, rdip));
12210Sstevel@tonic-gate 			}
12220Sstevel@tonic-gate 			return (DDI_SUCCESS);
12230Sstevel@tonic-gate 		default:
12240Sstevel@tonic-gate 			break;
12250Sstevel@tonic-gate 		}
12260Sstevel@tonic-gate 		break;
12270Sstevel@tonic-gate 
12280Sstevel@tonic-gate 	case DDI_CTLOPS_REPORTDEV:
12290Sstevel@tonic-gate 		return (px_report_dev(rdip));
12300Sstevel@tonic-gate 
12310Sstevel@tonic-gate 	case DDI_CTLOPS_IOMIN:
12320Sstevel@tonic-gate 		return (DDI_SUCCESS);
12330Sstevel@tonic-gate 
12340Sstevel@tonic-gate 	case DDI_CTLOPS_REGSIZE:
12350Sstevel@tonic-gate 		*((off_t *)result) = px_get_reg_set_size(rdip, *((int *)arg));
123627Sjchu 		return (*((off_t *)result) == 0 ? DDI_FAILURE : DDI_SUCCESS);
12370Sstevel@tonic-gate 
12380Sstevel@tonic-gate 	case DDI_CTLOPS_NREGS:
12390Sstevel@tonic-gate 		*((uint_t *)result) = px_get_nreg_set(rdip);
12400Sstevel@tonic-gate 		return (DDI_SUCCESS);
12410Sstevel@tonic-gate 
12420Sstevel@tonic-gate 	case DDI_CTLOPS_DVMAPAGESIZE:
12430Sstevel@tonic-gate 		*((ulong_t *)result) = MMU_PAGE_SIZE;
12440Sstevel@tonic-gate 		return (DDI_SUCCESS);
12450Sstevel@tonic-gate 
12460Sstevel@tonic-gate 	case DDI_CTLOPS_POKE:	/* platform dependent implementation. */
12470Sstevel@tonic-gate 		return (px_lib_ctlops_poke(dip, rdip,
12480Sstevel@tonic-gate 		    (peekpoke_ctlops_t *)arg));
12490Sstevel@tonic-gate 
12500Sstevel@tonic-gate 	case DDI_CTLOPS_PEEK:	/* platform dependent implementation. */
12510Sstevel@tonic-gate 		return (px_lib_ctlops_peek(dip, rdip,
12520Sstevel@tonic-gate 		    (peekpoke_ctlops_t *)arg, result));
12530Sstevel@tonic-gate 
12540Sstevel@tonic-gate 	case DDI_CTLOPS_POWER:
12550Sstevel@tonic-gate 	default:
12560Sstevel@tonic-gate 		break;
12570Sstevel@tonic-gate 	}
12580Sstevel@tonic-gate 
12590Sstevel@tonic-gate 	/*
12600Sstevel@tonic-gate 	 * Now pass the request up to our parent.
12610Sstevel@tonic-gate 	 */
12620Sstevel@tonic-gate 	DBG(DBG_CTLOPS, dip, "passing request to parent: rdip=%s%d\n",
12630Sstevel@tonic-gate 		ddi_driver_name(rdip), ddi_get_instance(rdip));
12640Sstevel@tonic-gate 	return (ddi_ctlops(dip, rdip, op, arg, result));
12650Sstevel@tonic-gate }
12660Sstevel@tonic-gate 
12670Sstevel@tonic-gate /* ARGSUSED */
12680Sstevel@tonic-gate int
12690Sstevel@tonic-gate px_intr_ops(dev_info_t *dip, dev_info_t *rdip, ddi_intr_op_t intr_op,
12700Sstevel@tonic-gate     ddi_intr_handle_impl_t *hdlp, void *result)
12710Sstevel@tonic-gate {
12720Sstevel@tonic-gate 	int	intr_types, ret = DDI_SUCCESS;
12730Sstevel@tonic-gate 
12740Sstevel@tonic-gate 	DBG(DBG_INTROPS, dip, "px_intr_ops: rdip=%s%d\n",
12750Sstevel@tonic-gate 	    ddi_driver_name(rdip), ddi_get_instance(rdip));
12760Sstevel@tonic-gate 
12770Sstevel@tonic-gate 	/* Process DDI_INTROP_SUPPORTED_TYPES request here */
12780Sstevel@tonic-gate 	if (intr_op == DDI_INTROP_SUPPORTED_TYPES) {
12790Sstevel@tonic-gate 		px_t		*px_p = DIP_TO_STATE(dip);
12800Sstevel@tonic-gate 		px_msi_state_t	*msi_state_p = &px_p->px_ib_p->ib_msi_state;
12810Sstevel@tonic-gate 
12820Sstevel@tonic-gate 		*(int *)result = i_ddi_get_nintrs(rdip) ?
12830Sstevel@tonic-gate 		    DDI_INTR_TYPE_FIXED : 0;
12840Sstevel@tonic-gate 
12850Sstevel@tonic-gate 		if ((pci_msi_get_supported_type(rdip,
12860Sstevel@tonic-gate 		    &intr_types)) == DDI_SUCCESS) {
12870Sstevel@tonic-gate 			/*
12880Sstevel@tonic-gate 			 * Double check supported interrupt types vs.
12890Sstevel@tonic-gate 			 * what the host bridge supports.
12900Sstevel@tonic-gate 			 */
12910Sstevel@tonic-gate 			*(int *)result |= (intr_types & msi_state_p->msi_type);
12920Sstevel@tonic-gate 		}
12930Sstevel@tonic-gate 
12940Sstevel@tonic-gate 		return (ret);
12950Sstevel@tonic-gate 	}
12960Sstevel@tonic-gate 
12970Sstevel@tonic-gate 	/*
12980Sstevel@tonic-gate 	 * PCI-E nexus driver supports fixed, MSI and MSI-X interrupts.
12990Sstevel@tonic-gate 	 * Return failure if interrupt type is not supported.
13000Sstevel@tonic-gate 	 */
13010Sstevel@tonic-gate 	switch (hdlp->ih_type) {
13020Sstevel@tonic-gate 	case DDI_INTR_TYPE_FIXED:
13030Sstevel@tonic-gate 		ret = px_intx_ops(dip, rdip, intr_op, hdlp, result);
13040Sstevel@tonic-gate 		break;
13050Sstevel@tonic-gate 	case DDI_INTR_TYPE_MSI:
13060Sstevel@tonic-gate 	case DDI_INTR_TYPE_MSIX:
13070Sstevel@tonic-gate 		ret = px_msix_ops(dip, rdip, intr_op, hdlp, result);
13080Sstevel@tonic-gate 		break;
13090Sstevel@tonic-gate 	default:
13100Sstevel@tonic-gate 		ret = DDI_ENOTSUP;
13110Sstevel@tonic-gate 		break;
13120Sstevel@tonic-gate 	}
13130Sstevel@tonic-gate 
13140Sstevel@tonic-gate 	return (ret);
13150Sstevel@tonic-gate }
1316