xref: /onnv-gate/usr/src/uts/common/os/ddi_intr.c (revision 4974:c7454a80ea3d)
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
51542Sjohnny  * Common Development and Distribution License (the "License").
61542Sjohnny  * 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 /*
223625Segillett  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <sys/note.h>
290Sstevel@tonic-gate #include <sys/sysmacros.h>
300Sstevel@tonic-gate #include <sys/types.h>
310Sstevel@tonic-gate #include <sys/param.h>
320Sstevel@tonic-gate #include <sys/systm.h>
330Sstevel@tonic-gate #include <sys/kmem.h>
340Sstevel@tonic-gate #include <sys/cmn_err.h>
350Sstevel@tonic-gate #include <sys/debug.h>
360Sstevel@tonic-gate #include <sys/avintr.h>
370Sstevel@tonic-gate #include <sys/autoconf.h>
380Sstevel@tonic-gate #include <sys/sunndi.h>
390Sstevel@tonic-gate #include <sys/ndi_impldefs.h>	/* include prototypes */
401725Segillett #include <sys/atomic.h>
410Sstevel@tonic-gate 
420Sstevel@tonic-gate /*
430Sstevel@tonic-gate  * New DDI interrupt framework
440Sstevel@tonic-gate  */
450Sstevel@tonic-gate 
460Sstevel@tonic-gate /*
47*4974Segillett  * MSI-X allocation limit.
48*4974Segillett  *
49*4974Segillett  * This MSI-X limit or tunable may be obsolete or change with Interrupt
50*4974Segillett  * Resource Management (IRM) support.
510Sstevel@tonic-gate  */
52*4974Segillett uint_t		ddi_msix_alloc_limit = DDI_DEFAULT_MSIX_ALLOC;
530Sstevel@tonic-gate 
540Sstevel@tonic-gate /*
550Sstevel@tonic-gate  * ddi_intr_get_supported_types:
560Sstevel@tonic-gate  *	Return, as a bit mask, the hardware interrupt types supported by
570Sstevel@tonic-gate  *	both the device and by the host in the integer pointed
580Sstevel@tonic-gate  *	to be the 'typesp' argument.
590Sstevel@tonic-gate  */
600Sstevel@tonic-gate int
610Sstevel@tonic-gate ddi_intr_get_supported_types(dev_info_t *dip, int *typesp)
620Sstevel@tonic-gate {
630Sstevel@tonic-gate 	int			ret;
640Sstevel@tonic-gate 	ddi_intr_handle_impl_t	hdl;
650Sstevel@tonic-gate 
660Sstevel@tonic-gate 	if (dip == NULL)
670Sstevel@tonic-gate 		return (DDI_EINVAL);
680Sstevel@tonic-gate 
690Sstevel@tonic-gate 	DDI_INTR_APIDBG((CE_CONT, "ddi_intr_get_supported_types: dip %p\n",
700Sstevel@tonic-gate 	    (void *)dip));
710Sstevel@tonic-gate 
720Sstevel@tonic-gate 	if (*typesp = i_ddi_intr_get_supported_types(dip))
730Sstevel@tonic-gate 		return (DDI_SUCCESS);
740Sstevel@tonic-gate 
750Sstevel@tonic-gate 	bzero(&hdl, sizeof (ddi_intr_handle_impl_t));
760Sstevel@tonic-gate 	hdl.ih_dip = dip;
770Sstevel@tonic-gate 
78693Sgovinda 	ret = i_ddi_intr_ops(dip, dip, DDI_INTROP_SUPPORTED_TYPES, &hdl,
790Sstevel@tonic-gate 	    (void *)typesp);
800Sstevel@tonic-gate 
810Sstevel@tonic-gate 	if (ret != DDI_SUCCESS)
820Sstevel@tonic-gate 		return (DDI_INTR_NOTFOUND);
830Sstevel@tonic-gate 
840Sstevel@tonic-gate 	DDI_INTR_APIDBG((CE_CONT, "ddi_intr_get_supported_types: types %x\n",
850Sstevel@tonic-gate 	    *typesp));
860Sstevel@tonic-gate 
870Sstevel@tonic-gate 	return (ret);
880Sstevel@tonic-gate }
890Sstevel@tonic-gate 
900Sstevel@tonic-gate 
910Sstevel@tonic-gate /*
920Sstevel@tonic-gate  * ddi_intr_get_nintrs:
930Sstevel@tonic-gate  * 	Return as an integer in the integer pointed to by the argument
940Sstevel@tonic-gate  * 	*nintrsp*, the number of interrupts the device supports for the
950Sstevel@tonic-gate  *	given interrupt type.
960Sstevel@tonic-gate  */
970Sstevel@tonic-gate int
980Sstevel@tonic-gate ddi_intr_get_nintrs(dev_info_t *dip, int type, int *nintrsp)
990Sstevel@tonic-gate {
1000Sstevel@tonic-gate 	int			ret;
1010Sstevel@tonic-gate 	ddi_intr_handle_impl_t	hdl;
1020Sstevel@tonic-gate 
103693Sgovinda 	DDI_INTR_APIDBG((CE_CONT, "ddi_intr_get_nintrs: dip %p, type: %d\n",
104693Sgovinda 	    (void *)dip, type));
1050Sstevel@tonic-gate 
1062433Sanish 	if ((dip == NULL) || !DDI_INTR_TYPE_FLAG_VALID(type) ||
1072433Sanish 	    !(i_ddi_intr_get_supported_types(dip) & type)) {
108693Sgovinda 		*nintrsp = 0;
1092433Sanish 		DDI_INTR_APIDBG((CE_CONT, "ddi_intr_get_nintrs: Invalid "
1102433Sanish 		    "input args\n"));
1110Sstevel@tonic-gate 		return (DDI_EINVAL);
112693Sgovinda 	}
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate 	if (*nintrsp = i_ddi_intr_get_supported_nintrs(dip, type))
1150Sstevel@tonic-gate 		return (DDI_SUCCESS);
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate 	bzero(&hdl, sizeof (ddi_intr_handle_impl_t));
1180Sstevel@tonic-gate 	hdl.ih_dip = dip;
1190Sstevel@tonic-gate 	hdl.ih_type = type;
1200Sstevel@tonic-gate 
121693Sgovinda 	ret = i_ddi_intr_ops(dip, dip, DDI_INTROP_NINTRS, &hdl,
1220Sstevel@tonic-gate 	    (void *)nintrsp);
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate 	DDI_INTR_APIDBG((CE_CONT, "ddi_intr_get_nintrs:: nintrs %x\n",
1250Sstevel@tonic-gate 	    *nintrsp));
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate 	return (ret);
1280Sstevel@tonic-gate }
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate /*
1320Sstevel@tonic-gate  * ddi_intr_get_navail:
1330Sstevel@tonic-gate  *	Bus nexus driver will return availble interrupt count value for
1340Sstevel@tonic-gate  *	a given interrupt type.
1350Sstevel@tonic-gate  *
1360Sstevel@tonic-gate  * 	Return as an integer in the integer pointed to by the argument
1370Sstevel@tonic-gate  * 	*navailp*, the number of interrupts currently available for the
1380Sstevel@tonic-gate  *	given interrupt type.
1390Sstevel@tonic-gate  */
1400Sstevel@tonic-gate int
1410Sstevel@tonic-gate ddi_intr_get_navail(dev_info_t *dip, int type, int *navailp)
1420Sstevel@tonic-gate {
1430Sstevel@tonic-gate 	int			ret;
1440Sstevel@tonic-gate 	ddi_intr_handle_impl_t	hdl;
1450Sstevel@tonic-gate 
146693Sgovinda 	DDI_INTR_APIDBG((CE_CONT, "ddi_intr_get_navail: dip %p, type: %d\n",
147693Sgovinda 	    (void *)dip, type));
1480Sstevel@tonic-gate 
1492433Sanish 	if ((dip == NULL) || !DDI_INTR_TYPE_FLAG_VALID(type) ||
1502433Sanish 	    !(i_ddi_intr_get_supported_types(dip) & type)) {
151693Sgovinda 		*navailp = 0;
1522433Sanish 		DDI_INTR_APIDBG((CE_CONT, "ddi_intr_get_navail: Invalid "
1532433Sanish 		    "input args\n"));
1540Sstevel@tonic-gate 		return (DDI_EINVAL);
155693Sgovinda 	}
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate 	/*
1580Sstevel@tonic-gate 	 * In future, this interface implementation will change
1590Sstevel@tonic-gate 	 * with Resource Management support.
1600Sstevel@tonic-gate 	 */
1610Sstevel@tonic-gate 	bzero(&hdl, sizeof (ddi_intr_handle_impl_t));
1620Sstevel@tonic-gate 	hdl.ih_dip = dip;
1630Sstevel@tonic-gate 	hdl.ih_type = type;
1640Sstevel@tonic-gate 
165693Sgovinda 	ret = i_ddi_intr_ops(dip, dip, DDI_INTROP_NAVAIL, &hdl,
166693Sgovinda 	    (void *)navailp);
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 	return (ret == DDI_SUCCESS ? DDI_SUCCESS : DDI_INTR_NOTFOUND);
1690Sstevel@tonic-gate }
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate /*
1730Sstevel@tonic-gate  * Interrupt allocate/free functions
1740Sstevel@tonic-gate  */
1750Sstevel@tonic-gate int
1760Sstevel@tonic-gate ddi_intr_alloc(dev_info_t *dip, ddi_intr_handle_t *h_array, int type, int inum,
1770Sstevel@tonic-gate     int count, int *actualp, int behavior)
1780Sstevel@tonic-gate {
1790Sstevel@tonic-gate 	ddi_intr_handle_impl_t	*hdlp, tmp_hdl;
1800Sstevel@tonic-gate 	int			i, ret, cap = 0, intr_type, nintrs = 0;
1813625Segillett 	uint_t			pri, curr_nintrs = 0;
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate 	DDI_INTR_APIDBG((CE_CONT, "ddi_intr_alloc: name %s dip 0x%p "
1840Sstevel@tonic-gate 	    "type %x inum %x count %x behavior %x\n", ddi_driver_name(dip),
1850Sstevel@tonic-gate 	    (void *)dip, type, inum, count, behavior));
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate 	/* Validate parameters */
1882433Sanish 	if (dip == NULL || h_array == NULL || count < 1 || inum < 0 ||
1892404Sanish 	    !DDI_INTR_BEHAVIOR_FLAG_VALID(behavior)) {
1900Sstevel@tonic-gate 		DDI_INTR_APIDBG((CE_CONT, "ddi_intr_alloc: Invalid args\n"));
1910Sstevel@tonic-gate 		return (DDI_EINVAL);
1920Sstevel@tonic-gate 	}
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate 	/* Validate interrupt type */
1952433Sanish 	if (!DDI_INTR_TYPE_FLAG_VALID(type) ||
1962433Sanish 	    !(i_ddi_intr_get_supported_types(dip) & type)) {
1970Sstevel@tonic-gate 		DDI_INTR_APIDBG((CE_CONT, "ddi_intr_alloc: type %x not "
1980Sstevel@tonic-gate 		    "supported\n", type));
1990Sstevel@tonic-gate 		return (DDI_EINVAL);
2000Sstevel@tonic-gate 	}
2010Sstevel@tonic-gate 
2022433Sanish 	/*
2032433Sanish 	 * Check if the 'inum' was previously allocated or not? Fail, if so.
2042433Sanish 	 */
2052433Sanish 	if ((type == DDI_INTR_TYPE_FIXED) &&
2062433Sanish 	    (i_ddi_get_intr_handle(dip, inum) != NULL)) {
2072433Sanish 		DDI_INTR_APIDBG((CE_CONT, "ddi_intr_alloc: inum %d is already "
2082433Sanish 		    "in use, can not allocate again!!\n", inum));
2092433Sanish 		return (DDI_EINVAL);
2102433Sanish 	}
2112433Sanish 
2120Sstevel@tonic-gate 	/* First, get how many interrupts the device supports */
2130Sstevel@tonic-gate 	if (!(nintrs = i_ddi_intr_get_supported_nintrs(dip, type))) {
2140Sstevel@tonic-gate 		if (ddi_intr_get_nintrs(dip, type, &nintrs) != DDI_SUCCESS) {
2150Sstevel@tonic-gate 			DDI_INTR_APIDBG((CE_CONT, "ddi_intr_alloc: no "
2160Sstevel@tonic-gate 			    "interrupts found of type %d\n", type));
2170Sstevel@tonic-gate 			return (DDI_INTR_NOTFOUND);
2180Sstevel@tonic-gate 		}
2190Sstevel@tonic-gate 	}
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate 	/* Is this function invoked with more interrupt than device supports? */
2220Sstevel@tonic-gate 	if (count > nintrs) {
2230Sstevel@tonic-gate 		DDI_INTR_APIDBG((CE_CONT, "ddi_intr_alloc: no of interrupts "
2240Sstevel@tonic-gate 		    "requested %d is more than supported %d\n", count, nintrs));
2250Sstevel@tonic-gate 		return (DDI_EINVAL);
2260Sstevel@tonic-gate 	}
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate 	/*
2290Sstevel@tonic-gate 	 * Check if requested interrupt type is not same as interrupt
2300Sstevel@tonic-gate 	 * type is in use if any.
2310Sstevel@tonic-gate 	 */
2320Sstevel@tonic-gate 	if (((intr_type = i_ddi_intr_get_current_type(dip)) != 0) &&
2330Sstevel@tonic-gate 	    (intr_type != type)) {
2340Sstevel@tonic-gate 		DDI_INTR_APIDBG((CE_CONT, "ddi_intr_alloc: Requested "
2350Sstevel@tonic-gate 		    "interrupt type %x is different from interrupt type %x"
2360Sstevel@tonic-gate 		    "already in use\n", type, intr_type));
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate 		return (DDI_EINVAL);
2390Sstevel@tonic-gate 	}
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate 	/*
2420Sstevel@tonic-gate 	 * Check if requested interrupt type is in use and requested number
2430Sstevel@tonic-gate 	 * of interrupts and number of interrupts already in use exceeds the
2440Sstevel@tonic-gate 	 * number of interrupts supported by this device.
2450Sstevel@tonic-gate 	 */
2460Sstevel@tonic-gate 	if (intr_type) {
2470Sstevel@tonic-gate 		DDI_INTR_APIDBG((CE_CONT, "ddi_intr_alloc: type %x "
2480Sstevel@tonic-gate 		    "is already being used\n", type));
2490Sstevel@tonic-gate 
2503625Segillett 		curr_nintrs = i_ddi_intr_get_current_nintrs(dip);
2513625Segillett 		if ((count + curr_nintrs) > nintrs) {
2520Sstevel@tonic-gate 			DDI_INTR_APIDBG((CE_CONT, "ddi_intr_alloc: count %d "
2530Sstevel@tonic-gate 			    "+ intrs in use %d exceeds supported %d intrs\n",
2543625Segillett 			    count, curr_nintrs, nintrs));
2550Sstevel@tonic-gate 			return (DDI_EINVAL);
2560Sstevel@tonic-gate 		}
2570Sstevel@tonic-gate 	}
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate 	/*
2600Sstevel@tonic-gate 	 * For MSI, ensure that the requested interrupt count is a power of 2
2610Sstevel@tonic-gate 	 */
2620Sstevel@tonic-gate 	if (type == DDI_INTR_TYPE_MSI && !ISP2(count)) {
2630Sstevel@tonic-gate 		DDI_INTR_APIDBG((CE_CONT, "ddi_intr_alloc: "
2640Sstevel@tonic-gate 		    "MSI count %d is not a power of two\n", count));
2650Sstevel@tonic-gate 		return (DDI_EINVAL);
2660Sstevel@tonic-gate 	}
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate 	/*
269*4974Segillett 	 * NOTE:
270*4974Segillett 	 *
271*4974Segillett 	 * An intermediate solution is added here to allocate more MSI-X
272*4974Segillett 	 * interrupts to drivers to address some significant performance
273*4974Segillett 	 * issues discovered on various SPARC platforms. More MSI-X interrupts
274*4974Segillett 	 * will be allocated based on existence of "#msix-request" property.
275*4974Segillett 	 * The DDI framework will not honor this property after the Interrupt
276*4974Segillett 	 * Resource Management (IRM) project's integration.
277*4974Segillett 	 *
278*4974Segillett 	 * Hard limit for maximum MSI allocation is set to DDI_MAX_MSI_ALLOC,
279*4974Segillett 	 * however MSI-X's max allocation is controlled by
280*4974Segillett 	 * ddi_msix_alloc_limit().
2810Sstevel@tonic-gate 	 */
2823625Segillett 	if (DDI_INTR_IS_MSI_OR_MSIX(type)) {
283*4974Segillett 		uint_t	alloc_limit = (type == DDI_INTR_TYPE_MSIX) ?
284*4974Segillett 		    i_ddi_get_msix_alloc_limit(dip) : DDI_MAX_MSI_ALLOC;
285*4974Segillett 
286*4974Segillett 		if (curr_nintrs == alloc_limit) {
2870Sstevel@tonic-gate 			DDI_INTR_APIDBG((CE_CONT, "ddi_intr_alloc: "
2883625Segillett 			    "max # of intrs %d already allocated\n",
2893625Segillett 			    curr_nintrs));
2903625Segillett 			return (DDI_EINVAL);
2913625Segillett 		}
292*4974Segillett 		if ((count + curr_nintrs) > alloc_limit) {
2933625Segillett 			DDI_INTR_APIDBG((CE_CONT, "ddi_intr_alloc: Requested "
2943625Segillett 			    "MSI/Xs %d Max MSI/Xs limit %d\n", count,
295*4974Segillett 			    alloc_limit));
2960Sstevel@tonic-gate 
2973625Segillett 			if (behavior == DDI_INTR_ALLOC_STRICT) {
2983625Segillett 				DDI_INTR_APIDBG((CE_CONT, "ddi_intr_alloc: "
2993625Segillett 				    "DDI_INTR_ALLOC_STRICT flag is passed, "
3003625Segillett 				    "return failure\n"));
3013625Segillett 				return (DDI_EAGAIN);
3023625Segillett 			}
3033625Segillett 
304*4974Segillett 			count = alloc_limit - curr_nintrs;
3050Sstevel@tonic-gate 		}
3060Sstevel@tonic-gate 	}
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate 	/* Now allocate required number of interrupts */
3090Sstevel@tonic-gate 	bzero(&tmp_hdl, sizeof (ddi_intr_handle_impl_t));
3100Sstevel@tonic-gate 	tmp_hdl.ih_type = type;
3110Sstevel@tonic-gate 	tmp_hdl.ih_inum = inum;
3120Sstevel@tonic-gate 	tmp_hdl.ih_scratch1 = count;
3131717Swesolows 	tmp_hdl.ih_scratch2 = (void *)(uintptr_t)behavior;
3140Sstevel@tonic-gate 	tmp_hdl.ih_dip = dip;
3150Sstevel@tonic-gate 
3161725Segillett 	i_ddi_intr_devi_init(dip);
3171725Segillett 
318693Sgovinda 	if (i_ddi_intr_ops(dip, dip, DDI_INTROP_ALLOC,
3190Sstevel@tonic-gate 	    &tmp_hdl, (void *)actualp) != DDI_SUCCESS) {
3200Sstevel@tonic-gate 		DDI_INTR_APIDBG((CE_CONT, "ddi_intr_alloc: allocation "
3210Sstevel@tonic-gate 		    "failed\n"));
3221725Segillett 		i_ddi_intr_devi_fini(dip);
3230Sstevel@tonic-gate 		return (*actualp ? DDI_EAGAIN : DDI_INTR_NOTFOUND);
3240Sstevel@tonic-gate 	}
3250Sstevel@tonic-gate 
326693Sgovinda 	if ((ret = i_ddi_intr_ops(dip, dip, DDI_INTROP_GETPRI,
3270Sstevel@tonic-gate 	    &tmp_hdl, (void *)&pri)) != DDI_SUCCESS) {
3280Sstevel@tonic-gate 		DDI_INTR_APIDBG((CE_CONT, "ddi_intr_alloc: get priority "
3290Sstevel@tonic-gate 		    "failed\n"));
3301725Segillett 		goto fail;
3310Sstevel@tonic-gate 	}
3320Sstevel@tonic-gate 
3330Sstevel@tonic-gate 	DDI_INTR_APIDBG((CE_CONT, "ddi_intr_alloc: getting capability\n"));
3340Sstevel@tonic-gate 
335693Sgovinda 	if ((ret = i_ddi_intr_ops(dip, dip, DDI_INTROP_GETCAP,
3360Sstevel@tonic-gate 	    &tmp_hdl, (void *)&cap)) != DDI_SUCCESS) {
3370Sstevel@tonic-gate 		DDI_INTR_APIDBG((CE_CONT, "ddi_intr_alloc: get capability "
3380Sstevel@tonic-gate 		    "failed\n"));
3391725Segillett 		goto fail;
3400Sstevel@tonic-gate 	}
3410Sstevel@tonic-gate 
3421725Segillett 	/*
3431725Segillett 	 * Save current interrupt type, supported and current intr count.
3441725Segillett 	 */
3450Sstevel@tonic-gate 	i_ddi_intr_set_current_type(dip, type);
3460Sstevel@tonic-gate 	i_ddi_intr_set_supported_nintrs(dip, nintrs);
3470Sstevel@tonic-gate 	i_ddi_intr_set_current_nintrs(dip,
3480Sstevel@tonic-gate 	    i_ddi_intr_get_current_nintrs(dip) + *actualp);
3490Sstevel@tonic-gate 
3500Sstevel@tonic-gate 	/* Now, go and handle each "handle" */
3510Sstevel@tonic-gate 	for (i = 0; i < *actualp; i++) {
3520Sstevel@tonic-gate 		hdlp = (ddi_intr_handle_impl_t *)kmem_zalloc(
3530Sstevel@tonic-gate 		    (sizeof (ddi_intr_handle_impl_t)), KM_SLEEP);
3540Sstevel@tonic-gate 		rw_init(&hdlp->ih_rwlock, NULL, RW_DRIVER, NULL);
3550Sstevel@tonic-gate 		h_array[i] = (struct __ddi_intr_handle *)hdlp;
3560Sstevel@tonic-gate 		hdlp->ih_type = type;
3570Sstevel@tonic-gate 		hdlp->ih_pri = pri;
3580Sstevel@tonic-gate 		hdlp->ih_cap = cap;
3590Sstevel@tonic-gate 		hdlp->ih_ver = DDI_INTR_VERSION;
3600Sstevel@tonic-gate 		hdlp->ih_state = DDI_IHDL_STATE_ALLOC;
3610Sstevel@tonic-gate 		hdlp->ih_dip = dip;
3620Sstevel@tonic-gate 		hdlp->ih_inum = inum + i;
363916Sschwartz 		i_ddi_alloc_intr_phdl(hdlp);
3640Sstevel@tonic-gate 		if (type & DDI_INTR_TYPE_FIXED)
3659Segillett 			i_ddi_set_intr_handle(dip, hdlp->ih_inum, &h_array[i]);
3660Sstevel@tonic-gate 
3670Sstevel@tonic-gate 		DDI_INTR_APIDBG((CE_CONT, "ddi_intr_alloc: hdlp = 0x%p\n",
3680Sstevel@tonic-gate 		    (void *)h_array[i]));
3690Sstevel@tonic-gate 	}
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate 	return (DDI_SUCCESS);
3721725Segillett 
3731725Segillett fail:
3741725Segillett 	(void) i_ddi_intr_ops(tmp_hdl.ih_dip, tmp_hdl.ih_dip,
3751725Segillett 	    DDI_INTROP_FREE, &tmp_hdl, NULL);
3761725Segillett 	i_ddi_intr_devi_fini(dip);
3771725Segillett 
3781725Segillett 	return (ret);
3790Sstevel@tonic-gate }
3800Sstevel@tonic-gate 
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate int
3830Sstevel@tonic-gate ddi_intr_free(ddi_intr_handle_t h)
3840Sstevel@tonic-gate {
3850Sstevel@tonic-gate 	ddi_intr_handle_impl_t	*hdlp = (ddi_intr_handle_impl_t *)h;
3860Sstevel@tonic-gate 	int			ret;
3870Sstevel@tonic-gate 
3880Sstevel@tonic-gate 	DDI_INTR_APIDBG((CE_CONT, "ddi_intr_free: hdlp = %p\n", (void *)hdlp));
3890Sstevel@tonic-gate 
3900Sstevel@tonic-gate 	if (hdlp == NULL)
3910Sstevel@tonic-gate 		return (DDI_EINVAL);
3920Sstevel@tonic-gate 
3930Sstevel@tonic-gate 	rw_enter(&hdlp->ih_rwlock, RW_WRITER);
3941725Segillett 	if (((hdlp->ih_flags & DDI_INTR_MSIX_DUP) &&
3951725Segillett 	    (hdlp->ih_state != DDI_IHDL_STATE_ADDED)) ||
3961725Segillett 	    ((hdlp->ih_state != DDI_IHDL_STATE_ALLOC) &&
3971725Segillett 	    (!(hdlp->ih_flags & DDI_INTR_MSIX_DUP)))) {
3980Sstevel@tonic-gate 		rw_exit(&hdlp->ih_rwlock);
3990Sstevel@tonic-gate 		return (DDI_EINVAL);
4000Sstevel@tonic-gate 	}
4010Sstevel@tonic-gate 
4022588Segillett 	/* Set the number of interrupts to free */
4032588Segillett 	hdlp->ih_scratch1 = 1;
4042588Segillett 
405693Sgovinda 	ret = i_ddi_intr_ops(hdlp->ih_dip, hdlp->ih_dip,
4060Sstevel@tonic-gate 	    DDI_INTROP_FREE, hdlp, NULL);
4070Sstevel@tonic-gate 
4080Sstevel@tonic-gate 	rw_exit(&hdlp->ih_rwlock);
4090Sstevel@tonic-gate 	if (ret == DDI_SUCCESS) {
4101725Segillett 		/* This would be the dup vector */
4111725Segillett 		if (hdlp->ih_flags & DDI_INTR_MSIX_DUP)
4121725Segillett 			atomic_dec_32(&hdlp->ih_main->ih_dup_cnt);
4131725Segillett 		else {
4141725Segillett 			i_ddi_intr_set_current_nintrs(hdlp->ih_dip,
4151725Segillett 			    i_ddi_intr_get_current_nintrs(hdlp->ih_dip) - 1);
4160Sstevel@tonic-gate 
4172588Segillett 			if (hdlp->ih_type & DDI_INTR_TYPE_FIXED)
4182588Segillett 				i_ddi_set_intr_handle(hdlp->ih_dip,
4192588Segillett 				    hdlp->ih_inum, NULL);
4201725Segillett 
4212588Segillett 			i_ddi_intr_devi_fini(hdlp->ih_dip);
4221725Segillett 			i_ddi_free_intr_phdl(hdlp);
423693Sgovinda 		}
4240Sstevel@tonic-gate 		rw_destroy(&hdlp->ih_rwlock);
4250Sstevel@tonic-gate 		kmem_free(hdlp, sizeof (ddi_intr_handle_impl_t));
4260Sstevel@tonic-gate 	}
4270Sstevel@tonic-gate 
4280Sstevel@tonic-gate 	return (ret);
4290Sstevel@tonic-gate }
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate /*
4320Sstevel@tonic-gate  * Interrupt get/set capacity functions
4330Sstevel@tonic-gate  *
4340Sstevel@tonic-gate  * The logic used to figure this out is shown here:
4350Sstevel@tonic-gate  *
4360Sstevel@tonic-gate  *			Device level		Platform level	    Intr source
4370Sstevel@tonic-gate  * 1. Fixed interrupts
4380Sstevel@tonic-gate  * (non-PCI)
4390Sstevel@tonic-gate  * o Flags supported	N/A			Maskable/Pending/    rootnex
4400Sstevel@tonic-gate  *						No Block Enable
4410Sstevel@tonic-gate  * o navail					1
4420Sstevel@tonic-gate  *
4430Sstevel@tonic-gate  * 2. PCI Fixed interrupts
4440Sstevel@tonic-gate  * o Flags supported	pending/Maskable	Maskable/pending/    pci
4450Sstevel@tonic-gate  *						No Block enable
4460Sstevel@tonic-gate  * o navail		N/A			1
4470Sstevel@tonic-gate  *
4480Sstevel@tonic-gate  * 3. PCI MSI
4490Sstevel@tonic-gate  * o Flags supported	Maskable/Pending	Maskable/Pending    pci
4500Sstevel@tonic-gate  *			Block Enable		(if drvr doesn't)   Block Enable
4510Sstevel@tonic-gate  * o navail		N/A			#vectors - #used    N/A
4520Sstevel@tonic-gate  *
4530Sstevel@tonic-gate  * 4. PCI MSI-X
4540Sstevel@tonic-gate  * o Flags supported	Maskable/Pending	Maskable/Pending    pci
4550Sstevel@tonic-gate  *			Block Enable				    Block Enable
4560Sstevel@tonic-gate  * o navail		N/A			#vectors - #used    N/A
4570Sstevel@tonic-gate  *
4580Sstevel@tonic-gate  * where:
4590Sstevel@tonic-gate  *	#vectors	- Total numbers of vectors available
4600Sstevel@tonic-gate  *	#used		- Total numbers of vectors currently being used
4610Sstevel@tonic-gate  *
4620Sstevel@tonic-gate  * For devices complying to PCI2.3 or greater, see bit10 of Command Register
4630Sstevel@tonic-gate  * 0 - enables assertion of INTx
4640Sstevel@tonic-gate  * 1 - disables assertion of INTx
4650Sstevel@tonic-gate  *
4660Sstevel@tonic-gate  * For non MSI/X interrupts; if the IRQ is shared then all ddi_intr_set_*()
4670Sstevel@tonic-gate  * operations return failure.
4680Sstevel@tonic-gate  */
4690Sstevel@tonic-gate int
4700Sstevel@tonic-gate ddi_intr_get_cap(ddi_intr_handle_t h, int *flagsp)
4710Sstevel@tonic-gate {
4720Sstevel@tonic-gate 	ddi_intr_handle_impl_t	*hdlp = (ddi_intr_handle_impl_t *)h;
4730Sstevel@tonic-gate 	int			ret;
4740Sstevel@tonic-gate 
4750Sstevel@tonic-gate 	DDI_INTR_APIDBG((CE_CONT, "ddi_intr_get_cap: hdlp = %p\n",
4760Sstevel@tonic-gate 	    (void *)hdlp));
4770Sstevel@tonic-gate 
4780Sstevel@tonic-gate 	*flagsp = 0;
4790Sstevel@tonic-gate 	if (hdlp == NULL)
4800Sstevel@tonic-gate 		return (DDI_EINVAL);
4810Sstevel@tonic-gate 
4820Sstevel@tonic-gate 	rw_enter(&hdlp->ih_rwlock, RW_READER);
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate 	if (hdlp->ih_cap) {
4850Sstevel@tonic-gate 		*flagsp = hdlp->ih_cap & ~DDI_INTR_FLAG_MSI64;
4860Sstevel@tonic-gate 		rw_exit(&hdlp->ih_rwlock);
4870Sstevel@tonic-gate 		return (DDI_SUCCESS);
4880Sstevel@tonic-gate 	}
4890Sstevel@tonic-gate 
490693Sgovinda 	ret = i_ddi_intr_ops(hdlp->ih_dip, hdlp->ih_dip,
4910Sstevel@tonic-gate 	    DDI_INTROP_GETCAP, hdlp, (void *)flagsp);
4920Sstevel@tonic-gate 
4930Sstevel@tonic-gate 	if (ret == DDI_SUCCESS) {
4940Sstevel@tonic-gate 		hdlp->ih_cap = *flagsp;
4950Sstevel@tonic-gate 
4960Sstevel@tonic-gate 		/* Mask out MSI/X 64-bit support to the consumer */
4970Sstevel@tonic-gate 		*flagsp &= ~DDI_INTR_FLAG_MSI64;
4980Sstevel@tonic-gate 	}
4990Sstevel@tonic-gate 
5000Sstevel@tonic-gate 	rw_exit(&hdlp->ih_rwlock);
5010Sstevel@tonic-gate 	return (ret);
5020Sstevel@tonic-gate }
5030Sstevel@tonic-gate 
5040Sstevel@tonic-gate int
5050Sstevel@tonic-gate ddi_intr_set_cap(ddi_intr_handle_t h, int flags)
5060Sstevel@tonic-gate {
5070Sstevel@tonic-gate 	ddi_intr_handle_impl_t	*hdlp = (ddi_intr_handle_impl_t *)h;
5080Sstevel@tonic-gate 	int			ret;
5090Sstevel@tonic-gate 
5100Sstevel@tonic-gate 	DDI_INTR_APIDBG((CE_CONT, "ddi_intr_set_cap: hdlp = %p", (void *)hdlp));
5110Sstevel@tonic-gate 
5120Sstevel@tonic-gate 	if (hdlp == NULL)
5130Sstevel@tonic-gate 		return (DDI_EINVAL);
5140Sstevel@tonic-gate 
5150Sstevel@tonic-gate 	rw_enter(&hdlp->ih_rwlock, RW_WRITER);
5160Sstevel@tonic-gate 	if (hdlp->ih_state != DDI_IHDL_STATE_ALLOC) {
5170Sstevel@tonic-gate 		rw_exit(&hdlp->ih_rwlock);
5180Sstevel@tonic-gate 		return (DDI_EINVAL);
5190Sstevel@tonic-gate 	}
5200Sstevel@tonic-gate 
5210Sstevel@tonic-gate 	/* Only DDI_INTR_FLAG_LEVEL or DDI_INTR_FLAG_EDGE are allowed */
5220Sstevel@tonic-gate 	if (!(flags & (DDI_INTR_FLAG_EDGE | DDI_INTR_FLAG_LEVEL))) {
5230Sstevel@tonic-gate 		DDI_INTR_APIDBG((CE_CONT, "%s%d: only LEVEL or EDGE capability "
5240Sstevel@tonic-gate 		    "can be set\n", ddi_driver_name(hdlp->ih_dip),
5250Sstevel@tonic-gate 		    ddi_get_instance(hdlp->ih_dip)));
5260Sstevel@tonic-gate 		rw_exit(&hdlp->ih_rwlock);
5270Sstevel@tonic-gate 		return (DDI_EINVAL);
5280Sstevel@tonic-gate 	}
5290Sstevel@tonic-gate 
5300Sstevel@tonic-gate 	/* Both level/edge flags must be currently supported */
5310Sstevel@tonic-gate 	if (!(hdlp->ih_cap & (DDI_INTR_FLAG_EDGE | DDI_INTR_FLAG_LEVEL))) {
5320Sstevel@tonic-gate 		DDI_INTR_APIDBG((CE_CONT, "%s%d: Both LEVEL and EDGE capability"
5330Sstevel@tonic-gate 		    " must be supported\n", ddi_driver_name(hdlp->ih_dip),
5340Sstevel@tonic-gate 		    ddi_get_instance(hdlp->ih_dip)));
5350Sstevel@tonic-gate 		rw_exit(&hdlp->ih_rwlock);
5360Sstevel@tonic-gate 		return (DDI_ENOTSUP);
5370Sstevel@tonic-gate 	}
5380Sstevel@tonic-gate 
539693Sgovinda 	ret = i_ddi_intr_ops(hdlp->ih_dip, hdlp->ih_dip,
5400Sstevel@tonic-gate 	    DDI_INTROP_SETCAP, hdlp, &flags);
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate 	rw_exit(&hdlp->ih_rwlock);
5430Sstevel@tonic-gate 	return (ret);
5440Sstevel@tonic-gate }
5450Sstevel@tonic-gate 
5460Sstevel@tonic-gate /*
5470Sstevel@tonic-gate  * Priority related functions
5480Sstevel@tonic-gate  */
5490Sstevel@tonic-gate 
5500Sstevel@tonic-gate /*
5510Sstevel@tonic-gate  * ddi_intr_get_hilevel_pri:
5520Sstevel@tonic-gate  *	Returns the minimum priority level for a
5530Sstevel@tonic-gate  *	high-level interrupt on a platform.
5540Sstevel@tonic-gate  */
5550Sstevel@tonic-gate uint_t
5560Sstevel@tonic-gate ddi_intr_get_hilevel_pri(void)
5570Sstevel@tonic-gate {
5580Sstevel@tonic-gate 	DDI_INTR_APIDBG((CE_CONT, "ddi_intr_get_hilevel_pri:\n"));
5590Sstevel@tonic-gate 	return (LOCK_LEVEL + 1);
5600Sstevel@tonic-gate }
5610Sstevel@tonic-gate 
5620Sstevel@tonic-gate int
5630Sstevel@tonic-gate ddi_intr_get_pri(ddi_intr_handle_t h, uint_t *prip)
5640Sstevel@tonic-gate {
5650Sstevel@tonic-gate 	ddi_intr_handle_impl_t	*hdlp = (ddi_intr_handle_impl_t *)h;
5660Sstevel@tonic-gate 	int			ret;
5670Sstevel@tonic-gate 
5680Sstevel@tonic-gate 	DDI_INTR_APIDBG((CE_CONT, "ddi_intr_get_pri: hdlp = %p\n",
5690Sstevel@tonic-gate 	    (void *)hdlp));
5700Sstevel@tonic-gate 
5710Sstevel@tonic-gate 	*prip = 0;
5720Sstevel@tonic-gate 	if (hdlp == NULL)
5730Sstevel@tonic-gate 		return (DDI_EINVAL);
5740Sstevel@tonic-gate 
5750Sstevel@tonic-gate 	rw_enter(&hdlp->ih_rwlock, RW_READER);
5760Sstevel@tonic-gate 	/* Already initialized, just return that */
5770Sstevel@tonic-gate 	if (hdlp->ih_pri) {
5780Sstevel@tonic-gate 		*prip = hdlp->ih_pri;
5790Sstevel@tonic-gate 		rw_exit(&hdlp->ih_rwlock);
5800Sstevel@tonic-gate 		return (DDI_SUCCESS);
5810Sstevel@tonic-gate 	}
5820Sstevel@tonic-gate 
583693Sgovinda 	ret = i_ddi_intr_ops(hdlp->ih_dip, hdlp->ih_dip,
5840Sstevel@tonic-gate 	    DDI_INTROP_GETPRI, hdlp, (void *)prip);
5850Sstevel@tonic-gate 
5860Sstevel@tonic-gate 	if (ret == DDI_SUCCESS)
5870Sstevel@tonic-gate 		hdlp->ih_pri = *prip;
5880Sstevel@tonic-gate 
5890Sstevel@tonic-gate 	rw_exit(&hdlp->ih_rwlock);
5900Sstevel@tonic-gate 	return (ret);
5910Sstevel@tonic-gate }
5920Sstevel@tonic-gate 
5930Sstevel@tonic-gate int
5940Sstevel@tonic-gate ddi_intr_set_pri(ddi_intr_handle_t h, uint_t pri)
5950Sstevel@tonic-gate {
5960Sstevel@tonic-gate 	ddi_intr_handle_impl_t	*hdlp = (ddi_intr_handle_impl_t *)h;
5970Sstevel@tonic-gate 	int			ret;
5980Sstevel@tonic-gate 
5990Sstevel@tonic-gate 	DDI_INTR_APIDBG((CE_CONT, "ddi_intr_set_pri: hdlp = %p", (void *)hdlp));
6000Sstevel@tonic-gate 
6010Sstevel@tonic-gate 	if (hdlp == NULL)
6020Sstevel@tonic-gate 		return (DDI_EINVAL);
6030Sstevel@tonic-gate 
6040Sstevel@tonic-gate 	/* Validate priority argument */
6050Sstevel@tonic-gate 	if (pri < DDI_INTR_PRI_MIN || pri > DDI_INTR_PRI_MAX) {
6060Sstevel@tonic-gate 		DDI_INTR_APIDBG((CE_CONT, "ddi_intr_set_pri: invalid priority "
6070Sstevel@tonic-gate 		    "specified  = %x\n", pri));
6080Sstevel@tonic-gate 		return (DDI_EINVAL);
6090Sstevel@tonic-gate 	}
6100Sstevel@tonic-gate 
6110Sstevel@tonic-gate 	rw_enter(&hdlp->ih_rwlock, RW_WRITER);
6120Sstevel@tonic-gate 	if (hdlp->ih_state != DDI_IHDL_STATE_ALLOC) {
6130Sstevel@tonic-gate 		rw_exit(&hdlp->ih_rwlock);
6140Sstevel@tonic-gate 		return (DDI_EINVAL);
6150Sstevel@tonic-gate 	}
6160Sstevel@tonic-gate 
6170Sstevel@tonic-gate 	/* If the passed priority is same as existing priority; do nothing */
6180Sstevel@tonic-gate 	if (pri == hdlp->ih_pri) {
6190Sstevel@tonic-gate 		rw_exit(&hdlp->ih_rwlock);
6200Sstevel@tonic-gate 		return (DDI_SUCCESS);
6210Sstevel@tonic-gate 	}
6220Sstevel@tonic-gate 
623693Sgovinda 	ret = i_ddi_intr_ops(hdlp->ih_dip, hdlp->ih_dip,
6240Sstevel@tonic-gate 	    DDI_INTROP_SETPRI, hdlp, &pri);
6250Sstevel@tonic-gate 
6260Sstevel@tonic-gate 	if (ret == DDI_SUCCESS)
6270Sstevel@tonic-gate 		hdlp->ih_pri = pri;
6280Sstevel@tonic-gate 
6290Sstevel@tonic-gate 	rw_exit(&hdlp->ih_rwlock);
6300Sstevel@tonic-gate 	return (ret);
6310Sstevel@tonic-gate }
6320Sstevel@tonic-gate 
6330Sstevel@tonic-gate /*
6340Sstevel@tonic-gate  * Interrupt add/duplicate/remove handlers
6350Sstevel@tonic-gate  */
6360Sstevel@tonic-gate int
6370Sstevel@tonic-gate ddi_intr_add_handler(ddi_intr_handle_t h, ddi_intr_handler_t inthandler,
6380Sstevel@tonic-gate     void *arg1, void *arg2)
6390Sstevel@tonic-gate {
6400Sstevel@tonic-gate 	ddi_intr_handle_impl_t	*hdlp = (ddi_intr_handle_impl_t *)h;
6410Sstevel@tonic-gate 	int			ret;
6420Sstevel@tonic-gate 
6430Sstevel@tonic-gate 	DDI_INTR_APIDBG((CE_CONT, "ddi_intr_add_handler: hdlp = 0x%p\n",
6440Sstevel@tonic-gate 	    (void *)hdlp));
6450Sstevel@tonic-gate 
6460Sstevel@tonic-gate 	if ((hdlp == NULL) || (inthandler == NULL))
6470Sstevel@tonic-gate 		return (DDI_EINVAL);
6480Sstevel@tonic-gate 
6490Sstevel@tonic-gate 	rw_enter(&hdlp->ih_rwlock, RW_WRITER);
6500Sstevel@tonic-gate 	if (hdlp->ih_state != DDI_IHDL_STATE_ALLOC) {
6510Sstevel@tonic-gate 		rw_exit(&hdlp->ih_rwlock);
6520Sstevel@tonic-gate 		return (DDI_EINVAL);
6530Sstevel@tonic-gate 	}
6540Sstevel@tonic-gate 
6550Sstevel@tonic-gate 	hdlp->ih_cb_func = inthandler;
6560Sstevel@tonic-gate 	hdlp->ih_cb_arg1 = arg1;
6570Sstevel@tonic-gate 	hdlp->ih_cb_arg2 = arg2;
6580Sstevel@tonic-gate 
659693Sgovinda 	ret = i_ddi_intr_ops(hdlp->ih_dip, hdlp->ih_dip,
6600Sstevel@tonic-gate 	    DDI_INTROP_ADDISR, hdlp, NULL);
6610Sstevel@tonic-gate 
6620Sstevel@tonic-gate 	if (ret != DDI_SUCCESS) {
6630Sstevel@tonic-gate 		hdlp->ih_cb_func = NULL;
6640Sstevel@tonic-gate 		hdlp->ih_cb_arg1 = NULL;
6650Sstevel@tonic-gate 		hdlp->ih_cb_arg2 = NULL;
6660Sstevel@tonic-gate 	} else
6670Sstevel@tonic-gate 		hdlp->ih_state = DDI_IHDL_STATE_ADDED;
6680Sstevel@tonic-gate 
6690Sstevel@tonic-gate 	rw_exit(&hdlp->ih_rwlock);
6700Sstevel@tonic-gate 	return (ret);
6710Sstevel@tonic-gate }
6720Sstevel@tonic-gate 
6730Sstevel@tonic-gate int
6741725Segillett ddi_intr_dup_handler(ddi_intr_handle_t org, int dup_inum,
6751725Segillett     ddi_intr_handle_t *dup)
6760Sstevel@tonic-gate {
6770Sstevel@tonic-gate 	ddi_intr_handle_impl_t	*hdlp = (ddi_intr_handle_impl_t *)org;
6780Sstevel@tonic-gate 	ddi_intr_handle_impl_t	*dup_hdlp;
6790Sstevel@tonic-gate 	int			ret;
6800Sstevel@tonic-gate 
6810Sstevel@tonic-gate 	DDI_INTR_APIDBG((CE_CONT, "ddi_intr_dup_handler: hdlp = 0x%p\n",
6820Sstevel@tonic-gate 	    (void *)hdlp));
6830Sstevel@tonic-gate 
6841725Segillett 	/* Do some input argument checking ("dup" handle is not allocated) */
6852433Sanish 	if ((hdlp == NULL) || (*dup != NULL) || (dup_inum < 0)) {
6862433Sanish 		DDI_INTR_APIDBG((CE_CONT, "ddi_intr_dup_handler: Invalid "
6872433Sanish 		    "input args\n"));
6880Sstevel@tonic-gate 		return (DDI_EINVAL);
6892433Sanish 	}
6900Sstevel@tonic-gate 
6910Sstevel@tonic-gate 	rw_enter(&hdlp->ih_rwlock, RW_READER);
6920Sstevel@tonic-gate 
6930Sstevel@tonic-gate 	/* Do some input argument checking */
6940Sstevel@tonic-gate 	if ((hdlp->ih_state == DDI_IHDL_STATE_ALLOC) ||	/* intr handle alloc? */
6951725Segillett 	    (hdlp->ih_type != DDI_INTR_TYPE_MSIX) ||	/* only MSI-X allowed */
6961725Segillett 	    (hdlp->ih_flags & DDI_INTR_MSIX_DUP)) {	/* only dup original */
6970Sstevel@tonic-gate 		rw_exit(&hdlp->ih_rwlock);
6980Sstevel@tonic-gate 		return (DDI_EINVAL);
6990Sstevel@tonic-gate 	}
7000Sstevel@tonic-gate 
7011725Segillett 	hdlp->ih_scratch1 = dup_inum;
702693Sgovinda 	ret = i_ddi_intr_ops(hdlp->ih_dip, hdlp->ih_dip,
7031725Segillett 	    DDI_INTROP_DUPVEC, hdlp, NULL);
7040Sstevel@tonic-gate 
7050Sstevel@tonic-gate 	if (ret == DDI_SUCCESS) {
7060Sstevel@tonic-gate 		dup_hdlp = (ddi_intr_handle_impl_t *)
7071725Segillett 		    kmem_alloc(sizeof (ddi_intr_handle_impl_t), KM_SLEEP);
7081725Segillett 
7091725Segillett 		atomic_add_32(&hdlp->ih_dup_cnt, 1);
7100Sstevel@tonic-gate 
7111725Segillett 		*dup = (ddi_intr_handle_t)dup_hdlp;
7121725Segillett 		bcopy(hdlp, dup_hdlp, sizeof (ddi_intr_handle_impl_t));
7131725Segillett 
7141725Segillett 		/* These fields are unique to each dupped msi-x vector */
7150Sstevel@tonic-gate 		rw_init(&dup_hdlp->ih_rwlock, NULL, RW_DRIVER, NULL);
7160Sstevel@tonic-gate 		dup_hdlp->ih_state = DDI_IHDL_STATE_ADDED;
7171725Segillett 		dup_hdlp->ih_inum = dup_inum;
7181725Segillett 		dup_hdlp->ih_flags |= DDI_INTR_MSIX_DUP;
7191725Segillett 		dup_hdlp->ih_dup_cnt = 0;
7200Sstevel@tonic-gate 
7211725Segillett 		/* Point back to original vector */
7221725Segillett 		dup_hdlp->ih_main = hdlp;
7230Sstevel@tonic-gate 	}
7240Sstevel@tonic-gate 
7250Sstevel@tonic-gate 	rw_exit(&hdlp->ih_rwlock);
7260Sstevel@tonic-gate 	return (ret);
7270Sstevel@tonic-gate }
7280Sstevel@tonic-gate 
7290Sstevel@tonic-gate int
7300Sstevel@tonic-gate ddi_intr_remove_handler(ddi_intr_handle_t h)
7310Sstevel@tonic-gate {
7320Sstevel@tonic-gate 	ddi_intr_handle_impl_t	*hdlp = (ddi_intr_handle_impl_t *)h;
7331725Segillett 	int			ret = DDI_SUCCESS;
7340Sstevel@tonic-gate 
7350Sstevel@tonic-gate 	DDI_INTR_APIDBG((CE_CONT, "ddi_intr_remove_handler: hdlp = %p\n",
7360Sstevel@tonic-gate 	    (void *)hdlp));
7370Sstevel@tonic-gate 
7380Sstevel@tonic-gate 	if (hdlp == NULL)
7390Sstevel@tonic-gate 		return (DDI_EINVAL);
7400Sstevel@tonic-gate 
7410Sstevel@tonic-gate 	rw_enter(&hdlp->ih_rwlock, RW_WRITER);
7421725Segillett 
7430Sstevel@tonic-gate 	if (hdlp->ih_state != DDI_IHDL_STATE_ADDED) {
7441725Segillett 		ret = DDI_EINVAL;
7451725Segillett 		goto done;
7461725Segillett 	} else if (hdlp->ih_flags & DDI_INTR_MSIX_DUP)
7471725Segillett 		goto done;
7481725Segillett 
7491725Segillett 	ASSERT(hdlp->ih_dup_cnt == 0);
7501725Segillett 	if (hdlp->ih_dup_cnt > 0) {
7511725Segillett 		DDI_INTR_APIDBG((CE_CONT, "ddi_intr_remove_handler: MSI-X "
7521725Segillett 		    "dup_cnt %d is not 0\n", hdlp->ih_dup_cnt));
7531725Segillett 		ret = DDI_FAILURE;
7541725Segillett 		goto done;
7550Sstevel@tonic-gate 	}
7560Sstevel@tonic-gate 
757693Sgovinda 	ret = i_ddi_intr_ops(hdlp->ih_dip, hdlp->ih_dip,
7580Sstevel@tonic-gate 	    DDI_INTROP_REMISR, hdlp, NULL);
7590Sstevel@tonic-gate 
7600Sstevel@tonic-gate 	if (ret == DDI_SUCCESS) {
7610Sstevel@tonic-gate 		hdlp->ih_state = DDI_IHDL_STATE_ALLOC;
7620Sstevel@tonic-gate 		hdlp->ih_cb_func = NULL;
7630Sstevel@tonic-gate 		hdlp->ih_cb_arg1 = NULL;
7640Sstevel@tonic-gate 		hdlp->ih_cb_arg2 = NULL;
7650Sstevel@tonic-gate 	}
7660Sstevel@tonic-gate 
7671725Segillett done:
7680Sstevel@tonic-gate 	rw_exit(&hdlp->ih_rwlock);
7690Sstevel@tonic-gate 	return (ret);
7700Sstevel@tonic-gate }
7710Sstevel@tonic-gate 
7720Sstevel@tonic-gate /*
7730Sstevel@tonic-gate  * Interrupt enable/disable/block_enable/block_disable handlers
7740Sstevel@tonic-gate  */
7750Sstevel@tonic-gate int
7760Sstevel@tonic-gate ddi_intr_enable(ddi_intr_handle_t h)
7770Sstevel@tonic-gate {
7780Sstevel@tonic-gate 	ddi_intr_handle_impl_t	*hdlp = (ddi_intr_handle_impl_t *)h;
7790Sstevel@tonic-gate 	int			ret;
7800Sstevel@tonic-gate 
7810Sstevel@tonic-gate 	DDI_INTR_APIDBG((CE_CONT, "ddi_intr_enable: hdlp = %p\n",
7820Sstevel@tonic-gate 	    (void *)hdlp));
7830Sstevel@tonic-gate 
7840Sstevel@tonic-gate 	if (hdlp == NULL)
7850Sstevel@tonic-gate 		return (DDI_EINVAL);
7860Sstevel@tonic-gate 
7870Sstevel@tonic-gate 	rw_enter(&hdlp->ih_rwlock, RW_WRITER);
7880Sstevel@tonic-gate 	if ((hdlp->ih_state != DDI_IHDL_STATE_ADDED) ||
7890Sstevel@tonic-gate 	    ((hdlp->ih_type == DDI_INTR_TYPE_MSI) &&
7900Sstevel@tonic-gate 	    (hdlp->ih_cap & DDI_INTR_FLAG_BLOCK))) {
7910Sstevel@tonic-gate 		rw_exit(&hdlp->ih_rwlock);
7920Sstevel@tonic-gate 		return (DDI_EINVAL);
7930Sstevel@tonic-gate 	}
7940Sstevel@tonic-gate 
7951725Segillett 	I_DDI_VERIFY_MSIX_HANDLE(hdlp);
7961725Segillett 
797693Sgovinda 	ret = i_ddi_intr_ops(hdlp->ih_dip, hdlp->ih_dip,
7980Sstevel@tonic-gate 	    DDI_INTROP_ENABLE, hdlp, NULL);
7990Sstevel@tonic-gate 
8000Sstevel@tonic-gate 	if (ret == DDI_SUCCESS)
8010Sstevel@tonic-gate 		hdlp->ih_state = DDI_IHDL_STATE_ENABLE;
8020Sstevel@tonic-gate 
8030Sstevel@tonic-gate 	rw_exit(&hdlp->ih_rwlock);
8040Sstevel@tonic-gate 	return (ret);
8050Sstevel@tonic-gate }
8060Sstevel@tonic-gate 
8070Sstevel@tonic-gate int
8080Sstevel@tonic-gate ddi_intr_disable(ddi_intr_handle_t h)
8090Sstevel@tonic-gate {
8100Sstevel@tonic-gate 	ddi_intr_handle_impl_t	*hdlp = (ddi_intr_handle_impl_t *)h;
8110Sstevel@tonic-gate 	int			ret;
8120Sstevel@tonic-gate 
8130Sstevel@tonic-gate 	DDI_INTR_APIDBG((CE_CONT, "ddi_intr_disable: hdlp = %p\n",
8140Sstevel@tonic-gate 	    (void *)hdlp));
8150Sstevel@tonic-gate 
8160Sstevel@tonic-gate 	if (hdlp == NULL)
8170Sstevel@tonic-gate 		return (DDI_EINVAL);
8180Sstevel@tonic-gate 
8190Sstevel@tonic-gate 	rw_enter(&hdlp->ih_rwlock, RW_WRITER);
8200Sstevel@tonic-gate 	if ((hdlp->ih_state != DDI_IHDL_STATE_ENABLE) ||
8210Sstevel@tonic-gate 	    ((hdlp->ih_type == DDI_INTR_TYPE_MSI) &&
8220Sstevel@tonic-gate 	    (hdlp->ih_cap & DDI_INTR_FLAG_BLOCK))) {
8230Sstevel@tonic-gate 		rw_exit(&hdlp->ih_rwlock);
8240Sstevel@tonic-gate 		return (DDI_EINVAL);
8250Sstevel@tonic-gate 	}
8260Sstevel@tonic-gate 
8271725Segillett 	I_DDI_VERIFY_MSIX_HANDLE(hdlp);
8281725Segillett 
829693Sgovinda 	ret = i_ddi_intr_ops(hdlp->ih_dip, hdlp->ih_dip,
8300Sstevel@tonic-gate 	    DDI_INTROP_DISABLE, hdlp, NULL);
8310Sstevel@tonic-gate 
8320Sstevel@tonic-gate 	if (ret == DDI_SUCCESS)
8330Sstevel@tonic-gate 		hdlp->ih_state = DDI_IHDL_STATE_ADDED;
8340Sstevel@tonic-gate 
8350Sstevel@tonic-gate 	rw_exit(&hdlp->ih_rwlock);
8360Sstevel@tonic-gate 	return (ret);
8370Sstevel@tonic-gate }
8380Sstevel@tonic-gate 
8390Sstevel@tonic-gate int
8400Sstevel@tonic-gate ddi_intr_block_enable(ddi_intr_handle_t *h_array, int count)
8410Sstevel@tonic-gate {
8420Sstevel@tonic-gate 	ddi_intr_handle_impl_t	*hdlp;
8430Sstevel@tonic-gate 	int			i, ret;
8440Sstevel@tonic-gate 
8450Sstevel@tonic-gate 	DDI_INTR_APIDBG((CE_CONT, "ddi_intr_block_enable: h_array = %p\n",
8460Sstevel@tonic-gate 	    (void *)h_array));
8470Sstevel@tonic-gate 
8480Sstevel@tonic-gate 	if (h_array == NULL)
8490Sstevel@tonic-gate 		return (DDI_EINVAL);
8500Sstevel@tonic-gate 
8510Sstevel@tonic-gate 	for (i = 0; i < count; i++) {
8520Sstevel@tonic-gate 		hdlp = (ddi_intr_handle_impl_t *)h_array[i];
8530Sstevel@tonic-gate 		rw_enter(&hdlp->ih_rwlock, RW_READER);
8540Sstevel@tonic-gate 
8550Sstevel@tonic-gate 		if (hdlp->ih_state != DDI_IHDL_STATE_ADDED ||
8560Sstevel@tonic-gate 		    hdlp->ih_type != DDI_INTR_TYPE_MSI ||
8570Sstevel@tonic-gate 		    !(hdlp->ih_cap & DDI_INTR_FLAG_BLOCK)) {
8580Sstevel@tonic-gate 			rw_exit(&hdlp->ih_rwlock);
8590Sstevel@tonic-gate 			return (DDI_EINVAL);
8600Sstevel@tonic-gate 		}
8610Sstevel@tonic-gate 		rw_exit(&hdlp->ih_rwlock);
8620Sstevel@tonic-gate 	}
8630Sstevel@tonic-gate 
8640Sstevel@tonic-gate 	hdlp = (ddi_intr_handle_impl_t *)h_array[0];
8650Sstevel@tonic-gate 	rw_enter(&hdlp->ih_rwlock, RW_WRITER);
8660Sstevel@tonic-gate 	hdlp->ih_scratch1 = count;
8671542Sjohnny 	hdlp->ih_scratch2 = (void *)h_array;
8680Sstevel@tonic-gate 
869693Sgovinda 	ret = i_ddi_intr_ops(hdlp->ih_dip, hdlp->ih_dip,
8700Sstevel@tonic-gate 	    DDI_INTROP_BLOCKENABLE, hdlp, NULL);
8710Sstevel@tonic-gate 
8720Sstevel@tonic-gate 	rw_exit(&hdlp->ih_rwlock);
8730Sstevel@tonic-gate 
8740Sstevel@tonic-gate 	if (ret == DDI_SUCCESS) {
8750Sstevel@tonic-gate 		for (i = 0; i < count; i++) {
8760Sstevel@tonic-gate 			hdlp = (ddi_intr_handle_impl_t *)h_array[i];
8770Sstevel@tonic-gate 			rw_enter(&hdlp->ih_rwlock, RW_WRITER);
8780Sstevel@tonic-gate 			hdlp->ih_state = DDI_IHDL_STATE_ENABLE;
8790Sstevel@tonic-gate 			rw_exit(&hdlp->ih_rwlock);
8800Sstevel@tonic-gate 		}
8810Sstevel@tonic-gate 	}
8820Sstevel@tonic-gate 
8830Sstevel@tonic-gate 	return (ret);
8840Sstevel@tonic-gate }
8850Sstevel@tonic-gate 
8860Sstevel@tonic-gate int
8870Sstevel@tonic-gate ddi_intr_block_disable(ddi_intr_handle_t *h_array, int count)
8880Sstevel@tonic-gate {
8890Sstevel@tonic-gate 	ddi_intr_handle_impl_t	*hdlp;
8900Sstevel@tonic-gate 	int			i, ret;
8910Sstevel@tonic-gate 
8920Sstevel@tonic-gate 	DDI_INTR_APIDBG((CE_CONT, "ddi_intr_block_disable: h_array = %p\n",
8930Sstevel@tonic-gate 	    (void *)h_array));
8940Sstevel@tonic-gate 
8950Sstevel@tonic-gate 	if (h_array == NULL)
8960Sstevel@tonic-gate 		return (DDI_EINVAL);
8970Sstevel@tonic-gate 
8980Sstevel@tonic-gate 	for (i = 0; i < count; i++) {
8990Sstevel@tonic-gate 		hdlp = (ddi_intr_handle_impl_t *)h_array[i];
9000Sstevel@tonic-gate 		rw_enter(&hdlp->ih_rwlock, RW_READER);
9010Sstevel@tonic-gate 		if (hdlp->ih_state != DDI_IHDL_STATE_ENABLE ||
9020Sstevel@tonic-gate 		    hdlp->ih_type != DDI_INTR_TYPE_MSI ||
9030Sstevel@tonic-gate 		    !(hdlp->ih_cap & DDI_INTR_FLAG_BLOCK)) {
9040Sstevel@tonic-gate 			rw_exit(&hdlp->ih_rwlock);
9050Sstevel@tonic-gate 			return (DDI_EINVAL);
9060Sstevel@tonic-gate 		}
9070Sstevel@tonic-gate 		rw_exit(&hdlp->ih_rwlock);
9080Sstevel@tonic-gate 	}
9090Sstevel@tonic-gate 
9100Sstevel@tonic-gate 	hdlp = (ddi_intr_handle_impl_t *)h_array[0];
9110Sstevel@tonic-gate 	rw_enter(&hdlp->ih_rwlock, RW_WRITER);
9120Sstevel@tonic-gate 	hdlp->ih_scratch1 = count;
9131542Sjohnny 	hdlp->ih_scratch2 = (void *)h_array;
9140Sstevel@tonic-gate 
915693Sgovinda 	ret = i_ddi_intr_ops(hdlp->ih_dip, hdlp->ih_dip,
9160Sstevel@tonic-gate 	    DDI_INTROP_BLOCKDISABLE, hdlp, NULL);
9170Sstevel@tonic-gate 
9180Sstevel@tonic-gate 	rw_exit(&hdlp->ih_rwlock);
9190Sstevel@tonic-gate 
9200Sstevel@tonic-gate 	if (ret == DDI_SUCCESS) {
9210Sstevel@tonic-gate 		for (i = 0; i < count; i++) {
9220Sstevel@tonic-gate 			hdlp = (ddi_intr_handle_impl_t *)h_array[i];
9230Sstevel@tonic-gate 			rw_enter(&hdlp->ih_rwlock, RW_WRITER);
9240Sstevel@tonic-gate 			hdlp->ih_state = DDI_IHDL_STATE_ADDED;
9250Sstevel@tonic-gate 			rw_exit(&hdlp->ih_rwlock);
9260Sstevel@tonic-gate 		}
9270Sstevel@tonic-gate 	}
9280Sstevel@tonic-gate 
9290Sstevel@tonic-gate 	return (ret);
9300Sstevel@tonic-gate }
9310Sstevel@tonic-gate 
9320Sstevel@tonic-gate /*
9330Sstevel@tonic-gate  * Interrupt set/clr mask handlers
9340Sstevel@tonic-gate  */
9350Sstevel@tonic-gate int
9360Sstevel@tonic-gate ddi_intr_set_mask(ddi_intr_handle_t h)
9370Sstevel@tonic-gate {
9380Sstevel@tonic-gate 	ddi_intr_handle_impl_t	*hdlp = (ddi_intr_handle_impl_t *)h;
9390Sstevel@tonic-gate 	int			ret;
9400Sstevel@tonic-gate 
9410Sstevel@tonic-gate 	DDI_INTR_APIDBG((CE_CONT, "ddi_intr_set_mask: hdlp = %p\n",
9420Sstevel@tonic-gate 	    (void *)hdlp));
9430Sstevel@tonic-gate 
9440Sstevel@tonic-gate 	if (hdlp == NULL)
9450Sstevel@tonic-gate 		return (DDI_EINVAL);
9460Sstevel@tonic-gate 
9470Sstevel@tonic-gate 	rw_enter(&hdlp->ih_rwlock, RW_WRITER);
9481653Sgovinda 	if ((hdlp->ih_state != DDI_IHDL_STATE_ENABLE) ||
9491653Sgovinda 	    (!(hdlp->ih_cap & DDI_INTR_FLAG_MASKABLE))) {
9500Sstevel@tonic-gate 		rw_exit(&hdlp->ih_rwlock);
9510Sstevel@tonic-gate 		return (DDI_EINVAL);
9520Sstevel@tonic-gate 	}
9530Sstevel@tonic-gate 
954693Sgovinda 	ret =  i_ddi_intr_ops(hdlp->ih_dip, hdlp->ih_dip,
9550Sstevel@tonic-gate 	    DDI_INTROP_SETMASK, hdlp, NULL);
9560Sstevel@tonic-gate 
9570Sstevel@tonic-gate 	rw_exit(&hdlp->ih_rwlock);
9580Sstevel@tonic-gate 	return (ret);
9590Sstevel@tonic-gate }
9600Sstevel@tonic-gate 
9610Sstevel@tonic-gate int
9620Sstevel@tonic-gate ddi_intr_clr_mask(ddi_intr_handle_t h)
9630Sstevel@tonic-gate {
9640Sstevel@tonic-gate 	ddi_intr_handle_impl_t	*hdlp = (ddi_intr_handle_impl_t *)h;
9650Sstevel@tonic-gate 	int			ret;
9660Sstevel@tonic-gate 
9670Sstevel@tonic-gate 	DDI_INTR_APIDBG((CE_CONT, "ddi_intr_clr_mask: hdlp = %p\n",
9680Sstevel@tonic-gate 	    (void *)hdlp));
9690Sstevel@tonic-gate 
9700Sstevel@tonic-gate 	if (hdlp == NULL)
9710Sstevel@tonic-gate 		return (DDI_EINVAL);
9720Sstevel@tonic-gate 
9730Sstevel@tonic-gate 	rw_enter(&hdlp->ih_rwlock, RW_WRITER);
9741653Sgovinda 	if ((hdlp->ih_state != DDI_IHDL_STATE_ENABLE) ||
9751653Sgovinda 	    (!(hdlp->ih_cap & DDI_INTR_FLAG_MASKABLE))) {
9760Sstevel@tonic-gate 		rw_exit(&hdlp->ih_rwlock);
9770Sstevel@tonic-gate 		return (DDI_EINVAL);
9780Sstevel@tonic-gate 	}
9790Sstevel@tonic-gate 
980693Sgovinda 	ret = i_ddi_intr_ops(hdlp->ih_dip, hdlp->ih_dip,
9810Sstevel@tonic-gate 	    DDI_INTROP_CLRMASK, hdlp, NULL);
9820Sstevel@tonic-gate 
9830Sstevel@tonic-gate 	rw_exit(&hdlp->ih_rwlock);
9840Sstevel@tonic-gate 	return (ret);
9850Sstevel@tonic-gate }
9860Sstevel@tonic-gate 
9870Sstevel@tonic-gate /*
9880Sstevel@tonic-gate  * Interrupt get_pending handler
9890Sstevel@tonic-gate  */
9900Sstevel@tonic-gate int
9910Sstevel@tonic-gate ddi_intr_get_pending(ddi_intr_handle_t h, int *pendingp)
9920Sstevel@tonic-gate {
9930Sstevel@tonic-gate 	ddi_intr_handle_impl_t	*hdlp = (ddi_intr_handle_impl_t *)h;
9940Sstevel@tonic-gate 	int			ret;
9950Sstevel@tonic-gate 
9960Sstevel@tonic-gate 	DDI_INTR_APIDBG((CE_CONT, "ddi_intr_get_pending: hdlp = %p\n",
9970Sstevel@tonic-gate 	    (void *)hdlp));
9980Sstevel@tonic-gate 
9990Sstevel@tonic-gate 	if (hdlp == NULL)
10000Sstevel@tonic-gate 		return (DDI_EINVAL);
10010Sstevel@tonic-gate 
10020Sstevel@tonic-gate 	rw_enter(&hdlp->ih_rwlock, RW_READER);
10030Sstevel@tonic-gate 	if (!(hdlp->ih_cap & DDI_INTR_FLAG_PENDING)) {
10040Sstevel@tonic-gate 		rw_exit(&hdlp->ih_rwlock);
10050Sstevel@tonic-gate 		return (DDI_EINVAL);
10060Sstevel@tonic-gate 	}
10070Sstevel@tonic-gate 
1008693Sgovinda 	ret = i_ddi_intr_ops(hdlp->ih_dip, hdlp->ih_dip,
10090Sstevel@tonic-gate 	    DDI_INTROP_GETPENDING, hdlp, (void *)pendingp);
10100Sstevel@tonic-gate 
10110Sstevel@tonic-gate 	rw_exit(&hdlp->ih_rwlock);
10120Sstevel@tonic-gate 	return (ret);
10130Sstevel@tonic-gate }
10140Sstevel@tonic-gate 
10150Sstevel@tonic-gate /*
10160Sstevel@tonic-gate  * Soft interrupt handlers
10170Sstevel@tonic-gate  */
10180Sstevel@tonic-gate /*
10190Sstevel@tonic-gate  * Add a soft interrupt and register its handler
10200Sstevel@tonic-gate  */
10210Sstevel@tonic-gate /* ARGSUSED */
10220Sstevel@tonic-gate int
10230Sstevel@tonic-gate ddi_intr_add_softint(dev_info_t *dip, ddi_softint_handle_t *h_p, int soft_pri,
10240Sstevel@tonic-gate     ddi_intr_handler_t handler, void *arg1)
10250Sstevel@tonic-gate {
10260Sstevel@tonic-gate 	ddi_softint_hdl_impl_t	*hdlp;
10270Sstevel@tonic-gate 	int			ret;
10280Sstevel@tonic-gate 
10290Sstevel@tonic-gate 	DDI_INTR_APIDBG((CE_CONT, "ddi_intr_add_softint: dip = %p, "
10300Sstevel@tonic-gate 	    "softpri = 0x%x\n", (void *)dip, soft_pri));
10310Sstevel@tonic-gate 
10320Sstevel@tonic-gate 	if ((dip == NULL) || (h_p == NULL) || (handler == NULL)) {
10330Sstevel@tonic-gate 		DDI_INTR_APIDBG((CE_CONT, "ddi_intr_add_softint: "
10340Sstevel@tonic-gate 		    "invalid arguments"));
10350Sstevel@tonic-gate 
10360Sstevel@tonic-gate 		return (DDI_EINVAL);
10370Sstevel@tonic-gate 	}
10380Sstevel@tonic-gate 
10390Sstevel@tonic-gate 	/* Validate input arguments */
10400Sstevel@tonic-gate 	if (soft_pri < DDI_INTR_SOFTPRI_MIN ||
10410Sstevel@tonic-gate 	    soft_pri > DDI_INTR_SOFTPRI_MAX) {
10420Sstevel@tonic-gate 		DDI_INTR_APIDBG((CE_CONT, "ddi_intr_add_softint: invalid "
10430Sstevel@tonic-gate 		    "soft_pri input given  = %x\n", soft_pri));
10440Sstevel@tonic-gate 		return (DDI_EINVAL);
10450Sstevel@tonic-gate 	}
10460Sstevel@tonic-gate 
10470Sstevel@tonic-gate 	hdlp = (ddi_softint_hdl_impl_t *)kmem_zalloc(
10480Sstevel@tonic-gate 	    sizeof (ddi_softint_hdl_impl_t), KM_SLEEP);
10490Sstevel@tonic-gate 
10500Sstevel@tonic-gate 	/* fill up internally */
10510Sstevel@tonic-gate 	rw_init(&hdlp->ih_rwlock, NULL, RW_DRIVER, NULL);
10520Sstevel@tonic-gate 	rw_enter(&hdlp->ih_rwlock, RW_WRITER);
10530Sstevel@tonic-gate 	hdlp->ih_pri = soft_pri;
10540Sstevel@tonic-gate 	hdlp->ih_dip = dip;
10550Sstevel@tonic-gate 	hdlp->ih_cb_func = handler;
10560Sstevel@tonic-gate 	hdlp->ih_cb_arg1 = arg1;
10570Sstevel@tonic-gate 	DDI_INTR_APIDBG((CE_CONT, "ddi_intr_add_softint: hdlp = %p\n",
10580Sstevel@tonic-gate 	    (void *)hdlp));
10590Sstevel@tonic-gate 
10600Sstevel@tonic-gate 	/* do the platform specific calls */
10610Sstevel@tonic-gate 	if ((ret = i_ddi_add_softint(hdlp)) != DDI_SUCCESS) {
10620Sstevel@tonic-gate 		rw_exit(&hdlp->ih_rwlock);
10630Sstevel@tonic-gate 		rw_destroy(&hdlp->ih_rwlock);
10640Sstevel@tonic-gate 		kmem_free(hdlp, sizeof (ddi_softint_hdl_impl_t));
10650Sstevel@tonic-gate 		return (ret);
10660Sstevel@tonic-gate 	}
10670Sstevel@tonic-gate 
10680Sstevel@tonic-gate 	*h_p = (ddi_softint_handle_t)hdlp;
10690Sstevel@tonic-gate 	rw_exit(&hdlp->ih_rwlock);
10700Sstevel@tonic-gate 	return (ret);
10710Sstevel@tonic-gate }
10720Sstevel@tonic-gate 
10730Sstevel@tonic-gate /*
10740Sstevel@tonic-gate  * Remove the soft interrupt
10750Sstevel@tonic-gate  */
10760Sstevel@tonic-gate int
10770Sstevel@tonic-gate ddi_intr_remove_softint(ddi_softint_handle_t h)
10780Sstevel@tonic-gate {
10790Sstevel@tonic-gate 	ddi_softint_hdl_impl_t	*hdlp = (ddi_softint_hdl_impl_t *)h;
10800Sstevel@tonic-gate 
10810Sstevel@tonic-gate 	DDI_INTR_APIDBG((CE_CONT, "ddi_intr_remove_softint: hdlp = %p\n",
10820Sstevel@tonic-gate 	    (void *)hdlp));
10830Sstevel@tonic-gate 
10840Sstevel@tonic-gate 	if (hdlp == NULL)
10850Sstevel@tonic-gate 		return (DDI_EINVAL);
10860Sstevel@tonic-gate 
10870Sstevel@tonic-gate 	rw_enter(&hdlp->ih_rwlock, RW_WRITER);
10880Sstevel@tonic-gate 	i_ddi_remove_softint(hdlp);
10890Sstevel@tonic-gate 	rw_exit(&hdlp->ih_rwlock);
10900Sstevel@tonic-gate 	rw_destroy(&hdlp->ih_rwlock);
10910Sstevel@tonic-gate 
10920Sstevel@tonic-gate 	/* kmem_free the hdl impl_t structure allocated earlier */
10930Sstevel@tonic-gate 	kmem_free(hdlp, sizeof (ddi_softint_hdl_impl_t));
10940Sstevel@tonic-gate 	return (DDI_SUCCESS);
10950Sstevel@tonic-gate }
10960Sstevel@tonic-gate 
10970Sstevel@tonic-gate /*
10980Sstevel@tonic-gate  * Trigger a soft interrupt
10990Sstevel@tonic-gate  */
11000Sstevel@tonic-gate int
11010Sstevel@tonic-gate ddi_intr_trigger_softint(ddi_softint_handle_t h, void *arg2)
11020Sstevel@tonic-gate {
11030Sstevel@tonic-gate 	ddi_softint_hdl_impl_t	*hdlp = (ddi_softint_hdl_impl_t *)h;
11040Sstevel@tonic-gate 	int			ret;
11050Sstevel@tonic-gate 
11060Sstevel@tonic-gate 	if (hdlp == NULL)
11070Sstevel@tonic-gate 		return (DDI_EINVAL);
11080Sstevel@tonic-gate 
1109278Sgovinda 	if ((ret = i_ddi_trigger_softint(hdlp, arg2)) != DDI_SUCCESS) {
11100Sstevel@tonic-gate 		DDI_INTR_APIDBG((CE_CONT, "ddi_intr_trigger_softint: failed, "
11110Sstevel@tonic-gate 		    " ret 0%x\n", ret));
1112278Sgovinda 
1113278Sgovinda 		return (ret);
11140Sstevel@tonic-gate 	}
11150Sstevel@tonic-gate 
1116278Sgovinda 	hdlp->ih_cb_arg2 = arg2;
1117278Sgovinda 	return (DDI_SUCCESS);
11180Sstevel@tonic-gate }
11190Sstevel@tonic-gate 
11200Sstevel@tonic-gate /*
11210Sstevel@tonic-gate  * Get the soft interrupt priority
11220Sstevel@tonic-gate  */
11230Sstevel@tonic-gate int
11240Sstevel@tonic-gate ddi_intr_get_softint_pri(ddi_softint_handle_t h, uint_t *soft_prip)
11250Sstevel@tonic-gate {
11260Sstevel@tonic-gate 	ddi_softint_hdl_impl_t	*hdlp = (ddi_softint_hdl_impl_t *)h;
11270Sstevel@tonic-gate 
11280Sstevel@tonic-gate 	DDI_INTR_APIDBG((CE_CONT, "ddi_intr_get_softint_pri: h = %p\n",
11290Sstevel@tonic-gate 	    (void *)h));
11300Sstevel@tonic-gate 
11310Sstevel@tonic-gate 	if (hdlp == NULL)
11320Sstevel@tonic-gate 		return (DDI_EINVAL);
11330Sstevel@tonic-gate 
11340Sstevel@tonic-gate 	rw_enter(&hdlp->ih_rwlock, RW_READER);
11350Sstevel@tonic-gate 	*soft_prip = hdlp->ih_pri;
11360Sstevel@tonic-gate 	rw_exit(&hdlp->ih_rwlock);
11370Sstevel@tonic-gate 	return (DDI_SUCCESS);
11380Sstevel@tonic-gate }
11390Sstevel@tonic-gate 
11400Sstevel@tonic-gate /*
11410Sstevel@tonic-gate  * Set the soft interrupt priority
11420Sstevel@tonic-gate  */
11430Sstevel@tonic-gate int
11440Sstevel@tonic-gate ddi_intr_set_softint_pri(ddi_softint_handle_t h, uint_t soft_pri)
11450Sstevel@tonic-gate {
11460Sstevel@tonic-gate 	ddi_softint_hdl_impl_t	*hdlp = (ddi_softint_hdl_impl_t *)h;
11470Sstevel@tonic-gate 	int			ret;
11480Sstevel@tonic-gate 	uint_t			orig_soft_pri;
11490Sstevel@tonic-gate 
11500Sstevel@tonic-gate 	DDI_INTR_APIDBG((CE_CONT, "ddi_intr_set_softint_pri: h = %p\n",
11510Sstevel@tonic-gate 	    (void *)h));
11520Sstevel@tonic-gate 
11530Sstevel@tonic-gate 	if (hdlp == NULL)
11540Sstevel@tonic-gate 		return (DDI_EINVAL);
11550Sstevel@tonic-gate 
11560Sstevel@tonic-gate 	/* Validate priority argument */
11570Sstevel@tonic-gate 	if (soft_pri < DDI_INTR_SOFTPRI_MIN ||
11580Sstevel@tonic-gate 	    soft_pri > DDI_INTR_SOFTPRI_MAX) {
11590Sstevel@tonic-gate 		DDI_INTR_APIDBG((CE_CONT, "ddi_intr_set_softint_pri: invalid "
11600Sstevel@tonic-gate 		    "soft_pri input given  = %x\n", soft_pri));
11610Sstevel@tonic-gate 		return (DDI_EINVAL);
11620Sstevel@tonic-gate 	}
11630Sstevel@tonic-gate 
11640Sstevel@tonic-gate 	rw_enter(&hdlp->ih_rwlock, RW_WRITER);
11650Sstevel@tonic-gate 	orig_soft_pri = hdlp->ih_pri;
11660Sstevel@tonic-gate 	hdlp->ih_pri = soft_pri;
11670Sstevel@tonic-gate 
11680Sstevel@tonic-gate 	if ((ret = i_ddi_set_softint_pri(hdlp, orig_soft_pri)) != DDI_SUCCESS) {
11690Sstevel@tonic-gate 		DDI_INTR_APIDBG((CE_CONT, "ddi_intr_set_softint_pri: failed, "
11700Sstevel@tonic-gate 		    " ret 0%x\n", ret));
11710Sstevel@tonic-gate 		hdlp->ih_pri = orig_soft_pri;
11720Sstevel@tonic-gate 	}
11730Sstevel@tonic-gate 
11740Sstevel@tonic-gate 	rw_exit(&hdlp->ih_rwlock);
11750Sstevel@tonic-gate 	return (ret);
11760Sstevel@tonic-gate }
11770Sstevel@tonic-gate 
11780Sstevel@tonic-gate /*
11790Sstevel@tonic-gate  * Old DDI interrupt framework
1180693Sgovinda  *
1181693Sgovinda  * The following DDI interrupt interfaces are obsolete.
1182693Sgovinda  * Use the above new DDI interrupt interfaces instead.
11830Sstevel@tonic-gate  */
11840Sstevel@tonic-gate 
11850Sstevel@tonic-gate int
11860Sstevel@tonic-gate ddi_intr_hilevel(dev_info_t *dip, uint_t inumber)
11870Sstevel@tonic-gate {
11880Sstevel@tonic-gate 	ddi_intr_handle_t	hdl, *existing_hdlp;
11890Sstevel@tonic-gate 	int			actual, ret;
11900Sstevel@tonic-gate 	uint_t			high_pri, pri;
11910Sstevel@tonic-gate 
11920Sstevel@tonic-gate 	DDI_INTR_APIDBG((CE_CONT, "ddi_intr_hilevel: name=%s%d dip=0x%p "
11930Sstevel@tonic-gate 	    "inum=0x%x\n", ddi_driver_name(dip), ddi_get_instance(dip),
11940Sstevel@tonic-gate 	    (void *)dip, inumber));
11950Sstevel@tonic-gate 
11960Sstevel@tonic-gate 	/*
11970Sstevel@tonic-gate 	 * The device driver may have already registed with the
11980Sstevel@tonic-gate 	 * framework. If so, first try to get the existing interrupt handle
11990Sstevel@tonic-gate 	 * for that given inumber and use that handle.
12000Sstevel@tonic-gate 	 */
12010Sstevel@tonic-gate 	existing_hdlp = i_ddi_get_intr_handle(dip, inumber);
12020Sstevel@tonic-gate 	if (existing_hdlp) {
12030Sstevel@tonic-gate 		hdl = existing_hdlp[0];	/* Use existing handle */
12040Sstevel@tonic-gate 	} else {
12050Sstevel@tonic-gate 		if ((ret = ddi_intr_alloc(dip, &hdl, DDI_INTR_TYPE_FIXED,
1206965Sgovinda 		    inumber, 1, &actual,
1207965Sgovinda 		    DDI_INTR_ALLOC_NORMAL)) != DDI_SUCCESS) {
12080Sstevel@tonic-gate 			DDI_INTR_APIDBG((CE_CONT, "ddi_intr_hilevel: "
12090Sstevel@tonic-gate 			    "ddi_intr_alloc failed, ret 0x%x\n", ret));
12100Sstevel@tonic-gate 			return (0);
12110Sstevel@tonic-gate 		}
12120Sstevel@tonic-gate 	}
12130Sstevel@tonic-gate 
12140Sstevel@tonic-gate 	if ((ret = ddi_intr_get_pri(hdl, &pri)) != DDI_SUCCESS) {
12150Sstevel@tonic-gate 		DDI_INTR_APIDBG((CE_CONT, "ddi_intr_hilevel: "
12160Sstevel@tonic-gate 		    "ddi_intr_get_pri failed, ret 0x%x\n", ret));
12170Sstevel@tonic-gate 		(void) ddi_intr_free(hdl);
12180Sstevel@tonic-gate 		return (0);
12190Sstevel@tonic-gate 	}
12200Sstevel@tonic-gate 
12210Sstevel@tonic-gate 	high_pri = ddi_intr_get_hilevel_pri();
12220Sstevel@tonic-gate 
12230Sstevel@tonic-gate 	DDI_INTR_APIDBG((CE_CONT, "ddi_intr_hilevel: pri = %x, "
12240Sstevel@tonic-gate 	    "high_pri = %x\n", pri, high_pri));
12250Sstevel@tonic-gate 
12260Sstevel@tonic-gate 	/* Free the handle allocated here only if no existing handle exists */
12270Sstevel@tonic-gate 	if (existing_hdlp == NULL)
12280Sstevel@tonic-gate 		(void) ddi_intr_free(hdl);
12290Sstevel@tonic-gate 
12300Sstevel@tonic-gate 	return (pri >= high_pri);
12310Sstevel@tonic-gate }
12320Sstevel@tonic-gate 
12330Sstevel@tonic-gate int
12340Sstevel@tonic-gate ddi_dev_nintrs(dev_info_t *dip, int *result)
12350Sstevel@tonic-gate {
12360Sstevel@tonic-gate 	DDI_INTR_APIDBG((CE_CONT, "ddi_dev_nintrs: name=%s%d dip=0x%p\n",
12370Sstevel@tonic-gate 	    ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip));
12380Sstevel@tonic-gate 
12390Sstevel@tonic-gate 	if (ddi_intr_get_nintrs(dip, DDI_INTR_TYPE_FIXED,
12400Sstevel@tonic-gate 	    result) != DDI_SUCCESS) {
12410Sstevel@tonic-gate 		DDI_INTR_APIDBG((CE_CONT, "ddi_dev_nintrs: "
12420Sstevel@tonic-gate 		    "ddi_intr_get_nintrs failed\n"));
12430Sstevel@tonic-gate 		*result = 0;
12440Sstevel@tonic-gate 	}
12450Sstevel@tonic-gate 
12460Sstevel@tonic-gate 	return (DDI_SUCCESS);
12470Sstevel@tonic-gate }
12480Sstevel@tonic-gate 
12490Sstevel@tonic-gate int
12500Sstevel@tonic-gate ddi_get_iblock_cookie(dev_info_t *dip, uint_t inumber,
12510Sstevel@tonic-gate     ddi_iblock_cookie_t *iblock_cookiep)
12520Sstevel@tonic-gate {
12530Sstevel@tonic-gate 	ddi_intr_handle_t	hdl, *existing_hdlp;
12540Sstevel@tonic-gate 	int			actual, ret;
12550Sstevel@tonic-gate 	uint_t			pri;
12560Sstevel@tonic-gate 
12570Sstevel@tonic-gate 	DDI_INTR_APIDBG((CE_CONT, "ddi_get_iblock_cookie: name=%s%d dip=0x%p "
12580Sstevel@tonic-gate 	    "inum=0x%x\n", ddi_driver_name(dip), ddi_get_instance(dip),
12590Sstevel@tonic-gate 	    (void *)dip, inumber));
12600Sstevel@tonic-gate 
12610Sstevel@tonic-gate 	ASSERT(iblock_cookiep != NULL);
12620Sstevel@tonic-gate 
12630Sstevel@tonic-gate 	/*
12640Sstevel@tonic-gate 	 * The device driver may have already registed with the
12650Sstevel@tonic-gate 	 * framework. If so, first try to get the existing interrupt handle
12660Sstevel@tonic-gate 	 * for that given inumber and use that handle.
12670Sstevel@tonic-gate 	 */
12680Sstevel@tonic-gate 	existing_hdlp = i_ddi_get_intr_handle(dip, inumber);
12690Sstevel@tonic-gate 	if (existing_hdlp) {
12700Sstevel@tonic-gate 		hdl = existing_hdlp[0];	/* Use existing handle */
12710Sstevel@tonic-gate 	} else {
12720Sstevel@tonic-gate 		if ((ret = ddi_intr_alloc(dip, &hdl, DDI_INTR_TYPE_FIXED,
1273965Sgovinda 		    inumber, 1, &actual,
1274965Sgovinda 		    DDI_INTR_ALLOC_NORMAL)) != DDI_SUCCESS) {
12750Sstevel@tonic-gate 			DDI_INTR_APIDBG((CE_CONT, "ddi_get_iblock_cookie: "
12760Sstevel@tonic-gate 			    "ddi_intr_alloc failed, ret 0x%x\n", ret));
12770Sstevel@tonic-gate 			return (DDI_INTR_NOTFOUND);
12780Sstevel@tonic-gate 		}
12790Sstevel@tonic-gate 	}
12800Sstevel@tonic-gate 
12810Sstevel@tonic-gate 	if ((ret = ddi_intr_get_pri(hdl, &pri)) != DDI_SUCCESS) {
12820Sstevel@tonic-gate 		DDI_INTR_APIDBG((CE_CONT, "ddi_get_iblock_cookie: "
12830Sstevel@tonic-gate 		    "ddi_intr_get_pri failed, ret 0x%x\n", ret));
12840Sstevel@tonic-gate 
12850Sstevel@tonic-gate 		(void) ddi_intr_free(hdl);
12860Sstevel@tonic-gate 		return (DDI_FAILURE);
12870Sstevel@tonic-gate 	}
12880Sstevel@tonic-gate 
128942Sagiri 	*iblock_cookiep = (ddi_iblock_cookie_t)(uintptr_t)pri;
12900Sstevel@tonic-gate 	/* Free the handle allocated here only if no existing handle exists */
12910Sstevel@tonic-gate 	if (existing_hdlp == NULL)
12920Sstevel@tonic-gate 		(void) ddi_intr_free(hdl);
12930Sstevel@tonic-gate 
12940Sstevel@tonic-gate 	return (DDI_SUCCESS);
12950Sstevel@tonic-gate }
12960Sstevel@tonic-gate 
12970Sstevel@tonic-gate int
12980Sstevel@tonic-gate ddi_add_intr(dev_info_t *dip, uint_t inumber,
12990Sstevel@tonic-gate     ddi_iblock_cookie_t *iblock_cookiep,
13000Sstevel@tonic-gate     ddi_idevice_cookie_t *idevice_cookiep,
13010Sstevel@tonic-gate     uint_t (*int_handler)(caddr_t int_handler_arg),
13020Sstevel@tonic-gate     caddr_t int_handler_arg)
13030Sstevel@tonic-gate {
13040Sstevel@tonic-gate 	ddi_intr_handle_t	*hdl_p;
13050Sstevel@tonic-gate 	int			actual, ret;
13060Sstevel@tonic-gate 	uint_t			pri;
13070Sstevel@tonic-gate 
13080Sstevel@tonic-gate 	DDI_INTR_APIDBG((CE_CONT, "ddi_add_intr: name=%s%d dip=0x%p "
13090Sstevel@tonic-gate 	    "inum=0x%x\n", ddi_driver_name(dip), ddi_get_instance(dip),
13100Sstevel@tonic-gate 	    (void *)dip, inumber));
13110Sstevel@tonic-gate 
13120Sstevel@tonic-gate 	hdl_p = kmem_zalloc(sizeof (ddi_intr_handle_t), KM_SLEEP);
13130Sstevel@tonic-gate 
13140Sstevel@tonic-gate 	if ((ret = ddi_intr_alloc(dip, hdl_p, DDI_INTR_TYPE_FIXED,
1315965Sgovinda 	    inumber, 1, &actual, DDI_INTR_ALLOC_NORMAL)) != DDI_SUCCESS) {
13160Sstevel@tonic-gate 		DDI_INTR_APIDBG((CE_CONT, "ddi_add_intr: "
13170Sstevel@tonic-gate 		    "ddi_intr_alloc failed, ret 0x%x\n", ret));
13180Sstevel@tonic-gate 		kmem_free(hdl_p, sizeof (ddi_intr_handle_t));
13190Sstevel@tonic-gate 		return (DDI_INTR_NOTFOUND);
13200Sstevel@tonic-gate 	}
13210Sstevel@tonic-gate 
13220Sstevel@tonic-gate 	if ((ret = ddi_intr_get_pri(hdl_p[0], &pri)) != DDI_SUCCESS)  {
13230Sstevel@tonic-gate 		DDI_INTR_APIDBG((CE_CONT, "ddi_add_intr: "
13240Sstevel@tonic-gate 		    "ddi_intr_get_pri failed, ret 0x%x\n", ret));
13250Sstevel@tonic-gate 		(void) ddi_intr_free(hdl_p[0]);
13260Sstevel@tonic-gate 		kmem_free(hdl_p, sizeof (ddi_intr_handle_t));
13270Sstevel@tonic-gate 		return (DDI_FAILURE);
13280Sstevel@tonic-gate 	}
13290Sstevel@tonic-gate 
13300Sstevel@tonic-gate 	if ((ret = ddi_intr_add_handler(hdl_p[0], (ddi_intr_handler_t *)
13310Sstevel@tonic-gate 	    int_handler, int_handler_arg, NULL)) != DDI_SUCCESS) {
13320Sstevel@tonic-gate 		DDI_INTR_APIDBG((CE_CONT, "ddi_add_intr: "
13330Sstevel@tonic-gate 		    "ddi_intr_add_handler failed, ret 0x%x\n", ret));
13340Sstevel@tonic-gate 		(void) ddi_intr_free(hdl_p[0]);
13350Sstevel@tonic-gate 		kmem_free(hdl_p, sizeof (ddi_intr_handle_t));
13360Sstevel@tonic-gate 		return (DDI_FAILURE);
13370Sstevel@tonic-gate 	}
13380Sstevel@tonic-gate 
13390Sstevel@tonic-gate 	if ((ret = ddi_intr_enable(hdl_p[0])) != DDI_SUCCESS) {
13400Sstevel@tonic-gate 		DDI_INTR_APIDBG((CE_CONT, "ddi_add_intr: "
13410Sstevel@tonic-gate 		    "ddi_intr_enable failed, ret 0x%x\n", ret));
13420Sstevel@tonic-gate 		(void) ddi_intr_remove_handler(hdl_p[0]);
13430Sstevel@tonic-gate 		(void) ddi_intr_free(hdl_p[0]);
13440Sstevel@tonic-gate 		kmem_free(hdl_p, sizeof (ddi_intr_handle_t));
13450Sstevel@tonic-gate 		return (DDI_FAILURE);
13460Sstevel@tonic-gate 	}
13470Sstevel@tonic-gate 
13480Sstevel@tonic-gate 	if (iblock_cookiep)
134942Sagiri 		*iblock_cookiep = (ddi_iblock_cookie_t)(uintptr_t)pri;
13500Sstevel@tonic-gate 
13510Sstevel@tonic-gate 	if (idevice_cookiep) {
13520Sstevel@tonic-gate 		idevice_cookiep->idev_vector = 0;
13530Sstevel@tonic-gate 		idevice_cookiep->idev_priority = pri;
13540Sstevel@tonic-gate 	}
13550Sstevel@tonic-gate 
13560Sstevel@tonic-gate 	return (DDI_SUCCESS);
13570Sstevel@tonic-gate }
13580Sstevel@tonic-gate 
13590Sstevel@tonic-gate /* ARGSUSED */
13600Sstevel@tonic-gate int
13610Sstevel@tonic-gate ddi_add_fastintr(dev_info_t *dip, uint_t inumber,
13620Sstevel@tonic-gate     ddi_iblock_cookie_t *iblock_cookiep,
13630Sstevel@tonic-gate     ddi_idevice_cookie_t *idevice_cookiep,
13640Sstevel@tonic-gate     uint_t (*hi_int_handler)(void))
13650Sstevel@tonic-gate {
13660Sstevel@tonic-gate 	DDI_INTR_APIDBG((CE_CONT, "ddi_add_fastintr: name=%s%d dip=0x%p "
13670Sstevel@tonic-gate 	    "inum=0x%x: Not supported, return failure\n", ddi_driver_name(dip),
13680Sstevel@tonic-gate 	    ddi_get_instance(dip), (void *)dip, inumber));
13690Sstevel@tonic-gate 
13700Sstevel@tonic-gate 	return (DDI_FAILURE);
13710Sstevel@tonic-gate }
13720Sstevel@tonic-gate 
13730Sstevel@tonic-gate /* ARGSUSED */
13740Sstevel@tonic-gate void
13750Sstevel@tonic-gate ddi_remove_intr(dev_info_t *dip, uint_t inum, ddi_iblock_cookie_t iblock_cookie)
13760Sstevel@tonic-gate {
13770Sstevel@tonic-gate 	ddi_intr_handle_t	*hdl_p;
13780Sstevel@tonic-gate 	int			ret;
13790Sstevel@tonic-gate 
13800Sstevel@tonic-gate 	DDI_INTR_APIDBG((CE_CONT, "ddi_remove_intr: name=%s%d dip=0x%p "
13810Sstevel@tonic-gate 	    "inum=0x%x\n", ddi_driver_name(dip), ddi_get_instance(dip),
13820Sstevel@tonic-gate 	    (void *)dip, inum));
13830Sstevel@tonic-gate 
13840Sstevel@tonic-gate 	if ((hdl_p = i_ddi_get_intr_handle(dip, inum)) == NULL) {
13850Sstevel@tonic-gate 		DDI_INTR_APIDBG((CE_CONT, "ddi_remove_intr: no handle "
13860Sstevel@tonic-gate 		    "found\n"));
13870Sstevel@tonic-gate 		return;
13880Sstevel@tonic-gate 	}
13890Sstevel@tonic-gate 
13900Sstevel@tonic-gate 	if ((ret = ddi_intr_disable(hdl_p[0])) != DDI_SUCCESS) {
13910Sstevel@tonic-gate 		DDI_INTR_APIDBG((CE_CONT, "ddi_remove_intr: "
13920Sstevel@tonic-gate 		    "ddi_intr_disable failed, ret 0x%x\n", ret));
13930Sstevel@tonic-gate 		return;
13940Sstevel@tonic-gate 	}
13950Sstevel@tonic-gate 
13960Sstevel@tonic-gate 	if ((ret = ddi_intr_remove_handler(hdl_p[0])) != DDI_SUCCESS) {
13970Sstevel@tonic-gate 		DDI_INTR_APIDBG((CE_CONT, "ddi_remove_intr: "
13980Sstevel@tonic-gate 		    "ddi_intr_remove_handler failed, ret 0x%x\n", ret));
13990Sstevel@tonic-gate 		return;
14000Sstevel@tonic-gate 	}
14010Sstevel@tonic-gate 
14020Sstevel@tonic-gate 	if ((ret = ddi_intr_free(hdl_p[0])) != DDI_SUCCESS) {
14030Sstevel@tonic-gate 		DDI_INTR_APIDBG((CE_CONT, "ddi_remove_intr: "
14040Sstevel@tonic-gate 		    "ddi_intr_free failed, ret 0x%x\n", ret));
14050Sstevel@tonic-gate 		return;
14060Sstevel@tonic-gate 	}
14070Sstevel@tonic-gate 
14080Sstevel@tonic-gate 	kmem_free(hdl_p, sizeof (ddi_intr_handle_t));
14090Sstevel@tonic-gate }
14100Sstevel@tonic-gate 
14110Sstevel@tonic-gate /* ARGSUSED */
14120Sstevel@tonic-gate int
14130Sstevel@tonic-gate ddi_get_soft_iblock_cookie(dev_info_t *dip, int preference,
14140Sstevel@tonic-gate     ddi_iblock_cookie_t *iblock_cookiep)
14150Sstevel@tonic-gate {
14160Sstevel@tonic-gate 	DDI_INTR_APIDBG((CE_CONT, "ddi_get_soft_iblock_cookie: name=%s%d "
14170Sstevel@tonic-gate 	    "dip=0x%p pref=0x%x\n", ddi_driver_name(dip), ddi_get_instance(dip),
14180Sstevel@tonic-gate 	    (void *)dip, preference));
14190Sstevel@tonic-gate 
14200Sstevel@tonic-gate 	ASSERT(iblock_cookiep != NULL);
14210Sstevel@tonic-gate 
14220Sstevel@tonic-gate 	if (preference == DDI_SOFTINT_FIXED)
14230Sstevel@tonic-gate 		return (DDI_FAILURE);
14240Sstevel@tonic-gate 
142542Sagiri 	*iblock_cookiep = (ddi_iblock_cookie_t)((uintptr_t)
14260Sstevel@tonic-gate 	    ((preference > DDI_SOFTINT_MED) ? DDI_SOFT_INTR_PRI_H :
14270Sstevel@tonic-gate 	    DDI_SOFT_INTR_PRI_M));
14280Sstevel@tonic-gate 
14290Sstevel@tonic-gate 	return (DDI_SUCCESS);
14300Sstevel@tonic-gate }
14310Sstevel@tonic-gate 
14320Sstevel@tonic-gate int
14330Sstevel@tonic-gate ddi_add_softintr(dev_info_t *dip, int preference, ddi_softintr_t *idp,
14340Sstevel@tonic-gate     ddi_iblock_cookie_t *iblock_cookiep,
14350Sstevel@tonic-gate     ddi_idevice_cookie_t *idevice_cookiep,
14360Sstevel@tonic-gate     uint_t (*int_handler)(caddr_t int_handler_arg),
14370Sstevel@tonic-gate     caddr_t int_handler_arg)
14380Sstevel@tonic-gate {
14390Sstevel@tonic-gate 	ddi_softint_handle_t	*hdl_p;
14400Sstevel@tonic-gate 	uint64_t		softpri;
14410Sstevel@tonic-gate 	int			ret;
14420Sstevel@tonic-gate 
14430Sstevel@tonic-gate 	DDI_INTR_APIDBG((CE_CONT, "ddi_add_softintr: name=%s%d dip=0x%p "
14440Sstevel@tonic-gate 	    "pref=0x%x\n", ddi_driver_name(dip), ddi_get_instance(dip),
14450Sstevel@tonic-gate 	    (void *)dip, preference));
14460Sstevel@tonic-gate 
14470Sstevel@tonic-gate 	if ((idp == NULL) || ((preference == DDI_SOFTINT_FIXED) &&
14480Sstevel@tonic-gate 	    (iblock_cookiep == NULL)))
14490Sstevel@tonic-gate 		return (DDI_FAILURE);
14500Sstevel@tonic-gate 
14510Sstevel@tonic-gate 	/* Translate the priority preference */
14520Sstevel@tonic-gate 	if (preference == DDI_SOFTINT_FIXED) {
1453190Seota 		softpri = (uint64_t)(uintptr_t)*iblock_cookiep;
14540Sstevel@tonic-gate 		softpri = MIN(softpri, DDI_SOFT_INTR_PRI_H);
14550Sstevel@tonic-gate 	} else {
14560Sstevel@tonic-gate 		softpri = (uint64_t)((preference > DDI_SOFTINT_MED) ?
14570Sstevel@tonic-gate 		    DDI_SOFT_INTR_PRI_H : DDI_SOFT_INTR_PRI_M);
14580Sstevel@tonic-gate 	}
14590Sstevel@tonic-gate 
14600Sstevel@tonic-gate 	DDI_INTR_APIDBG((CE_CONT, "ddi_add_softintr: preference 0x%x "
14610Sstevel@tonic-gate 	    "softpri 0x%lx\n", preference, (long)softpri));
14620Sstevel@tonic-gate 
14630Sstevel@tonic-gate 	hdl_p = kmem_zalloc(sizeof (ddi_softint_handle_t), KM_SLEEP);
14640Sstevel@tonic-gate 	if ((ret = ddi_intr_add_softint(dip, hdl_p, softpri,
14650Sstevel@tonic-gate 	    (ddi_intr_handler_t *)int_handler, int_handler_arg)) !=
14660Sstevel@tonic-gate 	    DDI_SUCCESS) {
14670Sstevel@tonic-gate 		DDI_INTR_APIDBG((CE_CONT, "ddi_add_softintr: "
14680Sstevel@tonic-gate 		    "ddi_intr_add_softint failed, ret 0x%x\n", ret));
14690Sstevel@tonic-gate 
14700Sstevel@tonic-gate 		kmem_free(hdl_p, sizeof (ddi_softint_handle_t));
14710Sstevel@tonic-gate 		return (DDI_FAILURE);
14720Sstevel@tonic-gate 	}
14730Sstevel@tonic-gate 
14740Sstevel@tonic-gate 	if (iblock_cookiep)
1475190Seota 		*iblock_cookiep =  (ddi_iblock_cookie_t)(uintptr_t)softpri;
14760Sstevel@tonic-gate 
14770Sstevel@tonic-gate 	if (idevice_cookiep) {
14780Sstevel@tonic-gate 		idevice_cookiep->idev_vector = 0;
14790Sstevel@tonic-gate 		idevice_cookiep->idev_priority = softpri;
14800Sstevel@tonic-gate 	}
14810Sstevel@tonic-gate 
14820Sstevel@tonic-gate 	*idp = (ddi_softintr_t)hdl_p;
14830Sstevel@tonic-gate 
14840Sstevel@tonic-gate 	DDI_INTR_APIDBG((CE_CONT, "ddi_add_softintr: dip = 0x%p, "
14850Sstevel@tonic-gate 	    "idp = 0x%p, ret = %x\n", (void *)dip, (void *)*idp, ret));
14860Sstevel@tonic-gate 
14870Sstevel@tonic-gate 	return (DDI_SUCCESS);
14880Sstevel@tonic-gate }
14890Sstevel@tonic-gate 
14900Sstevel@tonic-gate void
14910Sstevel@tonic-gate ddi_remove_softintr(ddi_softintr_t id)
14920Sstevel@tonic-gate {
14930Sstevel@tonic-gate 	ddi_softint_handle_t	*h_p = (ddi_softint_handle_t *)id;
14940Sstevel@tonic-gate 
14950Sstevel@tonic-gate 	DDI_INTR_APIDBG((CE_CONT, "ddi_remove_softintr: id=0x%p\n",
14960Sstevel@tonic-gate 	    (void *)id));
14970Sstevel@tonic-gate 
14980Sstevel@tonic-gate 	if (h_p == NULL)
14990Sstevel@tonic-gate 		return;
15000Sstevel@tonic-gate 
15010Sstevel@tonic-gate 	DDI_INTR_APIDBG((CE_CONT, "ddi_remove_softintr: handle 0x%p\n",
15020Sstevel@tonic-gate 	    (void *)h_p));
15030Sstevel@tonic-gate 
15040Sstevel@tonic-gate 	(void) ddi_intr_remove_softint(*h_p);
15050Sstevel@tonic-gate 	kmem_free(h_p, sizeof (ddi_softint_handle_t));
15060Sstevel@tonic-gate }
15070Sstevel@tonic-gate 
15080Sstevel@tonic-gate void
15090Sstevel@tonic-gate ddi_trigger_softintr(ddi_softintr_t id)
15100Sstevel@tonic-gate {
15110Sstevel@tonic-gate 	ddi_softint_handle_t	*h_p = (ddi_softint_handle_t *)id;
15120Sstevel@tonic-gate 	int			ret;
15130Sstevel@tonic-gate 
15140Sstevel@tonic-gate 	if (h_p == NULL)
15150Sstevel@tonic-gate 		return;
15160Sstevel@tonic-gate 
15170Sstevel@tonic-gate 	if ((ret = ddi_intr_trigger_softint(*h_p, NULL)) != DDI_SUCCESS) {
15180Sstevel@tonic-gate 		DDI_INTR_APIDBG((CE_CONT, "ddi_trigger_softintr: "
15190Sstevel@tonic-gate 		    "ddi_intr_trigger_softint failed, hdlp 0x%p "
15200Sstevel@tonic-gate 		    "ret 0x%x\n", (void *)h_p, ret));
15210Sstevel@tonic-gate 	}
15220Sstevel@tonic-gate }
1523