17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5d12abe7cSanish  * Common Development and Distribution License (the "License").
6d12abe7cSanish  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
225febcb4aSScott Carter, SD IOSW  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <sys/note.h>
277c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
287c478bd9Sstevel@tonic-gate #include <sys/types.h>
297c478bd9Sstevel@tonic-gate #include <sys/param.h>
307c478bd9Sstevel@tonic-gate #include <sys/systm.h>
317c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
327c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
337c478bd9Sstevel@tonic-gate #include <sys/debug.h>
347c478bd9Sstevel@tonic-gate #include <sys/avintr.h>
357c478bd9Sstevel@tonic-gate #include <sys/autoconf.h>
367c478bd9Sstevel@tonic-gate #include <sys/sunndi.h>
377c478bd9Sstevel@tonic-gate #include <sys/ndi_impldefs.h>	/* include prototypes */
387c478bd9Sstevel@tonic-gate 
39*63ea9ad2SEvan Yan #if defined(__i386) || defined(__amd64)
40*63ea9ad2SEvan Yan /*
41*63ea9ad2SEvan Yan  * MSI-X allocation limit.
42*63ea9ad2SEvan Yan  */
43*63ea9ad2SEvan Yan uint_t		ddi_msix_alloc_limit = DDI_DEFAULT_MSIX_ALLOC;
44*63ea9ad2SEvan Yan #endif
45ef643aefSegillett 
467c478bd9Sstevel@tonic-gate /*
477c478bd9Sstevel@tonic-gate  * New DDI interrupt framework
487c478bd9Sstevel@tonic-gate  */
497c478bd9Sstevel@tonic-gate void
507c478bd9Sstevel@tonic-gate i_ddi_intr_devi_init(dev_info_t *dip)
517c478bd9Sstevel@tonic-gate {
527c478bd9Sstevel@tonic-gate 	int	supported_types;
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate 	DDI_INTR_APIDBG((CE_CONT, "i_ddi_intr_devi_init: dip %p\n",
557c478bd9Sstevel@tonic-gate 	    (void *)dip));
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate 	if (DEVI(dip)->devi_intr_p)
587c478bd9Sstevel@tonic-gate 		return;
597c478bd9Sstevel@tonic-gate 
605febcb4aSScott Carter, SD IOSW 	DEVI(dip)->devi_intr_p = kmem_zalloc(sizeof (devinfo_intr_t), KM_SLEEP);
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate 	supported_types = i_ddi_intr_get_supported_types(dip);
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate 	/* Save supported interrupt types information */
657c478bd9Sstevel@tonic-gate 	i_ddi_intr_set_supported_types(dip, supported_types);
667c478bd9Sstevel@tonic-gate }
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate void
697c478bd9Sstevel@tonic-gate i_ddi_intr_devi_fini(dev_info_t *dip)
707c478bd9Sstevel@tonic-gate {
717c478bd9Sstevel@tonic-gate 	devinfo_intr_t	*intr_p = DEVI(dip)->devi_intr_p;
727c478bd9Sstevel@tonic-gate 
73a195726fSgovinda 	DDI_INTR_APIDBG((CE_CONT, "i_ddi_intr_devi_fini: dip %p\n",
74a195726fSgovinda 	    (void *)dip));
75a195726fSgovinda 
765febcb4aSScott Carter, SD IOSW 	if ((intr_p == NULL) || i_ddi_intr_get_current_nintrs(dip))
77a195726fSgovinda 		return;
78a195726fSgovinda 
797c478bd9Sstevel@tonic-gate 	/*
807c478bd9Sstevel@tonic-gate 	 * devi_intr_handle_p will only be used for devices
817c478bd9Sstevel@tonic-gate 	 * which are using the legacy DDI Interrupt interfaces.
827c478bd9Sstevel@tonic-gate 	 */
837c478bd9Sstevel@tonic-gate 	if (intr_p->devi_intr_handle_p) {
847c478bd9Sstevel@tonic-gate 		/* nintrs could be zero; so check for it first */
857c478bd9Sstevel@tonic-gate 		if (intr_p->devi_intr_sup_nintrs) {
867c478bd9Sstevel@tonic-gate 			kmem_free(intr_p->devi_intr_handle_p,
877c478bd9Sstevel@tonic-gate 			    intr_p->devi_intr_sup_nintrs *
887c478bd9Sstevel@tonic-gate 			    sizeof (ddi_intr_handle_t));
897c478bd9Sstevel@tonic-gate 		}
907c478bd9Sstevel@tonic-gate 	}
91a195726fSgovinda 
925febcb4aSScott Carter, SD IOSW 	/*
935febcb4aSScott Carter, SD IOSW 	 * devi_irm_req_p will only be used for devices which
945febcb4aSScott Carter, SD IOSW 	 * are mapped to an Interrupt Resource Management pool.
955febcb4aSScott Carter, SD IOSW 	 */
965febcb4aSScott Carter, SD IOSW 	if (intr_p->devi_irm_req_p)
975febcb4aSScott Carter, SD IOSW 		(void) i_ddi_irm_remove(dip);
985febcb4aSScott Carter, SD IOSW 
997c478bd9Sstevel@tonic-gate 	kmem_free(DEVI(dip)->devi_intr_p, sizeof (devinfo_intr_t));
1007c478bd9Sstevel@tonic-gate 	DEVI(dip)->devi_intr_p = NULL;
1017c478bd9Sstevel@tonic-gate }
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate uint_t
1047c478bd9Sstevel@tonic-gate i_ddi_intr_get_supported_types(dev_info_t *dip)
1057c478bd9Sstevel@tonic-gate {
1067c478bd9Sstevel@tonic-gate 	devinfo_intr_t		*intr_p = DEVI(dip)->devi_intr_p;
1077c478bd9Sstevel@tonic-gate 	ddi_intr_handle_impl_t	hdl;
1087c478bd9Sstevel@tonic-gate 	int			ret, intr_types;
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 	if ((intr_p) && (intr_p->devi_intr_sup_types))
1117c478bd9Sstevel@tonic-gate 		return (intr_p->devi_intr_sup_types);
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 	bzero(&hdl, sizeof (ddi_intr_handle_impl_t));
1147c478bd9Sstevel@tonic-gate 	hdl.ih_dip = dip;
1157c478bd9Sstevel@tonic-gate 
116a195726fSgovinda 	ret = i_ddi_intr_ops(dip, dip, DDI_INTROP_SUPPORTED_TYPES, &hdl,
1177c478bd9Sstevel@tonic-gate 	    (void *)&intr_types);
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 	return ((ret == DDI_SUCCESS) ? intr_types : 0);
1207c478bd9Sstevel@tonic-gate }
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate /*
1237c478bd9Sstevel@tonic-gate  * NOTE: This function is only called by i_ddi_dev_init().
1247c478bd9Sstevel@tonic-gate  */
1257c478bd9Sstevel@tonic-gate void
1267c478bd9Sstevel@tonic-gate i_ddi_intr_set_supported_types(dev_info_t *dip, int intr_types)
1277c478bd9Sstevel@tonic-gate {
1287c478bd9Sstevel@tonic-gate 	devinfo_intr_t		*intr_p = DEVI(dip)->devi_intr_p;
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 	if (intr_p)
1317c478bd9Sstevel@tonic-gate 		intr_p->devi_intr_sup_types = intr_types;
1327c478bd9Sstevel@tonic-gate }
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate uint_t
1357c478bd9Sstevel@tonic-gate i_ddi_intr_get_supported_nintrs(dev_info_t *dip, int intr_type)
1367c478bd9Sstevel@tonic-gate {
1377c478bd9Sstevel@tonic-gate 	devinfo_intr_t		*intr_p = DEVI(dip)->devi_intr_p;
1387c478bd9Sstevel@tonic-gate 	ddi_intr_handle_impl_t	hdl;
1397c478bd9Sstevel@tonic-gate 	int			ret, nintrs;
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 	if ((intr_p) && (intr_p->devi_intr_curr_type == intr_type) &&
1427c478bd9Sstevel@tonic-gate 	    (intr_p->devi_intr_sup_nintrs))
1437c478bd9Sstevel@tonic-gate 		return (intr_p->devi_intr_sup_nintrs);
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 	bzero(&hdl, sizeof (ddi_intr_handle_impl_t));
1467c478bd9Sstevel@tonic-gate 	hdl.ih_dip = dip;
1477c478bd9Sstevel@tonic-gate 	hdl.ih_type = intr_type;
1487c478bd9Sstevel@tonic-gate 
149a195726fSgovinda 	ret = i_ddi_intr_ops(dip, dip, DDI_INTROP_NINTRS, &hdl,
1507c478bd9Sstevel@tonic-gate 	    (void *)&nintrs);
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	return ((ret == DDI_SUCCESS) ? nintrs : 0);
1537c478bd9Sstevel@tonic-gate }
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate /*
1567c478bd9Sstevel@tonic-gate  * NOTE: This function is only called by ddi_intr_alloc().
1577c478bd9Sstevel@tonic-gate  */
1587c478bd9Sstevel@tonic-gate void
1597c478bd9Sstevel@tonic-gate i_ddi_intr_set_supported_nintrs(dev_info_t *dip, int nintrs)
1607c478bd9Sstevel@tonic-gate {
1617c478bd9Sstevel@tonic-gate 	devinfo_intr_t *intr_p = DEVI(dip)->devi_intr_p;
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 	if (intr_p)
1647c478bd9Sstevel@tonic-gate 		intr_p->devi_intr_sup_nintrs = nintrs;
1657c478bd9Sstevel@tonic-gate }
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate uint_t
1687c478bd9Sstevel@tonic-gate i_ddi_intr_get_current_type(dev_info_t *dip)
1697c478bd9Sstevel@tonic-gate {
1707c478bd9Sstevel@tonic-gate 	devinfo_intr_t *intr_p = DEVI(dip)->devi_intr_p;
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 	return (intr_p ? intr_p->devi_intr_curr_type : 0);
1737c478bd9Sstevel@tonic-gate }
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate /*
1767c478bd9Sstevel@tonic-gate  * NOTE: This function is only called by
1777c478bd9Sstevel@tonic-gate  *       ddi_intr_alloc() and ddi_intr_free().
1787c478bd9Sstevel@tonic-gate  */
1797c478bd9Sstevel@tonic-gate void
1807c478bd9Sstevel@tonic-gate i_ddi_intr_set_current_type(dev_info_t *dip, int intr_type)
1817c478bd9Sstevel@tonic-gate {
1827c478bd9Sstevel@tonic-gate 	devinfo_intr_t *intr_p = DEVI(dip)->devi_intr_p;
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	if (intr_p)
1857c478bd9Sstevel@tonic-gate 		intr_p->devi_intr_curr_type = intr_type;
1867c478bd9Sstevel@tonic-gate }
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate uint_t
1897c478bd9Sstevel@tonic-gate i_ddi_intr_get_current_nintrs(dev_info_t *dip)
1907c478bd9Sstevel@tonic-gate {
1917c478bd9Sstevel@tonic-gate 	devinfo_intr_t *intr_p = DEVI(dip)->devi_intr_p;
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 	return (intr_p ? intr_p->devi_intr_curr_nintrs : 0);
1947c478bd9Sstevel@tonic-gate }
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate /*
1977c478bd9Sstevel@tonic-gate  * NOTE: This function is only called by
1987c478bd9Sstevel@tonic-gate  *       ddi_intr_alloc() and ddi_intr_free().
1997c478bd9Sstevel@tonic-gate  */
2007c478bd9Sstevel@tonic-gate void
2017c478bd9Sstevel@tonic-gate i_ddi_intr_set_current_nintrs(dev_info_t *dip, int nintrs)
2027c478bd9Sstevel@tonic-gate {
2037c478bd9Sstevel@tonic-gate 	devinfo_intr_t *intr_p = DEVI(dip)->devi_intr_p;
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 	if (intr_p)
2067c478bd9Sstevel@tonic-gate 		intr_p->devi_intr_curr_nintrs = nintrs;
2077c478bd9Sstevel@tonic-gate }
2087c478bd9Sstevel@tonic-gate 
2095febcb4aSScott Carter, SD IOSW uint_t
2102145b8d4SGuoli Shu i_ddi_intr_get_current_nenables(dev_info_t *dip)
2112145b8d4SGuoli Shu {
2122145b8d4SGuoli Shu 	devinfo_intr_t *intr_p = DEVI(dip)->devi_intr_p;
2132145b8d4SGuoli Shu 
2142145b8d4SGuoli Shu 	return (intr_p ? intr_p->devi_intr_curr_nenables : 0);
2152145b8d4SGuoli Shu }
2162145b8d4SGuoli Shu 
2172145b8d4SGuoli Shu void
2182145b8d4SGuoli Shu i_ddi_intr_set_current_nenables(dev_info_t *dip, int nintrs)
2192145b8d4SGuoli Shu {
2202145b8d4SGuoli Shu 	devinfo_intr_t *intr_p = DEVI(dip)->devi_intr_p;
2212145b8d4SGuoli Shu 
2222145b8d4SGuoli Shu 	if (intr_p)
2232145b8d4SGuoli Shu 		intr_p->devi_intr_curr_nenables = nintrs;
2242145b8d4SGuoli Shu }
2252145b8d4SGuoli Shu 
2262145b8d4SGuoli Shu uint_t
2275febcb4aSScott Carter, SD IOSW i_ddi_intr_get_current_navail(dev_info_t *dip, int type)
2285febcb4aSScott Carter, SD IOSW {
2295febcb4aSScott Carter, SD IOSW 	ddi_intr_handle_impl_t	hdl;
2305febcb4aSScott Carter, SD IOSW 	devinfo_intr_t		*intr_p = DEVI(dip)->devi_intr_p;
2315febcb4aSScott Carter, SD IOSW 	ddi_cb_t		*cb_p;
2325febcb4aSScott Carter, SD IOSW 	ddi_irm_pool_t		*pool_p;
2335febcb4aSScott Carter, SD IOSW 	ddi_irm_req_t		*req_p;
234*63ea9ad2SEvan Yan 	uint_t			navail = 0, nintrs;
2355febcb4aSScott Carter, SD IOSW 
2365febcb4aSScott Carter, SD IOSW 	/* Get maximum number of supported interrupts */
2375febcb4aSScott Carter, SD IOSW 	nintrs = i_ddi_intr_get_supported_nintrs(dip, type);
2385febcb4aSScott Carter, SD IOSW 
2395febcb4aSScott Carter, SD IOSW 	/* Check for an interrupt pool */
2405febcb4aSScott Carter, SD IOSW 	pool_p = i_ddi_intr_get_pool(dip, type);
2415febcb4aSScott Carter, SD IOSW 
2425febcb4aSScott Carter, SD IOSW 	/*
2435febcb4aSScott Carter, SD IOSW 	 * If a pool exists, then IRM determines the availability.
2445febcb4aSScott Carter, SD IOSW 	 * Otherwise, use the older INTROP method.
2455febcb4aSScott Carter, SD IOSW 	 */
2465febcb4aSScott Carter, SD IOSW 	if (pool_p) {
2475febcb4aSScott Carter, SD IOSW 		if (intr_p && (req_p = intr_p->devi_irm_req_p) &&
2485febcb4aSScott Carter, SD IOSW 		    (type == req_p->ireq_type)) {
2495febcb4aSScott Carter, SD IOSW 			mutex_enter(&pool_p->ipool_navail_lock);
2505febcb4aSScott Carter, SD IOSW 			navail = req_p->ireq_navail;
2515febcb4aSScott Carter, SD IOSW 			mutex_exit(&pool_p->ipool_navail_lock);
2525febcb4aSScott Carter, SD IOSW 			return (navail);
2535febcb4aSScott Carter, SD IOSW 		}
2545febcb4aSScott Carter, SD IOSW 		if ((type == DDI_INTR_TYPE_MSIX) &&
2555febcb4aSScott Carter, SD IOSW 		    (cb_p = DEVI(dip)->devi_cb_p) &&
2565febcb4aSScott Carter, SD IOSW 		    (cb_p->cb_flags & DDI_CB_FLAG_INTR)) {
2575febcb4aSScott Carter, SD IOSW 			return (nintrs);
2585febcb4aSScott Carter, SD IOSW 		}
2595febcb4aSScott Carter, SD IOSW 		navail = pool_p->ipool_defsz;
2605febcb4aSScott Carter, SD IOSW 	} else {
2615febcb4aSScott Carter, SD IOSW 		bzero(&hdl, sizeof (ddi_intr_handle_impl_t));
2625febcb4aSScott Carter, SD IOSW 		hdl.ih_dip = dip;
2635febcb4aSScott Carter, SD IOSW 		hdl.ih_type = type;
2645febcb4aSScott Carter, SD IOSW 
2655febcb4aSScott Carter, SD IOSW 		if (i_ddi_intr_ops(dip, dip, DDI_INTROP_NAVAIL, &hdl,
2665febcb4aSScott Carter, SD IOSW 		    (void *)&navail) != DDI_SUCCESS) {
2675febcb4aSScott Carter, SD IOSW 			return (0);
2685febcb4aSScott Carter, SD IOSW 		}
2695febcb4aSScott Carter, SD IOSW 	}
2705febcb4aSScott Carter, SD IOSW 
271*63ea9ad2SEvan Yan #if defined(__i386) || defined(__amd64)
2725febcb4aSScott Carter, SD IOSW 	/* Global tunable workaround */
273*63ea9ad2SEvan Yan 	if (type == DDI_INTR_TYPE_MSIX) {
2745febcb4aSScott Carter, SD IOSW 		navail = MIN(nintrs, ddi_msix_alloc_limit);
2755febcb4aSScott Carter, SD IOSW 	}
276*63ea9ad2SEvan Yan #endif
2775febcb4aSScott Carter, SD IOSW 
2785febcb4aSScott Carter, SD IOSW 	/* Always restrict MSI to a precise limit */
2795febcb4aSScott Carter, SD IOSW 	if (type == DDI_INTR_TYPE_MSI)
2805febcb4aSScott Carter, SD IOSW 		navail = MIN(navail, DDI_MAX_MSI_ALLOC);
2815febcb4aSScott Carter, SD IOSW 
2825febcb4aSScott Carter, SD IOSW 	/* Ensure availability doesn't exceed what's supported */
2835febcb4aSScott Carter, SD IOSW 	navail = MIN(navail, nintrs);
2845febcb4aSScott Carter, SD IOSW 
2855febcb4aSScott Carter, SD IOSW 	return (navail);
2865febcb4aSScott Carter, SD IOSW }
2875febcb4aSScott Carter, SD IOSW 
2887c478bd9Sstevel@tonic-gate ddi_intr_msix_t *
2897c478bd9Sstevel@tonic-gate i_ddi_get_msix(dev_info_t *dip)
2907c478bd9Sstevel@tonic-gate {
2917c478bd9Sstevel@tonic-gate 	devinfo_intr_t *intr_p = DEVI(dip)->devi_intr_p;
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 	return (intr_p ? intr_p->devi_msix_p : NULL);
2947c478bd9Sstevel@tonic-gate }
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate void
2977c478bd9Sstevel@tonic-gate i_ddi_set_msix(dev_info_t *dip, ddi_intr_msix_t *msix_p)
2987c478bd9Sstevel@tonic-gate {
2997c478bd9Sstevel@tonic-gate 	devinfo_intr_t *intr_p = DEVI(dip)->devi_intr_p;
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 	if (intr_p)
3027c478bd9Sstevel@tonic-gate 		intr_p->devi_msix_p = msix_p;
3037c478bd9Sstevel@tonic-gate }
3047c478bd9Sstevel@tonic-gate 
3055febcb4aSScott Carter, SD IOSW ddi_intr_handle_t
3067c478bd9Sstevel@tonic-gate i_ddi_get_intr_handle(dev_info_t *dip, int inum)
3077c478bd9Sstevel@tonic-gate {
3087c478bd9Sstevel@tonic-gate 	devinfo_intr_t *intr_p = DEVI(dip)->devi_intr_p;
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate 	if (intr_p == NULL)
3117c478bd9Sstevel@tonic-gate 		return (NULL);
3127c478bd9Sstevel@tonic-gate 
313e821bd34Sanish 	/*
314e821bd34Sanish 	 * Changed this to a check and return NULL if an invalid inum
315e821bd34Sanish 	 * is passed to retrieve a handle
316e821bd34Sanish 	 */
317e821bd34Sanish 	if ((inum < 0) || (inum >= intr_p->devi_intr_sup_nintrs))
318e821bd34Sanish 		return (NULL);
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 	return ((intr_p->devi_intr_handle_p) ?
3217c478bd9Sstevel@tonic-gate 	    intr_p->devi_intr_handle_p[inum] : NULL);
3227c478bd9Sstevel@tonic-gate }
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate void
3255febcb4aSScott Carter, SD IOSW i_ddi_set_intr_handle(dev_info_t *dip, int inum, ddi_intr_handle_t intr_hdl)
3267c478bd9Sstevel@tonic-gate {
3277c478bd9Sstevel@tonic-gate 	devinfo_intr_t	*intr_p = DEVI(dip)->devi_intr_p;
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 	if (intr_p == NULL)
3307c478bd9Sstevel@tonic-gate 		return;
3317c478bd9Sstevel@tonic-gate 
332e821bd34Sanish 	/*
333e821bd34Sanish 	 * Changed this to a check and return if an invalid inum
334e821bd34Sanish 	 * is passed to set a handle
335e821bd34Sanish 	 */
336e821bd34Sanish 	if ((inum < 0) || (inum >= intr_p->devi_intr_sup_nintrs))
337e821bd34Sanish 		return;
3387c478bd9Sstevel@tonic-gate 
3395febcb4aSScott Carter, SD IOSW 	if (intr_hdl && (intr_p->devi_intr_handle_p == NULL)) {
3407c478bd9Sstevel@tonic-gate 		/* nintrs could be zero; so check for it first */
3417c478bd9Sstevel@tonic-gate 		if (intr_p->devi_intr_sup_nintrs)
3427c478bd9Sstevel@tonic-gate 			intr_p->devi_intr_handle_p = kmem_zalloc(
3437c478bd9Sstevel@tonic-gate 			    sizeof (ddi_intr_handle_t) *
3447c478bd9Sstevel@tonic-gate 			    intr_p->devi_intr_sup_nintrs, KM_SLEEP);
3457c478bd9Sstevel@tonic-gate 	}
3467c478bd9Sstevel@tonic-gate 
347a195726fSgovinda 	if (intr_p->devi_intr_handle_p)
3485febcb4aSScott Carter, SD IOSW 		intr_p->devi_intr_handle_p[inum] = intr_hdl;
3497c478bd9Sstevel@tonic-gate }
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate /*
3527c478bd9Sstevel@tonic-gate  * The "ddi-intr-weight" property contains the weight of each interrupt
3537c478bd9Sstevel@tonic-gate  * associated with a dev_info node. For devices with multiple interrupts per
3547c478bd9Sstevel@tonic-gate  * dev_info node, the total load of the device is "devi_intr_weight * nintr",
3557c478bd9Sstevel@tonic-gate  * possibly spread out over multiple CPUs.
3567c478bd9Sstevel@tonic-gate  *
3577c478bd9Sstevel@tonic-gate  * Maintaining this as a property permits possible tweaking in the product
3587c478bd9Sstevel@tonic-gate  * in response to customer problems via driver.conf property definitions at
3597c478bd9Sstevel@tonic-gate  * the driver or the instance level.  This does not mean that "ddi-intr_weight"
3607c478bd9Sstevel@tonic-gate  * is a formal or committed interface.
3617c478bd9Sstevel@tonic-gate  */
3627c478bd9Sstevel@tonic-gate int32_t
3637c478bd9Sstevel@tonic-gate i_ddi_get_intr_weight(dev_info_t *dip)
3647c478bd9Sstevel@tonic-gate {
3657c478bd9Sstevel@tonic-gate 	int32_t	weight;
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 	weight = ddi_prop_get_int(DDI_DEV_T_ANY, dip,
3687c478bd9Sstevel@tonic-gate 	    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, "ddi-intr-weight", -1);
3697c478bd9Sstevel@tonic-gate 	if (weight < -1)
3707c478bd9Sstevel@tonic-gate 		weight = -1;			/* undefined */
3717c478bd9Sstevel@tonic-gate 	return (weight);
3727c478bd9Sstevel@tonic-gate }
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate int32_t
3757c478bd9Sstevel@tonic-gate i_ddi_set_intr_weight(dev_info_t *dip, int32_t weight)
3767c478bd9Sstevel@tonic-gate {
3777c478bd9Sstevel@tonic-gate 	int32_t oweight;
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 	oweight = i_ddi_get_intr_weight(dip);
3807c478bd9Sstevel@tonic-gate 	if ((weight > 0) && (oweight != weight))
3817c478bd9Sstevel@tonic-gate 		(void) ndi_prop_update_int(DDI_DEV_T_NONE, dip,
3827c478bd9Sstevel@tonic-gate 		    "ddi-intr-weight", weight);
3837c478bd9Sstevel@tonic-gate 	return (oweight);
3847c478bd9Sstevel@tonic-gate }
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate /*
3877c478bd9Sstevel@tonic-gate  * Old DDI interrupt framework
3887c478bd9Sstevel@tonic-gate  *
3897c478bd9Sstevel@tonic-gate  * NOTE:
3907c478bd9Sstevel@tonic-gate  *	The following 4 busops entry points are obsoleted with version
3917c478bd9Sstevel@tonic-gate  *	9 or greater. Use i_ddi_intr_op interface in place of these
3927c478bd9Sstevel@tonic-gate  *	obsolete interfaces.
3937c478bd9Sstevel@tonic-gate  *
3947c478bd9Sstevel@tonic-gate  *	Remove these busops entry points and all related data structures
3957c478bd9Sstevel@tonic-gate  *	in future major/minor solaris release.
3967c478bd9Sstevel@tonic-gate  */
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate /* ARGSUSED */
3997c478bd9Sstevel@tonic-gate ddi_intrspec_t
4007c478bd9Sstevel@tonic-gate i_ddi_get_intrspec(dev_info_t *dip, dev_info_t *rdip, uint_t inumber)
4017c478bd9Sstevel@tonic-gate {
4027c478bd9Sstevel@tonic-gate 	dev_info_t	*pdip = ddi_get_parent(dip);
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 	cmn_err(CE_WARN, "Failed to process interrupt "
4057c478bd9Sstevel@tonic-gate 	    "for %s%d due to down-rev nexus driver %s%d",
406a195726fSgovinda 	    ddi_driver_name(rdip), ddi_get_instance(rdip),
407a195726fSgovinda 	    ddi_driver_name(pdip), ddi_get_instance(pdip));
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate 	return (NULL);
4107c478bd9Sstevel@tonic-gate }
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate /* ARGSUSED */
4137c478bd9Sstevel@tonic-gate int
4147c478bd9Sstevel@tonic-gate i_ddi_add_intrspec(dev_info_t *dip, dev_info_t *rdip, ddi_intrspec_t intrspec,
4157c478bd9Sstevel@tonic-gate     ddi_iblock_cookie_t *iblock_cookiep,
4167c478bd9Sstevel@tonic-gate     ddi_idevice_cookie_t *idevice_cookiep,
4177c478bd9Sstevel@tonic-gate     uint_t (*int_handler)(caddr_t int_handler_arg),
4187c478bd9Sstevel@tonic-gate     caddr_t int_handler_arg, int kind)
4197c478bd9Sstevel@tonic-gate {
4207c478bd9Sstevel@tonic-gate 	dev_info_t	*pdip = ddi_get_parent(dip);
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 	cmn_err(CE_WARN, "Failed to process interrupt "
4237c478bd9Sstevel@tonic-gate 	    "for %s%d due to down-rev nexus driver %s%d",
424a195726fSgovinda 	    ddi_driver_name(rdip), ddi_get_instance(rdip),
425a195726fSgovinda 	    ddi_driver_name(pdip), ddi_get_instance(pdip));
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate 	return (DDI_ENOTSUP);
4287c478bd9Sstevel@tonic-gate }
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate /* ARGSUSED */
4317c478bd9Sstevel@tonic-gate void
4327c478bd9Sstevel@tonic-gate i_ddi_remove_intrspec(dev_info_t *dip, dev_info_t *rdip,
4337c478bd9Sstevel@tonic-gate     ddi_intrspec_t intrspec, ddi_iblock_cookie_t iblock_cookie)
4347c478bd9Sstevel@tonic-gate {
4357c478bd9Sstevel@tonic-gate 	dev_info_t	*pdip = ddi_get_parent(dip);
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate 	cmn_err(CE_WARN, "Failed to process interrupt "
4387c478bd9Sstevel@tonic-gate 	    "for %s%d due to down-rev nexus driver %s%d",
439a195726fSgovinda 	    ddi_driver_name(rdip), ddi_get_instance(rdip),
440a195726fSgovinda 	    ddi_driver_name(pdip), ddi_get_instance(pdip));
4417c478bd9Sstevel@tonic-gate }
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate /* ARGSUSED */
4447c478bd9Sstevel@tonic-gate int
4457c478bd9Sstevel@tonic-gate i_ddi_intr_ctlops(dev_info_t *dip, dev_info_t *rdip, ddi_intr_ctlop_t op,
4467c478bd9Sstevel@tonic-gate     void *arg, void *val)
4477c478bd9Sstevel@tonic-gate {
4487c478bd9Sstevel@tonic-gate 	dev_info_t	*pdip = ddi_get_parent(dip);
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 	cmn_err(CE_WARN, "Failed to process interrupt "
4517c478bd9Sstevel@tonic-gate 	    "for %s%d due to down-rev nexus driver %s%d",
452a195726fSgovinda 	    ddi_driver_name(rdip), ddi_get_instance(rdip),
453a195726fSgovinda 	    ddi_driver_name(pdip), ddi_get_instance(pdip));
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 	return (DDI_ENOTSUP);
4567c478bd9Sstevel@tonic-gate }
457d12abe7cSanish 
458d12abe7cSanish #if defined(__i386) || defined(__amd64)
459d12abe7cSanish ddi_acc_handle_t
460d12abe7cSanish i_ddi_get_pci_config_handle(dev_info_t *dip)
461d12abe7cSanish {
462d12abe7cSanish 	devinfo_intr_t *intr_p = DEVI(dip)->devi_intr_p;
463d12abe7cSanish 
464d12abe7cSanish 	return (intr_p ? intr_p->devi_cfg_handle : NULL);
465d12abe7cSanish }
466d12abe7cSanish 
467d12abe7cSanish void
468d12abe7cSanish i_ddi_set_pci_config_handle(dev_info_t *dip, ddi_acc_handle_t handle)
469d12abe7cSanish {
470d12abe7cSanish 	devinfo_intr_t *intr_p = DEVI(dip)->devi_intr_p;
471d12abe7cSanish 
472d12abe7cSanish 	if (intr_p)
473d12abe7cSanish 		intr_p->devi_cfg_handle = handle;
474d12abe7cSanish }
475d12abe7cSanish 
476d12abe7cSanish 
477d12abe7cSanish int
478d12abe7cSanish i_ddi_get_msi_msix_cap_ptr(dev_info_t *dip)
479d12abe7cSanish {
480d12abe7cSanish 	devinfo_intr_t *intr_p = DEVI(dip)->devi_intr_p;
481d12abe7cSanish 
482d12abe7cSanish 	return (intr_p ? intr_p->devi_cap_ptr : 0);
483d12abe7cSanish }
484d12abe7cSanish 
485d12abe7cSanish void
486d12abe7cSanish i_ddi_set_msi_msix_cap_ptr(dev_info_t *dip, int cap_ptr)
487d12abe7cSanish {
488d12abe7cSanish 	devinfo_intr_t *intr_p = DEVI(dip)->devi_intr_p;
489d12abe7cSanish 
490d12abe7cSanish 	if (intr_p)
491d12abe7cSanish 		intr_p->devi_cap_ptr = cap_ptr;
492d12abe7cSanish }
493d12abe7cSanish #endif
494