xref: /onnv-gate/usr/src/uts/sun4u/io/pci/pci.c (revision 12027:6c3efe08f39d)
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
51865Sdilpreet  * Common Development and Distribution License (the "License").
61865Sdilpreet  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*12027SStephen.Hanson@Sun.COM  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate  * PCI nexus driver interface
280Sstevel@tonic-gate  */
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #include <sys/types.h>
310Sstevel@tonic-gate #include <sys/conf.h>		/* nulldev */
320Sstevel@tonic-gate #include <sys/stat.h>		/* devctl */
330Sstevel@tonic-gate #include <sys/kmem.h>
340Sstevel@tonic-gate #include <sys/async.h>		/* ecc_flt for pci_ecc.h */
350Sstevel@tonic-gate #include <sys/sunddi.h>
360Sstevel@tonic-gate #include <sys/sunndi.h>
370Sstevel@tonic-gate #include <sys/ndifm.h>
380Sstevel@tonic-gate #include <sys/ontrap.h>
390Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
400Sstevel@tonic-gate #include <sys/ddi_subrdefs.h>
410Sstevel@tonic-gate #include <sys/epm.h>
420Sstevel@tonic-gate #include <sys/hotplug/pci/pcihp.h>
43777Sschwartz #include <sys/pci/pci_tools_ext.h>
440Sstevel@tonic-gate #include <sys/spl.h>
450Sstevel@tonic-gate #include <sys/pci/pci_obj.h>
460Sstevel@tonic-gate 
470Sstevel@tonic-gate /*LINTLIBRARY*/
480Sstevel@tonic-gate 
490Sstevel@tonic-gate /*
500Sstevel@tonic-gate  * function prototype for hotplug routine:
510Sstevel@tonic-gate  */
520Sstevel@tonic-gate static void
530Sstevel@tonic-gate pci_init_hotplug(struct pci *);
540Sstevel@tonic-gate 
550Sstevel@tonic-gate /*
560Sstevel@tonic-gate  * function prototypes for dev ops routines:
570Sstevel@tonic-gate  */
580Sstevel@tonic-gate static int pci_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
590Sstevel@tonic-gate static int pci_detach(dev_info_t *dip, ddi_detach_cmd_t cmd);
600Sstevel@tonic-gate static int pci_info(dev_info_t *dip, ddi_info_cmd_t infocmd,
610Sstevel@tonic-gate 	void *arg, void **result);
620Sstevel@tonic-gate static int pci_ctlops_poke(pci_t *pci_p, peekpoke_ctlops_t *in_args);
630Sstevel@tonic-gate static int pci_ctlops_peek(pci_t *pci_p, peekpoke_ctlops_t *in_args,
640Sstevel@tonic-gate     void *result);
650Sstevel@tonic-gate static off_t get_reg_set_size(dev_info_t *child, int rnumber);
660Sstevel@tonic-gate 
670Sstevel@tonic-gate /*
680Sstevel@tonic-gate  * bus ops and dev ops structures:
690Sstevel@tonic-gate  */
700Sstevel@tonic-gate static struct bus_ops pci_bus_ops = {
710Sstevel@tonic-gate 	BUSO_REV,
720Sstevel@tonic-gate 	pci_map,
730Sstevel@tonic-gate 	0,
740Sstevel@tonic-gate 	0,
750Sstevel@tonic-gate 	0,
760Sstevel@tonic-gate 	i_ddi_map_fault,
770Sstevel@tonic-gate 	pci_dma_setup,
780Sstevel@tonic-gate 	pci_dma_allochdl,
790Sstevel@tonic-gate 	pci_dma_freehdl,
800Sstevel@tonic-gate 	pci_dma_bindhdl,
810Sstevel@tonic-gate 	pci_dma_unbindhdl,
820Sstevel@tonic-gate 	pci_dma_sync,
830Sstevel@tonic-gate 	pci_dma_win,
840Sstevel@tonic-gate 	pci_dma_ctlops,
850Sstevel@tonic-gate 	pci_ctlops,
860Sstevel@tonic-gate 	ddi_bus_prop_op,
870Sstevel@tonic-gate 	ndi_busop_get_eventcookie,	/* (*bus_get_eventcookie)(); */
880Sstevel@tonic-gate 	ndi_busop_add_eventcall,	/* (*bus_add_eventcall)(); */
890Sstevel@tonic-gate 	ndi_busop_remove_eventcall,	/* (*bus_remove_eventcall)(); */
900Sstevel@tonic-gate 	ndi_post_event,			/* (*bus_post_event)(); */
910Sstevel@tonic-gate 	NULL,				/* (*bus_intr_ctl)(); */
920Sstevel@tonic-gate 	NULL,				/* (*bus_config)(); */
930Sstevel@tonic-gate 	NULL,				/* (*bus_unconfig)(); */
940Sstevel@tonic-gate 	pci_fm_init_child,		/* (*bus_fm_init)(); */
950Sstevel@tonic-gate 	NULL,				/* (*bus_fm_fini)(); */
960Sstevel@tonic-gate 	pci_bus_enter,			/* (*bus_fm_access_enter)(); */
970Sstevel@tonic-gate 	pci_bus_exit,			/* (*bus_fm_access_fini)(); */
980Sstevel@tonic-gate 	NULL,				/* (*bus_power)(); */
990Sstevel@tonic-gate 	pci_intr_ops			/* (*bus_intr_op)(); */
1000Sstevel@tonic-gate };
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate extern struct cb_ops pci_cb_ops;
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate static struct dev_ops pci_ops = {
1050Sstevel@tonic-gate 	DEVO_REV,
1060Sstevel@tonic-gate 	0,
1070Sstevel@tonic-gate 	pci_info,
1080Sstevel@tonic-gate 	nulldev,
1090Sstevel@tonic-gate 	0,
1100Sstevel@tonic-gate 	pci_attach,
1110Sstevel@tonic-gate 	pci_detach,
1120Sstevel@tonic-gate 	nodev,
1130Sstevel@tonic-gate 	&pci_cb_ops,
1140Sstevel@tonic-gate 	&pci_bus_ops,
1157656SSherry.Moore@Sun.COM 	0,
1167656SSherry.Moore@Sun.COM 	ddi_quiesce_not_supported,	/* devo_quiesce */
1170Sstevel@tonic-gate };
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate /*
1200Sstevel@tonic-gate  * module definitions:
1210Sstevel@tonic-gate  */
1220Sstevel@tonic-gate #include <sys/modctl.h>
1230Sstevel@tonic-gate extern struct mod_ops mod_driverops;
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate static struct modldrv modldrv = {
12610923SEvan.Yan@Sun.COM 	&mod_driverops, 			/* Type of module - driver */
12710923SEvan.Yan@Sun.COM 	"Sun4u Host to PCI nexus driver",	/* Name of module. */
12810923SEvan.Yan@Sun.COM 	&pci_ops,				/* driver ops */
1290Sstevel@tonic-gate };
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate static struct modlinkage modlinkage = {
1320Sstevel@tonic-gate 	MODREV_1, (void *)&modldrv, NULL
1330Sstevel@tonic-gate };
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate /*
1360Sstevel@tonic-gate  * driver global data:
1370Sstevel@tonic-gate  */
1380Sstevel@tonic-gate void *per_pci_state;		/* per-pbm soft state pointer */
1390Sstevel@tonic-gate void *per_pci_common_state;	/* per-psycho soft state pointer */
1400Sstevel@tonic-gate kmutex_t pci_global_mutex;	/* attach/detach common struct lock */
1410Sstevel@tonic-gate errorq_t *pci_ecc_queue = NULL;	/* per-system ecc handling queue */
1421865Sdilpreet extern errorq_t *pci_target_queue;
143162Sschwartz struct cb_ops *pcihp_ops = NULL;	/* hotplug module cb ops */
144162Sschwartz 
1450Sstevel@tonic-gate extern void pci_child_cfg_save(dev_info_t *dip);
1460Sstevel@tonic-gate extern void pci_child_cfg_restore(dev_info_t *dip);
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate int
_init(void)1490Sstevel@tonic-gate _init(void)
1500Sstevel@tonic-gate {
1510Sstevel@tonic-gate 	int e;
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate 	/*
1540Sstevel@tonic-gate 	 * Initialize per-pci bus soft state pointer.
1550Sstevel@tonic-gate 	 */
1560Sstevel@tonic-gate 	e = ddi_soft_state_init(&per_pci_state, sizeof (pci_t), 1);
1570Sstevel@tonic-gate 	if (e != 0)
1580Sstevel@tonic-gate 		return (e);
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate 	/*
1610Sstevel@tonic-gate 	 * Initialize per-psycho soft state pointer.
1620Sstevel@tonic-gate 	 */
1630Sstevel@tonic-gate 	e = ddi_soft_state_init(&per_pci_common_state,
1647656SSherry.Moore@Sun.COM 	    sizeof (pci_common_t), 1);
1650Sstevel@tonic-gate 	if (e != 0) {
1660Sstevel@tonic-gate 		ddi_soft_state_fini(&per_pci_state);
1670Sstevel@tonic-gate 		return (e);
1680Sstevel@tonic-gate 	}
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 	/*
1710Sstevel@tonic-gate 	 * Initialize global mutexes.
1720Sstevel@tonic-gate 	 */
1730Sstevel@tonic-gate 	mutex_init(&pci_global_mutex, NULL, MUTEX_DRIVER, NULL);
1740Sstevel@tonic-gate 	pci_reloc_init();
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate 	/*
1770Sstevel@tonic-gate 	 * Create the performance kstats.
1780Sstevel@tonic-gate 	 */
1790Sstevel@tonic-gate 	pci_kstat_init();
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 	/*
1820Sstevel@tonic-gate 	 * Install the module.
1830Sstevel@tonic-gate 	 */
1840Sstevel@tonic-gate 	e = mod_install(&modlinkage);
1850Sstevel@tonic-gate 	if (e != 0) {
1860Sstevel@tonic-gate 		ddi_soft_state_fini(&per_pci_state);
1870Sstevel@tonic-gate 		ddi_soft_state_fini(&per_pci_common_state);
1880Sstevel@tonic-gate 		mutex_destroy(&pci_global_mutex);
1890Sstevel@tonic-gate 	}
1900Sstevel@tonic-gate 	return (e);
1910Sstevel@tonic-gate }
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate int
_fini(void)1940Sstevel@tonic-gate _fini(void)
1950Sstevel@tonic-gate {
1960Sstevel@tonic-gate 	int e;
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate 	/*
1990Sstevel@tonic-gate 	 * Remove the module.
2000Sstevel@tonic-gate 	 */
2010Sstevel@tonic-gate 	e = mod_remove(&modlinkage);
2020Sstevel@tonic-gate 	if (e != 0)
2030Sstevel@tonic-gate 		return (e);
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate 	/*
2060Sstevel@tonic-gate 	 * Destroy pci_ecc_queue, and set it to NULL.
2070Sstevel@tonic-gate 	 */
2080Sstevel@tonic-gate 	if (pci_ecc_queue)
2090Sstevel@tonic-gate 		errorq_destroy(pci_ecc_queue);
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate 	pci_ecc_queue = NULL;
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate 	/*
2140Sstevel@tonic-gate 	 * Destroy pci_target_queue, and set it to NULL.
2150Sstevel@tonic-gate 	 */
2160Sstevel@tonic-gate 	if (pci_target_queue)
2170Sstevel@tonic-gate 		errorq_destroy(pci_target_queue);
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 	pci_target_queue = NULL;
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate 	/*
2220Sstevel@tonic-gate 	 * Destroy the performance kstats.
2230Sstevel@tonic-gate 	 */
2240Sstevel@tonic-gate 	pci_kstat_fini();
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate 	/*
2270Sstevel@tonic-gate 	 * Free the per-pci and per-psycho soft state info and destroy
2280Sstevel@tonic-gate 	 * mutex for per-psycho soft state.
2290Sstevel@tonic-gate 	 */
2300Sstevel@tonic-gate 	ddi_soft_state_fini(&per_pci_state);
2310Sstevel@tonic-gate 	ddi_soft_state_fini(&per_pci_common_state);
2320Sstevel@tonic-gate 	mutex_destroy(&pci_global_mutex);
2330Sstevel@tonic-gate 	pci_reloc_fini();
2340Sstevel@tonic-gate 	return (e);
2350Sstevel@tonic-gate }
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate int
_info(struct modinfo * modinfop)2380Sstevel@tonic-gate _info(struct modinfo *modinfop)
2390Sstevel@tonic-gate {
2400Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
2410Sstevel@tonic-gate }
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate /*ARGSUSED*/
2440Sstevel@tonic-gate static int
pci_info(dev_info_t * dip,ddi_info_cmd_t infocmd,void * arg,void ** result)2450Sstevel@tonic-gate pci_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
2460Sstevel@tonic-gate {
2470Sstevel@tonic-gate 	int	instance = PCIHP_AP_MINOR_NUM_TO_INSTANCE(getminor((dev_t)arg));
2480Sstevel@tonic-gate 	pci_t	*pci_p = get_pci_soft_state(instance);
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate 	/* allow hotplug to deal with ones it manages */
2510Sstevel@tonic-gate 	if (pci_p && (pci_p->hotplug_capable == B_TRUE))
2520Sstevel@tonic-gate 		return (pcihp_info(dip, infocmd, arg, result));
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate 	/* non-hotplug or not attached */
2550Sstevel@tonic-gate 	switch (infocmd) {
2560Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
257946Smathue 		*result = (void *)(uintptr_t)instance;
2580Sstevel@tonic-gate 		return (DDI_SUCCESS);
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
2610Sstevel@tonic-gate 		if (pci_p == NULL)
2620Sstevel@tonic-gate 			return (DDI_FAILURE);
2630Sstevel@tonic-gate 		*result = (void *)pci_p->pci_dip;
2640Sstevel@tonic-gate 		return (DDI_SUCCESS);
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate 	default:
2670Sstevel@tonic-gate 		return (DDI_FAILURE);
2680Sstevel@tonic-gate 	}
2690Sstevel@tonic-gate }
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate /* device driver entry points */
2730Sstevel@tonic-gate /*
2740Sstevel@tonic-gate  * attach entry point:
2750Sstevel@tonic-gate  */
2760Sstevel@tonic-gate static int
pci_attach(dev_info_t * dip,ddi_attach_cmd_t cmd)2770Sstevel@tonic-gate pci_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
2780Sstevel@tonic-gate {
2790Sstevel@tonic-gate 	pci_t *pci_p;			/* per bus state pointer */
2800Sstevel@tonic-gate 	int instance = ddi_get_instance(dip);
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate 	switch (cmd) {
2830Sstevel@tonic-gate 	case DDI_ATTACH:
2840Sstevel@tonic-gate 		DEBUG0(DBG_ATTACH, dip, "DDI_ATTACH\n");
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate 		/*
2870Sstevel@tonic-gate 		 * Allocate and get the per-pci soft state structure.
2880Sstevel@tonic-gate 		 */
2890Sstevel@tonic-gate 		if (alloc_pci_soft_state(instance) != DDI_SUCCESS) {
2900Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s%d: can't allocate pci state",
2917656SSherry.Moore@Sun.COM 			    ddi_driver_name(dip), instance);
2920Sstevel@tonic-gate 			goto err_bad_pci_softstate;
2930Sstevel@tonic-gate 		}
2940Sstevel@tonic-gate 		pci_p = get_pci_soft_state(instance);
2950Sstevel@tonic-gate 		pci_p->pci_dip = dip;
2960Sstevel@tonic-gate 		mutex_init(&pci_p->pci_mutex, NULL, MUTEX_DRIVER, NULL);
2970Sstevel@tonic-gate 		pci_p->pci_soft_state = PCI_SOFT_STATE_CLOSED;
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate 		/*
3000Sstevel@tonic-gate 		 * Get key properties of the pci bridge node and
3010Sstevel@tonic-gate 		 * determine it's type (psycho, schizo, etc ...).
3020Sstevel@tonic-gate 		 */
3030Sstevel@tonic-gate 		if (get_pci_properties(pci_p, dip) == DDI_FAILURE)
3040Sstevel@tonic-gate 			goto err_bad_pci_prop;
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate 		/*
3070Sstevel@tonic-gate 		 * Map in the registers.
3080Sstevel@tonic-gate 		 */
3090Sstevel@tonic-gate 		if (map_pci_registers(pci_p, dip) == DDI_FAILURE)
3100Sstevel@tonic-gate 			goto err_bad_reg_prop;
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate 		if (pci_obj_setup(pci_p) != DDI_SUCCESS)
3130Sstevel@tonic-gate 			goto err_bad_objs;
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate 		/*
3160Sstevel@tonic-gate 		 * If this PCI leaf has hotplug and this platform
3170Sstevel@tonic-gate 		 * loads hotplug modules then initialize the
3180Sstevel@tonic-gate 		 * hotplug framework.
3190Sstevel@tonic-gate 		 */
3200Sstevel@tonic-gate 		pci_init_hotplug(pci_p);
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate 		/*
3230Sstevel@tonic-gate 		 * Create the "devctl" node for hotplug support.
3240Sstevel@tonic-gate 		 * For non-hotplug bus, we still need ":devctl" to
3250Sstevel@tonic-gate 		 * support DEVCTL_DEVICE_* and DEVCTL_BUS_* ioctls.
3260Sstevel@tonic-gate 		 */
3270Sstevel@tonic-gate 		if (pci_p->hotplug_capable == B_FALSE) {
3280Sstevel@tonic-gate 			if (ddi_create_minor_node(dip, "devctl", S_IFCHR,
3290Sstevel@tonic-gate 			    PCIHP_AP_MINOR_NUM(instance, PCIHP_DEVCTL_MINOR),
3300Sstevel@tonic-gate 			    DDI_NT_NEXUS, 0) != DDI_SUCCESS)
3310Sstevel@tonic-gate 				goto err_bad_devctl_node;
3320Sstevel@tonic-gate 		}
3330Sstevel@tonic-gate 
3340Sstevel@tonic-gate 		/*
335117Sschwartz 		 * Create pcitool nodes for register access and interrupt
336117Sschwartz 		 * routing.
337117Sschwartz 		 */
338117Sschwartz 		if (pcitool_init(dip) != DDI_SUCCESS) {
339117Sschwartz 			goto err_bad_pcitool_nodes;
340117Sschwartz 		}
3410Sstevel@tonic-gate 		ddi_report_dev(dip);
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate 		pci_p->pci_state = PCI_ATTACHED;
3440Sstevel@tonic-gate 		DEBUG0(DBG_ATTACH, dip, "attach success\n");
3450Sstevel@tonic-gate 		break;
3460Sstevel@tonic-gate 
347117Sschwartz err_bad_pcitool_nodes:
348117Sschwartz 		if (pci_p->hotplug_capable == B_FALSE)
349117Sschwartz 			ddi_remove_minor_node(dip, "devctl");
350117Sschwartz 		else
351117Sschwartz 			(void) pcihp_uninit(dip);
352117Sschwartz err_bad_devctl_node:
353117Sschwartz 		pci_obj_destroy(pci_p);
3540Sstevel@tonic-gate err_bad_objs:
3550Sstevel@tonic-gate 		unmap_pci_registers(pci_p);
3560Sstevel@tonic-gate err_bad_reg_prop:
3570Sstevel@tonic-gate 		free_pci_properties(pci_p);
3580Sstevel@tonic-gate err_bad_pci_prop:
3590Sstevel@tonic-gate 		mutex_destroy(&pci_p->pci_mutex);
3600Sstevel@tonic-gate 		free_pci_soft_state(instance);
3610Sstevel@tonic-gate err_bad_pci_softstate:
3620Sstevel@tonic-gate 		return (DDI_FAILURE);
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate 	case DDI_RESUME:
3650Sstevel@tonic-gate 		DEBUG0(DBG_ATTACH, dip, "DDI_RESUME\n");
3660Sstevel@tonic-gate 
3670Sstevel@tonic-gate 		/*
3680Sstevel@tonic-gate 		 * Make sure the Psycho control registers and IOMMU
3690Sstevel@tonic-gate 		 * are configured properly.
3700Sstevel@tonic-gate 		 */
3710Sstevel@tonic-gate 		pci_p = get_pci_soft_state(instance);
3720Sstevel@tonic-gate 		mutex_enter(&pci_p->pci_mutex);
3730Sstevel@tonic-gate 
3740Sstevel@tonic-gate 		/*
3750Sstevel@tonic-gate 		 * Make sure this instance has been suspended.
3760Sstevel@tonic-gate 		 */
3770Sstevel@tonic-gate 		if (pci_p->pci_state != PCI_SUSPENDED) {
3780Sstevel@tonic-gate 			DEBUG0(DBG_ATTACH, dip, "instance NOT suspended\n");
3790Sstevel@tonic-gate 			mutex_exit(&pci_p->pci_mutex);
3800Sstevel@tonic-gate 			return (DDI_FAILURE);
3810Sstevel@tonic-gate 		}
3820Sstevel@tonic-gate 		pci_obj_resume(pci_p);
3830Sstevel@tonic-gate 		pci_p->pci_state = PCI_ATTACHED;
3840Sstevel@tonic-gate 
3850Sstevel@tonic-gate 		pci_child_cfg_restore(dip);
3860Sstevel@tonic-gate 
3870Sstevel@tonic-gate 		mutex_exit(&pci_p->pci_mutex);
3880Sstevel@tonic-gate 		break;
3890Sstevel@tonic-gate 
3900Sstevel@tonic-gate 	default:
3910Sstevel@tonic-gate 		DEBUG0(DBG_ATTACH, dip, "unsupported attach op\n");
3920Sstevel@tonic-gate 		return (DDI_FAILURE);
3930Sstevel@tonic-gate 	}
3940Sstevel@tonic-gate 
3950Sstevel@tonic-gate 	return (DDI_SUCCESS);
3960Sstevel@tonic-gate }
3970Sstevel@tonic-gate 
3980Sstevel@tonic-gate /*
3990Sstevel@tonic-gate  * detach entry point:
4000Sstevel@tonic-gate  */
4010Sstevel@tonic-gate static int
pci_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)4020Sstevel@tonic-gate pci_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
4030Sstevel@tonic-gate {
4040Sstevel@tonic-gate 	int instance = ddi_get_instance(dip);
4050Sstevel@tonic-gate 	pci_t *pci_p = get_pci_soft_state(instance);
4060Sstevel@tonic-gate 
4070Sstevel@tonic-gate 	/*
4080Sstevel@tonic-gate 	 * Make sure we are currently attached
4090Sstevel@tonic-gate 	 */
4100Sstevel@tonic-gate 	if (pci_p->pci_state != PCI_ATTACHED) {
4110Sstevel@tonic-gate 		DEBUG0(DBG_ATTACH, dip, "failed - instance not attached\n");
4120Sstevel@tonic-gate 		return (DDI_FAILURE);
4130Sstevel@tonic-gate 	}
4140Sstevel@tonic-gate 
4150Sstevel@tonic-gate 	mutex_enter(&pci_p->pci_mutex);
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate 	switch (cmd) {
4180Sstevel@tonic-gate 	case DDI_DETACH:
4190Sstevel@tonic-gate 		DEBUG0(DBG_DETACH, dip, "DDI_DETACH\n");
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate 		if (pci_p->hotplug_capable == B_TRUE)
4220Sstevel@tonic-gate 			if (pcihp_uninit(dip) == DDI_FAILURE) {
4230Sstevel@tonic-gate 				mutex_exit(&pci_p->pci_mutex);
4240Sstevel@tonic-gate 				return (DDI_FAILURE);
4250Sstevel@tonic-gate 			}
4260Sstevel@tonic-gate 
427117Sschwartz 		pcitool_uninit(dip);
428117Sschwartz 
4290Sstevel@tonic-gate 		pci_obj_destroy(pci_p);
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate 		/*
4320Sstevel@tonic-gate 		 * Free the pci soft state structure and the rest of the
4330Sstevel@tonic-gate 		 * resources it's using.
4340Sstevel@tonic-gate 		 */
4350Sstevel@tonic-gate 		free_pci_properties(pci_p);
4360Sstevel@tonic-gate 		unmap_pci_registers(pci_p);
4370Sstevel@tonic-gate 		mutex_exit(&pci_p->pci_mutex);
4380Sstevel@tonic-gate 		mutex_destroy(&pci_p->pci_mutex);
4390Sstevel@tonic-gate 		free_pci_soft_state(instance);
4400Sstevel@tonic-gate 
4417656SSherry.Moore@Sun.COM 		/* Free the interrupt-priorities prop if we created it. */
4427656SSherry.Moore@Sun.COM 		{
4430Sstevel@tonic-gate 			int len;
4440Sstevel@tonic-gate 
4450Sstevel@tonic-gate 			if (ddi_getproplen(DDI_DEV_T_ANY, dip,
4460Sstevel@tonic-gate 			    DDI_PROP_NOTPROM | DDI_PROP_DONTPASS,
4470Sstevel@tonic-gate 			    "interrupt-priorities", &len) == DDI_PROP_SUCCESS)
4480Sstevel@tonic-gate 				(void) ddi_prop_remove(DDI_DEV_T_NONE, dip,
4490Sstevel@tonic-gate 				    "interrupt-priorities");
4500Sstevel@tonic-gate 		}
4510Sstevel@tonic-gate 		return (DDI_SUCCESS);
4520Sstevel@tonic-gate 
4530Sstevel@tonic-gate 	case DDI_SUSPEND:
4540Sstevel@tonic-gate 		pci_child_cfg_save(dip);
4550Sstevel@tonic-gate 		pci_obj_suspend(pci_p);
4560Sstevel@tonic-gate 		pci_p->pci_state = PCI_SUSPENDED;
4570Sstevel@tonic-gate 
4580Sstevel@tonic-gate 		mutex_exit(&pci_p->pci_mutex);
4590Sstevel@tonic-gate 		return (DDI_SUCCESS);
4600Sstevel@tonic-gate 
4610Sstevel@tonic-gate 	default:
4620Sstevel@tonic-gate 		DEBUG0(DBG_DETACH, dip, "unsupported detach op\n");
4630Sstevel@tonic-gate 		mutex_exit(&pci_p->pci_mutex);
4640Sstevel@tonic-gate 		return (DDI_FAILURE);
4650Sstevel@tonic-gate 	}
4660Sstevel@tonic-gate }
4670Sstevel@tonic-gate 
4680Sstevel@tonic-gate 
4690Sstevel@tonic-gate /* bus driver entry points */
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate /*
4720Sstevel@tonic-gate  * bus map entry point:
4730Sstevel@tonic-gate  *
4740Sstevel@tonic-gate  * 	if map request is for an rnumber
4750Sstevel@tonic-gate  *		get the corresponding regspec from device node
4760Sstevel@tonic-gate  * 	build a new regspec in our parent's format
4770Sstevel@tonic-gate  *	build a new map_req with the new regspec
4780Sstevel@tonic-gate  *	call up the tree to complete the mapping
4790Sstevel@tonic-gate  */
4800Sstevel@tonic-gate int
pci_map(dev_info_t * dip,dev_info_t * rdip,ddi_map_req_t * mp,off_t off,off_t len,caddr_t * addrp)4810Sstevel@tonic-gate pci_map(dev_info_t *dip, dev_info_t *rdip, ddi_map_req_t *mp,
4820Sstevel@tonic-gate 	off_t off, off_t len, caddr_t *addrp)
4830Sstevel@tonic-gate {
4840Sstevel@tonic-gate 	pci_t *pci_p = get_pci_soft_state(ddi_get_instance(dip));
4850Sstevel@tonic-gate 	struct regspec p_regspec;
4860Sstevel@tonic-gate 	ddi_map_req_t p_mapreq;
4870Sstevel@tonic-gate 	int reglen, rval, r_no;
4880Sstevel@tonic-gate 	pci_regspec_t reloc_reg, *rp = &reloc_reg;
4890Sstevel@tonic-gate 
4900Sstevel@tonic-gate 	DEBUG2(DBG_MAP, dip, "rdip=%s%d:",
4917656SSherry.Moore@Sun.COM 	    ddi_driver_name(rdip), ddi_get_instance(rdip));
4920Sstevel@tonic-gate 
4930Sstevel@tonic-gate 	if (mp->map_flags & DDI_MF_USER_MAPPING)
4940Sstevel@tonic-gate 		return (DDI_ME_UNIMPLEMENTED);
4950Sstevel@tonic-gate 
4960Sstevel@tonic-gate 	switch (mp->map_type) {
4970Sstevel@tonic-gate 	case DDI_MT_REGSPEC:
4980Sstevel@tonic-gate 		reloc_reg = *(pci_regspec_t *)mp->map_obj.rp;	/* dup whole */
4990Sstevel@tonic-gate 		break;
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate 	case DDI_MT_RNUMBER:
5020Sstevel@tonic-gate 		r_no = mp->map_obj.rnumber;
5030Sstevel@tonic-gate 		DEBUG1(DBG_MAP | DBG_CONT, dip, " r#=%x", r_no);
5040Sstevel@tonic-gate 
505506Scth 		if (ddi_getlongprop(DDI_DEV_T_ANY, rdip, DDI_PROP_DONTPASS,
5067656SSherry.Moore@Sun.COM 		    "reg", (caddr_t)&rp, &reglen) != DDI_SUCCESS)
5070Sstevel@tonic-gate 				return (DDI_ME_RNUMBER_RANGE);
5080Sstevel@tonic-gate 
5090Sstevel@tonic-gate 		if (r_no < 0 || r_no >= reglen / sizeof (pci_regspec_t)) {
5100Sstevel@tonic-gate 			kmem_free(rp, reglen);
5110Sstevel@tonic-gate 			return (DDI_ME_RNUMBER_RANGE);
5120Sstevel@tonic-gate 		}
5130Sstevel@tonic-gate 		rp += r_no;
5140Sstevel@tonic-gate 		break;
5150Sstevel@tonic-gate 
5160Sstevel@tonic-gate 	default:
5170Sstevel@tonic-gate 		return (DDI_ME_INVAL);
5180Sstevel@tonic-gate 	}
5190Sstevel@tonic-gate 	DEBUG0(DBG_MAP | DBG_CONT, dip, "\n");
5200Sstevel@tonic-gate 
5210Sstevel@tonic-gate 	/* use "assigned-addresses" to relocate regspec within pci space */
5220Sstevel@tonic-gate 	if (rval = pci_reloc_reg(dip, rdip, pci_p, rp))
5230Sstevel@tonic-gate 		goto done;
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate 	if (len)	/* adjust regspec according to mapping request */
5260Sstevel@tonic-gate 		rp->pci_size_low = len;
5270Sstevel@tonic-gate 	rp->pci_phys_low += off;
5280Sstevel@tonic-gate 
5290Sstevel@tonic-gate 	/* use "ranges" to translate relocated pci regspec into parent space */
5300Sstevel@tonic-gate 	if (rval = pci_xlate_reg(pci_p, rp, &p_regspec))
5310Sstevel@tonic-gate 		goto done;
5320Sstevel@tonic-gate 
5330Sstevel@tonic-gate 	p_mapreq = *mp;		/* dup the whole structure */
5340Sstevel@tonic-gate 	p_mapreq.map_type = DDI_MT_REGSPEC;
5350Sstevel@tonic-gate 	p_mapreq.map_obj.rp = &p_regspec;
5360Sstevel@tonic-gate 	rval = ddi_map(dip, &p_mapreq, 0, 0, addrp);
5370Sstevel@tonic-gate 
5380Sstevel@tonic-gate 	if (rval == DDI_SUCCESS) {
5390Sstevel@tonic-gate 		/*
5400Sstevel@tonic-gate 		 * Set-up access functions for FM access error capable drivers.
5410Sstevel@tonic-gate 		 * The axq workaround prevents fault management support
5420Sstevel@tonic-gate 		 */
5430Sstevel@tonic-gate 		if (DDI_FM_ACC_ERR_CAP(pci_p->pci_fm_cap) &&
5440Sstevel@tonic-gate 		    DDI_FM_ACC_ERR_CAP(ddi_fm_capable(rdip)) &&
5450Sstevel@tonic-gate 		    mp->map_handlep->ah_acc.devacc_attr_access !=
5460Sstevel@tonic-gate 		    DDI_DEFAULT_ACC)
5470Sstevel@tonic-gate 			pci_fm_acc_setup(mp, rdip);
5480Sstevel@tonic-gate 		pci_axq_setup(mp, pci_p->pci_pbm_p);
5490Sstevel@tonic-gate 	}
5500Sstevel@tonic-gate 
5510Sstevel@tonic-gate done:
5520Sstevel@tonic-gate 	if (mp->map_type == DDI_MT_RNUMBER)
5530Sstevel@tonic-gate 		kmem_free(rp - r_no, reglen);
5540Sstevel@tonic-gate 
5550Sstevel@tonic-gate 	return (rval);
5560Sstevel@tonic-gate }
5570Sstevel@tonic-gate 
5580Sstevel@tonic-gate /*
5590Sstevel@tonic-gate  * bus dma map entry point
5600Sstevel@tonic-gate  * return value:
5610Sstevel@tonic-gate  *	DDI_DMA_PARTIAL_MAP	 1
5620Sstevel@tonic-gate  *	DDI_DMA_MAPOK		 0
5630Sstevel@tonic-gate  *	DDI_DMA_MAPPED		 0
5640Sstevel@tonic-gate  *	DDI_DMA_NORESOURCES	-1
5650Sstevel@tonic-gate  *	DDI_DMA_NOMAPPING	-2
5660Sstevel@tonic-gate  *	DDI_DMA_TOOBIG		-3
5670Sstevel@tonic-gate  */
5680Sstevel@tonic-gate int
pci_dma_setup(dev_info_t * dip,dev_info_t * rdip,ddi_dma_req_t * dmareq,ddi_dma_handle_t * handlep)5690Sstevel@tonic-gate pci_dma_setup(dev_info_t *dip, dev_info_t *rdip, ddi_dma_req_t *dmareq,
5700Sstevel@tonic-gate 	ddi_dma_handle_t *handlep)
5710Sstevel@tonic-gate {
5720Sstevel@tonic-gate 	pci_t *pci_p = get_pci_soft_state(ddi_get_instance(dip));
5730Sstevel@tonic-gate 	iommu_t *iommu_p = pci_p->pci_iommu_p;
5740Sstevel@tonic-gate 	ddi_dma_impl_t *mp;
5750Sstevel@tonic-gate 	int ret;
5760Sstevel@tonic-gate 
5770Sstevel@tonic-gate 	DEBUG3(DBG_DMA_MAP, dip, "mapping - rdip=%s%d type=%s\n",
5787656SSherry.Moore@Sun.COM 	    ddi_driver_name(rdip), ddi_get_instance(rdip),
5797656SSherry.Moore@Sun.COM 	    handlep ? "alloc" : "advisory");
5800Sstevel@tonic-gate 
5810Sstevel@tonic-gate 	if (!(mp = pci_dma_lmts2hdl(dip, rdip, iommu_p, dmareq)))
5820Sstevel@tonic-gate 		return (DDI_DMA_NORESOURCES);
5830Sstevel@tonic-gate 	if (mp == (ddi_dma_impl_t *)DDI_DMA_NOMAPPING)
5840Sstevel@tonic-gate 		return (DDI_DMA_NOMAPPING);
5850Sstevel@tonic-gate 	if (ret = pci_dma_type(pci_p, dmareq, mp))
5860Sstevel@tonic-gate 		goto freehandle;
5870Sstevel@tonic-gate 	if (ret = pci_dma_pfn(pci_p, dmareq, mp))
5880Sstevel@tonic-gate 		goto freehandle;
5890Sstevel@tonic-gate 
5900Sstevel@tonic-gate 	switch (PCI_DMA_TYPE(mp)) {
5910Sstevel@tonic-gate 	case DMAI_FLAGS_DVMA:	/* LINTED E_EQUALITY_NOT_ASSIGNMENT */
5920Sstevel@tonic-gate 		if ((ret = pci_dvma_win(pci_p, dmareq, mp)) || !handlep)
5930Sstevel@tonic-gate 			goto freehandle;
5940Sstevel@tonic-gate 		if (!PCI_DMA_CANCACHE(mp)) {	/* try fast track */
5950Sstevel@tonic-gate 			if (PCI_DMA_CANFAST(mp)) {
5960Sstevel@tonic-gate 				if (!pci_dvma_map_fast(iommu_p, mp))
5970Sstevel@tonic-gate 					break;
5980Sstevel@tonic-gate 			/* LINTED E_NOP_ELSE_STMT */
5990Sstevel@tonic-gate 			} else {
6000Sstevel@tonic-gate 				PCI_DVMA_FASTTRAK_PROF(mp);
6010Sstevel@tonic-gate 			}
6020Sstevel@tonic-gate 		}
6030Sstevel@tonic-gate 		if (ret = pci_dvma_map(mp, dmareq, iommu_p))
6040Sstevel@tonic-gate 			goto freehandle;
6050Sstevel@tonic-gate 		break;
6060Sstevel@tonic-gate 	case DMAI_FLAGS_PEER_TO_PEER:	/* LINTED E_EQUALITY_NOT_ASSIGNMENT */
6070Sstevel@tonic-gate 		if ((ret = pci_dma_physwin(pci_p, dmareq, mp)) || !handlep)
6080Sstevel@tonic-gate 			goto freehandle;
6090Sstevel@tonic-gate 		break;
6100Sstevel@tonic-gate 	case DMAI_FLAGS_BYPASS:
6110Sstevel@tonic-gate 	default:
6120Sstevel@tonic-gate 		panic("%s%d: pci_dma_setup: bad dma type 0x%x",
6130Sstevel@tonic-gate 		    ddi_driver_name(rdip), ddi_get_instance(rdip),
6140Sstevel@tonic-gate 		    PCI_DMA_TYPE(mp));
6150Sstevel@tonic-gate 		/*NOTREACHED*/
6160Sstevel@tonic-gate 	}
6170Sstevel@tonic-gate 	*handlep = (ddi_dma_handle_t)mp;
6180Sstevel@tonic-gate 	mp->dmai_flags |= (DMAI_FLAGS_INUSE | DMAI_FLAGS_MAPPED);
6190Sstevel@tonic-gate 	dump_dma_handle(DBG_DMA_MAP, dip, mp);
6200Sstevel@tonic-gate 
6210Sstevel@tonic-gate 	return ((mp->dmai_nwin == 1) ? DDI_DMA_MAPPED : DDI_DMA_PARTIAL_MAP);
6220Sstevel@tonic-gate freehandle:
6230Sstevel@tonic-gate 	if (ret == DDI_DMA_NORESOURCES)
6240Sstevel@tonic-gate 		pci_dma_freemp(mp); /* don't run_callback() */
6250Sstevel@tonic-gate 	else
6260Sstevel@tonic-gate 		(void) pci_dma_freehdl(dip, rdip, (ddi_dma_handle_t)mp);
6270Sstevel@tonic-gate 	return (ret);
6280Sstevel@tonic-gate }
6290Sstevel@tonic-gate 
6300Sstevel@tonic-gate 
6310Sstevel@tonic-gate /*
6320Sstevel@tonic-gate  * bus dma alloc handle entry point:
6330Sstevel@tonic-gate  */
6340Sstevel@tonic-gate int
pci_dma_allochdl(dev_info_t * dip,dev_info_t * rdip,ddi_dma_attr_t * attrp,int (* waitfp)(caddr_t),caddr_t arg,ddi_dma_handle_t * handlep)6350Sstevel@tonic-gate pci_dma_allochdl(dev_info_t *dip, dev_info_t *rdip, ddi_dma_attr_t *attrp,
6360Sstevel@tonic-gate 	int (*waitfp)(caddr_t), caddr_t arg, ddi_dma_handle_t *handlep)
6370Sstevel@tonic-gate {
6380Sstevel@tonic-gate 	pci_t *pci_p = get_pci_soft_state(ddi_get_instance(dip));
6390Sstevel@tonic-gate 	ddi_dma_impl_t *mp;
6400Sstevel@tonic-gate 	int rval;
6410Sstevel@tonic-gate 
6420Sstevel@tonic-gate 	DEBUG2(DBG_DMA_ALLOCH, dip, "rdip=%s%d\n",
6437656SSherry.Moore@Sun.COM 	    ddi_driver_name(rdip), ddi_get_instance(rdip));
6440Sstevel@tonic-gate 
6450Sstevel@tonic-gate 	if (attrp->dma_attr_version != DMA_ATTR_V0)
6460Sstevel@tonic-gate 		return (DDI_DMA_BADATTR);
6470Sstevel@tonic-gate 
6480Sstevel@tonic-gate 	if (!(mp = pci_dma_allocmp(dip, rdip, waitfp, arg)))
6490Sstevel@tonic-gate 		return (DDI_DMA_NORESOURCES);
6500Sstevel@tonic-gate 
6510Sstevel@tonic-gate 	/*
6520Sstevel@tonic-gate 	 * Save requestor's information
6530Sstevel@tonic-gate 	 */
6540Sstevel@tonic-gate 	mp->dmai_attr	= *attrp; /* whole object - augmented later  */
6550Sstevel@tonic-gate 	*DEV_ATTR(mp)	= *attrp; /* whole object - device orig attr */
6560Sstevel@tonic-gate 	DEBUG1(DBG_DMA_ALLOCH, dip, "mp=%p\n", mp);
6570Sstevel@tonic-gate 
6580Sstevel@tonic-gate 	/* check and convert dma attributes to handle parameters */
6590Sstevel@tonic-gate 	if (rval = pci_dma_attr2hdl(pci_p, mp)) {
6600Sstevel@tonic-gate 		pci_dma_freehdl(dip, rdip, (ddi_dma_handle_t)mp);
6610Sstevel@tonic-gate 		*handlep = NULL;
6620Sstevel@tonic-gate 		return (rval);
6630Sstevel@tonic-gate 	}
6640Sstevel@tonic-gate 	*handlep = (ddi_dma_handle_t)mp;
6650Sstevel@tonic-gate 	return (DDI_SUCCESS);
6660Sstevel@tonic-gate }
6670Sstevel@tonic-gate 
6680Sstevel@tonic-gate 
6690Sstevel@tonic-gate /*
6700Sstevel@tonic-gate  * bus dma free handle entry point:
6710Sstevel@tonic-gate  */
6720Sstevel@tonic-gate /*ARGSUSED*/
6730Sstevel@tonic-gate int
pci_dma_freehdl(dev_info_t * dip,dev_info_t * rdip,ddi_dma_handle_t handle)6740Sstevel@tonic-gate pci_dma_freehdl(dev_info_t *dip, dev_info_t *rdip, ddi_dma_handle_t handle)
6750Sstevel@tonic-gate {
6760Sstevel@tonic-gate 	DEBUG3(DBG_DMA_FREEH, dip, "rdip=%s%d mp=%p\n",
6777656SSherry.Moore@Sun.COM 	    ddi_driver_name(rdip), ddi_get_instance(rdip), handle);
6780Sstevel@tonic-gate 	pci_dma_freemp((ddi_dma_impl_t *)handle);
6790Sstevel@tonic-gate 
6800Sstevel@tonic-gate 	if (pci_kmem_clid) {
6810Sstevel@tonic-gate 		DEBUG0(DBG_DMA_FREEH, dip, "run handle callback\n");
6820Sstevel@tonic-gate 		ddi_run_callback(&pci_kmem_clid);
6830Sstevel@tonic-gate 	}
6840Sstevel@tonic-gate 	return (DDI_SUCCESS);
6850Sstevel@tonic-gate }
6860Sstevel@tonic-gate 
6870Sstevel@tonic-gate 
6880Sstevel@tonic-gate /*
6890Sstevel@tonic-gate  * bus dma bind handle entry point:
6900Sstevel@tonic-gate  */
6910Sstevel@tonic-gate int
pci_dma_bindhdl(dev_info_t * dip,dev_info_t * rdip,ddi_dma_handle_t handle,ddi_dma_req_t * dmareq,ddi_dma_cookie_t * cookiep,uint_t * ccountp)6920Sstevel@tonic-gate pci_dma_bindhdl(dev_info_t *dip, dev_info_t *rdip,
6930Sstevel@tonic-gate 	ddi_dma_handle_t handle, ddi_dma_req_t *dmareq,
6940Sstevel@tonic-gate 	ddi_dma_cookie_t *cookiep, uint_t *ccountp)
6950Sstevel@tonic-gate {
6960Sstevel@tonic-gate 	pci_t *pci_p = get_pci_soft_state(ddi_get_instance(dip));
6970Sstevel@tonic-gate 	iommu_t *iommu_p = pci_p->pci_iommu_p;
6980Sstevel@tonic-gate 	ddi_dma_impl_t *mp = (ddi_dma_impl_t *)handle;
6990Sstevel@tonic-gate 	int ret;
7000Sstevel@tonic-gate 
7010Sstevel@tonic-gate 	DEBUG4(DBG_DMA_BINDH, dip, "rdip=%s%d mp=%p dmareq=%p\n",
7027656SSherry.Moore@Sun.COM 	    ddi_driver_name(rdip), ddi_get_instance(rdip), mp, dmareq);
7030Sstevel@tonic-gate 
7040Sstevel@tonic-gate 	if (mp->dmai_flags & DMAI_FLAGS_INUSE)
7050Sstevel@tonic-gate 		return (DDI_DMA_INUSE);
7060Sstevel@tonic-gate 
7070Sstevel@tonic-gate 	ASSERT((mp->dmai_flags & ~DMAI_FLAGS_PRESERVE) == 0);
7080Sstevel@tonic-gate 	mp->dmai_flags |= DMAI_FLAGS_INUSE;
7090Sstevel@tonic-gate 
7100Sstevel@tonic-gate 	if (ret = pci_dma_type(pci_p, dmareq, mp))
7110Sstevel@tonic-gate 		goto err;
7120Sstevel@tonic-gate 	if (ret = pci_dma_pfn(pci_p, dmareq, mp))
7130Sstevel@tonic-gate 		goto err;
7140Sstevel@tonic-gate 
7150Sstevel@tonic-gate 	switch (PCI_DMA_TYPE(mp)) {
7160Sstevel@tonic-gate 	case DMAI_FLAGS_DVMA:
7170Sstevel@tonic-gate 		if (ret = pci_dvma_win(pci_p, dmareq, mp))
7180Sstevel@tonic-gate 			goto map_err;
7190Sstevel@tonic-gate 		if (!PCI_DMA_CANCACHE(mp)) {	/* try fast track */
7200Sstevel@tonic-gate 			if (PCI_DMA_CANFAST(mp)) {
7210Sstevel@tonic-gate 				if (!pci_dvma_map_fast(iommu_p, mp))
7220Sstevel@tonic-gate 					goto mapped; /*LINTED E_NOP_ELSE_STMT*/
7230Sstevel@tonic-gate 			} else {
7240Sstevel@tonic-gate 				PCI_DVMA_FASTTRAK_PROF(mp);
7250Sstevel@tonic-gate 			}
7260Sstevel@tonic-gate 		}
7270Sstevel@tonic-gate 		if (ret = pci_dvma_map(mp, dmareq, iommu_p))
7280Sstevel@tonic-gate 			goto map_err;
7290Sstevel@tonic-gate mapped:
7300Sstevel@tonic-gate 		*ccountp = 1;
7310Sstevel@tonic-gate 		MAKE_DMA_COOKIE(cookiep, mp->dmai_mapping, mp->dmai_size);
7320Sstevel@tonic-gate 		break;
7330Sstevel@tonic-gate 	case DMAI_FLAGS_BYPASS:
7340Sstevel@tonic-gate 	case DMAI_FLAGS_PEER_TO_PEER:
7350Sstevel@tonic-gate 		if (ret = pci_dma_physwin(pci_p, dmareq, mp))
7360Sstevel@tonic-gate 			goto map_err;
7370Sstevel@tonic-gate 		*ccountp = WINLST(mp)->win_ncookies;
7380Sstevel@tonic-gate 		*cookiep = *(ddi_dma_cookie_t *)(WINLST(mp) + 1); /* wholeobj */
7390Sstevel@tonic-gate 		break;
7400Sstevel@tonic-gate 	default:
7410Sstevel@tonic-gate 		panic("%s%d: pci_dma_bindhdl(%p): bad dma type",
7420Sstevel@tonic-gate 		    ddi_driver_name(rdip), ddi_get_instance(rdip), mp);
7430Sstevel@tonic-gate 		/*NOTREACHED*/
7440Sstevel@tonic-gate 	}
7450Sstevel@tonic-gate 	DEBUG2(DBG_DMA_BINDH, dip, "cookie %x+%x\n", cookiep->dmac_address,
7460Sstevel@tonic-gate 	    cookiep->dmac_size);
7470Sstevel@tonic-gate 	dump_dma_handle(DBG_DMA_MAP, dip, mp);
7480Sstevel@tonic-gate 
749*12027SStephen.Hanson@Sun.COM 	if (mp->dmai_attr.dma_attr_flags & DDI_DMA_FLAGERR)
7501865Sdilpreet 		mp->dmai_error.err_cf = impl_dma_check;
7510Sstevel@tonic-gate 
7520Sstevel@tonic-gate 	mp->dmai_flags |= DMAI_FLAGS_MAPPED;
7530Sstevel@tonic-gate 	return (mp->dmai_nwin == 1 ? DDI_DMA_MAPPED : DDI_DMA_PARTIAL_MAP);
7540Sstevel@tonic-gate map_err:
7550Sstevel@tonic-gate 	pci_dvma_unregister_callbacks(pci_p, mp);
7560Sstevel@tonic-gate 	pci_dma_freepfn(mp);
7570Sstevel@tonic-gate err:
7580Sstevel@tonic-gate 	mp->dmai_flags &= DMAI_FLAGS_PRESERVE;
7590Sstevel@tonic-gate 	return (ret);
7600Sstevel@tonic-gate }
7610Sstevel@tonic-gate 
7620Sstevel@tonic-gate /*
7630Sstevel@tonic-gate  * bus dma unbind handle entry point:
7640Sstevel@tonic-gate  */
7650Sstevel@tonic-gate /*ARGSUSED*/
7660Sstevel@tonic-gate int
pci_dma_unbindhdl(dev_info_t * dip,dev_info_t * rdip,ddi_dma_handle_t handle)7670Sstevel@tonic-gate pci_dma_unbindhdl(dev_info_t *dip, dev_info_t *rdip, ddi_dma_handle_t handle)
7680Sstevel@tonic-gate {
7690Sstevel@tonic-gate 	ddi_dma_impl_t *mp = (ddi_dma_impl_t *)handle;
7700Sstevel@tonic-gate 	pci_t *pci_p = get_pci_soft_state(ddi_get_instance(dip));
7710Sstevel@tonic-gate 	iommu_t *iommu_p = pci_p->pci_iommu_p;
7720Sstevel@tonic-gate 
7730Sstevel@tonic-gate 	DEBUG3(DBG_DMA_UNBINDH, dip, "rdip=%s%d, mp=%p\n",
7747656SSherry.Moore@Sun.COM 	    ddi_driver_name(rdip), ddi_get_instance(rdip), handle);
7750Sstevel@tonic-gate 	if ((mp->dmai_flags & DMAI_FLAGS_INUSE) == 0) {
7760Sstevel@tonic-gate 		DEBUG0(DBG_DMA_UNBINDH, dip, "handle not in use\n");
7770Sstevel@tonic-gate 		return (DDI_FAILURE);
7780Sstevel@tonic-gate 	}
7790Sstevel@tonic-gate 
7800Sstevel@tonic-gate 	mp->dmai_flags &= ~DMAI_FLAGS_MAPPED;
7810Sstevel@tonic-gate 
7820Sstevel@tonic-gate 	switch (PCI_DMA_TYPE(mp)) {
7830Sstevel@tonic-gate 	case DMAI_FLAGS_DVMA:
7840Sstevel@tonic-gate 		pci_dvma_unregister_callbacks(pci_p, mp);
7850Sstevel@tonic-gate 		pci_dma_sync_unmap(dip, rdip, mp);
7860Sstevel@tonic-gate 		pci_dvma_unmap(iommu_p, mp);
7870Sstevel@tonic-gate 		pci_dma_freepfn(mp);
7880Sstevel@tonic-gate 		break;
7890Sstevel@tonic-gate 	case DMAI_FLAGS_BYPASS:
7900Sstevel@tonic-gate 	case DMAI_FLAGS_PEER_TO_PEER:
7910Sstevel@tonic-gate 		pci_dma_freewin(mp);
7920Sstevel@tonic-gate 		break;
7930Sstevel@tonic-gate 	default:
7940Sstevel@tonic-gate 		panic("%s%d: pci_dma_unbindhdl:bad dma type %p",
7950Sstevel@tonic-gate 		    ddi_driver_name(rdip), ddi_get_instance(rdip), mp);
7960Sstevel@tonic-gate 		/*NOTREACHED*/
7970Sstevel@tonic-gate 	}
7980Sstevel@tonic-gate 	if (iommu_p->iommu_dvma_clid != 0) {
7990Sstevel@tonic-gate 		DEBUG0(DBG_DMA_UNBINDH, dip, "run dvma callback\n");
8000Sstevel@tonic-gate 		ddi_run_callback(&iommu_p->iommu_dvma_clid);
8010Sstevel@tonic-gate 	}
8020Sstevel@tonic-gate 	if (pci_kmem_clid) {
8030Sstevel@tonic-gate 		DEBUG0(DBG_DMA_UNBINDH, dip, "run handle callback\n");
8040Sstevel@tonic-gate 		ddi_run_callback(&pci_kmem_clid);
8050Sstevel@tonic-gate 	}
8060Sstevel@tonic-gate 	mp->dmai_flags &= DMAI_FLAGS_PRESERVE;
8070Sstevel@tonic-gate 	SYNC_BUF_PA(mp) = 0;
8080Sstevel@tonic-gate 
809*12027SStephen.Hanson@Sun.COM 	mp->dmai_error.err_cf = NULL;
8100Sstevel@tonic-gate 
8110Sstevel@tonic-gate 	return (DDI_SUCCESS);
8120Sstevel@tonic-gate }
8130Sstevel@tonic-gate 
8140Sstevel@tonic-gate 
8150Sstevel@tonic-gate /*
8160Sstevel@tonic-gate  * bus dma win entry point:
8170Sstevel@tonic-gate  */
8180Sstevel@tonic-gate int
pci_dma_win(dev_info_t * dip,dev_info_t * rdip,ddi_dma_handle_t handle,uint_t win,off_t * offp,size_t * lenp,ddi_dma_cookie_t * cookiep,uint_t * ccountp)8190Sstevel@tonic-gate pci_dma_win(dev_info_t *dip, dev_info_t *rdip,
8200Sstevel@tonic-gate 	ddi_dma_handle_t handle, uint_t win, off_t *offp,
8210Sstevel@tonic-gate 	size_t *lenp, ddi_dma_cookie_t *cookiep, uint_t *ccountp)
8220Sstevel@tonic-gate {
8230Sstevel@tonic-gate 	ddi_dma_impl_t *mp = (ddi_dma_impl_t *)handle;
8240Sstevel@tonic-gate 	DEBUG2(DBG_DMA_WIN, dip, "rdip=%s%d\n",
8250Sstevel@tonic-gate 	    ddi_driver_name(rdip), ddi_get_instance(rdip));
8260Sstevel@tonic-gate 	dump_dma_handle(DBG_DMA_WIN, dip, mp);
8270Sstevel@tonic-gate 	if (win >= mp->dmai_nwin) {
8280Sstevel@tonic-gate 		DEBUG1(DBG_DMA_WIN, dip, "%x out of range\n", win);
8290Sstevel@tonic-gate 		return (DDI_FAILURE);
8300Sstevel@tonic-gate 	}
8310Sstevel@tonic-gate 
8320Sstevel@tonic-gate 	switch (PCI_DMA_TYPE(mp)) {
8330Sstevel@tonic-gate 	case DMAI_FLAGS_DVMA:
8340Sstevel@tonic-gate 		if (win != PCI_DMA_CURWIN(mp)) {
8350Sstevel@tonic-gate 			pci_t *pci_p =
8367656SSherry.Moore@Sun.COM 			    get_pci_soft_state(ddi_get_instance(dip));
8370Sstevel@tonic-gate 			pci_dma_sync_unmap(dip, rdip, mp);
8380Sstevel@tonic-gate 			/* map_window sets dmai_mapping/size/offset */
8390Sstevel@tonic-gate 			iommu_map_window(pci_p->pci_iommu_p, mp, win);
8400Sstevel@tonic-gate 		}
8410Sstevel@tonic-gate 		if (cookiep)
8420Sstevel@tonic-gate 			MAKE_DMA_COOKIE(cookiep, mp->dmai_mapping,
8437656SSherry.Moore@Sun.COM 			    mp->dmai_size);
8440Sstevel@tonic-gate 		if (ccountp)
8450Sstevel@tonic-gate 			*ccountp = 1;
8460Sstevel@tonic-gate 		break;
8470Sstevel@tonic-gate 	case DMAI_FLAGS_PEER_TO_PEER:
8480Sstevel@tonic-gate 	case DMAI_FLAGS_BYPASS: {
8490Sstevel@tonic-gate 		int i;
8500Sstevel@tonic-gate 		ddi_dma_cookie_t *ck_p;
8510Sstevel@tonic-gate 		pci_dma_win_t *win_p = mp->dmai_winlst;
8520Sstevel@tonic-gate 
8537656SSherry.Moore@Sun.COM 		for (i = 0; i < win; win_p = win_p->win_next, i++)
8547656SSherry.Moore@Sun.COM 			;
8550Sstevel@tonic-gate 		ck_p = (ddi_dma_cookie_t *)(win_p + 1);
8560Sstevel@tonic-gate 		*cookiep = *ck_p;
8570Sstevel@tonic-gate 		mp->dmai_offset = win_p->win_offset;
8580Sstevel@tonic-gate 		mp->dmai_size   = win_p->win_size;
8590Sstevel@tonic-gate 		mp->dmai_mapping = ck_p->dmac_laddress;
8600Sstevel@tonic-gate 		mp->dmai_cookie = ck_p + 1;
8610Sstevel@tonic-gate 		win_p->win_curseg = 0;
8620Sstevel@tonic-gate 		if (ccountp)
8630Sstevel@tonic-gate 			*ccountp = win_p->win_ncookies;
8640Sstevel@tonic-gate 		}
8650Sstevel@tonic-gate 		break;
8660Sstevel@tonic-gate 	default:
8670Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d: pci_dma_win:bad dma type 0x%x",
8680Sstevel@tonic-gate 		    ddi_driver_name(rdip), ddi_get_instance(rdip),
8690Sstevel@tonic-gate 		    PCI_DMA_TYPE(mp));
8700Sstevel@tonic-gate 		return (DDI_FAILURE);
8710Sstevel@tonic-gate 	}
8720Sstevel@tonic-gate 	if (cookiep)
8730Sstevel@tonic-gate 		DEBUG2(DBG_DMA_WIN, dip,
8747656SSherry.Moore@Sun.COM 		    "cookie - dmac_address=%x dmac_size=%x\n",
8757656SSherry.Moore@Sun.COM 		    cookiep->dmac_address, cookiep->dmac_size);
8760Sstevel@tonic-gate 	if (offp)
8770Sstevel@tonic-gate 		*offp = (off_t)mp->dmai_offset;
8780Sstevel@tonic-gate 	if (lenp)
8790Sstevel@tonic-gate 		*lenp = mp->dmai_size;
8800Sstevel@tonic-gate 	return (DDI_SUCCESS);
8810Sstevel@tonic-gate }
8820Sstevel@tonic-gate 
8830Sstevel@tonic-gate #ifdef DEBUG
8840Sstevel@tonic-gate static char *pci_dmactl_str[] = {
8850Sstevel@tonic-gate 	"DDI_DMA_FREE",
8860Sstevel@tonic-gate 	"DDI_DMA_SYNC",
8870Sstevel@tonic-gate 	"DDI_DMA_HTOC",
8880Sstevel@tonic-gate 	"DDI_DMA_KVADDR",
8890Sstevel@tonic-gate 	"DDI_DMA_MOVWIN",
8900Sstevel@tonic-gate 	"DDI_DMA_REPWIN",
8910Sstevel@tonic-gate 	"DDI_DMA_GETERR",
8920Sstevel@tonic-gate 	"DDI_DMA_COFF",
8930Sstevel@tonic-gate 	"DDI_DMA_NEXTWIN",
8940Sstevel@tonic-gate 	"DDI_DMA_NEXTSEG",
8950Sstevel@tonic-gate 	"DDI_DMA_SEGTOC",
8960Sstevel@tonic-gate 	"DDI_DMA_RESERVE",
8970Sstevel@tonic-gate 	"DDI_DMA_RELEASE",
8980Sstevel@tonic-gate 	"DDI_DMA_RESETH",
8990Sstevel@tonic-gate 	"DDI_DMA_CKSYNC",
9000Sstevel@tonic-gate 	"DDI_DMA_IOPB_ALLOC",
9010Sstevel@tonic-gate 	"DDI_DMA_IOPB_FREE",
9020Sstevel@tonic-gate 	"DDI_DMA_SMEM_ALLOC",
9030Sstevel@tonic-gate 	"DDI_DMA_SMEM_FREE",
9040Sstevel@tonic-gate 	"DDI_DMA_SET_SBUS64",
9050Sstevel@tonic-gate 	"DDI_DMA_REMAP"
9060Sstevel@tonic-gate };
9070Sstevel@tonic-gate #endif
9080Sstevel@tonic-gate 
9090Sstevel@tonic-gate /*
9100Sstevel@tonic-gate  * bus dma control entry point:
9110Sstevel@tonic-gate  */
9120Sstevel@tonic-gate int
pci_dma_ctlops(dev_info_t * dip,dev_info_t * rdip,ddi_dma_handle_t handle,enum ddi_dma_ctlops cmd,off_t * offp,size_t * lenp,caddr_t * objp,uint_t cache_flags)9130Sstevel@tonic-gate pci_dma_ctlops(dev_info_t *dip, dev_info_t *rdip, ddi_dma_handle_t handle,
9140Sstevel@tonic-gate 	enum ddi_dma_ctlops cmd, off_t *offp, size_t *lenp, caddr_t *objp,
9150Sstevel@tonic-gate 	uint_t cache_flags)
9160Sstevel@tonic-gate {
9170Sstevel@tonic-gate 	ddi_dma_impl_t *mp = (ddi_dma_impl_t *)handle;
9180Sstevel@tonic-gate 	DEBUG3(DBG_DMA_CTL, dip, "%s: rdip=%s%d\n", pci_dmactl_str[cmd],
9190Sstevel@tonic-gate 	    ddi_driver_name(rdip), ddi_get_instance(rdip));
9200Sstevel@tonic-gate 
9210Sstevel@tonic-gate 	switch (cmd) {
9220Sstevel@tonic-gate 	case DDI_DMA_FREE:
9230Sstevel@tonic-gate 		(void) pci_dma_unbindhdl(dip, rdip, handle);
9240Sstevel@tonic-gate 		(void) pci_dma_freehdl(dip, rdip, handle);
9250Sstevel@tonic-gate 		return (DDI_SUCCESS);
9260Sstevel@tonic-gate 	case DDI_DMA_RESERVE: {
9270Sstevel@tonic-gate 		pci_t *pci_p = get_pci_soft_state(ddi_get_instance(dip));
9280Sstevel@tonic-gate 		return (pci_fdvma_reserve(dip, rdip, pci_p,
9297656SSherry.Moore@Sun.COM 		    (ddi_dma_req_t *)offp, (ddi_dma_handle_t *)objp));
9300Sstevel@tonic-gate 		}
9310Sstevel@tonic-gate 	case DDI_DMA_RELEASE: {
9320Sstevel@tonic-gate 		pci_t *pci_p = get_pci_soft_state(ddi_get_instance(dip));
9330Sstevel@tonic-gate 		return (pci_fdvma_release(dip, pci_p, mp));
9340Sstevel@tonic-gate 		}
9350Sstevel@tonic-gate 	default:
9360Sstevel@tonic-gate 		break;
9370Sstevel@tonic-gate 	}
9380Sstevel@tonic-gate 
9390Sstevel@tonic-gate 	switch (PCI_DMA_TYPE(mp)) {
9400Sstevel@tonic-gate 	case DMAI_FLAGS_DVMA:
9410Sstevel@tonic-gate 		return (pci_dvma_ctl(dip, rdip, mp, cmd, offp, lenp, objp,
9427656SSherry.Moore@Sun.COM 		    cache_flags));
9430Sstevel@tonic-gate 	case DMAI_FLAGS_PEER_TO_PEER:
9440Sstevel@tonic-gate 	case DMAI_FLAGS_BYPASS:
9450Sstevel@tonic-gate 		return (pci_dma_ctl(dip, rdip, mp, cmd, offp, lenp, objp,
9467656SSherry.Moore@Sun.COM 		    cache_flags));
9470Sstevel@tonic-gate 	default:
9480Sstevel@tonic-gate 		panic("%s%d: pci_dma_ctlops(%x):bad dma type %x",
9490Sstevel@tonic-gate 		    ddi_driver_name(rdip), ddi_get_instance(rdip), cmd,
9500Sstevel@tonic-gate 		    mp->dmai_flags);
9510Sstevel@tonic-gate 		/*NOTREACHED*/
9520Sstevel@tonic-gate 	}
9530Sstevel@tonic-gate }
9540Sstevel@tonic-gate 
9550Sstevel@tonic-gate #ifdef  DEBUG
9560Sstevel@tonic-gate int	pci_peekfault_cnt = 0;
9570Sstevel@tonic-gate int	pci_pokefault_cnt = 0;
9580Sstevel@tonic-gate #endif  /* DEBUG */
9590Sstevel@tonic-gate 
9600Sstevel@tonic-gate static int
pci_do_poke(pci_t * pci_p,peekpoke_ctlops_t * in_args)9610Sstevel@tonic-gate pci_do_poke(pci_t *pci_p, peekpoke_ctlops_t *in_args)
9620Sstevel@tonic-gate {
9630Sstevel@tonic-gate 	pbm_t *pbm_p = pci_p->pci_pbm_p;
9640Sstevel@tonic-gate 	int err = DDI_SUCCESS;
9650Sstevel@tonic-gate 	on_trap_data_t otd;
9660Sstevel@tonic-gate 
9670Sstevel@tonic-gate 	mutex_enter(&pbm_p->pbm_pokefault_mutex);
9680Sstevel@tonic-gate 	pbm_p->pbm_ontrap_data = &otd;
9690Sstevel@tonic-gate 
9700Sstevel@tonic-gate 	/* Set up protected environment. */
9710Sstevel@tonic-gate 	if (!on_trap(&otd, OT_DATA_ACCESS)) {
9720Sstevel@tonic-gate 		uintptr_t tramp = otd.ot_trampoline;
9730Sstevel@tonic-gate 
9740Sstevel@tonic-gate 		otd.ot_trampoline = (uintptr_t)&poke_fault;
9750Sstevel@tonic-gate 		err = do_poke(in_args->size, (void *)in_args->dev_addr,
9760Sstevel@tonic-gate 		    (void *)in_args->host_addr);
9770Sstevel@tonic-gate 		otd.ot_trampoline = tramp;
9780Sstevel@tonic-gate 	} else
9790Sstevel@tonic-gate 		err = DDI_FAILURE;
9800Sstevel@tonic-gate 
9810Sstevel@tonic-gate 	/*
9820Sstevel@tonic-gate 	 * Read the async fault register for the PBM to see it sees
9830Sstevel@tonic-gate 	 * a master-abort.
9840Sstevel@tonic-gate 	 */
9850Sstevel@tonic-gate 	pbm_clear_error(pbm_p);
9860Sstevel@tonic-gate 
9870Sstevel@tonic-gate 	if (otd.ot_trap & OT_DATA_ACCESS)
9880Sstevel@tonic-gate 		err = DDI_FAILURE;
9890Sstevel@tonic-gate 
9900Sstevel@tonic-gate 	/* Take down protected environment. */
9910Sstevel@tonic-gate 	no_trap();
9920Sstevel@tonic-gate 
9930Sstevel@tonic-gate 	pbm_p->pbm_ontrap_data = NULL;
9940Sstevel@tonic-gate 	mutex_exit(&pbm_p->pbm_pokefault_mutex);
9950Sstevel@tonic-gate 
9960Sstevel@tonic-gate #ifdef  DEBUG
9970Sstevel@tonic-gate 	if (err == DDI_FAILURE)
9980Sstevel@tonic-gate 		pci_pokefault_cnt++;
9990Sstevel@tonic-gate #endif
10000Sstevel@tonic-gate 	return (err);
10010Sstevel@tonic-gate }
10020Sstevel@tonic-gate 
10030Sstevel@tonic-gate 
10040Sstevel@tonic-gate static int
pci_do_caut_put(pci_t * pci_p,peekpoke_ctlops_t * cautacc_ctlops_arg)10050Sstevel@tonic-gate pci_do_caut_put(pci_t *pci_p, peekpoke_ctlops_t *cautacc_ctlops_arg)
10060Sstevel@tonic-gate {
10070Sstevel@tonic-gate 	size_t size = cautacc_ctlops_arg->size;
10080Sstevel@tonic-gate 	uintptr_t dev_addr = cautacc_ctlops_arg->dev_addr;
10090Sstevel@tonic-gate 	uintptr_t host_addr = cautacc_ctlops_arg->host_addr;
10100Sstevel@tonic-gate 	ddi_acc_impl_t *hp = (ddi_acc_impl_t *)cautacc_ctlops_arg->handle;
10110Sstevel@tonic-gate 	size_t repcount = cautacc_ctlops_arg->repcount;
10120Sstevel@tonic-gate 	uint_t flags = cautacc_ctlops_arg->flags;
10130Sstevel@tonic-gate 
10140Sstevel@tonic-gate 	hp->ahi_err->err_expected = DDI_FM_ERR_EXPECTED;
10150Sstevel@tonic-gate 
10160Sstevel@tonic-gate 	/*
10170Sstevel@tonic-gate 	 * Note that i_ndi_busop_access_enter ends up grabbing the pokefault
10180Sstevel@tonic-gate 	 * mutex.
10190Sstevel@tonic-gate 	 */
10200Sstevel@tonic-gate 	i_ndi_busop_access_enter(hp->ahi_common.ah_dip, (ddi_acc_handle_t)hp);
10210Sstevel@tonic-gate 
10220Sstevel@tonic-gate 	if (!i_ddi_ontrap((ddi_acc_handle_t)hp)) {
10230Sstevel@tonic-gate 		for (; repcount; repcount--) {
10240Sstevel@tonic-gate 			switch (size) {
10250Sstevel@tonic-gate 
10260Sstevel@tonic-gate 			case sizeof (uint8_t):
10270Sstevel@tonic-gate 				i_ddi_put8(hp, (uint8_t *)dev_addr,
10280Sstevel@tonic-gate 				    *(uint8_t *)host_addr);
10290Sstevel@tonic-gate 				break;
10300Sstevel@tonic-gate 
10310Sstevel@tonic-gate 			case sizeof (uint16_t):
10320Sstevel@tonic-gate 				i_ddi_put16(hp, (uint16_t *)dev_addr,
10330Sstevel@tonic-gate 				    *(uint16_t *)host_addr);
10340Sstevel@tonic-gate 				break;
10350Sstevel@tonic-gate 
10360Sstevel@tonic-gate 			case sizeof (uint32_t):
10370Sstevel@tonic-gate 				i_ddi_put32(hp, (uint32_t *)dev_addr,
10380Sstevel@tonic-gate 				    *(uint32_t *)host_addr);
10390Sstevel@tonic-gate 				break;
10400Sstevel@tonic-gate 
10410Sstevel@tonic-gate 			case sizeof (uint64_t):
10420Sstevel@tonic-gate 				i_ddi_put64(hp, (uint64_t *)dev_addr,
10430Sstevel@tonic-gate 				    *(uint64_t *)host_addr);
10440Sstevel@tonic-gate 				break;
10450Sstevel@tonic-gate 			}
10460Sstevel@tonic-gate 
10470Sstevel@tonic-gate 			host_addr += size;
10480Sstevel@tonic-gate 
10490Sstevel@tonic-gate 			if (flags == DDI_DEV_AUTOINCR)
10500Sstevel@tonic-gate 				dev_addr += size;
10510Sstevel@tonic-gate 
10520Sstevel@tonic-gate 		}
10530Sstevel@tonic-gate 	}
10540Sstevel@tonic-gate 
10550Sstevel@tonic-gate 	i_ddi_notrap((ddi_acc_handle_t)hp);
10560Sstevel@tonic-gate 	i_ndi_busop_access_exit(hp->ahi_common.ah_dip, (ddi_acc_handle_t)hp);
10570Sstevel@tonic-gate 	hp->ahi_err->err_expected = DDI_FM_ERR_UNEXPECTED;
10580Sstevel@tonic-gate 
1059815Sdilpreet 	if (hp->ahi_err->err_status != DDI_FM_OK) {
1060815Sdilpreet 		/* Clear the expected fault from the handle before returning */
1061815Sdilpreet 		hp->ahi_err->err_status = DDI_FM_OK;
1062815Sdilpreet 		return (DDI_FAILURE);
1063815Sdilpreet 	}
1064815Sdilpreet 
1065815Sdilpreet 	return (DDI_SUCCESS);
10660Sstevel@tonic-gate }
10670Sstevel@tonic-gate 
10680Sstevel@tonic-gate 
10690Sstevel@tonic-gate static int
pci_ctlops_poke(pci_t * pci_p,peekpoke_ctlops_t * in_args)10700Sstevel@tonic-gate pci_ctlops_poke(pci_t *pci_p, peekpoke_ctlops_t *in_args)
10710Sstevel@tonic-gate {
10720Sstevel@tonic-gate 	return (in_args->handle ? pci_do_caut_put(pci_p, in_args) :
10730Sstevel@tonic-gate 	    pci_do_poke(pci_p, in_args));
10740Sstevel@tonic-gate }
10750Sstevel@tonic-gate 
10760Sstevel@tonic-gate 
10770Sstevel@tonic-gate static int
pci_do_peek(pci_t * pci_p,peekpoke_ctlops_t * in_args)10780Sstevel@tonic-gate pci_do_peek(pci_t *pci_p, peekpoke_ctlops_t *in_args)
10790Sstevel@tonic-gate {
10800Sstevel@tonic-gate 	int err = DDI_SUCCESS;
10810Sstevel@tonic-gate 	on_trap_data_t otd;
10820Sstevel@tonic-gate 
10830Sstevel@tonic-gate 	if (!on_trap(&otd, OT_DATA_ACCESS)) {
10840Sstevel@tonic-gate 		uintptr_t tramp = otd.ot_trampoline;
10850Sstevel@tonic-gate 
10860Sstevel@tonic-gate 		otd.ot_trampoline = (uintptr_t)&peek_fault;
10870Sstevel@tonic-gate 		err = do_peek(in_args->size, (void *)in_args->dev_addr,
10880Sstevel@tonic-gate 		    (void *)in_args->host_addr);
10890Sstevel@tonic-gate 		otd.ot_trampoline = tramp;
10900Sstevel@tonic-gate 	} else
10910Sstevel@tonic-gate 		err = DDI_FAILURE;
10920Sstevel@tonic-gate 
10930Sstevel@tonic-gate 	no_trap();
10940Sstevel@tonic-gate 
10950Sstevel@tonic-gate #ifdef  DEBUG
10960Sstevel@tonic-gate 	if (err == DDI_FAILURE)
10970Sstevel@tonic-gate 		pci_peekfault_cnt++;
10980Sstevel@tonic-gate #endif
10990Sstevel@tonic-gate 	return (err);
11000Sstevel@tonic-gate }
11010Sstevel@tonic-gate 
11020Sstevel@tonic-gate static int
pci_do_caut_get(pci_t * pci_p,peekpoke_ctlops_t * cautacc_ctlops_arg)11030Sstevel@tonic-gate pci_do_caut_get(pci_t *pci_p, peekpoke_ctlops_t *cautacc_ctlops_arg)
11040Sstevel@tonic-gate {
11050Sstevel@tonic-gate 	size_t size = cautacc_ctlops_arg->size;
11060Sstevel@tonic-gate 	uintptr_t dev_addr = cautacc_ctlops_arg->dev_addr;
11070Sstevel@tonic-gate 	uintptr_t host_addr = cautacc_ctlops_arg->host_addr;
11080Sstevel@tonic-gate 	ddi_acc_impl_t *hp = (ddi_acc_impl_t *)cautacc_ctlops_arg->handle;
11090Sstevel@tonic-gate 	size_t repcount = cautacc_ctlops_arg->repcount;
11100Sstevel@tonic-gate 	uint_t flags = cautacc_ctlops_arg->flags;
11110Sstevel@tonic-gate 
11120Sstevel@tonic-gate 	int err = DDI_SUCCESS;
11130Sstevel@tonic-gate 
11140Sstevel@tonic-gate 	hp->ahi_err->err_expected = DDI_FM_ERR_EXPECTED;
11150Sstevel@tonic-gate 	i_ndi_busop_access_enter(hp->ahi_common.ah_dip, (ddi_acc_handle_t)hp);
11160Sstevel@tonic-gate 
1117815Sdilpreet 	if (!i_ddi_ontrap((ddi_acc_handle_t)hp)) {
1118815Sdilpreet 		for (; repcount; repcount--) {
11190Sstevel@tonic-gate 			i_ddi_caut_get(size, (void *)dev_addr,
11200Sstevel@tonic-gate 			    (void *)host_addr);
1121815Sdilpreet 
1122815Sdilpreet 			host_addr += size;
11230Sstevel@tonic-gate 
1124815Sdilpreet 			if (flags == DDI_DEV_AUTOINCR)
1125815Sdilpreet 				dev_addr += size;
11260Sstevel@tonic-gate 		}
11270Sstevel@tonic-gate 	} else {
1128815Sdilpreet 		int i;
1129815Sdilpreet 		uint8_t *ff_addr = (uint8_t *)host_addr;
1130815Sdilpreet 		for (i = 0; i < size; i++)
1131815Sdilpreet 			*ff_addr++ = 0xff;
11320Sstevel@tonic-gate 
1133815Sdilpreet 		err = DDI_FAILURE;
11340Sstevel@tonic-gate 	}
11350Sstevel@tonic-gate 
11360Sstevel@tonic-gate 	i_ddi_notrap((ddi_acc_handle_t)hp);
11370Sstevel@tonic-gate 	i_ndi_busop_access_exit(hp->ahi_common.ah_dip, (ddi_acc_handle_t)hp);
11380Sstevel@tonic-gate 	hp->ahi_err->err_expected = DDI_FM_ERR_UNEXPECTED;
11390Sstevel@tonic-gate 
11400Sstevel@tonic-gate 	return (err);
11410Sstevel@tonic-gate }
11420Sstevel@tonic-gate 
11430Sstevel@tonic-gate 
11440Sstevel@tonic-gate static int
pci_ctlops_peek(pci_t * pci_p,peekpoke_ctlops_t * in_args,void * result)11450Sstevel@tonic-gate pci_ctlops_peek(pci_t *pci_p, peekpoke_ctlops_t *in_args, void *result)
11460Sstevel@tonic-gate {
11470Sstevel@tonic-gate 	result = (void *)in_args->host_addr;
11480Sstevel@tonic-gate 	return (in_args->handle ? pci_do_caut_get(pci_p, in_args) :
11490Sstevel@tonic-gate 	    pci_do_peek(pci_p, in_args));
11500Sstevel@tonic-gate }
11510Sstevel@tonic-gate 
11520Sstevel@tonic-gate /*
11530Sstevel@tonic-gate  * get_reg_set_size
11540Sstevel@tonic-gate  *
11550Sstevel@tonic-gate  * Given a dev info pointer to a pci child and a register number, this
11560Sstevel@tonic-gate  * routine returns the size element of that reg set property.
11570Sstevel@tonic-gate  * return value: size of reg set on success, -1 on error
11580Sstevel@tonic-gate  */
11590Sstevel@tonic-gate static off_t
get_reg_set_size(dev_info_t * child,int rnumber)11600Sstevel@tonic-gate get_reg_set_size(dev_info_t *child, int rnumber)
11610Sstevel@tonic-gate {
11620Sstevel@tonic-gate 	pci_regspec_t *pci_rp;
11630Sstevel@tonic-gate 	off_t size;
11640Sstevel@tonic-gate 	int i;
11650Sstevel@tonic-gate 
11660Sstevel@tonic-gate 	if (rnumber < 0)
11670Sstevel@tonic-gate 		return (-1);
11680Sstevel@tonic-gate 
11690Sstevel@tonic-gate 	/*
11700Sstevel@tonic-gate 	 * Get the reg property for the device.
11710Sstevel@tonic-gate 	 */
11720Sstevel@tonic-gate 	if (ddi_getlongprop(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS, "reg",
11730Sstevel@tonic-gate 	    (caddr_t)&pci_rp, &i) != DDI_SUCCESS)
11740Sstevel@tonic-gate 		return (-1);
11750Sstevel@tonic-gate 
11760Sstevel@tonic-gate 	if (rnumber >= (i / (int)sizeof (pci_regspec_t))) {
11770Sstevel@tonic-gate 		kmem_free(pci_rp, i);
11780Sstevel@tonic-gate 		return (-1);
11790Sstevel@tonic-gate 	}
11800Sstevel@tonic-gate 
11810Sstevel@tonic-gate 	size = pci_rp[rnumber].pci_size_low |
11827656SSherry.Moore@Sun.COM 	    ((uint64_t)pci_rp[rnumber].pci_size_hi << 32);
11830Sstevel@tonic-gate 	kmem_free(pci_rp, i);
11840Sstevel@tonic-gate 	return (size);
11850Sstevel@tonic-gate }
11860Sstevel@tonic-gate 
11870Sstevel@tonic-gate 
11880Sstevel@tonic-gate /*
11890Sstevel@tonic-gate  * control ops entry point:
11900Sstevel@tonic-gate  *
11910Sstevel@tonic-gate  * Requests handled completely:
11920Sstevel@tonic-gate  *	DDI_CTLOPS_INITCHILD	see init_child() for details
11930Sstevel@tonic-gate  *	DDI_CTLOPS_UNINITCHILD
11940Sstevel@tonic-gate  *	DDI_CTLOPS_REPORTDEV	see report_dev() for details
11950Sstevel@tonic-gate  *	DDI_CTLOPS_IOMIN	cache line size if streaming otherwise 1
11960Sstevel@tonic-gate  *	DDI_CTLOPS_REGSIZE
11970Sstevel@tonic-gate  *	DDI_CTLOPS_NREGS
11980Sstevel@tonic-gate  *	DDI_CTLOPS_DVMAPAGESIZE
11990Sstevel@tonic-gate  *	DDI_CTLOPS_POKE
12000Sstevel@tonic-gate  *	DDI_CTLOPS_PEEK
12010Sstevel@tonic-gate  *	DDI_CTLOPS_QUIESCE
12020Sstevel@tonic-gate  *	DDI_CTLOPS_UNQUIESCE
12030Sstevel@tonic-gate  *
12040Sstevel@tonic-gate  * All others passed to parent.
12050Sstevel@tonic-gate  */
12060Sstevel@tonic-gate int
pci_ctlops(dev_info_t * dip,dev_info_t * rdip,ddi_ctl_enum_t op,void * arg,void * result)12070Sstevel@tonic-gate pci_ctlops(dev_info_t *dip, dev_info_t *rdip,
12080Sstevel@tonic-gate 	ddi_ctl_enum_t op, void *arg, void *result)
12090Sstevel@tonic-gate {
12100Sstevel@tonic-gate 	pci_t *pci_p = get_pci_soft_state(ddi_get_instance(dip));
12110Sstevel@tonic-gate 
12120Sstevel@tonic-gate 	switch (op) {
12130Sstevel@tonic-gate 	case DDI_CTLOPS_INITCHILD:
12140Sstevel@tonic-gate 		return (init_child(pci_p, (dev_info_t *)arg));
12150Sstevel@tonic-gate 
12160Sstevel@tonic-gate 	case DDI_CTLOPS_UNINITCHILD:
12170Sstevel@tonic-gate 		return (uninit_child(pci_p, (dev_info_t *)arg));
12180Sstevel@tonic-gate 
12190Sstevel@tonic-gate 	case DDI_CTLOPS_REPORTDEV:
12200Sstevel@tonic-gate 		return (report_dev(rdip));
12210Sstevel@tonic-gate 
12220Sstevel@tonic-gate 	case DDI_CTLOPS_IOMIN:
12230Sstevel@tonic-gate 
12240Sstevel@tonic-gate 		/*
12250Sstevel@tonic-gate 		 * If we are using the streaming cache, align at
12260Sstevel@tonic-gate 		 * least on a cache line boundary. Otherwise use
12270Sstevel@tonic-gate 		 * whatever alignment is passed in.
12280Sstevel@tonic-gate 		 */
12290Sstevel@tonic-gate 
1230946Smathue 		if ((uintptr_t)arg) {
12310Sstevel@tonic-gate 			int val = *((int *)result);
12320Sstevel@tonic-gate 
12330Sstevel@tonic-gate 			val = maxbit(val, PCI_SBUF_LINE_SIZE);
12340Sstevel@tonic-gate 			*((int *)result) = val;
12350Sstevel@tonic-gate 		}
12360Sstevel@tonic-gate 		return (DDI_SUCCESS);
12370Sstevel@tonic-gate 
12380Sstevel@tonic-gate 	case DDI_CTLOPS_REGSIZE:
12390Sstevel@tonic-gate 		*((off_t *)result) = get_reg_set_size(rdip, *((int *)arg));
12400Sstevel@tonic-gate 		return (*((off_t *)result) == -1 ? DDI_FAILURE : DDI_SUCCESS);
12410Sstevel@tonic-gate 
12420Sstevel@tonic-gate 	case DDI_CTLOPS_NREGS:
12430Sstevel@tonic-gate 		*((uint_t *)result) = get_nreg_set(rdip);
12440Sstevel@tonic-gate 		return (DDI_SUCCESS);
12450Sstevel@tonic-gate 
12460Sstevel@tonic-gate 	case DDI_CTLOPS_DVMAPAGESIZE:
12470Sstevel@tonic-gate 		*((ulong_t *)result) = IOMMU_PAGE_SIZE;
12480Sstevel@tonic-gate 		return (DDI_SUCCESS);
12490Sstevel@tonic-gate 
12500Sstevel@tonic-gate 	case DDI_CTLOPS_POKE:
12510Sstevel@tonic-gate 		return (pci_ctlops_poke(pci_p, (peekpoke_ctlops_t *)arg));
12520Sstevel@tonic-gate 
12530Sstevel@tonic-gate 	case DDI_CTLOPS_PEEK:
12540Sstevel@tonic-gate 		return (pci_ctlops_peek(pci_p, (peekpoke_ctlops_t *)arg,
12550Sstevel@tonic-gate 		    result));
12560Sstevel@tonic-gate 
12570Sstevel@tonic-gate 	case DDI_CTLOPS_AFFINITY:
12580Sstevel@tonic-gate 		break;
12590Sstevel@tonic-gate 
12600Sstevel@tonic-gate 	case DDI_CTLOPS_QUIESCE:
12610Sstevel@tonic-gate 		return (pci_bus_quiesce(pci_p, rdip, result));
12620Sstevel@tonic-gate 
12630Sstevel@tonic-gate 	case DDI_CTLOPS_UNQUIESCE:
12640Sstevel@tonic-gate 		return (pci_bus_unquiesce(pci_p, rdip, result));
12650Sstevel@tonic-gate 
12660Sstevel@tonic-gate 	default:
12670Sstevel@tonic-gate 		break;
12680Sstevel@tonic-gate 	}
12690Sstevel@tonic-gate 
12700Sstevel@tonic-gate 	/*
12710Sstevel@tonic-gate 	 * Now pass the request up to our parent.
12720Sstevel@tonic-gate 	 */
12730Sstevel@tonic-gate 	DEBUG2(DBG_CTLOPS, dip, "passing request to parent: rdip=%s%d\n",
12747656SSherry.Moore@Sun.COM 	    ddi_driver_name(rdip), ddi_get_instance(rdip));
12750Sstevel@tonic-gate 	return (ddi_ctlops(dip, rdip, op, arg, result));
12760Sstevel@tonic-gate }
12770Sstevel@tonic-gate 
12780Sstevel@tonic-gate 
12790Sstevel@tonic-gate /* ARGSUSED */
12800Sstevel@tonic-gate int
pci_intr_ops(dev_info_t * dip,dev_info_t * rdip,ddi_intr_op_t intr_op,ddi_intr_handle_impl_t * hdlp,void * result)12810Sstevel@tonic-gate pci_intr_ops(dev_info_t *dip, dev_info_t *rdip, ddi_intr_op_t intr_op,
12820Sstevel@tonic-gate     ddi_intr_handle_impl_t *hdlp, void *result)
12830Sstevel@tonic-gate {
128410053SEvan.Yan@Sun.COM 	pci_t		*pci_p = get_pci_soft_state(ddi_get_instance(dip));
128510053SEvan.Yan@Sun.COM 	ib_ino_t	ino;
12860Sstevel@tonic-gate 	int		ret = DDI_SUCCESS;
12870Sstevel@tonic-gate 
12880Sstevel@tonic-gate 	switch (intr_op) {
12890Sstevel@tonic-gate 	case DDI_INTROP_GETCAP:
12900Sstevel@tonic-gate 		/* GetCap will always fail for all non PCI devices */
12910Sstevel@tonic-gate 		(void) pci_intx_get_cap(rdip, (int *)result);
12920Sstevel@tonic-gate 		break;
12930Sstevel@tonic-gate 	case DDI_INTROP_SETCAP:
12940Sstevel@tonic-gate 		ret = DDI_ENOTSUP;
12950Sstevel@tonic-gate 		break;
12960Sstevel@tonic-gate 	case DDI_INTROP_ALLOC:
12970Sstevel@tonic-gate 		*(int *)result = hdlp->ih_scratch1;
12980Sstevel@tonic-gate 		break;
12990Sstevel@tonic-gate 	case DDI_INTROP_FREE:
13000Sstevel@tonic-gate 		break;
13010Sstevel@tonic-gate 	case DDI_INTROP_GETPRI:
1302693Sgovinda 		*(int *)result = hdlp->ih_pri ?
1303693Sgovinda 		    hdlp->ih_pri : pci_class_to_pil(rdip);
13040Sstevel@tonic-gate 		break;
13050Sstevel@tonic-gate 	case DDI_INTROP_SETPRI:
13060Sstevel@tonic-gate 		break;
13070Sstevel@tonic-gate 	case DDI_INTROP_ADDISR:
13080Sstevel@tonic-gate 		ret = pci_add_intr(dip, rdip, hdlp);
13090Sstevel@tonic-gate 		break;
13100Sstevel@tonic-gate 	case DDI_INTROP_REMISR:
13110Sstevel@tonic-gate 		ret = pci_remove_intr(dip, rdip, hdlp);
13120Sstevel@tonic-gate 		break;
131310053SEvan.Yan@Sun.COM 	case DDI_INTROP_GETTARGET:
131410053SEvan.Yan@Sun.COM 		ino = IB_MONDO_TO_INO(pci_xlate_intr(dip, rdip,
131510053SEvan.Yan@Sun.COM 		    pci_p->pci_ib_p, IB_MONDO_TO_INO(hdlp->ih_vector)));
131610053SEvan.Yan@Sun.COM 		ret = ib_get_intr_target(pci_p, ino, (int *)result);
131710053SEvan.Yan@Sun.COM 		break;
131810053SEvan.Yan@Sun.COM 	case DDI_INTROP_SETTARGET:
131910053SEvan.Yan@Sun.COM 		ret = DDI_ENOTSUP;
132010053SEvan.Yan@Sun.COM 		break;
13210Sstevel@tonic-gate 	case DDI_INTROP_ENABLE:
13220Sstevel@tonic-gate 		ret = ib_update_intr_state(pci_p, rdip, hdlp,
13230Sstevel@tonic-gate 		    PCI_INTR_STATE_ENABLE);
13240Sstevel@tonic-gate 		break;
13250Sstevel@tonic-gate 	case DDI_INTROP_DISABLE:
13260Sstevel@tonic-gate 		ret = ib_update_intr_state(pci_p, rdip, hdlp,
13270Sstevel@tonic-gate 		    PCI_INTR_STATE_DISABLE);
13280Sstevel@tonic-gate 		break;
13290Sstevel@tonic-gate 	case DDI_INTROP_SETMASK:
13300Sstevel@tonic-gate 		ret = pci_intx_set_mask(rdip);
13310Sstevel@tonic-gate 		break;
13320Sstevel@tonic-gate 	case DDI_INTROP_CLRMASK:
13330Sstevel@tonic-gate 		ret = pci_intx_clr_mask(rdip);
13340Sstevel@tonic-gate 		break;
13350Sstevel@tonic-gate 	case DDI_INTROP_GETPENDING:
13360Sstevel@tonic-gate 		ret = pci_intx_get_pending(rdip, (int *)result);
13370Sstevel@tonic-gate 		break;
13380Sstevel@tonic-gate 	case DDI_INTROP_NINTRS:
13390Sstevel@tonic-gate 	case DDI_INTROP_NAVAIL:
13402580Sanish 		*(int *)result = i_ddi_get_intx_nintrs(rdip);
13410Sstevel@tonic-gate 		break;
13420Sstevel@tonic-gate 	case DDI_INTROP_SUPPORTED_TYPES:
13430Sstevel@tonic-gate 		/* PCI nexus driver supports only fixed interrupts */
13442580Sanish 		*(int *)result = i_ddi_get_intx_nintrs(rdip) ?
13450Sstevel@tonic-gate 		    DDI_INTR_TYPE_FIXED : 0;
13460Sstevel@tonic-gate 		break;
13470Sstevel@tonic-gate 	default:
13480Sstevel@tonic-gate 		ret = DDI_ENOTSUP;
13490Sstevel@tonic-gate 		break;
13500Sstevel@tonic-gate 	}
13510Sstevel@tonic-gate 
13520Sstevel@tonic-gate 	return (ret);
13530Sstevel@tonic-gate }
13540Sstevel@tonic-gate 
13550Sstevel@tonic-gate static void
pci_init_hotplug(struct pci * pci_p)13560Sstevel@tonic-gate pci_init_hotplug(struct pci *pci_p)
13570Sstevel@tonic-gate {
13580Sstevel@tonic-gate 	pci_bus_range_t bus_range;
13590Sstevel@tonic-gate 	dev_info_t *dip;
13600Sstevel@tonic-gate 
13610Sstevel@tonic-gate 	/*
13620Sstevel@tonic-gate 	 * Before initializing hotplug - open up
13630Sstevel@tonic-gate 	 * bus range.  The busra module will
13640Sstevel@tonic-gate 	 * initialize its pool of bus numbers from
13650Sstevel@tonic-gate 	 * this. "busra" will be the agent that keeps
13660Sstevel@tonic-gate 	 * track of them during hotplug.  Also, note,
13670Sstevel@tonic-gate 	 * that busra will remove any bus numbers
13680Sstevel@tonic-gate 	 * already in use from boot time.
13690Sstevel@tonic-gate 	 */
13700Sstevel@tonic-gate 	bus_range.lo = 0x0;
13710Sstevel@tonic-gate 	bus_range.hi = 0xff;
13720Sstevel@tonic-gate 	dip = pci_p->pci_dip;
13730Sstevel@tonic-gate 	pci_p->hotplug_capable = B_FALSE;
13740Sstevel@tonic-gate 
13750Sstevel@tonic-gate 	/*
13760Sstevel@tonic-gate 	 * If this property exists, this nexus has hot-plug
13770Sstevel@tonic-gate 	 * slots.
13780Sstevel@tonic-gate 	 */
13790Sstevel@tonic-gate 	if (ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
13807656SSherry.Moore@Sun.COM 	    "hotplug-capable")) {
13810Sstevel@tonic-gate 		if (ndi_prop_update_int_array(DDI_DEV_T_NONE,
13827656SSherry.Moore@Sun.COM 		    dip, "bus-range",
13837656SSherry.Moore@Sun.COM 		    (int *)&bus_range,
13847656SSherry.Moore@Sun.COM 		    2) != DDI_PROP_SUCCESS) {
13850Sstevel@tonic-gate 			return;
13860Sstevel@tonic-gate 		}
13870Sstevel@tonic-gate 
13880Sstevel@tonic-gate 		if (pcihp_init(dip) != DDI_SUCCESS) {
13890Sstevel@tonic-gate 			return;
13900Sstevel@tonic-gate 		}
13910Sstevel@tonic-gate 
1392162Sschwartz 		if ((pcihp_ops = pcihp_get_cb_ops()) != NULL) {
13930Sstevel@tonic-gate 			DEBUG2(DBG_ATTACH, dip, "%s%d hotplug enabled",
13940Sstevel@tonic-gate 			    ddi_driver_name(dip), ddi_get_instance(dip));
1395162Sschwartz 			pci_p->hotplug_capable = B_TRUE;
13960Sstevel@tonic-gate 		}
13970Sstevel@tonic-gate 	}
13980Sstevel@tonic-gate }
1399